PluginProbe
Metricool – Social media and site statistics / 2.1.1
Metricool – Social media and site statistics v2.1.1
2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
← All changes | app/Managers/AbstractManager.php +70 -0 2.0.0 → 2.1.1 View file →
@@ -1,0 +1,70 @@
1 +<?php
2 +
3 +declare(strict_types=1);
4 +
5 +namespace Metricool\Managers;
6 +
7 +use Metricool\Bootstrap\App;
8 +
9 +abstract class AbstractManager
10 +{
11 + /**
12 + * Overwrite this property to true when the entries that the child Manager
13 + * registers should be added to the container registry. For details see:
14 + * {@see App::make}
15 + */
16 + protected bool $useRegistry = false;
17 +
18 + /**
19 + * Overwrite this property to true when the dependencies of the entries that
20 + * the child Manager registers should be added to the container registry.
21 + * For details see: {@see App::make}
22 + */
23 + protected bool $useRegistryForDependencies = true;
24 +
25 + /**
26 + * Child class should check if the given class can be registered. For
27 + * example by checking if it implements an interface to know the logic in
28 + * the {@see registerClass} method can be executed.
29 + */
30 + abstract public function isRegistrable(object $class): bool;
31 +
32 + /**
33 + * Logic to register the given class. If this method can be executed is
34 + * checked by the {@see isRegistrable} method.
35 + */
36 + abstract public function registerClass(object $class): void;
37 +
38 + /**
39 + * Method called after all classes given to the manager are registered.
40 + */
41 + abstract public function afterRegister(): void;
42 +
43 + /**
44 + * Register the given class as long as the entries are registrable according
45 + * to the child managers. Class are autowired, but not registered via
46 + * {@see App::make}
47 + *
48 + * @throws \LogicException When a developer is doing it wrong.
49 + * @throws \ReflectionException When the controller cannot be loaded.
50 + */
51 + public function register(array $classes): void
52 + {
53 + foreach ($classes as $fullyClassifiedName) {
54 + if (is_string($fullyClassifiedName) === false) {
55 + $type = gettype($fullyClassifiedName);
56 + throw new \LogicException(esc_html("Class must be a fully qualified name. Given type: $type"));
57 + }
58 +
59 + $class = App::getInstance()->make($fullyClassifiedName, $this->useRegistry, $this->useRegistryForDependencies);
60 +
61 + if ($this->isRegistrable($class) === false) {
62 + throw new \LogicException('Class is not registrable: ' . esc_html($fullyClassifiedName));
63 + }
64 +
65 + $this->registerClass($class);
66 + }
67 +
68 + $this->afterRegister();
69 + }
70 +}