Skip to content

Instantly share code, notes, and snippets.

@VladaHejda
Created June 2, 2014 12:55
Show Gist options
  • Save VladaHejda/048b21d9b1a0964e8687 to your computer and use it in GitHub Desktop.
Save VladaHejda/048b21d9b1a0964e8687 to your computer and use it in GitHub Desktop.
LazyDataMapper Nette compiler extension.
services:
lazyDataMapperCache: LazyDataMapper\Cache(@someStorage)
extensions:
lazyDataMapper: LazyDataMapper\Extension
lazyDataMapper:
cache: @lazyDataMapperCache
facades:
- Product\Facade
<?php
namespace LazyDataMapper;
use Nette\Caching\IStorage;
use Nette\Caching\Cache as NetteCache;
class Cache implements IExternalCache
{
/** @var NetteCache */
private $cache;
function __construct(IStorage $storage)
{
$this->cache = new NetteCache($storage, 'LDM');
}
public function load($key)
{
return $this->cache->load($key);
}
public function save($key, $data)
{
$this->cache->save($key, $data);
}
}
<?php
namespace LazyDataMapper;
use Nette\Config\Configurator;
use Nette\Config\Compiler;
class Extension extends \Nette\Config\CompilerExtension
{
protected $defaults = array(
'requestKey' => 'LazyDataMapper\RequestKey',
'serviceAccessor' => 'LazyDataMapper\EntityServiceAccessor',
'facades' => array(),
);
/** @var array */
private $configuration;
public static function register(Configurator $configurator)
{
$class = __CLASS__;
$configurator->onCompile[] = function (Configurator $config, Compiler $compiler) use ($class) {
$compiler->addExtension('lazyDataMapper', new $class);
};
}
public function loadConfiguration()
{
$this->configuration = $config = $this->getConfig($this->defaults);
$definitions = $this->getServiceDefinitions(array('cache', 'requestKey', 'serviceAccessor'));
$builder = $this->getContainerBuilder();
$builder->addDefinition($this->prefix('suggestorCache'))
->setClass('LazyDataMapper\SuggestorCache', $definitions);
$definitions['suggestorCache'] = $this->prefix('@suggestorCache');
$builder->addDefinition($this->prefix('accessor'))
->setClass('LazyDataMapper\Accessor', array($definitions['suggestorCache'], $definitions['serviceAccessor']));
$definitions['accessor'] = $this->prefix('@accessor');
if (!is_array($config['facades'])) {
throw new \Nette\InvalidArgumentException("Configuration directive 'facades' must be array.");
}
foreach ($config['facades'] as $name => $facade) {
$builder->addDefinition($this->prefix($name))
->setClass($facade, array($definitions['accessor'], $definitions['serviceAccessor']));
}
}
protected function getServiceDefinitions(array $serviceNames)
{
$config = $this->configuration;
$definitions = array();
foreach ($serviceNames as $serviceName) {
if (!isset($config[$serviceName])) {
$this->throwMissingDirectiveException($serviceName);
}
$serviceDefined = strpos($config[$serviceName], '@') === 0;
if ($serviceDefined) {
$definitions[$serviceName] = $config[$serviceName];
} else {
$this->getContainerBuilder()
->addDefinition($this->prefix($serviceName))
->setClass($config[$serviceName]);
$definitions[$serviceName] = $this->prefix("@$serviceName");
}
}
return $definitions;
}
protected function throwMissingDirectiveException($directive)
{
throw new \Nette\InvalidArgumentException("Configuration directive '$directive' is required for LazyDataMapper extension.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment