| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify\MCP; |
| 5 |
|
| 6 |
use Imagify\Abilities\BulkOptimize; |
| 7 |
use Imagify\Abilities\GenerateMissingNextgen; |
| 8 |
use Imagify\Abilities\GetAccount; |
| 9 |
use Imagify\Abilities\GetMediaStatus; |
| 10 |
use Imagify\Abilities\GetNextgenCoverage; |
| 11 |
use Imagify\Abilities\GetSettings; |
| 12 |
use Imagify\Abilities\GetStats; |
| 13 |
use Imagify\Abilities\OptimizeMedia; |
| 14 |
use Imagify\Abilities\RestoreMedia; |
| 15 |
use Imagify\Abilities\UpdateSettings; |
| 16 |
use Imagify\Bulk\Bulk; |
| 17 |
use Imagify\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider; |
| 18 |
use Imagify\Stats\OptimizedMediaWithoutNextGen; |
| 19 |
|
| 20 |
/** |
| 21 |
* Service provider for the MCP (Model Context Protocol) module. |
| 22 |
* |
| 23 |
* Wires `ConfigSubscriber` and `AbilitiesSubscriber` into the DI container. |
| 24 |
* For the foundation `AbilitiesSubscriber` is registered with no ability |
| 25 |
* arguments. Downstream sub-issues extend the wiring via `addArguments()` |
| 26 |
* once concrete ability classes are added (see Downstream Wiring Contract |
| 27 |
* in spec #1108). |
| 28 |
* |
| 29 |
* @since 2.3.0 |
| 30 |
*/ |
| 31 |
class ServiceProvider extends AbstractServiceProvider { |
| 32 |
|
| 33 |
/** |
| 34 |
* Services provided by this provider. |
| 35 |
* |
| 36 |
* @var array<int, string> |
| 37 |
*/ |
| 38 |
protected $provides = [ |
| 39 |
ConfigSubscriber::class, |
| 40 |
AbilitiesSubscriber::class, |
| 41 |
Bulk::class, |
| 42 |
BulkOptimize::class, |
| 43 |
GenerateMissingNextgen::class, |
| 44 |
GetAccount::class, |
| 45 |
GetMediaStatus::class, |
| 46 |
GetNextgenCoverage::class, |
| 47 |
GetSettings::class, |
| 48 |
GetStats::class, |
| 49 |
OptimizeMedia::class, |
| 50 |
RestoreMedia::class, |
| 51 |
UpdateSettings::class, |
| 52 |
]; |
| 53 |
|
| 54 |
/** |
| 55 |
* Subscribers provided by this provider. |
| 56 |
* |
| 57 |
* @var array<int, string> |
| 58 |
*/ |
| 59 |
public $subscribers = [ |
| 60 |
ConfigSubscriber::class, |
| 61 |
AbilitiesSubscriber::class, |
| 62 |
]; |
| 63 |
|
| 64 |
/** |
| 65 |
* Checks whether this provider provides a given service. |
| 66 |
* |
| 67 |
* @param string $id Service identifier. |
| 68 |
* @return bool |
| 69 |
*/ |
| 70 |
public function provides( string $id ): bool { |
| 71 |
return in_array( $id, $this->provides, true ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Registers the provided services into the container. |
| 76 |
* |
| 77 |
* Bulk is registered by passing the already-initialised singleton directly |
| 78 |
* (Plugin::init() calls Bulk::get_instance()->init() before any provider |
| 79 |
* runs), so no factory closure is needed. This avoids the ~12 KB opcode |
| 80 |
* allocation that a closure would incur in CLI environments with a tight |
| 81 |
* memory limit (e.g. wp-env at 128 MB). |
| 82 |
* |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public function register(): void { |
| 86 |
$this->getContainer()->addShared( Bulk::class, Bulk::get_instance() ); |
| 87 |
$this->getContainer()->addShared( BulkOptimize::class ) |
| 88 |
->addArgument( Bulk::get_instance() ); |
| 89 |
$this->getContainer()->addShared( GenerateMissingNextgen::class ) |
| 90 |
->addArgument( Bulk::class ) |
| 91 |
->addArgument( OptimizedMediaWithoutNextGen::class ); |
| 92 |
$this->getContainer()->addShared( ConfigSubscriber::class ); |
| 93 |
$this->getContainer()->addShared( GetAccount::class ); |
| 94 |
$this->getContainer()->addShared( GetMediaStatus::class ); |
| 95 |
$this->getContainer()->addShared( GetNextgenCoverage::class ) |
| 96 |
->addArgument( OptimizedMediaWithoutNextGen::class ); |
| 97 |
$this->getContainer()->addShared( GetSettings::class ); |
| 98 |
$this->getContainer()->addShared( GetStats::class ); |
| 99 |
$this->getContainer()->addShared( OptimizeMedia::class ); |
| 100 |
$this->getContainer()->addShared( RestoreMedia::class ); |
| 101 |
$this->getContainer()->addShared( UpdateSettings::class ); |
| 102 |
$this->getContainer()->addShared( AbilitiesSubscriber::class ) |
| 103 |
->addArguments( [ BulkOptimize::class, GenerateMissingNextgen::class, GetAccount::class, GetMediaStatus::class, GetNextgenCoverage::class, GetSettings::class, GetStats::class, OptimizeMedia::class, RestoreMedia::class, UpdateSettings::class ] ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns the list of subscriber class names. |
| 108 |
* |
| 109 |
* @return array<int, string> |
| 110 |
*/ |
| 111 |
public function get_subscribers(): array { |
| 112 |
return $this->subscribers; |
| 113 |
} |
| 114 |
} |
| 115 |
|