PHPackages                             fom/module-annotations - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. fom/module-annotations

ActiveMagento2-module[Utility &amp; Helpers](/categories/utility)

fom/module-annotations
======================

N/A

1.0.1(4y ago)111PHP

Since Apr 21Pushed 4y ago2 watchersCompare

[ Source](https://github.com/FriendsOfMagento/module-annotations)[ Packagist](https://packagist.org/packages/fom/module-annotations)[ RSS](/packages/fom-module-annotations/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (4)Used By (0)

[![M2 Coding Standard](https://github.com/FriendsOfMagento/module-annotations/actions/workflows/coding-standard.yml/badge.svg?branch=develop)](https://github.com/FriendsOfMagento/module-annotations/actions/workflows/coding-standard.yml)[![M2 Mess Detector](https://github.com/FriendsOfMagento/module-annotations/actions/workflows/mess-detector.yml/badge.svg?branch=develop)](https://github.com/FriendsOfMagento/module-annotations/actions/workflows/mess-detector.yml)[![M2 PHPStan](https://github.com/FriendsOfMagento/module-annotations/actions/workflows/phpstan.yml/badge.svg?branch=develop)](https://github.com/FriendsOfMagento/module-annotations/actions/workflows/phpstan.yml)

Annotations for Magento 2
=========================

[](#annotations-for-magento-2)

During the development of extensions, we often encounter a situation where some [plugin](https://devdocs.magento.com/guides/v2.4/extension-dev-guide/plugins.html) or [observer](https://devdocs.magento.com/guides/v2.4/extension-dev-guide/events-and-observers.html) 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
------------

[](#requirements)

- PHP &gt;= 7.1
- Magento Any Edition &gt;= 2.3.0

How to install
--------------

[](#how-to-install)

### Install via composer (recommended)

[](#install-via-composer-recommended)

Run the following command in Magento 2 root folder:

```
composer require fom/module-annotations
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy

```

Developer Guide
---------------

[](#developer-guide)

In Magento 2, all [plugins](https://devdocs.magento.com/guides/v2.4/extension-dev-guide/plugins.html) and [observers](https://devdocs.magento.com/guides/v2.4/extension-dev-guide/events-and-observers.html) 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](https://www.php.net/manual/en/language.attributes.php) (for PHP &gt;= 8.0) or [Doctrine Annotations](https://www.doctrine-project.org/projects/annotations.html) (for PHP &gt;= 7.1) are used to configure [plugins](https://devdocs.magento.com/guides/v2.4/extension-dev-guide/plugins.html) and [observers](https://devdocs.magento.com/guides/v2.4/extension-dev-guide/events-and-observers.html).

**Annotations for Magento 2** provides two attribute classes:

- [`Fom\Annotations\Attribute\Platform`](Attribute/Platform.php)
- [`Fom\Annotations\Attribute\Operator`](Attribute/Operator.php)

### [`Fom\Annotations\Attribute\Platform`](Attribute/Platform.php)

[](#fomannotationsattributeplatform)

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 constant [`Platform::EDITION_ANY`](Attribute/Platform.php#L21)
    - `'Community'` - corresponds to the constant [`Platform::EDITION_COMMUNITY`](Attribute/Platform.php#L22)
    - `'Commerce'` - corresponds to the constant [`Platform::EDITION_COMMERCE`](Attribute/Platform.php#L23)
    - `'B2B'` - corresponds to the constant [`Platform::EDITION_B2B`](Attribute/Platform.php#L24)

    If an empty value is passed, the check will be performed as for the value [`Platform::EDITION_ANY`](Attribute/Platform.php#L21).
- `version` - `string`, any correct version that will be passed to the [version\_compare()](https://www.php.net/manual/en/function.version-compare.php) function.
- `comparison` - `string`, any valid comparison operator accepted by the [version\_compare()](https://www.php.net/manual/en/function.version-compare.php) function:

    - `'='` - corresponds to the constant [`Platform::COMPARISON_GREATER_THAN_OR_EQUAL`](Attribute/Platform.php#L32)
    - `'='` - corresponds to the constant [`Platform::COMPARISON_EQUAL`](Attribute/Platform.php#L33)
    - `'!='` - corresponds to the constant [`Platform::COMPARISON_NOT_EQUAL`](Attribute/Platform.php#L34)

    If the value was not passed, the [`Platform::COMPARISON_GREATER_THAN_OR_EQUAL`](Attribute/Platform.php#L32) will be used by default.

### [`Fom\Annotations\Attribute\Operator`](Attribute/Operator.php)

[](#fomannotationsattributeoperator)

The attribute is used if the [`Platform`](Attribute/Platform.php) attribute has been added to the class several times, it is used to summarize the results obtained from [`Platform`](Attribute/Platform.php) list. This attribute is optional and can be applied to a class only 1 time.

Parameters:

- `operator` - `string`, used to sum the result as logical `AND` and logical `OR`.

    - `'and'` - corresponds to the constant [`Operator::OPERATOR_AND`](Attribute/Operator.php#L17)
    - `'or'` - corresponds to the constant [`Operator::OPERATOR_OR`](Attribute/Operator.php#L18)

    By default, [`Operator::OPERATOR_AND`](Attribute/Operator.php#L17) is used.

Examples
--------

[](#examples)

In the examples we will demonstrate observers, but the exact same declaration is used for plugins.

### Observer with PHP8 Attributes

[](#observer-with-php8-attributes)

Let's disable observer for Any Magento Edition with versions lower than 2.4.4.

```
