1
0
mirror of https://github.com/monstra-cms/monstra.git synced 2025-07-10 16:16:18 +02:00

Core Improvements: Next Round #79 #80

This commit is contained in:
Awilum
2013-01-08 18:29:30 +02:00
parent ef76f8befc
commit b7dcc5be49
34 changed files with 341 additions and 194 deletions

View File

@ -1,88 +0,0 @@
<?php
/**
* Gelato Library
*
* This source file is part of the Gelato Library. More information,
* documentation and tutorials can be found at http://gelato.monstra.org
*
* @package Gelato
*
* @author Romanenko Sergey / Awilum
* @copyright (c) 2013 Romanenko Sergey / Awilum
* @since 1.0.0
*/
class Alert
{
/**
* Protected constructor since this is a static class.
*
* @access protected
*/
protected function __construct()
{
// Nothing here
}
/**
* Show success message
*
* <code>
* Alert::success('Message here...');
* </code>
*
* @param string $message Message
* @param integer $time Time
*/
public static function success($message, $time = 3000)
{
// Redefine vars
$message = (string) $message;
$time = (int) $time;
echo '<div class="alert alert-info">'.$message.'</div>
<script type="text/javascript">setTimeout(\'$(".alert").slideUp("slow")\', '.$time.'); </script>';
}
/**
* Show warning message
*
* <code>
* Alert::warning('Message here...');
* </code>
*
* @param string $message Message
* @param integer $time Time
*/
public static function warning($message, $time = 3000)
{
// Redefine vars
$message = (string) $message;
$time = (int) $time;
echo '<div class="alert alert-warning">'.$message.'</div>
<script type="text/javascript">setTimeout(\'$(".alert").slideUp("slow")\', '.$time.'); </script>';
}
/**
* Show error message
*
* <code>
* Alert::error('Message here...');
* </code>
*
* @param string $message Message
* @param integer $time Time
*/
public static function error($message, $time = 3000)
{
// Redefine vars
$message = (string) $message;
$time = (int) $time;
echo '<div class="alert alert-error">'.$message.'</div>
<script type="text/javascript">setTimeout(\'$(".alert").slideUp("slow")\', '.$time.'); </script>';
}
}

View File

@ -0,0 +1,205 @@
<?php
/**
* Gelato Library
*
* This source file is part of the Gelato Library. More information,
* documentation and tutorials can be found at http://gelato.monstra.org
*
* @package Gelato
*
* @author Romanenko Sergey / Awilum
* @copyright (c) 2013 Romanenko Sergey / Awilum
* @since 1.0.0
*/
class ClassLoader
{
/**
* Mapping from class names to paths.
*
* @var array
*/
protected static $classes = array();
/**
* PSR-0 directories.
*
* @var array
*/
protected static $directories = array();
/**
* Registered namespaces.
*
* @var array
*/
protected static $namespaces = array();
/**
* Class aliases.
*
* @var array
*/
protected static $aliases = array();
/**
* Protected constructor since this is a static class.
*
* @access protected
*/
protected function __construct()
{
// Nothing here
}
/**
* Add class to mapping.
*
* @access public
* @param string $className Class name
* @param string $classPath Full path to class
*/
public static function mapClass($className, $classPath)
{
static::$classes[$className] = $classPath;
}
/**
* Add multiple classes to mapping.
*
* @access public
* @param array $classes Array of classes to map (key = class name and value = class path)
*/
public static function mapClasses(array $classes)
{
foreach($classes as $name => $path)
{
static::$classes[$name] = $path;
}
}
/**
* Adds a PSR-0 directory path.
*
* @access public
* @param string $path Path to PSR-0 directory
*/
public static function directory($path)
{
static::$directories[] = rtrim($path, '/');
echo rtrim($path, '/');
}
/**
* Registers a namespace.
*
* @access public
* @param string $namespace Namespace
* @param string $path Path
*/
public static function registerNamespace($namespace, $path)
{
static::$namespaces[trim($namespace, '\\') . '\\'] = rtrim($path, '/');
}
/**
* Set an alias for a class.
*
* @access public
* @param string $alias Class alias
* @param string $className Class name
*/
public static function alias($alias, $className)
{
static::$aliases[$alias] = $className;
}
/**
* Try to load a PSR-0 compatible class.
*
* @access protected
* @param string $className Class name
* @param string $directory (Optional) Overrides the array of PSR-0 paths
* @return boolean
*/
protected static function loadPSR0($className, $directory = null)
{
$classPath = '';
if(($pos = strripos($className, '\\')) !== false)
{
$namespace = substr($className, 0, $pos);
$className = substr($className, $pos + 1);
$classPath = str_replace('\\', '/', $namespace) . '/';
}
$classPath .= str_replace('_', '/', $className) . '.php';
$directories = ($directory === null) ? static::$directories : array($directory);
foreach($directories as $directory)
{
if(file_exists($directory . '/' . $classPath))
{
include($directory . '/' . $classPath);
return true;
}
}
return false;
}
/**
* Autoloader.
*
* @access public
* @param string $className Class name
* @return boolean
*/
public static function load($className)
{
$className = ltrim($className, '\\');
// Try to autoload an aliased class
if(isset(static::$aliases[$className]))
{
return class_alias(static::$aliases[$className], $className);
}
// Try to load a mapped class
if(isset(static::$classes[$className]) && file_exists(static::$classes[$className]))
{
include static::$classes[$className];
return true;
}
// Try to load class from a registered namespace
foreach(static::$namespaces as $namespace => $path)
{
if(strpos($className, $namespace) === 0)
{
if(static::loadPSR0(substr($className, strlen($namespace)), $path))
{
return true;
}
}
}
// Try to load a PSR-0 compatible class
// The second call to the loadPSR0 method is used to autoload legacy code
if (static::loadPSR0($className) || static::loadPSR0(strtolower($className)))
{
return true;
}
return false;
}
}

View File

@ -260,10 +260,10 @@ class ErrorHandler
$error['highlighted'] = static::highlightCode($error['file'], $error['line']);
Response::status(500);
include 'Resources/Templates/exception.php';
include 'Resources/Views/Errors/exception.php';
} else {
Response::status(500);
include 'Resources/Templates/error.php';
include 'Resources/Views/Errors/error.php';
}
} catch (Exception $e) {
while(ob_get_level() > 0) ob_end_clean();

View File

@ -25,18 +25,10 @@ if ( ! defined('GELATO_DISPLAY_ERRORS')) {
define('GELATO_DISPLAY_ERRORS', true);
}
/**
* Should we use the Gelato Autoloader to ensure the dependancies are automatically
* loaded?
*/
if ( ! defined('GELATO_AUTOLOADER')) {
define('GELATO_AUTOLOADER', true);
}
/**
* Load Gelato Error Handler
*/
require_once __DIR__ . '/ErrorHandler.php';
require_once __DIR__ . '/ErrorHandler/ErrorHandler.php';
/**
* Set Error Handler
@ -54,90 +46,42 @@ register_shutdown_function('ErrorHandler::fatalErrorHandler');
set_exception_handler('ErrorHandler::exception');
/**
* Register Gelato Autoloader
* Gelato Class Loader
*/
if (GELATO_AUTOLOADER) {
spl_autoload_register(array('Gelato', 'autoload'));
}
require_once __DIR__ . '/ClassLoader/ClassLoader.php';
/**
* Gelato
* Map all Gelato Classes
*/
class Gelato
{
/**
* Registry of variables
*
* @var array
ClassLoader::mapClasses(array(
'Agent' => __DIR__.'/Agent/Agent.php',
'Arr' => __DIR__.'/Arr/Arr.php',
'Cache' => __DIR__.'/Cache/Cache.php',
'Cookie' => __DIR__.'/Cookie/Cookie.php',
'Curl' => __DIR__.'/Curl/Curl.php',
'Date' => __DIR__.'/Date/Date.php',
'Debug' => __DIR__.'/Debug/Debug.php',
'File' => __DIR__.'/FileSystem/File.php',
'Dir' => __DIR__.'/FileSystem/Dir.php',
'Form' => __DIR__.'/Form/Form.php',
'Html' => __DIR__.'/Html/Html.php',
'Image' => __DIR__.'/Image/Image.php',
'Inflector' => __DIR__.'/Inflector/Inflector.php',
'Minify' => __DIR__.'/Minify/Minify.php',
'Notification' => __DIR__.'/Notification/Notification.php',
'Number' => __DIR__.'/Number/Number.php',
'Registry' => __DIR__.'/Registry/Registry.php',
'Request' => __DIR__.'/Http/Request.php',
'Response' => __DIR__.'/Http/Response.php',
'Token' => __DIR__.'/Security/Token.php',
'Text' => __DIR__.'/Text/Text.php',
'Session' => __DIR__.'/Session/Session.php',
'Url' => __DIR__.'/Url/Url.php',
'Valid' => __DIR__.'/Validation/Valid.php',
'Zip' => __DIR__.'/Zip/Zip.php',
));
/**
* Register Gelato Autoloader
*/
private static $registry = array();
/**
* Gelato Autload
*/
public static function autoload($className)
{
$path = dirname(realpath(__FILE__));
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $path.DIRECTORY_SEPARATOR.$fileName;
}
/**
* Checks if an object with this name is in the registry.
*
* @return bool
* @param string $name The name of the registry item to check for existence.
*/
public static function exists($name)
{
return isset(Gelato::$registry[(string) $name]);
}
/**
* Registers a given value under a given name.
*
* @param string $name The name of the value to store.
* @param mixed[optional] $value The value that needs to be stored.
*/
public static function set($name, $value = null)
{
// redefine name
$name = (string) $name;
// delete item
if ($value === null) {
unset(Gelato::$registry[$name]);
} else {
Gelato::$registry[$name] = $value;
return Gelato::get($name);
}
}
/**
* Fetch an item from the registry.
*
* @return mixed
* @param string $name The name of the item to fetch.
*/
public static function get($name)
{
$name = (string) $name;
if ( ! isset(Gelato::$registry[$name])) {
throw new RuntimeException('No item "' . $name . '" exists in the registry.');
}
return Gelato::$registry[$name];
}
}
spl_autoload_register('ClassLoader::load');

View File

@ -21,11 +21,9 @@ class Response
* @var array
*/
public static $messages = array(
// Informational 1xx
100 => 'Continue',
101 => 'Switching Protocols',
// Success 2xx
102 => 'Processing', // RFC2518
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
@ -33,18 +31,18 @@ class Response
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
// Redirection 3xx
207 => 'Multi-Status', // RFC4918
208 => 'Already Reported', // RFC5842
226 => 'IM Used', // RFC3229
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found', // 1.1
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
// 306 is deprecated but reserved
306 => 'Reserved',
307 => 'Temporary Redirect',
// Client Error 4xx
308 => 'Permanent Redirect', // RFC-reschke-http-status-308-07
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
@ -63,15 +61,26 @@ class Response
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
// Server Error 5xx
418 => 'I\'m a teapot', // RFC2324
422 => 'Unprocessable Entity', // RFC4918
423 => 'Locked', // RFC4918
424 => 'Failed Dependency', // RFC4918
425 => 'Reserved for WebDAV advanced collections expired proposal', // RFC2817
426 => 'Upgrade Required', // RFC2817
428 => 'Precondition Required', // RFC6585
429 => 'Too Many Requests', // RFC6585
431 => 'Request Header Fields Too Large', // RFC6585
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
509 => 'Bandwidth Limit Exceeded'
506 => 'Variant Also Negotiates (Experimental)', // RFC2295
507 => 'Insufficient Storage', // RFC4918
508 => 'Loop Detected', // RFC5842
510 => 'Not Extended', // RFC2774
511 => 'Network Authentication Required', // RFC6585
);
/**

View File

@ -0,0 +1,77 @@
<?php
/**
* Gelato Library
*
* This source file is part of the Gelato Library. More information,
* documentation and tutorials can be found at http://gelato.monstra.org
*
* @package Gelato
*
* @author Romanenko Sergey / Awilum
* @copyright (c) 2013 Romanenko Sergey / Awilum
* @since 1.0.0
*/
class Registry
{
/**
* Registry of variables
*
* @var array
*/
private static $registry = array();
/**
* Checks if an object with this name is in the registry.
*
* @return bool
* @param string $name The name of the registry item to check for existence.
*/
public static function exists($name)
{
return isset(Gelato::$registry[(string) $name]);
}
/**
* Registers a given value under a given name.
*
* @param string $name The name of the value to store.
* @param mixed[optional] $value The value that needs to be stored.
*/
public static function set($name, $value = null)
{
// redefine name
$name = (string) $name;
// delete item
if ($value === null) {
unset(Gelato::$registry[$name]);
} else {
Gelato::$registry[$name] = $value;
return Gelato::get($name);
}
}
/**
* Fetch an item from the registry.
*
* @return mixed
* @param string $name The name of the item to fetch.
*/
public static function get($name)
{
$name = (string) $name;
if ( ! isset(Gelato::$registry[$name])) {
throw new RuntimeException('No item "' . $name . '" exists in the registry.');
}
return Gelato::$registry[$name];
}
}