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/Providers/Provider.php +65 -0 2.0.1 → 2.1.1 View file →
@@ -1,0 +1,65 @@
1 +<?php
2 +
3 +declare(strict_types=1);
4 +
5 +namespace Metricool\Providers;
6 +
7 +use Metricool\Bootstrap\App;
8 +use Metricool\Support\Utility\StringUtility;
9 +use Metricool\Interfaces\ProviderInterface;
10 +
11 +/**
12 + * Providers are classes that provide functionality to the container. Child
13 + * classes should never use the container instance themselves to prevent
14 + * recursion in the container registry. Therefor child Providers should
15 + * always return the provided functionality directly in the
16 + * provide{Function} method instead of setting it in the
17 + * container {@see App}
18 + */
19 +class Provider implements ProviderInterface
20 +{
21 + /**
22 + * Register the provided services. Will be used to find and call the
23 + * provide{Service} methods. You can use lowercase for the service name.
24 + * @var string[]
25 + */
26 + protected array $provides = [];
27 +
28 + /**
29 + * Register the provided singleton services. The key is the name of the
30 + * service and is used to find and call the provide{Service}Singleton
31 + * method. The value is the class string that will be used to register
32 + * and retrieve the singleton in the container.
33 + * @var array<string, class-string>
34 + */
35 + protected array $singletons = [];
36 +
37 + /**
38 + * Method will be called by the ProviderManager to serve the provided
39 + * services.
40 + */
41 + final public function provide(): void
42 + {
43 + foreach ($this->provides as $provide) {
44 + $method = 'provide' . StringUtility::snakeToPascalCase($provide);
45 + if (method_exists($this, $method) === false) {
46 + continue;
47 + }
48 +
49 + App::getInstance()->set($provide, static function() use ($method) {
50 + return static::$method();
51 + });
52 + }
53 +
54 + foreach ($this->singletons as $key => $classString) {
55 + $method = 'provide' . StringUtility::snakeToPascalCase($key) . 'Singleton';
56 + if (method_exists($this, $method) === false) {
57 + continue;
58 + }
59 +
60 + App::getInstance()->set($classString, static function() use ($method) {
61 + return static::$method();
62 + });
63 + }
64 + }
65 +}