added module support!
This commit is contained in:
@@ -36,21 +36,19 @@ class Config {
|
||||
*/
|
||||
public static function get($key, $default = null)
|
||||
{
|
||||
if (strpos($key, '.') === false)
|
||||
{
|
||||
static::load($key);
|
||||
list($module, $file, $key) = static::parse($key);
|
||||
|
||||
return Arr::get(static::$items, $key, $default);
|
||||
}
|
||||
|
||||
list($file, $key) = static::parse($key);
|
||||
|
||||
if ( ! static::load($file))
|
||||
if ( ! static::load($module, $file))
|
||||
{
|
||||
return is_callable($default) ? call_user_func($default) : $default;
|
||||
}
|
||||
|
||||
return Arr::get(static::$items[$file], $key, $default);
|
||||
if (is_null($key))
|
||||
{
|
||||
return static::$items[$module][$file];
|
||||
}
|
||||
|
||||
return Arr::get(static::$items[$module][$file], $key, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,11 +60,14 @@ class Config {
|
||||
*/
|
||||
public static function set($key, $value)
|
||||
{
|
||||
list($file, $key) = static::parse($key);
|
||||
list($module, $file, $key) = static::parse($key);
|
||||
|
||||
static::load($file);
|
||||
if (is_null($key) or ! static::load($module, $file))
|
||||
{
|
||||
throw new \Exception("Unable to find configuration file item [$key].");
|
||||
}
|
||||
|
||||
static::$items[$file][$key] = $value;
|
||||
static::$items[$module][$file][$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,14 +81,24 @@ class Config {
|
||||
*/
|
||||
private static function parse($key)
|
||||
{
|
||||
$segments = explode('.', $key);
|
||||
// Check for a module qualifier. If a module name is present, we need to extract it from
|
||||
// the configuration key, otherwise, we will use "application" as the module.
|
||||
$module = (strpos($key, '::') !== false) ? substr($key, 0, strpos($key, ':')) : 'application';
|
||||
|
||||
if (count($segments) < 2)
|
||||
// If the configuration item is stored in a module, we need to strip the module qualifier
|
||||
// off of the configuration key before continuing.
|
||||
if ($module != 'application')
|
||||
{
|
||||
throw new \Exception("Invalid configuration key [$key].");
|
||||
$key = substr($key, strpos($key, ':') + 2);
|
||||
}
|
||||
|
||||
return array($segments[0], implode('.', array_slice($segments, 1)));
|
||||
$segments = explode('.', $key);
|
||||
|
||||
// If there is more than one segment, we need to splice together and portion of the
|
||||
// configuration key that comes after the first segment, which is the file name.
|
||||
$key = (count($segments) > 1) ? implode('.', array_slice($segments, 1)) : null;
|
||||
|
||||
return array($module, $segments[0], $key);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,25 +108,32 @@ class Config {
|
||||
* Any environment specific configuration files will be merged with the root file.
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $module
|
||||
* @return bool
|
||||
*/
|
||||
public static function load($file)
|
||||
public static function load($module, $file)
|
||||
{
|
||||
if (array_key_exists($file, static::$items)) return true;
|
||||
// If the configuration items for this module and file have already been
|
||||
// loaded, we can bail out of this method.
|
||||
if (isset(static::$items[$module]) and array_key_exists($file, static::$items[$module])) return true;
|
||||
|
||||
$config = (file_exists($path = CONFIG_PATH.$file.EXT)) ? require $path : array();
|
||||
$path = ($module === 'application') ? CONFIG_PATH : MODULE_PATH.$module.'/config/';
|
||||
|
||||
if (isset($_SERVER['LARAVEL_ENV']) and file_exists($path = CONFIG_PATH.$_SERVER['LARAVEL_ENV'].'/'.$file.EXT))
|
||||
// Load the base configuration items for the module and file.
|
||||
$config = (file_exists($base = $path.$file.EXT)) ? require $base : array();
|
||||
|
||||
// Merge any enviornment specific configuration items for the module and file.
|
||||
if (isset($_SERVER['LARAVEL_ENV']) and file_exists($path = $path.$_SERVER['LARAVEL_ENV'].'/'.$file.EXT))
|
||||
{
|
||||
$config = array_merge($config, require $path);
|
||||
}
|
||||
|
||||
if (count($config) > 0)
|
||||
{
|
||||
static::$items[$file] = $config;
|
||||
static::$items[$module][$file] = $config;
|
||||
}
|
||||
|
||||
return isset(static::$items[$file]);
|
||||
return isset(static::$items[$module][$file]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -151,7 +151,22 @@ abstract class Model {
|
||||
return $class::$table;
|
||||
}
|
||||
|
||||
return strtolower(Inflector::plural($class));
|
||||
return strtolower(Inflector::plural(static::model($class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an Eloquent model name without any namespaces.
|
||||
*
|
||||
* @param string|Model $model
|
||||
* @return string
|
||||
*/
|
||||
public static function model($model)
|
||||
{
|
||||
$class = (is_object($model)) ? get_class($model) : $model;
|
||||
|
||||
$segments = array_reverse(explode('\\', $class));
|
||||
|
||||
return $segments[0];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -254,7 +269,7 @@ abstract class Model {
|
||||
*/
|
||||
private function has_one_or_many($model, $foreign_key)
|
||||
{
|
||||
$this->relating_key = (is_null($foreign_key)) ? strtolower(get_class($this)).'_id' : $foreign_key;
|
||||
$this->relating_key = (is_null($foreign_key)) ? strtolower(static::model($this)).'_id' : $foreign_key;
|
||||
|
||||
return static::query($model)->where($this->relating_key, '=', $this->id);
|
||||
}
|
||||
@@ -308,11 +323,11 @@ abstract class Model {
|
||||
|
||||
// Allowing the overriding of the foreign and associated keys provides the flexibility for
|
||||
// self-referential many-to-many relationships, such as a "buddy list".
|
||||
$this->relating_key = (is_null($foreign_key)) ? strtolower(get_class($this)).'_id' : $foreign_key;
|
||||
$this->relating_key = (is_null($foreign_key)) ? strtolower(static::model($this)).'_id' : $foreign_key;
|
||||
|
||||
// The associated key is the foreign key name of the related model. So, if the related model
|
||||
// is "Role", the associated key on the intermediate table would be "role_id".
|
||||
$associated_key = (is_null($associated_key)) ? strtolower($model).'_id' : $associated_key;
|
||||
$associated_key = (is_null($associated_key)) ? strtolower(static::model($model)).'_id' : $associated_key;
|
||||
|
||||
return static::query($model)
|
||||
->select(array(static::table($model).'.*'))
|
||||
@@ -325,13 +340,13 @@ abstract class Model {
|
||||
*
|
||||
* By default, the intermediate table name is the plural names of the models
|
||||
* arranged alphabetically and concatenated with an underscore.
|
||||
*
|
||||
*
|
||||
* @param string $model
|
||||
* @return string
|
||||
*/
|
||||
private function intermediate_table($model)
|
||||
{
|
||||
$models = array(Inflector::plural($model), Inflector::plural(get_class($this)));
|
||||
$models = array(Inflector::plural(static::model($model)), Inflector::plural(static::model($this)));
|
||||
|
||||
sort($models);
|
||||
|
||||
|
||||
@@ -68,16 +68,16 @@ class Lang {
|
||||
$language = Config::get('application.language');
|
||||
}
|
||||
|
||||
list($file, $line) = $this->parse($this->key);
|
||||
list($module, $file, $line) = $this->parse($this->key, $language);
|
||||
|
||||
$this->load($file, $language);
|
||||
$this->load($module, $file, $language);
|
||||
|
||||
if ( ! isset(static::$lines[$language.$file][$line]))
|
||||
if ( ! isset(static::$lines[$module][$language.$file][$line]))
|
||||
{
|
||||
return is_callable($default) ? call_user_func($default) : $default;
|
||||
}
|
||||
|
||||
$line = static::$lines[$language.$file][$line];
|
||||
$line = static::$lines[$module][$language.$file][$line];
|
||||
|
||||
foreach ($this->replacements as $key => $value)
|
||||
{
|
||||
@@ -94,34 +94,51 @@ class Lang {
|
||||
* while the right side of the dot is the item within that file.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $language
|
||||
* @return array
|
||||
*/
|
||||
private function parse($key)
|
||||
private function parse($key, $language)
|
||||
{
|
||||
$segments = explode('.', $key);
|
||||
// Check for a module qualifier. If a module name is present, we need to extract it from
|
||||
// the language line, otherwise, we will use "application" as the module.
|
||||
$module = (strpos($key, '::') !== false) ? substr($key, 0, strpos($key, ':')) : 'application';
|
||||
|
||||
if (count($segments) < 2)
|
||||
// If the language line is stored in a module, we need to strip the module qualifier
|
||||
// off of the language key before continuing.
|
||||
if ($module != 'application')
|
||||
{
|
||||
throw new \Exception("Invalid language key [$key].");
|
||||
$key = substr($key, strpos($key, ':') + 2);
|
||||
}
|
||||
|
||||
return array($segments[0], implode('.', array_slice($segments, 1)));
|
||||
$segments = explode('.', $key);
|
||||
|
||||
if (count($segments) > 1)
|
||||
{
|
||||
return array($module, $segments[0], $segments[1]);
|
||||
}
|
||||
|
||||
throw new \Exception("Invalid language line [$key]. A specific line must be specified.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a language file.
|
||||
*
|
||||
* @param string $module
|
||||
* @param string $file
|
||||
* @param string $language
|
||||
* @return void
|
||||
*/
|
||||
private function load($file, $language)
|
||||
private function load($module, $file, $language)
|
||||
{
|
||||
if (array_key_exists($language.$file, static::$lines)) return;
|
||||
// If the language lines for the given module, file, and language have already been
|
||||
// loaded, we can bail out of this method.
|
||||
if (isset(static::$lines[$module][$language.$file])) return;
|
||||
|
||||
if (file_exists($path = LANG_PATH.$language.'/'.$file.EXT))
|
||||
$path = ($module === 'application') ? LANG_PATH : MODULE_PATH.$module.'/lang/';
|
||||
|
||||
if (file_exists($path = $path.$language.'/'.$file.EXT))
|
||||
{
|
||||
static::$lines[$language.$file] = require $path;
|
||||
static::$lines[$module][$language.$file] = require $path;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,13 @@ class Loader {
|
||||
*/
|
||||
private static $aliases = array();
|
||||
|
||||
/**
|
||||
* All of the active modules.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $modules = array();
|
||||
|
||||
/**
|
||||
* Bootstrap the auto-loader.
|
||||
*
|
||||
@@ -23,16 +30,15 @@ class Loader {
|
||||
*/
|
||||
public static function bootstrap()
|
||||
{
|
||||
static::$aliases = require CONFIG_PATH.'aliases'.EXT;
|
||||
static::$aliases = Config::get('aliases');
|
||||
static::$modules = Config::get('application.modules');
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a class file for a given class name.
|
||||
*
|
||||
* This function is registered on the SPL auto-loader stack by the front controller during each request.
|
||||
*
|
||||
* All Laravel class names follow a namespace to directory convention. So, if a class exists in
|
||||
* application/libraries/user, it shouold be placed in the "User" namespace.
|
||||
* All Laravel class names follow a namespace to directory convention.
|
||||
*
|
||||
* @param string $class
|
||||
* @return void
|
||||
@@ -46,13 +52,60 @@ class Loader {
|
||||
return class_alias(static::$aliases[$class], $class);
|
||||
}
|
||||
|
||||
if ( ! static::load_from_registered($file))
|
||||
{
|
||||
static::load_from_module($file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a class that is stored in the registered directories.
|
||||
*
|
||||
* @param string $file
|
||||
* @return bool
|
||||
*/
|
||||
private static function load_from_registered($file)
|
||||
{
|
||||
foreach (static::$paths as $directory)
|
||||
{
|
||||
if (file_exists($path = $directory.$file.EXT))
|
||||
{
|
||||
require $path;
|
||||
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a class that is stored in a module.
|
||||
*
|
||||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
private static function load_from_module($file)
|
||||
{
|
||||
// Since all module models and libraries must be namespaced to the
|
||||
// module name, we'll extract the module name from the file.
|
||||
$module = substr($file, 0, strpos($file, '/'));
|
||||
|
||||
if (in_array($module, static::$modules))
|
||||
{
|
||||
$module = MODULE_PATH.$module.'/';
|
||||
|
||||
// Slice the module name off of the filename. Even though module libraries
|
||||
// and models are namespaced under the module, there will obviously not be
|
||||
// a folder matching that namespace in the libraries or models directories
|
||||
// of the module. Slicing it off will allow us to make a clean search for
|
||||
// the relevant class file.
|
||||
$file = substr($file, strpos($file, '/') + 1);
|
||||
|
||||
foreach (array($module.'models', $module.'libraries') as $directory)
|
||||
{
|
||||
if (file_exists($path = $directory.'/'.$file.EXT))
|
||||
{
|
||||
return require $path;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ class Request {
|
||||
}
|
||||
|
||||
// Remove the application index page from the URI.
|
||||
if (strpos($uri, $index = '/'.Config::get('application.index')) === 0)
|
||||
if (strpos($uri, $index = '/index.php') === 0)
|
||||
{
|
||||
$uri = substr($uri, strlen($index));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php namespace System\Routing;
|
||||
|
||||
use System\Config;
|
||||
|
||||
class Loader {
|
||||
|
||||
/**
|
||||
@@ -35,19 +37,17 @@ class Loader {
|
||||
*/
|
||||
public function load($uri)
|
||||
{
|
||||
return array_merge($this->load_nested_routes($uri), require $this->path.'routes'.EXT);
|
||||
return array_merge($this->load_nested_routes(explode('/', $uri)), require $this->path.'routes'.EXT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the appropriate routes from the routes directory.
|
||||
*
|
||||
* @param string $uri
|
||||
* @param array $segments
|
||||
* @return array
|
||||
*/
|
||||
private function load_nested_routes($uri)
|
||||
private function load_nested_routes($segments)
|
||||
{
|
||||
$segments = explode('/', $uri);
|
||||
|
||||
// Work backwards through the URI segments until we find the deepest possible
|
||||
// matching route directory. Once we find it, we will return those routes.
|
||||
foreach (array_reverse($segments, true) as $key => $value)
|
||||
@@ -75,19 +75,35 @@ class Loader {
|
||||
{
|
||||
if ( ! is_null(static::$routes) and ! $reload) return static::$routes;
|
||||
|
||||
$routes = require $path.'routes'.EXT;
|
||||
// Merge all of the module paths in with the specified path so that all
|
||||
// active module routes will also be loaded. So, by default, this method
|
||||
// will search the application path and all active module paths for routes.
|
||||
$paths = array_merge(array($path), array_map(function($module) { return MODULE_PATH.$module.'/'; }, Config::get('application.modules')));
|
||||
|
||||
// Since route files can be nested deep within the route directory, we need to
|
||||
// recursively spin through the directory to find every file.
|
||||
$directoryIterator = new \RecursiveDirectoryIterator($path.'routes');
|
||||
$routes = array();
|
||||
|
||||
$recursiveIterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST);
|
||||
|
||||
foreach ($recursiveIterator as $file)
|
||||
foreach ($paths as $path)
|
||||
{
|
||||
if (filetype($file) === 'file' and strpos($file, EXT) !== false)
|
||||
if (file_exists($path.'routes'.EXT))
|
||||
{
|
||||
$routes = array_merge(require $file, $routes);
|
||||
$routes = array_merge($routes, require $path.'routes'.EXT);
|
||||
}
|
||||
|
||||
if (is_dir($path.'routes'))
|
||||
{
|
||||
// Since route files can be nested deep within the route directory, we need to
|
||||
// recursively spin through the directory to find every file.
|
||||
$directoryIterator = new \RecursiveDirectoryIterator($path.'routes');
|
||||
|
||||
$recursiveIterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST);
|
||||
|
||||
foreach ($recursiveIterator as $file)
|
||||
{
|
||||
if (filetype($file) === 'file' and strpos($file, EXT) !== false)
|
||||
{
|
||||
$routes = array_merge($routes, require $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,12 +40,12 @@ class Router {
|
||||
*
|
||||
* @param string $method
|
||||
* @param string $uri
|
||||
* @param array $routes
|
||||
* @param Loader $loader
|
||||
* @return Router
|
||||
*/
|
||||
public static function make($method, $uri, $routes = null)
|
||||
public static function make($method, $uri, $loader)
|
||||
{
|
||||
return new static($method, $uri, $routes);
|
||||
return new static($method, $uri, $loader);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,6 +16,13 @@ class View {
|
||||
*/
|
||||
public $data = array();
|
||||
|
||||
/**
|
||||
* The module that contains the view.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $module;
|
||||
|
||||
/**
|
||||
* The path to the view.
|
||||
*
|
||||
@@ -24,12 +31,19 @@ class View {
|
||||
public $path;
|
||||
|
||||
/**
|
||||
* The view composers.
|
||||
* The defined view composers.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $composers;
|
||||
|
||||
/**
|
||||
* The defined view names.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $names;
|
||||
|
||||
/**
|
||||
* Create a new view instance.
|
||||
*
|
||||
@@ -39,15 +53,16 @@ class View {
|
||||
*/
|
||||
public function __construct($view, $data = array())
|
||||
{
|
||||
$this->view = $view;
|
||||
$this->data = $data;
|
||||
|
||||
if ( ! file_exists($path = VIEW_PATH.$view.EXT))
|
||||
list($this->module, $this->path, $this->view) = static::parse($view);
|
||||
|
||||
if ( ! file_exists($this->path.$this->view.EXT))
|
||||
{
|
||||
throw new \Exception("View [$view] does not exist.");
|
||||
}
|
||||
|
||||
$this->path = $path;
|
||||
$this->compose();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,33 +74,49 @@ class View {
|
||||
*/
|
||||
public static function make($view, $data = array())
|
||||
{
|
||||
if (is_null(static::$composers))
|
||||
{
|
||||
static::$composers = require APP_PATH.'composers'.EXT;
|
||||
}
|
||||
|
||||
$instance = new static($view, $data);
|
||||
|
||||
return (isset(static::$composers[$view])) ? call_user_func(static::$composers[$view], $instance) : $instance;
|
||||
return new static($view, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new named view instance.
|
||||
* Parse a view identifier and get the module, path, and view name.
|
||||
*
|
||||
* @param string $view
|
||||
* @param array $data
|
||||
* @return View
|
||||
* @return array
|
||||
*/
|
||||
public static function of($view, $data = array())
|
||||
private static function parse($view)
|
||||
{
|
||||
$views = Config::get('view.names');
|
||||
// Check for a module qualifier. If a module name is present, we need to extract it from
|
||||
// the view name, otherwise, we will use "application" as the module.
|
||||
$module = (strpos($view, '::') !== false) ? substr($view, 0, strpos($view, ':')) : 'application';
|
||||
|
||||
if ( ! array_key_exists($view, $views))
|
||||
$path = ($module == 'application') ? VIEW_PATH : MODULE_PATH.$module.'/views/';
|
||||
|
||||
// If the view is stored in a module, we need to strip the module qualifier off
|
||||
// of the view name before continuing.
|
||||
if ($module != 'application')
|
||||
{
|
||||
throw new \Exception("Named view [$view] is not defined.");
|
||||
$view = substr($view, strpos($view, ':') + 2);
|
||||
}
|
||||
|
||||
return static::make($views[$view], $data);
|
||||
return array($module, $path, str_replace('.', '/', $view));
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the composer for the view instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function compose()
|
||||
{
|
||||
if (is_null(static::$composers[$this->module]))
|
||||
{
|
||||
static::$composers[$this->module] = (file_exists($path = $this->path.'composers'.EXT)) ? require $path : array();
|
||||
}
|
||||
|
||||
if (isset(static::$composers[$this->module][$this->view]))
|
||||
{
|
||||
call_user_func(static::$composers[$this->module][$this->view], $this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,7 +138,7 @@ class View {
|
||||
|
||||
ob_start();
|
||||
|
||||
try { include $this->path; } catch (\Exception $e) { Error::handle($e); }
|
||||
try { include $this->path.$this->view.EXT; } catch (\Exception $e) { Error::handle($e); }
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
@@ -138,17 +169,6 @@ class View {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Method for creating named view instances.
|
||||
*/
|
||||
public static function __callStatic($method, $parameters)
|
||||
{
|
||||
if (strpos($method, 'of_') === 0)
|
||||
{
|
||||
return static::of(substr($method, 3), Arr::get($parameters, 0, array()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Method for getting items from the view data.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user