Commit 4559829f by Scott

Simplify Container, move helpers

parent eacf34fc
......@@ -181,9 +181,9 @@ function qa_get_request_content()
$routing = qa_page_routing();
qa_routing_config();
$route = app('router')->match($requestlower);
$route = qa_service('router')->match($requestlower);
if (isset($route)) {
if ($route !== null) {
// use new Controller system
qa_set_template($route->getId());
$controllerClass = $route->getController();
......@@ -455,12 +455,13 @@ function qa_page_routing()
);
}
/**
* [qa_routing_config description]
* Set up routing.
*/
function qa_routing_config()
{
$router = app('router');
$router = qa_service('router');
$router->addRoute('user', 'get', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile');
$router->addRoute('user', 'post', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile');
$router->addRoute('user-self', 'get', 'user', '\Q2A\Controllers\User\UserProfile', 'index');
......
......@@ -46,6 +46,8 @@ function qa_autoload($class)
}
spl_autoload_register('qa_autoload');
use Q2A\App\Application;
// Execution section of this file - remainder contains function definitions
......@@ -60,11 +62,6 @@ if (defined('QA_WORDPRESS_LOAD_FILE')) {
require_once QA_JOOMLA_LOAD_FILE;
}
require QA_BASE_DIR . 'qa-src/helpers.php';
app();
qa_initialize_constants_2();
qa_initialize_modularity();
qa_register_core_modules();
......@@ -1843,6 +1840,36 @@ function qa_retrieve_url($url)
/**
* Helper function to access the Application object.
* @return Application
*/
function qa_app()
{
return Application::getInstance();
}
/**
* Helper function to access services in the Container.
* If the $key parameter is set and the $object parameter is null the container is called to resolve the $key.
* If the $key and the $object parameters are null the container is called to bind the $object to the $key.
* @param mixed $key Identifier for the object to get/set.
* @param mixed $object Object to set in the $key (if null, a stored object is returned)
* @return mixed
*/
function qa_service($key, $object = null)
{
$app = Application::getInstance();
if ($object === null) {
return $app->getContainer()->get($key);
}
$app->getContainer()->set($key, $object);
}
/**
* Shortcut to get or set an option value without specifying database
* @param $name
* @param mixed $value
......
......@@ -28,11 +28,11 @@ class Application
/** @var static */
protected static $instance;
public function __construct()
protected function __construct()
{
$this->container = new Container();
$this->registerCoreBindings();
$this->registerCoreServices();
}
/**
......@@ -41,7 +41,7 @@ class Application
*/
public static function getInstance()
{
if (is_null(static::$instance)) {
if (static::$instance === null) {
static::$instance = new static();
}
......@@ -49,11 +49,11 @@ class Application
}
/**
* Register the bindings used by the core.
* Register the services used by the core.
*/
private function registerCoreBindings()
private function registerCoreServices()
{
$this->container->bind('router', new Router());
$this->container->set('router', new Router());
}
/**
......
......@@ -23,29 +23,29 @@ use Q2A\Exceptions\FatalErrorException;
class Container
{
/** @var array */
protected $bindings = array();
protected $services = array();
/**
* Bind an object to a key.
* @param mixed $key The key to bind the object to
* @param string $key The key to bind the object to
* @param mixed $object The object to bind to the key
*/
public function bind($key, $object)
public function set($key, $object)
{
$this->bindings[$key] = $object;
$this->services[$key] = $object;
}
/**
* Return an object bound to the given key. If the key is not found an exception is thrown.
* @param mixed $key The key to look for
* 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
* @return mixed
*/
public function resolve($key)
public function get($key)
{
if (isset($this->bindings[$key])) {
return $this->bindings[$key];
if (isset($this->services[$key])) {
return $this->services[$key];
}
throw new FatalErrorException(sprintf('Key "%s" not bound in container', $key));
throw new FatalErrorException(sprintf('Key "%s" not found in container', $key));
}
}
......@@ -29,7 +29,7 @@ class PageNotFoundException extends ErrorMessageException
*/
public function __construct($message = null)
{
if (is_null($message)) {
if ($message === null) {
$message = qa_lang_html('main/page_not_found');
}
......
......@@ -50,16 +50,6 @@ class Route
}
/**
* Bind actual request parameters to the route, replacing any existing ones.
*
* @param array $parameters
*/
public function bindParameters($parameters)
{
$this->parameters = $parameters;
}
/**
* @return string
*/
public function getId()
......@@ -106,4 +96,14 @@ class Route
{
return $this->parameters;
}
/**
* Bind actual request parameters to the route, replacing any existing ones.
*
* @param array $parameters
*/
public function setParameters($parameters)
{
$this->parameters = $parameters;
}
}
......@@ -61,7 +61,7 @@ class Router
$pathRegex = $this->buildPathRegex($route->getRoutePath());
if (preg_match($pathRegex, $request, $matches)) {
$route->bindParameters(array_slice($matches, 1));
$route->setParameters(array_slice($matches, 1));
return $route;
}
......
<?php
use Q2A\App\Application;
if (!function_exists('app')) {
/**
* Helper function to access the application object.
* If the $key parameter is null the application instance is returned.
* If the $key parameter is set and the $object parameter is null the container is called to resolve the $key.
* If the $key and the $object parameters are null the container is called to bind the $object to the $key.
* @param mixed $key
* @param mixed $object
* @return mixed
*/
function app($key = null, $object = null)
{
if (is_null($key)) {
return Application::getInstance();
}
if (is_null($object)) {
return Application::getInstance()->getContainer()->resolve($key);
}
Application::getInstance()->getContainer()->bind($key, $object);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment