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

Composerプラグインを作ってみよう /phpcon2016

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
Hiraku NAKANO
November 03, 2016

Composerプラグインを作ってみよう /phpcon2016

PHPカンファレンス2016 トラック1 の発表資料です。

Hiraku NAKANO

November 03, 2016

Transcript

93 slides
  1. $ composer ______ / ____/___ ____ ___ ____ ____ ________

    _____ / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/ Composer version 1.1.2 2016-05-31 19:48:11 Usage: command [options] [arguments] Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question --profile Display timing and memory usage information --no-plugins Whether to disable plugins. -d, --working-dir=WORKING-DIR If specified, use the given directory as working directory. -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
  2. Α͘࢖͏αϒίϚϯυ 対話形式でプロジェクトをセットアップ $ composer init ライブラリの追加 $ composer require 'vendor/package'

    ライブラリのアップデート $ composer update composer.jsonに従ってまとめてインストール $ composer install
  3. composer.json͕͜͏ͳΔ "require": { "fxp/composer-asset-plugin": "^1.2", "npm-asset/jquery": "2.2.0", "npm-asset/bootstrap": "3.3.7" }

    • npm-asset/xxxx͸packagistʹొ࿥͞Ε͍ͯͳ͍ɻ
 npmΛΫϩʔϧͯ͠औͬͯ͘Δ • vendor/npm-asset ΍ vendor/bower-asset ΁഑ஔ͞ΕΔ • node.jsͳ͠Ͱasset؅ཧ͕Ͱ͖ͯศརʂ
  4. scriptsͱ͸ • composer.jsonʹscriptsͱ͍͏ϓϩύςΟΛ࡞Δ • ೚ҙͷεΫϦϓτΛ࣮ߦͰ͖Δ • ΠϕϯτϑοΫͰࣗಈ࣮ߦ / खಈ࣮ߦ •

    ରԠ͢ΔΠϕϯτ͕ͳ͚Ε͹ɺcomposerͷαϒίϚϯυ ʹͳΔ • https://getcomposer.org/doc/articles/scripts.md
  5. ͍͍ͩͨ͜Μͳײ͡ { "scripts": { "test": "phpunit", "postinstall": "rm -rf ./cache",

    "migrate": "App\Db\Migration::run" } } WFOEPSCJOʹ1"5) ͕௨ͬͯΔͷͰݺ΂Δ γΣϧεΫϦϓτͰ΋ 0, BVUPMPBE͕௨ͬͯΕ͹1)1ͷ TUBUJDϝιουͰ΋ॻ͚Δ
  6. ݺͼग़͠ $ composer test $ composer migrate $ composer install

    # postinstallが自動発動 $ composer run postinstall # 明示発動
  7. ιʔείʔυͷ··composer Λಈ͔͢ $ git clone git://github.com/composer/composer.git $ cd composer $

    php path/to/composer.phar install --no-dev $ bin/composer --version Composer version @package_branch_alias_version@ (@package_version@) @release_date@
  8. ιʔείʔυͷ··composer Λಈ͔͢ $ git clone git://github.com/composer/composer.git $ cd composer $

    php path/to/composer.phar install --no-dev $ bin/composer --version Composer version @package_branch_alias_version@ (@package_version@) @release_date@
  9. ϓϥάΠϯͷcomposerϓϩ δΣΫτΛ࡞Δ • $ composer init • type Λ composer-plugin

    ʹ͢Δ • require ʹ composer-plugin-api ΛೖΕΔɻ࠷৽൛^1.0Ͱྑ͍ • Ϣχοτςετ΋ॻ͖͍ͨͳΒɺrequire-devʹ"composer/ composer"ΛՃ͑Δ • extra.classʹىಈΫϥε໊Λొ࿥
  10. composer.jsonͷྫ { "name": "my/plugin-package", "type": "composer-plugin", "require": { "composer-plugin-api": "^1.0"

    }, "require-dev": { "composer/composer": "1.2.0", "phpunit/phpunit": "^5.3" }, "autoload": { "psr-4": {"My\\": "src/"} }, "extra": { "class": "My\\Plugin" } }
  11. pathͰpluginΛrequire͢Δ { "name": "my/somepj", "type": "library", "repositories": [ { "type":

    "path", "url": "../plugin-package" } ], "require": { "my/plugin-package": "@dev" } }
  12. <?php namespace My; use Composer\Composer; use Composer\IO\IOInterface; use Composer\Plugin\PluginInterface; class

    Plugin implements PluginInterface { private $composer; private $io; public function activate(Composer $composer, IOInterface $io) { $this->composer = $composer; $this->io = $io; } } جຊܕ
  13. <?php namespace My; use Composer\Composer; use Composer\IO\IOInterface; use Composer\Plugin\PluginInterface; use

    Composer\EventDispatcher\EventSubscriberInterface; use Composer\EventDispatcher\Event; class Plugin implements PluginInterface, EventSubscriberInterface { private $composer; private $io; public function activate(Composer $composer, IOInterface $io) { $this->composer = $composer; $this->io = $io; } public static function getSubscribedEvents() { return array( 'post-autoload-dump' => 'onPostAutoloadDump', ); } public function onPostAutoloadDump(Event $ev) { $this->io->write('onPostAutoloadDump'); } } Πϕϯτϋϯυϥܕ ΠϯλʔϑΣʔεΛҰͭ௥Ճ ϑοΫ͢ΔΠϕϯτΛྻڍ ࣮ߦ͞ΕΔॲཧͷຊମ
  14. <?php namespace My; use Composer\Composer; use Composer\IO\IOInterface; use Composer\Plugin\PluginInterface; use

    Composer\Plugin\Capable; class Plugin implements PluginInterface, Capable { private $composer; private $info; public function activate(Composer $composer, IOInterface $io) { $this->composer = $composer; $this->io = $io; } public function getCapabilities() { return array( 'Composer\Plugin\Capability\CommandProvider' => 'My\Commands', ); } } αϒίϚϯυܕ ΠϯλʔϑΣʔεΛҰͭ௥Ճ $PNNBOEҰཡΛ
 ද͢ΫϥεΛઃఆ
  15. <?php namespace My; use Composer\Plugin\Capability\CommandProvider; class Commands implements CommandProvider {

    public function getCommands() { return array( new HelloCommand ); } } αϒίϚϯυܕ(2) ഑ྻͰίϚϯυΛ
 ྻڍ
  16. <?php namespace My; use Composer\Command\BaseCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class

    HelloCommand extends BaseCommand { protected function configure() { $this->setName('hello'); } protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Hello, world!!'); } } αϒίϚϯυܕ(3) TZNGPOZDPOTPMF ͷίϚϯυΛ࡞Δͷͱ ͍͍ͩͨಉ͡
  17. ։ൃத͚ͩॻ͖׵͑ͯ͠·͑ src/Composer/Util/ErrorHandler.php //... public static function register(IOInterface $io = null)

    { // set_error_handler(array(__CLASS__, 'handle')); error_reporting(E_ALL | E_STRICT); self::$io = $io; }
  18. PHPʹ͓͚ΔϓϩάϥϜͷ୯Ґ 1. 1ຕͷεΫϦϓτ 2. require ʹΑΔϑΝΠϧ෼ׂ 3. ؔ਺ / ΫϥεԽ

    4. ύοέʔδ 5. αʔϏε (Microservices) 8FCΞϓϦέʔγϣϯ
 ϑϨʔϜϫʔΫͷର৅ $PNQPTFS͸͜͜ʹ
 ఆٛΛ༩͑ͨ
  19. ͲΜͳෳࡶͳιϑτ΢ΣΞ΋ɺ ύοέʔδͷ֓೦ʹམͱ͠ࠐΊΔ ϝλ QBDLBHF ΂ΜΓ QBDLBHF QBDLBHF ΦϨΦϨ QBDLBHF ద౰

    QBDLBHF ࡶڕ QBDLBHF ࠷ߴ QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF QBDLBHF

More Decks by Hiraku NAKANO

Other Decks in Technology