maslosoft / addendum
Powerful and easy to use PHP annotations
Installs: 6 377
Dependents: 10
Suggesters: 1
Security: 0
Stars: 7
Watchers: 4
Forks: 0
Open Issues: 6
Requires
- php: >=7.4
- maslosoft/cache: *
- maslosoft/cli-shared: ^1 | ^2
- maslosoft/embedi: ^2
- maslosoft/gazebo: ^2
- psr/log: ^1|^2|^3
Requires (Dev)
- codeception/codeception: ^5
- codeception/module-asserts: ^3
- codeception/specify: ^2
- maslosoft/signals: *@dev
- maslosoft/zamm: ^1 || ^2
- monolog/monolog: ^2
- roave/security-advisories: dev-latest
Suggests
- maslosoft/signals: Collect annotation namespaces with signals
- monolog/monolog: Sends your logs to files, sockets, inboxes, databases and various web services
- dev-master
- 7.0.5
- 7.0.4
- 7.0.3
- 7.0.2
- 7.0.1
- 7.0.0
- 6.0.5
- 6.0.4
- 6.0.3
- 6.0.2
- 6.0.1
- 6.0.0
- 5.0.21
- 5.0.20
- 5.0.19
- 5.0.18
- 5.0.17
- 5.0.16
- 5.0.15
- 5.0.14
- 5.0.12
- 5.0.11
- 5.0.10
- 5.0.9
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.0.21
- 4.0.20
- 4.0.19
- 4.0.18
- 4.0.17
- 4.0.16
- 4.0.15
- 4.0.14
- 4.0.13
- 4.0.12
- 4.0.11
- 4.0.10
- 4.0.9
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.1.8
- 3.1.7
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.13
- 3.0.12
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0-alpha.1
- 2.1.0
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.1
- 1.0.0
- dev-dependabot/composer/guzzlehttp/psr7-2.5.0
This package is auto-updated.
Last update: 2024-11-22 16:21:05 UTC
README
Maslosoft Addendum
Powerful and easy to use PHP annotations
Quick Install
composer require maslosoft/addendum
Documentation
Annotations for PHP
This project provides smart annotations support for PHP language with the aim on performance and ease of use.
Annotations are in fact a kind declarative language embedded in PHP code. It does not allow and flow control structures, loops, conditions. It allows us to describe what is required or what we expect from parf of code.
Where to use annotations
While (PHP) programming language itself is very flexible and allows us to describe complex bahaviors, there are some aspects where we want to only describe (or configure) such behaviors. And we want to keep our main code as is, while adding some additional behaviors, which might include:
- Access control
- Way to display data
- Whether to store data
- Where to store data
- Should the data be searchable
And so on, with ever growing list of aspects and behaviors. While this could be made by implementing in our classe more and more interfaces, soon those would end up in hundreds of methods.
Another way could be some kind of extra meta-data configuration, let it be in XML, JSON or YAML files. This requires us to keep those too files in sync and also separates declarations from code. But we might need extra behaviors/aspects for many libraries - resulting in code being split into two or more files.
Embed behavior into code
So the idea is to embed those extra aspects of class into code. With annotations - special comment tags placed just above class/method/property declaration.
These annotations can be interpreted independently by different parts of program. These are extensible, so that adding more annotations does not influence existing code.
Different application parts might interpret just part of annotations while ignoring unknown ones.
In below example one part will react @Label
and @Description
declarations,
other will perform some extra operations when will encouner @I18N
annotation.
Example:
class Page implements AnnotatedInterface { /** * Page URL * @Label('Page URL') * @Description('Relative URL, it should be simple lowercase string with words separated by hyphen') * @SearchBoost(3.5) * @I18N * @SafeValidator * @UrlValidator * @Decorator(Action, 'update') * @see Action * @var string */ public $url = ''; }
All extra metadata is contained just where class property is declared. This is it.
Annotations itself are not evaluated on each request. These are used to generate
metadata, which is later available by using meta containers. Having any object
implementing AnnotatedInterface
, metadata can be obtained by passing either
object or class name.
Example of getting meta data:
echo Meta::create(Page::class)->url->label; // Will echo "Page URL"
This metadata is cached, so there is no overhead of parsing annotations.