AssetManager\Resolver\CollectionResolver::resolve PHP Method

resolve() public method

{@inheritDoc}
public resolve ( $name )
    public function resolve($name)
    {
        if (!isset($this->collections[$name])) {
            return null;
        }
        if (!is_array($this->collections[$name])) {
            throw new Exception\RuntimeException("Collection with name {$name} is not an an array.");
        }
        $collection = new AssetCollection();
        $mimeType = null;
        $collection->setTargetPath($name);
        foreach ($this->collections[$name] as $asset) {
            if (!is_string($asset)) {
                throw new Exception\RuntimeException('Asset should be of type string. got ' . gettype($asset));
            }
            if (null === ($res = $this->getAggregateResolver()->resolve($asset))) {
                throw new Exception\RuntimeException("Asset '{$asset}' could not be found.");
            }
            if (!$res instanceof AssetInterface) {
                throw new Exception\RuntimeException("Asset '{$asset}' does not implement Assetic\\Asset\\AssetInterface.");
            }
            if (null !== $mimeType && $res->mimetype !== $mimeType) {
                throw new Exception\RuntimeException(sprintf('Asset "%s" from collection "%s" doesn\'t have the expected mime-type "%s".', $asset, $name, $mimeType));
            }
            $mimeType = $res->mimetype;
            $this->getAssetFilterManager()->setFilters($asset, $res);
            $collection->add($res);
        }
        $collection->mimetype = $mimeType;
        return $collection;
    }

Usage Example

 public function testSuccessResolve()
 {
     $callbackInvocationCount = 0;
     $callback = function () use(&$callbackInvocationCount) {
         $asset1 = new Asset\StringAsset('bacon');
         $asset2 = new Asset\StringAsset('eggs');
         $asset3 = new Asset\StringAsset('Mud');
         $asset1->mimetype = 'text/plain';
         $asset2->mimetype = 'text/plain';
         $asset3->mimetype = 'text/plain';
         $callbackInvocationCount += 1;
         $assetName = "asset{$callbackInvocationCount}";
         return ${$assetName};
     };
     $aggregateResolver = $this->getMock('AssetManager\\Resolver\\ResolverInterface');
     $aggregateResolver->expects($this->exactly(3))->method('resolve')->will($this->returnCallback($callback));
     $resolver = new CollectionResolver(array('myCollection' => array('bacon', 'eggs', 'mud')));
     $mimeResolver = new MimeResolver();
     $assetFilterManager = new AssetFilterManager();
     $assetFilterManager->setMimeResolver($mimeResolver);
     $resolver->setAggregateResolver($aggregateResolver);
     $resolver->setAssetFilterManager($assetFilterManager);
     $collectionResolved = $resolver->resolve('myCollection');
     $this->assertEquals($collectionResolved->mimetype, 'text/plain');
     $this->assertTrue($collectionResolved instanceof Asset\AssetCollection);
 }
All Usage Examples Of AssetManager\Resolver\CollectionResolver::resolve