|
| 1 | +Metadata |
| 2 | +===== |
| 3 | + |
| 4 | +The model of this library is as closed as possible. |
| 5 | +Main reason is because consumers of the library should rely on cache. |
| 6 | +A mutable and flexible interface of the model would most likely break the caching. |
| 7 | +However after some time the users to this library started requesting for a more flexible format. |
| 8 | +This is why metadata was introduced. |
| 9 | + |
| 10 | +Create your first metadata |
| 11 | +-------------------------- |
| 12 | + |
| 13 | +First step is to create your own metadata implementation. |
| 14 | + |
| 15 | +.. code:: php |
| 16 | + final class Hook implements \phpDocumentor\Reflection\Metadata\Metadata |
| 17 | + { |
| 18 | + private string $hook; |
| 19 | +
|
| 20 | + public function __construct(string $hook) |
| 21 | + { |
| 22 | + $this->hook = $hook; |
| 23 | + } |
| 24 | +
|
| 25 | + public function key(): string |
| 26 | + { |
| 27 | + return "project-metadata"; |
| 28 | + } |
| 29 | +
|
| 30 | + public function hook(): string |
| 31 | + { |
| 32 | + return $this->hook; |
| 33 | + } |
| 34 | + } |
| 35 | +
|
| 36 | +.. note:: |
| 37 | + We do highly recommend to keep your metadata objects small. |
| 38 | + When reflecting a large project the number of objects will grow fast. |
| 39 | + |
| 40 | +Now we have an class that can be used it is time to create a :php:class:`\phpDocumentor\Reflection\Php\ProjectFactoryStrategy`. |
| 41 | +Strategies are used to reflect nodes in the AST of `phpparser`_. |
| 42 | + |
| 43 | +In the example below we are adding the Hook metadata to any functions containing a function call. |
| 44 | + |
| 45 | +.. code:: php |
| 46 | +
|
| 47 | + use \phpDocumentor\Reflection\Php\Function; |
| 48 | +
|
| 49 | + final class HookStrategy implements \phpDocumentor\Reflection\Php\ProjectFactoryStrategy |
| 50 | + { |
| 51 | + public function matches(ContextStack $context, object $object): bool |
| 52 | + { |
| 53 | + return $this->context->peek() instanceof Function_ && |
| 54 | + $object instanceof \PhpParser\Node\Expr\FuncCall && |
| 55 | + ((string)$object->name) === 'hook' |
| 56 | + } |
| 57 | +
|
| 58 | + public function create(ContextStack $context, object $object, StrategyContainer $strategies): void |
| 59 | + { |
| 60 | + $method = $context->peek(); |
| 61 | + $method->addMetadata(new Hook($object->args[0]->value)); |
| 62 | + } |
| 63 | + } |
| 64 | +
|
| 65 | +.. note:: |
| 66 | + To speed up the reflection of your project the default factory instance has a Noop strategy. This strategy will |
| 67 | + ignore all statements that are not handled by any strategy. Keep this in mind when implementing your own strategies |
| 68 | + especially the statements you are looking for are nested in other statements like a ``while`` loop. |
| 69 | + |
| 70 | +Finally add your new strategy to the project factory. |
| 71 | + |
| 72 | +.. code:: php |
| 73 | +
|
| 74 | + $factory = \phpDocumentor\Reflection\Php\ProjectFactory::createInstance(); |
| 75 | + $factory->addStrategy(new HookStrategy()); |
| 76 | +
|
| 77 | +.. _phpparser: https://github.com/nikic/PHP-Parser/ |
0 commit comments