1: <?php
2: namespace Define\Core;
3: use Define\Traits\SingletonTrait;
4: use Define\Core\IDefine;
5: use Define\Exceptions\FrameworkException;
6:
7: if(!defined('DIRECT_ACCESS')) {
8: die("Direct access is forbidden.");
9: }
10:
11: /**
12: * OBJECT CONTAINER
13: *
14: * Object registry class. Registry pattern.
15: * All the instances created will be stored in this container and will be called when need.
16: *
17: * @category Define
18: * @package Core
19: * @author Nitesh Apte <me@niteshapte.com>
20: * @copyright 2015 Nitesh Apte
21: * @version 1.0.0
22: * @since 1.0.0
23: * @license https://www.gnu.org/licenses/gpl.txt GNU General Public License v3
24: *
25: * This program is free software: you can redistribute it and/or modify
26: * it under the terms of the GNU General Public License as published by
27: * the Free Software Foundation, either version 3 of the License, or
28: * (at your option) any later version.
29: *
30: * This program is distributed in the hope that it will be useful,
31: * but WITHOUT ANY WARRANTY; without even the implied warranty of
32: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33: * GNU General Public License for more details.
34: *
35: * You should have received a copy of the GNU General Public License
36: * along with this program. If not, see <http://www.gnu.org/licenses/>.
37: */
38: final class ObjectContainer implements IDefine, \ArrayAccess {
39:
40: // Container should be singleton.
41: use SingletonTrait;
42:
43: private $registry = [];
44:
45: /**
46: * Check if an object has been already registered or not.
47: *
48: * @param String $key Name of the object.
49: * @return boolean
50: */
51: public function offsetExists($key) {
52: return isset($this->registry[$key]);
53: }
54:
55: /**
56: * Get an object
57: *
58: * @param String $key Name of the object.
59: * @return Object
60: * @throws FrameworkException if object has not been registered.
61: */
62: public function offsetGet($key) {
63: if(!isset($this->registry[$key])) {
64: throw new FrameworkException("Object for key {$key} has not been set.");
65: }
66: return $this->registry[$key];
67: }
68:
69: /**
70: * Set an object
71: *
72: * @param String $key Name of the object.
73: * @param Object $value Object to be registered.
74: * @return void
75: * @throws FrameworkException If object has been already registered.
76: */
77: public function offsetSet($key, $value) {
78: if(isset($this->registry[$key])) {
79: throw new FrameworkException("Object for key {$key} has already been set.");
80: }
81: $this->registry[$key] = $value;
82: }
83:
84: /**
85: * Unset an object from the registry
86: *
87: * @param String $key Name of the object.
88: * @return void
89: */
90: public function offsetUnset($key) {
91: unset($this->registry[$key]);
92: }
93:
94: /**
95: * Get all the instances of a particular type.
96: *
97: * @param Object $instanceof
98: * @param Array $object Array of objects.
99: */
100: public function allOffsetGet($instanceof) {
101: $objects = array();
102: foreach ($this->registry as $value) {
103: if($value instanceof $instanceof) {
104: $objects[] = $value;
105: }
106: }
107: return $objects;
108: }
109: }