|
| 1 | +<?php |
| 2 | + |
| 3 | +// Required namespaces for Driver Management and Service Management components. |
| 4 | +use CommonPHP\Drivers\Contracts\DriverAttributeContract; |
| 5 | +use CommonPHP\Drivers\Contracts\DriverContract; |
| 6 | +use CommonPHP\Drivers\DriverManager; |
| 7 | +use CommonPHP\ServiceManagement\ServiceManager; |
| 8 | +use CommonPHP\Drivers\ServiceProviders\DriverManagerServiceProvider; |
| 9 | + |
| 10 | +// Include Composer's autoloader to handle class loading. |
| 11 | +require_once '../vendor/autoload.php'; |
| 12 | + |
| 13 | +// Define a custom attribute for drivers, implementing the DriverAttributeContract. |
| 14 | +#[Attribute(Attribute::TARGET_CLASS)] |
| 15 | +class GeneralExampleDriverAttribute implements DriverAttributeContract |
| 16 | +{ |
| 17 | + // This class is empty but serves as a marker for driver classes using attributes. |
| 18 | +} |
| 19 | + |
| 20 | +// Define a contract that drivers can implement, extending the base DriverContract. |
| 21 | +interface GeneralExampleDriverContract extends DriverContract |
| 22 | +{ |
| 23 | + // Interface body remains empty for this example, acting as a marker. |
| 24 | +} |
| 25 | + |
| 26 | +// Abstract class that implements the DriverContract, can be extended by concrete driver classes. |
| 27 | +abstract class AbstractGeneralExampleDriver implements DriverContract |
| 28 | +{ |
| 29 | + // Abstract class body is empty, serving as a base for specific drivers. |
| 30 | +} |
| 31 | + |
| 32 | +// Factory class configured to use the DriverManager with an attribute-based configuration. |
| 33 | +class AttributeDemoFactory |
| 34 | +{ |
| 35 | + public function __construct(DriverManager $driverManager) |
| 36 | + { |
| 37 | + // Configures the DriverManager to recognize drivers by the specified attribute. |
| 38 | + $driverManager->configure(GeneralExampleDriverAttribute::class, null); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// Factory class configured to use the DriverManager with a contract-based configuration. |
| 43 | +class ContractDemoFactory |
| 44 | +{ |
| 45 | + public function __construct(DriverManager $driverManager) |
| 46 | + { |
| 47 | + // Configures the DriverManager to recognize drivers implementing the specified contract. |
| 48 | + $driverManager->configure(null, GeneralExampleDriverContract::class); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// Factory class configured to use both attribute and contract-based configurations. |
| 53 | +class AttributeContractDemoFactory |
| 54 | +{ |
| 55 | + public function __construct(DriverManager $driverManager) |
| 56 | + { |
| 57 | + // Configures the DriverManager to recognize drivers both by attribute and by implementing a contract. |
| 58 | + $driverManager->configure(GeneralExampleDriverAttribute::class, AbstractGeneralExampleDriver::class); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Instantiate the ServiceManager and register the DriverManagerServiceProvider for dependency management. |
| 63 | +$serviceManager = new ServiceManager(); |
| 64 | +$serviceManager->providers->registerProvider(DriverManagerServiceProvider::class); |
| 65 | + |
| 66 | +// Instantiate each factory class, triggering their respective configuration logic in the DriverManager. |
| 67 | +// These demonstrate different ways to utilize the DriverManager for managing drivers. |
| 68 | +$serviceManager->di->instantiate(AttributeDemoFactory::class, []); |
| 69 | +$serviceManager->di->instantiate(ContractDemoFactory::class, []); |
| 70 | +$serviceManager->di->instantiate(AttributeContractDemoFactory::class, []); |
0 commit comments