Skip to content

Commit

Permalink
Remove redundant dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
BoShurik committed Sep 21, 2017
1 parent 3cf5d46 commit 4a4dcd9
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 39 deletions.
20 changes: 16 additions & 4 deletions Command/AcceleratorCacheClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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']) {
Expand Down
110 changes: 97 additions & 13 deletions Composer/ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@
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.
*
* @param $event Event A instance
*/
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)) {
Expand Down Expand Up @@ -52,4 +51,89 @@ public static function clearCache(Event $event)
$event->getIO()->write('<error>'.$e->getMessage().'</error>');
}
}

/**
* @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);
}
}
5 changes: 5 additions & 0 deletions Resources/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
<argument>%accelerator_cache.mode%</argument>
<argument>%accelerator_cache.curl_opts%</argument>
</service>

<service id="accelerator_cache.command" class="SmartCore\Bundle\AcceleratorCacheBundle\Command\AcceleratorCacheClearCommand">
<argument type="service" id="accelerator_cache.clearer" />
<tag name="console.command" />
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion Tests/AcceleratorCacheClearerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @author Kevin Bond <[email protected]>
*/
class AcceleratorCacheClearerTest extends \PHPUnit_Framework_TestCase
class AcceleratorCacheClearerTest extends \PHPUnit\Framework\TestCase
{
public function testClearCache()
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/CacheClearerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* @author Kevin Bond <[email protected]>
*/
class CacheClearerServiceTest extends \PHPUnit_Framework_TestCase
class CacheClearerServiceTest extends \PHPUnit\Framework\TestCase
{
/**
* @expectedException \RuntimeException
Expand Down
30 changes: 13 additions & 17 deletions Tests/Command/AcceleratorCacheClearCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,46 @@
/**
* @author Kevin Bond <[email protected]>
*/
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());
}

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);
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 4a4dcd9

Please sign in to comment.