Created
June 19, 2012 11:05
-
-
Save Hounddog/2953551 to your computer and use it in GitHub Desktop.
Implementing annotations in Zf
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 Doctrine\Common\Annotations\AnnotationRegistry; | |
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { | |
.... | |
public function _registerAnnotations() { | |
AnnotationRegistry::registerNamespace('Annotation'); | |
} | |
} |
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 | |
namespace Annotation\ACL; | |
use Annotation\ACL; | |
class Dependencies | |
{ | |
/** | |
* @ACL\Privileges("name", dataType="string") | |
*/ | |
public function getName() | |
{ | |
return 'Matthias Noback'; | |
} | |
} |
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 | |
namespace Annotation\ACL; | |
/** | |
* @Annotation | |
*/ | |
class Privileges | |
{ | |
private $propertyName; | |
private $dataType = 'string'; | |
public function __construct($options) | |
{ | |
if (isset($options['value'])) { | |
$options['propertyName'] = $options['value']; | |
unset($options['value']); | |
} | |
foreach ($options as $key => $value) { | |
if (!property_exists($this, $key)) { | |
throw new \InvalidArgumentException(sprintf('Property "%s" does not exist', $key)); | |
} | |
$this->$key = $value; | |
} | |
} | |
public function getPropertyName() | |
{ | |
return $this->propertyName; | |
} | |
public function getDataType() | |
{ | |
return $this->dataType; | |
} | |
} |
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 | |
namespace Annotation\Conversion; | |
use Doctrine\Common\Annotations\Reader; | |
class PrivilegesConverter | |
{ | |
private $reader; | |
private $annotationClass = 'Annotation\\Privileges'; | |
public function __construct(Reader $reader) | |
{ | |
$this->reader = $reader; | |
} | |
public function convert($originalObject) | |
{ | |
$convertedObject = new \stdClass; | |
$reflectionObject = new \ReflectionObject($originalObject); | |
foreach ($reflectionObject->getMethods() as $reflectionMethod) { | |
// fetch the @StandardObject annotation from the annotation reader | |
$annotation = $this->reader->getMethodAnnotation($reflectionMethod, $this->annotationClass); | |
if (null !== $annotation) { | |
$propertyName = $annotation->getPropertyName(); | |
// retrieve the value for the property, by making a call to the method | |
$value = $reflectionMethod->invoke($originalObject); | |
// try to convert the value to the requested type | |
$type = $annotation->getDataType(); | |
if (false === settype($value, $type)) { | |
throw new \RuntimeException(sprintf('Could not convert value to type "%s"', $value)); | |
} | |
$convertedObject->$propertyName = $value; | |
} | |
} | |
return $convertedObject; | |
} | |
} |
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 Doctrine\Common\Annotations\AnnotationReader; | |
use Annotation\Conversion\PrivilegesConverter; | |
use Annotation\ACL\Dependencies; | |
/** | |
* @package | |
* @copyright 2011 Doyousoft | |
* @version $Id | |
* | |
*/ | |
class Acl_Admin_TestController | |
extends Pwb_ControllerAbstract | |
{ | |
public function init() | |
{ | |
parent::init(); | |
$this->_helper->viewRenderer->setNoRender(true); | |
$this->_helper->layout->disableLayout(); | |
} | |
public function indexAction() | |
{ | |
$reader = new AnnotationReader(); | |
$converter = new PrivilegesConverter($reader); | |
$person = new Dependencies(); | |
$standardObject = $converter->convert($person); | |
print_r($standardObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment