Commit dbdd45d9 authored by Taylor Otwell's avatar Taylor Otwell

tweaked cache and session namespacing.

parent 5b285882
<?php
return array(
/*
|--------------------------------------------------------------------------
| View Composers
|--------------------------------------------------------------------------
|
| View composers provide a convenient way to add common elements to a view
| each time it is created. For example, you may wish to bind a header and
| footer partial each time the view is created.
|
| The composer will receive an instance of the view being created, and is
| free to modify the view however you wish. But, always return the view
| instance at the end of the composer.
|
| For more information, check out: http://laravel.com/docs/start/views#composers
|
*/
'home/index' => function($view)
{
return $view;
},
);
\ No newline at end of file
......@@ -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
*/
......
<?php namespace System;
class Asset {
/**
* All of the asset containers.
*
* @var array
*/
public static $containers = array();
/**
* Get an asset container instance.
*
* @param string $container
* @return Container
*/
public static function container($container = 'default')
{
if ( ! isset(static::$containers[$container]))
{
static::$containers[$container] = new Asset\Container($container);
}
return static::$containers[$container];
}
/**
* Magic Method for calling methods on the default Asset container.
*/
public static function __callStatic($method, $parameters)
{
return call_user_func_array(array(static::container(), $method), $parameters);
}
}
\ No newline at end of file
<?php namespace System\Asset;
use System\HTML;
class Container {
/**
* The asset container name.
*
* @var string
*/
public $name;
/**
* All of the registered assets.
*
* @var array
*/
public $assets = array();
/**
* Create a new asset container instance.
*
* @param string $name
* @return void
*/
public function __construct($name)
{
$this->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
<?php namespace System\Cache\Driver;
<?php namespace System\Cache;
use System\Config;
class APC implements \System\Cache\Driver {
class APC implements Driver {
/**
* Determine if an item exists in the cache.
......
<?php namespace System\Cache\Driver;
<?php namespace System\Cache;
class File implements \System\Cache\Driver {
class File implements Driver {
/**
* Determine if an item exists in the cache.
......
<?php namespace System\Cache\Driver;
<?php namespace System\Cache;
use System\Config;
class Memcached implements \System\Cache\Driver {
class Memcached implements Driver {
/**
* Determine if an item exists in the cache.
......
......@@ -17,22 +17,29 @@ class HTML {
* Generate a JavaScript reference.
*
* @param string $url
* @param array $attributes
* @return string
*/
public static function script($url)
public static function script($url, $attributes = array())
{
return '<script type="text/javascript" src="'.static::entities(URL::to_asset($url)).'"></script>'.PHP_EOL;
return '<script type="text/javascript" src="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'></script>'.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 '<link href="'.static::entities(URL::to_asset($url)).'" rel="stylesheet" type="text/css" media="'.$media.'">'.PHP_EOL;
if ( ! array_key_exists('media', $attributes))
{
$attributes['media'] = 'all';
}
return '<link href="'.static::entities(URL::to_asset($url)).'" rel="stylesheet" type="text/css"'.static::attributes($attributes).'>'.PHP_EOL;
}
/**
......
<?php namespace System\Session\Driver;
<?php namespace System\Session;
class APC implements \System\Session\Driver {
use System\Cache;
class APC implements Driver {
/**
* Load a session by ID.
......@@ -10,7 +12,7 @@ class APC implements \System\Session\Driver {
*/
public function load($id)
{
return \System\Cache::driver('apc')->get($id);
return Cache::driver('apc')->get($id);
}
/**
......@@ -21,7 +23,7 @@ class APC implements \System\Session\Driver {
*/
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 @@ class APC implements \System\Session\Driver {
*/
public function delete($id)
{
\System\Cache::driver('apc')->forget($id);
Cache::driver('apc')->forget($id);
}
/**
......
<?php namespace System\Session\Driver;
<?php namespace System\Session;
class DB implements \System\Session\Driver {
use System\Config;
class DB implements Driver {
/**
* Load a session by ID.
......@@ -60,7 +62,7 @@ class DB implements \System\Session\Driver {
*/
private function table()
{
return \System\DB::table(\System\Config::get('session.table'));
return \System\DB::table(Config::get('session.table'));
}
}
\ No newline at end of file
<?php namespace System\Session\Driver;
<?php namespace System\Session;
class File implements \System\Session\Driver {
class File implements Driver {
/**
* Load a session by ID.
......
<?php namespace System\Session\Driver;
<?php namespace System\Session;
class Memcached implements \System\Session\Driver {
use System\Cache;
use System\Config;
class Memcached implements Driver {
/**
* Load a session by ID.
......@@ -10,7 +13,7 @@ class Memcached implements \System\Session\Driver {
*/
public function load($id)
{
return \System\Cache::driver('memcached')->get($id);
return Cache::driver('memcached')->get($id);
}
/**
......@@ -21,7 +24,7 @@ class Memcached implements \System\Session\Driver {
*/
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 @@ class Memcached implements \System\Session\Driver {
*/
public function delete($id)
{
\System\Cache::driver('memcached')->forget($id);
Cache::driver('memcached')->forget($id);
}
/**
......
......@@ -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 @@ class View {
*/
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 @@ class View {
*/
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)
......
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