Skip to content

Commit d0c11d0

Browse files
committed
[BC BREAK] simplify interface of IntroductionAdvisor, also allow only one interface/trait per introduction
1 parent 97db001 commit d0c11d0

7 files changed

Lines changed: 41 additions & 99 deletions

File tree

src/Aop/Framework/TraitIntroductionInfo.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,48 @@ class TraitIntroductionInfo implements IntroductionInfo
1919
{
2020

2121
/**
22-
* List of interfaces to introduce
22+
* Introduced interface
2323
*
24-
* @var array
24+
* @var string
2525
*/
26-
private $introducedInterfaces;
26+
private $introducedInterface = '';
2727

2828
/**
29-
* List of traits to include
29+
* Trait to use
3030
*
31-
* @var array
31+
* @var string
3232
*/
33-
private $introducedTraits;
33+
private $introducedTrait = '';
3434

3535
/**
3636
* Create a DefaultIntroductionAdvisor for the given advice.
3737
*
38-
* @param string|string[] $introducedInterfaces List of introduced interfaces
39-
* @param string|string[] $introducedTraits List of introduced traits
38+
* @param string $introducedTrait Introduced trait
39+
* @param string $introducedInterface Introduced interface
4040
*/
41-
public function __construct($introducedInterfaces, $introducedTraits)
41+
public function __construct($introducedTrait, $introducedInterface)
4242
{
43-
$this->introducedInterfaces = (array) $introducedInterfaces;
44-
$this->introducedTraits = (array) $introducedTraits;
43+
$this->introducedTrait = $introducedTrait;
44+
$this->introducedInterface = $introducedInterface;
4545
}
4646

4747
/**
48-
* Return the additional interfaces introduced by this Advisor or Advice.
48+
* Return the additional interface introduced by this Advisor or Advice.
4949
*
50-
* @return array|string[] introduced interfaces
50+
* @return string The introduced interface or empty
5151
*/
52-
public function getInterfaces()
52+
public function getInterface()
5353
{
54-
return $this->introducedInterfaces;
54+
return $this->introducedInterface;
5555
}
5656

5757
/**
58-
* Return the list of traits with realization of introduced interfaces
58+
* Return the additional trait with realization of introduced interface
5959
*
60-
* @return array|string[] trait implementations
60+
* @return string The trait name to use or empty
6161
*/
62-
public function getTraits()
62+
public function getTrait()
6363
{
64-
return $this->introducedTraits;
64+
return $this->introducedTrait;
6565
}
6666
}

src/Aop/IntroductionAdvisor.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,4 @@ interface IntroductionAdvisor extends Advisor
2929
* @return PointFilter The class filter
3030
*/
3131
public function getClassFilter();
32-
33-
/**
34-
* Can the advised interfaces be implemented by the introduction advice?
35-
*
36-
* Invoked before adding an IntroductionAdvisor.
37-
*
38-
* @return void
39-
* @throws \InvalidArgumentException if the advised interfaces can't be implemented by the introduction advice
40-
*/
41-
public function validateInterfaces();
4232
}

src/Aop/IntroductionInfo.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ interface IntroductionInfo extends Advice
2121
{
2222

2323
/**
24-
* Return the additional interfaces introduced by this Advisor or Advice.
24+
* Return the additional interface introduced by this Advisor or Advice.
2525
*
26-
* @return array|string[] the introduced interfaces
26+
* @return string The introduced interface or empty
2727
*/
28-
public function getInterfaces();
28+
public function getInterface();
2929

3030
/**
31-
* Return the list of traits with realization of introduced interfaces
31+
* Return the additional trait with realization of introduced interface
3232
*
33-
* @return array|string[] the implementations
33+
* @return string The trait name to use or empty
3434
*/
35-
public function getTraits();
35+
public function getTrait();
3636
}

src/Aop/Support/DeclareParentsAdvisor.php

Lines changed: 6 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010

1111
namespace Go\Aop\Support;
1212

13-
use InvalidArgumentException;
14-
use ReflectionClass;
15-
use Go\Aop\Advice;
16-
use Go\Aop\PointFilter;
17-
use Go\Aop\IntroductionInfo;
1813
use Go\Aop\IntroductionAdvisor;
14+
use Go\Aop\IntroductionInfo;
15+
use Go\Aop\PointFilter;
1916

2017
/**
2118
* Introduction advisor delegating to the given object.
@@ -24,7 +21,9 @@ class DeclareParentsAdvisor implements IntroductionAdvisor
2421
{
2522

2623
/**
27-
* @var null|IntroductionInfo
24+
* Information about introduced interface/trait
25+
*
26+
* @var IntroductionInfo
2827
*/
2928
private $advice;
3029

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

47-
/**
48-
* Can the advised interfaces be implemented by the introduction advice?
49-
*
50-
* Invoked before adding an IntroductionAdvisor.
51-
*
52-
* @return void
53-
* @throws \InvalidArgumentException if the advised interfaces can't be implemented by the introduction advice
54-
*/
55-
public function validateInterfaces()
56-
{
57-
$refInterface = new ReflectionClass(reset($this->advice->getInterfaces()));
58-
$refImplementation = new ReflectionClass(reset($this->advice->getTraits()));
59-
if (!$refInterface->isInterface()) {
60-
throw new \InvalidArgumentException("Only interface can be introduced");
61-
}
62-
if (!$refImplementation->isTrait()) {
63-
throw new \InvalidArgumentException("Only trait can be used as implementation");
64-
}
65-
66-
foreach ($refInterface->getMethods() as $interfaceMethod) {
67-
if (!$refImplementation->hasMethod($interfaceMethod->name)) {
68-
throw new \DomainException("Implementation requires method {$interfaceMethod->name}");
69-
}
70-
}
71-
}
72-
7346
/**
7447
* Returns an advice to apply
7548
*
76-
* @return Advice|IntroductionInfo|null
49+
* @return IntroductionInfo
7750
*/
7851
public function getAdvice()
7952
{
@@ -91,27 +64,4 @@ public function getClassFilter()
9164
{
9265
return $this->classFilter;
9366
}
94-
95-
/**
96-
* Set the class filter for advisor
97-
*
98-
* @param PointFilter $classFilter Filter for classes
99-
*/
100-
public function setClassFilter(PointFilter $classFilter)
101-
{
102-
$this->classFilter = $classFilter;
103-
}
104-
105-
/**
106-
* Return string representation of object
107-
*
108-
* @return string
109-
*/
110-
public function __toString()
111-
{
112-
$adviceClass = get_class($this->advice);
113-
$interfaceClasses = implode(',', $this->advice->getInterfaces());
114-
115-
return get_called_class() . ": advice [{$adviceClass}]; interfaces [{$interfaceClasses}] ";
116-
}
11767
}

src/Core/IntroductionAspectExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ public function load(Aspect $aspect, $reflection, $metaInformation = null)
8080

8181
switch (true) {
8282
case ($metaInformation instanceof Annotation\DeclareParents):
83-
$interface = $metaInformation->interface;
8483
$implement = $metaInformation->defaultImpl;
85-
$advice = new Framework\TraitIntroductionInfo($interface, $implement);
84+
$interface = $metaInformation->interface;
85+
$advice = new Framework\TraitIntroductionInfo($implement, $interface);
8686
$advisor = new Support\DeclareParentsAdvisor($pointcut->getClassFilter(), $advice);
8787
$loadedItems[$propertyId] = $advisor;
8888
break;

src/Lang/Annotation/DeclareParents.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*
1919
* @Attributes({
2020
* @Attribute("value", type = "string", required=true),
21-
* @Attribute("interface", type = "array"),
22-
* @Attribute("defaultImpl", type = "array")
21+
* @Attribute("interface", type = "string"),
22+
* @Attribute("defaultImpl", type = "string")
2323
* })
2424
*/
2525
class DeclareParents extends BaseAnnotation

src/Proxy/ClassProxy.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,13 @@ public function __construct(ReflectionClass $parent, array $classAdvices)
147147
case AspectContainer::INTRODUCTION_TRAIT_PREFIX:
148148
foreach ($typedAdvices as $advice) {
149149
/** @var $advice IntroductionInfo */
150-
foreach ($advice->getInterfaces() as $interface) {
151-
$this->addInterface($interface);
150+
$introducedTrait = $advice->getTrait();
151+
if (!empty($introducedTrait)) {
152+
$this->addTrait($introducedTrait);
152153
}
153-
foreach ($advice->getTraits() as $trait) {
154-
$this->addTrait($trait);
154+
$introducedInterface = $advice->getInterface();
155+
if (!empty($introducedInterface)) {
156+
$this->addInterface($introducedInterface);
155157
}
156158
}
157159
break;

0 commit comments

Comments
 (0)