-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* introduction: Update demo to show DeclareParents in action #32 Initial support for Introduction advice #32 Added TraitIntroductionInfo advice #32 Fix typo in the class name #32
- Loading branch information
Showing
13 changed files
with
468 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* Go! OOP&AOP PHP framework | ||
* | ||
* @copyright Copyright 2012, Lissachenko Alexander <[email protected]> | ||
* @license http://www.opensource.org/licenses/mit-license.php The MIT License | ||
*/ | ||
|
||
namespace Aspect\Introduce; | ||
|
||
/** | ||
* Example class to test aspects | ||
*/ | ||
trait SerializableImpl | ||
{ | ||
/** | ||
* String representation of object | ||
* @return string the string representation of the object or null | ||
*/ | ||
public function serialize() | ||
{ | ||
return serialize(get_object_vars($this)); | ||
} | ||
|
||
/** | ||
* Constructs the object | ||
* @param string $serialized <p> | ||
* The string representation of the object. | ||
* </p> | ||
* @return mixed the original value unserialized. | ||
*/ | ||
public function unserialize($serialized) | ||
{ | ||
$data = unserialize($serialized); | ||
foreach($data as $key=>$value) { | ||
$this->$key = $value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* Go! OOP&AOP PHP framework | ||
* | ||
* @copyright Copyright 2013, Lissachenko Alexander <[email protected]> | ||
* @license http://www.opensource.org/licenses/mit-license.php The MIT License | ||
*/ | ||
|
||
namespace Go\Aop\Framework; | ||
|
||
use Go\Aop\IntroductionInfo; | ||
|
||
/** | ||
* @package go | ||
*/ | ||
class TraitIntroductionInfo implements IntroductionInfo | ||
{ | ||
|
||
/** | ||
* Name of the interface to introduce | ||
* | ||
* @var string | ||
*/ | ||
private $introducedInterface; | ||
|
||
/** | ||
* Name of the class with implementation (trait) | ||
* | ||
* @var string | ||
*/ | ||
private $implementationClass; | ||
|
||
/** | ||
* Create a DefaultIntroductionAdvisor for the given advice. | ||
*/ | ||
public function __construct($interfaceType, $implementationClass) | ||
{ | ||
$this->introducedInterface = $interfaceType; | ||
$this->implementationClass = $implementationClass; | ||
} | ||
|
||
/** | ||
* Return the additional interfaces introduced by this Advisor or Advice. | ||
* | ||
* @return array|string[] the introduced interfaces | ||
*/ | ||
public function getInterfaces() | ||
{ | ||
return array($this->introducedInterface); | ||
} | ||
|
||
/** | ||
* Return the list of traits with realization of introduced interfaces | ||
* | ||
* @return array|string[] the implementations | ||
*/ | ||
public function getTraits() | ||
{ | ||
return array($this->implementationClass); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
/** | ||
* Go! OOP&AOP PHP framework | ||
* | ||
* @copyright Copyright 2012, Lissachenko Alexander <[email protected]> | ||
* @license http://www.opensource.org/licenses/mit-license.php The MIT License | ||
*/ | ||
|
||
namespace Go\Aop\Support; | ||
|
||
use InvalidArgumentException; | ||
use ReflectionClass; | ||
|
||
use Go\Aop\Advice; | ||
use Go\Aop\ClassFilter; | ||
use Go\Aop\IntroductionInfo; | ||
use Go\Aop\IntroductionAdvisor; | ||
|
||
/** | ||
* Introduction advisor delegating to the given object. | ||
*/ | ||
class DeclareParentsAdvisor implements IntroductionAdvisor | ||
{ | ||
|
||
/** | ||
* @var null|IntroductionInfo | ||
*/ | ||
private $advice = null; | ||
|
||
/** | ||
* Type pattern the introduction is restricted to | ||
* | ||
* @var ClassFilter | ||
*/ | ||
private $classFilter; | ||
|
||
/** | ||
* Create a DefaultIntroductionAdvisor for the given advice. | ||
*/ | ||
public function __construct(ClassFilter $classFilter, IntroductionInfo $info) | ||
{ | ||
$this->classFilter = $classFilter; | ||
$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 | ||
*/ | ||
public function getAdvice() | ||
{ | ||
return $this->advice; | ||
} | ||
|
||
/** | ||
* Return whether this advice is associated with a particular instance or shared with all instances | ||
* of the advised class | ||
* | ||
* @return bool Whether this advice is associated with a particular target instance | ||
*/ | ||
public function isPerInstance() | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* Return the filter determining which target classes this introduction should apply to. | ||
* | ||
* This represents the class part of a pointcut. Note that method matching doesn't make sense to introductions. | ||
* | ||
* @return ClassFilter The class filter | ||
*/ | ||
public function getClassFilter() | ||
{ | ||
return $this->classFilter; | ||
} | ||
|
||
/** | ||
* Set the class filter for advisor | ||
* | ||
* @param ClassFilter $classFilter Filter for classes | ||
*/ | ||
public function setClassFilter(ClassFilter $classFilter) | ||
{ | ||
$this->classFilter = $classFilter; | ||
} | ||
|
||
/** | ||
* Return string representation of object | ||
* | ||
* @return string | ||
*/ | ||
public function __toString() | ||
{ | ||
$adviceClass = get_class($this->advice); | ||
$interfaceClasses = join(',', $this->advice->getInterfaces()); | ||
return get_called_class() . ": advice [{$adviceClass}]; interfaces [{$interfaceClasses}] "; | ||
} | ||
} |
Oops, something went wrong.