Use Pimple instead of Core\Registry and add Monolog for logging

This commit is contained in:
Frédéric Guillot
2014-11-14 22:44:25 -05:00
parent 1487cb2763
commit b081288188
57 changed files with 549 additions and 593 deletions

View File

@@ -1,83 +0,0 @@
<?php
namespace Core;
use RuntimeException;
/**
* The registry class is a dependency injection container
*
* @property mixed db
* @property mixed event
* @package core
* @author Frederic Guillot
*/
class Registry
{
/**
* Contains all dependencies
*
* @access private
* @var array
*/
private $container = array();
/**
* Contains all instances
*
* @access private
* @var array
*/
private $instances = array();
/**
* Set a dependency
*
* @access public
* @param string $name Unique identifier for the service/parameter
* @param mixed $value The value of the parameter or a closure to define an object
*/
public function __set($name, $value)
{
$this->container[$name] = $value;
}
/**
* Get a dependency
*
* @access public
* @param string $name Unique identifier for the service/parameter
* @return mixed The value of the parameter or an object
* @throws RuntimeException If the identifier is not found
*/
public function __get($name)
{
if (isset($this->container[$name])) {
if (is_callable($this->container[$name])) {
return $this->container[$name]();
}
else {
return $this->container[$name];
}
}
throw new \RuntimeException('Identifier not found in the registry: '.$name);
}
/**
* Return a shared instance of a dependency
*
* @access public
* @param string $name Unique identifier for the service/parameter
* @return mixed Same object instance of the dependency
*/
public function shared($name)
{
if (! isset($this->instances[$name])) {
$this->instances[$name] = $this->$name;
}
return $this->instances[$name];
}
}

View File

@@ -124,6 +124,20 @@ class Request
return $this->getHeader('X-Requested-With') === 'XMLHttpRequest';
}
/**
* Check if the page is requested through HTTPS
*
* Note: IIS return the value 'off' and other web servers an empty value when it's not HTTPS
*
* @static
* @access public
* @return boolean
*/
public static function isHTTPS()
{
return isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== '' && $_SERVER['HTTPS'] !== 'off';
}
/**
* Return a HTTP header value
*

View File

@@ -246,7 +246,7 @@ class Response
*/
public function hsts()
{
if (Tool::isHTTPS()) {
if (Request::isHTTPS()) {
header('Strict-Transport-Security: max-age=31536000');
}
}

View File

@@ -2,6 +2,8 @@
namespace Core;
use Pimple\Container;
/**
* Router class
*
@@ -27,24 +29,24 @@ class Router
private $action = '';
/**
* Registry instance
* Container instance
*
* @access private
* @var \Core\Registry
* @var Pimple\Container
*/
private $registry;
private $container;
/**
* Constructor
*
* @access public
* @param Registry $registry Registry instance
* @param string $controller Controller name
* @param string $action Action name
* @param Pimple\Container $container Container instance
* @param string $controller Controller name
* @param string $action Action name
*/
public function __construct(Registry $registry, $controller = '', $action = '')
public function __construct(Container $container, $controller = '', $action = '')
{
$this->registry = $registry;
$this->container = $container;
$this->controller = empty($_GET['controller']) ? $controller : $_GET['controller'];
$this->action = empty($_GET['action']) ? $action : $_GET['action'];
}
@@ -81,7 +83,7 @@ class Router
return false;
}
$instance = new $class($this->registry);
$instance = new $class($this->container);
$instance->request = new Request;
$instance->response = new Response;
$instance->session = new Session;

View File

@@ -49,7 +49,7 @@ class Session
self::SESSION_LIFETIME,
$base_path ?: '/',
null,
Tool::isHTTPS(),
Request::isHTTPS(),
true
);

View File

@@ -2,6 +2,8 @@
namespace Core;
use Pimple\Container;
/**
* Tool class
*
@@ -37,31 +39,17 @@ class Tool
*
* @static
* @access public
* @param Core\Registry $registry DPI container
* @param string $name Model name
* @param Pimple\Container $container Container instance
* @param string $name Model name
* @return mixed
*/
public static function loadModel(Registry $registry, $name)
public static function loadModel(Container $container, $name)
{
if (! isset($registry->$name)) {
if (! isset($container[$name])) {
$class = '\Model\\'.ucfirst($name);
$registry->$name = new $class($registry);
$container[$name] = new $class($container);
}
return $registry->shared($name);
}
/**
* Check if the page is requested through HTTPS
*
* Note: IIS return the value 'off' and other web servers an empty value when it's not HTTPS
*
* @static
* @access public
* @return boolean
*/
public static function isHTTPS()
{
return isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== '' && $_SERVER['HTTPS'] !== 'off';
return $container[$name];
}
}