|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of CacheTool. |
| 5 | + * |
| 6 | + * (c) Samuel Gordalina <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CacheTool\Command; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Helper\Table; |
| 15 | +use Symfony\Component\Console\Input\InputArgument; |
| 16 | +use Symfony\Component\Console\Input\InputInterface; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | +use Symfony\Component\Finder\Finder; |
| 19 | + |
| 20 | +class OpcacheCompileScriptsCommand extends AbstractCommand |
| 21 | +{ |
| 22 | + /** |
| 23 | + * {@inheritdoc} |
| 24 | + */ |
| 25 | + protected function configure() |
| 26 | + { |
| 27 | + $this |
| 28 | + ->setName('opcache:compile:scripts') |
| 29 | + ->setDescription('Compile scripts from path to the opcode cache') |
| 30 | + ->addArgument('path', InputArgument::REQUIRED) |
| 31 | + ->setHelp(''); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * {@inheritdoc} |
| 36 | + */ |
| 37 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 38 | + { |
| 39 | + $this->ensureExtensionLoaded('Zend OPcache'); |
| 40 | + $path = $input->getArgument('path'); |
| 41 | + |
| 42 | + $info = $this->getCacheTool()->opcache_get_status(true); |
| 43 | + |
| 44 | + if ($info === false) { |
| 45 | + throw new \RuntimeException('opcache_get_status(): No Opcache status info available. Perhaps Opcache is disabled via opcache.enable or opcache.enable_cli?'); |
| 46 | + } |
| 47 | + |
| 48 | + $splFiles = $this->prepareFileList($path); |
| 49 | + |
| 50 | + $table = new Table($output); |
| 51 | + $table |
| 52 | + ->setHeaders(array( |
| 53 | + 'Compiled', |
| 54 | + 'Filename' |
| 55 | + )) |
| 56 | + ->setRows($this->processFilelist($splFiles)) |
| 57 | + ; |
| 58 | + |
| 59 | + $table->render(); |
| 60 | + } |
| 61 | + |
| 62 | + protected function processFileList($splFiles) |
| 63 | + { |
| 64 | + $list = array(); |
| 65 | + |
| 66 | + foreach ($splFiles as $file) { |
| 67 | + $list[] = array( |
| 68 | + $this->getCacheTool()->opcache_compile_file($file->getRealPath()), |
| 69 | + $file->getRealPath() |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + return $list; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param string $path |
| 78 | + * |
| 79 | + * @return \Traversable|\SplFileInfo[] |
| 80 | + */ |
| 81 | + private function prepareFileList($path) |
| 82 | + { |
| 83 | + return Finder::create() |
| 84 | + ->files() |
| 85 | + ->in($path) |
| 86 | + ->name('*.php') |
| 87 | + ->notPath('/Tests/') |
| 88 | + ->notPath('/tests/') |
| 89 | + ->ignoreUnreadableDirs() |
| 90 | + ->ignoreDotFiles(true) |
| 91 | + ->ignoreVCS(true); |
| 92 | + } |
| 93 | +} |
0 commit comments