From bf1fd90ede1ab2c0d065b58b50e319566e41c706 Mon Sep 17 00:00:00 2001 From: Ionut Cioflan Date: Fri, 22 Apr 2022 12:06:43 +0300 Subject: [PATCH 1/6] "assetic/framework": "^2.0 || ^3.0" --- composer.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f285e16..af826ef 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "laminas/laminas-loader": "^2.5.1", "laminas/laminas-eventmanager": "^2.6.3 || ^3.0.1", "laminas/laminas-mvc": "^2.7.9 || ^3.0.2", - "assetic/framework": "^2.0", + "assetic/framework": "^2.0 || ^3.0", "laminas/laminas-dependency-plugin": "^2.0" }, "require-dev": { @@ -52,5 +52,10 @@ "psr-0": { "AssetManagerTest": "tests" } + }, + "config": { + "allow-plugins": { + "laminas/laminas-dependency-plugin": true + } } } From d86cfee3ad6f874e6f02046bb872bf8ee7d936fd Mon Sep 17 00:00:00 2001 From: Ionut Cioflan Date: Fri, 14 Jun 2024 12:20:17 +0300 Subject: [PATCH 2/6] dcs/assetmanager - Remove mvc support in order to prepare for Laminas 4 (remove deprecations also) - config provider --- Module.php | 7 - composer.json | 26 +-- config/module.config.php | 69 ------ src/AssetManager/ConfigProvider.php | 54 +++++ .../Controller/ConsoleController.php | 154 -------------- .../Controller/ConsoleControllerFactory.php | 35 --- src/AssetManager/Module.php | 101 --------- .../Resolver/ResolverInterface.php | 6 +- .../AggregateResolverServiceFactory.php | 24 +-- .../AliasPathStackResolverServiceFactory.php | 5 +- .../Service/AssetCacheManager.php | 30 ++- .../AssetCacheManagerAwareInterface.php | 2 - .../AssetCacheManagerServiceFactory.php | 2 +- .../Service/AssetFilterManager.php | 38 ++-- .../AssetFilterManagerAwareInterface.php | 2 - .../AssetFilterManagerServiceFactory.php | 16 +- src/AssetManager/Service/AssetManager.php | 26 +-- .../Service/AssetManagerServiceFactory.php | 22 +- .../Service/AssetViewHelperFactory.php | 74 ------- .../CollectionResolverServiceFactory.php | 25 +-- .../Service/ConcatResolverServiceFactory.php | 20 +- .../Service/MapResolverServiceFactory.php | 17 +- src/AssetManager/Service/MimeResolver.php | 19 +- .../PathStackResolverServiceFactory.php | 23 +- ...PrioritizedPathsResolverServiceFactory.php | 18 +- tests/AssetManagerTest/ConfigProviderTest.php | 22 ++ .../Controller/ConsoleControllerTest.php | 123 ----------- tests/AssetManagerTest/ModuleTest.php | 201 ------------------ .../AggregateResolverServiceFactoryTest.php | 5 +- .../PathStackResolverServiceFactoryTest.php | 38 ++-- .../View/Helper/AssetTest.php | 125 ----------- 31 files changed, 214 insertions(+), 1115 deletions(-) delete mode 100644 Module.php delete mode 100644 config/module.config.php create mode 100644 src/AssetManager/ConfigProvider.php delete mode 100644 src/AssetManager/Controller/ConsoleController.php delete mode 100644 src/AssetManager/Controller/ConsoleControllerFactory.php delete mode 100644 src/AssetManager/Module.php delete mode 100644 src/AssetManager/Service/AssetViewHelperFactory.php create mode 100644 tests/AssetManagerTest/ConfigProviderTest.php delete mode 100644 tests/AssetManagerTest/Controller/ConsoleControllerTest.php delete mode 100644 tests/AssetManagerTest/ModuleTest.php delete mode 100644 tests/AssetManagerTest/View/Helper/AssetTest.php diff --git a/Module.php b/Module.php deleted file mode 100644 index 3987e47..0000000 --- a/Module.php +++ /dev/null @@ -1,7 +0,0 @@ - [ - 'factories' => [ - AssetManager\Service\AssetManager::class => AssetManager\Service\AssetManagerServiceFactory::class, - AssetManager\Service\AssetFilterManager::class => AssetManager\Service\AssetFilterManagerServiceFactory::class, - AssetManager\Service\AssetCacheManager::class => AssetManager\Service\AssetCacheManagerServiceFactory::class, - AssetManager\Resolver\AggregateResolver::class => AssetManager\Service\AggregateResolverServiceFactory::class, - AssetManager\Resolver\MapResolver::class => AssetManager\Service\MapResolverServiceFactory::class, - AssetManager\Resolver\PathStackResolver::class => AssetManager\Service\PathStackResolverServiceFactory::class, - AssetManager\Resolver\PrioritizedPathsResolver::class => AssetManager\Service\PrioritizedPathsResolverServiceFactory::class, - AssetManager\Resolver\CollectionResolver::class => AssetManager\Service\CollectionResolverServiceFactory::class, - AssetManager\Resolver\ConcatResolver::class => AssetManager\Service\ConcatResolverServiceFactory::class, - AssetManager\Resolver\AliasPathStackResolver::class => AssetManager\Service\AliasPathStackResolverServiceFactory::class, - ], - 'invokables' => [ - AssetManager\Service\MimeResolver::class => AssetManager\Service\MimeResolver::class, - ], - 'aliases' => [ - //Alias left here for BC - 'mime_resolver' => AssetManager\Service\MimeResolver::class, - 'AssetManager\Service\AggregateResolver' => AssetManager\Resolver\AggregateResolver::class - ], - ], - 'asset_manager' => [ - 'clear_output_buffer' => true, - 'resolvers' => [ - AssetManager\Resolver\MapResolver::class => 3000, - AssetManager\Resolver\ConcatResolver::class => 2500, - AssetManager\Resolver\CollectionResolver::class => 2000, - AssetManager\Resolver\PrioritizedPathsResolver::class => 1500, - AssetManager\Resolver\AliasPathStackResolver::class => 1000, - AssetManager\Resolver\PathStackResolver::class => 500, - ], - 'view_helper' => [ - 'append_timestamp' => true, - 'query_string' => '_', - 'cache' => null, - ], - ], - 'controllers' => [ - 'factories' => [ - 'AssetManager\Controller\Console' => AssetManager\Controller\ConsoleControllerFactory::class, - ], - ], - 'view_helpers' => [ - 'factories' => [ - AssetManager\View\Helper\Asset::class => AssetManager\Service\AssetViewHelperFactory::class - ], - 'aliases' => [ - 'asset' => AssetManager\View\Helper\Asset::class - ] - ], - 'console' => [ - 'router' => [ - 'routes' => [ - 'AssetManager-warmup' => [ - 'options' => [ - 'route' => 'assetmanager warmup [--purge] [--verbose|-v]', - 'defaults' => [ - 'controller' => 'AssetManager\Controller\Console', - 'action' => 'warmup', - ], - ], - ], - ], - ], - ], -]; diff --git a/src/AssetManager/ConfigProvider.php b/src/AssetManager/ConfigProvider.php new file mode 100644 index 0000000..34d3c76 --- /dev/null +++ b/src/AssetManager/ConfigProvider.php @@ -0,0 +1,54 @@ + [ + 'factories' => [ + \AssetManager\Service\AssetManager::class => \AssetManager\Service\AssetManagerServiceFactory::class, + \AssetManager\Service\AssetFilterManager::class => \AssetManager\Service\AssetFilterManagerServiceFactory::class, + \AssetManager\Service\AssetCacheManager::class => \AssetManager\Service\AssetCacheManagerServiceFactory::class, + \AssetManager\Resolver\AggregateResolver::class => \AssetManager\Service\AggregateResolverServiceFactory::class, + \AssetManager\Resolver\MapResolver::class => \AssetManager\Service\MapResolverServiceFactory::class, + \AssetManager\Resolver\PathStackResolver::class => \AssetManager\Service\PathStackResolverServiceFactory::class, + \AssetManager\Resolver\PrioritizedPathsResolver::class => \AssetManager\Service\PrioritizedPathsResolverServiceFactory::class, + \AssetManager\Resolver\CollectionResolver::class => \AssetManager\Service\CollectionResolverServiceFactory::class, + \AssetManager\Resolver\ConcatResolver::class => \AssetManager\Service\ConcatResolverServiceFactory::class, + \AssetManager\Resolver\AliasPathStackResolver::class => \AssetManager\Service\AliasPathStackResolverServiceFactory::class, + ], + 'invokables' => [ + \AssetManager\Service\MimeResolver::class => \AssetManager\Service\MimeResolver::class, + ], + 'aliases' => [ + //Alias left here for BC + 'mime_resolver' => \AssetManager\Service\MimeResolver::class, + 'AssetManager\Service\AggregateResolver' => \AssetManager\Resolver\AggregateResolver::class, + ], + ], + 'asset_manager' => [ + 'clear_output_buffer' => true, + 'resolvers' => [ + \AssetManager\Resolver\MapResolver::class => 3000, + \AssetManager\Resolver\ConcatResolver::class => 2500, + \AssetManager\Resolver\CollectionResolver::class => 2000, + \AssetManager\Resolver\PrioritizedPathsResolver::class => 1500, + \AssetManager\Resolver\AliasPathStackResolver::class => 1000, + \AssetManager\Resolver\PathStackResolver::class => 500, + ], + 'view_helper' => [ + 'append_timestamp' => true, + 'query_string' => '_', + 'cache' => null, + ], + ], + ]; + } + +} \ No newline at end of file diff --git a/src/AssetManager/Controller/ConsoleController.php b/src/AssetManager/Controller/ConsoleController.php deleted file mode 100644 index 1fc9303..0000000 --- a/src/AssetManager/Controller/ConsoleController.php +++ /dev/null @@ -1,154 +0,0 @@ -console = $console; - $this->assetManager = $assetManager; - $this->appConfig = $appConfig; - } - - /** - * {@inheritdoc} - * @param RequestInterface $request - * @param ResponseInterface $response - * @return mixed|ResponseInterface - * @throws \RuntimeException - */ - public function dispatch(RequestInterface $request, ResponseInterface $response = null) - { - if (!($request instanceof ConsoleRequest)) { - throw new \RuntimeException('You can use this controller only from a console!'); - } - - return parent::dispatch($request, $response); - } - - /** - * Dumps all assets to cache directories. - */ - public function warmupAction() - { - $request = $this->getRequest(); - $purge = $request->getParam('purge', false); - $verbose = $request->getParam('verbose', false) || $request->getParam('v', false); - - // purge cache for every configuration - if ($purge) { - $this->purgeCache($verbose); - } - - $this->output('Collecting all assets...', $verbose); - - $collection = $this->assetManager->getResolver()->collect(); - $this->output(sprintf('Collected %d assets, warming up...', count($collection)), $verbose); - - foreach ($collection as $path) { - $asset = $this->assetManager->getResolver()->resolve($path); - $this->assetManager->getAssetFilterManager()->setFilters($path, $asset); - $this->assetManager->getAssetCacheManager()->setCache($path, $asset)->dump(); - } - - $this->output(sprintf('Warming up finished...', $verbose)); - } - - /** - * Purges all directories defined as AssetManager cache dir. - * @param bool $verbose verbose flag, default false - * @return bool false if caching is not set, otherwise true - */ - protected function purgeCache($verbose = false) - { - - if (empty($this->appConfig['asset_manager']['caching'])) { - return false; - } - - foreach ($this->appConfig['asset_manager']['caching'] as $configName => $config) { - if (empty($config['options']['dir'])) { - continue; - } - $this->output(sprintf('Purging %s on "%s"...', $config['options']['dir'], $configName), $verbose); - - $node = $config['options']['dir']; - - if ($configName !== 'default') { - $node .= '/'.$configName; - } - - $this->recursiveRemove($node, $verbose); - } - - return true; - } - - /** - * Removes given node from filesystem (recursively). - * @param string $node - uri of node that should be removed from filesystem - * @param bool $verbose verbose flag, default false - */ - protected function recursiveRemove($node, $verbose = false) - { - if (is_dir($node)) { - $objects = scandir($node); - - foreach ($objects as $object) { - if ($object === '.' || $object === '..') { - continue; - } - $this->recursiveRemove($node . '/' . $object); - } - } elseif (is_file($node)) { - $this->output(sprintf("unlinking %s...", $node), $verbose); - unlink($node); - } - } - - /** - * Outputs given $line if $verbose i truthy value. - * @param $line - * @param bool $verbose verbose flag, default true - */ - protected function output($line, $verbose = true) - { - if ($verbose) { - $this->console->writeLine($line); - } - } -} diff --git a/src/AssetManager/Controller/ConsoleControllerFactory.php b/src/AssetManager/Controller/ConsoleControllerFactory.php deleted file mode 100644 index 568ea5d..0000000 --- a/src/AssetManager/Controller/ConsoleControllerFactory.php +++ /dev/null @@ -1,35 +0,0 @@ -get('console'); - $assetManager = $container->get(AssetManager::class); - $appConfig = $container->get('config'); - - return new ConsoleController($console, $assetManager, $appConfig); - } - - /** - * @inheritDoc - */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - if ($serviceLocator instanceof AbstractPluginManager) { - $serviceLocator = $serviceLocator->getServiceLocator() ?: $serviceLocator; - } - return $this($serviceLocator, ConsoleController::class); - } -} diff --git a/src/AssetManager/Module.php b/src/AssetManager/Module.php deleted file mode 100644 index 16c35e1..0000000 --- a/src/AssetManager/Module.php +++ /dev/null @@ -1,101 +0,0 @@ - array( - StandardAutoloader::LOAD_NS => array( - __NAMESPACE__ => __DIR__, - ), - ), - ); - } - - /** - * {@inheritDoc} - */ - public function getConfig() - { - return include __DIR__ . '/../../config/module.config.php'; - } - - /** - * Callback method for dispatch and dispatch.error events. - * - * @param MvcEvent $event - */ - public function onDispatch(MvcEvent $event) - { - /* @var $response \Laminas\Http\Response */ - $response = $event->getResponse(); - if (!method_exists($response, 'getStatusCode') || $response->getStatusCode() !== 404) { - return; - } - $request = $event->getRequest(); - $serviceManager = $event->getApplication()->getServiceManager(); - $assetManager = $serviceManager->get(__NAMESPACE__ . '\Service\AssetManager'); - - if (!$assetManager->resolvesToAsset($request)) { - return; - } - - $response->setStatusCode(200); - - return $assetManager->setAssetOnResponse($response); - } - - /** - * {@inheritDoc} - */ - public function onBootstrap(EventInterface $event) - { - // Attach for dispatch, and dispatch.error (with low priority to make sure statusCode gets set) - /* @var $eventManager \Laminas\EventManager\EventManagerInterface */ - $eventManager = $event->getTarget()->getEventManager(); - $callback = array($this, 'onDispatch'); - $priority = -9999999; - $eventManager->attach(MvcEvent::EVENT_DISPATCH, $callback, $priority); - $eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, $callback, $priority); - } - - /** - * @param \Laminas\Console\Adapter\AdapterInterface $console - * @return array - * - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function getConsoleUsage(AdapterInterface $console) - { - return array( - 'Warmup', - 'assetmanager warmup [--purge] [--verbose|-v]' => 'Warm AssetManager up', - array('--purge', '(optional) forces cache flushing'), - array('--verbose | -v', '(optional) verbose mode'), - ); - } -} diff --git a/src/AssetManager/Resolver/ResolverInterface.php b/src/AssetManager/Resolver/ResolverInterface.php index 0fb6a2c..9652b0d 100644 --- a/src/AssetManager/Resolver/ResolverInterface.php +++ b/src/AssetManager/Resolver/ResolverInterface.php @@ -2,14 +2,16 @@ namespace AssetManager\Resolver; +use Assetic\Contracts\Asset\AssetInterface; + interface ResolverInterface { /** * Resolve an Asset * - * @param string $path The path to resolve. + * @param string $path The path to resolve. * - * @return \Assetic\Contracts\Asset\AssetInterface|null Asset instance when found, null when not. + * @return AssetInterface|null Asset instance when found, null when not. */ public function resolve($path); } diff --git a/src/AssetManager/Service/AggregateResolverServiceFactory.php b/src/AssetManager/Service/AggregateResolverServiceFactory.php index da2140e..2db81cb 100644 --- a/src/AssetManager/Service/AggregateResolverServiceFactory.php +++ b/src/AssetManager/Service/AggregateResolverServiceFactory.php @@ -8,7 +8,7 @@ use AssetManager\Resolver\MimeResolverAwareInterface; use AssetManager\Resolver\ResolverInterface; use Interop\Container\ContainerInterface; -use Laminas\ServiceManager\FactoryInterface; +use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\ServiceManager\ServiceLocatorInterface; /** @@ -19,14 +19,11 @@ */ class AggregateResolverServiceFactory implements FactoryInterface { - /** - * @inheritDoc - */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null): AggregateResolver { - $config = $container->get('config'); - $config = isset($config['asset_manager']) ? $config['asset_manager'] : array(); - $resolver = new AggregateResolver(); + $config = $container->get('config'); + $config = $config['asset_manager'] ?? []; + $resolver = new AggregateResolver(); if (empty($config['resolvers'])) { return $resolver; @@ -37,7 +34,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o if (!$resolverService instanceof ResolverInterface) { throw new Exception\RuntimeException( - 'Service does not implement the required interface ResolverInterface.' + 'Service does not implement the required interface ResolverInterface.', ); } @@ -51,7 +48,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o if ($resolverService instanceof AssetFilterManagerAwareInterface) { $resolverService->setAssetFilterManager( - $container->get(AssetFilterManager::class) + $container->get(AssetFilterManager::class), ); } @@ -61,12 +58,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o return $resolver; } - /** - * {@inheritDoc} - * - * @return AggregateResolver - */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function createService(ServiceLocatorInterface $serviceLocator): AggregateResolver { return $this($serviceLocator, AggregateResolver::class); } diff --git a/src/AssetManager/Service/AliasPathStackResolverServiceFactory.php b/src/AssetManager/Service/AliasPathStackResolverServiceFactory.php index f38bf40..44cc664 100644 --- a/src/AssetManager/Service/AliasPathStackResolverServiceFactory.php +++ b/src/AssetManager/Service/AliasPathStackResolverServiceFactory.php @@ -5,10 +5,7 @@ use AssetManager\Resolver\AliasPathStackResolver; use AssetManager\Resolver\PathStackResolver; use Interop\Container\ContainerInterface; -use Interop\Container\Exception\ContainerException; -use Laminas\ServiceManager\Exception\ServiceNotCreatedException; -use Laminas\ServiceManager\Exception\ServiceNotFoundException; -use Laminas\ServiceManager\FactoryInterface; +use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\ServiceManager\ServiceLocatorInterface; class AliasPathStackResolverServiceFactory implements FactoryInterface diff --git a/src/AssetManager/Service/AssetCacheManager.php b/src/AssetManager/Service/AssetCacheManager.php index a953317..8b943b2 100644 --- a/src/AssetManager/Service/AssetCacheManager.php +++ b/src/AssetManager/Service/AssetCacheManager.php @@ -5,37 +5,32 @@ use Assetic\Asset\AssetCache; use Assetic\Contracts\Asset\AssetInterface; use Assetic\Contracts\Cache\CacheInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; +use Psr\Container\ContainerInterface; /** * Asset Cache Manager. Sets asset cache based on configuration. */ class AssetCacheManager { - /** - * @var \Laminas\ServiceManager\ServiceLocatorInterface - */ - protected $serviceLocator; + protected ContainerInterface $serviceLocator; /** * @var array Cache configuration. */ - protected $config = array(); + protected $config = []; /** * Construct the AssetCacheManager * - * @param ServiceLocatorInterface $serviceLocator - * @param array $config - * - * @return AssetCacheManager + * @param ContainerInterface $serviceLocator + * @param array $config */ public function __construct( - ServiceLocatorInterface $serviceLocator, - $config + ContainerInterface $serviceLocator, + $config, ) { $this->serviceLocator = $serviceLocator; - $this->config = $config; + $this->config = $config; } /** @@ -44,7 +39,7 @@ public function __construct( * @param string $path Path to asset * @param AssetInterface $asset Assetic Asset Interface * - * @return AssetCache + * @return AssetCache|AssetInterface */ public function setCache($path, AssetInterface $asset) { @@ -54,8 +49,8 @@ public function setCache($path, AssetInterface $asset) return $asset; } - $assetCache = new AssetCache($asset, $provider); - $assetCache->mimetype = $asset->mimetype; + $assetCache = new AssetCache($asset, $provider); + $assetCache->mimetype = $asset->mimetype; return $assetCache; } @@ -88,7 +83,7 @@ private function getProvider($path) return call_user_func($cacheProvider['cache'], $path); } - $dir = ''; + $dir = ''; $class = $cacheProvider['cache']; if (!empty($cacheProvider['options']['dir'])) { @@ -96,6 +91,7 @@ private function getProvider($path) } $class = $this->classMapper($class); + return new $class($dir, $path); } diff --git a/src/AssetManager/Service/AssetCacheManagerAwareInterface.php b/src/AssetManager/Service/AssetCacheManagerAwareInterface.php index 86cd542..c254e63 100644 --- a/src/AssetManager/Service/AssetCacheManagerAwareInterface.php +++ b/src/AssetManager/Service/AssetCacheManagerAwareInterface.php @@ -6,8 +6,6 @@ interface AssetCacheManagerAwareInterface { /** * Set the AssetCacheManager. - * - * @param AssetCacheManager $cacheManager */ public function setAssetCacheManager(AssetCacheManager $cacheManager); diff --git a/src/AssetManager/Service/AssetCacheManagerServiceFactory.php b/src/AssetManager/Service/AssetCacheManagerServiceFactory.php index 1c65d3c..bb29d68 100644 --- a/src/AssetManager/Service/AssetCacheManagerServiceFactory.php +++ b/src/AssetManager/Service/AssetCacheManagerServiceFactory.php @@ -3,7 +3,7 @@ namespace AssetManager\Service; use Interop\Container\ContainerInterface; -use Laminas\ServiceManager\FactoryInterface; +use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\ServiceManager\ServiceLocatorInterface; /** diff --git a/src/AssetManager/Service/AssetFilterManager.php b/src/AssetManager/Service/AssetFilterManager.php index 92255ac..09049b0 100644 --- a/src/AssetManager/Service/AssetFilterManager.php +++ b/src/AssetManager/Service/AssetFilterManager.php @@ -7,6 +7,7 @@ use AssetManager\Exception; use AssetManager\Resolver\MimeResolverAwareInterface; use Laminas\ServiceManager\ServiceLocatorInterface; +use Psr\Container\ContainerInterface; class AssetFilterManager implements MimeResolverAwareInterface { @@ -16,7 +17,7 @@ class AssetFilterManager implements MimeResolverAwareInterface protected $config; /** - * @var ServiceLocatorInterface + * @var ContainerInterface */ protected $serviceLocator; @@ -24,19 +25,18 @@ class AssetFilterManager implements MimeResolverAwareInterface * @var MimeResolver */ protected $mimeResolver; - + /** * @var FilterInterface[] Filters already instantiated */ - protected $filterInstances = array(); - + protected $filterInstances = []; + /** * Construct the AssetFilterManager * - * @param array $config - * @return AssetFilterManager + * @param array $config */ - public function __construct(array $config = array()) + public function __construct(array $config = []) { $this->setConfig($config); } @@ -64,8 +64,8 @@ protected function setConfig($config) /** * See if there are filters for the asset, and if so, set them. * - * @param string $path - * @param AssetInterface $asset + * @param string $path + * @param AssetInterface $asset * * @throws Exception\RuntimeException on invalid filters */ @@ -96,7 +96,7 @@ public function setFilters($path, AssetInterface $asset) $this->ensureByService($asset, $filter['service']); } else { throw new Exception\RuntimeException( - 'Invalid filter supplied. Expected Filter or Service.' + 'Invalid filter supplied. Expected Filter or Service.', ); } } @@ -105,8 +105,9 @@ public function setFilters($path, AssetInterface $asset) /** * Ensure that the filters as service are set. * - * @param AssetInterface $asset - * @param string $service A valid service name. + * @param AssetInterface $asset + * @param string $service A valid service name. + * * @throws Exception\RuntimeException */ protected function ensureByService(AssetInterface $asset, $service) @@ -115,7 +116,7 @@ protected function ensureByService(AssetInterface $asset, $service) $this->ensureByFilter($asset, $this->getServiceLocator()->get($service)); } else { throw new Exception\RuntimeException( - 'Unexpected service provided. Expected string or callback.' + 'Unexpected service provided. Expected string or callback.', ); } } @@ -123,8 +124,9 @@ protected function ensureByService(AssetInterface $asset, $service) /** * Ensure that the filters as filter are set. * - * @param AssetInterface $asset - * @param mixed $filter Either an instance of FilterInterface or a classname. + * @param AssetInterface $asset + * @param mixed $filter Either an instance of FilterInterface or a classname. + * * @throws Exception\RuntimeException */ protected function ensureByFilter(AssetInterface $asset, $filter) @@ -140,12 +142,12 @@ protected function ensureByFilter(AssetInterface $asset, $filter) if (!is_subclass_of($filterClass, 'Assetic\Contracts\Filter\FilterInterface', true)) { $filterClass .= (substr($filterClass, -6) === 'Filter') ? '' : 'Filter'; - $filterClass = 'Assetic\Filter\\' . $filterClass; + $filterClass = 'Assetic\Filter\\' . $filterClass; } if (!class_exists($filterClass)) { throw new Exception\RuntimeException( - 'No filter found for ' . $filter + 'No filter found for ' . $filter, ); } @@ -185,7 +187,7 @@ public function getServiceLocator() /** * {@inheritDoc} */ - public function setServiceLocator(ServiceLocatorInterface $serviceLocator) + public function setServiceLocator(ContainerInterface $serviceLocator) { $this->serviceLocator = $serviceLocator; } diff --git a/src/AssetManager/Service/AssetFilterManagerAwareInterface.php b/src/AssetManager/Service/AssetFilterManagerAwareInterface.php index 849a78b..ff12d2f 100644 --- a/src/AssetManager/Service/AssetFilterManagerAwareInterface.php +++ b/src/AssetManager/Service/AssetFilterManagerAwareInterface.php @@ -6,8 +6,6 @@ interface AssetFilterManagerAwareInterface { /** * Set the AssetFilterManager. - * - * @param AssetFilterManager $filterManager */ public function setAssetFilterManager(AssetFilterManager $filterManager); diff --git a/src/AssetManager/Service/AssetFilterManagerServiceFactory.php b/src/AssetManager/Service/AssetFilterManagerServiceFactory.php index fdb5e30..d7e3316 100644 --- a/src/AssetManager/Service/AssetFilterManagerServiceFactory.php +++ b/src/AssetManager/Service/AssetFilterManagerServiceFactory.php @@ -3,17 +3,14 @@ namespace AssetManager\Service; use Interop\Container\ContainerInterface; -use Laminas\ServiceManager\FactoryInterface; +use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\ServiceManager\ServiceLocatorInterface; class AssetFilterManagerServiceFactory implements FactoryInterface { - /** - * @inheritDoc - */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null): AssetFilterManager { - $filters = array(); + $filters = []; $config = $container->get('config'); if (!empty($config['asset_manager']['filters'])) { @@ -28,12 +25,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o return $assetFilterManager; } - /** - * {@inheritDoc} - * - * @return AssetFilterManager - */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function createService(ServiceLocatorInterface $serviceLocator): AssetFilterManager { return $this($serviceLocator, AssetFilterManager::class); } diff --git a/src/AssetManager/Service/AssetManager.php b/src/AssetManager/Service/AssetManager.php index d9d6b09..af6c63d 100644 --- a/src/AssetManager/Service/AssetManager.php +++ b/src/AssetManager/Service/AssetManager.php @@ -57,10 +57,8 @@ class AssetManager implements * * @param ResolverInterface $resolver * @param array $config - * - * @return AssetManager */ - public function __construct($resolver, $config = array()) + public function __construct($resolver, $config = []) { $this->setResolver($resolver); $this->setConfig($config); @@ -79,7 +77,8 @@ protected function setConfig(array $config) /** * Check if the request resolves to an asset. * - * @param RequestInterface $request + * @param RequestInterface $request + * * @return boolean */ public function resolvesToAsset(RequestInterface $request) @@ -88,7 +87,7 @@ public function resolvesToAsset(RequestInterface $request) $this->asset = $this->resolve($request); } - return (bool)$this->asset; + return (bool) $this->asset; } /** @@ -124,7 +123,8 @@ public function getResolver() /** * Set the asset on the response, including headers and content. * - * @param ResponseInterface $response + * @param ResponseInterface $response + * * @return ResponseInterface * @throws Exception\RuntimeException */ @@ -132,7 +132,7 @@ public function setAssetOnResponse(ResponseInterface $response) { if (!$this->asset instanceof AssetInterface) { throw new Exception\RuntimeException( - 'Unable to set asset on response. Request has not been resolved to an asset.' + 'Unable to set asset on response. Request has not been resolved to an asset.', ); } @@ -143,9 +143,9 @@ public function setAssetOnResponse(ResponseInterface $response) $this->getAssetFilterManager()->setFilters($this->path, $this->asset); - $this->asset = $this->getAssetCacheManager()->setCache($this->path, $this->asset); - $mimeType = $this->asset->mimetype; - $assetContents = $this->asset->dump(); + $this->asset = $this->getAssetCacheManager()->setCache($this->path, $this->asset); + $mimeType = $this->asset->mimetype; + $assetContents = $this->asset->dump(); // @codeCoverageIgnoreStart if (function_exists('mb_strlen')) { @@ -164,9 +164,9 @@ public function setAssetOnResponse(ResponseInterface $response) } $response->getHeaders() - ->addHeaderLine('Content-Transfer-Encoding', 'binary') - ->addHeaderLine('Content-Type', $mimeType) - ->addHeaderLine('Content-Length', $contentLength); + ->addHeaderLine('Content-Transfer-Encoding', 'binary') + ->addHeaderLine('Content-Type', $mimeType) + ->addHeaderLine('Content-Length', $contentLength); $response->setContent($assetContents); diff --git a/src/AssetManager/Service/AssetManagerServiceFactory.php b/src/AssetManager/Service/AssetManagerServiceFactory.php index 711a61a..28f67c3 100644 --- a/src/AssetManager/Service/AssetManagerServiceFactory.php +++ b/src/AssetManager/Service/AssetManagerServiceFactory.php @@ -4,7 +4,7 @@ use AssetManager\Resolver\AggregateResolver; use Interop\Container\ContainerInterface; -use Laminas\ServiceManager\FactoryInterface; +use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\ServiceManager\ServiceLocatorInterface; /** @@ -15,13 +15,10 @@ */ class AssetManagerServiceFactory implements FactoryInterface { - /** - * @inheritDoc - */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null): AssetManager { $config = $container->get('config'); - $assetManagerConfig = array(); + $assetManagerConfig = []; if (!empty($config['asset_manager'])) { $assetManagerConfig = $config['asset_manager']; @@ -29,26 +26,21 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o $assetManager = new AssetManager( $container->get(AggregateResolver::class), - $assetManagerConfig + $assetManagerConfig, ); $assetManager->setAssetFilterManager( - $container->get(AssetFilterManager::class) + $container->get(AssetFilterManager::class), ); $assetManager->setAssetCacheManager( - $container->get(AssetCacheManager::class) + $container->get(AssetCacheManager::class), ); return $assetManager; } - /** - * {@inheritDoc} - * - * @return AssetManager - */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function createService(ServiceLocatorInterface $serviceLocator): AssetManager { return $this($serviceLocator, AssetManager::class); } diff --git a/src/AssetManager/Service/AssetViewHelperFactory.php b/src/AssetManager/Service/AssetViewHelperFactory.php deleted file mode 100644 index a80e40a..0000000 --- a/src/AssetManager/Service/AssetViewHelperFactory.php +++ /dev/null @@ -1,74 +0,0 @@ -get('config')['asset_manager']; - - /** @var ResolverInterface $assetManagerResolver */ - $assetManagerResolver = $container->get(AssetManager::class)->getResolver(); - - /** @var AbstractCacheAdapter|null $cache */ - $cache = $this->loadCache($container, $config); - - return new Asset($assetManagerResolver, $cache, $config); - } - - /** - * @param ServiceLocatorInterface $serviceLocator - * @param array $config - * - * @return null - */ - private function loadCache($serviceLocator, $config) - { - // check if the cache is configured - if (!isset($config['view_helper']['cache']) || $config['view_helper']['cache'] === null) { - return null; - } - - // get the cache, if it's a string, search it among services - $cache = $config['view_helper']['cache']; - if (is_string($cache)) { - $cache = $serviceLocator->get($cache); - } - - // exception in case cache is not an Adapter that extend the AbstractAdapter of Laminas\Cache\Storage - if ($cache !== null && !($cache instanceof AbstractCacheAdapter)) { - throw new InvalidArgumentException( - 'Invalid cache provided, you must pass a Cache Adapter that extend - Laminas\Cache\Storage\Adapter\AbstractAdapter' - ); - } - - return $cache; - } - - /** - * {@inheritDoc} - * - * @return Asset - */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - if ($serviceLocator instanceof AbstractPluginManager) { - $serviceLocator = $serviceLocator->getServiceLocator() ?: $serviceLocator; - } - return $this($serviceLocator, Asset::class); - } -} diff --git a/src/AssetManager/Service/CollectionResolverServiceFactory.php b/src/AssetManager/Service/CollectionResolverServiceFactory.php index d8056e6..5e30219 100644 --- a/src/AssetManager/Service/CollectionResolverServiceFactory.php +++ b/src/AssetManager/Service/CollectionResolverServiceFactory.php @@ -4,35 +4,18 @@ use AssetManager\Resolver\CollectionResolver; use Interop\Container\ContainerInterface; -use Laminas\ServiceManager\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; -class CollectionResolverServiceFactory implements FactoryInterface +class CollectionResolverServiceFactory implements \Laminas\ServiceManager\Factory\FactoryInterface { - /** - * @inheritDoc - */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null): CollectionResolver { $config = $container->get('config'); - $collections = array(); + $collections = []; if (isset($config['asset_manager']['resolver_configs']['collections'])) { $collections = $config['asset_manager']['resolver_configs']['collections']; } - $collectionResolver = new CollectionResolver($collections); - - return $collectionResolver; - } - - /** - * {@inheritDoc} - * - * @return CollectionResolver - */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - return $this($serviceLocator, CollectionResolver::class); + return new CollectionResolver($collections); } } diff --git a/src/AssetManager/Service/ConcatResolverServiceFactory.php b/src/AssetManager/Service/ConcatResolverServiceFactory.php index 9076704..3a8fe5e 100644 --- a/src/AssetManager/Service/ConcatResolverServiceFactory.php +++ b/src/AssetManager/Service/ConcatResolverServiceFactory.php @@ -4,26 +4,16 @@ use AssetManager\Resolver\ConcatResolver; use Interop\Container\ContainerInterface; -use Laminas\ServiceManager\FactoryInterface; +use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\ServiceManager\ServiceLocatorInterface; class ConcatResolverServiceFactory implements FactoryInterface { - /** - * @inheritDoc - */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null): ConcatResolver { - $config = $container->get('config'); - $files = array(); - - if (isset($config['asset_manager']['resolver_configs']['concat'])) { - $files = $config['asset_manager']['resolver_configs']['concat']; - } - - $concatResolver = new ConcatResolver($files); - - return $concatResolver; + $config = $container->get('config'); + $files = $config['asset_manager']['resolver_configs']['concat'] ?? []; + return new ConcatResolver($files); } /** diff --git a/src/AssetManager/Service/MapResolverServiceFactory.php b/src/AssetManager/Service/MapResolverServiceFactory.php index 4a9b131..4f19c31 100644 --- a/src/AssetManager/Service/MapResolverServiceFactory.php +++ b/src/AssetManager/Service/MapResolverServiceFactory.php @@ -4,10 +4,7 @@ use AssetManager\Resolver\MapResolver; use Interop\Container\ContainerInterface; -use Interop\Container\Exception\ContainerException; -use Laminas\ServiceManager\Exception\ServiceNotCreatedException; -use Laminas\ServiceManager\Exception\ServiceNotFoundException; -use Laminas\ServiceManager\FactoryInterface; +use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\ServiceManager\ServiceLocatorInterface; class MapResolverServiceFactory implements FactoryInterface @@ -15,26 +12,22 @@ class MapResolverServiceFactory implements FactoryInterface /** * @inheritDoc */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null): MapResolver { $config = $container->get('config'); - $map = array(); + $map = []; if (isset($config['asset_manager']['resolver_configs']['map'])) { $map = $config['asset_manager']['resolver_configs']['map']; } - $patchStackResolver = new MapResolver($map); - - return $patchStackResolver; + return new MapResolver($map); } /** * {@inheritDoc} - * - * @return MapResolver */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function createService(ServiceLocatorInterface $serviceLocator): MapResolver { return $this($serviceLocator, MapResolver::class); } diff --git a/src/AssetManager/Service/MimeResolver.php b/src/AssetManager/Service/MimeResolver.php index cd19176..5d97fe9 100644 --- a/src/AssetManager/Service/MimeResolver.php +++ b/src/AssetManager/Service/MimeResolver.php @@ -1,4 +1,5 @@ 'text/css', - 'js' => 'application/javascript', - ); + protected $mainMimeTypes = [ + 'css' => 'text/css', + 'js' => 'application/javascript', + ]; /** * Map between file extensions and mime types. * * @var array */ - protected $mimeTypes = array( + protected $mimeTypes = [ '%' => 'application/x-trash', '3gp' => 'video/3gpp', '7z' => 'application/x-7z-compressed', @@ -553,12 +554,13 @@ class MimeResolver 'zmt' => 'chemical/x-mopac-input', '~' => 'application/x-trash', '323' => 'text/h323', - ); + ]; /** * Get the mime type from a file extension. * - * @param string $filename + * @param string $filename + * * @return string The mime type found. Falls back to text/plain. */ public function getMimeType($filename) @@ -575,7 +577,8 @@ public function getMimeType($filename) /** * Get the extension that matches given mimetype. * - * @param string $mimetype + * @param string $mimetype + * * @return mixed null when not found, extension (string) when found. */ public function getExtension($mimetype) diff --git a/src/AssetManager/Service/PathStackResolverServiceFactory.php b/src/AssetManager/Service/PathStackResolverServiceFactory.php index f5d1273..9793db1 100644 --- a/src/AssetManager/Service/PathStackResolverServiceFactory.php +++ b/src/AssetManager/Service/PathStackResolverServiceFactory.php @@ -4,36 +4,19 @@ use AssetManager\Resolver\PathStackResolver; use Interop\Container\ContainerInterface; -use Laminas\ServiceManager\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; +use Laminas\ServiceManager\Factory\FactoryInterface; class PathStackResolverServiceFactory implements FactoryInterface { - /** - * @inheritDoc - */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null): PathStackResolver { $config = $container->get('config'); $pathStackResolver = new PathStackResolver(); - $paths = array(); - if (isset($config['asset_manager']['resolver_configs']['paths'])) { - $paths = $config['asset_manager']['resolver_configs']['paths']; - } + $paths = $config['asset_manager']['resolver_configs']['paths'] ?? []; $pathStackResolver->addPaths($paths); return $pathStackResolver; } - - /** - * {@inheritDoc} - * - * @return PathStackResolver - */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - return $this($serviceLocator, PathStackResolver::class); - } } diff --git a/src/AssetManager/Service/PrioritizedPathsResolverServiceFactory.php b/src/AssetManager/Service/PrioritizedPathsResolverServiceFactory.php index 7287cf0..ab0ce60 100644 --- a/src/AssetManager/Service/PrioritizedPathsResolverServiceFactory.php +++ b/src/AssetManager/Service/PrioritizedPathsResolverServiceFactory.php @@ -4,32 +4,22 @@ use AssetManager\Resolver\PrioritizedPathsResolver; use Interop\Container\ContainerInterface; -use Laminas\ServiceManager\FactoryInterface; +use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\ServiceManager\ServiceLocatorInterface; class PrioritizedPathsResolverServiceFactory implements FactoryInterface { - /** - * @inheritDoc - */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null): PrioritizedPathsResolver { $config = $container->get('config'); $prioritizedPathsResolver = new PrioritizedPathsResolver(); - $paths = isset($config['asset_manager']['resolver_configs']['prioritized_paths']) - ? $config['asset_manager']['resolver_configs']['prioritized_paths'] - : array(); + $paths = $config['asset_manager']['resolver_configs']['prioritized_paths'] ?? []; $prioritizedPathsResolver->addPaths($paths); return $prioritizedPathsResolver; } - /** - * {@inheritDoc} - * - * @return PrioritizedPathsResolver - */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function createService(ServiceLocatorInterface $serviceLocator): PrioritizedPathsResolver { return $this($serviceLocator, PrioritizedPathsResolver::class); } diff --git a/tests/AssetManagerTest/ConfigProviderTest.php b/tests/AssetManagerTest/ConfigProviderTest.php new file mode 100644 index 0000000..e75b036 --- /dev/null +++ b/tests/AssetManagerTest/ConfigProviderTest.php @@ -0,0 +1,22 @@ +assertIsArray($configProvider->__invoke()); + } +} diff --git a/tests/AssetManagerTest/Controller/ConsoleControllerTest.php b/tests/AssetManagerTest/Controller/ConsoleControllerTest.php deleted file mode 100644 index ea660e6..0000000 --- a/tests/AssetManagerTest/Controller/ConsoleControllerTest.php +++ /dev/null @@ -1,123 +0,0 @@ - array( - self::$assetName => array( - array( - 'filter' => 'JSMin', - ), - ), - ), - ); - - $assetFilterManager = new AssetFilterManager($config['filters']); - $assetCacheManager = $this->getAssetCacheManager(); - - $resolver = $this->getResolver(__DIR__ . '/../../_files/require-jquery.js'); - $assetManager = new AssetManager($resolver, $config); - $assetManager->setAssetFilterManager($assetFilterManager); - $assetManager->setAssetCacheManager($assetCacheManager); - - $this->request = new ConsoleRequest(); - $this->routeMatch = $this->createRouteMatch(['controller' => 'console']); - - $this->event = new MvcEvent(); - $this->event->setRouteMatch($this->routeMatch); - - $this->controller = new ConsoleController( - $this->createMock(AdapterInterface::class), - $assetManager, - array() - ); - $this->controller->setEvent($this->event); - } - - public function createRouteMatch(array $params = []) - { - $class = class_exists(V2RouteMatch::class) ? V2RouteMatch::class : RouteMatch::class; - return new $class($params); - } - - /** - * - * @return ResolverInterface - */ - protected function getResolver() - { - $mimeResolver = new MimeResolver(); - $resolver = new MapResolver(array( - self::$assetName => __DIR__ . '/../../_files/require-jquery.js' - )); - $resolver->setMimeResolver($mimeResolver); - return $resolver; - } - - /** - * @return AssetCacheManager - */ - protected function getAssetCacheManager() - { - $serviceLocator = $this->createMock(ServiceLocatorInterface::class); - $config = array( - self::$assetName => array( - 'cache' => 'FilePathCache', - 'options' => array( - 'dir' => sys_get_temp_dir() - ) - ), - ); - $assetCacheManager = new AssetCacheManager($serviceLocator, $config); - return $assetCacheManager; - } - - public function testWarmupAction(): void - { - $this->routeMatch->setParam('action', 'warmup'); - $this->controller->dispatch($this->request); - - $dumpedAsset = sys_get_temp_dir() . '/' . self::$assetName; - $this->assertEquals( - file_get_contents($dumpedAsset), - JSMin::minify(file_get_contents(__DIR__ . '/../../_files/require-jquery.js')) - ); - } -} diff --git a/tests/AssetManagerTest/ModuleTest.php b/tests/AssetManagerTest/ModuleTest.php deleted file mode 100644 index 543b49c..0000000 --- a/tests/AssetManagerTest/ModuleTest.php +++ /dev/null @@ -1,201 +0,0 @@ -assertIsArray($module->getAutoloaderConfig()); - } - - public function testGetConfig(): void - { - $module = new Module(); - // just testing ZF specification requirements - $this->assertIsArray($module->getConfig()); - } - - /** - * Verifies that dispatch listener does nothing on other repsponse codes - */ - public function testDispatchListenerIgnoresOtherResponseCodes(): void - { - $event = new MvcEvent(); - $response = new Response(); - $module = new Module(); - - $response->setStatusCode(500); - $event->setResponse($response); - - $response = $module->onDispatch($event); - - $this->assertNull($response); - } - - public function testOnDispatchDoesntResolveToAsset(): void - { - $resolver = $this->createMock(ResolverInterface::class); - $assetManager = $this->createMock( - AssetManager::class, - array('resolvesToAsset'), - array($resolver) - ); - $assetManager - ->expects($this->once()) - ->method('resolvesToAsset') - ->will($this->returnValue(false)); - - $serviceManager = $this->createMock(ServiceLocatorInterface::class); - $serviceManager - ->expects($this->any()) - ->method('get') - ->will($this->returnValue($assetManager)); - - $application = $this->createMock(ApplicationInterface::class); - $application - ->expects($this->once()) - ->method('getServiceManager') - ->will($this->returnValue($serviceManager)); - - $event = new MvcEvent(); - $response = new Response(); - $request = new Request(); - $module = new Module(); - - $event->setApplication($application); - $response->setStatusCode(404); - $event->setResponse($response); - $event->setRequest($request); - - $return = $module->onDispatch($event); - - $this->assertNull($return); - } - - public function testOnDispatchStatus200(): void - { - $resolver = $this->createMock(ResolverInterface::class); - $assetManager = $this->createMock( - AssetManager::class, - array('resolvesToAsset', 'setAssetOnResponse'), - array($resolver) - ); - $assetManager - ->expects($this->once()) - ->method('resolvesToAsset') - ->will($this->returnValue(true)); - - - $amResponse = new Response(); - $amResponse->setContent('bacon'); - - $assetManager - ->expects($this->once()) - ->method('setAssetOnResponse') - ->will($this->returnValue($amResponse)); - - $serviceManager = $this->createMock(ServiceLocatorInterface::class); - $serviceManager - ->expects($this->any()) - ->method('get') - ->will($this->returnValue($assetManager)); - - $application = $this->createMock(ApplicationInterface::class); - $application - ->expects($this->once()) - ->method('getServiceManager') - ->will($this->returnValue($serviceManager)); - - $event = new MvcEvent(); - $response = new Response(); - $request = new Request(); - $module = new Module(); - - $event->setApplication($application); - $response->setStatusCode(404); - $event->setResponse($response); - $event->setRequest($request); - - $return = $module->onDispatch($event); - - $this->assertEquals(200, $return->getStatusCode()); - } - - /** - * @covers \AssetManager\Module::onDispatch - */ - public function testWillIgnoreInvalidResponseType(): void - { - $cliResponse = $this->createMock(ConsoleResponse::class, array(), array(), '', false); - $mvcEvent = $this->createMock(MvcEvent::class); - $module = new Module(); - - $cliResponse->expects($this->never())->method('getErrorLevel'); - $mvcEvent->expects($this->once())->method('getResponse')->will($this->returnValue($cliResponse)); - - $this->assertNull($module->onDispatch($mvcEvent)); - } - - public function testOnBootstrap(): void - { - $applicationEventManager = new EventManager(); - - $application = $this->createMock(ApplicationInterface::class); - $application - ->expects($this->any()) - ->method('getEventManager') - ->will($this->returnValue($applicationEventManager)); - - $event = new Event(); - $event->setTarget($application); - - $module = new Module(); - $module->onBootstrap($event); - - $this->assertListenerAtPriority( - [$module, 'onDispatch'], - -9999999, - MvcEvent::EVENT_DISPATCH, - $applicationEventManager - ); - - $this->assertListenerAtPriority( - [$module, 'onDispatch'], - -9999999, - MvcEvent::EVENT_DISPATCH_ERROR, - $applicationEventManager - ); - } -} diff --git a/tests/AssetManagerTest/Service/AggregateResolverServiceFactoryTest.php b/tests/AssetManagerTest/Service/AggregateResolverServiceFactoryTest.php index ed94804..bd14f62 100644 --- a/tests/AssetManagerTest/Service/AggregateResolverServiceFactoryTest.php +++ b/tests/AssetManagerTest/Service/AggregateResolverServiceFactoryTest.php @@ -2,6 +2,7 @@ namespace AssetManagerTest\Service; +use AssetManager\Resolver\AggregateResolver; use AssetManager\Resolver\ResolverInterface; use AssetManager\Service\AggregateResolverServiceFactory; use AssetManager\Service\AssetFilterManager; @@ -55,7 +56,7 @@ public function testWillAttachResolver(): void $serviceManager->setService(MimeResolver::class, new MimeResolver); $factory = new AggregateResolverServiceFactory(); - $resolver = $factory->createService($serviceManager); + $resolver = $factory->__invoke($serviceManager, AggregateResolver::class); $this->assertSame('test-resolved-path', $resolver->resolve('test-path')); } @@ -80,7 +81,7 @@ public function testInvalidCustomResolverFails(): void ); $factory = new AggregateResolverServiceFactory(); - $factory->createService($serviceManager); + $factory->__invoke($serviceManager, AggregateResolver::class); } public function testWillPrioritizeResolversCorrectly(): void diff --git a/tests/AssetManagerTest/Service/PathStackResolverServiceFactoryTest.php b/tests/AssetManagerTest/Service/PathStackResolverServiceFactoryTest.php index df48bfd..252aadf 100644 --- a/tests/AssetManagerTest/Service/PathStackResolverServiceFactoryTest.php +++ b/tests/AssetManagerTest/Service/PathStackResolverServiceFactoryTest.php @@ -4,8 +4,8 @@ use AssetManager\Resolver\PathStackResolver; use AssetManager\Service\PathStackResolverServiceFactory; -use PHPUnit\Framework\TestCase; use Laminas\ServiceManager\ServiceManager; +use PHPUnit\Framework\TestCase; class PathStackResolverServiceFactoryTest extends TestCase { @@ -17,27 +17,27 @@ public function testCreateService(): void $serviceManager = new ServiceManager(); $serviceManager->setService( 'config', - array( - 'asset_manager' => array( - 'resolver_configs' => array( - 'paths' => array( - 'path1/', - 'path2/', - ), - ), - ), - ) + [ + 'asset_manager' => [ + 'resolver_configs' => [ + 'paths' => [ + 'path1' . DIRECTORY_SEPARATOR, + 'path2' . DIRECTORY_SEPARATOR, + ], + ], + ], + ], ); $factory = new PathStackResolverServiceFactory(); /* @var $resolver PathStackResolver */ - $resolver = $factory->createService($serviceManager); + $resolver = $factory->__invoke($serviceManager, \AssetManager\Resolver\PathStackResolver::class); $this->assertSame( - array( - 'path2/', - 'path1/', - ), - $resolver->getPaths()->toArray() + [ + 'path2' . DIRECTORY_SEPARATOR, + 'path1' . DIRECTORY_SEPARATOR, + ], + $resolver->getPaths()->toArray(), ); } @@ -47,11 +47,11 @@ public function testCreateService(): void public function testCreateServiceWithNoConfig(): void { $serviceManager = new ServiceManager(); - $serviceManager->setService('config', array()); + $serviceManager->setService('config', []); $factory = new PathStackResolverServiceFactory(); /* @var $resolver PathStackResolver */ - $resolver = $factory->createService($serviceManager); + $resolver = $factory->__invoke($serviceManager, \AssetManager\Resolver\PathStackResolver::class); $this->assertEmpty($resolver->getPaths()->toArray()); } } diff --git a/tests/AssetManagerTest/View/Helper/AssetTest.php b/tests/AssetManagerTest/View/Helper/AssetTest.php deleted file mode 100644 index cadf150..0000000 --- a/tests/AssetManagerTest/View/Helper/AssetTest.php +++ /dev/null @@ -1,125 +0,0 @@ -assertTrue($resolver instanceof MimeResolverAwareInterface); - - $mimeResolver = new MimeResolver; - - $resolver->setMimeResolver($mimeResolver); - - return $resolver; - } - - public function testInvoke(): void - { - $configWithCache = array( - 'view_helper' => array( - 'append_timestamp' => true, - 'query_string' => '_', - 'cache' => null, - ), - 'caching' => array( - 'default' => array( - 'cache' => FilePathCache::class, - 'options' => array( - 'dir' => 'public/assets', - ), - ), - ), - ); - - $configWithoutCache = array( - 'view_helper' => array( - 'append_timestamp' => true, - 'query_string' => '_', - 'cache' => null, - ), - ); - - $filename = 'porn-food/bac.on'; - - $resolver = $this->getGenericResolver(); - - $resolver->setMap(array( - 'porn-food/bac.on' => __FILE__, - )); - - $helperWithCache = new Asset($resolver, null, $configWithCache); - $newFilenameWithCache = $helperWithCache->__invoke($filename); - - // with cache file should have a timestamp query param - $this->assertStringContainsString('?_=', $newFilenameWithCache); - - $helperWithoutCache = new Asset($resolver, null, $configWithoutCache); - $newFilenameWithoutCache = $helperWithoutCache->__invoke($filename); - - // without cache file should have a timestamp query param - $this->assertStringContainsString('?_=', $newFilenameWithoutCache); - - // without cache the timestamp query param should be different than with cache - $this->assertNotSame($newFilenameWithCache, $newFilenameWithoutCache); - } - - public function testSameResultWithoutCachingConfig(): void - { - $config = array( - 'view_helper' => array( - 'append_timestamp' => true, - 'query_string' => '_', - 'cache' => null, - ), - ); - - $filename = 'porn-food/bac.on'; - - $resolver = $this->getGenericResolver(); - - $resolver->setMap(array( - 'porn-food/bac.on' => __FILE__, - )); - - $helper = new Asset($resolver, null, $config); - $newFilename = $helper->__invoke($filename); - - $this->assertStringContainsString('?_=', $newFilename); - $this->assertNotSame($newFilename, $filename); - } - - public function testForceToNotAppendTimestampWithoutCache(): void - { - $config = array( - 'view_helper' => array( - 'append_timestamp' => false, - 'query_string' => '_', - 'cache' => null, - ), - ); - - $filename = 'porn-food/bac.on'; - - $resolver = $this->getGenericResolver(); - - $resolver->setMap(array( - 'porn-food/bac.on' => __FILE__, - )); - - $helper = new Asset($resolver, null, $config); - $newFilename = $helper->__invoke($filename); - - $this->assertStringNotContainsString('?_=', $newFilename); - $this->assertSame($newFilename, $filename); - } -} From c9bed75ce37886cbfba9220a41db1a366b9af103 Mon Sep 17 00:00:00 2001 From: Ionut Cioflan Date: Fri, 14 Jun 2024 12:31:35 +0300 Subject: [PATCH 3/6] dcs/assetmanager - Remove mvc support in order to prepare for Laminas 4 (remove deprecations also) - config provider --- src/AssetManager/ConfigProvider.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/AssetManager/ConfigProvider.php b/src/AssetManager/ConfigProvider.php index 34d3c76..51718ad 100644 --- a/src/AssetManager/ConfigProvider.php +++ b/src/AssetManager/ConfigProvider.php @@ -10,7 +10,7 @@ public function __invoke(): array * @noinspection PhpFullyQualifiedNameUsageInspection */ return [ - 'service_manager' => [ + 'dependencies' => [ 'factories' => [ \AssetManager\Service\AssetManager::class => \AssetManager\Service\AssetManagerServiceFactory::class, \AssetManager\Service\AssetFilterManager::class => \AssetManager\Service\AssetFilterManagerServiceFactory::class, @@ -22,17 +22,10 @@ public function __invoke(): array \AssetManager\Resolver\CollectionResolver::class => \AssetManager\Service\CollectionResolverServiceFactory::class, \AssetManager\Resolver\ConcatResolver::class => \AssetManager\Service\ConcatResolverServiceFactory::class, \AssetManager\Resolver\AliasPathStackResolver::class => \AssetManager\Service\AliasPathStackResolverServiceFactory::class, - ], - 'invokables' => [ - \AssetManager\Service\MimeResolver::class => \AssetManager\Service\MimeResolver::class, - ], - 'aliases' => [ - //Alias left here for BC - 'mime_resolver' => \AssetManager\Service\MimeResolver::class, - 'AssetManager\Service\AggregateResolver' => \AssetManager\Resolver\AggregateResolver::class, + \AssetManager\Service\MimeResolver::class => \Laminas\ServiceManager\Factory\InvokableFactory::class, ], ], - 'asset_manager' => [ + 'asset_manager' => [ 'clear_output_buffer' => true, 'resolvers' => [ \AssetManager\Resolver\MapResolver::class => 3000, From e1d3f0453e68566a566606669c5f55eb2a62937d Mon Sep 17 00:00:00 2001 From: Ionut Cioflan Date: Wed, 10 Jul 2024 15:39:32 +0300 Subject: [PATCH 4/6] Replace Interop\Container\ContainerInterface with Psr\Container\ContainerInterface remove requirement laminas/laminas-dependency-plugin , laminas/laminas-eventmanager Signed-off-by: Ionut Cioflan --- composer.json | 13 +- .../AggregateResolverServiceFactory.php | 8 +- .../AliasPathStackResolverServiceFactory.php | 25 ++-- .../AssetCacheManagerServiceFactory.php | 17 +-- .../Service/AssetFilterManager.php | 1 - .../AssetFilterManagerServiceFactory.php | 8 +- .../Service/AssetManagerServiceFactory.php | 8 +- .../CollectionResolverServiceFactory.php | 5 +- .../Service/ConcatResolverServiceFactory.php | 13 +- .../Service/MapResolverServiceFactory.php | 11 +- .../PathStackResolverServiceFactory.php | 2 +- ...PrioritizedPathsResolverServiceFactory.php | 19 ++- src/AssetManager/View/Helper/Asset.php | 135 ------------------ 13 files changed, 33 insertions(+), 232 deletions(-) delete mode 100644 src/AssetManager/View/Helper/Asset.php diff --git a/composer.json b/composer.json index d41b1e9..9c1fef4 100644 --- a/composer.json +++ b/composer.json @@ -25,12 +25,11 @@ "rwoverdijk/assetmanager": "self.version" }, "require": { - "php": "^8.1", + "php": "^8.1|^8.2|^8.3", "laminas/laminas-stdlib": "^3.2.1", "laminas/laminas-servicemanager": "^3.1.1", - "laminas/laminas-eventmanager": "^3.0.1", "assetic/framework": "^3.0", - "laminas/laminas-dependency-plugin": "^2.0" + "psr/container": "^1.1|^2.0" }, "require-dev": { "laminas/laminas-http": "^2.5.4", @@ -40,9 +39,6 @@ "squizlabs/php_codesniffer": "^3.5.6", "phpunit/phpunit": "^9.1" }, - "suggest": { - "laminas/laminas-mvc-console": "laminas-mvc-console provides the ability to expose laminas-mvc as a console application" - }, "autoload": { "psr-0": { "AssetManager": "src/" @@ -53,9 +49,10 @@ "AssetManagerTest": "tests" } }, + "config": { - "allow-plugins": { - "laminas/laminas-dependency-plugin": true + "platform": { + "php": "8.1.99" } } } diff --git a/src/AssetManager/Service/AggregateResolverServiceFactory.php b/src/AssetManager/Service/AggregateResolverServiceFactory.php index 2db81cb..ac13cab 100644 --- a/src/AssetManager/Service/AggregateResolverServiceFactory.php +++ b/src/AssetManager/Service/AggregateResolverServiceFactory.php @@ -7,9 +7,8 @@ use AssetManager\Resolver\AggregateResolverAwareInterface; use AssetManager\Resolver\MimeResolverAwareInterface; use AssetManager\Resolver\ResolverInterface; -use Interop\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; +use Psr\Container\ContainerInterface; /** * Factory class for AssetManagerService @@ -57,9 +56,4 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o return $resolver; } - - public function createService(ServiceLocatorInterface $serviceLocator): AggregateResolver - { - return $this($serviceLocator, AggregateResolver::class); - } } diff --git a/src/AssetManager/Service/AliasPathStackResolverServiceFactory.php b/src/AssetManager/Service/AliasPathStackResolverServiceFactory.php index 44cc664..a13966c 100644 --- a/src/AssetManager/Service/AliasPathStackResolverServiceFactory.php +++ b/src/AssetManager/Service/AliasPathStackResolverServiceFactory.php @@ -3,20 +3,21 @@ namespace AssetManager\Service; use AssetManager\Resolver\AliasPathStackResolver; -use AssetManager\Resolver\PathStackResolver; -use Interop\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; +use Psr\Container\ContainerInterface; class AliasPathStackResolverServiceFactory implements FactoryInterface { /** * @inheritDoc */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) - { - $config = $container->get('config'); - $aliases = array(); + public function __invoke( + ContainerInterface $container, + $requestedName, + array $options = null, + ): AliasPathStackResolver { + $config = $container->get('config'); + $aliases = []; if (isset($config['asset_manager']['resolver_configs']['aliases'])) { $aliases = $config['asset_manager']['resolver_configs']['aliases']; @@ -24,14 +25,4 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o return new AliasPathStackResolver($aliases); } - - /** - * {@inheritDoc} - * - * @return PathStackResolver - */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - return $this($serviceLocator, AliasPathStackResolver::class); - } } diff --git a/src/AssetManager/Service/AssetCacheManagerServiceFactory.php b/src/AssetManager/Service/AssetCacheManagerServiceFactory.php index bb29d68..efb14ac 100644 --- a/src/AssetManager/Service/AssetCacheManagerServiceFactory.php +++ b/src/AssetManager/Service/AssetCacheManagerServiceFactory.php @@ -2,9 +2,8 @@ namespace AssetManager\Service; -use Interop\Container\ContainerInterface; +use Psr\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; /** * Factory for the Asset Cache Manager Service @@ -16,9 +15,9 @@ class AssetCacheManagerServiceFactory implements FactoryInterface /** * @inheritDoc */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null): AssetCacheManager { - $config = array(); + $config = []; $globalConfig = $container->get('config'); @@ -28,14 +27,4 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o return new AssetCacheManager($container, $config); } - - /** - * {@inheritDoc} - * - * @return AssetFilterManager - */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - return $this($serviceLocator, AssetCacheManager::class); - } } diff --git a/src/AssetManager/Service/AssetFilterManager.php b/src/AssetManager/Service/AssetFilterManager.php index 09049b0..9110ba1 100644 --- a/src/AssetManager/Service/AssetFilterManager.php +++ b/src/AssetManager/Service/AssetFilterManager.php @@ -6,7 +6,6 @@ use Assetic\Contracts\Filter\FilterInterface; use AssetManager\Exception; use AssetManager\Resolver\MimeResolverAwareInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; use Psr\Container\ContainerInterface; class AssetFilterManager implements MimeResolverAwareInterface diff --git a/src/AssetManager/Service/AssetFilterManagerServiceFactory.php b/src/AssetManager/Service/AssetFilterManagerServiceFactory.php index d7e3316..1089b19 100644 --- a/src/AssetManager/Service/AssetFilterManagerServiceFactory.php +++ b/src/AssetManager/Service/AssetFilterManagerServiceFactory.php @@ -2,9 +2,8 @@ namespace AssetManager\Service; -use Interop\Container\ContainerInterface; +use Psr\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; class AssetFilterManagerServiceFactory implements FactoryInterface { @@ -24,9 +23,4 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o return $assetFilterManager; } - - public function createService(ServiceLocatorInterface $serviceLocator): AssetFilterManager - { - return $this($serviceLocator, AssetFilterManager::class); - } } diff --git a/src/AssetManager/Service/AssetManagerServiceFactory.php b/src/AssetManager/Service/AssetManagerServiceFactory.php index 28f67c3..ff5908e 100644 --- a/src/AssetManager/Service/AssetManagerServiceFactory.php +++ b/src/AssetManager/Service/AssetManagerServiceFactory.php @@ -3,9 +3,8 @@ namespace AssetManager\Service; use AssetManager\Resolver\AggregateResolver; -use Interop\Container\ContainerInterface; +use Psr\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; /** * Factory class for AssetManagerService @@ -39,9 +38,4 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o return $assetManager; } - - public function createService(ServiceLocatorInterface $serviceLocator): AssetManager - { - return $this($serviceLocator, AssetManager::class); - } } diff --git a/src/AssetManager/Service/CollectionResolverServiceFactory.php b/src/AssetManager/Service/CollectionResolverServiceFactory.php index 5e30219..b34f9c4 100644 --- a/src/AssetManager/Service/CollectionResolverServiceFactory.php +++ b/src/AssetManager/Service/CollectionResolverServiceFactory.php @@ -3,9 +3,10 @@ namespace AssetManager\Service; use AssetManager\Resolver\CollectionResolver; -use Interop\Container\ContainerInterface; +use Laminas\ServiceManager\Factory\FactoryInterface; +use Psr\Container\ContainerInterface; -class CollectionResolverServiceFactory implements \Laminas\ServiceManager\Factory\FactoryInterface +class CollectionResolverServiceFactory implements FactoryInterface { public function __invoke(ContainerInterface $container, $requestedName, array $options = null): CollectionResolver { diff --git a/src/AssetManager/Service/ConcatResolverServiceFactory.php b/src/AssetManager/Service/ConcatResolverServiceFactory.php index 3a8fe5e..c5bd8ea 100644 --- a/src/AssetManager/Service/ConcatResolverServiceFactory.php +++ b/src/AssetManager/Service/ConcatResolverServiceFactory.php @@ -3,9 +3,8 @@ namespace AssetManager\Service; use AssetManager\Resolver\ConcatResolver; -use Interop\Container\ContainerInterface; +use Psr\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; class ConcatResolverServiceFactory implements FactoryInterface { @@ -15,14 +14,4 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o $files = $config['asset_manager']['resolver_configs']['concat'] ?? []; return new ConcatResolver($files); } - - /** - * {@inheritDoc} - * - * @return ConcatResolver - */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - return $this($serviceLocator, ConcatResolver::class); - } } diff --git a/src/AssetManager/Service/MapResolverServiceFactory.php b/src/AssetManager/Service/MapResolverServiceFactory.php index 4f19c31..f7af92f 100644 --- a/src/AssetManager/Service/MapResolverServiceFactory.php +++ b/src/AssetManager/Service/MapResolverServiceFactory.php @@ -3,9 +3,8 @@ namespace AssetManager\Service; use AssetManager\Resolver\MapResolver; -use Interop\Container\ContainerInterface; +use Psr\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; class MapResolverServiceFactory implements FactoryInterface { @@ -23,12 +22,4 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o return new MapResolver($map); } - - /** - * {@inheritDoc} - */ - public function createService(ServiceLocatorInterface $serviceLocator): MapResolver - { - return $this($serviceLocator, MapResolver::class); - } } diff --git a/src/AssetManager/Service/PathStackResolverServiceFactory.php b/src/AssetManager/Service/PathStackResolverServiceFactory.php index 9793db1..a4a41b7 100644 --- a/src/AssetManager/Service/PathStackResolverServiceFactory.php +++ b/src/AssetManager/Service/PathStackResolverServiceFactory.php @@ -3,7 +3,7 @@ namespace AssetManager\Service; use AssetManager\Resolver\PathStackResolver; -use Interop\Container\ContainerInterface; +use Psr\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; class PathStackResolverServiceFactory implements FactoryInterface diff --git a/src/AssetManager/Service/PrioritizedPathsResolverServiceFactory.php b/src/AssetManager/Service/PrioritizedPathsResolverServiceFactory.php index ab0ce60..bdb39ed 100644 --- a/src/AssetManager/Service/PrioritizedPathsResolverServiceFactory.php +++ b/src/AssetManager/Service/PrioritizedPathsResolverServiceFactory.php @@ -3,24 +3,21 @@ namespace AssetManager\Service; use AssetManager\Resolver\PrioritizedPathsResolver; -use Interop\Container\ContainerInterface; +use Psr\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; class PrioritizedPathsResolverServiceFactory implements FactoryInterface { - public function __invoke(ContainerInterface $container, $requestedName, array $options = null): PrioritizedPathsResolver - { - $config = $container->get('config'); + public function __invoke( + ContainerInterface $container, + $requestedName, + array $options = null, + ): PrioritizedPathsResolver { + $config = $container->get('config'); $prioritizedPathsResolver = new PrioritizedPathsResolver(); - $paths = $config['asset_manager']['resolver_configs']['prioritized_paths'] ?? []; + $paths = $config['asset_manager']['resolver_configs']['prioritized_paths'] ?? []; $prioritizedPathsResolver->addPaths($paths); return $prioritizedPathsResolver; } - - public function createService(ServiceLocatorInterface $serviceLocator): PrioritizedPathsResolver - { - return $this($serviceLocator, PrioritizedPathsResolver::class); - } } diff --git a/src/AssetManager/View/Helper/Asset.php b/src/AssetManager/View/Helper/Asset.php deleted file mode 100644 index e3ba5e8..0000000 --- a/src/AssetManager/View/Helper/Asset.php +++ /dev/null @@ -1,135 +0,0 @@ -assetManagerResolver = $assetManagerResolver; - $this->cache = $cache; - $this->config = $config; - } - - /** - * Append timestamp as query param to the filename - * - * @param string $filename - * @param string $queryString - * @param int|null $timestamp - * - * @return string - */ - private function appendTimestamp($filename, $queryString, $timestamp = null) - { - // current timestamp as default - $timestamp = $timestamp === null ? time() : $timestamp; - - return $filename . '?' . urlencode($queryString) . '=' . $timestamp; - } - - /** - * find the file and if it exists, append its unix modification time to the filename - * - * @param string $filename - * @param string $queryString - * @return string - */ - private function elaborateFilePath($filename, $queryString) - { - $asset = $this->assetManagerResolver->resolve($filename); - if ($asset !== null) { - // append last modified date to the filepath and use a custom query string - return $this->appendTimestamp($filename, $queryString, $asset->getLastModified()); - } - - return $filename; - } - - /** - * Use the cache to get the filePath - * - * @param string $filename - * @param string $queryString - * - * @return mixed|string - */ - private function getFilePathFromCache($filename, $queryString) - { - // return if cache not found - if ($this->cache == null) { - return null; - } - - // cache key based on the filename - $cacheKey = md5($filename); - $itemIsFoundInCache = false; - $filePath = $this->cache->getItem($cacheKey, $itemIsFoundInCache); - - // if there is no element in the cache, elaborate and cache it - if ($itemIsFoundInCache === false || $filePath === null) { - $filePath = $this->elaborateFilePath($filename, $queryString); - $this->cache->setItem($cacheKey, $filePath); - } - - return $filePath; - } - - /** - * Output the filepath with its unix modification time as query param - * - * @param string $filename - * @return string - */ - public function __invoke($filename) - { - // nothing to append - if (empty($this->config['view_helper']['append_timestamp'])) { - return $filename; - } - - // search the cache config for the specific file requested (if none, use the default one) - if (isset($this->config['caching'][$filename])) { - $cacheConfig = $this->config['caching'][$filename]; - } elseif (isset($this->config['caching']['default'])) { - $cacheConfig = $this->config['caching']['default']; - } - - // query string params - $queryString = isset($this->config['view_helper']['query_string']) - ? $this->config['view_helper']['query_string'] - : '_'; - - // no cache dir is defined - if (!isset($cacheConfig['options']['dir'])) { - // append current timestamp to the filepath and use a custom query string - return $this->appendTimestamp($filename, $queryString); - } - - // get the filePath from the cache (if available) - $filePath = $this->getFilePathFromCache($filename, $queryString); - if ($filePath !== null) { - return $filePath; - } - - return $this->elaborateFilePath($filename, $queryString); - } -} From f97d91db8bb0acdce5ee069c257c43f3fd983a5a Mon Sep 17 00:00:00 2001 From: Ionut Cioflan Date: Sun, 27 Oct 2024 11:33:10 +0200 Subject: [PATCH 5/6] dcs/assetmanager - change compatibility from Laminas\Http to Psr\Http --- composer.json | 12 +- .../Resolver/AggregateResolver.php | 2 +- .../Resolver/AliasPathStackResolver.php | 67 +++-- src/AssetManager/Service/AssetManager.php | 72 ++--- .../Service/AssetManagerTest.php | 254 ++++++++---------- 5 files changed, 192 insertions(+), 215 deletions(-) diff --git a/composer.json b/composer.json index 9c1fef4..7a5c710 100644 --- a/composer.json +++ b/composer.json @@ -8,17 +8,17 @@ "asset manager", "laminas" ], - "homepage": "https://github.com/RWOverdijk/AssetManager", + "homepage": "https://github.com/10n/AssetManager.git", "authors": [ { "name": "Wesley Overdijk", "email": "r.w.overdijk@gmail.com", - "homepage": "http://blog.spoonx.nl/" + "homepage": "https://blog.spoonx.nl/" }, { "name": "Marco Pivetta", "email": "ocramius@gmail.com", - "homepage": "http://ocramius.github.com/" + "homepage": "https://ocramius.github.com/" } ], "replace": { @@ -26,13 +26,15 @@ }, "require": { "php": "^8.1|^8.2|^8.3", + "ext-mbstring": "*", + "laminas/laminas-diactoros": "^3.5", "laminas/laminas-stdlib": "^3.2.1", "laminas/laminas-servicemanager": "^3.1.1", "assetic/framework": "^3.0", - "psr/container": "^1.1|^2.0" + "psr/container": "^1.1|^2.0", + "psr/http-message": "^2.0" }, "require-dev": { - "laminas/laminas-http": "^2.5.4", "laminas/laminas-cache": "^3.0", "laminas/laminas-cache-storage-adapter-blackhole": "^2.0", "laminas/laminas-cache-storage-adapter-memory": "^2.0", diff --git a/src/AssetManager/Resolver/AggregateResolver.php b/src/AssetManager/Resolver/AggregateResolver.php index acfcf3e..c67b3f2 100644 --- a/src/AssetManager/Resolver/AggregateResolver.php +++ b/src/AssetManager/Resolver/AggregateResolver.php @@ -13,7 +13,7 @@ class AggregateResolver implements ResolverInterface /** * @var PriorityQueue|ResolverInterface[] */ - protected $queue; + protected PriorityQueue $queue; /** * Constructor diff --git a/src/AssetManager/Resolver/AliasPathStackResolver.php b/src/AssetManager/Resolver/AliasPathStackResolver.php index 85e7f3f..2aa1df7 100644 --- a/src/AssetManager/Resolver/AliasPathStackResolver.php +++ b/src/AssetManager/Resolver/AliasPathStackResolver.php @@ -5,10 +5,11 @@ use Assetic\Asset\FileAsset; use Assetic\Factory\Resource\DirectoryResource; use AssetManager\Exception; +use AssetManager\Exception\InvalidArgumentException; +use AssetManager\Exception\RuntimeException; use AssetManager\Service\MimeResolver; -use SplFileInfo; -use Laminas\Db\TableGateway\Exception\RuntimeException; use Laminas\Stdlib\SplStack; +use SplFileInfo; /** * This resolver allows you to resolve from a stack of aliases to a path. @@ -18,14 +19,14 @@ class AliasPathStackResolver implements ResolverInterface, MimeResolverAwareInte /** * @var array */ - protected $aliases = array(); + protected array $aliases = []; /** - * Flag indicating whether or not LFI protection for rendering view scripts is enabled + * Flag indicating whether LFI protection for rendering view scripts is enabled * * @var bool */ - protected $lfiProtectionOn = true; + protected bool $lfiProtectionOn = true; /** * The mime resolver. @@ -39,10 +40,11 @@ class AliasPathStackResolver implements ResolverInterface, MimeResolverAwareInte * * Populate the array stack with a list of aliases and their corresponding paths * - * @param array $aliases - * @throws Exception\InvalidArgumentException + * @param array $aliases + * + * @throws InvalidArgumentException */ - public function __construct(Array $aliases) + public function __construct(array $aliases) { foreach ($aliases as $alias => $path) { $this->addAlias($alias, $path); @@ -52,24 +54,29 @@ public function __construct(Array $aliases) /** * Add a single alias to the stack * - * @param string $alias - * @param string $path - * @throws Exception\InvalidArgumentException + * @param string $alias + * @param string $path + * + * @throws InvalidArgumentException */ - private function addAlias($alias, $path) + private function addAlias($alias, $path): void { if (!is_string($path)) { - throw new Exception\InvalidArgumentException(sprintf( - 'Invalid path provided; must be a string, received %s', - gettype($path) - )); + throw new InvalidArgumentException( + sprintf( + 'Invalid path provided; must be a string, received %s', + gettype($path), + ), + ); } if (!is_string($alias)) { - throw new Exception\InvalidArgumentException(sprintf( - 'Invalid alias provided; must be a string, received %s', - gettype($alias) - )); + throw new InvalidArgumentException( + sprintf( + 'Invalid alias provided; must be a string, received %s', + gettype($alias), + ), + ); } $this->aliases[$alias] = $this->normalizePath($path); @@ -78,10 +85,11 @@ private function addAlias($alias, $path) /** * Normalize a path for insertion in the stack * - * @param string $path + * @param string $path + * * @return string */ - private function normalizePath($path) + private function normalizePath($path): string { return rtrim($path, '/\\') . DIRECTORY_SEPARATOR; } @@ -91,7 +99,7 @@ private function normalizePath($path) * * @param MimeResolver $resolver */ - public function setMimeResolver(MimeResolver $resolver) + public function setMimeResolver(MimeResolver $resolver): void { $this->mimeResolver = $resolver; } @@ -109,12 +117,15 @@ public function getMimeResolver() /** * Set LFI protection flag * - * @param bool $flag + * @param bool $flag + * * @return self */ - public function setLfiProtection($flag) + public function setLfiProtection($flag): static { $this->lfiProtectionOn = (bool) $flag; + + return $this; } /** @@ -122,7 +133,7 @@ public function setLfiProtection($flag) * * @return bool */ - public function isLfiProtectionOn() + public function isLfiProtectionOn(): bool { return $this->lfiProtectionOn; } @@ -164,11 +175,11 @@ public function resolve($name) */ public function collect() { - $collection = array(); + $collection = []; foreach ($this->aliases as $alias => $path) { $locations = new SplStack(); - $pathInfo = new SplFileInfo($path); + $pathInfo = new SplFileInfo($path); $locations->push($pathInfo); $basePath = $this->normalizePath($pathInfo->getRealPath()); diff --git a/src/AssetManager/Service/AssetManager.php b/src/AssetManager/Service/AssetManager.php index af6c63d..4bb3564 100644 --- a/src/AssetManager/Service/AssetManager.php +++ b/src/AssetManager/Service/AssetManager.php @@ -3,11 +3,11 @@ namespace AssetManager\Service; use Assetic\Contracts\Asset\AssetInterface; -use AssetManager\Exception; +use AssetManager\Exception\RuntimeException; use AssetManager\Resolver\ResolverInterface; -use Laminas\Http\PhpEnvironment\Request; -use Laminas\Stdlib\RequestInterface; -use Laminas\Stdlib\ResponseInterface; +use Laminas\Diactoros\StreamFactory; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; /** * @category AssetManager @@ -50,7 +50,9 @@ class AssetManager implements /** * @var bool Whether this instance has at least one asset successfully set on response */ - protected $assetSetOnResponse = false; + protected bool $assetSetOnResponse = false; + + private StreamFactory $streamFactory; /** * Constructor @@ -62,6 +64,7 @@ public function __construct($resolver, $config = []) { $this->setResolver($resolver); $this->setConfig($config); + $this->streamFactory = new StreamFactory(); } /** @@ -77,11 +80,8 @@ protected function setConfig(array $config) /** * Check if the request resolves to an asset. * - * @param RequestInterface $request - * - * @return boolean */ - public function resolvesToAsset(RequestInterface $request) + public function resolvesToAsset(RequestInterface $request): bool { if (null === $this->asset) { $this->asset = $this->resolve($request); @@ -92,10 +92,8 @@ public function resolvesToAsset(RequestInterface $request) /** * Returns true if this instance of asset manager has at least one asset successfully set on response - * - * @return bool */ - public function assetSetOnResponse() + public function assetSetOnResponse(): bool { return $this->assetSetOnResponse; } @@ -105,7 +103,7 @@ public function assetSetOnResponse() * * @param ResolverInterface $resolver */ - public function setResolver(ResolverInterface $resolver) + public function setResolver(ResolverInterface $resolver): void { $this->resolver = $resolver; } @@ -123,22 +121,19 @@ public function getResolver() /** * Set the asset on the response, including headers and content. * - * @param ResponseInterface $response - * - * @return ResponseInterface - * @throws Exception\RuntimeException + * @throws RuntimeException */ - public function setAssetOnResponse(ResponseInterface $response) + public function setAssetOnResponse(ResponseInterface $response): ResponseInterface { if (!$this->asset instanceof AssetInterface) { - throw new Exception\RuntimeException( + throw new RuntimeException( 'Unable to set asset on response. Request has not been resolved to an asset.', ); } // @todo: Create Asset wrapper for mimetypes if (empty($this->asset->mimetype)) { - throw new Exception\RuntimeException('Expected property "mimetype" on asset.'); + throw new RuntimeException('Expected property "mimetype" on asset.'); } $this->getAssetFilterManager()->setFilters($this->path, $this->asset); @@ -147,14 +142,6 @@ public function setAssetOnResponse(ResponseInterface $response) $mimeType = $this->asset->mimetype; $assetContents = $this->asset->dump(); - // @codeCoverageIgnoreStart - if (function_exists('mb_strlen')) { - $contentLength = mb_strlen($assetContents, '8bit'); - } else { - $contentLength = strlen($assetContents); - } - // @codeCoverageIgnoreEnd - if (!empty($this->config['clear_output_buffer']) && $this->config['clear_output_buffer']) { // Only clean the output buffer if it's turned on and something // has been buffered. @@ -163,12 +150,10 @@ public function setAssetOnResponse(ResponseInterface $response) } } - $response->getHeaders() - ->addHeaderLine('Content-Transfer-Encoding', 'binary') - ->addHeaderLine('Content-Type', $mimeType) - ->addHeaderLine('Content-Length', $contentLength); - - $response->setContent($assetContents); + $response = $response + ->withBody($this->streamFactory->createStream($assetContents)) + ->withAddedHeader('Content-Transfer-Encoding', 'binary') + ->withAddedHeader('Content-Type', $mimeType); $this->assetSetOnResponse = true; @@ -178,21 +163,16 @@ public function setAssetOnResponse(ResponseInterface $response) /** * Resolve the request to a file. * - * @param RequestInterface $request - * * @return mixed false when not found, AssetInterface when resolved. */ protected function resolve(RequestInterface $request) { - if (!$request instanceof Request) { - return false; - } + $uri = $request->getUri(); - /* @var $request Request */ - /* @var $uri \Laminas\Uri\UriInterface */ - $uri = $request->getUri(); - $fullPath = $uri->getPath(); - $path = substr($fullPath, strlen($request->getBasePath()) + 1); +// $fullPath = $uri->getPath(); +// $path = substr($fullPath, strlen($request->getBasePath()) + 1); + $path = $uri->getPath(); + $path = ltrim($path, '/'); $this->path = $path; $asset = $this->getResolver()->resolve($path); @@ -208,7 +188,7 @@ protected function resolve(RequestInterface $request) * * @param AssetFilterManager $filterManager */ - public function setAssetFilterManager(AssetFilterManager $filterManager) + public function setAssetFilterManager(AssetFilterManager $filterManager): void { $this->filterManager = $filterManager; } @@ -228,7 +208,7 @@ public function getAssetFilterManager() * * @param AssetCacheManager $cacheManager */ - public function setAssetCacheManager(AssetCacheManager $cacheManager) + public function setAssetCacheManager(AssetCacheManager $cacheManager): void { $this->cacheManager = $cacheManager; } diff --git a/tests/AssetManagerTest/Service/AssetManagerTest.php b/tests/AssetManagerTest/Service/AssetManagerTest.php index 5370330..052c4dc 100644 --- a/tests/AssetManagerTest/Service/AssetManagerTest.php +++ b/tests/AssetManagerTest/Service/AssetManagerTest.php @@ -3,7 +3,7 @@ namespace AssetManagerTest\Service; use Assetic\Asset; -use AssetManager\Cache\FilePathCache; +use AssetManager\Exception\RuntimeException; use AssetManager\Resolver\AggregateResolver; use AssetManager\Resolver\CollectionResolver; use AssetManager\Resolver\ResolverInterface; @@ -11,11 +11,14 @@ use AssetManager\Service\AssetFilterManager; use AssetManager\Service\AssetManager; use AssetManager\Service\MimeResolver; +use CustomFilter; +use JSMin; +use Laminas\Diactoros\Request; +use Laminas\Diactoros\Response; +use Laminas\Diactoros\Uri; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Laminas\Console\Request as ConsoleRequest; -use Laminas\Http\PhpEnvironment\Request; -use Laminas\Http\Response; +use ReflectionClass; class AssetManagerTest extends TestCase { @@ -32,9 +35,10 @@ public function setUp(): void protected function getRequest() { - $request = new Request(); - $request->setUri('http://localhost/base-path/asset-path'); - $request->setBasePath('/base-path'); + $request = (new Request()) + ->withUri(new Uri('http://localhost/base-path/asset-path')); +// $request->setUri('http://localhost/base-path/asset-path'); +// $request->setBasePath('/base-path'); return $request; } @@ -46,10 +50,10 @@ protected function getRequest() */ protected function getResolver($resolveTo = __FILE__) { - $mimeResolver = new MimeResolver; - $asset = new Asset\FileAsset($resolveTo); - $asset->mimetype = $mimeResolver->getMimeType($resolveTo); - $resolver = $this->createMock(ResolverInterface::class); + $mimeResolver = new MimeResolver; + $asset = new Asset\FileAsset($resolveTo); +// $asset->mimetype = $mimeResolver->getMimeType($resolveTo); + $resolver = $this->createMock(ResolverInterface::class); $resolver ->expects($this->once()) ->method('resolve') @@ -61,14 +65,14 @@ protected function getResolver($resolveTo = __FILE__) public function getCollectionResolver() { - $aggregateResolver = new AggregateResolver; - $mockedResolver = $this->getResolver(__DIR__ . '/../../_files/require-jquery.js'); - $collArr = array( - 'blah.js' => array( - 'asset-path' - ) - ); - $resolver = new CollectionResolver($collArr); + $aggregateResolver = new AggregateResolver; + $mockedResolver = $this->getResolver(__DIR__ . '/../../_files/require-jquery.js'); + $collArr = [ + 'blah.js' => [ + 'asset-path', + ], + ]; + $resolver = new CollectionResolver($collArr); $resolver->setAggregateResolver($aggregateResolver); $aggregateResolver->attach($mockedResolver, 500); @@ -80,45 +84,23 @@ public function getCollectionResolver() public function testConstruct(): void { $resolver = $this->createMock(ResolverInterface::class); - $assetManager = new AssetManager($resolver, array('herp', 'derp')); + $assetManager = new AssetManager($resolver, ['herp', 'derp']); - $refClass = new \ReflectionClass(AssetManager::class); + $refClass = new ReflectionClass(AssetManager::class); $configProp = $refClass->getProperty('config'); $configProp->setAccessible(true); $this->assertSame($resolver, $assetManager->getResolver()); - $this->assertSame(array('herp', 'derp'), $configProp->getValue($assetManager)); + $this->assertSame(['herp', 'derp'], $configProp->getValue($assetManager)); } public function testConstructFailsOnOtherType(): void { - $this->expectError(); - if (PHP_MAJOR_VERSION >= 7) { - $this->expectException('\TypeError'); - } + $this->expectException('\TypeError'); new AssetManager('invalid'); } - public function testInvalidRequest(): void - { - $mimeResolver = new MimeResolver; - $asset = new Asset\FileAsset(__FILE__); - $asset->mimetype = $mimeResolver->getMimeType(__FILE__); - $resolver = $this->createMock(ResolverInterface::class); - $resolver - ->expects($this->any()) - ->method('resolve') - ->with('asset-path') - ->will($this->returnValue($asset)); - - $request = new ConsoleRequest(); - - $assetManager = new AssetManager($resolver); - $resolvesToAsset = $assetManager->resolvesToAsset($request); - - $this->assertFalse($resolvesToAsset); - } public function testResolvesToAsset(): void { @@ -185,24 +167,24 @@ public function testGetResolver(): void public function testSetStandardFilters(): void { - $config = array( - 'filters' => array( - 'asset-path' => array( - array( + $config = [ + 'filters' => [ + 'asset-path' => [ + [ 'filter' => 'JSMin', - ), - ), - ), - ); + ], + ], + ], + ]; $assetFilterManager = new AssetFilterManager($config['filters']); - $assetCacheManager = $this->getAssetCacheManagerMock(); + $assetCacheManager = $this->getAssetCacheManagerMock(); $response = new Response; $resolver = $this->getResolver(__DIR__ . '/../../_files/require-jquery.js'); $request = $this->getRequest(); $assetManager = new AssetManager($resolver, $config); - $minified = \JSMin::minify(file_get_contents(__DIR__ . '/../../_files/require-jquery.js')); + $minified = JSMin::minify(file_get_contents(__DIR__ . '/../../_files/require-jquery.js')); $assetManager->setAssetFilterManager($assetFilterManager); $assetManager->setAssetCacheManager($assetCacheManager); $this->assertTrue($assetManager->resolvesToAsset($request)); @@ -212,25 +194,25 @@ public function testSetStandardFilters(): void public function testSetExtensionFilters(): void { - $config = array( - 'filters' => array( - 'js' => array( - array( + $config = [ + 'filters' => [ + 'js' => [ + [ 'filter' => 'JSMin', - ), - ), - ), - ); + ], + ], + ], + ]; $assetFilterManager = new AssetFilterManager($config['filters']); - $assetCacheManager = $this->getAssetCacheManagerMock(); + $assetCacheManager = $this->getAssetCacheManagerMock(); $mimeResolver = new MimeResolver; $response = new Response; $resolver = $this->getResolver(__DIR__ . '/../../_files/require-jquery.js'); $request = $this->getRequest(); $assetManager = new AssetManager($resolver, $config); - $minified = \JSMin::minify(file_get_contents(__DIR__ . '/../../_files/require-jquery.js')); + $minified = JSMin::minify(file_get_contents(__DIR__ . '/../../_files/require-jquery.js')); $assetFilterManager->setMimeResolver($mimeResolver); $assetManager->setAssetFilterManager($assetFilterManager); $assetManager->setAssetCacheManager($assetCacheManager); @@ -242,27 +224,27 @@ public function testSetExtensionFilters(): void public function testSetExtensionFiltersNotDuplicate(): void { - $config = array( - 'filters' => array( - 'js' => array( - array( + $config = [ + 'filters' => [ + 'js' => [ + [ 'filter' => '\ReverseFilter', - ), - ), - ), - ); + ], + ], + ], + ]; $resolver = $this->getCollectionResolver(); $assetFilterManager = new AssetFilterManager($config['filters']); $mimeResolver = new MimeResolver; $assetFilterManager->setMimeResolver($mimeResolver); $resolver->setAssetFilterManager($assetFilterManager); - - $response = new Response; - $request = $this->getRequest(); + + $response = new Response(); + $request = $this->getRequest(); // Have to change uri because asset-path would cause an infinite loop - $request->setUri('http://localhost/base-path/blah.js'); - + $request->withUri(new Uri('http://localhost/base-path/blah.js')); + $assetCacheManager = $this->getAssetCacheManagerMock(); $assetManager = new AssetManager($resolver->getAggregateResolver(), $config); $assetManager->setAssetCacheManager($assetCacheManager); @@ -277,25 +259,25 @@ public function testSetExtensionFiltersNotDuplicate(): void public function testSetMimeTypeFilters(): void { - $config = array( - 'filters' => array( - 'application/javascript' => array( - array( + $config = [ + 'filters' => [ + 'application/javascript' => [ + [ 'filter' => 'JSMin', - ), - ), - ), - ); + ], + ], + ], + ]; $assetFilterManager = new AssetFilterManager($config['filters']); - $assetCacheManager = $this->getAssetCacheManagerMock(); + $assetCacheManager = $this->getAssetCacheManagerMock(); - $mimeResolver = new MimeResolver; - $response = new Response; - $resolver = $this->getResolver(__DIR__ . '/../../_files/require-jquery.js'); - $request = $this->getRequest(); - $assetManager = new AssetManager($resolver, $config); - $minified = \JSMin::minify(file_get_contents(__DIR__ . '/../../_files/require-jquery.js')); + $mimeResolver = new MimeResolver; + $response = new Response; + $resolver = $this->getResolver(__DIR__ . '/../../_files/require-jquery.js'); + $request = $this->getRequest(); + $assetManager = new AssetManager($resolver, $config); + $minified = JSMin::minify(file_get_contents(__DIR__ . '/../../_files/require-jquery.js')); $assetFilterManager->setMimeResolver($mimeResolver); $assetManager->setAssetFilterManager($assetFilterManager); $assetManager->setAssetCacheManager($assetCacheManager); @@ -307,18 +289,18 @@ public function testSetMimeTypeFilters(): void public function testCustomFilters(): void { - $config = array( - 'filters' => array( - 'asset-path' => array( - array( - 'filter' => new \CustomFilter, - ), - ), - ), - ); + $config = [ + 'filters' => [ + 'asset-path' => [ + [ + 'filter' => new CustomFilter, + ], + ], + ], + ]; $assetFilterManager = new AssetFilterManager($config['filters']); - $assetCacheManager = $this->getAssetCacheManagerMock(); + $assetCacheManager = $this->getAssetCacheManagerMock(); $mimeResolver = new MimeResolver; $response = new Response; $resolver = $this->getResolver(__DIR__ . '/../../_files/require-jquery.js'); @@ -335,12 +317,12 @@ public function testCustomFilters(): void public function testSetEmptyFilters(): void { - $config = array( - 'filters' => array( - 'asset-path' => array( - ), - ), - ); + $config = [ + 'filters' => [ + 'asset-path' => [ + ], + ], + ]; $assetFilterManager = new AssetFilterManager($config['filters']); $assetCacheManager = $this->getAssetCacheManagerMock(); @@ -360,16 +342,16 @@ public function testSetEmptyFilters(): void public function testSetFalseClassFilter(): void { - $this->expectException(\AssetManager\Exception\RuntimeException::class); - $config = array( - 'filters' => array( - 'asset-path' => array( - array( - 'filter' => 'Bacon', - ), - ), - ), - ); + $this->expectException(RuntimeException::class); + $config = [ + 'filters' => [ + 'asset-path' => [ + [ + 'filter' => 'Bacon', + ], + ], + ], + ]; $assetFilterManager = new AssetFilterManager($config['filters']); $assetCacheManager = $this->getAssetCacheManagerMock(); @@ -394,16 +376,16 @@ public function testSetAssetOnResponse(): void $assetFilterManager->setMimeResolver($mimeResolver); $assetManager->setAssetFilterManager($assetFilterManager); $assetManager->setAssetCacheManager($assetCacheManager); - $request = $this->getRequest(); + $request = $this->getRequest(); $assetManager->resolvesToAsset($request); - $response = $assetManager->setAssetOnResponse(new Response); + $response = $assetManager->setAssetOnResponse(new Response); $this->assertSame(file_get_contents(__FILE__), $response->getContent()); } public function testAssetSetOnResponse(): void { - $assetManager = new AssetManager($this->getResolver()); + $assetManager = new AssetManager($this->getResolver()); $assetCacheManager = $this->getAssetCacheManagerMock(); $this->assertFalse($assetManager->assetSetOnResponse()); @@ -419,14 +401,14 @@ public function testAssetSetOnResponse(): void public function testSetAssetOnResponseNoMimeType(): void { - $this->expectException(\AssetManager\Exception\RuntimeException::class); + $this->expectException(RuntimeException::class); $asset = new Asset\FileAsset(__FILE__); $resolver = $this->createMock(ResolverInterface::class); $resolver - ->expects($this->once()) - ->method('resolve') - ->with('asset-path') - ->will($this->returnValue($asset)); + ->expects($this->once()) + ->method('resolve') + ->with('asset-path') + ->will($this->returnValue($asset)); $assetManager = new AssetManager($resolver); $request = $this->getRequest(); @@ -445,7 +427,7 @@ public function testResponseHeadersForAsset(): void $assetManager->setAssetFilterManager($assetFilterManager); $assetManager->setAssetCacheManager($assetCacheManager); - $request = $this->getRequest(); + $request = $this->getRequest(); $assetManager->resolvesToAsset($request); $response = $assetManager->setAssetOnResponse(new Response); $thisFile = file_get_contents(__FILE__); @@ -466,7 +448,7 @@ public function testResponseHeadersForAsset(): void public function testSetAssetOnReponseFailsWhenNotResolved(): void { - $this->expectException(\AssetManager\Exception\RuntimeException::class); + $this->expectException(RuntimeException::class); $resolver = $this->createMock(ResolverInterface::class); $assetManager = new AssetManager($resolver); @@ -493,7 +475,7 @@ public function testClearOutputBufferInSetAssetOnResponse(): void $assetFilterManager = new AssetFilterManager(); $assetCacheManager = $this->getAssetCacheManagerMock(); $mimeResolver = new MimeResolver; - $assetManager = new AssetManager($this->getResolver(), array('clear_output_buffer' => true)); + $assetManager = new AssetManager($this->getResolver(), ['clear_output_buffer' => true]); $assetFilterManager->setMimeResolver($mimeResolver); $assetManager->setAssetFilterManager($assetFilterManager); @@ -528,11 +510,13 @@ protected function getAssetCacheManagerMock() $assetCacheManager->expects($this->any()) ->method('setCache') - ->will($this->returnCallback( - function ($path, $asset) { - return $asset; - } - )); + ->will( + $this->returnCallback( + function ($path, $asset) { + return $asset; + }, + ), + ); return $assetCacheManager; } From 22202cfbfb52a9dc6c22a7547c71126eaf210d44 Mon Sep 17 00:00:00 2001 From: Ionut Cioflan Date: Sun, 27 Oct 2024 11:37:29 +0200 Subject: [PATCH 6/6] dcs/assetmanager - change compatibility from Laminas\Http to Psr\Http --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7a5c710..de3717f 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "laminas/laminas-servicemanager": "^3.1.1", "assetic/framework": "^3.0", "psr/container": "^1.1|^2.0", - "psr/http-message": "^2.0" + "psr/http-message": "^1.1|^2.0" }, "require-dev": { "laminas/laminas-cache": "^3.0",