Skip to content

Commit 17e4cdb

Browse files
committed
Initial version
0 parents  commit 17e4cdb

22 files changed

+866
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
composer.lock
3+
vendor/
4+
.phpunit.result.cache

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 commonphp
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "comphp/drivers",
3+
"type": "library",
4+
"description": "Allows for drivers to be dynamically managed",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "timothy.mcclatchey",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"CommonPHP\\Drivers\\": "src/"
15+
}
16+
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"CommonPHP\\Tests\\Drivers\\": "tests/"
20+
}
21+
},
22+
"require": {
23+
"php": "^8.3",
24+
"comphp/service-management": "^0.2"
25+
},
26+
"require-dev": {
27+
"phpunit/phpunit": "^10.5.9"
28+
}
29+
}

examples/general-usage.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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, []);

readme.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# CommonPHP Driver Management Library
2+
3+
The CommonPHP Driver Management Library provides a robust framework for managing driver implementations in PHP applications. By leveraging attributes and contracts, it offers a flexible system to register, enable, and utilize various drivers seamlessly within your projects.
4+
5+
## Features
6+
7+
- **Flexible Driver Configuration**: Configure drivers using PHP 8 Attributes or by specifying driver contracts.
8+
- **Dynamic Driver Support Checking**: Determine if a class is supported as a driver based on its configuration.
9+
- **Driver Instantiation**: On-demand driver instantiation managed by the CommonPHP Service Management framework, ensuring dependency injection and singleton management.
10+
- **Comprehensive Exception Handling**: Detailed exception handling for configuration errors, unsupported drivers, and instantiation issues.
11+
12+
## Installation
13+
14+
Use Composer to integrate the Driver Management Library into your project:
15+
16+
```bash
17+
composer require comphp/drivers
18+
```
19+
20+
## Configuration
21+
22+
### Define Driver Contracts or Attributes
23+
24+
Implement `DriverContract` for interface/abstract class-based drivers or use `DriverAttributeContract` to mark drivers with attributes.
25+
26+
### Register and Configure DriverManager
27+
28+
```php
29+
use CommonPHP\Drivers\DriverManager;
30+
use CommonPHP\Drivers\ServiceProviders\DriverManagerServiceProvider;
31+
use CommonPHP\ServiceManagement\ServiceManager;
32+
33+
// Initialize ServiceManager and register DriverManagerServiceProvider
34+
$serviceManager = new ServiceManager();
35+
$serviceManager->providers->registerProvider(DriverManagerServiceProvider::class);
36+
37+
// Retrieve an instance of DriverManager
38+
$driverManager = $serviceManager->get(DriverManager::class);
39+
40+
// Configure DriverManager with an attribute or contract class
41+
$driverManager->configure(
42+
attributeClass: YourDriverAttribute::class,
43+
contractClass: YourDriverContract::class
44+
);
45+
```
46+
47+
## Usage
48+
49+
### Enable a Driver
50+
51+
Once a driver meets the configured criteria (attribute or contract), enable it using:
52+
53+
```php
54+
$driverManager->enable(YourDriverClass::class);
55+
```
56+
57+
### Utilize an Enabled Driver
58+
59+
Retrieve and use an enabled driver:
60+
61+
```php
62+
$driver = $driverManager->get(YourDriverClass::class);
63+
$driver->performAction();
64+
```
65+
66+
## Contributing
67+
68+
Contributions to the CommonPHP Driver Management Library are welcome. Please follow the contribution guidelines provided in the repository for submitting pull requests or issues.
69+
70+
## License
71+
72+
This library is released under the MIT license. See [LICENSE](LICENSE) for details.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/**
4+
* DriverAttributeContract.php
5+
*
6+
* Defines a contract for driver attributes within the CommonPHP Driver Management system. This interface
7+
* is intentionally left empty to serve as a marker for PHP 8 Attributes that can be used to identify
8+
* driver classes. By implementing this contract, attributes signal their capability to participate in
9+
* the driver management process, allowing the DriverManager to recognize and manage them accordingly.
10+
*
11+
* Although empty, the presence of this contract is crucial for the architecture of the driver management
12+
* system, providing a standardized way to designate attributes as part of the driver identification
13+
* mechanism.
14+
*
15+
* @package CommonPHP\Drivers\Contracts
16+
* @author Timothy McClatchey <[email protected]>
17+
* @copyright 2024 CommonPHP.org
18+
* @license http://opensource.org/licenses/MIT MIT License
19+
*/
20+
21+
namespace CommonPHP\Drivers\Contracts;
22+
23+
interface DriverAttributeContract
24+
{
25+
// This interface is intentionally left empty as a marker for driver attributes.
26+
}

src/Contracts/DriverContract.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* DriverContract.php
4+
*
5+
* Defines a contract for drivers within the CommonPHP Driver Management system. This interface
6+
* is intentionally left empty to serve as a marker for classes that can be considered drivers.
7+
* Implementing this contract allows the DriverManager to recognize and manage these classes
8+
* as part of the driver system.
9+
*
10+
* The absence of methods within this contract underscores its role as a flexible marker,
11+
* enabling a wide variety of classes to qualify as drivers without imposing specific
12+
* implementation requirements. It is an essential component of the driver management
13+
* architecture, facilitating the dynamic discovery and utilization of drivers.
14+
*
15+
* @package CommonPHP\Drivers\Contracts
16+
* @author Timothy McClatchey <[email protected]>
17+
* @copyright 2024 CommonPHP.org
18+
* @license http://opensource.org/licenses/MIT MIT License
19+
*/
20+
21+
namespace CommonPHP\Drivers\Contracts;
22+
23+
interface DriverContract
24+
{
25+
// This interface is intentionally left empty as a marker
26+
}

0 commit comments

Comments
 (0)