Last active
December 20, 2015 15:29
-
-
Save faizalpribadi/6154233 to your computer and use it in GitHub Desktop.
Inject Annotation On Property Target For Use Other Class
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
<?php | |
use Mozart\Library\Annotations as Mozart; | |
class User | |
{ | |
/** | |
* @Mozart\Inject("Mapper") | |
*/ | |
protected $mapper; | |
/** | |
* @Mozart\Inject("Repository") | |
*/ | |
protected $repository; | |
} | |
class Mapper | |
{ | |
protected $name; | |
public function getName() | |
{ | |
$this->name = 'John'; | |
return $this->name . "\r\n"; | |
} | |
} | |
class Repository | |
{ | |
protected $url; | |
protected $branch; | |
public function getUrl() | |
{ | |
$this->url = 'https://github.com'; | |
return $this; | |
} | |
public function getBranch() | |
{ | |
$this->branch = 'master'; | |
return $this; | |
} | |
public function cloning() | |
{ | |
return $this->url . PHP_EOL . $this->branch; | |
} | |
} | |
$loader = new Mozart\Loader\AnnotationLoader(); | |
$propertyMapper = new \ReflectionProperty('User', 'mapper'); | |
$propertyRepository = new \ReflectionProperty('User', 'repository'); | |
$propertyOne = $loader->getAnnotationReader()->getPropertyAnnotations($propertyMapper); | |
$propertyTwo = $loader->getAnnotationReader()->getPropertyAnnotations($propertyRepository); | |
foreach ($propertyOne as $objectMapper) { | |
foreach ($objectMapper as $objectOne) { | |
$mapper = new $objectOne(); | |
echo $mapper->getName(); | |
} | |
} | |
foreach ($propertyTwo as $objectRepository) { | |
foreach ($objectRepository as $objectTwo) { | |
$repository = new $objectTwo(); | |
$repository | |
->getUrl() | |
->getBranch(); | |
echo $repository->cloning(); | |
} | |
} | |
// Output : (string) John - https://github.com - master |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment