Migrate email editor Contaienr to WP Coding Standard
[MAILPOET-6240]
This commit is contained in:
@@ -1,29 +1,61 @@
|
|||||||
<?php declare(strict_types = 1);
|
<?php
|
||||||
|
/**
|
||||||
|
* This file is part of the MailPoet plugin.
|
||||||
|
*
|
||||||
|
* @package MailPoet\EmailEditor
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare( strict_types = 1 );
|
||||||
namespace MailPoet\EmailEditor;
|
namespace MailPoet\EmailEditor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Container is a simple dependency injection container.
|
||||||
|
*
|
||||||
|
* @package MailPoet\EmailEditor
|
||||||
|
*/
|
||||||
class Container {
|
class Container {
|
||||||
|
/**
|
||||||
|
* A list of registered services
|
||||||
|
*
|
||||||
|
* @var array $services
|
||||||
|
*/
|
||||||
protected array $services = array();
|
protected array $services = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list of created instances
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
protected array $instances = array();
|
protected array $instances = array();
|
||||||
|
|
||||||
public function set( string $name, callable $callable ): void {
|
/**
|
||||||
$this->services[ $name ] = $callable;
|
* The method for registering a new service
|
||||||
|
*
|
||||||
|
* @param string $name The name of the service.
|
||||||
|
* @param callable $callback The callable that will be used to create the service.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function set( string $name, callable $callback ): void {
|
||||||
|
$this->services[ $name ] = $callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Method for getting a registered service
|
||||||
|
*
|
||||||
* @template T
|
* @template T
|
||||||
* @param class-string<T> $name
|
* @param class-string<T> $name The name of the service.
|
||||||
* @return T
|
* @return T
|
||||||
|
* @throws \Exception If the service is not found.
|
||||||
*/
|
*/
|
||||||
public function get( string $name ) {
|
public function get( $name ) {
|
||||||
// Check if the service is already instantiated
|
// Check if the service is already instantiated.
|
||||||
if ( isset( $this->instances[ $name ] ) ) {
|
if ( isset( $this->instances[ $name ] ) ) {
|
||||||
return $this->instances[ $name ];
|
return $this->instances[ $name ];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the service is registered
|
// Check if the service is registered.
|
||||||
if ( ! isset( $this->services[ $name ] ) ) {
|
if ( ! isset( $this->services[ $name ] ) ) {
|
||||||
throw new \Exception( "Service not found: $name" );
|
throw new \Exception( esc_html( "Service not found: $name" ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->instances[ $name ] = $this->services[ $name ]( $this );
|
$this->instances[ $name ] = $this->services[ $name ]( $this );
|
||||||
|
Reference in New Issue
Block a user