ObjectFactory.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 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  * @see Factory::getInstance()
38  */
39  public static function getInstance($name, $dynamicConfiguration=array()) {
40  self::checkConfig();
41  return self::$_factory->getInstance($name, $dynamicConfiguration);
42  }
43 
44  /**
45  * @see Factory::getClassInstance()
46  */
47  public static function getClassInstance($class, $dynamicConfiguration=array()) {
48  self::checkConfig();
49  return self::$_factory->getClassInstance($class, $dynamicConfiguration);
50  }
51 
52  /**
53  * @see Factory::registerInstance()
54  */
55  public static function registerInstance($name, $instance) {
56  self::checkConfig();
57  self::$_factory->registerInstance($name, $instance);
58  }
59 
60  /**
61  * @see Factory::addInterfaces()
62  */
63  public function addInterfaces($interfaces) {
64  self::checkConfig();
65  self::$_factory->addInterfaces($interfaces);
66  }
67 
68  /**
69  * @see Factory::clear()
70  */
71  public static function clear() {
72  self::checkConfig();
73  self::$_factory->clear();
74  }
75 
76  /**
77  * Check if the configuration is valid.
78  */
79  private static function checkConfig() {
80  if (self::$_factory == null) {
81  throw new ConfigurationException('No Factory instance provided. Do this by calling the configure() method.');
82  }
83  }
84 }
85 ?>
static getClassInstance($class, $dynamicConfiguration=array())
static getInstance($name, $dynamicConfiguration=array())
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...
static registerInstance($name, $instance)
Interface for Factory implementations.
Definition: Factory.php:19
ConfigurationException signals an exception in the configuration.
static configure(Factory $factory)
Configure the factory.
Core classes.
Definition: namespaces.php:11