Created
October 10, 2012 14:41
-
-
Save alexshelkov/3866057 to your computer and use it in GitHub Desktop.
AssetFilterManager as AbstractPluginManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AssetManager\Service; | |
use Assetic\Asset\AssetInterface; | |
use Assetic\Filter\FilterInterface; | |
use AssetManager\Exception; | |
use AssetManager\Resolver\MimeResolverAwareInterface; | |
use AssetManager\Service\MimeResolver; | |
use Zend\ServiceManager\AbstractPluginManager; | |
class AssetFilterManager extends AbstractPluginManager implements MimeResolverAwareInterface | |
{ | |
protected $invokableClasses = array( | |
'lessphp' => 'Assetic\Filter\LessphpFilter' | |
); | |
protected $autoAddInvokableClass = false; | |
/** | |
* @var MimeResolver The mime resolver. | |
*/ | |
protected $mimeResolver; | |
/** | |
* @var array Filter configuration. | |
*/ | |
protected $config; | |
/** | |
* Get the filter configuration. | |
* | |
* @return array | |
*/ | |
public function getConfig() | |
{ | |
return $this->config; | |
} | |
/** | |
* Set the filter configuration. | |
* | |
* @param array $config | |
*/ | |
public function setConfig($config) | |
{ | |
$this->config = $config; | |
} | |
/** | |
* See if there are filters for the asset, and if so, set them. | |
* | |
* @param string $path | |
* @param AssetInterface $asset | |
*/ | |
public function setFilters($path, AssetInterface $asset) | |
{ | |
$config = $this->getConfig(); | |
if (!empty($config[$path])) { | |
$filters = $config[$path]; | |
} elseif (!empty($config[$asset->mimetype])) { | |
$filters = $config[$asset->mimetype]; | |
} else { | |
$extension = $this->getMimeResolver()->getExtension($asset->mimetype); | |
if (!empty($config[$extension])) { | |
$filters = $config[$extension]; | |
} else { | |
return; | |
} | |
} | |
foreach ($filters as $filter) { | |
$filterInstance = $this->get($filter['filter']); | |
$asset->ensureFilter($filterInstance); | |
} | |
} | |
/** | |
*/ | |
public function validatePlugin($plugin) | |
{ | |
if ( ! $plugin instanceof FilterInterface ) { | |
$type = gettype($plugin) == 'object' ? get_class($plugin) : gettype($plugin); | |
throw new Exception\RuntimeException('Expect instance of Assetic\Filter\FilterInterface got ' . $type); | |
} | |
return true; | |
} | |
/** | |
* Set the mime resolver | |
* | |
* @param MimeResolver $resolver | |
*/ | |
public function setMimeResolver(MimeResolver $resolver) | |
{ | |
$this->mimeResolver = $resolver; | |
} | |
/** | |
* Get the mime resolver | |
* | |
* @return MimeResolver | |
*/ | |
public function getMimeResolver() | |
{ | |
return $this->mimeResolver; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment