Skip to content

Commit

Permalink
[BC BREAK] simplify interface of IntroductionAdvisor, also allow only…
Browse files Browse the repository at this point in the history
… one interface/trait per introduction
  • Loading branch information
lisachenko committed Aug 19, 2017
1 parent 97db001 commit d0c11d0
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 99 deletions.
38 changes: 19 additions & 19 deletions src/Aop/Framework/TraitIntroductionInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,48 @@ class TraitIntroductionInfo implements IntroductionInfo
{

/**
* List of interfaces to introduce
* Introduced interface
*
* @var array
* @var string
*/
private $introducedInterfaces;
private $introducedInterface = '';

/**
* List of traits to include
* Trait to use
*
* @var array
* @var string
*/
private $introducedTraits;
private $introducedTrait = '';

/**
* Create a DefaultIntroductionAdvisor for the given advice.
*
* @param string|string[] $introducedInterfaces List of introduced interfaces
* @param string|string[] $introducedTraits List of introduced traits
* @param string $introducedTrait Introduced trait
* @param string $introducedInterface Introduced interface
*/
public function __construct($introducedInterfaces, $introducedTraits)
public function __construct($introducedTrait, $introducedInterface)
{
$this->introducedInterfaces = (array) $introducedInterfaces;
$this->introducedTraits = (array) $introducedTraits;
$this->introducedTrait = $introducedTrait;
$this->introducedInterface = $introducedInterface;
}

/**
* Return the additional interfaces introduced by this Advisor or Advice.
* Return the additional interface introduced by this Advisor or Advice.
*
* @return array|string[] introduced interfaces
* @return string The introduced interface or empty
*/
public function getInterfaces()
public function getInterface()
{
return $this->introducedInterfaces;
return $this->introducedInterface;
}

/**
* Return the list of traits with realization of introduced interfaces
* Return the additional trait with realization of introduced interface
*
* @return array|string[] trait implementations
* @return string The trait name to use or empty
*/
public function getTraits()
public function getTrait()
{
return $this->introducedTraits;
return $this->introducedTrait;
}
}
10 changes: 0 additions & 10 deletions src/Aop/IntroductionAdvisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,4 @@ interface IntroductionAdvisor extends Advisor
* @return PointFilter The class filter
*/
public function getClassFilter();

/**
* Can the advised interfaces be implemented by the introduction advice?
*
* Invoked before adding an IntroductionAdvisor.
*
* @return void
* @throws \InvalidArgumentException if the advised interfaces can't be implemented by the introduction advice
*/
public function validateInterfaces();
}
12 changes: 6 additions & 6 deletions src/Aop/IntroductionInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ interface IntroductionInfo extends Advice
{

/**
* Return the additional interfaces introduced by this Advisor or Advice.
* Return the additional interface introduced by this Advisor or Advice.
*
* @return array|string[] the introduced interfaces
* @return string The introduced interface or empty
*/
public function getInterfaces();
public function getInterface();

/**
* Return the list of traits with realization of introduced interfaces
* Return the additional trait with realization of introduced interface
*
* @return array|string[] the implementations
* @return string The trait name to use or empty
*/
public function getTraits();
public function getTrait();
}
62 changes: 6 additions & 56 deletions src/Aop/Support/DeclareParentsAdvisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@

namespace Go\Aop\Support;

use InvalidArgumentException;
use ReflectionClass;
use Go\Aop\Advice;
use Go\Aop\PointFilter;
use Go\Aop\IntroductionInfo;
use Go\Aop\IntroductionAdvisor;
use Go\Aop\IntroductionInfo;
use Go\Aop\PointFilter;

/**
* Introduction advisor delegating to the given object.
Expand All @@ -24,7 +21,9 @@ class DeclareParentsAdvisor implements IntroductionAdvisor
{

/**
* @var null|IntroductionInfo
* Information about introduced interface/trait
*
* @var IntroductionInfo
*/
private $advice;

Expand All @@ -44,36 +43,10 @@ public function __construct(PointFilter $classFilter, IntroductionInfo $info)
$this->advice = $info;
}

/**
* Can the advised interfaces be implemented by the introduction advice?
*
* Invoked before adding an IntroductionAdvisor.
*
* @return void
* @throws \InvalidArgumentException if the advised interfaces can't be implemented by the introduction advice
*/
public function validateInterfaces()
{
$refInterface = new ReflectionClass(reset($this->advice->getInterfaces()));
$refImplementation = new ReflectionClass(reset($this->advice->getTraits()));
if (!$refInterface->isInterface()) {
throw new \InvalidArgumentException("Only interface can be introduced");
}
if (!$refImplementation->isTrait()) {
throw new \InvalidArgumentException("Only trait can be used as implementation");
}

foreach ($refInterface->getMethods() as $interfaceMethod) {
if (!$refImplementation->hasMethod($interfaceMethod->name)) {
throw new \DomainException("Implementation requires method {$interfaceMethod->name}");
}
}
}

/**
* Returns an advice to apply
*
* @return Advice|IntroductionInfo|null
* @return IntroductionInfo
*/
public function getAdvice()
{
Expand All @@ -91,27 +64,4 @@ public function getClassFilter()
{
return $this->classFilter;
}

/**
* Set the class filter for advisor
*
* @param PointFilter $classFilter Filter for classes
*/
public function setClassFilter(PointFilter $classFilter)
{
$this->classFilter = $classFilter;
}

/**
* Return string representation of object
*
* @return string
*/
public function __toString()
{
$adviceClass = get_class($this->advice);
$interfaceClasses = implode(',', $this->advice->getInterfaces());

return get_called_class() . ": advice [{$adviceClass}]; interfaces [{$interfaceClasses}] ";
}
}
4 changes: 2 additions & 2 deletions src/Core/IntroductionAspectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public function load(Aspect $aspect, $reflection, $metaInformation = null)

switch (true) {
case ($metaInformation instanceof Annotation\DeclareParents):
$interface = $metaInformation->interface;
$implement = $metaInformation->defaultImpl;
$advice = new Framework\TraitIntroductionInfo($interface, $implement);
$interface = $metaInformation->interface;
$advice = new Framework\TraitIntroductionInfo($implement, $interface);
$advisor = new Support\DeclareParentsAdvisor($pointcut->getClassFilter(), $advice);
$loadedItems[$propertyId] = $advisor;
break;
Expand Down
4 changes: 2 additions & 2 deletions src/Lang/Annotation/DeclareParents.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*
* @Attributes({
* @Attribute("value", type = "string", required=true),
* @Attribute("interface", type = "array"),
* @Attribute("defaultImpl", type = "array")
* @Attribute("interface", type = "string"),
* @Attribute("defaultImpl", type = "string")
* })
*/
class DeclareParents extends BaseAnnotation
Expand Down
10 changes: 6 additions & 4 deletions src/Proxy/ClassProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ public function __construct(ReflectionClass $parent, array $classAdvices)
case AspectContainer::INTRODUCTION_TRAIT_PREFIX:
foreach ($typedAdvices as $advice) {
/** @var $advice IntroductionInfo */
foreach ($advice->getInterfaces() as $interface) {
$this->addInterface($interface);
$introducedTrait = $advice->getTrait();
if (!empty($introducedTrait)) {
$this->addTrait($introducedTrait);
}
foreach ($advice->getTraits() as $trait) {
$this->addTrait($trait);
$introducedInterface = $advice->getInterface();
if (!empty($introducedInterface)) {
$this->addInterface($introducedInterface);
}
}
break;
Expand Down

0 comments on commit d0c11d0

Please sign in to comment.