Add abstract provider for tagging interfaces
This commit is contained in:
parent
f03821904b
commit
2c689ee858
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders;
|
||||
|
||||
use Automattic\WooCommerce\Internal\DependencyManagement\AbstractServiceProvider;
|
||||
use Automattic\WooCommerce\Vendor\League\Container\Definition\DefinitionInterface;
|
||||
|
||||
/**
|
||||
* Class AbstractInterfaceServiceProvider
|
||||
*
|
||||
* @since x.x.x
|
||||
*/
|
||||
abstract class AbstractInterfaceServiceProvider extends AbstractServiceProvider {
|
||||
|
||||
/**
|
||||
* Determine whether this service provides the given alias.
|
||||
*
|
||||
* @param string $alias The alias to check.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function provides( string $alias ): bool {
|
||||
$provides = parent::provides( $alias );
|
||||
if ( $provides ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static $implements = array();
|
||||
if ( empty( $implements ) ) {
|
||||
foreach ( $this->provides as $class ) {
|
||||
$implements = array_merge( $implements, class_implements( $class ) );
|
||||
}
|
||||
|
||||
$implements = array_unique( $implements );
|
||||
}
|
||||
|
||||
return array_key_exists( $alias, $implements );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a class in the container and add tags for all the interfaces it implements.
|
||||
*
|
||||
* This also updates the `$this->provides` property with the interfaces provided by the class, ane ensures
|
||||
* that the property doesn't contain duplicates.
|
||||
*
|
||||
* @since x.x.x
|
||||
*
|
||||
* @param string $id Entry ID (typically a class or interface name).
|
||||
* @param mixed|null $concrete Concrete entity to register under that ID, null for automatic creation.
|
||||
* @param bool|null $shared Whether to register the class as shared (`get` always returns the same instance)
|
||||
* or not.
|
||||
*
|
||||
* @return DefinitionInterface
|
||||
*/
|
||||
protected function add_with_implements_tags( string $id, $concrete = null, bool $shared = null ): DefinitionInterface {
|
||||
$definition = $this->add( $id, $concrete, $shared );
|
||||
foreach ( class_implements( $id ) as $interface ) {
|
||||
$definition->addTag( $interface );
|
||||
}
|
||||
|
||||
return $definition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a shared class in the container and add tags for all the interfaces it implements.
|
||||
*
|
||||
* @since x.x.x
|
||||
*
|
||||
* @param string $id Entry ID (typically a class or interface name).
|
||||
* @param mixed|null $concrete Concrete entity to register under that ID, null for automatic creation.
|
||||
*
|
||||
* @return DefinitionInterface
|
||||
*/
|
||||
protected function share_with_implements_tags( string $id, $concrete = null ): DefinitionInterface {
|
||||
return $this->add_with_implements_tags( $id, $concrete, true );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue