Skip to content

Commit

Permalink
The extension should be extensible by other extensions using IAspects…
Browse files Browse the repository at this point in the history
…Provider
  • Loading branch information
fprochazka committed Aug 18, 2013
1 parent 48ba2ba commit 1a502bf
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Kdyby/Aop/DI/AopExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,36 @@ class_alias('Nette\Config\Configurator', 'Nette\Configurator');
class AopExtension extends Nette\DI\CompilerExtension
{

const ASPECT_TAG = 'kdyby.aspect';

/**
* @var array
*/
public $defaults = array();



public function loadConfiguration()
{
$builder = $this->getContainerBuilder();

foreach ($this->compiler->getExtensions() as $extension) {
if (!$extension instanceof IAspectsProvider) {
continue;
}

if (!($config = $extension->getAspectsConfiguration()) || !$config instanceof AspectsConfig) {
$refl = new Nette\Reflection\Method($extension, 'getAspectsConfiguration');
$given = is_object($config) ? 'instance of ' . get_class($config) : gettype($config);
throw new Kdyby\Aop\UnexpectedValueException("Method $refl is expected to return instance of \\Kdyby\\Aop\\DI\\AspectsConfig, but $given given.");
}

$config->load($this->compiler, $builder);
}
}



public function beforeCompile()
{
$builder = $this->getContainerBuilder();
Expand All @@ -43,6 +70,18 @@ public function beforeCompile()



/**
* @param string $configFile
* @param Nette\DI\CompilerExtension $extension
* @return AspectsConfig
*/
public static function loadAspects($configFile, Nette\DI\CompilerExtension $extension)
{
return new AspectsConfig($extension->loadFromFile($configFile), $extension);
}



/**
* @param \Nette\Configurator $configurator
*/
Expand Down
76 changes: 76 additions & 0 deletions src/Kdyby/Aop/DI/AspectsConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008 Filip Procházka ([email protected])
*
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/

namespace Kdyby\Aop\DI;

use Kdyby;
use Nette;



/**
* @author Filip Procházka <[email protected]>
*/
class AspectsConfig extends Nette\Object
{

/**
* @var array
*/
private $aspectsList;

/**
* @var \Nette\DI\CompilerExtension
*/
private $extension;

/**
* @var bool
*/
private $prefix = TRUE;



public function __construct(array $aspectsList, Nette\DI\CompilerExtension $extension)
{
$this->aspectsList = $aspectsList;
$this->extension = $extension;
}



public function disablePrefixing()
{
$this->prefix = FALSE;

return $this;
}



public function load(Nette\DI\Compiler $compiler, Nette\DI\ContainerBuilder $containerBuilder)
{
$aspects = array();
foreach ($this->aspectsList as $def) {
if (!is_array($def)) {
if (!$def instanceof \stdClass || empty($def->entity)) {
$serialised = Nette\Utils\Json::encode($def);
throw new Kdyby\Aop\UnexpectedValueException("The service definition $serialised is expected to be an array or Neon entity.");
}
$def = array('factory' => $def);
}
$def['tags'][] = AopExtension::ASPECT_TAG;
$aspects[] = $def;
}

$compiler->parseServices($containerBuilder, $aspects, $this->prefix ? substr($this->extension->prefix('self'), 0, -5) : NULL);
}

}
45 changes: 45 additions & 0 deletions src/Kdyby/Aop/DI/IAspectsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008 Filip Procházka ([email protected])
*
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/

namespace Kdyby\Aop\DI;

use Kdyby;
use Nette;



/**
* Implement this interface to your CompilerExtension if you want it to provide aspects.
*
* Example:
* <code>
* class AclExtension extends Nette\DI\CompilerExtension implements \Kdyby\Aop\DI\IAspectsProvider
* {
* public function getAspectsConfiguration()
* {
* return \Kdyby\Aop\DI\AopExtension::loadAspects(__DIR__ . '/aspects.neon', $this);
* }
* }
* </code>
*
* The `aspects.neon` file should be list of unnamed services
*
*
* @author Filip Procházka <[email protected]>
*/
interface IAspectsProvider
{

/**
* @return AspectsConfig
*/
function getAspectsConfiguration();

}
30 changes: 30 additions & 0 deletions src/Kdyby/Aop/exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,33 @@ class InvalidStateException extends \RuntimeException implements Exception
{

}



/**
* @author Filip Procházka <[email protected]>
*/
class InvalidArgumentException extends \InvalidArgumentException implements Exception
{

}



/**
* @author Filip Procházka <[email protected]>
*/
class UnexpectedValueException extends \UnexpectedValueException
{

}



/**
* @author Filip Procházka <[email protected]>
*/
class InvalidAspectExceptions extends \LogicException implements Exception
{

}

0 comments on commit 1a502bf

Please sign in to comment.