<?php
namespace Acd;
/**
* Registry class
*
* Simple class to store or get elements from configuration registry
*/
class Registry
{
/** @var array Registry configuration array */
private $data = [];
/**
* Class constructor
* @param array $data List of values to add to the registry
*/
public function __construct(array $data = [])
{
if(!empty($data)) {
foreach ($data as $key => $value) {
$this->set($key, $value);
}
}
}
public function offsetSet($key, $value)
{
if (!$key)
{
$this->data[] = $value;
} else
{
$this->data[$key] = $value;
}
}
/**
* Key to retrieve
*
* @param mixed $key
* @return string|null
*/
public function offsetGet($key)
{
if (isset($this->data[$key]))
{
return $this->data[$key];
}
return null;
}
/**
* Whether a key exists
*
* @param mixed $key
* @return bool
*/
public function offsetExists($key)
{
return isset($this->data[$key]);
}
/**
* Key to unset
*
* @param mixed $key
*/
public function offsetUnset($key)
{
unset($this->data[$key]);
}
/**
* Adds element to registry array
*
* @param string $key - registry Key
* @param mixed $value - registry Value
* @throws Exception When there is a duplicate $key
*/
public function set($key, $value)
{
if (isset($this->data[$key]))
{
throw new \Exception('There is already an entry for key: ' . $key);
}
$this->data[$key] = $value;
}
/**
* Retrieves elements from registry array
*
* @param string $key
* @return mixed returns a registry value
* @throws Exception when no $key found
*/
public function get($key)
{
if (!isset($this->data[$key]))
{
throw new \Exception('There is no entry for key: ' . $key);
}
return $this->data[$key];
}
/**
* Remove an entry from the Registry
*
* @param string $key
* @return void
*/
public function remove($key)
{
unset($this->data[$key]);
}
/**
* Return true if value is empty for given key
*
* @param string $key
* @return bool
*/
public function isEmpty($key)
{
return empty($this->data[$key]);
}
/**
* Reset Registry container
*/
public function reset() {
$this->data = [];
}
/**
* Return total number of data elements
* @return int
*/
public function count()
{
return count($this->data);
}
/**
* IteratorAggregate interface required method
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->data);
}
}
class Main {
private $registry = null;
private $service = null;
private $obj = null;
public function __construct()
{
if (!($this->registry instanceof Registry))
{
$this->registry = new Registry;
}
}
public function getService($service)
{
return $this->registry->get($service);
}
public function setService($class, array $args = null)
{
return $this->registry->set($class, function() use ($class, $args) { $class = $this->isValidService($class); return new $class($args); });
}
private function isValidService($classname)
{
$classname = __NAMESPACE__ . '\\' . ucwords($classname);
if(class_exists($classname))
{
return $classname;
}
else {
throw new \Exception("Invalid class name given: " . $classname);
}
}
/**
* Magic method to retrieve the Object
* @param string $obj
* @return object Object instance
*/
public function __get($obj)
{
$obj = $this->registry->get($obj);
return $obj();
}
}
// Initialize main class
$app = new Acd\Main();
// Start Request Service
$app->setService('request');
Fatal error: Uncaught Error: Class "Acd\Acd\Main" not found in /in/sa2So:214
Stack trace:
#0 {main}
thrown in /in/sa2So on line 214
Process exited with code 255.
Fatal error: Uncaught Error: Class 'Acd\Acd\Main' not found in /in/sa2So:214
Stack trace:
#0 {main}
thrown in /in/sa2So on line 214
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Fatal error: Class 'Acd\Acd\Main' not found in /in/sa2So on line 214
Process exited with code 255.