src/includes/Caching/Actions/InitializeCachingServiceTrait.php
<?php
namespace DeepWebSolutions\Framework\Utilities\Caching\Actions;
use DeepWebSolutions\Framework\Foundations\Actions\Initializable\InitializableExtensionTrait;
use DeepWebSolutions\Framework\Foundations\Actions\Initializable\InitializationFailureException;
use DeepWebSolutions\Framework\Foundations\DependencyInjection\ContainerAwareInterface;
use DeepWebSolutions\Framework\Foundations\Hierarchy\ChildInterface;
use DeepWebSolutions\Framework\Foundations\PluginAwareInterface;
use DeepWebSolutions\Framework\Utilities\Caching\CachingService;
use DeepWebSolutions\Framework\Utilities\Caching\CachingServiceAwareInterface;
use DeepWebSolutions\Framework\Utilities\Caching\CachingServiceAwareTrait;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
\defined( 'ABSPATH' ) || exit;
/**
* Trait for setting the caching service on the using instance.
*
* @since 1.0.0
* @version 1.0.0
* @author Antonius Hegyes <a.hegyes@deep-web-solutions.com>
* @package DeepWebSolutions\WP-Framework\Utilities\Caching\Actions
*/
trait InitializeCachingServiceTrait {
// region TRAITS
use CachingServiceAwareTrait;
use InitializableExtensionTrait;
// endregion
// region METHODS
/**
* Try to automagically set a caching service on the instance.
*
* @since 1.0.0
* @version 1.0.0
*
* @throws NotFoundExceptionInterface Thrown if the container can't find an entry.
* @throws ContainerExceptionInterface Thrown if the container encounters some other error.
*
* @return InitializationFailureException|null
*/
public function initialize_caching_service(): ?InitializationFailureException {
if ( $this instanceof ChildInterface && $this->get_parent() instanceof CachingServiceAwareInterface ) {
/* @noinspection PhpUndefinedMethodInspection */
$service = $this->get_parent()->get_caching_service();
} elseif ( $this instanceof ContainerAwareInterface ) {
$service = $this->get_container()->get( CachingService::class );
} elseif ( $this instanceof PluginAwareInterface && $this->get_plugin() instanceof ContainerAwareInterface ) {
/* @noinspection PhpUndefinedMethodInspection */
$service = $this->get_plugin()->get_container()->get( CachingService::class );
} else {
return new InitializationFailureException( 'Caching service initialization scenario not supported' );
}
$this->set_caching_service( $service );
return null;
}
// endregion
}