Mais conteúdo relacionado Semelhante a Annotations in PHP: They Exist (20) Mais de Rafael Dohms (20) Annotations in PHP: They Exist10. Rafael Dohms
photo credit: Eli White
@rdohms
Evangelist, Speaker and
Contributor.
Developer at WEBclusive.
Enabler at AmsterdamPHP.
we
18. uses
What? Why? Where?
How?
ustom
29. -- In English --
An annotation is a note that is made while
reading any form of text.
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
44. -- In Code Speak --
“An annotation is metadata attached to your
code, that can be read at runtime.”
effects
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
76.
getDocComments()
phpDoc
~2000 2005
80.
getDocComments()
phpDoc
~2000 2005
First
88.
getDocComments()
phpDoc
~2000 2005 2008
First
96.
getDocComments()
phpDoc
~2000 2005 2008
First
99.
Annotation
Annotation
107.
getDocComments()
RFC:
113.
phpDoc syntax
~2000 2005 2008 2010
First
116.
Annotation
Annotation
124.
getDocComments()
RFC:
130. c
w
phpDoc E
Rsyntax
~2000 2005 2008 2010
First
133.
Annotation
Annotation
143.
Annotations
getDocComments()
“in
150. c
w
phpDoc E
Rsyntax
~2000 2005 2008 2010 2011
First
153.
Annotation
Annotation
163.
Annotations
getDocComments()
“in
173. again
phpDoc E
Rsyntax
~2000 2005 2008 2010 2011 2013
First
176.
Annotation
Annotation
186.
Annotations
getDocComments()
“in
196. again
phpDoc E
Rsyntax
~2000 2005 2008 2010 2011 2013 ?
First
199.
Annotation
Annotation
207. /**
* 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;
210. 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;
212. On docblocks vs. comments
// this is a comment
/* This is a multiline comment */
T_COMMENT
213. On docblocks vs. comments
// this is a comment
/* This is a multiline comment */
T_COMMENT
/**
* this is a docblock
*/
T_DOC_COMMENT
217. cache
// this is a comment
/* This is a multiline comment */
T_COMMENT
/**
* this is a docblock
*/
T_DOC_COMMENT
221. cache
// this is a comment
/* This is a multiline comment */
T_COMMENT
cached
224. cache
/**
* this is a docblock
*/
T_DOC_COMMENT
226. 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; }
227. 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; }
228. 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;
parameterized
public class Customer
{
[Required]
[StringLength(50)]
public string Prename { get; set; }
[Column(TypeName = image)]
public byte[] Image { get; set; }
404. varchar
MyProjectUser:
class User type: entity
e
409. b
table: users
{ fields:
more
415. blank
type: string
length: 255
-
417. letters
protected $name
Validation
MyProjectUser:
properties:
name:
- NotBlank: ~
... - MaxLength: 255
filter
MyProjectUser:
filters:
} name:
- alpha
418. ?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
419. ?php
class User
{
/**
* @ORMColumn(‘string’, length=255)
* @AssertNotBlank()
* @AssertMaxLength(255)
* @FilterAlpha()
*/
protected $name
...
}
420. ?
http://ecdesignrebels.blogspot.com
W h e r e tions
424. ?
http://ecdesignrebels.blogspot.com
W h e r e tions
428. 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),
);
}
}
429. /**
* @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;
}
430. /**
* @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
432. 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);
}
}
447. /**
* @tag(parameters)
ReflectionClass-getDocComment() */
public function method()
Code
448. /**
* @tag(parameters)
ReflectionClass-getDocComment() */
public function method()
Code
/**
* @tag(parameters)
*/
DOCBlock
449. /**
* @tag(parameters)
ReflectionClass-getDocComment() */
public function method()
Code
/**
* @tag(parameters)
*/
DOCBlock
Tag() + parameters
Annotation
452. Commons https://github.com/doctrine/common
phpDocumentor
453. 2 https://github.com/phpDocumentor/phpDocumentor2
php-annotations https://github.com/mindplay-dk/php-annotations
Notoj https://github.com/crodas/Notoj
459. 2 2.0
php-annotations 1.1
Notoj -
462. 2 2.0
php-annotations 1.1
Notoj -
465. 2 2.0
php-annotations 1.1
Notoj -
498. ?php
use DoctrineORMMapping as ORM;
use SymfonyComponentValidator
Constraints as Assert;
/**
* @ORMColumn(‘string’)
* @AssertNotBlank()
*/
How
506. use
use DoctrineORMMapping as ORM;
use SymfonyComponentValidator
Constraints as Assert;
/**
* @ORMColumn(‘string’)
* @AssertNotBlank()
*/
How
514. use
use DoctrineORMMapping as ORM;
use SymfonyComponentValidator
Constraints as Assert;
/**
* @ORMColumn(‘string’)
* @AssertNotBlank()
*/
How
522. use
use DoctrineORMMapping as ORM;
use SymfonyComponentValidator
Constraints as Assert;
/**
* @ORMColumn(‘string’)
* @AssertNotBlank()
“metadata” */
new ORMColumn(‘string’)
new AssertNotBlank()
How
530. use
use DoctrineORMMapping as ORM;
use SymfonyComponentValidator
Constraints as Assert;
/**
* @ORMColumn(‘string’)
* @AssertNotBlank()
“metadata” */
new ORMColumn(‘string’)
new AssertNotBlank()
Walker
538. Support!
DMSFilter: https://github.com/rdohms/DMS
573. bWorld/b
$executioner = new FilterStripTags();
$executioner-filter($metadata, $value);
576.
namespace DMSFilterRules; use DMSFilterRules as Filter;
/** @FilterStripTags(‘bi’)
* StripTags Rule
*
* @Annotation
*/
class StripTags extends Rule
{
/**
* String of allowed tags. Ex: bia
*
* @var string
*/
public $allowed = null;
/**
* {@inheritDoc}
*/
public function getDefaultOption()
{
return 'allowed';
}
}
578.
namespace DMSFilterRules; use DMSFilterRules as Filter;
/** @FilterStripTags(‘bi’)
* StripTags Rule
*
* @Annotation
*/
class StripTags extends Rule
{
/**
* String of allowed tags. Ex: bia
*
* @var string
tell
581.
*/
public $allowed = null; is
583. annotation
/**
* {@inheritDoc}
*/
public function getDefaultOption()
{
return 'allowed';
}
}
585.
namespace DMSFilterRules; use DMSFilterRules as Filter;
/** @FilterStripTags(‘bi’)
* StripTags Rule
*
* @Annotation
*/
class StripTags extends Rule
{
/**
* String of allowed tags. Ex: bia
*
* @var string
tell
588.
*/
public $allowed = null; is
590. annotation
/**
* {@inheritDoc}
*/
public function getDefaultOption()
{
return 'allowed';
}
}
DMSFilterRulesRule:
public function __construct($options = null)
610. ?php
namespace 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);
}
}
611. ?php
namespace 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
613. filtering
return strip_tags($value, $filter-allowed);
}
}
617.
*
* @Annotation configuration.
*/
class StripTags extends BaseFilter
{
Ex:
619. tags
/**
* {@inheritDoc}
*
* @param DMSFilterRulesStripTags $filter
* @param mixed $filter
*/
public function apply( Rule $filter, $value)
{ Does
621. filtering
return strip_tags($value, $filter-allowed);
}
}
638. call new FilterService($reader);
648. call new FilterService($reader);
09:05
654. call $service-filter($object);
664. call new FilterService($reader);
09:05
670. call $service-filter($object);
09:06
675. properties $reader-getPropertyAnnotations($p);
$a = array(StripTags(), Alpha());
685. call new FilterService($reader);
09:05
691. call $service-filter($object);
09:06
696. properties $reader-getPropertyAnnotations($p);
$a = array(StripTags(), Alpha());
09:07
701. Filters new FiltersStripTags();
711. call new FilterService($reader);
09:05
717. call $service-filter($object);
09:06
722. properties $reader-getPropertyAnnotations($p);
$a = array(StripTags(), Alpha());
09:07
727. Filters new FiltersStripTags();
09:08
731. filtered $filter-apply($rule, $value);