Annotations in PHP - ConFoo 2013
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Annotations in PHP - ConFoo 2013

  • 6,401 views
Uploaded on

Annotations are more than phpdoc comments, they're a fully-featured way of including additional information alongside your code. We might have rejected an RFC to add support into the PHP core, but......

Annotations are more than phpdoc comments, they're a fully-featured way of including additional information alongside your code. We might have rejected an RFC to add support into the PHP core, but the community has embraced this tool anyway! This session shows you who is doing what with annotations, and will give you some ideas on how to use the existing tools in your own projects to keep life simple. Developers, architects and anyone responsible for the technical direction of an application should attend this session.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
6,401
On Slideshare
6,355
From Embeds
46
Number of Embeds
4

Actions

Shares
Downloads
37
Comments
2
Likes
12

Embeds 46

http://blog.jetbrains.com 30
http://eventifier.co 10
http://librosweb.es 5
https://twitter.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. *
  • 2.   ConFoo
  • 3.   -
  • 4.   Montreal/2013Annotations
  • 5.   in
  • 6.   PHP y
  • 7.   E xist! T he on
  • 8.   twitter Rafael
  • 9.   Dohms @rdohms
  • 10. Rafael Dohms photo credit: Eli White @rdohms Evangelist, Speaker and Contributor.Developer at WEBclusive.Enabler at AmsterdamPHP. we
  • 11.   make
  • 12.   awesome
  • 13.   crowd-funding
  • 14.   software!
  • 15. a
  • 16.   little
  • 17.   history existing
  • 18.   uses What? Why? Where? How? ustom
  • 19.   ann otations Impl ementing
  • 20.   c DMSFilter based
  • 21.   on
  • 22.    show
  • 23.   me
  • 24.   the
  • 25.   code!
  • 26. http://ecdesignrebels.blogspot.com w h a t? ta tio ns? re 
  • 27.   an noW ha t
  • 28.   a
  • 29. -- In English --"An annotation is a note that is made while reading any form of text."
  • 30. something
  • 31.   that
  • 32.   describes
  • 33.   an
  • 34.   aspect
  • 35.   of
  • 36.   the
  • 37.   subject -- In English --"An annotation is a note that is made while reading any form of text."
  • 38. -- In Code Speak --“An annotation is metadata attached to your code, that can be read at runtime.”“Annotations do not directly affect program semantics”
  • 39. -- In Code Speak --“An annotation is metadata attached to your code, that can be read at runtime.” effects
  • 40.   are
  • 41.   only
  • 42.   observed
  • 43.   at“Annotations do not directly affect program semantics”
  • 44. -- In Code Speak --“An annotation is metadata attached to your code, that can be read at runtime.” effects
  • 45.   are
  • 46.   only
  • 47.   observed
  • 48.   at“Annotations do not directly affect program semantics” just
  • 49.   like
  • 50.   your
  • 51.   notes
  • 52. Annotations in the wild C#
  • 53. annotations Annotations in the wild C# attributes
  • 54. @Entity @Table(name = "people") class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id;annotations Annotations in the wild C# attributes
  • 55. @Entity @Table(name = "people") class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id;annotations Annotations in the wild public class Customer C# { [Required] [StringLength(50)] public string Prename { get; set; } [Column(TypeName = "image")] public byte[] Image { get; set; } attributes
  • 56. @Entity @Table(name = "people") class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id;annotations after
  • 57.   v1.5 Annotations in the wild public class Customer C# { [Required] [StringLength(50)] public string Prename { get; set; } [Column(TypeName = "image")] public byte[] Image { get; set; } attributes
  • 58. @Entity @Table(name = "people") class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id;annotations after
  • 59.   v1.5 Annotations in the wild public class Customer C# { [Required] [StringLength(50)] public string Prename { get; set; } [Column(TypeName = "image")] public byte[] Image { get; set; } attributes since
  • 60.   first
  • 61.   release
  • 62. No
  • 63.   core
  • 64.   annotation
  • 65.   support
  • 66. Questions?
  • 67. Questions?I’m
  • 68.   kidding!
  • 69. phpDoc~2000
  • 70. PHP
  • 71.   5.1 Reflection
  • 72.   supports
  • 73.    getDocComments()phpDoc~2000 2005
  • 74. PHP
  • 75.   5.1 Reflection
  • 76.   supports
  • 77.    getDocComments()phpDoc~2000 2005 First
  • 78.    Annotation
  • 79.   Engines
  • 80.   in
  • 81.   PHP
  • 82. PHP
  • 83.   5.1 Reflection
  • 84.   supports
  • 85.    getDocComments()phpDoc~2000 2005 2008 First
  • 86.    Annotation
  • 87.   Engines
  • 88.   in
  • 89.   PHP
  • 90. PHP
  • 91.   5.1 Reflection
  • 92.   supports
  • 93.    getDocComments()phpDoc~2000 2005 2008 First
  • 94.    Doctrine
  • 95.   2
  • 96.    Annotation Annotation
  • 97.   Engine
  • 98.   Engines
  • 99.   in
  • 100.   PHP
  • 101. PHP
  • 102.   5.1 Reflection
  • 103.   supports
  • 104.    getDocComments() RFC:
  • 105.   Annotations
  • 106.    in
  • 107.   core
  • 108.   w/
  • 109.   custom
  • 110.   phpDoc syntax~2000 2005 2008 2010 First
  • 111.    Doctrine
  • 112.   2
  • 113.    Annotation Annotation
  • 114.   Engine
  • 115.   Engines
  • 116.   in
  • 117.   PHP
  • 118. PHP
  • 119.   5.1 Reflection
  • 120.   supports
  • 121.    getDocComments() RFC:
  • 122.   Annotations
  • 123.    ED CTustom
  • 124.    in
  • 125.   core
  • 126.   JE/
  • 127.   c wphpDoc E Rsyntax~2000 2005 2008 2010 First
  • 128.    Doctrine
  • 129.   2
  • 130.    Annotation Annotation
  • 131.   Engine
  • 132.   Engines
  • 133.   in
  • 134.   PHP
  • 135. PHP
  • 136.   5.1 RFC:
  • 137.   DocBlock
  • 138.    Reflection
  • 139.   supports
  • 140.    Annotations getDocComments() “in
  • 141.   discussion” RFC:
  • 142.   Annotations
  • 143.    ED CTustom
  • 144.    in
  • 145.   core
  • 146.   JE/
  • 147.   c wphpDoc E Rsyntax~2000 2005 2008 2010 2011 First
  • 148.    Doctrine
  • 149.   2
  • 150.    Annotation Annotation
  • 151.   Engine
  • 152.   Engines
  • 153.   in
  • 154.   PHP
  • 155. PHP
  • 156.   5.1 RFC:
  • 157.   DocBlock
  • 158.    Reflection
  • 159.   supports
  • 160.    Annotations getDocComments() “in
  • 161.   discussion” RFC:
  • 162.   Annotations
  • 163.    ED Tustom
  • 164.    discussion
  • 165.    wC in
  • 166.   core
  • 167.   JE/
  • 168.   c sparks
  • 169.   up
  • 170.   againphpDoc E Rsyntax~2000 2005 2008 2010 2011 2013 First
  • 171.    Doctrine
  • 172.   2
  • 173.    Annotation Annotation
  • 174.   Engine
  • 175.   Engines
  • 176.   in
  • 177.   PHP
  • 178. PHP
  • 179.   5.1 RFC:
  • 180.   DocBlock
  • 181.    Reflection
  • 182.   supports
  • 183.    Annotations getDocComments() “in
  • 184.   discussion” RFC:
  • 185.   Annotations
  • 186.    ED Tustom
  • 187.    discussion
  • 188.    wC in
  • 189.   core
  • 190.   JE/
  • 191.   c sparks
  • 192.   up
  • 193.   againphpDoc E Rsyntax~2000 2005 2008 2010 2011 2013 ? First
  • 194.    Doctrine
  • 195.   2
  • 196.    Annotation Annotation
  • 197.   Engine
  • 198.   Engines
  • 199.   in
  • 200.   PHP
  • 201. Current situation in php-internals
  • 202. Current situation in php-internals
  • 203. annotations
  • 204. /** * EntityReward * * @ORMTable("reward") * @ORMEntity(repositoryClass="RewardRepository") */ class Reward { /** * @var integer $id * * @ORMColumn(name="id", type="integer") * @ORMId * @ORMGeneratedValue(strategy="AUTO") */annotations protected $id; /** * @var string $title * * @ORMColumn(name="title", type="string", length=150, nullable=true) * * @AssertMaxLength(150) */ protected $title;
  • 205. re-use
  • 206.   of
  • 207.   docblocks /** * EntityReward * * @ORMTable("reward") * @ORMEntity(repositoryClass="RewardRepository") */ class Reward { /** * @var integer $id * * @ORMColumn(name="id", type="integer") * @ORMId * @ORMGeneratedValue(strategy="AUTO") */annotations protected $id; /** * @var string $title * * @ORMColumn(name="title", type="string", length=150, nullable=true) * * @AssertMaxLength(150) */ protected $title;
  • 208. On docblocks vs. comments
  • 209. On docblocks vs. comments // this is a comment /* This is a multiline comment */ T_COMMENT
  • 210. On docblocks vs. comments // this is a comment /* This is a multiline comment */ T_COMMENT /** * this is a docblock */ T_DOC_COMMENT
  • 211. On docblocks vs. comments ignored
  • 212.   by
  • 213.   opcode
  • 214.   cache // this is a comment /* This is a multiline comment */ T_COMMENT /** * this is a docblock */ T_DOC_COMMENT
  • 215. On docblocks vs. comments ignored
  • 216.   by
  • 217.   opcode
  • 218.   cache // this is a comment /* This is a multiline comment */ T_COMMENT cached
  • 219.   by
  • 220.   opcode
  • 221.   cache /** * this is a docblock */ T_DOC_COMMENT
  • 222. class Reward{ /** * @var integer $id * @deprecated * @ORMColumn(name="id", type="integer") * @ORMId */ protected $id;@Entity @Table(name = "people") class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id;public class Customer{ [Required] [StringLength(50)] public string Prename { get; set; } [Column(TypeName = "image")] public byte[] Image { get; set; }
  • 223. class Reward{ /** * @var integer $id marker * @deprecated * @ORMColumn(name="id", type="integer") * @ORMId */ protected $id;@Entity @Table(name = "people") class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id;public class Customer{ [Required] [StringLength(50)] public string Prename { get; set; } [Column(TypeName = "image")] public byte[] Image { get; set; }
  • 224. class Reward{ /** * @var integer $id marker * @deprecated * @ORMColumn(name="id", type="integer") * @ORMId */ protected $id;@Entity @Table(name = "people") class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; parameterizedpublic class Customer{ [Required] [StringLength(50)] public string Prename { get; set; } [Column(TypeName = "image")] public byte[] Image { get; set; }
  • 225. http://ecdesignrebels.blogspot.comWh y ? 
  • 226.   an no tatio ns? ld
  • 227.    I
  • 228.   use hy 
  • 229.   shouWAre
  • 230.   annotations
  • 231.   for
  • 232.   you?
  • 233. I don’t know.
  • 234. hate
  • 235.   annotations
  • 236. hate
  • 237.    love
  • 238.   annotations annotations
  • 239. hate
  • 240.    love
  • 241.   annotations annotations never
  • 242.   used
  • 243.   annotations
  • 244. The downside or why not but
  • 245.   its
  • 246.   code,
  • 247.   in
  • 248.   comments! docblocksharder
  • 249. The downside or why not but
  • 250.   its
  • 251.   code,
  • 252.   in
  • 253.   comments! docblocks harderdocblocks
  • 254.   are
  • 255.   first
  • 256.   class
  • 257.   citizens.
  • 258. The downside or why not harderIts
  • 259.   impossible
  • 260.   to
  • 261.   debug/test!
  • 262. The downside or why not harder Test/debug
  • 263.   Its
  • 264.   impossible
  • 265.    annotation
  • 266.   to
  • 267.   debug/test! “executor”
  • 268. The downside or why not It
  • 269.   does
  • 270.   not
  • 271.    perform!
  • 272. The downside or why not It
  • 273.   does
  • 274.   not
  • 275.   Caching
  • 276.   FTW! perform!
  • 277. Why I love Annotations
  • 278. Why I love AnnotationsEasier
  • 279.   to
  • 280.   inject
  • 281.   behavior
  • 282.   without
  • 283.   extending
  • 284. Why I love AnnotationsEasier
  • 285.   to
  • 286.   inject
  • 287.   behavior
  • 288.   without
  • 289.   extending Doctrine
  • 290.   1
  • 291.   vs
  • 292.   Doctrine
  • 293.   2
  • 294. Why I love Annotations Easier
  • 295.   to
  • 296.   inject
  • 297.   behavior
  • 298.   without
  • 299.   extending Doctrine
  • 300.   1
  • 301.   vs
  • 302.   Doctrine
  • 303.   2Contextualizes
  • 304.   behavior/config
  • 305.   in
  • 306.   the
  • 307.   object
  • 308. Why I love Annotations Easier
  • 309.   to
  • 310.   inject
  • 311.   behavior
  • 312.   without
  • 313.   extending Doctrine
  • 314.   1
  • 315.   vs
  • 316.   Doctrine
  • 317.   2Contextualizes
  • 318.   behavior/config
  • 319.   in
  • 320.   the
  • 321.   object In
  • 322.   object
  • 323.   vs.
  • 324.   external
  • 325.   configuration
  • 326.   file
  • 327. Why I love Annotations Easier
  • 328.   to
  • 329.   inject
  • 330.   behavior
  • 331.   without
  • 332.   extending Doctrine
  • 333.   1
  • 334.   vs
  • 335.   Doctrine
  • 336.   2Contextualizes
  • 337.   behavior/config
  • 338.   in
  • 339.   the
  • 340.   object In
  • 341.   object
  • 342.   vs.
  • 343.   external
  • 344.   configuration
  • 345.   file Its
  • 346.   documented/stored
  • 347.   by
  • 348.   phpDocumentor
  • 349. Why I love Annotations Easier
  • 350.   to
  • 351.   inject
  • 352.   behavior
  • 353.   without
  • 354.   extending Doctrine
  • 355.   1
  • 356.   vs
  • 357.   Doctrine
  • 358.   2Contextualizes
  • 359.   behavior/config
  • 360.   in
  • 361.   the
  • 362.   object In
  • 363.   object
  • 364.   vs.
  • 365.   external
  • 366.   configuration
  • 367.   file Its
  • 368.   documented/stored
  • 369.   by
  • 370.   phpDocumentor Its
  • 371.   in
  • 372.   docblocks,
  • 373.   so
  • 374.   its
  • 375.   parsed
  • 376. <?phpclass User{ protected $name ...}
  • 377. <?phpclass User -
  • 378.   persist
  • 379.   as
  • 380.   varchar e
  • 381.   { -
  • 382.   length
  • 383.   should
  • 384.   not
  • 385.   b more
  • 386.   then
  • 387.   255 -
  • 388.   should
  • 389.   not
  • 390.   be
  • 391.   blank -
  • 392.   only
  • 393.   letters protected $name ...}
  • 394. <?php persistence -
  • 395.   persist
  • 396.   as
  • 397.   varchar MyProjectUser:class User type: entity e
  • 398.    -
  • 399.   length
  • 400.   should
  • 401.   not
  • 402.   b table: users{ fields: more
  • 403.   then
  • 404.   255 name: -
  • 405.   should
  • 406.   not
  • 407.   be
  • 408.   blank type: string length: 255 -
  • 409.   only
  • 410.   letters protected $name Validation MyProjectUser: properties: name: - NotBlank: ~ ... - MaxLength: 255 filter MyProjectUser: filters:} name: - alpha
  • 411. <?php persistence MyProjectUser:class User type: entity table: users{ fields: name: /** type: string * @ORMColumn(‘string’, length=255) length: 255 * @AssertNotBlank() * @AssertMaxLength(255) * @FilterAlpha() */ protected $name Validation MyProjectUser: properties: name: - NotBlank: ~ ... - MaxLength: 255 filter MyProjectUser: filters:} name: - alpha
  • 412. <?phpclass User{ /** * @ORMColumn(‘string’, length=255) * @AssertNotBlank() * @AssertMaxLength(255) * @FilterAlpha() */ protected $name ...}
  • 413. ? http://ecdesignrebels.blogspot.comW h e r e tions 
  • 414.   used? 
  • 415.   ann ota er e
  • 416.    areWh
  • 417. ? http://ecdesignrebels.blogspot.comW h e r e tions 
  • 418.   used? 
  • 419.   ann ota er e
  • 420.    areWh
  • 421. class DataTest extends PHPUnit_Framework_TestCase{ /** * @dataProvider provider repetition * * @expectedException InvalidArgumentException * @expectedExceptionMessage Right Message */ public function testAdd($a, $b, $c) expectations { /* Test code */ } public function provider() { return array( array(0, 0, 0), ); }}
  • 422. /** * @ORMTable("myentity") * @ORMEntity(repositoryClass="MyEntityRepository") */class MyEntity{ /** * @var integer $id * * @ORMColumn(name="id", type="integer") * @ORMId * @ORMGeneratedValue(strategy="AUTO") persistance */ protected $id; /** * @var string $title * * @ORMColumn(name="title", type="string", length=255) association * @AssertMaxLength(255) * @AssertNotBlank() */ protected $title; /** * @var DoctrineCommonCollectionsArrayCollection $users * * @ORMOneToMany(targetEntity="OtherEntity", mappedBy="myEntity", cascade={"persist","remove"}) */ protected $otherEntities;}
  • 423. /** * @Route("/myaction/{id}", name="myaction") * @Method("POST") * * @Template("MyBundle:MyController:my.html.twig") * * @param int $id * @return array routing */public function myAction($id){ /* Controller Logic */ templating return array(data => $data);}class MyEntity{ /** * @var string $title * * @ORMColumn(name="title", type="string", length=255) * @AssertMaxLength(255) * @AssertNotBlank() */ protected $title;} Validation
  • 424. /** Dependency
  • 425.   Injection * @FLOW3Aspect */class LoggingAspect { /** * @FLOW3Inject * @var ExamplesForumLoggerApplicationLoggerInterface AOP */ protected $applicationLogger; /** * Log a message if a post is deleted * * @param TYPO3FLOW3AOPJoinPointInterface $joinPoint * @FLOW3Before("method(ExamplesForumDomainModelForum->deletePost())") * @return void */ public function logDeletePost(TYPO3FLOW3AOPJoinPointInterface $joinPoint) { $post = $joinPoint->getMethodArgument(post); $this->applicationLogger->log(Removing post . $post->getTitle(), LOG_INFO); }}
  • 426. http://ecdesignrebels.blogspot.comH o w ? e
  • 427.    my 
  • 428.   ow n
  • 429.    rit 
  • 430.   i
  • 431.   w ns?How
  • 432.    can tio a nno ta
  • 433. My
  • 434.   ProjectAnnotations
  • 435. My
  • 436.   Project Annotation
  • 437.    EngineAnnotations
  • 438. /** * @tag(parameters) */public function method() Code
  • 439. /** * @tag(parameters)ReflectionClass->getDocComment() */ public function method() Code
  • 440. /** * @tag(parameters) ReflectionClass->getDocComment() */ public function method() Code/** * @tag(parameters) */DOCBlock
  • 441. /** * @tag(parameters) ReflectionClass->getDocComment() */ public function method() Code/** * @tag(parameters) */DOCBlock Tag() + parameters Annotation
  • 442.   Instances
  • 443. Annotation EnginesDoctrine
  • 444.   Commons https://github.com/doctrine/commonphpDocumentor
  • 445.   2 https://github.com/phpDocumentor/phpDocumentor2php-annotations https://github.com/mindplay-dk/php-annotationsNotoj https://github.com/crodas/Notoj
  • 446. Annotation EnginesDoctrine
  • 447.   CommonsphpDocumentor
  • 448.   2php-annotationsNotoj
  • 449. Annotation EnginesDoctrine
  • 450.   Commons 2.3phpDocumentor
  • 451.   2 2.0php-annotations 1.1Notoj -
  • 452. Annotation EnginesDoctrine
  • 453.   Commons 2.3phpDocumentor
  • 454.   2 2.0php-annotations 1.1Notoj -
  • 455. Annotation EnginesDoctrine
  • 456.   Commons 2.3phpDocumentor
  • 457.   2 2.0php-annotations 1.1Notoj -
  • 458. Annotation EnginesDoctrine
  • 459.   Commons 2.3 full
  • 460.   supportphpDocumentor
  • 461.   2 2.0 only
  • 462.   string
  • 463.   php-annotations 1.1 full
  • 464.   supportNotoj - full
  • 465.   support
  • 466. Annotation EnginesDoctrine
  • 467.   Commons 2.3 full
  • 468.   supportphpDocumentor
  • 469.   2 2.0 only
  • 470.   string
  • 471.   php-annotations 1.1 full
  • 472.   supportNotoj - full
  • 473.   support
  • 474. Annotation EnginesDoctrine
  • 475.   Commons 2.3 full
  • 476.   supportphpDocumentor
  • 477.   2 2.0 only
  • 478.   string
  • 479.   php-annotations 1.1 full
  • 480.   supportNotoj - full
  • 481.   support
  • 482. <?php use DoctrineORMMapping as ORM; use SymfonyComponentValidator Constraints as Assert;/** * @ORMColumn(‘string’) * @AssertNotBlank() */ How
  • 483.   it
  • 484.   Works
  • 485. Declare
  • 486.   which
  • 487.   Annotations
  • 488.   <?php you
  • 489.   will
  • 490.   use use DoctrineORMMapping as ORM; use SymfonyComponentValidator Constraints as Assert;/** * @ORMColumn(‘string’) * @AssertNotBlank() */ How
  • 491.   it
  • 492.   Works
  • 493. AnnotationReader Declare
  • 494.   which
  • 495.   Annotations
  • 496.    <?php you
  • 497.   will
  • 498.   use use DoctrineORMMapping as ORM; use SymfonyComponentValidator Constraints as Assert; /** * @ORMColumn(‘string’) * @AssertNotBlank() */ How
  • 499.   it
  • 500.   Works
  • 501. AnnotationReader Declare
  • 502.   which
  • 503.   Annotations
  • 504.    <?php you
  • 505.   will
  • 506.   use use DoctrineORMMapping as ORM; use SymfonyComponentValidator Constraints as Assert; /** * @ORMColumn(‘string’) * @AssertNotBlank() “metadata” */new ORMColumn(‘string’)new AssertNotBlank() How
  • 507.   it
  • 508.   Works
  • 509. AnnotationReader Declare
  • 510.   which
  • 511.   Annotations
  • 512.    <?php you
  • 513.   will
  • 514.   use use DoctrineORMMapping as ORM; use SymfonyComponentValidator Constraints as Assert; /** * @ORMColumn(‘string’) * @AssertNotBlank() “metadata” */new ORMColumn(‘string’)new AssertNotBlank() Walker
  • 515.   /
  • 516.   code Cache How
  • 517.   it
  • 518.   Works
  • 519. Input Filtering DMSFilter: https://github.com/rdohms/DMS
  • 520. Input Filtering With
  • 521.   Annotation
  • 522.   Support! DMSFilter: https://github.com/rdohms/DMS
  • 523. Ingredients:-
  • 524.   Filter
  • 525.   Rules the
  • 526.   annotation
  • 527.   /
  • 528.   metadata,
  • 529.   configuration-
  • 530.   Filter
  • 531.   Executioners applies
  • 532.   the
  • 533.   rules-
  • 534.   Filter
  • 535.   Service the
  • 536.   middle-man-
  • 537.   Annotation
  • 538.   Engine gets
  • 539.   the
  • 540.   rules
  • 541.   out
  • 542.   of
  • 543.   the
  • 544.   docblocks-
  • 545.   Annotated
  • 546.   Objects defines
  • 547.   the
  • 548.   rules
  • 549. annotated
  • 550.   object “Configured”
  • 551.   Rules@FilterStripTags(‘<b><i>’) new RulesStripTags(‘<b><i>’); <p>Hello
  • 552.   <b>World</b></p> Filter
  • 553.   Service Annotation
  • 554.   Engine Filtered
  • 555.   values Filter
  • 556.   Enforcer Hello
  • 557.   <b>World</b> $executioner = new FilterStripTags(); $executioner->filter($metadata, $value);
  • 558. The “rule”
  • 559. <?php Usage:
  • 560.   namespace DMSFilterRules; use DMSFilterRules as Filter;/** @FilterStripTags(‘<b><i>’) * StripTags Rule * * @Annotation */class StripTags extends Rule{ /** * String of allowed tags. Ex: <b><i><a> * * @var string */ public $allowed = null; /** * {@inheritDoc} */ public function getDefaultOption() { return allowed; }}
  • 561. <?php Usage:
  • 562.   namespace DMSFilterRules; use DMSFilterRules as Filter;/** @FilterStripTags(‘<b><i>’) * StripTags Rule * * @Annotation */class StripTags extends Rule{ /** * String of allowed tags. Ex: <b><i><a> * * @var string tell
  • 563.   doctrine
  • 564.   this
  • 565.    */ public $allowed = null; is
  • 566.   an
  • 567.   annotation /** * {@inheritDoc} */ public function getDefaultOption() { return allowed; }}
  • 568. <?php Usage:
  • 569.   namespace DMSFilterRules; use DMSFilterRules as Filter;/** @FilterStripTags(‘<b><i>’) * StripTags Rule * * @Annotation */class StripTags extends Rule{ /** * String of allowed tags. Ex: <b><i><a> * * @var string tell
  • 570.   doctrine
  • 571.   this
  • 572.    */ public $allowed = null; is
  • 573.   an
  • 574.   annotation /** * {@inheritDoc} */ public function getDefaultOption() { return allowed; }} DMSFilterRulesRule: public function __construct($options = null)
  • 575. /** * @MyAnnotation(“name”, nullable=true) */
  • 576. defaultProperty/** * @MyAnnotation(“name”, nullable=true) */
  • 577. defaultProperty/** * @MyAnnotation(“name”, nullable=true) */new MyAnnotation($options);
  • 578. defaultProperty/** * @MyAnnotation(“name”, nullable=true) */ array:
  • 579.   key
  • 580.   =>
  • 581.   value or string:
  • 582.   valuenew MyAnnotation($options);
  • 583. defaultProperty/** * @MyAnnotation(“name”, nullable=true) */ array:
  • 584.   key
  • 585.   =>
  • 586.   value or string:
  • 587.   valuenew MyAnnotation($options); or
  • 588. defaultProperty/** * @MyAnnotation(“name”, nullable=true) */ array:
  • 589.   key
  • 590.   =>
  • 591.   value or string:
  • 592.   valuenew MyAnnotation($options); or$a = new MyAnnotation();$a->nullable = true;$a->{$a->getDefaultProperty()} = “name”;
  • 593. The “enforcer”
  • 594. <?phpnamespace DMSFilterFilters;use DMSFilterRulesRule;/** * StripTags Rule * * @package DMS * @subpackage Filter * * @Annotation */class StripTags extends BaseFilter{ /** * {@inheritDoc} * * @param DMSFilterRulesStripTags $filter * @param mixed $filter */ public function apply( Rule $filter, $value) { return strip_tags($value, $filter->allowed); }}
  • 595. <?phpnamespace DMSFilterFilters;use DMSFilterRulesRule;/** * StripTags Rule * * @package DMS * @subpackage Filter * * @Annotation */class StripTags extends BaseFilter{ /** * {@inheritDoc} * * @param DMSFilterRulesStripTags $filter * @param mixed $filter */ public function apply( Rule $filter, $value) { Does
  • 596.   the
  • 597.   filtering return strip_tags($value, $filter->allowed); }}
  • 598. <?phpnamespace DMSFilterFilters;use DMSFilterRulesRule;/** * StripTags Rule * * @package DMS * @subpackage Filter Rule
  • 599.   has
  • 600.   the
  • 601.    * * @Annotation configuration. */class StripTags extends BaseFilter{ Ex:
  • 602.   allowed
  • 603.   tags /** * {@inheritDoc} * * @param DMSFilterRulesStripTags $filter * @param mixed $filter */ public function apply( Rule $filter, $value) { Does
  • 604.   the
  • 605.   filtering return strip_tags($value, $filter->allowed); }}
  • 606. The Filter Service
  • 607. The
  • 608.   Life
  • 609.   of
  • 610.   a
  • 611.   Filter
  • 612.   Service
  • 613. The
  • 614.   Life
  • 615.   of
  • 616.   a
  • 617.   Filter
  • 618.   Service09:00
  • 619.   -
  • 620.   DIC
  • 621.   wake-up
  • 622.   call new FilterService($reader);
  • 623. The
  • 624.   Life
  • 625.   of
  • 626.   a
  • 627.   Filter
  • 628.   Service09:00
  • 629.   -
  • 630.   DIC
  • 631.   wake-up
  • 632.   call new FilterService($reader);09:05
  • 633.   -
  • 634.   Reply
  • 635.   to
  • 636.   emergency
  • 637.   filter
  • 638.   call $service->filter($object);
  • 639. The
  • 640.   Life
  • 641.   of
  • 642.   a
  • 643.   Filter
  • 644.   Service09:00
  • 645.   -
  • 646.   DIC
  • 647.   wake-up
  • 648.   call new FilterService($reader);09:05
  • 649.   -
  • 650.   Reply
  • 651.   to
  • 652.   emergency
  • 653.   filter
  • 654.   call $service->filter($object);09:06
  • 655.   -
  • 656.   Get
  • 657.   Annotations
  • 658.   for
  • 659.   properties $reader->getPropertyAnnotations($p); $a = array(StripTags(), Alpha());
  • 660. The
  • 661.   Life
  • 662.   of
  • 663.   a
  • 664.   Filter
  • 665.   Service09:00
  • 666.   -
  • 667.   DIC
  • 668.   wake-up
  • 669.   call new FilterService($reader);09:05
  • 670.   -
  • 671.   Reply
  • 672.   to
  • 673.   emergency
  • 674.   filter
  • 675.   call $service->filter($object);09:06
  • 676.   -
  • 677.   Get
  • 678.   Annotations
  • 679.   for
  • 680.   properties $reader->getPropertyAnnotations($p); $a = array(StripTags(), Alpha());09:07
  • 681.   -
  • 682.   Track
  • 683.   down
  • 684.   the
  • 685.   Filters new FiltersStripTags();
  • 686. The
  • 687.   Life
  • 688.   of
  • 689.   a
  • 690.   Filter
  • 691.   Service09:00
  • 692.   -
  • 693.   DIC
  • 694.   wake-up
  • 695.   call new FilterService($reader);09:05
  • 696.   -
  • 697.   Reply
  • 698.   to
  • 699.   emergency
  • 700.   filter
  • 701.   call $service->filter($object);09:06
  • 702.   -
  • 703.   Get
  • 704.   Annotations
  • 705.   for
  • 706.   properties $reader->getPropertyAnnotations($p); $a = array(StripTags(), Alpha());09:07
  • 707.   -
  • 708.   Track
  • 709.   down
  • 710.   the
  • 711.   Filters new FiltersStripTags(); 09:08
  • 712.   -
  • 713.   Get
  • 714.   values
  • 715.   filtered $filter->apply($rule, $value);
  • 716. The
  • 717.   Life
  • 718.   of
  • 719.   a
  • 720.   Filter
  • 721.   Service09:00
  • 722.   -
  • 723.   DIC
  • 724.   wake-up
  • 725.   call new FilterService($reader);09:05
  • 726.   -
  • 727.   Reply
  • 728.   to
  • 729.   emergency
  • 730.   filter
  • 731.   call $service->filter($object);09:06
  • 732.   -
  • 733.   Get
  • 734.   Annotations
  • 735.   for
  • 736.   properties $reader->getPropertyAnnotations($p); $a = array(StripTags(), Alpha());09:07
  • 737.   -
  • 738.   Track
  • 739.   down
  • 740.   the
  • 741.   Filters new FiltersStripTags(); 09:08
  • 742.   -
  • 743.   Get
  • 744.   values
  • 745.   filtered $filter->apply($rule, $value); 09:10
  • 746.   -
  • 747.   Update
  • 748.   the
  • 749.   object
  • 750.   with
  • 751.   new
  • 752.   data $object->prop = $newValue;
  • 753. The
  • 754.   Life
  • 755.   of
  • 756.   a
  • 757.   Filter
  • 758.   Service09:00
  • 759.   -
  • 760.   DIC
  • 761.   wake-up
  • 762.   call new FilterService($reader);09:05
  • 763.   -
  • 764.   Reply
  • 765.   to
  • 766.   emergency
  • 767.   filter
  • 768.   call $service->filter($object);09:06
  • 769.   -
  • 770.   Get
  • 771.   Annotations
  • 772.   for
  • 773.   properties $reader->getPropertyAnnotations($p); $a = array(StripTags(), Alpha());09:07
  • 774.   -
  • 775.   Track
  • 776.   down
  • 777.   the
  • 778.   Filters new FiltersStripTags(); 09:08
  • 779.   -
  • 780.   Get
  • 781.   values
  • 782.   filtered $filter->apply($rule, $value); 09:10
  • 783.   -
  • 784.   Update
  • 785.   the
  • 786.   object
  • 787.   with
  • 788.   new
  • 789.   data $object->prop = $newValue; 09:11
  • 790.   -
  • 791.   send
  • 792.   the
  • 793.   object
  • 794.   on
  • 795.   its
  • 796.   way return $object;
  • 797. The
  • 798.   Life
  • 799.   of
  • 800.   a
  • 801.   Filter
  • 802.   Service09:00
  • 803.   -
  • 804.   DIC
  • 805.   wake-up
  • 806.   call new FilterService($reader);09:05
  • 807.   -
  • 808.   Reply
  • 809.   to
  • 810.   emergency
  • 811.   filter
  • 812.   call $service->filter($object);09:06
  • 813.   -
  • 814.   Get
  • 815.   Annotations
  • 816.   for
  • 817.   properties $reader->getPropertyAnnotations($p); $a = array(StripTags(), Alpha());09:07
  • 818.   -
  • 819.   Track
  • 820.   down
  • 821.   the
  • 822.   Filters new FiltersStripTags(); 09:08
  • 823.   -
  • 824.   Get
  • 825.   values
  • 826.   filtered $filter->apply($rule, $value); 09:10
  • 827.   -
  • 828.   Update
  • 829.   the
  • 830.   object
  • 831.   with
  • 832.   new
  • 833.   data $object->prop = $newValue; 09:11
  • 834.   -
  • 835.   send
  • 836.   the
  • 837.   object
  • 838.   on
  • 839.   its
  • 840.   way return $object; 09:12
  • 841.   -
  • 842.   DO
  • 843.   IT
  • 844.   ALL
  • 845.   AGAIN!
  • 846. The
  • 847.   Life
  • 848.   of
  • 849.   a
  • 850.   Filter
  • 851.   Service09:00
  • 852.   -
  • 853.   DIC
  • 854.   wake-up
  • 855.   call new FilterService($reader);09:05
  • 856.   -
  • 857.   Reply
  • 858.   to
  • 859.   emergency
  • 860.   filter
  • 861.   call $service->filter($object);09:06
  • 862.   -
  • 863.   Get
  • 864.   Annotations
  • 865.   for
  • 866.   properties $reader->getPropertyAnnotations($p); $a = array(StripTags(), Alpha());09:07
  • 867.   -
  • 868.   Track
  • 869.   down
  • 870.   the
  • 871.   Filters new FiltersStripTags(); 09:08
  • 872.   -
  • 873.   Get
  • 874.   values
  • 875.   filtered $filter->apply($rule, $value); 09:10
  • 876.   -
  • 877.   Update
  • 878.   the
  • 879.   object
  • 880.   with
  • 881.   new
  • 882.   data $object->prop = $newValue; 09:11
  • 883.   -
  • 884.   send
  • 885.   the
  • 886.   object
  • 887.   on
  • 888.   its
  • 889.   way return $object; 09:12
  • 890.   -
  • 891.   DO
  • 892.   IT
  • 893.   ALL
  • 894.   AGAIN!
  • 895. //Iterate over all annotationsforeach($this->reader->getPropertyAnnotations($property) as $rule) { //Skip is its not a rule if ( ! $rule instanceof RulesRule ) continue; //Add Rule $metadata->addPropertyRule($property->getName(), $rule);}
  • 896. get
  • 897.   all
  • 898.   annotation
  • 899.    objects//Iterate over all annotationsforeach($this->reader->getPropertyAnnotations($property) as $rule) { //Skip is its not a rule if ( ! $rule instanceof RulesRule ) continue; //Add Rule $metadata->addPropertyRule($property->getName(), $rule);}
  • 900. get
  • 901.   all
  • 902.   annotation
  • 903.    objects//Iterate over all annotationsforeach($this->reader->getPropertyAnnotations($property) as $rule) { //Skip is its not a rule if ( ! $rule instanceof RulesRule ) continue; //Add Rule $metadata->addPropertyRule($property->getName(), $rule);} filter
  • 904.   out
  • 905.   “our”
  • 906.    annotations
  • 907. get
  • 908.   all
  • 909.   annotation
  • 910.    objects//Iterate over all annotationsforeach($this->reader->getPropertyAnnotations($property) as $rule) { //Skip is its not a rule if ( ! $rule instanceof RulesRule ) continue; //Add Rule $metadata->addPropertyRule($property->getName(), $rule);} filter
  • 911.   out
  • 912.   “our”
  • 913.    annotations glorified
  • 914.   array
  • 915. Necessary Object Calisthenics Disclaimer FilterService MetadataFactory ObjectWalkerLoader CacheReader Cache ReflectionProperty
  • 916. //Get Doctrine Reader$reader = new AnnotationsAnnotationReader();$reader->setEnableParsePhpImports(true);//Load AnnotationLoader$loader = new MappingLoaderAnnotationLoader($reader);$this->loader = $loader;//Get a MetadataFactory$metadataFactory = new MappingClassMetadataFactory($loader);//Get a Filter$filter = new DMSFilterFilter($metadataFactory);//Get your Entity$user = new AppEntityUser();$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"
  • 917. //Get Doctrine Reader$reader = new AnnotationsAnnotationReader();$reader->setEnableParsePhpImports(true); put
  • 918.   this
  • 919.   in
  • 920.   your
  • 921.   DIC//Load AnnotationLoader$loader = new MappingLoaderAnnotationLoader($reader);$this->loader = $loader;//Get a MetadataFactory$metadataFactory = new MappingClassMetadataFactory($loader);//Get a Filter$filter = new DMSFilterFilter($metadataFactory);//Get your Entity$user = new AppEntityUser();$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"
  • 922. //Get Doctrine Reader$reader = new AnnotationsAnnotationReader();$reader->setEnableParsePhpImports(true); put
  • 923.   this
  • 924.   in
  • 925.   your
  • 926.   DIC//Load AnnotationLoader$loader = new MappingLoaderAnnotationLoader($reader);$this->loader = $loader;//Get a MetadataFactory$metadataFactory = new MappingClassMetadataFactory($loader);//Get a Filter$filter = new DMSFilterFilter($metadataFactory);//Get your Entity$user = new AppEntityUser(); Calling
  • 927.   the
  • 928.   Service$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"
  • 929. What? Why? Where? How?
  • 930. Questions? Rate
  • 931.   me,
  • 932.   please! https://joind.in/7912 on
  • 933.   twitter@rdohms these
  • 934.   slides
  • 935.   http://doh.ms will
  • 936.   be
  • 937.   herehttp://slides.doh.ms