Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow add collections at runtime #88

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
2013-07-25
----------

* Allow add collections at runtime

```php
<?php
public function onBootstrap(MvcEvent $e){
if ($e->getRequest() instanceof HttpRequest){
$sm = $e->getApplication()->getServiceManager();
$sm->get('AsseticBundle\Service')->getEventManager()->attach('setupRenderer', array($this, 'configureAssets'));
}
}
```

```php
<?php
public function configureAssets(EventInterface $e){
$response = array();
/** @var \AsseticBundle\Service $target */
$target = $e->getTarget();
$config = $target->getServiceLocator()->get('config');
$config = $config['collections'];
foreach($config as $assetName){
if ($target->getAssetManager()->has($assetName)){
$response[] = $assetName;
}
}
return $response;
}
```


2013-06-21
----------

Expand Down
2 changes: 1 addition & 1 deletion src/AsseticBundle/CacheBuster/LastModifiedStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class LastModifiedStrategy implements WorkerInterface
{
public function process(AssetInterface $asset, AssetFactory $factory)
public function process(AssetInterface $asset)
{
$path = $asset->getTargetPath();
$ext = pathinfo($path, PATHINFO_EXTENSION);
Expand Down
95 changes: 93 additions & 2 deletions src/AsseticBundle/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,27 @@
Assetic\Cache\FilesystemCache,
Zend\View\Renderer\RendererInterface as Renderer,
AsseticBundle\View\StrategyInterface;

class Service
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ResponseCollection;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Traversable;

class Service implements EventManagerAwareInterface, ServiceLocatorAwareInterface
{
/**
* @var ServiceLocatorInterface
*/
protected $serviceLocator = null;

/**
* @var EventManagerInterface
*/
protected $events;


const DEFAULT_ROUTE_NAME = 'default';

/**
Expand Down Expand Up @@ -67,6 +85,69 @@ public function __construct(Configuration $configuration)
$this->configuration = $configuration;
}

/**
* Set service locator
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;

return $this;
}

/**
* Set the event manager instance used by this context
*
* @param EventManagerInterface $events
* @return mixed
*/
public function setEventManager(EventManagerInterface $events)
{
$identifiers = array(__CLASS__, get_class($this));
if (isset($this->eventIdentifier)) {
if ((is_string($this->eventIdentifier))
|| (is_array($this->eventIdentifier))
|| ($this->eventIdentifier instanceof Traversable)
) {
$identifiers = array_unique(array_merge($identifiers, (array) $this->eventIdentifier));
} elseif (is_object($this->eventIdentifier)) {
$identifiers[] = $this->eventIdentifier;
}
// silently ignore invalid eventIdentifier types
}
$events->setIdentifiers($identifiers);
$this->events = $events;
return $this;
}

/**
* Retrieve the event manager
*
* Lazy-loads an EventManager instance if none registered.
*
* @return EventManagerInterface
*/
public function getEventManager()
{
if (!$this->events instanceof EventManagerInterface) {
$this->setEventManager(new EventManager());
}
return $this->events;
}

/**
* Get service locator
*
* @return ServiceLocatorInterface
*/
public function getServiceLocator()
{
return $this->serviceLocator;
}

public function setRouteName($routeName)
{
$this->routeName = $routeName;
Expand Down Expand Up @@ -237,6 +318,16 @@ public function setupRenderer(Renderer $renderer)
$config = $this->getRouterConfig();
}

/** @var ResponseCollection $response */
$params = array(
'renderer' => $renderer,
'config' => $config
);
$response = $this->getEventManager()->trigger(__FUNCTION__, $this, $params);
foreach($response as $_config){
$config = array_merge($config, $_config);
}

// If we don't have any assets listed by now, or if we are mixing in
// the default assets, then merge in the default assets to the config array
$defaultConfig = $this->getDefaultConfig();
Expand Down