Transcript
Page 1: Annotating with Annotations - PHPBenelux June/2012

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

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

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

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

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

Page 2: Annotating with Annotations - PHPBenelux June/2012

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

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

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

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

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

Page 3: Annotating with Annotations - PHPBenelux June/2012

phot

o cr

edit:

Eli W

hite

Evangelist, Speaker and Contributor.

Developer at WEBclusive.

Enabler at AmsterdamPHP.

Rafael Dohms@rdohms

Page 4: Annotating with Annotations - PHPBenelux June/2012

What? Why? Where?

How?

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

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

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

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

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

Page 5: Annotating with Annotations - PHPBenelux June/2012

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

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

s?

http://ecdesignrebels.blogspot.com

Page 6: Annotating with Annotations - PHPBenelux June/2012

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

reading any form of text."

Page 7: Annotating with Annotations - PHPBenelux June/2012

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

reading any form of text."

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

Page 8: Annotating with Annotations - PHPBenelux June/2012

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

“Annotations do not directly affect program semantics”

-- In Code Speak --

Page 9: Annotating with Annotations - PHPBenelux June/2012

“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

Page 10: Annotating with Annotations - PHPBenelux June/2012
Page 11: Annotating with Annotations - PHPBenelux June/2012

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

Page 12: Annotating with Annotations - PHPBenelux June/2012

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

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

Page 13: Annotating with Annotations - PHPBenelux June/2012

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;

Page 14: Annotating with Annotations - PHPBenelux June/2012

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

Page 15: Annotating with Annotations - PHPBenelux June/2012
Page 16: Annotating with Annotations - PHPBenelux June/2012

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

Page 17: Annotating with Annotations - PHPBenelux June/2012

Questions?

Page 18: Annotating with Annotations - PHPBenelux June/2012

Questions?

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

Page 19: Annotating with Annotations - PHPBenelux June/2012
Page 20: Annotating with Annotations - PHPBenelux June/2012
Page 21: Annotating with Annotations - PHPBenelux June/2012

phpDoc

~2000

Page 22: Annotating with Annotations - PHPBenelux June/2012

phpDoc

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

~2000 2005

Page 23: Annotating with Annotations - PHPBenelux June/2012

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

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

phpDoc

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

~2000 2005

Page 24: Annotating with Annotations - PHPBenelux June/2012

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

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

phpDoc

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

~2000 2005 2008

Page 25: Annotating with Annotations - PHPBenelux June/2012

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

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

phpDoc

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

~2000 2005 2008

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

Page 26: Annotating with Annotations - PHPBenelux June/2012

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

Page 27: Annotating with Annotations - PHPBenelux June/2012

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

Page 28: Annotating with Annotations - PHPBenelux June/2012

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

Page 29: Annotating with Annotations - PHPBenelux June/2012

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

?

Page 30: Annotating with Annotations - PHPBenelux June/2012

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;

Page 31: Annotating with Annotations - PHPBenelux June/2012

Note:

Page 32: Annotating with Annotations - PHPBenelux June/2012

Note:

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

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

Page 33: Annotating with Annotations - PHPBenelux June/2012

Note:

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

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

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

T_COMMENT

T_DOC_COMMENT

Page 34: Annotating with Annotations - PHPBenelux June/2012

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

Page 35: Annotating with Annotations - PHPBenelux June/2012

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

marker

Page 36: Annotating with Annotations - PHPBenelux June/2012

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

markerparameterized

Page 37: Annotating with Annotations - PHPBenelux June/2012

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

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

otations?

http://ecdesignrebels.blogspot.com

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

pros/cons?

Page 38: Annotating with Annotations - PHPBenelux June/2012

-

Page 39: Annotating with Annotations - PHPBenelux June/2012

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

-

Page 40: Annotating with Annotations - PHPBenelux June/2012

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

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

Page 41: Annotating with Annotations - PHPBenelux June/2012

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

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

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

Page 42: Annotating with Annotations - PHPBenelux June/2012

+

Page 43: Annotating with Annotations - PHPBenelux June/2012

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

+

Page 44: Annotating with Annotations - PHPBenelux June/2012

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

+

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

Page 45: Annotating with Annotations - PHPBenelux June/2012

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

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

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

Page 46: Annotating with Annotations - PHPBenelux June/2012

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

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

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

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

Page 47: Annotating with Annotations - PHPBenelux June/2012

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

Page 48: Annotating with Annotations - PHPBenelux June/2012

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

Page 49: Annotating with Annotations - PHPBenelux June/2012
Page 50: Annotating with Annotations - PHPBenelux June/2012
Page 51: Annotating with Annotations - PHPBenelux June/2012

<?php

class User{

protected $name

...

}

Page 52: Annotating with Annotations - PHPBenelux June/2012

<?php

class User{

protected $name

...

}

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

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

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

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

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

Page 53: Annotating with Annotations - PHPBenelux June/2012

<?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

Page 54: Annotating with Annotations - PHPBenelux June/2012

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

class User{

protected $name

...

}

<?phpValidation

persistence

filter

Page 55: Annotating with Annotations - PHPBenelux June/2012

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

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

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

http://ecdesignrebels.blogspot.com

Page 56: Annotating with Annotations - PHPBenelux June/2012
Page 57: Annotating with Annotations - PHPBenelux June/2012

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

Page 58: Annotating with Annotations - PHPBenelux June/2012

/** * @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

Page 59: Annotating with Annotations - PHPBenelux June/2012

/** * @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

Page 60: Annotating with Annotations - PHPBenelux June/2012

/** * @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

Page 61: Annotating with Annotations - PHPBenelux June/2012

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

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

annotations?

http://ecdesignrebels.blogspot.com

Page 62: Annotating with Annotations - PHPBenelux June/2012

Annotations

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

Page 63: Annotating with Annotations - PHPBenelux June/2012

Annotations

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

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

Page 64: Annotating with Annotations - PHPBenelux June/2012
Page 65: Annotating with Annotations - PHPBenelux June/2012

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

Code

Page 66: Annotating with Annotations - PHPBenelux June/2012

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

Code

ReflectionClass->getDocComment()

Page 67: Annotating with Annotations - PHPBenelux June/2012

DOCBlock

/** * @tag parameters */

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

Code

ReflectionClass->getDocComment()

Page 68: Annotating with Annotations - PHPBenelux June/2012

DOCBlock

/** * @tag parameters */

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

Code

ReflectionClass->getDocComment()

Page 69: Annotating with Annotations - PHPBenelux June/2012

DOCBlock

/** * @tag parameters */

Tag() + parameters

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

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

Code

ReflectionClass->getDocComment()

Page 70: Annotating with Annotations - PHPBenelux June/2012

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

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

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

php-annotationsaddendumStubble

abandoned?

Notoj

Annotation Engines

Page 71: Annotating with Annotations - PHPBenelux June/2012

Annotation Engines

Notoj

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

Parameterized marker parameterized

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

Page 72: Annotating with Annotations - PHPBenelux June/2012

Annotation Engines

Notoj

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

Parameterized marker parameterized

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

Page 73: Annotating with Annotations - PHPBenelux June/2012

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

<?php

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

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

Page 74: Annotating with Annotations - PHPBenelux June/2012

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

Page 75: Annotating with Annotations - PHPBenelux June/2012

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

Page 76: Annotating with Annotations - PHPBenelux June/2012

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

Page 77: Annotating with Annotations - PHPBenelux June/2012

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

Page 78: Annotating with Annotations - PHPBenelux June/2012

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

Page 79: Annotating with Annotations - PHPBenelux June/2012

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

Page 80: Annotating with Annotations - PHPBenelux June/2012

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

Doctrine\CommonsYourApp

Page 81: Annotating with Annotations - PHPBenelux June/2012

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

Reader

Doctrine\CommonsYourApp

Page 82: Annotating with Annotations - PHPBenelux June/2012

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

Parsing

Reader

Doctrine\CommonsYourApp

Page 83: Annotating with Annotations - PHPBenelux June/2012

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

Parsing

ReaderFilter

Doctrine\CommonsYourApp

Page 84: Annotating with Annotations - PHPBenelux June/2012

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

Parsing“Enforcing”

ReaderFilter

Doctrine\CommonsYourApp

Page 85: Annotating with Annotations - PHPBenelux June/2012

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

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

Doctrine\CommonsYourApp

Page 86: Annotating with Annotations - PHPBenelux June/2012

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

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

Doctrine\CommonsYourApp

MyAnnotation

Page 87: Annotating with Annotations - PHPBenelux June/2012

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

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

filter()

Doctrine\CommonsYourApp

MyAnnotation

Page 88: Annotating with Annotations - PHPBenelux June/2012

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

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

getMethodAnnotation()filter()

Doctrine\CommonsYourApp

MyAnnotation

Page 89: Annotating with Annotations - PHPBenelux June/2012

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

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

getMethodAnnotation()filter()

Doctrine\CommonsYourApp

MyAnnotation

Page 90: Annotating with Annotations - PHPBenelux June/2012

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

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

getMethodAnnotation()filter()

filtered value

Doctrine\CommonsYourApp

MyAnnotation

Page 91: Annotating with Annotations - PHPBenelux June/2012

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()

Page 92: Annotating with Annotations - PHPBenelux June/2012

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()

Page 93: Annotating with Annotations - PHPBenelux June/2012

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

Page 94: Annotating with Annotations - PHPBenelux June/2012

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

Page 95: Annotating with Annotations - PHPBenelux June/2012

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 ); } }

Page 96: Annotating with Annotations - PHPBenelux June/2012

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

Page 97: Annotating with Annotations - PHPBenelux June/2012

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

Page 98: Annotating with Annotations - PHPBenelux June/2012

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

Page 99: Annotating with Annotations - PHPBenelux June/2012

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

Reader

Filter

Page 100: Annotating with Annotations - PHPBenelux June/2012

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

Reader

Loader

Walker

Filter

Page 101: Annotating with Annotations - PHPBenelux June/2012

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

Parsing

Loading

Using/Walking

Reader

Loader

Walker

Filter

Page 102: Annotating with Annotations - PHPBenelux June/2012

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

Parsing

Loading

Using/Walking

Reader

Loader

Walker

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

Filter

Page 103: Annotating with Annotations - PHPBenelux June/2012

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

Parsing

Loading

Using/Walking

Reader

Loader

Walker

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

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

Filter

Page 104: Annotating with Annotations - PHPBenelux June/2012

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

Parsing

Loading

Using/Walking

Reader

Loader

Walker

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

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

Filter

Page 105: Annotating with Annotations - PHPBenelux June/2012

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

Parsing

Loading

Using/Walking

Reader

Loader

Walker

annotations

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

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

Filter

Page 106: Annotating with Annotations - PHPBenelux June/2012

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

Parsing

Loading

Using/Walking

Reader

Loader

Walker

annotations metadata

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

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

Filter

Page 107: Annotating with Annotations - PHPBenelux June/2012

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

Page 108: Annotating with Annotations - PHPBenelux June/2012

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

defaultProperty

Page 109: Annotating with Annotations - PHPBenelux June/2012

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

new My\Annotation($options);

defaultProperty

Page 110: Annotating with Annotations - PHPBenelux June/2012

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

new My\Annotation($options);

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

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

defaultProperty

Page 111: Annotating with Annotations - PHPBenelux June/2012

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

new My\Annotation($options);

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

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

defaultProperty

or

Page 112: Annotating with Annotations - PHPBenelux June/2012

/** * @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

Page 113: Annotating with Annotations - PHPBenelux June/2012

//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 protected]"; //Filter you entity $filter->filter($user); echo $user->name; //"My name" echo $user->email; //"[email protected]"

Page 114: Annotating with Annotations - PHPBenelux June/2012

//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 protected]"; //Filter you entity $filter->filter($user); echo $user->name; //"My name" echo $user->email; //"[email protected]"

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

Page 115: Annotating with Annotations - PHPBenelux June/2012

//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 protected]"; //Filter you entity $filter->filter($user); echo $user->name; //"My name" echo $user->email; //"[email protected]"

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

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

Page 116: Annotating with Annotations - PHPBenelux June/2012

What? Why? Where?

How?

Page 117: Annotating with Annotations - PHPBenelux June/2012

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!


Recommended