Upgrade to Pro — share decks privately, control downloads, hide ads and more …

EC-CUBE3のプラグインを作ってみよう!

amidaike
September 27, 2015

 EC-CUBE3のプラグインを作ってみよう!

amidaike

September 27, 2015
Tweet

More Decks by amidaike

Other Decks in Programming

Transcript

  1. EC-CUBE3ʹ͍ͭͯ σΟϨΫτϦ໊ ػೳ໊ Eccube௚Լ ϕʔεͱͳΔApplication.php͕ଘࡏ͠ɺඞ ͣ͜ͷΫϥε͔Β࣮ߦ͞ΕΔ Application Application.phpͷ਌ΫϥεϑΝΠϧ͕֨ೲ Command ConsoleίϚϯυ༻Ϋϥε

    Common ఆ਺ఆٛΫϥε Controller ίϯτϩʔϥʔΫϥε ControllerProvider URLϚοϐϯάఆٛؔ਺ Doctrine Doctirine֦ுΫϥε Entity DB࿈ܞ༻EntityΫϥε Event FormΠϕϯτ༻Ϋϥε EventListner ΠϕϯτϦεφʔ༻Ϋϥε
  2. EC-CUBE3ʹ͍ͭͯ σΟϨΫτϦ໊ ػೳ໊ Exception ۀ຿ΤϥʔΫϥε Form FormλΠϓΫϥε Plugin ϓϥάΠϯ༻ManagerΫϥε Repository

    DBΞΫηε༻ϦϙδτϦΫϥε Resource doctrine༻dcmϑΝΠϧ΍twigϑΝΠϧ౳ Security ύεϫʔυϋογϡ༻Ϋϥε Service αʔϏεΫϥε ServiceProvider DIఆٛ༻Ϋϥε Twig Twig֦ு༻Ϋϥε Util ڞ௨ؔ਺Ϋϥε
  3. ϓϥάΠϯΛ࡞ͬͯΈΑ͏ 1. config.ymlͷهड़ํ๏ 2. event.ymlͷهड़ํ๏ 3. PluginManagerͷهड़ํ๏ 4. Eventͷهड़ํ๏ 5.

    ServiceProviderͷهड़ํ๏ 6. ຊମʹ߹ΘͤͨσΟϨΫτϦߏ଄ 7. ϓϥάΠϯ༻؅ཧը໘ͷ࡞੒ํ๏ 8. طଘը໘ͷFormʹ߲໨Λ௥Ճ͢Δํ๏ 9. ؅ཧը໘ʹϝχϡʔΛ௥Ճ͢Δํ๏(࣍ճػձ͕͋Ε͹͓ݟͤ͠·͢)
  4. config.yml name: GiftWrappingϓϥάΠϯ ※ඞਢ code: GiftWrapping ※ඞਢ version: 1.0.0 ※ඞਢ

    service: - GiftWrappingServiceProvider orm.path: - /Resource/doctrine ※ϓϥάΠϯͰ௥Ճͨ͠ςʔϒϧΛ࡞੒͢Δ৔߹ɺorm.pathͷهड़͸ඞਢ event: GiftWrappingEvent ※eventΛ௥Ճ͢Δ৔߹ɺeventͷهड़͸ඞਢ form: - onPreSubmit: SampleForm ※FormΠϕϯτΛ֦ு͢Δ৔߹ɺformͷهड़͸ඞਢ ·ͨɺඞཁͰ͋Ε͹ const: HOGE: 1 HOGEHOGE: 2 ͱఆ਺΋هड़Մೳɺୠ͠ݱόʔδϣϯͩͱϓϥάΠϯ͕ແޮͷ৔߹ɺ͜ͷఆ਺͕ಡΈࠐΊͳ͍
  5. event.yml eccube.event.render.shopping.before: - [onRenderGiftWrappingShoppingBefore, NORMAL] eccube.event.render.admin_setting_shop_payment_edit.before: - [onRenderGiftWrappingAdminPaymentBefore, NORMAL] Πϕϯτͷछྨʹ͸

    ΞϓϦέʔγϣϯશମͷϑοΫϙΠϯτ eccube.event.app.{before | after} ίϯτϩʔϥʔΠϕϯτͷϑοΫϙΠϯτ eccube.event.controller.{ϧʔςΟϯά໊}.{before | after | finish} ϨϯμʔΠϕϯτͷϑοΫϙΠϯτ eccube.event.render.{ϧʔςΟϯά໊}.before
  6. ϧʔςΟϯάͷ֬ೝํ๏ $ php app/console router:debug +----------------------------------------+-----------------------------------------------------------+---------+ | Name | Path

    | Pattern | +----------------------------------------+-----------------------------------------------------------+---------+ | admin_logout | /admin/logout | GET | | admin_login_check | /admin/login_check | | | logout | /logout | GET | | login_check | /login_check | | | user_data | /user_data/{route} | ANY | | homepage | / | ANY | | top | / | ANY | | index | / | ANY | | block | /block | ANY | | cart | /cart | ANY | | cart_add | /cart/add | POST | | cart_up | /cart/up/{productClassId} | ANY | | cart_down | /cart/down/{productClassId} | ANY | | cart_set_quantity | /cart/setQuantity/{productClassId}/{quantity} | ANY | | cart_remove | /cart/remove/{productClassId} | ANY | | cart_buystep | /cart/buystep | ANY | | contact | /contact | ANY | | contact_complete | /contact/complete | ANY | | entry | /entry | ANY | | entry_complete | /entry/complete | ANY | | entry_activate | /entry/activate/{secret_key} | ANY | | forgot | /forgot | ANY | | forgot_reset | /forgot/reset/{reset_key} | ANY | | block_category | /block/category | ANY | | block_cart | /block/cart | ANY | | block_search_product | /block/search_product | ANY | | block_news | /block/news | ANY | | block_login | /block/login | ANY | | help_about | /help/about | ANY | | help_guide | /help/guide | ANY | | help_privacy | /help/privacy | ANY | | help_tradelaw | /help/tradelaw | ANY | | help_agreement | /help/agreement | ANY |
  7. PluginManagerͷهड़ <?php namespace Plugin\PayPalExpress; class PluginManager extends AbstractPluginManager { public

    function install($config, $app) { $this->migrationSchema($app, __DIR__ . '/Resource/doctrine/migration', $config['code']); } public function uninstall($config, $app) { $this->migrationSchema($app, __DIR__ . '/Resource/doctrine/migration', $config['code'], 0); } public function enable($config, $app) { } public function disable($config, $app) { } public function update($config, $app) { } }
  8. Eventͷهड़ <?php namespace Plugin\GiftWrapping; class GiftWrappingEvent { private $app; public

    function __construct($app) { $this->app = $app; } /** * ߪೖը໘ʹϥοϐϯά߲໨Λදࣔ * * @param FilterResponseEvent $event */ public function onRenderGiftWrappingShoppingBefore(FilterResponseEvent $event) { $request = $event->getRequest(); $response = $event->getResponse(); $Wrapping = $this->app['eccube.plugin.repository.wrapping']->find(1); if ($Wrapping->getIsWrapping()) { $html = $this->getHtmlWrapping($request, $response); $response->setContent($html); } $event->setResponse($response); } /** * * @param Request $request
  9. ServiceProviderͷهड़ <?php namespace Plugin\GiftWrapping\ServiceProvider; class GiftWrappingServiceProvider implements ServiceProviderInterface { public

    function register(BaseApplication $app) { // ؅ཧը໘ $app->match('/' . $app['config']['admin_route'] . '/plugin/giftwrapping/config', 'Plugin\GiftWrapping\Controller\ConfigController::index')- >bind('plugin_GiftWrapping_config'); $app->match('/plugin/giftwrapping/checkout', 'Plugin\GiftWrapping\Controller\GiftWrappingController::index')->bind('plugin_giftwrapping_index'); // Form $app['form.types'] = $app->share($app->extend('form.types', function ($types) use ($app) { $types[] = new GiftWrappingConfigType($app); return $types; })); // Form Extension $app['form.type.extensions'] = $app->share($app->extend('form.type.extensions', function ($extensions) use ($app) { $extensions[] = new ShoppingTypeExtension($app); return $extensions; })); // Repository $app['eccube.plugin.repository.wrapping'] = $app->share(function () use ($app) { return $app['orm.em']->getRepository('Plugin\GiftWrapping\Entity\Wrapping'); }); // Service $app['eccube.plugin.service.gift_wrapping'] = $app->share(function () use ($app) { return new \Plugin\GiftWrapping\Service\GiftWrappingService($app); }); // ϝοηʔδొ࿥ $app['translator'] = $app->share($app->extend('translator', function ($translator, \Silex\Application $app) {
  10. ϓϥάΠϯ༻؅ཧը໘ͷهड़ class SampleServiceProvider implements ServiceProviderInterface { public function register(BaseApplication $app)

    { // ؅ཧը໘ $app->match('/' . $app['config']['admin_route'] . '/plugin/giftwrapping/config', 'Plugin \GiftWrapping\Controller\ConfigController::index')->bind('plugin_GiftWrapping_config'); // Form $app['form.types'] = $app->share($app->extend('form.types', function ($types) use ($app) { $types[] = new GiftWrappingConfigType($app); return $types; })); } public function boot(BaseApplication $app) { } }
  11. ରԠϒϥ΢βʹ͍ͭͯ OS ϒϥ΢β Windows (Windows7Ҏ߱) Internet Explorer10 Ҏ߱ Google Chrome

    ࠷৽൛ Firefox ࠷৽൛ Mac Safari ࠷৽൛ iOS(7Ҏ߱) Safari ࠷৽൛ Android(4.1Ҏ߱) ඪ४ϒϥ΢β ࠷৽൛
  12. σόοάϞʔυͷ༗ޮԽ • ௨ৗ / ΍/index.phpͰΞΫηε͍ͯ͠Δͱ͜ΖΛ /index_dev.php ͱॻ͖׵͑ͯΞΫηε͢Δ͜ͱʹΑΓɺtwigϑΝΠϧ͕Ωϟο γϡ͞ΕͣʹσόοάϞʔυ͕༗ޮʹͳΓ·͢ɻ
 
 http://localhost/index_dev.php

    • You are not allowed to access this file. Check index_dev.php for more information.ͱΤϥʔ͕දࣔ͞ΕΔ৔߹ɺindex_dev.phpΛ ։͖ɺΞΫηεݩͷIPΛҎԼͷ഑ྻʹ௥Ճ͍ͯͩ͘͠͞ɻ 
 $allow = array( '127.0.0.1', 'fe80::1', '::1', );