| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin: Service provider abstract |
| 4 |
* |
| 5 |
* @package SimplePay |
| 6 |
* @subpackage Core |
| 7 |
* @copyright Copyright (c) 2022, Sandhills Development, LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 4.4.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SimplePay\Core; |
| 13 |
|
| 14 |
use SimplePay\Vendor\League\Container\ServiceProvider\AbstractServiceProvider; |
| 15 |
|
| 16 |
/** |
| 17 |
* AbstractPluginServiceProvider class. |
| 18 |
* |
| 19 |
* @since 4.4.0 |
| 20 |
*/ |
| 21 |
abstract class AbstractPluginServiceProvider extends AbstractServiceProvider { |
| 22 |
|
| 23 |
/** |
| 24 |
* Works around Container 2.x looking in $this->provides property. |
| 25 |
* |
| 26 |
* Once we can use Container 4.x we can simply override ::provides(). |
| 27 |
* |
| 28 |
* @todo Remove when using Container 4.x |
| 29 |
* @since 4.4.0 |
| 30 |
* |
| 31 |
* @param string $property Property name to retrieve. |
| 32 |
* @return mixed |
| 33 |
*/ |
| 34 |
public function __get( $property ) { |
| 35 |
switch ( $property ) { |
| 36 |
// Retrieves a merged list of services and subscribers. |
| 37 |
case 'provides': |
| 38 |
return $this->get_provides(); |
| 39 |
default: |
| 40 |
return $this->$property; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Returns a list of services and subscribers that are provided. |
| 46 |
* |
| 47 |
* @since 4.4.0 |
| 48 |
* |
| 49 |
* @return string[] |
| 50 |
*/ |
| 51 |
private function get_provides() { |
| 52 |
return array_merge( |
| 53 |
$this->get_services(), |
| 54 |
$this->get_subscribers() |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* {@inheritdoc} |
| 60 |
* |
| 61 |
* @return bool|string[] |
| 62 |
*/ |
| 63 |
public function provides( $id = null ) { |
| 64 |
$provides = $this->get_provides(); |
| 65 |
|
| 66 |
// @todo Remove when using Container 4.x. |
| 67 |
if ( null === $id ) { |
| 68 |
return $provides; |
| 69 |
} |
| 70 |
|
| 71 |
return in_array( $id, $provides, true ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Returns a list of services the service provider provides. |
| 76 |
* |
| 77 |
* @since 4.4.0 |
| 78 |
* |
| 79 |
* @return string[] |
| 80 |
*/ |
| 81 |
abstract public function get_services(); |
| 82 |
|
| 83 |
/** |
| 84 |
* Returns a list of subscribers the service provider provides. |
| 85 |
* |
| 86 |
* A subscriber is a subset of a standard service that is automatically resolved |
| 87 |
* during each request. These are used for the WordPress plugin API. |
| 88 |
* |
| 89 |
* @since 4.4.0 |
| 90 |
* |
| 91 |
* @return string[] |
| 92 |
*/ |
| 93 |
abstract public function get_subscribers(); |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns a list of child service providers. |
| 97 |
* |
| 98 |
* @since 4.4.3 |
| 99 |
* |
| 100 |
* @return array<\SimplePay\Core\AbstractPluginServiceProvider> |
| 101 |
*/ |
| 102 |
public function get_service_providers() { |
| 103 |
return array(); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* {@inheritdoc} |
| 108 |
*/ |
| 109 |
abstract public function register(); |
| 110 |
|
| 111 |
} |
| 112 |
|