|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AssetManager\Controller; |
| 4 | + |
| 5 | +use Zend\Console\Adapter\AdapterInterface as Console; |
| 6 | +use Zend\Console\Request as ConsoleRequest; |
| 7 | +use Zend\Mvc\Controller\AbstractActionController; |
| 8 | +use Zend\Stdlib\RequestInterface; |
| 9 | +use Zend\Stdlib\ResponseInterface; |
| 10 | + |
| 11 | +use AssetManager\Service\AssetManager; |
| 12 | + |
| 13 | +/** |
| 14 | + * Class ConsoleController |
| 15 | + * |
| 16 | + * @package AssetManager\Controller |
| 17 | + */ |
| 18 | +class ConsoleController extends AbstractActionController |
| 19 | +{ |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Zend\Console\Adapter\AdapterInterface console object |
| 23 | + */ |
| 24 | + protected $console; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \AssetManager\Service\AssetManager asset manager object |
| 28 | + */ |
| 29 | + protected $assetManager; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var array associative array represents app config |
| 33 | + */ |
| 34 | + protected $appConfig; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param Console $console |
| 38 | + * @param AssetManager $assetManager |
| 39 | + * @param array $appConfig |
| 40 | + */ |
| 41 | + public function __construct(Console $console, AssetManager $assetManager, array $appConfig) |
| 42 | + { |
| 43 | + $this->console = $console; |
| 44 | + $this->assetManager = $assetManager; |
| 45 | + $this->appConfig = $appConfig; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritdoc} |
| 50 | + * @param RequestInterface $request |
| 51 | + * @param ResponseInterface $response |
| 52 | + * @return mixed|ResponseInterface |
| 53 | + * @throws \RuntimeException |
| 54 | + */ |
| 55 | + public function dispatch(RequestInterface $request, ResponseInterface $response = null) |
| 56 | + { |
| 57 | + if (!($request instanceof ConsoleRequest)) { |
| 58 | + throw new \RuntimeException('You can use this controller only from a console!'); |
| 59 | + } |
| 60 | + |
| 61 | + return parent::dispatch($request, $response); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Dumps all assets to cache directories. |
| 66 | + */ |
| 67 | + public function warmupAction() |
| 68 | + { |
| 69 | + $request = $this->getRequest(); |
| 70 | + $purge = $request->getParam('purge', false); |
| 71 | + $verbose = $request->getParam('verbose', false) || $request->getParam('v', false); |
| 72 | + |
| 73 | + // purge cache for every configuration |
| 74 | + if ($purge) { |
| 75 | + $this->purgeCache($verbose); |
| 76 | + } |
| 77 | + |
| 78 | + $this->output('Collecting all assets...', $verbose); |
| 79 | + |
| 80 | + $collection = $this->assetManager->getResolver()->collect(); |
| 81 | + $this->output(sprintf('Collected %d assets, warming up...', count($collection)), $verbose); |
| 82 | + |
| 83 | + foreach ($collection as $path) { |
| 84 | + $asset = $this->assetManager->getResolver()->resolve($path); |
| 85 | + $this->assetManager->getAssetCacheManager()->setCache($path, $asset)->dump(); |
| 86 | + } |
| 87 | + |
| 88 | + $this->output(sprintf('Warming up finished...', $verbose); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Purges all directories defined as AssetManager cache dir. |
| 93 | + * @param bool $verbose verbose flag, default false |
| 94 | + * @return bool false if caching is not set, otherwise true |
| 95 | + */ |
| 96 | + protected function purgeCache($verbose = false) |
| 97 | + { |
| 98 | + |
| 99 | + if (empty($this->appConfig['asset_manager']['caching'])) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + foreach ($this->appConfig['asset_manager']['caching'] as $configName => $config) { |
| 104 | + |
| 105 | + if (empty($config['options']['dir'])) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + $this->output(sprintf('Purging %s on "%s"...', $config['options']['dir'], $configName), $verbose); |
| 109 | + $this->recursiveRemove($config['options']['dir']); |
| 110 | + } |
| 111 | + |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Removes given node from filesystem (recursively). |
| 117 | + * @param string $node - uri of node that should be removed from filesystem |
| 118 | + */ |
| 119 | + protected function recursiveRemove($node) |
| 120 | + { |
| 121 | + if (is_dir($node)) { |
| 122 | + $objects = scandir($node); |
| 123 | + |
| 124 | + foreach ($objects as $object) { |
| 125 | + if ($object === '.' || $object === '..') { |
| 126 | + continue; |
| 127 | + } |
| 128 | + $this->recursiveRemove($node . '/' . $object); |
| 129 | + } |
| 130 | + |
| 131 | + rmdir($node); |
| 132 | + } else { |
| 133 | + unlink($node); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Outputs given $line if $verbose i truthy value. |
| 139 | + * @param $line |
| 140 | + * @param bool $verbose verbose flag, default true |
| 141 | + */ |
| 142 | + protected function output($line, $verbose = true) |
| 143 | + { |
| 144 | + if ($verbose) { |
| 145 | + $this->console->writeLine($line); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments