From 4a4dcd93283f66d5553ba3c48759d97de706ac2c Mon Sep 17 00:00:00 2001 From: BoShurik Date: Thu, 21 Sep 2017 19:13:39 +0300 Subject: [PATCH] Remove redundant dependencies --- Command/AcceleratorCacheClearCommand.php | 20 +++- Composer/ScriptHandler.php | 110 +++++++++++++++--- Resources/services.xml | 5 + Tests/AcceleratorCacheClearerTest.php | 2 +- Tests/CacheClearerServiceTest.php | 2 +- .../AcceleratorCacheClearCommandTest.php | 30 +++-- composer.json | 6 +- 7 files changed, 136 insertions(+), 39 deletions(-) diff --git a/Command/AcceleratorCacheClearCommand.php b/Command/AcceleratorCacheClearCommand.php index b8e23cc..ff7cc57 100644 --- a/Command/AcceleratorCacheClearCommand.php +++ b/Command/AcceleratorCacheClearCommand.php @@ -3,13 +3,26 @@ namespace SmartCore\Bundle\AcceleratorCacheBundle\Command; use SmartCore\Bundle\AcceleratorCacheBundle\AcceleratorCacheClearer; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use SmartCore\Bundle\AcceleratorCacheBundle\CacheClearerService; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -class AcceleratorCacheClearCommand extends ContainerAwareCommand +class AcceleratorCacheClearCommand extends Command { + /** + * @var CacheClearerService + */ + private $cacheClearer; + + public function __construct(CacheClearerService $cacheClearer) + { + parent::__construct(null); + + $this->cacheClearer = $cacheClearer; + } + /** * {@inheritdoc} */ @@ -38,8 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $type = 'cli'; $result = AcceleratorCacheClearer::clearCache($clearUser, $clearOpcode); } else { - $result = $this->getContainer()->get('accelerator_cache.clearer') - ->clearCache($clearUser, $clearOpcode, $input->getOption('auth')); + $result = $this->cacheClearer->clearCache($clearUser, $clearOpcode, $input->getOption('auth')); } if (!$result['success']) { diff --git a/Composer/ScriptHandler.php b/Composer/ScriptHandler.php index 1d7ba1f..f12b46d 100644 --- a/Composer/ScriptHandler.php +++ b/Composer/ScriptHandler.php @@ -3,10 +3,18 @@ namespace SmartCore\Bundle\AcceleratorCacheBundle\Composer; use Composer\Script\Event; -use Sensio\Bundle\DistributionBundle\Composer\ScriptHandler as SymfonyScriptHandler; +use Symfony\Component\Process\PhpExecutableFinder; +use Symfony\Component\Process\Process; -class ScriptHandler extends SymfonyScriptHandler +/** + * @see \Sensio\Bundle\DistributionBundle\Composer\ScriptHandler + */ +class ScriptHandler { + private static $options = array( + 'symfony-bin-dir' => 'app', + ); + /** * Clears the APC/Wincache/Opcache cache. * @@ -14,17 +22,8 @@ class ScriptHandler extends SymfonyScriptHandler */ public static function clearCache(Event $event) { - $options = parent::getOptions($event); - //$consoleDir = parent::getConsoleDir($event, 'clear the PHP Accelerator cache'); - if (isset($options['symfony-bin-dir'])) { - $binDir = $options['symfony-bin-dir']; - } else { - $binDir = $options['symfony-app-dir']; - } - - if (null === $binDir) { - return; - } + $options = self::getOptions($event); + $binDir = $options['symfony-bin-dir']; $opcode = ''; if (array_key_exists('accelerator-cache-opcode', $options)) { @@ -52,4 +51,89 @@ public static function clearCache(Event $event) $event->getIO()->write(''.$e->getMessage().''); } } + + /** + * @param Event $event + * @return array + */ + private static function getOptions(Event $event) + { + $options = array_merge(static::$options, $event->getComposer()->getPackage()->getExtra()); + + $options['process-timeout'] = $event->getComposer()->getConfig()->get('process-timeout'); + + return $options; + } + + /** + * @param Event $event + * @param $consoleDir + * @param $cmd + * @param int $timeout + */ + private static function executeCommand(Event $event, $consoleDir, $cmd, $timeout = 300) + { + $php = escapeshellarg(static::getPhp(false)); + $phpArgs = implode(' ', array_map('escapeshellarg', static::getPhpArguments())); + $console = escapeshellarg($consoleDir.'/console'); + if ($event->getIO()->isDecorated()) { + $console .= ' --ansi'; + } + + $process = new Process($php.($phpArgs ? ' '.$phpArgs : '').' '.$console.' '.$cmd, null, null, null, $timeout); + $process->run(function ($type, $buffer) use ($event) { $event->getIO()->write($buffer, false); }); + if (!$process->isSuccessful()) { + throw new \RuntimeException(sprintf("An error occurred when executing the \"%s\" command:\n\n%s\n\n%s", escapeshellarg($cmd), self::removeDecoration($process->getOutput()), self::removeDecoration($process->getErrorOutput()))); + } + } + + /** + * @param bool $includeArgs + * @return mixed + */ + private static function getPhp($includeArgs = true) + { + $phpFinder = new PhpExecutableFinder(); + if (!$phpPath = $phpFinder->find($includeArgs)) { + throw new \RuntimeException('The php executable could not be found, add it to your PATH environment variable and try again'); + } + + return $phpPath; + } + + /** + * @return array + */ + private static function getPhpArguments() + { + $ini = null; + $arguments = array(); + + $phpFinder = new PhpExecutableFinder(); + if (method_exists($phpFinder, 'findArguments')) { + $arguments = $phpFinder->findArguments(); + } + + if ($env = getenv('COMPOSER_ORIGINAL_INIS')) { + $paths = explode(PATH_SEPARATOR, $env); + $ini = array_shift($paths); + } else { + $ini = php_ini_loaded_file(); + } + + if ($ini) { + $arguments[] = '--php-ini='.$ini; + } + + return $arguments; + } + + /** + * @param $string + * @return mixed + */ + private static function removeDecoration($string) + { + return preg_replace("/\033\[[^m]*m/", '', $string); + } } diff --git a/Resources/services.xml b/Resources/services.xml index 036bd69..73b55a2 100644 --- a/Resources/services.xml +++ b/Resources/services.xml @@ -12,5 +12,10 @@ %accelerator_cache.mode% %accelerator_cache.curl_opts% + + + + + diff --git a/Tests/AcceleratorCacheClearerTest.php b/Tests/AcceleratorCacheClearerTest.php index 4035428..92a10e2 100644 --- a/Tests/AcceleratorCacheClearerTest.php +++ b/Tests/AcceleratorCacheClearerTest.php @@ -7,7 +7,7 @@ /** * @author Kevin Bond */ -class AcceleratorCacheClearerTest extends \PHPUnit_Framework_TestCase +class AcceleratorCacheClearerTest extends \PHPUnit\Framework\TestCase { public function testClearCache() { diff --git a/Tests/CacheClearerServiceTest.php b/Tests/CacheClearerServiceTest.php index ff92ad1..1be4974 100644 --- a/Tests/CacheClearerServiceTest.php +++ b/Tests/CacheClearerServiceTest.php @@ -8,7 +8,7 @@ /** * @author Kevin Bond */ -class CacheClearerServiceTest extends \PHPUnit_Framework_TestCase +class CacheClearerServiceTest extends \PHPUnit\Framework\TestCase { /** * @expectedException \RuntimeException diff --git a/Tests/Command/AcceleratorCacheClearCommandTest.php b/Tests/Command/AcceleratorCacheClearCommandTest.php index 6c1283c..7dd852a 100644 --- a/Tests/Command/AcceleratorCacheClearCommandTest.php +++ b/Tests/Command/AcceleratorCacheClearCommandTest.php @@ -9,28 +9,22 @@ /** * @author Kevin Bond */ -class AcceleratorCacheClearCommandTest extends \PHPUnit_Framework_TestCase +class AcceleratorCacheClearCommandTest extends \PHPUnit\Framework\TestCase { public function testClearCache() { - $cacheClearer = $this->getMock('SmartCore\Bundle\AcceleratorCacheBundle\CacheClearerService', array(), array(), '', false); + $cacheClearer = $this->createCacheClearer(); $cacheClearer->expects($this->once()) ->method('clearCache') ->with(true, true) ->willReturn(array('success' => true, 'message' => 'foobar')); - $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); - $container->expects($this->once()) - ->method('get') - ->with('accelerator_cache.clearer') - ->willReturn($cacheClearer); - - $this->assertContains('(Web) foobar', $this->createCommandTester(array(), $container)->getDisplay()); + $this->assertContains('(Web) foobar', $this->createCommandTester($cacheClearer, array())->getDisplay()); } public function testCliClearUser() { - $commandTester = $this->createCommandTester(array('--cli' => true, '--user' => true)); + $commandTester = $this->createCommandTester($this->createCacheClearer(), array('--cli' => true, '--user' => true)); $this->assertContains('(cli) Clear PHP Accelerator Cache... APC User Cache: success.', $commandTester->getDisplay()); } @@ -38,21 +32,23 @@ public function testCliClearUser() public function testCliClearOpcode() { if (PHP_VERSION_ID >= 50500) { - $this->setExpectedException('\RuntimeException', 'Clear PHP Accelerator Cache... Opcode Cache: failure.'); - $this->createCommandTester(array('--cli' => true, '--opcode' => true)); + $this->expectException('\RuntimeException'); + $this->expectExceptionMessage('Clear PHP Accelerator Cache... Opcode Cache: failure.'); + $this->createCommandTester($this->createCacheClearer(), array('--cli' => true, '--opcode' => true)); } else { $commandTester = $this->createCommandTester(array('--cli' => true, '--opcode' => true)); $this->assertContains('(cli) Clear PHP Accelerator Cache... APC Opcode Cache: success.', $commandTester->getDisplay()); } } - private function createCommandTester(array $options = array(), $container = null) + private function createCacheClearer() { - $command = new AcceleratorCacheClearCommand(); + return $this->createMock('SmartCore\Bundle\AcceleratorCacheBundle\CacheClearerService'); + } - if ($container) { - $command->setContainer($container); - } + private function createCommandTester($cacheClearer, array $options = array()) + { + $command = new AcceleratorCacheClearCommand($cacheClearer); $application = new Application(); $application->add($command); diff --git a/composer.json b/composer.json index 5a899bb..662d956 100644 --- a/composer.json +++ b/composer.json @@ -17,12 +17,12 @@ ], "require": { "symfony/framework-bundle": ">=2.1", - "sensio/distribution-bundle": ">=2.1" + "symfony/console": ">=2.1" }, "require-dev": { "composer/composer": "1.0.*@dev", - "symfony/console": ">=2.1", - "matthiasnoback/symfony-dependency-injection-test": "^0.7.3" + "phpunit/phpunit": "~6.3", + "matthiasnoback/symfony-dependency-injection-test": "~2.1" }, "extra": { "branch-alias": {