diff --git a/.scenarios.lock/symfony2/composer.json b/.scenarios.lock/symfony2/composer.json index fb53d5fe9..1b6a1c18b 100644 --- a/.scenarios.lock/symfony2/composer.json +++ b/.scenarios.lock/symfony2/composer.json @@ -25,17 +25,17 @@ "require": { "symfony/console": "^2.8", "php": ">=5.5.0", - "league/container": "^2.2", - "consolidation/log": "~1", + "consolidation/annotated-command": "^2.11.0", "consolidation/config": "^1.0.10", - "consolidation/annotated-command": "^2.10.2", + "consolidation/log": "^1.1.1", "consolidation/output-formatters": "^3.1.13", "consolidation/self-update": "^1", "grasmash/yaml-expander": "^1.3", - "symfony/finder": "^2.5|^3|^4", - "symfony/process": "^2.5|^3|^4", + "league/container": "^2.2", + "symfony/event-dispatcher": "^2.5|^3|^4", "symfony/filesystem": "^2.5|^3|^4", - "symfony/event-dispatcher": "^2.5|^3|^4" + "symfony/finder": "^2.5|^3|^4", + "symfony/process": "^2.5|^3|^4" }, "require-dev": { "g1a/composer-test-scenarios": "^3", diff --git a/.scenarios.lock/symfony4/composer.json b/.scenarios.lock/symfony4/composer.json index f789595f4..231b0b91c 100644 --- a/.scenarios.lock/symfony4/composer.json +++ b/.scenarios.lock/symfony4/composer.json @@ -25,17 +25,17 @@ "require": { "symfony/console": "^4", "php": ">=5.5.0", - "league/container": "^2.2", - "consolidation/log": "~1", + "consolidation/annotated-command": "^2.11.0", "consolidation/config": "^1.0.10", - "consolidation/annotated-command": "^2.10.2", + "consolidation/log": "^1.1.1", "consolidation/output-formatters": "^3.1.13", "consolidation/self-update": "^1", "grasmash/yaml-expander": "^1.3", - "symfony/finder": "^2.5|^3|^4", - "symfony/process": "^2.5|^3|^4", + "league/container": "^2.2", + "symfony/event-dispatcher": "^2.5|^3|^4", "symfony/filesystem": "^2.5|^3|^4", - "symfony/event-dispatcher": "^2.5|^3|^4" + "symfony/finder": "^2.5|^3|^4", + "symfony/process": "^2.5|^3|^4" }, "require-dev": { "g1a/composer-test-scenarios": "^3", diff --git a/.scenarios.lock/symfony4/composer.lock b/.scenarios.lock/symfony4/composer.lock index 45ce682f3..52801b63e 100644 --- a/.scenarios.lock/symfony4/composer.lock +++ b/.scenarios.lock/symfony4/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f152a6c19524f672f3900c4d4fe623d6", + "content-hash": "8ef0fbf15c7ab6e6a7e91096f1e1ed60", "packages": [ { "name": "consolidation/annotated-command", - "version": "2.10.2", + "version": "2.11.0", "source": { "type": "git", "url": "https://github.com/consolidation/annotated-command.git", - "reference": "5cbb8c320e0d3d2e6905374d56dc3610b7443f7b" + "reference": "edea407f57104ed518cc3c3b47d5b84403ee267a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/consolidation/annotated-command/zipball/5cbb8c320e0d3d2e6905374d56dc3610b7443f7b", - "reference": "5cbb8c320e0d3d2e6905374d56dc3610b7443f7b", + "url": "https://api.github.com/repos/consolidation/annotated-command/zipball/edea407f57104ed518cc3c3b47d5b84403ee267a", + "reference": "edea407f57104ed518cc3c3b47d5b84403ee267a", "shasum": "" }, "require": { @@ -100,7 +100,7 @@ } ], "description": "Initialize Symfony Console commands from annotated command class methods.", - "time": "2018-12-21T03:50:15+00:00" + "time": "2018-12-29T04:43:17+00:00" }, { "name": "consolidation/config", @@ -158,31 +158,72 @@ }, { "name": "consolidation/log", - "version": "1.0.6", + "version": "1.1.1", "source": { "type": "git", "url": "https://github.com/consolidation/log.git", - "reference": "dfd8189a771fe047bf3cd669111b2de5f1c79395" + "reference": "b2e887325ee90abc96b0a8b7b474cd9e7c896e3a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/consolidation/log/zipball/dfd8189a771fe047bf3cd669111b2de5f1c79395", - "reference": "dfd8189a771fe047bf3cd669111b2de5f1c79395", + "url": "https://api.github.com/repos/consolidation/log/zipball/b2e887325ee90abc96b0a8b7b474cd9e7c896e3a", + "reference": "b2e887325ee90abc96b0a8b7b474cd9e7c896e3a", "shasum": "" }, "require": { - "php": ">=5.5.0", - "psr/log": "~1.0", + "php": ">=5.4.5", + "psr/log": "^1.0", "symfony/console": "^2.8|^3|^4" }, "require-dev": { - "g1a/composer-test-scenarios": "^1", - "phpunit/phpunit": "4.*", - "satooshi/php-coveralls": "^2", - "squizlabs/php_codesniffer": "2.*" + "g1a/composer-test-scenarios": "^3", + "php-coveralls/php-coveralls": "^1", + "phpunit/phpunit": "^6", + "squizlabs/php_codesniffer": "^2" }, "type": "library", "extra": { + "scenarios": { + "symfony4": { + "require": { + "symfony/console": "^4.0" + }, + "config": { + "platform": { + "php": "7.1.3" + } + } + }, + "symfony2": { + "require": { + "symfony/console": "^2.8" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.36" + }, + "remove": [ + "php-coveralls/php-coveralls" + ], + "config": { + "platform": { + "php": "5.4.8" + } + } + }, + "phpunit4": { + "require-dev": { + "phpunit/phpunit": "^4.8.36" + }, + "remove": [ + "php-coveralls/php-coveralls" + ], + "config": { + "platform": { + "php": "5.4.8" + } + } + } + }, "branch-alias": { "dev-master": "1.x-dev" } @@ -203,7 +244,7 @@ } ], "description": "Improved Psr-3 / Psr\\Log logger based on Symfony Console components.", - "time": "2018-05-25T18:14:39+00:00" + "time": "2019-01-01T17:30:51+00:00" }, { "name": "consolidation/output-formatters", @@ -1378,21 +1419,21 @@ }, { "name": "codeception/phpunit-wrapper", - "version": "6.0.12", + "version": "6.0.13", "source": { "type": "git", "url": "https://github.com/Codeception/phpunit-wrapper.git", - "reference": "de922c760dfddf8a33c4a066efcd0cd93576d957" + "reference": "d25db254173582bc27aa8c37cb04ce2481675bcd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Codeception/phpunit-wrapper/zipball/de922c760dfddf8a33c4a066efcd0cd93576d957", - "reference": "de922c760dfddf8a33c4a066efcd0cd93576d957", + "url": "https://api.github.com/repos/Codeception/phpunit-wrapper/zipball/d25db254173582bc27aa8c37cb04ce2481675bcd", + "reference": "d25db254173582bc27aa8c37cb04ce2481675bcd", "shasum": "" }, "require": { "phpunit/php-code-coverage": ">=4.0.4 <6.0", - "phpunit/phpunit": ">=5.7.27 <7.0", + "phpunit/phpunit": ">=5.7.27 <6.5.13", "sebastian/comparator": ">=1.2.4 <3.0", "sebastian/diff": ">=1.4 <4.0" }, @@ -1420,7 +1461,7 @@ } ], "description": "PHPUnit classes used by Codeception", - "time": "2018-09-12T20:19:47+00:00" + "time": "2019-01-01T15:39:52+00:00" }, { "name": "codeception/stub", @@ -4079,20 +4120,21 @@ }, { "name": "webmozart/assert", - "version": "1.3.0", + "version": "1.4.0", "source": { "type": "git", "url": "https://github.com/webmozart/assert.git", - "reference": "0df1908962e7a3071564e857d86874dad1ef204a" + "reference": "83e253c8e0be5b0257b881e1827274667c5c17a9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webmozart/assert/zipball/0df1908962e7a3071564e857d86874dad1ef204a", - "reference": "0df1908962e7a3071564e857d86874dad1ef204a", + "url": "https://api.github.com/repos/webmozart/assert/zipball/83e253c8e0be5b0257b881e1827274667c5c17a9", + "reference": "83e253c8e0be5b0257b881e1827274667c5c17a9", "shasum": "" }, "require": { - "php": "^5.3.3 || ^7.0" + "php": "^5.3.3 || ^7.0", + "symfony/polyfill-ctype": "^1.8" }, "require-dev": { "phpunit/phpunit": "^4.6", @@ -4125,7 +4167,7 @@ "check", "validate" ], - "time": "2018-01-29T19:49:41+00:00" + "time": "2018-12-25T11:19:39+00:00" } ], "aliases": [], diff --git a/LICENSE b/LICENSE index 605e7ebe3..0938cc28e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014-2018 Codegyre Developers Team, Consolidation Team +Copyright (c) 2014-2019 Codegyre Developers Team, Consolidation Team Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in @@ -22,9 +22,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. DEPENDENCY LICENSES: Name Version License -consolidation/annotated-command 2.10.2 MIT +consolidation/annotated-command 2.11.0 MIT consolidation/config 1.1.1 MIT -consolidation/log 1.0.6 MIT +consolidation/log 1.1.1 MIT consolidation/output-formatters 3.4.0 MIT consolidation/self-update 1.1.5 MIT container-interop/container-interop 1.2.0 MIT diff --git a/composer.json b/composer.json index 34a89417e..5fbc953ec 100644 --- a/composer.json +++ b/composer.json @@ -22,18 +22,18 @@ "bin":["robo"], "require": { "php": ">=5.5.0", - "league/container": "^2.2", - "consolidation/log": "~1", + "consolidation/annotated-command": "^2.11.0", "consolidation/config": "^1.0.10", - "consolidation/annotated-command": "^2.10.2", + "consolidation/log": "^1.1.1", "consolidation/output-formatters": "^3.1.13", "consolidation/self-update": "^1", "grasmash/yaml-expander": "^1.3", - "symfony/finder": "^2.5|^3|^4", + "league/container": "^2.2", "symfony/console": "^2.8|^3|^4", - "symfony/process": "^2.5|^3|^4", + "symfony/event-dispatcher": "^2.5|^3|^4", "symfony/filesystem": "^2.5|^3|^4", - "symfony/event-dispatcher": "^2.5|^3|^4" + "symfony/finder": "^2.5|^3|^4", + "symfony/process": "^2.5|^3|^4" }, "require-dev": { "g1a/composer-test-scenarios": "^3", diff --git a/composer.lock b/composer.lock index 6537187d8..ffacd1162 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c87f64d18ac46a6166aa8e2bd6c1a841", + "content-hash": "7585c852d8f6e6f1fef3b9c3ed4768c9", "packages": [ { "name": "consolidation/annotated-command", - "version": "2.10.2", + "version": "2.11.0", "source": { "type": "git", "url": "https://github.com/consolidation/annotated-command.git", - "reference": "5cbb8c320e0d3d2e6905374d56dc3610b7443f7b" + "reference": "edea407f57104ed518cc3c3b47d5b84403ee267a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/consolidation/annotated-command/zipball/5cbb8c320e0d3d2e6905374d56dc3610b7443f7b", - "reference": "5cbb8c320e0d3d2e6905374d56dc3610b7443f7b", + "url": "https://api.github.com/repos/consolidation/annotated-command/zipball/edea407f57104ed518cc3c3b47d5b84403ee267a", + "reference": "edea407f57104ed518cc3c3b47d5b84403ee267a", "shasum": "" }, "require": { @@ -100,7 +100,7 @@ } ], "description": "Initialize Symfony Console commands from annotated command class methods.", - "time": "2018-12-21T03:50:15+00:00" + "time": "2018-12-29T04:43:17+00:00" }, { "name": "consolidation/config", @@ -158,31 +158,72 @@ }, { "name": "consolidation/log", - "version": "1.0.6", + "version": "1.1.1", "source": { "type": "git", "url": "https://github.com/consolidation/log.git", - "reference": "dfd8189a771fe047bf3cd669111b2de5f1c79395" + "reference": "b2e887325ee90abc96b0a8b7b474cd9e7c896e3a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/consolidation/log/zipball/dfd8189a771fe047bf3cd669111b2de5f1c79395", - "reference": "dfd8189a771fe047bf3cd669111b2de5f1c79395", + "url": "https://api.github.com/repos/consolidation/log/zipball/b2e887325ee90abc96b0a8b7b474cd9e7c896e3a", + "reference": "b2e887325ee90abc96b0a8b7b474cd9e7c896e3a", "shasum": "" }, "require": { - "php": ">=5.5.0", - "psr/log": "~1.0", + "php": ">=5.4.5", + "psr/log": "^1.0", "symfony/console": "^2.8|^3|^4" }, "require-dev": { - "g1a/composer-test-scenarios": "^1", - "phpunit/phpunit": "4.*", - "satooshi/php-coveralls": "^2", - "squizlabs/php_codesniffer": "2.*" + "g1a/composer-test-scenarios": "^3", + "php-coveralls/php-coveralls": "^1", + "phpunit/phpunit": "^6", + "squizlabs/php_codesniffer": "^2" }, "type": "library", "extra": { + "scenarios": { + "symfony4": { + "require": { + "symfony/console": "^4.0" + }, + "config": { + "platform": { + "php": "7.1.3" + } + } + }, + "symfony2": { + "require": { + "symfony/console": "^2.8" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.36" + }, + "remove": [ + "php-coveralls/php-coveralls" + ], + "config": { + "platform": { + "php": "5.4.8" + } + } + }, + "phpunit4": { + "require-dev": { + "phpunit/phpunit": "^4.8.36" + }, + "remove": [ + "php-coveralls/php-coveralls" + ], + "config": { + "platform": { + "php": "5.4.8" + } + } + } + }, "branch-alias": { "dev-master": "1.x-dev" } @@ -203,7 +244,7 @@ } ], "description": "Improved Psr-3 / Psr\\Log logger based on Symfony Console components.", - "time": "2018-05-25T18:14:39+00:00" + "time": "2019-01-01T17:30:51+00:00" }, { "name": "consolidation/output-formatters", @@ -1365,21 +1406,21 @@ }, { "name": "codeception/phpunit-wrapper", - "version": "6.0.12", + "version": "6.0.13", "source": { "type": "git", "url": "https://github.com/Codeception/phpunit-wrapper.git", - "reference": "de922c760dfddf8a33c4a066efcd0cd93576d957" + "reference": "d25db254173582bc27aa8c37cb04ce2481675bcd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Codeception/phpunit-wrapper/zipball/de922c760dfddf8a33c4a066efcd0cd93576d957", - "reference": "de922c760dfddf8a33c4a066efcd0cd93576d957", + "url": "https://api.github.com/repos/Codeception/phpunit-wrapper/zipball/d25db254173582bc27aa8c37cb04ce2481675bcd", + "reference": "d25db254173582bc27aa8c37cb04ce2481675bcd", "shasum": "" }, "require": { "phpunit/php-code-coverage": ">=4.0.4 <6.0", - "phpunit/phpunit": ">=5.7.27 <7.0", + "phpunit/phpunit": ">=5.7.27 <6.5.13", "sebastian/comparator": ">=1.2.4 <3.0", "sebastian/diff": ">=1.4 <4.0" }, @@ -1407,7 +1448,7 @@ } ], "description": "PHPUnit classes used by Codeception", - "time": "2018-09-12T20:19:47+00:00" + "time": "2019-01-01T15:39:52+00:00" }, { "name": "codeception/stub", @@ -4057,20 +4098,21 @@ }, { "name": "webmozart/assert", - "version": "1.3.0", + "version": "1.4.0", "source": { "type": "git", "url": "https://github.com/webmozart/assert.git", - "reference": "0df1908962e7a3071564e857d86874dad1ef204a" + "reference": "83e253c8e0be5b0257b881e1827274667c5c17a9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webmozart/assert/zipball/0df1908962e7a3071564e857d86874dad1ef204a", - "reference": "0df1908962e7a3071564e857d86874dad1ef204a", + "url": "https://api.github.com/repos/webmozart/assert/zipball/83e253c8e0be5b0257b881e1827274667c5c17a9", + "reference": "83e253c8e0be5b0257b881e1827274667c5c17a9", "shasum": "" }, "require": { - "php": "^5.3.3 || ^7.0" + "php": "^5.3.3 || ^7.0", + "symfony/polyfill-ctype": "^1.8" }, "require-dev": { "phpunit/phpunit": "^4.6", @@ -4103,7 +4145,7 @@ "check", "validate" ], - "time": "2018-01-29T19:49:41+00:00" + "time": "2018-12-25T11:19:39+00:00" } ], "aliases": [], diff --git a/examples/src/Robo/Plugin/Commands/ExampleCommands.php b/examples/src/Robo/Plugin/Commands/ExampleCommands.php index 23ce7d249..bbad27574 100644 --- a/examples/src/Robo/Plugin/Commands/ExampleCommands.php +++ b/examples/src/Robo/Plugin/Commands/ExampleCommands.php @@ -9,6 +9,7 @@ use Consolidation\OutputFormatters\StructuredData\PropertyList; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Example Robo Plugin Commands. @@ -170,24 +171,32 @@ public function tryArrayArgs(array $a, array $options = ['foo' => []]) } /** - * Demonstrate use of Symfony $input object in Robo in place of - * the usual "parameter arguments". + * Demonstrate use of SymfonyStyle $io object and Symfony $input object in + * Robo in place of the usual "parameter arguments". * * @arg array $a A list of commandline parameters. * @option foo * @default a [] * @default foo [] */ - public function trySymfony(InputInterface $input) + public function trySymfony(SymfonyStyle $io, InputInterface $input) { + $io->title('Symfony Style demo'); $a = $input->getArgument('a'); - $this->say("The parameters passed are:\n" . var_export($a, true)); + $io->writeln("The parameters passed are:\n" . var_export($a, true)); $foo = $input->getOption('foo'); if (!empty($foo)) { $this->say("The options passed via --foo are:\n" . var_export($foo, true)); } } + public function tryText() + { + $this->io()->text('This is some text'); + $this->io()->text('This is some more text'); + $this->io()->text('This is the last text'); + } + /** * Demonstrate Robo boolean options. * diff --git a/src/Application.php b/src/Application.php index 6e9bc0dcd..000ca9422 100644 --- a/src/Application.php +++ b/src/Application.php @@ -1,13 +1,22 @@ getName(), $this->getVersion(), $repository); $this->add($selfUpdateCommand); } + + protected function configureIO(InputInterface $input, OutputInterface $output) + { + parent::configureIO($input, $output); + $this->resetIO($input, $output); + if ($this->logger instanceof \Consolidation\Log\LoggerManager) { + $this->logger->add('default', $this->createLogger($output)); + } + } + + protected function createLogger($output) + { + return new \Robo\Log\RoboLogger($output); + } } diff --git a/src/Common/IO.php b/src/Common/IO.php index d6c77bff8..fb0dd87e9 100644 --- a/src/Common/IO.php +++ b/src/Common/IO.php @@ -1,20 +1,58 @@ ioStorage = $ioStorage; + } + + public function resetIO(InputInterface $input, OutputInterface $output) + { + if (!$this->ioStorage) { + $this->ioStorage = new IOStorage(); + } + $this->ioStorage->create($input, $output); + } + + protected function output() + { + $result = null; + if ($this->ioStorage) { + $result = $this->ioStorage->output(); + } + return $result ?: $this->parentOutput(); + } + + protected function input() + { + $result = null; + if ($this->ioStorage) { + $result = $this->ioStorage->input(); + } + return $result ?: $this->parentInput(); + } /** * Provide access to SymfonyStyle object. @@ -25,10 +63,10 @@ trait IO */ protected function io() { - if (!$this->io) { - $this->io = new SymfonyStyle($this->input(), $this->output()); + if (!$this->ioStorage) { + $this->ioStorage = new IOStorage(); } - return $this->io; + return $this->ioStorage->get($this->input, $this->output); } /** diff --git a/src/Contract/IOAwareInterface.php b/src/Contract/IOAwareInterface.php index 2daa5fb64..e1874f419 100644 --- a/src/Contract/IOAwareInterface.php +++ b/src/Contract/IOAwareInterface.php @@ -6,8 +6,13 @@ namespace Robo\Contract; -use \Symfony\Component\Console\Input\InputAwareInterface; +use Robo\Symfony\IOStorage; +use Symfony\Component\Console\Input\InputAwareInterface; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; interface IOAwareInterface extends OutputAwareInterface, InputAwareInterface { + public function setIOStorage(IOStorage $ioStorage); + public function resetIO(InputInterface $input, OutputInterface $output); } diff --git a/src/Robo.php b/src/Robo.php index a4ead5d9c..7daf45df7 100644 --- a/src/Robo.php +++ b/src/Robo.php @@ -150,19 +150,29 @@ public static function createDefaultContainer($input = null, $output = null, $ap // Set up our dependency injection container. $container = new Container(); static::configureContainer($container, $app, $config, $input, $output, $classLoader); + static::configureApplication($app, $container); + return $container; + } + + public static function configureApplication($app, $container) + { // Set the application dispatcher $app->setDispatcher($container->get('eventDispatcher')); - return $container; + if ($app instanceof \Robo\Contract\IOAwareInterface) { + $app->setIOStorage($container->get('ioStorage')); + } + if ($app instanceof \Psr\Log\LoggerAwareInterface) { + $app->setLogger($container->get('logger')); + } } /** * Initialize a container with all of the default Robo services. * IMPORTANT: after calling this method, clients MUST call: * - * $dispatcher = $container->get('eventDispatcher'); - * $app->setDispatcher($dispatcher); + * \Robo\Robo::configureApplication($app, $container); * * Any modification to the container should be done prior to fetching * objects from it. @@ -207,9 +217,12 @@ public static function configureContainer(ContainerInterface $container, Symfony // Register logging and related services. $container->share('logStyler', \Robo\Log\RoboLogStyle::class); - $container->share('logger', \Robo\Log\RoboLogger::class) - ->withArgument('output') - ->withMethodCall('setLogOutputStyler', ['logStyler']); + $container->share('roboLogger', \Robo\Log\RoboLogger::class) + ->withMethodCall('setLogOutputStyler', ['logStyler']) + ->withArgument('output'); + $container->share('logger', \Consolidation\Log\LoggerManager::class) + ->withMethodCall('setLogOutputStyler', ['logStyler']) + ->withMethodCall('fallbackLogger', ['roboLogger']); $container->add('progressBar', \Symfony\Component\Console\Helper\ProgressBar::class) ->withArgument('output'); $container->share('progressIndicator', \Robo\Common\ProgressIndicator::class) @@ -237,10 +250,14 @@ public static function configureContainer(ContainerInterface $container, Symfony ->withMethodCall('addDefaultSimplifiers', []); $container->share('prepareTerminalWidthOption', \Consolidation\AnnotatedCommand\Options\PrepareTerminalWidthOption::class) ->withMethodCall('setApplication', ['application']); + $container->share('symfonyStyleInjector', \Robo\Symfony\SymfonyStyleInjector::class); + $container->share('parameterInjection', \Consolidation\AnnotatedCommand\ParameterInjection::class) + ->withMethodCall('register', ['Symfony\Component\Console\Style\SymfonyStyle', 'symfonyStyleInjector']); $container->share('commandProcessor', \Consolidation\AnnotatedCommand\CommandProcessor::class) ->withArgument('hookManager') ->withMethodCall('setFormatterManager', ['formatterManager']) ->withMethodCall('addPrepareFormatter', ['prepareTerminalWidthOption']) + ->withMethodCall('setParameterInjection', ['parameterInjection']) ->withMethodCall( 'setDisplayErrorFunction', [ @@ -250,6 +267,7 @@ function ($output, $message) use ($container) { } ] ); + $container->share('ioStorage', \Robo\Symfony\IOStorage::class); $container->share('stdinHandler', \Consolidation\AnnotatedCommand\Input\StdinHandler::class); $container->share('commandFactory', \Consolidation\AnnotatedCommand\AnnotatedCommandFactory::class) ->withMethodCall('setCommandProcessor', ['commandProcessor']); @@ -298,6 +316,8 @@ public static function addInflectors($container) ->invokeMethod('setLogger', ['logger']); $container->inflector(\League\Container\ContainerAwareInterface::class) ->invokeMethod('setContainer', ['container']); + $container->inflector(\Robo\Symfony\IOAwareInterface::class) + ->invokeMethod('setIOStorage', ['ioStorage']); $container->inflector(\Symfony\Component\Console\Input\InputAwareInterface::class) ->invokeMethod('setInput', ['input']); $container->inflector(\Robo\Contract\OutputAwareInterface::class) diff --git a/src/Symfony/IOStorage.php b/src/Symfony/IOStorage.php new file mode 100644 index 000000000..c64a907c1 --- /dev/null +++ b/src/Symfony/IOStorage.php @@ -0,0 +1,127 @@ +input = null; + $this->output = null; + $this->io = null; + } + + /** + * setStyleClass sets a new style class to use. + * + * @param string $styleClass Name of class to instantiate when style object + * is requested. Must be a subclass of SymfonyStyle. + */ + public function setStyleClass($styleClass) + { + $this->styleClass = $styleClass; + $this->recreate(); + } + + /** + * hasStyle indicates whether there is a cached style available + * + * @return bool + */ + public function hasStyle() + { + return !empty($this->io); + } + + /** + * create will instantiate a new style instance, replacing what was + * there before. + * + * @param InputInterface $input + * @param OutputInterface $output + * @return SymfonyStyle + */ + public function create(InputInterface $input, OutputInterface $output) + { + $this->input = $input; + $this->output = $output; + return $this->instantiate(); + } + + /** + * recreate will make a new style object iff we have cached $input + * and $output objects. Otherwise it clears the cached style object. + */ + protected function recreate() + { + if (!empty($this->input) && !empty($this->output)) { + return $this->instantiate(); + } + $this->io = null; + return null; + } + + /** + * instantiate will make a new style object from the cached input and + * output objects. + */ + protected function instantiate() + { + $this->io = new $this->styleClass($this->input, $this->output); + return $this->cached(); + } + + /** + * get will return the cached style object, if it exists; otherwise, + * it will create and cache a new style object using the provided + * input and output objects. + * + * @param InputInterface $input + * @param OutputInterface $output + * @return SymfonyStyle + */ + public function get(InputInterface $input, OutputInterface $output) + { + if (!$this->hasStyle()) { + $this->create($input, $output); + } + return $this->cached(); + } + + /** + * cached returns the cached style object, or null if none is available. + * + * @return SymfonyStyle|null + */ + public function cached() + { + return $this->io; + } +} diff --git a/src/Symfony/SymfonyStyleInjector.php b/src/Symfony/SymfonyStyleInjector.php new file mode 100644 index 000000000..1324f9f95 --- /dev/null +++ b/src/Symfony/SymfonyStyleInjector.php @@ -0,0 +1,15 @@ +input(), $commandData->output()); + } +}