| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package WPEmerge |
| 4 |
* @author Atanas Angelov <hi@atanas.dev> |
| 5 |
* @copyright 2017-2019 Atanas Angelov |
| 6 |
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
| 7 |
* @link https://wpemerge.com/ |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace WPEmerge\Application; |
| 11 |
|
| 12 |
use Pimple\Container; |
| 13 |
use WPEmerge\Exceptions\ClassNotFoundException; |
| 14 |
|
| 15 |
/** |
| 16 |
* Generic class instance factory. |
| 17 |
*/ |
| 18 |
class GenericFactory { |
| 19 |
/** |
| 20 |
* Container. |
| 21 |
* |
| 22 |
* @var Container |
| 23 |
*/ |
| 24 |
protected $container = null; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor. |
| 28 |
* |
| 29 |
* @codeCoverageIgnore |
| 30 |
* @param Container $container |
| 31 |
*/ |
| 32 |
public function __construct( Container $container ) { |
| 33 |
$this->container = $container; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Make a class instance. |
| 38 |
* |
| 39 |
* @throws ClassNotFoundException |
| 40 |
* @param string $class |
| 41 |
* @return object |
| 42 |
*/ |
| 43 |
public function make( $class ) { |
| 44 |
if ( isset( $this->container[ $class ] ) ) { |
| 45 |
return $this->container[ $class ]; |
| 46 |
} |
| 47 |
|
| 48 |
if ( ! class_exists( $class ) ) { |
| 49 |
throw new ClassNotFoundException( 'Class not found: ' . $class ); |
| 50 |
} |
| 51 |
|
| 52 |
return new $class(); |
| 53 |
} |
| 54 |
} |
| 55 |
|