ObjectFactory.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\core;
12 
14 
15 /**
16  * ObjectFactory implements the _service locator_ pattern by wrapping a
17  * Factory instance and providing static access to it.
18  *
19  * It delegates the work of actually instantiating services to the configured
20  * Factory instance.
21  *
22  * @author ingo herwig <ingo@wemove.com>
23  */
25 
26  private static $factory = null;
27 
28  /**
29  * Configure the factory.
30  * @param $factory Factory instance that actually does the instantiation.
31  */
32  public static function configure(Factory $factory) {
33  self::$factory = $factory;
34  }
35 
36  /**
37  * Check if the factory is configured.
38  * @return Boolean
39  */
40  public static function isConfigured() {
41  return self::$factory != null;
42  }
43 
44  /**
45  * @see Factory::getInstance()
46  */
47  public static function getInstance($name, $dynamicConfiguration=[]) {
48  self::checkConfig();
49  return self::$factory->getInstance($name, $dynamicConfiguration);
50  }
51 
52  /**
53  * @see Factory::getNewInstance()
54  */
55  public static function getNewInstance($name, $dynamicConfiguration=[]) {
56  self::checkConfig();
57  return self::$factory->getNewInstance($name, $dynamicConfiguration);
58  }
59 
60  /**
61  * @see Factory::getInstanceOf()
62  */
63  public static function getInstanceOf($class, $dynamicConfiguration=[]) {
64  self::checkConfig();
65  return self::$factory->getInstanceOf($class, $dynamicConfiguration);
66  }
67 
68  /**
69  * @see Factory::registerInstance()
70  */
71  public static function registerInstance($name, $instance) {
72  self::checkConfig();
73  self::$factory->registerInstance($name, $instance);
74  }
75 
76  /**
77  * @see Factory::addInterfaces()
78  */
79  public function addInterfaces($interfaces) {
80  self::checkConfig();
81  self::$factory->addInterfaces($interfaces);
82  }
83 
84  /**
85  * @see Factory::clear()
86  */
87  public static function clear() {
88  if (self::$factory != null) {
89  self::$factory->clear();
90  }
91  self::$factory = null;
92  }
93 
94  /**
95  * Check if the configuration is valid.
96  */
97  private static function checkConfig() {
98  if (self::$factory == null) {
99  throw new ConfigurationException('No Factory instance provided. Do this by calling the configure() method.');
100  }
101  }
102 }
103 ?>
static getInstanceOf($class, $dynamicConfiguration=[])
Interface for Factory implementations.
Definition: Factory.php:19
ConfigurationException signals an exception in the configuration.
static isConfigured()
Check if the factory is configured.
Core classes.
Definition: namespaces.php:11
static getNewInstance($name, $dynamicConfiguration=[])
static getInstance($name, $dynamicConfiguration=[])
static configure(Factory $factory)
Configure the factory.
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...
static registerInstance($name, $instance)