Annotating with Annotations - ForumPHP 2012

Download Annotating with Annotations - ForumPHP 2012

Post on 08-May-2015

21.438 views

Category:

Technology

1 download

Embed Size (px)

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

<ul><li>1.*</li></ul><p>2. Forum 3. PHP 4. Paris 5. 2012Annotating 6. with 7. Annotations A 8. look 9. into 10. Annotations 11. in 12. PHPon 13. twitterRafael 14. Dohms@rdohms 15. Rafael Dohms photo credit: Eli White@rdohms Evangelist, Speaker andContributor. Podcaster, User GroupLeader.Developer at WEBclusive. 16. a 17. little 18. history existing 19. uses What?Why?Where? How? ustom 20. ann otations Impl ementing 21. c DMSFilter based 22. on 23. show 24. me 25. the 26. code! 27. http://ecdesignrebels.blogspot.com w h a t? ta tio ns?re 28. an noW ha t 29. a 30. -- In English --An annotation is a note that is made whilereading any form of text. 31. something 32. that 33. describes 34. an 35. aspect 36. of 37. the 38. subject -- In English --An annotation is a note that is made whilereading any form of text. 39. -- In Code Speak --An annotation describes behavior of code and affects your application in runtime. Annotations do not directly affect programsemantics 40. -- In Code Speak --An annotation describes behavior of code and affects your application in runtime. Annotations do not directly affect programsemanticsjust 41. like 42. your 43. notes 44. In 45. 2004: 46. Metadata 47. or 48. General 49. purpose 50. Annotations 51. In 52. 2004: 53. Metadata 54. or 55. General 56. purpose 57. Annotations available 58. at 59. Runtime 60. using 61. Reflection 62. In 63. 2004: 64. Metadata 65. or 66. General 67. purpose 68. Annotations@Entity@Table(name = people)class Person implements Serializable {@Id@GeneratedValue(strategy = GenerationType.AUTO)private Integer id;@Column(length = 32)private String name; available 69. at 70. Runtime 71. using 72. Reflection 73. In 74. 2004: 75. Metadata 76. or 77. General 78. purpose 79. Annotationsspecific 80. syntax@Entity@Table(name = people)class Person implements Serializable {@Id@GeneratedValue(strategy = GenerationType.AUTO)private Integer id;@Column(length = 32)private String name; available 81. at 82. Runtime 83. using 84. Reflection 85. No 86. core 87. annotation 88. support 89. Questions? 90. Questions?Im 91. kidding! 92. phpDoc~2000 93. PHP 94. 5.1Reflection 95. supports 96. getDocComments()phpDoc~20002005 97. PHP 98. 5.1 Reflection 99. supports 100. getDocComments()phpDoc~2000 2005First 101. Annotation 102. Engines 103. in 104. PHP 105. PHP 106. 5.1 Reflection 107. supports 108. getDocComments()phpDoc~2000 2005 2008First 109. Annotation 110. Engines 111. in 112. PHP 113. PHP 114. 5.1 Reflection 115. supports 116. getDocComments()phpDoc~2000 2005 2008First 117. Doctrine 118. 2 119. AnnotationAnnotation 120. Engine 121. Engines 122. in 123. PHP 124. PHP 125. 5.1 Reflection 126. supports 127. getDocComments()RFC: 128. Annotations 129. in 130. core 131. w/ 132. custom 133. phpDocsyntax~2000 2005 2008 2010First 134. Doctrine 135. 2 136. Annotation Annotation 137. Engine 138. Engines 139. in 140. PHP 141. PHP 142. 5.1 Reflection 143. supports 144. getDocComments()RFC: 145. Annotations 146. ED CTustom 147. in 148. core 149. JE/ 150. c wphpDoc E Rsyntax~2000 2005 20082010First 151. Doctrine 152. 2 153. Annotation Annotation 154. Engine 155. Engines 156. in 157. PHP 158. PHP 159. 5.1 RFC: 160. DocBlock 161. Reflection 162. supports 163. AnnotationsgetDocComments()in 164. discussionRFC: 165. Annotations 166. ED CTustom 167. in 168. core 169. JE/ 170. c wphpDoc E Rsyntax~2000 2005 20082010 2011First 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. AnnotationsgetDocComments()in 184. discussionRFC: 185. Annotations 186. ED CTustom 187. in 188. core 189. JE/ 190. c wphpDoc E Rsyntax~2000 2005 20082010 2011?First 191. Doctrine 192. 2 193. Annotation Annotation 194. Engine 195. Engines 196. in 197. PHP 198. PHP 199. 5.1 RFC: 200. DocBlock 201. Reflection 202. supports 203. AnnotationsgetDocComments()/*** CaasBundleFundingBundleEntityRewardin 204. discussion*RFC: 205. Annotations 206. ED* @ORMTable(reward) CTustom 207. * @ORMEntity(repositoryClass=CaasBundleFundingBundleEntityRewardRepository)*/class Reward in 208. core 209. JE/ 210. c w E{phpDoc /** Rsyntax?* @var integer $id** @ORMColumn(name=id, type=integer)* @ORMId* @ORMGeneratedValue(strategy=AUTO)~2000 */ protected $id; 2005 20082010 2011/** * @var string $title * * @ORMColumn(name=title, type=string, length=150, nullable=true) *First 211. * @AssertMaxLength(150) */protected $title;Doctrine 212. 2 213. Annotation Annotation 214. Engine 215. Engines 216. in 217. PHP 218. Note: 219. Note:// 220. This 221. is 222. a 223. commentT_COMMENT/* 224. This 225. is 226. a 227. multiline 228. comment 229. */ 230. Note:// 231. This 232. is 233. a 234. commentT_COMMENT/* 235. This 236. is 237. a 238. multiline 239. comment 240. *//** 241. 242. * 243. This 244. is 245. a 246. doc 247. block T_DOC_COMMENT 248. */ 249. /** * @deprecated * * @ORMColumn(string, length=255) */public function method() 250. marker/** * @deprecated * * @ORMColumn(string, length=255) */public function method() 251. markerparameterized/** * @deprecated * * @ORMColumn(string, length=255) */public function method() 252. http://ecdesignrebels.blogspot.com Wh y ? 253. an no tatio ns?uld 254. I 255. use/co ns?hy 256. shoe 257. p rosW t 258. are 259. thw ha 260. - 261. Harder 262. to 263. debug 264. (dynamic 265. code)- 266. Harder 267. to 268. debug 269. (dynamic 270. code)- Performance 271. hit 272. (non-native 273. code) 274. Harder 275. to 276. debug 277. (dynamic 278. code)- Performance 279. hit 280. (non-native 281. code)Its 282. implemented 283. in 284. comments 285. docblocks! 286. + 287. Easier 288. to 289. inject 290. behavior 291. without 292. extending+ 293. Easier 294. to 295. inject 296. behavior 297. without 298. extending Doctrine 299. 1 300. vs 301. Doctrine 302. 2+ 303. Easier 304. to 305. inject 306. behavior 307. without 308. extending Doctrine 309. 1 310. vs 311. Doctrine 312. 2Contextualizes 313. behavior/config 314. in 315. the 316. object+ 317. Easier 318. to 319. inject 320. behavior 321. without 322. extendingDoctrine 323. 1 324. vs 325. Doctrine 326. 2Contextualizes 327. behavior/config 328. in 329. the 330. object+In 331. object 332. vs. 333. external 334. configuration 335. file 336. Easier 337. to 338. inject 339. behavior 340. without 341. extendingDoctrine 342. 1 343. vs 344. Doctrine 345. 2Contextualizes 346. behavior/config 347. in 348. the 349. object+In 350. object 351. vs. 352. external 353. configuration 354. fileIts 355. documented/stored 356. by 357. phpDocumentor 358. Easier 359. to 360. inject 361. behavior 362. without 363. extending Doctrine 364. 1 365. vs 366. Doctrine 367. 2Contextualizes 368. behavior/config 369. in 370. the 371. object+In 372. object 373. vs. 374. external 375. configuration 376. fileIts 377. documented/stored 378. by 379. phpDocumentorIts 380. in 381. docblocks, 382. so 383. its 384. parsed 385. ?phpclass User{protected $name...} 386. ?php - 387. persist 388. as 389. varchar length 390. should 391. not 392. be 393. class User{- 394. more 395. then 396. 255- 397. should 398. not 399. be 400. blank- 401. only 402. lettersprotected $name...} 403. persistence DoctrineTestsORMMappingUser: type: entity table: cms_users?phpid: id: - 404. persist 405. as 406. varchar type: integer length 407. should 408. not 409. be 410. class User generator:{- 411. strategy: AUTO more 412. then 413. 255 fields: name:- 414. should 415. not 416. be 417. blanktype: string- 418. only 419. letters length: 255protected $name ValidationAcmeBlogBundleEntityAuthor:properties:name:... - NotBlank: ~- MaxLength: 255filterAcmeBlogBundleEntityAuthor:} filters:name: - alpha 420. persistenceValidation?phpclass Userfilter{/** * @ORMColumn(string, length=255) * @AssertNotBlank() * @AssertMaxLength(255) * @FilterAlpha() */protected $name...} 421. ?http://ecdesignrebels.blogspot.comW h e r e tions 422. used? 423. ann ota er e 424. areWh 425. class DataTest extends PHPUnit_Framework_TestCase{/*** @dataProvider providerrepetition** @expectedException InvalidArgumentException* @expectedExceptionMessage Right Message*/public function testAdd($a, $b, $c) expectations{ /* Test code */}public function provider(){return array(array(0, 0, 0),);}} 426. /*** @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;} 427. /*** @Route(/myaction/{id}, name=myaction)* @Method(POST)** @Template(MyBundle:MyController:my.html.twig)** @param int $id* @return arrayrouting*/public function myAction($id){/* Controller Logic */templatingreturn array(data = $data);}class MyEntity{/** * @var string $title * * @ORMColumn(name=title, type=string, length=255) * @AssertMaxLength(255) * @AssertNotBlank() */protected $title;} Validation 428. /** Dependency 429. 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);}} 430. http://ecdesignrebels.blogspot.comH o w ? e 431. my 432. ow n 433. rit 434. i 435. w ns?How 436. can tioa nnota 437. My 438. ProjectAnnotations 439. Annotation 440. EngineMy 441. ProjectAnnotations 442. /** * @tag parameters */public function method() Code 443. /**ReflectionClass-getDocComment()* @tag parameters*/ public function method()Code 444. /** ReflectionClass-getDocComment()* @tag parameters */public function method() Code/** * @tag parameters */DOCBlock 445. /** ReflectionClass-getDocComment()* @tag parameters */public function method() Code/** * @tag parameters */DOCBlock 446. /** ReflectionClass-getDocComment()* @tag parameters */public function method()Code/** * @tag parameters */DOCBlockTag() + parametersAnnotation 447. Instances 448. Annotation EnginesDoctrine 449. CommonsZF2 450. AnnotationsphpDocumentor 451. 2abandoned?Notoj php-annotations addendumStubble 452. Annotation EnginesNotoj 2.3 2.0 453. - 454. alpha ?Parameterizedmarkerparameterizedvery 455. mature maturingnew 456. Annotation EnginesNotoj 2.3 2.0 457. - 458. alpha ?Parameterizedmarkerparameterizedvery 459. mature maturingnew 460. How 461. it 462. Works ?phpuse DoctrineORMMapping as ORM;use SymfonyComponentValidatorConstraints as Assert; /*** @ORMColumn(string)* @AssertNotBlank()*/ 463. How 464. it 465. Works Declare 466. which 467. Annotations 468. you 469. will 470. use ?phpuse DoctrineORMMapping as ORM;use SymfonyComponentValidatorConstraints as Assert; /*** @ORMColumn(string)* @AssertNotBlank()*/ 471. How 472. it 473. Works Declare 474. which 475. Annotations 476. you 477. will 478. useAnnotationReader ?phpuse DoctrineORMMapping as ORM;use SymfonyComponentValidatorConstraints as Assert; /*** @ORMColumn(string)* @AssertNotBlank()*/ 479. How 480. it 481. Works Declare 482. which 483. Annotations 484. you 485. will 486. useAnnotationReader ?phpuse DoctrineORMMapping as ORM;use SymfonyComponentValidatorConstraints as Assert; /*** @ORMColumn(string)* @AssertNotBlank()metadata*/new ORMColumn(string)new AssertNotBlank() 487. How 488. it 489. Works Declare 490. which 491. Annotations 492. you 493. will 494. useAnnotationReader ?phpuse DoctrineORMMapping as ORM;use SymfonyComponentValidatorConstraints as Assert; /*** @ORMColumn(string)* @AssertNotBlank()metadata*/new ORMColumn(string)new AssertNotBlank()Walker 495. / 496. code Cache 497. DMSFilterhttps://github.com/rdohms/DMS 498. The 499. Gears 500. The 501. Gears YourApp DoctrineCommons 502. The 503. Gears Reader YourApp DoctrineCommons 504. The 505. GearsParsing Reader YourApp DoctrineCommons 506. The 507. Gears Parsing Filter ReaderYourApp DoctrineCommons 508. The 509. Gears Enforcing Parsing Filter ReaderYourApp DoctrineCommons 510. The 511. Gears Enforcing Parsing Filter ReaderAnnotatedObjectYourApp DoctrineCommons 512. The 513. Gears Enforcing Parsing FilterReaderAnnotatedObjectMyAnnotationYourApp DoctrineCommons 514. The 515. Gears Enforcing Parsing FilterReaderAnnotatedObject lter()MyAnnotationYourApp DoctrineCommons 516. The 517. Gears Enforcing Parsing Filter ReaderAnnotatedObject lter() getMethodAnnotation()MyAnnotationYourAppDoctrineCommons 518. The 519. Gears Enforcing Parsing Filter ReaderAnnotatedObject lter() getMethodAnnotation()MyAnnotationYourAppDoctrineCommons 520. The 521. Gears Enforcing ParsingFilterReaderAnnotatedObject lter() getMethodAnnotation()ltered valueMyAnnotationYourAppDoctrineCommons 522. Usage: 523. @FilterNoDogs()namespace FilterRule;/*** @Annotation*/class NoDogs{ public function __construct($options) { /* no options */ }public function filter($value){return str_replace(dogs, , (string) $value);}} 524. Usage: 525. @FilterNoDogs()namespace FilterRule;/*** @Annotation tell 526. doctrine 527. this 528. */class NoDogs is 529. an 530. annotation{ public function __construct($options) { /* no options */ }public function filter($value){return str_replace(dogs, , (string) $value);}} 531. Usage: 532. @FilterNoDogs()namespace FilterRule;/*** @Annotation tell 533. doctrine 534. this 535. */class NoDogs is 536. an 537. annotation{ public function __construct($options) { /* no options */ }array 538. of 539. the 540. parameterspublic function filter($value){return str_replace(dogs, , (string) $value);}} 541. Usage: 542. @FilterNoDogs()namespace FilterRule;/*** @Annotation tell 543. doctrine 544. this 545. */class NoDogs is 546. an 547. annotation{ public function __construct($options) { /* no options */ }array 548. of 549. the 550. parameterspublic function filter($value){return str_replace(dogs, , (string) $value);}} this 551. is 552. the 553. effect 554. of 555. our 556. annotation 557. namespace Filter;class Filter{private $reader;public function __construct(DoctrineCommonAnnotationsReader $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, FilterRuleNoDog);if (null !== $annotation) {return;}$reflectionPropert...</p>