Download the PHP package fom/module-annotations without Composer
On this page you can find all versions of the php package fom/module-annotations. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package module-annotations
Annotations for Magento 2
During the development of extensions, we often encounter a situation where some plugin or observer should be executed only on certain Magento versions and Magento editions. To solve this problem, we usually inject \Magento\Framework\App\ProductMetadataInterface and then use additional checks for versions and editions.
Annotations for Magento 2 will help solve this problem and provide flexible configuration for conditions combination.
Requirements
- PHP >= 7.1
- Magento Any Edition >= 2.3.0
How to install
Install via composer (recommended)
Run the following command in Magento 2 root folder:
Developer Guide
In Magento 2, all plugins and observers using \Magento\Framework\App\ProductMetadataInterface to check the current version and edition still continue to be instantiated, executed, and use memory and processor. Annotations for Magento 2 module uses an optimized approach, plugins and observers that should not be executed for the current version and edition will be disabled at the stage of building the configuration of plugins and observers. This means that checking whether the plugin or the observer should be executed will be performed ONLY once, at the stage of cache building, which will slightly reduce stack traces, and this in turn should have a positive impact on performance.
Attributes (for PHP >= 8.0) or Doctrine Annotations (for PHP >= 7.1) are used to configure plugins and observers.
Annotations for Magento 2 provides two attribute classes:
Fom\Annotations\Attribute\PlatformFom\Annotations\Attribute\Operator
Fom\Annotations\Attribute\Platform
The attribute is used to describe the edition, version, and comparison operator for the Magento. This attribute is repeatable, and can be added to the class several times.
Parameters:
-
edition-string, contains the Magento edition. Acceptable values:'any'- corresponds to the constantPlatform::EDITION_ANY'Community'- corresponds to the constantPlatform::EDITION_COMMUNITY'Commerce'- corresponds to the constantPlatform::EDITION_COMMERCE'B2B'- corresponds to the constantPlatform::EDITION_B2B
If an empty value is passed, the check will be performed as for the value
Platform::EDITION_ANY. -
version-string, any correct version that will be passed to the version_compare() function. -
comparison-string, any valid comparison operator accepted by the version_compare() function:'<'- corresponds to the constantPlatform::COMPARISON_LESS_THAN'<='- corresponds to the constantPlatform::COMPARISON_LESS_THAN_OR_EQUAL'>'- corresponds to the constantPlatform::COMPARISON_GREATER_THAN'>='- corresponds to the constantPlatform::COMPARISON_GREATER_THAN_OR_EQUAL'='- corresponds to the constantPlatform::COMPARISON_EQUAL'!='- corresponds to the constantPlatform::COMPARISON_NOT_EQUAL
If the value was not passed, the
Platform::COMPARISON_GREATER_THAN_OR_EQUALwill be used by default.
Fom\Annotations\Attribute\Operator
The attribute is used if the Platform list. This attribute is optional and can be applied to a class only 1 time.
Parameters:
-
operator-string, used to sum the result as logicalANDand logicalOR.'and'- corresponds to the constantOperator::OPERATOR_AND'or'- corresponds to the constantOperator::OPERATOR_OR
By default,
Operator::OPERATOR_ANDis used.
Examples
In the examples we will demonstrate observers, but the exact same declaration is used for plugins.
Observer with PHP8 Attributes
Let's disable observer for Any Magento Edition with versions lower than 2.4.4.
We can also import our attributes to use a short class name.
Observer with Doctrine Annotations (PHP < 8.0).
Let's do the same, but with a docblock section to allow use for older PHP versions.
We can still import our attributes to use a short class name.
Observer with a combined declaration, PHP 8 Attribute and Doctrine annotations, allowing this module to be used with different PHP versions.
We have to use the one-line PHP8 Attribute notation. In this case, our PHP8 Attribute will be interpreted as a comment for older PHP versions, since it starts with #.
Example without using the Operator attribute
Since Operator::OPERATOR_AND by default, we can omit it in the following cases:
We use only one platform condition
We use several platform conditions, but all their conditions must be satisfied at the same time
Example of using Operator::OPERATOR_AND
In rare cases, we may need to use specific conditions when a plugin or an observer must work on different versions and editions at the same time, in this case we can describe several necessary platform conditions and summarize them using Operator::OPERATOR_AND. In this case, if at least one of the conditions is met, our observer or plugin will be executed.
In the example below, the observer will be executed on Magento Community Edition = 2.4.3 or on Magento Commerce Edition = 2.4.2, on any other versions or editions, this observer will be disabled.
Additional Tools
- PHP Annotations - Extends PhpStorm to support annotations in DocBlocks.
Copyright
Copyright © FriendsOfMagento