annotating with annotations - dpc uncon

114
A look into Annotations in PHP Rafael Dohms * Dutch PHP Conference 2012 - UnCon Annotating with Annotations @rdohms on twitter

Upload: rafael-dohms

Post on 08-May-2015

2.104 views

Category:

Technology


0 download

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

Page 1: Annotating with Annotations - DPC UnCon

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

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

*����������� ������������������  Dutch����������� ������������������  PHP����������� ������������������  Conference����������� ������������������  2012����������� ������������������  -����������� ������������������  UnCon

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

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

Page 2: Annotating with Annotations - DPC UnCon

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

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

*����������� ������������������  Forum����������� ������������������  PHP����������� ������������������  Paris����������� ������������������  2012

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

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

Page 3: Annotating with Annotations - DPC UnCon

phot

o cr

edit:

Eli W

hite

Evangelist, Speaker and Contributor.

Podcaster, User Group Leader.

Developer at WEBclusive.

Rafael Dohms@rdohms

Page 4: Annotating with Annotations - DPC UnCon

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 - DPC UnCon

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

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

s?

http://ecdesignrebels.blogspot.com

Page 6: Annotating with Annotations - DPC UnCon

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

reading any form of text."

Page 7: Annotating with Annotations - DPC UnCon

-- 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 - DPC UnCon

“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 - DPC UnCon

“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 - DPC UnCon
Page 11: Annotating with Annotations - DPC UnCon

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

Page 12: Annotating with Annotations - DPC UnCon

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

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

Page 13: Annotating with Annotations - DPC UnCon

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 - DPC UnCon

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 - DPC UnCon
Page 16: Annotating with Annotations - DPC UnCon

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

Page 17: Annotating with Annotations - DPC UnCon

Questions?

Page 18: Annotating with Annotations - DPC UnCon

Questions?

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

Page 19: Annotating with Annotations - DPC UnCon
Page 20: Annotating with Annotations - DPC UnCon
Page 21: Annotating with Annotations - DPC UnCon

phpDoc

~2000

Page 22: Annotating with Annotations - DPC UnCon

phpDoc

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

~2000 2005

Page 23: Annotating with Annotations - DPC UnCon

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

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

phpDoc

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

~2000 2005

Page 24: Annotating with Annotations - DPC UnCon

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

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

phpDoc

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

~2000 2005 2008

Page 25: Annotating with Annotations - DPC UnCon

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

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

phpDoc

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

~2000 2005 2008

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

Page 26: Annotating with Annotations - DPC UnCon

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 - DPC UnCon

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 - DPC UnCon

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 - DPC UnCon

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 - DPC UnCon

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 - DPC UnCon

Note:

Page 32: Annotating with Annotations - DPC UnCon

Note:

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

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

Page 33: Annotating with Annotations - DPC UnCon

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 - DPC UnCon

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

Page 35: Annotating with Annotations - DPC UnCon

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

marker

Page 36: Annotating with Annotations - DPC UnCon

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

markerparameterized

Page 37: Annotating with Annotations - DPC UnCon

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

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

otations?

http://ecdesignrebels.blogspot.com

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

pros/cons?

Page 38: Annotating with Annotations - DPC UnCon

-

Page 39: Annotating with Annotations - DPC UnCon

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

-

Page 40: Annotating with Annotations - DPC UnCon

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

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

Page 41: Annotating with Annotations - DPC UnCon

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

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

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

Page 42: Annotating with Annotations - DPC UnCon

+

Page 43: Annotating with Annotations - DPC UnCon

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

+

Page 44: Annotating with Annotations - DPC UnCon

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

+

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

Page 45: Annotating with Annotations - DPC UnCon

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

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

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

Page 46: Annotating with Annotations - DPC UnCon

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 - DPC UnCon

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 - DPC UnCon

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 - DPC UnCon

<?php

class User{

protected $name

...

}

Page 50: Annotating with Annotations - DPC UnCon

<?php

class User{

protected $name

...

}

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

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

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

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

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

Page 51: Annotating with Annotations - DPC UnCon

<?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 52: Annotating with Annotations - DPC UnCon

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

class User{

protected $name

...

}

<?phpValidation

persistence

filter

Page 53: Annotating with Annotations - DPC UnCon

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

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

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

http://ecdesignrebels.blogspot.com

Page 54: Annotating with Annotations - DPC UnCon
Page 55: Annotating with Annotations - DPC UnCon

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 56: Annotating with Annotations - DPC UnCon

/** * @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 57: Annotating with Annotations - DPC UnCon

/** * @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 58: Annotating with Annotations - DPC UnCon

/** * @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 59: Annotating with Annotations - DPC UnCon

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

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

annotations?

http://ecdesignrebels.blogspot.com

Page 60: Annotating with Annotations - DPC UnCon

Annotations

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

Page 61: Annotating with Annotations - DPC UnCon

Annotations

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

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

Page 62: Annotating with Annotations - DPC UnCon
Page 63: Annotating with Annotations - DPC UnCon

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

Code

Page 64: Annotating with Annotations - DPC UnCon

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

Code

ReflectionClass->getDocComment()

Page 65: Annotating with Annotations - DPC UnCon

DOCBlock

/** * @tag parameters */

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

Code

ReflectionClass->getDocComment()

Page 66: Annotating with Annotations - DPC UnCon

DOCBlock

/** * @tag parameters */

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

Code

ReflectionClass->getDocComment()

Page 67: Annotating with Annotations - DPC UnCon

DOCBlock

/** * @tag parameters */

Tag() + parameters

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

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

Code

ReflectionClass->getDocComment()

Page 68: Annotating with Annotations - DPC UnCon

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

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

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

php-annotationsaddendumStubble

abandoned?

Notoj

Annotation Engines

Page 69: Annotating with Annotations - DPC UnCon

Annotation Engines

Notoj

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

Parameterized marker parameterized

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

Page 70: Annotating with Annotations - DPC UnCon

Annotation Engines

Notoj

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

Parameterized marker parameterized

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

Page 71: Annotating with Annotations - DPC UnCon

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

<?php

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

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

Page 72: Annotating with Annotations - DPC UnCon

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 73: Annotating with Annotations - DPC UnCon

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 74: Annotating with Annotations - DPC UnCon

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 75: Annotating with Annotations - DPC UnCon

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 76: Annotating with Annotations - DPC UnCon

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

Page 77: Annotating with Annotations - DPC UnCon

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

Page 78: Annotating with Annotations - DPC UnCon

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

Doctrine\CommonsYourApp

Page 79: Annotating with Annotations - DPC UnCon

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

Reader

Doctrine\CommonsYourApp

Page 80: Annotating with Annotations - DPC UnCon

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

Parsing

Reader

Doctrine\CommonsYourApp

Page 81: Annotating with Annotations - DPC UnCon

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

Parsing

ReaderFilter

Doctrine\CommonsYourApp

Page 82: Annotating with Annotations - DPC UnCon

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

Parsing“Enforcing”

ReaderFilter

Doctrine\CommonsYourApp

Page 83: Annotating with Annotations - DPC UnCon

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

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

Doctrine\CommonsYourApp

Page 84: Annotating with Annotations - DPC UnCon

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

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

Doctrine\CommonsYourApp

MyAnnotation

Page 85: Annotating with Annotations - DPC UnCon

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

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

filter()

Doctrine\CommonsYourApp

MyAnnotation

Page 86: Annotating with Annotations - DPC UnCon

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

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

getMethodAnnotation()filter()

Doctrine\CommonsYourApp

MyAnnotation

Page 87: Annotating with Annotations - DPC UnCon

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

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

getMethodAnnotation()filter()

Doctrine\CommonsYourApp

MyAnnotation

Page 88: Annotating with Annotations - DPC UnCon

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

Parsing“Enforcing”

Reader

AnnotatedObject

Filter

getMethodAnnotation()filter()

filtered value

Doctrine\CommonsYourApp

MyAnnotation

Page 89: Annotating with Annotations - DPC UnCon

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 90: Annotating with Annotations - DPC UnCon

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 91: Annotating with Annotations - DPC UnCon

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 92: Annotating with Annotations - DPC UnCon

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 93: Annotating with Annotations - DPC UnCon

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 94: Annotating with Annotations - DPC UnCon

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 95: Annotating with Annotations - DPC UnCon

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 96: Annotating with Annotations - DPC UnCon

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

Page 97: Annotating with Annotations - DPC UnCon

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

Reader

Filter

Page 98: Annotating with Annotations - DPC UnCon

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

Reader

Loader

Walker

Filter

Page 99: Annotating with Annotations - DPC UnCon

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

Parsing

Loading

Using/Walking

Reader

Loader

Walker

Filter

Page 100: Annotating with Annotations - DPC UnCon

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

Parsing

Loading

Using/Walking

Reader

Loader

Walker

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

Filter

Page 101: Annotating with Annotations - DPC UnCon

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

Parsing

Loading

Using/Walking

Reader

Loader

Walker

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

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

Filter

Page 102: Annotating with Annotations - DPC UnCon

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

Parsing

Loading

Using/Walking

Reader

Loader

Walker

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

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

Filter

Page 103: Annotating with Annotations - DPC UnCon

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

Parsing

Loading

Using/Walking

Reader

Loader

Walker

annotations

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

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

Filter

Page 104: Annotating with Annotations - DPC UnCon

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

Parsing

Loading

Using/Walking

Reader

Loader

Walker

annotations metadata

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

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

Filter

Page 105: Annotating with Annotations - DPC UnCon

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

Page 106: Annotating with Annotations - DPC UnCon

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

defaultProperty

Page 107: Annotating with Annotations - DPC UnCon

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

new My\Annotation($options);

defaultProperty

Page 108: Annotating with Annotations - DPC UnCon

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

new My\Annotation($options);

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

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

defaultProperty

Page 109: Annotating with Annotations - DPC UnCon

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

new My\Annotation($options);

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

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

defaultProperty

or

Page 110: Annotating with Annotations - DPC UnCon

/** * @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 111: Annotating with Annotations - DPC UnCon

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

/** * Filter\Alpha(true) */protected $name;

Alpha->filter($name);

Rafael����������� ������������������  Dohms23

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

Page 112: Annotating with Annotations - DPC UnCon

What? Why? Where?

How?

Page 113: Annotating with Annotations - DPC UnCon

Questions?

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

@rdohms

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

https://joind.in/6681

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

please!

Page 114: Annotating with Annotations - DPC UnCon

Questions?