Container.php 1.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

namespace Q2A\App;

use Q2A\Exceptions\FatalErrorException;

class Container
{
	/** @var array */
26
	protected $services = array();
27 28 29

	/**
	 * Bind an object to a key.
30
	 * @param string $key The key to bind the object to
31 32
	 * @param mixed $object The object to bind to the key
	 */
33
	public function set($key, $object)
34
	{
35
		$this->services[$key] = $object;
36 37 38
	}

	/**
39 40
	 * Return an object assigned to the given key. If the key is not found an exception is thrown.
	 * @param string $key The key to look for
41 42
	 * @return mixed
	 */
43
	public function get($key)
44
	{
45 46
		if (isset($this->services[$key])) {
			return $this->services[$key];
47 48
		}

49
		throw new FatalErrorException(sprintf('Key "%s" not found in container', $key));
50 51
	}
}