Skip to content

Instantly share code, notes, and snippets.

@faizalpribadi
Last active December 20, 2015 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faizalpribadi/6154233 to your computer and use it in GitHub Desktop.
Save faizalpribadi/6154233 to your computer and use it in GitHub Desktop.
Inject Annotation On Property Target For Use Other Class
<?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