Skip to content

Instantly share code, notes, and snippets.

@Hounddog
Created June 19, 2012 11:05
Show Gist options
  • Save Hounddog/2953551 to your computer and use it in GitHub Desktop.
Save Hounddog/2953551 to your computer and use it in GitHub Desktop.
Implementing annotations in Zf
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
....
public function _registerAnnotations() {
AnnotationRegistry::registerNamespace('Annotation');
}
}
<?php
namespace Annotation\ACL;
use Annotation\ACL;
class Dependencies
{
/**
* @ACL\Privileges("name", dataType="string")
*/
public function getName()
{
return 'Matthias Noback';
}
}
<?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;
}
}
<?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;
}
}
<?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