diff --git a/application/composers.php b/application/composers.php new file mode 100644 index 00000000..e7709904 --- /dev/null +++ b/application/composers.php @@ -0,0 +1,27 @@ + function($view) + { + return $view; + }, + +); \ No newline at end of file diff --git a/public/index.php b/public/index.php index 9f578faf..1c6ed1bb 100644 --- a/public/index.php +++ b/public/index.php @@ -3,7 +3,7 @@ * Laravel - A clean and classy framework for PHP web development. * * @package Laravel - * @version 1.3.1 + * @version 1.4.0 * @author Taylor Otwell * @link http://laravel.com */ diff --git a/system/asset.php b/system/asset.php new file mode 100644 index 00000000..bbd3a92a --- /dev/null +++ b/system/asset.php @@ -0,0 +1,36 @@ +name = $name; + } + + /** + * Add an asset to the container. + * + * @param string $name + * @param string $source + * @param array $dependencies + * @param array $attributes + * @return void + */ + public function add($name, $source, $dependencies = array(), $attributes = array()) + { + return call_user_func(array($this, (\System\File::extension($source) == 'css') ? 'style' : 'script'), $name, $source, $dependencies, $attributes); + } + + /** + * Add CSS to the registered assets. + * + * @param string $name + * @param string $source + * @param array $dependencies + * @param array $attributes + * @return void + */ + public function style($name, $source, $dependencies = array(), $attributes = array()) + { + if ( ! array_key_exists('media', $attributes)) + { + $attributes['media'] = 'all'; + } + + $this->register('style', $name, $source, $dependencies, $attributes); + } + + /** + * Add JavaScript to the registered assets. + * + * @param string $name + * @param string $source + * @param array $dependencies + * @param array $attributes + * @return void + */ + public function script($name, $source, $dependencies = array(), $attributes = array()) + { + $this->register('script', $name, $source, $dependencies, $attributes); + } + + /** + * Add an asset to the registered assets. + * + * @param string $type + * @param string $name + * @param string $source + * @param array $dependencies + * @param array $attributes + * @return void + */ + private function register($type, $name, $source, $dependencies, $attributes) + { + $dependencies = (array) $dependencies; + + $this->assets[$type][$name] = compact('source', 'dependencies', 'attributes'); + } + + /** + * Get all of the registered CSS assets. + * + * @return string + */ + public function styles() + { + return $this->get_group('style'); + } + + /** + * Get all of the registered JavaScript assets. + * + * @return string + */ + public function scripts() + { + return $this->get_group('script'); + } + + /** + * Get all of the registered assets for a given group. + * + * @param string $group + * @return string + */ + private function get_group($group) + { + if ( ! isset($this->assets[$group]) or count($this->assets[$group]) == 0) return ''; + + $assets = ''; + + foreach ($this->arrange($this->assets[$group]) as $name => $data) + { + $assets .= $this->get_asset($group, $name); + } + + return $assets; + } + + /** + * Get a registered CSS asset. + * + * @param string $name + * @return string + */ + public function get_style($name) + { + return $this->get_asset('style', $name); + } + + /** + * Get a registered JavaScript asset. + * + * @param string $name + * @return string + */ + public function get_script($name) + { + return $this->get_asset('script', $name); + } + + /** + * Get a registered asset. + * + * @param string $group + * @param string $name + * @return string + */ + private function get_asset($group, $name) + { + if ( ! isset($this->assets[$group][$name])) + { + return ''; + } + + $asset = $this->assets[$group][$name]; + + return HTML::$group($asset['source'], $asset['attributes']); + } + + /** + * Sort and retrieve assets based on their dependencies + * + * @param array $assets + * @return array + */ + private function arrange($assets) + { + list($original, $sorted) = array($assets, array()); + + while (count($assets) > 0) + { + foreach ($assets as $asset => $value) + { + $this->evaluate_asset($asset, $value, $original, $sorted, $assets); + } + } + + return $sorted; + } + + /** + * Evaluate an asset and its dependencies. + * + * @param string $asset + * @param string $value + * @param array $original + * @param array $sorted + * @param array $assets + * @return void + */ + private function evaluate_asset($asset, $value, $original, &$sorted, &$assets) + { + // If the asset has no more dependencies, we can add it to the sorted list + // and remove it from the array of assets. Otherwise, we will not verify + // the asset's dependencies and determine if they have already been sorted. + if (count($assets[$asset]['dependencies']) == 0) + { + $sorted[$asset] = $value; + unset($assets[$asset]); + } + else + { + foreach ($assets[$asset]['dependencies'] as $key => $dependency) + { + if ( ! $this->dependency_is_valid($asset, $dependency, $original, $assets)) + { + unset($assets[$asset]['dependencies'][$key]); + continue; + } + + // If the dependency has not yet been added to the sorted list, we can not + // remove it from this asset's array of dependencies. We'll try again on + // the next trip through the loop. + if ( ! isset($sorted[$dependency])) continue; + + unset($assets[$asset]['dependencies'][$key]); + } + } + } + + /** + * Check that a dependency is valid. + * + * @param string $asset + * @param string $dependency + * @param array $original + * @param array $assets + * @return bool + */ + private function dependency_is_valid($asset, $dependency, $original, $assets) + { + if ( ! isset($original[$dependency])) + { + return false; + } + elseif ($dependency === $asset) + { + throw new \Exception("Asset [$asset] is dependent on itself."); + } + elseif (isset($assets[$dependency]) and in_array($asset, $assets[$dependency]['dependencies'])) + { + throw new \Exception("Assets [$asset] and [$dependency] have a circular dependency."); + } + } + +} \ No newline at end of file diff --git a/system/cache/driver/apc.php b/system/cache/apc.php similarity index 91% rename from system/cache/driver/apc.php rename to system/cache/apc.php index ba178634..deb07090 100644 --- a/system/cache/driver/apc.php +++ b/system/cache/apc.php @@ -1,8 +1,8 @@ -'.PHP_EOL; + return ''.PHP_EOL; } /** * Generate a CSS reference. * * @param string $url + * @param array $attributes * @return string */ - public static function style($url, $media = 'all') + public static function style($url, $attributes = array()) { - return ''.PHP_EOL; + if ( ! array_key_exists('media', $attributes)) + { + $attributes['media'] = 'all'; + } + + return ''.PHP_EOL; } /** diff --git a/system/session/driver/apc.php b/system/session/apc.php similarity index 65% rename from system/session/driver/apc.php rename to system/session/apc.php index d240d015..3716b5e4 100644 --- a/system/session/driver/apc.php +++ b/system/session/apc.php @@ -1,6 +1,8 @@ -get($id); + return Cache::driver('apc')->get($id); } /** @@ -21,7 +23,7 @@ public function load($id) */ public function save($session) { - \System\Cache::driver('apc')->put($session['id'], $session, \System\Config::get('session.lifetime')); + Cache::driver('apc')->put($session['id'], $session, Config::get('session.lifetime')); } /** @@ -32,7 +34,7 @@ public function save($session) */ public function delete($id) { - \System\Cache::driver('apc')->forget($id); + Cache::driver('apc')->forget($id); } /** diff --git a/system/session/driver/db.php b/system/session/db.php similarity index 87% rename from system/session/driver/db.php rename to system/session/db.php index 969c642c..aefa7fb7 100644 --- a/system/session/driver/db.php +++ b/system/session/db.php @@ -1,6 +1,8 @@ -get($id); + return Cache::driver('memcached')->get($id); } /** @@ -21,7 +24,7 @@ public function load($id) */ public function save($session) { - \System\Cache::driver('memcached')->put($session['id'], $session, \System\Config::get('session.lifetime')); + Cache::driver('memcached')->put($session['id'], $session, Config::get('session.lifetime')); } /** @@ -32,7 +35,7 @@ public function save($session) */ public function delete($id) { - \System\Cache::driver('memcached')->forget($id); + Cache::driver('memcached')->forget($id); } /** diff --git a/system/view.php b/system/view.php index bcfc9750..b4e3e5f6 100644 --- a/system/view.php +++ b/system/view.php @@ -23,6 +23,13 @@ class View { */ public $path; + /** + * The view composers. + * + * @var array + */ + private static $composers; + /** * Create a new view instance. * @@ -46,7 +53,14 @@ public function __construct($view, $data = array()) */ public static function make($view, $data = array()) { - return new static($view, $data); + 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; } /** @@ -75,7 +89,6 @@ public static function of($view, $data = array()) */ public function get() { - // Get the evaluated content of all of the sub-views. foreach ($this->data as &$data) { if ($data instanceof View or $data instanceof Response)