annotating with annotations - phpbenelux june/2012

Post on 08-May-2015

1.827 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

What exactly are annotations? How can they change the way you code and make life simpler? Annotations allow you to implement new functionality into code using "notes" this allows you to easily maintain your own architecture but benefit from external tools. Let's look at how annotations are used today in PHP and how we can develop our own solutions based on the existing libraries.

TRANSCRIPT

A����������� ������������������  look����������� ������������������  into����������� ������������������  Annotations����������� ������������������  in����������� ������������������  PHP

Rafael����������� ������������������  Dohms

*����������� ������������������  PHPBenelux����������� ������������������  -����������� ������������������  June����������� ������������������  Meeting

Annotating����������� ������������������  with����������� ������������������  Annotations

@rdohmson����������� ������������������  twitter

A����������� ������������������  look����������� ������������������  into����������� ������������������  Annotations����������� ������������������  in����������� ������������������  PHP

Rafael����������� ������������������  Dohms

*����������� ������������������  PHPBenelux����������� ������������������  -����������� ������������������  June����������� ������������������  Meeting

Annotating����������� ������������������  with����������� ������������������  Annotations

@rdohmson����������� ������������������  twitter

phot

o cr

edit:

Eli W

hite

Evangelist, Speaker and Contributor.

Developer at WEBclusive.

Enabler at AmsterdamPHP.

Rafael Dohms@rdohms

What? Why? Where?

How?

a����������� ������������������  little����������� ������������������  history

show����������� ������������������  me����������� ������������������  the����������� ������������������  code!

existing����������� ������������������  uses

Implementing����������� ������������������  custom����������� ������������������  annotations

based����������� ������������������  on����������� ������������������  DMS\Filter

what?What����������� ������������������  ar

e����������� ������������������  annotation

s?

http://ecdesignrebels.blogspot.com

-- In English --"An annotation is a note that is made while

reading any form of text."

-- In English --"An annotation is a note that is made while

reading any form of text."

something����������� ������������������  that����������� ������������������  describes����������� ������������������  an����������� ������������������  aspect����������� ������������������  of����������� ������������������  the����������� ������������������  subject

“An annotation describes behavior of code and affects your application in runtime.”

“Annotations do not directly affect program semantics”

-- In Code Speak --

“An annotation describes behavior of code and affects your application in runtime.”

“Annotations do not directly affect program semantics”

-- In Code Speak --

just����������� ������������������  like����������� ������������������  your����������� ������������������  notes

In����������� ������������������  2004:����������� ������������������  Metadata����������� ������������������  or����������� ������������������  “General����������� ������������������  purpose����������� ������������������  Annotations”

In����������� ������������������  2004:����������� ������������������  Metadata����������� ������������������  or����������� ������������������  “General����������� ������������������  purpose����������� ������������������  Annotations”

available����������� ������������������  at����������� ������������������  Runtime����������� ������������������  using����������� ������������������  Reflection

In����������� ������������������  2004:����������� ������������������  Metadata����������� ������������������  or����������� ������������������  “General����������� ������������������  purpose����������� ������������������  Annotations”

available����������� ������������������  at����������� ������������������  Runtime����������� ������������������  using����������� ������������������  Reflection

@Entity @Table(name = "people") class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(length = 32) private String name;

In����������� ������������������  2004:����������� ������������������  Metadata����������� ������������������  or����������� ������������������  “General����������� ������������������  purpose����������� ������������������  Annotations”

available����������� ������������������  at����������� ������������������  Runtime����������� ������������������  using����������� ������������������  Reflection

@Entity @Table(name = "people") class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(length = 32) private String name;

specific����������� ������������������  syntax

No����������� ������������������  core����������� ������������������  annotation����������� ������������������  support

Questions?

Questions?

I’m����������� ������������������  kidding!

phpDoc

~2000

phpDoc

PHP����������� ������������������  5.1Reflection����������� ������������������  supports����������� ������������������  getDocComments()

~2000 2005

First����������� ������������������  Annotation

����������� ������������������  Engines����������� ������������������  in����������� ������������������  PHP

phpDoc

PHP����������� ������������������  5.1Reflection����������� ������������������  supports����������� ������������������  getDocComments()

~2000 2005

First����������� ������������������  Annotation

����������� ������������������  Engines����������� ������������������  in����������� ������������������  PHP

phpDoc

PHP����������� ������������������  5.1Reflection����������� ������������������  supports����������� ������������������  getDocComments()

~2000 2005 2008

First����������� ������������������  Annotation

����������� ������������������  Engines����������� ������������������  in����������� ������������������  PHP

phpDoc

PHP����������� ������������������  5.1Reflection����������� ������������������  supports����������� ������������������  getDocComments()

~2000 2005 2008

Doctrine����������� ������������������  2����������� ������������������  Annotation����������� ������������������  Engine

First����������� ������������������  Annotation

����������� ������������������  Engines����������� ������������������  in����������� ������������������  PHP

phpDoc

PHP����������� ������������������  5.1Reflection����������� ������������������  supports����������� ������������������  getDocComments()

~2000 2005 2008

Doctrine����������� ������������������  2����������� ������������������  Annotation����������� ������������������  Engine

2010

RFC:����������� ������������������  Annotations����������� ������������������  in����������� ������������������  core����������� ������������������  w/����������� ������������������  custom����������� ������������������  

syntax

First����������� ������������������  Annotation

����������� ������������������  Engines����������� ������������������  in����������� ������������������  PHP

phpDoc

PHP����������� ������������������  5.1Reflection����������� ������������������  supports����������� ������������������  getDocComments()

~2000 2005 2008

Doctrine����������� ������������������  2����������� ������������������  Annotation����������� ������������������  Engine

2010

RFC:����������� ������������������  Annotations����������� ������������������  in����������� ������������������  core����������� ������������������  w/����������� ������������������  custom����������� ������������������  

syntaxREJECTED

First����������� ������������������  Annotation

����������� ������������������  Engines����������� ������������������  in����������� ������������������  PHP

phpDoc

PHP����������� ������������������  5.1Reflection����������� ������������������  supports����������� ������������������  getDocComments()

~2000 2005 2008

Doctrine����������� ������������������  2����������� ������������������  Annotation����������� ������������������  Engine

2010

RFC:����������� ������������������  Annotations����������� ������������������  in����������� ������������������  core����������� ������������������  w/����������� ������������������  custom����������� ������������������  

syntax

2011

RFC:����������� ������������������  DocBlock����������� ������������������  Annotations

“in����������� ������������������  discussion”

REJECTED

First����������� ������������������  Annotation

����������� ������������������  Engines����������� ������������������  in����������� ������������������  PHP

phpDoc

PHP����������� ������������������  5.1Reflection����������� ������������������  supports����������� ������������������  getDocComments()

~2000 2005 2008

Doctrine����������� ������������������  2����������� ������������������  Annotation����������� ������������������  Engine

2010

RFC:����������� ������������������  Annotations����������� ������������������  in����������� ������������������  core����������� ������������������  w/����������� ������������������  custom����������� ������������������  

syntax

2011

RFC:����������� ������������������  DocBlock����������� ������������������  Annotations

“in����������� ������������������  discussion”

REJECTED

?

First����������� ������������������  Annotation

����������� ������������������  Engines����������� ������������������  in����������� ������������������  PHP

phpDoc

PHP����������� ������������������  5.1Reflection����������� ������������������  supports����������� ������������������  getDocComments()

~2000 2005 2008

Doctrine����������� ������������������  2����������� ������������������  Annotation����������� ������������������  Engine

2010

RFC:����������� ������������������  Annotations����������� ������������������  in����������� ������������������  core����������� ������������������  w/����������� ������������������  custom����������� ������������������  

syntax

2011

RFC:����������� ������������������  DocBlock����������� ������������������  Annotations

“in����������� ������������������  discussion”

REJECTED

?

/** * Caas\Bundle\FundingBundle\Entity\Reward * * @ORM\Table("reward") * @ORM\Entity(repositoryClass="Caas\Bundle\FundingBundle\Entity\RewardRepository") */ class Reward { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var string $title * * @ORM\Column(name="title", type="string", length=150, nullable=true) * * @Assert\MaxLength(150) */ protected $title;

Note:

Note:

//����������� ������������������  This����������� ������������������  is����������� ������������������  a����������� ������������������  comment

/*����������� ������������������  This����������� ������������������  is����������� ������������������  a����������� ������������������  multiline����������� ������������������  comment����������� ������������������  */T_COMMENT

Note:

//����������� ������������������  This����������� ������������������  is����������� ������������������  a����������� ������������������  comment

/*����������� ������������������  This����������� ������������������  is����������� ������������������  a����������� ������������������  multiline����������� ������������������  comment����������� ������������������  */

/**����������� ������������������  ����������� ������������������  *����������� ������������������  This����������� ������������������  is����������� ������������������  a����������� ������������������  doc����������� ������������������  block����������� ������������������  */

T_COMMENT

T_DOC_COMMENT

/** * @deprecated * * @ORM\Column(‘string’, length=255) */public function method()

/** * @deprecated * * @ORM\Column(‘string’, length=255) */public function method()

marker

/** * @deprecated * * @ORM\Column(‘string’, length=255) */public function method()

markerparameterized

Why?Why����������� ������������������  sho

uld����������� ������������������  I����������� ������������������  use����������� ������������������  ann

otations?

http://ecdesignrebels.blogspot.com

what����������� ������������������  are����������� ������������������  the����������� ������������������  

pros/cons?

-

Harder����������� ������������������  to����������� ������������������  debug����������� ������������������  (dynamic����������� ������������������  code)

-

Harder����������� ������������������  to����������� ������������������  debug����������� ������������������  (dynamic����������� ������������������  code)

- Performance����������� ������������������  hit����������� ������������������  (non-native����������� ������������������  code)

Harder����������� ������������������  to����������� ������������������  debug����������� ������������������  (dynamic����������� ������������������  code)

Its����������� ������������������  implemented����������� ������������������  in����������� ������������������  comments����������� ������������������  docblocks!

- Performance����������� ������������������  hit����������� ������������������  (non-native����������� ������������������  code)

+

Easier����������� ������������������  to����������� ������������������  inject����������� ������������������  behavior����������� ������������������  without����������� ������������������  extending

+

Easier����������� ������������������  to����������� ������������������  inject����������� ������������������  behavior����������� ������������������  without����������� ������������������  extending

+

Doctrine����������� ������������������  1����������� ������������������  vs����������� ������������������  Doctrine����������� ������������������  2

Easier����������� ������������������  to����������� ������������������  inject����������� ������������������  behavior����������� ������������������  without����������� ������������������  extending

Contextualizes����������� ������������������  behavior/config����������� ������������������  in����������� ������������������  the����������� ������������������  object+

Doctrine����������� ������������������  1����������� ������������������  vs����������� ������������������  Doctrine����������� ������������������  2

Easier����������� ������������������  to����������� ������������������  inject����������� ������������������  behavior����������� ������������������  without����������� ������������������  extending

Contextualizes����������� ������������������  behavior/config����������� ������������������  in����������� ������������������  the����������� ������������������  object+

Doctrine����������� ������������������  1����������� ������������������  vs����������� ������������������  Doctrine����������� ������������������  2

In����������� ������������������  object����������� ������������������  vs.����������� ������������������  external����������� ������������������  configuration����������� ������������������  file

Easier����������� ������������������  to����������� ������������������  inject����������� ������������������  behavior����������� ������������������  without����������� ������������������  extending

Contextualizes����������� ������������������  behavior/config����������� ������������������  in����������� ������������������  the����������� ������������������  object+

Doctrine����������� ������������������  1����������� ������������������  vs����������� ������������������  Doctrine����������� ������������������  2

In����������� ������������������  object����������� ������������������  vs.����������� ������������������  external����������� ������������������  configuration����������� ������������������  file

Its����������� ������������������  documented/stored����������� ������������������  by����������� ������������������  phpDocumentor

Easier����������� ������������������  to����������� ������������������  inject����������� ������������������  behavior����������� ������������������  without����������� ������������������  extending

Contextualizes����������� ������������������  behavior/config����������� ������������������  in����������� ������������������  the����������� ������������������  object+

Doctrine����������� ������������������  1����������� ������������������  vs����������� ������������������  Doctrine����������� ������������������  2

In����������� ������������������  object����������� ������������������  vs.����������� ������������������  external����������� ������������������  configuration����������� ������������������  file

Its����������� ������������������  documented/stored����������� ������������������  by����������� ������������������  phpDocumentorIts����������� ������������������  in����������� ������������������  docblocks,����������� ������������������  so����������� ������������������  its����������� ������������������  parsed

<?php

class User{

protected $name

...

}

<?php

class User{

protected $name

...

}

-����������� ������������������  persist����������� ������������������  as����������� ������������������  varchar

-����������� ������������������  length����������� ������������������  should����������� ������������������  not����������� ������������������  be����������� ������������������  

more����������� ������������������  then����������� ������������������  255

-����������� ������������������  should����������� ������������������  not����������� ������������������  be����������� ������������������  blank

-����������� ������������������  only����������� ������������������  letters

<?php

class User{

protected $name

...

}

-����������� ������������������  persist����������� ������������������  as����������� ������������������  varchar

-����������� ������������������  length����������� ������������������  should����������� ������������������  not����������� ������������������  be����������� ������������������  

more����������� ������������������  then����������� ������������������  255

-����������� ������������������  should����������� ������������������  not����������� ������������������  be����������� ������������������  blank

-����������� ������������������  only����������� ������������������  letters

Acme\BlogBundle\Entity\Author: properties: name: - NotBlank: ~ - MaxLength: 255

Doctrine\Tests\ORM\Mapping\User: type: entity table: cms_users id: id: type: integer generator: strategy: AUTO fields: name: type: string length: 255

Acme\BlogBundle\Entity\Author: filters: name: - alpha

Validation

filter

persistence

/** * @ORM\Column(‘string’, length=255) * @Assert\NotBlank() * @Assert\MaxLength(255) * @Filter\Alpha() */

class User{

protected $name

...

}

<?phpValidation

persistence

filter

Where?Where����������� ������������������  a

re����������� ������������������  annotatio

ns����������� ������������������  used?

http://ecdesignrebels.blogspot.com

class DataTest extends PHPUnit_Framework_TestCase { /** * @dataProvider provider * * @expectedException InvalidArgumentException * @expectedExceptionMessage Right Message */ public function testAdd($a, $b, $c) { /* Test code */ } public function provider() { return array( array(0, 0, 0), ); } }

expectations

repetition

/** * @ORM\Table("myentity") * @ORM\Entity(repositoryClass="MyEntityRepository") */ class MyEntity { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var string $title * * @ORM\Column(name="title", type="string", length=255) * @Assert\MaxLength(255) * @Assert\NotBlank() */ protected $title; /** * @var \Doctrine\Common\Collections\ArrayCollection $users * * @ORM\OneToMany(targetEntity="OtherEntity", mappedBy="myEntity", cascade={"persist","remove"}) */ protected $otherEntities; }

persistance

association

/** * @Route("/myaction/{id}", name="myaction") * @Method("POST") * * @Template("MyBundle:MyController:my.html.twig") * * @param int $id * @return array */ public function myAction($id) { /* Controller Logic */ return array('data' => $data); }

class MyEntity { /** * @var string $title * * @ORM\Column(name="title", type="string", length=255) * @Assert\MaxLength(255) * @Assert\NotBlank() */ protected $title; }

routing

Validation

templating

/** * @FLOW3\Aspect */ class LoggingAspect { /** * @FLOW3\Inject * @var \Examples\Forum\Logger\ApplicationLoggerInterface */ protected $applicationLogger; /** * Log a message if a post is deleted * * @param \TYPO3\FLOW3\AOP\JoinPointInterface $joinPoint * @FLOW3\Before("method(Examples\Forum\Domain\Model\Forum->deletePost())") * @return void */ public function logDeletePost(\TYPO3\FLOW3\AOP\JoinPointInterface $joinPoint) { $post = $joinPoint->getMethodArgument('post'); $this->applicationLogger->log('Removing post ' . $post->getTitle(), LOG_INFO); } }

Dependency����������� ������������������  Injection

AOP

How?How����������� ������������������  can����������� ������������������  i����������� ������������������  

write����������� ������������������  my����������� ������������������  own����������� ������������������  

annotations?

http://ecdesignrebels.blogspot.com

Annotations

My����������� ������������������  Project

Annotations

My����������� ������������������  Project

Annotation����������� ������������������  Engine

/** * @tag parameters */public function method()

Code

/** * @tag parameters */public function method()

Code

ReflectionClass->getDocComment()

DOCBlock

/** * @tag parameters */

/** * @tag parameters */public function method()

Code

ReflectionClass->getDocComment()

DOCBlock

/** * @tag parameters */

/** * @tag parameters */public function method()

Code

ReflectionClass->getDocComment()

DOCBlock

/** * @tag parameters */

Tag() + parameters

Annotation����������� ������������������  Instances

/** * @tag parameters */public function method()

Code

ReflectionClass->getDocComment()

Doctrine����������� ������������������  Commons

ZF2����������� ������������������  Annotations

phpDocumentor����������� ������������������  2

php-annotationsaddendumStubble

abandoned?

Notoj

Annotation Engines

Annotation Engines

Notoj

2.3 2.0����������� ������������������  -����������� ������������������  alpha ?

Parameterized marker parameterized

very����������� ������������������  mature maturing new

Annotation Engines

Notoj

2.3 2.0����������� ������������������  -����������� ������������������  alpha ?

Parameterized marker parameterized

very����������� ������������������  mature maturing new

How����������� ������������������  it����������� ������������������  Works

<?php

/** * @ORM\Column(‘string’) * @Assert\NotBlank() */

use Doctrine\ORM\Mapping as ORM;use Symfony\Component\Validator\ Constraints as Assert;

How����������� ������������������  it����������� ������������������  Works

<?php

/** * @ORM\Column(‘string’) * @Assert\NotBlank() */

use Doctrine\ORM\Mapping as ORM;use Symfony\Component\Validator\ Constraints as Assert;

Declare����������� ������������������  which����������� ������������������  Annotations����������� ������������������  you����������� ������������������  will����������� ������������������  use

How����������� ������������������  it����������� ������������������  Works

AnnotationReader<?php

/** * @ORM\Column(‘string’) * @Assert\NotBlank() */

use Doctrine\ORM\Mapping as ORM;use Symfony\Component\Validator\ Constraints as Assert;

Declare����������� ������������������  which����������� ������������������  Annotations����������� ������������������  you����������� ������������������  will����������� ������������������  use

How����������� ������������������  it����������� ������������������  Works

AnnotationReader<?php

/** * @ORM\Column(‘string’) * @Assert\NotBlank() */

new ORM\Column(‘string’)new Assert\NotBlank()

“metadata”

use Doctrine\ORM\Mapping as ORM;use Symfony\Component\Validator\ Constraints as Assert;

Declare����������� ������������������  which����������� ������������������  Annotations����������� ������������������  you����������� ������������������  will����������� ������������������  use

How����������� ������������������  it����������� ������������������  Works

AnnotationReader<?php

/** * @ORM\Column(‘string’) * @Assert\NotBlank() */

new ORM\Column(‘string’)new Assert\NotBlank()

Cache

“metadata”

Walker����������� ������������������  /����������� ������������������  code

use Doctrine\ORM\Mapping as ORM;use Symfony\Component\Validator\ Constraints as Assert;

Declare����������� ������������������  which����������� ������������������  Annotations����������� ������������������  you����������� ������������������  will����������� ������������������  use

DMS\Filterhttps://github.com/rdohms/DMS

The����������� ������������������  Gears

The����������� ������������������  Gears

Doctrine\CommonsYourApp

The����������� ������������������  Gears

Reader

Doctrine\CommonsYourApp

The����������� ������������������  Gears

Parsing

Reader

Doctrine\CommonsYourApp

The����������� ������������������  Gears

Parsing

ReaderFilter

Doctrine\CommonsYourApp

The����������� ������������������  Gears

Parsing“Enforcing”

ReaderFilter

Doctrine\CommonsYourApp

The����������� ������������������  Gears

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

Doctrine\CommonsYourApp

The����������� ������������������  Gears

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

Doctrine\CommonsYourApp

MyAnnotation

The����������� ������������������  Gears

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

filter()

Doctrine\CommonsYourApp

MyAnnotation

The����������� ������������������  Gears

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

getMethodAnnotation()filter()

Doctrine\CommonsYourApp

MyAnnotation

The����������� ������������������  Gears

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

getMethodAnnotation()filter()

Doctrine\CommonsYourApp

MyAnnotation

The����������� ������������������  Gears

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

getMethodAnnotation()filter()

filtered value

Doctrine\CommonsYourApp

MyAnnotation

namespace Filter\Rule; /** * @Annotation */ class NoDogs { public function __construct($options) { /* no options */ } public function filter($value) { return str_replace("dogs", "", (string) $value); } }

Usage:����������� ������������������  @Filter\NoDogs()

namespace Filter\Rule; /** * @Annotation */ class NoDogs { public function __construct($options) { /* no options */ } public function filter($value) { return str_replace("dogs", "", (string) $value); } }

tell����������� ������������������  doctrine����������� ������������������  this����������� ������������������  is����������� ������������������  an����������� ������������������  annotation

Usage:����������� ������������������  @Filter\NoDogs()

namespace Filter\Rule; /** * @Annotation */ class NoDogs { public function __construct($options) { /* no options */ } public function filter($value) { return str_replace("dogs", "", (string) $value); } }

tell����������� ������������������  doctrine����������� ������������������  this����������� ������������������  is����������� ������������������  an����������� ������������������  annotation

Usage:����������� ������������������  @Filter\NoDogs()

array����������� ������������������  of����������� ������������������  the����������� ������������������  parameters

namespace Filter\Rule; /** * @Annotation */ class NoDogs { public function __construct($options) { /* no options */ } public function filter($value) { return str_replace("dogs", "", (string) $value); } }

tell����������� ������������������  doctrine����������� ������������������  this����������� ������������������  is����������� ������������������  an����������� ������������������  annotation

Usage:����������� ������������������  @Filter\NoDogs()

array����������� ������������������  of����������� ������������������  the����������� ������������������  parameters

this����������� ������������������  is����������� ������������������  the����������� ������������������  effect����������� ������������������  of����������� ������������������  our����������� ������������������  annotation

namespace Filter; class Filter { private $reader; public function __construct(Doctrine\Common\Annotations\Reader $reader) { $this->reader = $reader; } public function filter($object) { $reflectionObject = new \ReflectionObject($object); foreach ($reflectionObject->getProperties() as $reflectionProperty) { $this->filterProperty($object, $reflectionProperty); } } public function filterProperty($object, $reflectionProperty) { // fetch the @NoDog annotation from the annotation reader $annotation = $this->reader->getMethodAnnotation(

$reflectionProperty, 'Filter\Rule\NoDog'); if (null !== $annotation) { return; } $reflectionProperty->setAccessible(true); $value = $reflectionProperty->getValue($object); $filteredValue = $annotation->filter($value); $reflectionProperty->setValue( $filteredValue ); } }

namespace Filter; class Filter { private $reader; public function __construct(Doctrine\Common\Annotations\Reader $reader) { $this->reader = $reader; } public function filter($object) { $reflectionObject = new \ReflectionObject($object); foreach ($reflectionObject->getProperties() as $reflectionProperty) { $this->filterProperty($object, $reflectionProperty); } } public function filterProperty($object, $reflectionProperty) { // fetch the @NoDog annotation from the annotation reader $annotation = $this->reader->getMethodAnnotation(

$reflectionProperty, 'Filter\Rule\NoDog'); if (null !== $annotation) { return; } $reflectionProperty->setAccessible(true); $value = $reflectionProperty->getValue($object); $filteredValue = $annotation->filter($value); $reflectionProperty->setValue( $filteredValue ); } }

Get����������� ������������������  only����������� ������������������  our����������� ������������������  annotation

namespace Filter; class Filter { private $reader; public function __construct(Doctrine\Common\Annotations\Reader $reader) { $this->reader = $reader; } public function filter($object) { $reflectionObject = new \ReflectionObject($object); foreach ($reflectionObject->getProperties() as $reflectionProperty) { $this->filterProperty($object, $reflectionProperty); } } public function filterProperty($object, $reflectionProperty) { // fetch the @NoDog annotation from the annotation reader $annotation = $this->reader->getMethodAnnotation(

$reflectionProperty, 'Filter\Rule\NoDog'); if (null !== $annotation) { return; } $reflectionProperty->setAccessible(true); $value = $reflectionProperty->getValue($object); $filteredValue = $annotation->filter($value); $reflectionProperty->setValue( $filteredValue ); } }

Get����������� ������������������  only����������� ������������������  our����������� ������������������  annotation

Make����������� ������������������  Annotation����������� ������������������  affect����������� ������������������  application

Taking����������� ������������������  it����������� ������������������  up����������� ������������������  a����������� ������������������  notch

Taking����������� ������������������  it����������� ������������������  up����������� ������������������  a����������� ������������������  notch

Reader

Filter

Taking����������� ������������������  it����������� ������������������  up����������� ������������������  a����������� ������������������  notch

Reader

Loader

Walker

Filter

Taking����������� ������������������  it����������� ������������������  up����������� ������������������  a����������� ������������������  notch

Parsing

Loading

Using/Walking

Reader

Loader

Walker

Filter

Taking����������� ������������������  it����������� ������������������  up����������� ������������������  a����������� ������������������  notch

Parsing

Loading

Using/Walking

Reader

Loader

Walker

Filters����������� ������������������  Annotations

Filter

Taking����������� ������������������  it����������� ������������������  up����������� ������������������  a����������� ������������������  notch

Parsing

Loading

Using/Walking

Reader

Loader

Walker

Filters����������� ������������������  Annotationsnavigates����������� ������������������  object,����������� ������������������  

reflects����������� ������������������  and����������� ������������������  filters

Filter

Taking����������� ������������������  it����������� ������������������  up����������� ������������������  a����������� ������������������  notch

Parsing

Loading

Using/Walking

Reader

Loader

Walker

Filters����������� ������������������  Annotationsnavigates����������� ������������������  object,����������� ������������������  

reflects����������� ������������������  and����������� ������������������  filters

Filter

Taking����������� ������������������  it����������� ������������������  up����������� ������������������  a����������� ������������������  notch

Parsing

Loading

Using/Walking

Reader

Loader

Walker

annotations

Filters����������� ������������������  Annotationsnavigates����������� ������������������  object,����������� ������������������  

reflects����������� ������������������  and����������� ������������������  filters

Filter

Taking����������� ������������������  it����������� ������������������  up����������� ������������������  a����������� ������������������  notch

Parsing

Loading

Using/Walking

Reader

Loader

Walker

annotations metadata

Filters����������� ������������������  Annotationsnavigates����������� ������������������  object,����������� ������������������  

reflects����������� ������������������  and����������� ������������������  filters

Filter

/** * @My\Annotation(“name”, nullable=true) */

/** * @My\Annotation(“name”, nullable=true) */

defaultProperty

/** * @My\Annotation(“name”, nullable=true) */

new My\Annotation($options);

defaultProperty

/** * @My\Annotation(“name”, nullable=true) */

new My\Annotation($options);

array:����������� ������������������  key����������� ������������������  =>����������� ������������������  valueor

string:����������� ������������������  value

defaultProperty

/** * @My\Annotation(“name”, nullable=true) */

new My\Annotation($options);

array:����������� ������������������  key����������� ������������������  =>����������� ������������������  valueor

string:����������� ������������������  value

defaultProperty

or

/** * @My\Annotation(“name”, nullable=true) */

new My\Annotation($options);

$a = new My\Annotation();$a->nullable = true;$a->{$a->getDefaultProperty()} = “name”;

array:����������� ������������������  key����������� ������������������  =>����������� ������������������  valueor

string:����������� ������������������  value

defaultProperty

or

//Get Doctrine Reader $reader = new Annotations\AnnotationReader(); $reader->setEnableParsePhpImports(true); //Load AnnotationLoader $loader = new Mapping\Loader\AnnotationLoader($reader); $this->loader = $loader; //Get a MetadataFactory $metadataFactory = new Mapping\ClassMetadataFactory($loader); //Get a Filter $filter = new DMS\Filter\Filter($metadataFactory); //Get your Entity $user = new App\Entity\User(); $user->name = "My <b>name</b>"; $user->email = " email@mail.com"; //Filter you entity $filter->filter($user); echo $user->name; //"My name" echo $user->email; //"email@mail.com"

//Get Doctrine Reader $reader = new Annotations\AnnotationReader(); $reader->setEnableParsePhpImports(true); //Load AnnotationLoader $loader = new Mapping\Loader\AnnotationLoader($reader); $this->loader = $loader; //Get a MetadataFactory $metadataFactory = new Mapping\ClassMetadataFactory($loader); //Get a Filter $filter = new DMS\Filter\Filter($metadataFactory); //Get your Entity $user = new App\Entity\User(); $user->name = "My <b>name</b>"; $user->email = " email@mail.com"; //Filter you entity $filter->filter($user); echo $user->name; //"My name" echo $user->email; //"email@mail.com"

put����������� ������������������  this����������� ������������������  in����������� ������������������  your����������� ������������������  DIC

//Get Doctrine Reader $reader = new Annotations\AnnotationReader(); $reader->setEnableParsePhpImports(true); //Load AnnotationLoader $loader = new Mapping\Loader\AnnotationLoader($reader); $this->loader = $loader; //Get a MetadataFactory $metadataFactory = new Mapping\ClassMetadataFactory($loader); //Get a Filter $filter = new DMS\Filter\Filter($metadataFactory); //Get your Entity $user = new App\Entity\User(); $user->name = "My <b>name</b>"; $user->email = " email@mail.com"; //Filter you entity $filter->filter($user); echo $user->name; //"My name" echo $user->email; //"email@mail.com"

Calling����������� ������������������  the����������� ������������������  Enforcer

put����������� ������������������  this����������� ������������������  in����������� ������������������  your����������� ������������������  DIC

What? Why? Where?

How?

Questions?

http://doh.mshttp://slides.doh.ms

@rdohms

Rate����������� ������������������  this����������� ������������������  talk����������� ������������������  on����������� ������������������  Joind.in!

https://joind.in/6696

on����������� ������������������  twitter these����������� ������������������  slides����������� ������������������  will����������� ������������������  be����������� ������������������  here

please!

top related