Installation¶
To install the Metadata component, you will need to use Composer. Run the following command in your terminal:
composer require runopencode/metadata
This will download and install the Metadata component along with its dependencies.
In your project, you will need to initialize the Reader which you may use to read class, property and method metadata:
1 <?php
2
3 declare(strict_types=1);
4
5 use RunOpenCode\Component\Metadata\Reader;
6 use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
7
8 // Optionally, provide a cache adapter to the Reader
9 // that implements \Psr\Cache\CacheItemPoolInterface
10 $cache = new PhpFilesAdapter(directory: '/path/to/cache/directory');
11 $reader = new Reader($cache);
However, concrete implementation of reader should not be used as dependency in
your classes. Instead, use ReaderInterface.
1 <?php
2
3 declare(strict_types=1);
4
5 namespace App;
6
7 use RunOpenCode\Component\Metadata\Contract\ReaderInterface;
8
9 final readonly class Foo {
10
11 public function __construct(private ReaderInterface $reader)
12 {
13 }
14 }
By default, metadata is always cached using in-memory cache. It is highly recommended to provide a persistent cache adapter to the Reader in order to improve performance. Choice of cache adapter is up to you, however, it is advisable to use the ones which are “local” to your application, such as file system cache or APCu cache.
Cache will not check for changes in attributes or classes. In that matter, use only in-memory cache during development, and persistent cache only in production environments.