1 Friendica strategy Hooks
2 ===========================================
8 This type of hook is based on the [Strategy Design Pattern](https://refactoring.guru/design-patterns/strategy).
10 A strategy class defines a possible implementation of a given interface based on a unique name.
11 Every name is possible as long as it's unique and not `null`.
12 Using an empty name (`''`) is possible as well and should be used as the "default" implementation.
13 To register a strategy, use the [`ICanRegisterInstance`](../src/Core/Hooks/Capability/ICanRegisterInstances.php) interface.
15 After registration, a caller can automatically create this instance with the [`ICanCreateInstances`](../src/Core/Hooks/Capability/ICanCreateInstances.php) interface and the chosen name.
17 This is useful in case there are different, possible implementations for the same purpose, like for logging, locking, caching, ...
19 Normally, a config entry is used to choose the right implementation at runtime.
20 And if no config entry is set, the "default" implementation should be used.
25 interface ExampleInterface
27 public function testMethod();
30 public class ConcreteClassA implements ExampleInterface
32 public function testMethod()
34 echo "concrete class A";
38 public class ConcreteClassB implements ExampleInterface
40 public function testMethod()
42 echo "concrete class B";
46 /** @var \Friendica\Core\Hooks\Capability\ICanRegisterStrategies $instanceRegister */
47 $instanceRegister->registerStrategy(ExampleInterface::class, ConcreteClassA::class, 'A');
48 $instanceRegister->registerStrategy(ExampleInterface::class, ConcreteClassB::class, 'B');
50 /** @var \Friendica\Core\Hooks\Capability\ICanCreateInstances $instanceManager */
51 /** @var ConcreteClassA $concreteClass */
52 $concreteClass = $instanceManager->create(ExampleInterface::class, 'A');
54 $concreteClass->testMethod();
56 // "concrete class A";
61 To avoid registering all strategies manually inside the code, Friendica introduced the [`hooks.config.php`](../static/hooks.config.php) file.
63 There, you can register all kind of strategies in one file.
65 ### [`HookType::STRATEGY`](../src/Core/Hooks/Capability/HookType.php)
67 For each given interface, a list of key-value pairs can be set, where the key is the concrete implementation class and the value is an array of unique names.
72 use Friendica\Core\Hooks\Capability\BehavioralHookType as H;
76 ExampleInterface::class => [
77 ConcreteClassA::class => ['A'],
78 ConcreteClassB::class => ['B'],
86 The hook logic is useful for decoupling the Friendica core logic, but its primary goal is to modularize Friendica in creating addons.
88 Therefor you can either use the interfaces directly as shown above, or you can place your own `hooks.config.php` file inside a `static` directory directly under your addon core directory.
89 Friendica will automatically search these config files for each **activated** addon and register the given hooks.