Commit: a4aac55a0d39de9d16c20325255dc3e4a0b947ca

Author: gwoo | Date: 2010-04-03 13:10:49 -0700
updating for 0.8
diff --git a/config/bootstrap.php b/config/bootstrap.php index c112d51..01ee338 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -41,10 +41,22 @@ if (!include LITHIUM_LIBRARY_PATH . '/lithium/core/Libraries.php') { require __DIR__ . '/bootstrap/libraries.php'; /** + * Include this file if your application uses a database connection. + */ +require __DIR__ . '/connections.php'; + +/** + * This file defines bindings between classes which are triggered during the request cycle, and + * allow the framework to automatically configure its environmental settings. You can add your own + * behavior and modify the dispatch cycle to suit your needs. + */ +require __DIR__ . '/bootstrap/action.php'; + +/** * This file contains configurations for connecting to external caching resources, as well as * default caching rules for various systems within your application */ -// require __DIR__ . '/bootstrap/cache.php'; +require __DIR__ . '/bootstrap/cache.php'; /** * This file contains your application's globalization rules, including inflections, @@ -54,45 +66,25 @@ require __DIR__ . '/bootstrap/libraries.php'; // require __DIR__ . '/bootstrap/g11n.php'; /** + * This file contains configurations for handling different content types within the framework, + * including converting data to and from different formats, and handling static media assets. + */ +// require __DIR__ . '/bootstrap/media.php'; + +/** * This file configures console filters and settings, specifically output behavior and coloring. */ // require __DIR__ . '/bootstrap/console.php'; /** * This configures your session storage. The Cookie storage adapter must be connected first, since - * it intercepts any writes where the `'expires'` key is set in the options array. When creating a - * new application, it is suggested that you change the value of `'key'` below. + * it intercepts any writes where the `'expires'` key is set in the options array. */ use \lithium\storage\Session; Session::config(array( - 'default' => array('adapter' => 'Cookie') + 'cookie' => array('adapter' => 'Cookie', 'expire' => '+10 days'), + 'default' => array('adapter' => 'Php') )); - -/** - * The `Collection` class, which serves as the base class for some of Lithium's data objects - * (`RecordSet` and `Document`) provides a way to manage data collections in a very flexible and - * intuitive way, using closures and SPL interfaces. The `to()` method allows a `Collection` (or - * subclass) to be converted to another format, such as an array. The `Collection` class also allows - * other classes to be connected as handlers to convert `Collection` objects to other formats. - * - * The following connects the `Media` class as a format handler, which allows `Collection`s to be - * exported to any format with a handler provided by `Media`, i.e. JSON. This enables things like - * the following: - * {{{ - * $posts = Post::find('all'); - * return $posts->to('json'); - * }}} - */ -use \lithium\util\Collection; - -Collection::formats('\lithium\net\http\Media'); - -/** - * Include Media handling of addtional render types. - */ -require __DIR__ . '/bootstrap/media.php'; - - ?> \ No newline at end of file diff --git a/config/bootstrap/action.php b/config/bootstrap/action.php new file mode 100644 index 0000000..146039b --- /dev/null +++ b/config/bootstrap/action.php @@ -0,0 +1,57 @@ +<?php +/** + * Lithium: the most rad php framework + * + * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org) + * @license http://opensource.org/licenses/bsd-license.php The BSD License + */ + +/** + * This file contains a series of method filters that allow you to intercept different parts of + * Lithium's dispatch cycle. The filters below are used for on-demand loading of routing + * configuration, and automatically configuring the correct environment in which the application + * runs. + * + * For more information on in the filters system, see `lithium\util\collection\Filters`. + * + * @see lithium\util\collection\Filters + */ + +use \lithium\core\Libraries; +use \lithium\net\http\Router; +use \lithium\core\Environment; +use \lithium\action\Dispatcher; + +/** + * This filter loads all application routes in all plugins, loading the default application routes + * last. Change this code if plugin routes must be loaded in a specific order, or if application + * routes must be loaded first (in which case the catch-all routes should be removed). If + * `Dispatcher::run()` is called multiple times in the course of a single request, change the + * `include`s to `include_once`. + * + * @see lithium\net\http\Router + */ +Dispatcher::applyFilter('run', function($self, $params, $chain) { + foreach (array_reverse(Libraries::get()) as $name => $config) { + if ($name === 'lithium') { + continue; + } + $file = "{$config['path']}/config/routes.php"; + file_exists($file) ? include $file : null; + } + return $chain->next($self, $params, $chain); +}); + +/** + * Intercepts the `Dispatcher` as it finds a controller object, and passes the `'request'` parameter + * to the `Environment` class to detect which environment the application is running in. + * + * @see lithium\action\Request + * @see lithium\core\Environment + */ +Dispatcher::applyFilter('_callable', function($self, $params, $chain) { + Environment::set($params['request']); + return $chain->next($self, $params, $chain); +}); + +?> \ No newline at end of file diff --git a/config/bootstrap/cache.php b/config/bootstrap/cache.php index 435ccef..2039220 100644 --- a/config/bootstrap/cache.php +++ b/config/bootstrap/cache.php @@ -6,26 +6,37 @@ * @license http://opensource.org/licenses/bsd-license.php The BSD License */ +/** + * This file creates a default cache configuration using the most optimized adapter available, and + * uses it to provide default caching for high-overhead operations. + */ use lithium\storage\Cache; -use lithium\storage\cache\adapter\Apc; use lithium\core\Libraries; use lithium\action\Dispatcher; +use lithium\storage\cache\adapter\Apc; + +/** + * If APC is not available and the cache directory is not writeable, bail out. + */ +if (!$apcEnabled = Apc::enabled() && !is_writable(LITHIUM_APP_PATH . '/resources/tmp/cache')) { + return; +} Cache::config(array( 'default' => array( - 'adapter' => '\lithium\storage\cache\adapter\\' . (Apc::enabled() ? 'Apc' : 'File') + 'adapter' => '\lithium\storage\cache\adapter\\' . ($apcEnabled ? 'Apc' : 'File') ) )); Dispatcher::applyFilter('run', function($self, $params, $chain) { - if ($cache = Cache::read('default', 'core.libraryCache')) { + if ($cache = Cache::read('default', 'core.libraries')) { $cache = (array) unserialize($cache) + Libraries::cache(); Libraries::cache($cache); } $result = $chain->next($self, $params, $chain); if ($cache != Libraries::cache()) { - Cache::write('default', 'core.libraryCache', serialize(Libraries::cache()), '+1 day'); + Cache::write('default', 'core.libraries', serialize(Libraries::cache()), '+1 day'); } return $result; }); diff --git a/config/bootstrap/g11n.php b/config/bootstrap/g11n.php index 2f10e11..abecb67 100644 --- a/config/bootstrap/g11n.php +++ b/config/bootstrap/g11n.php @@ -75,14 +75,7 @@ Catalog::config(array( 'adapter' => 'Php', 'path' => LITHIUM_LIBRARY_PATH . '/lithium/g11n/resources/php' ) -)); - -/** - * Globalization runtime data. You can add globalized data during runtime utilizing a - * configuration set up to use the _memory_ adapter. - */ -$data = function($n) { return $n != 1 ? 1 : 0; }; -Catalog::write('message.plural', 'root', $data, array('name' => 'runtime')); +) + Catalog::config()); /** * Integration with `Inflector`. diff --git a/config/bootstrap/libraries.php b/config/bootstrap/libraries.php index a96c03b..575daae 100644 --- a/config/bootstrap/libraries.php +++ b/config/bootstrap/libraries.php @@ -48,12 +48,12 @@ Libraries::add('lithium'); * Add the application. You can pass a `'path'` key here if this bootstrap file is outside of * your main application, but generally you should not need to change any settings. */ -Libraries::add('app'); +Libraries::add('app', array('default' => true)); /** * Add some plugins */ -// Libraries::add('plugin', 'li3_docs'); +// Libraries::add('li3_docs'); /** * Adding extra libraries diff --git a/config/bootstrap/media.php b/config/bootstrap/media.php index b950278..e04c51b 100644 --- a/config/bootstrap/media.php +++ b/config/bootstrap/media.php @@ -1,4 +1,5 @@ <?php +<?php /** * Lithium: the most rad php framework * @@ -6,12 +7,59 @@ * @license http://opensource.org/licenses/bsd-license.php The BSD License */ +/** + * The `Collection` class, which serves as the base class for some of Lithium's data objects + * (`RecordSet` and `Document`) provides a way to manage data collections in a very flexible and + * intuitive way, using closures and SPL interfaces. The `to()` method allows a `Collection` (or + * subclass) to be converted to another format, such as an array. The `Collection` class also allows + * other classes to be connected as handlers to convert `Collection` objects to other formats. + * + * The following connects the `Media` class as a format handler, which allows `Collection`s to be + * exported to any format with a handler provided by `Media`, i.e. JSON. This enables things like + * the following: + * {{{ + * $posts = Post::find('all'); + * return $posts->to('json'); + * }}} + */ +use \lithium\util\Collection; + +Collection::formats('\lithium\net\http\Media'); + +/** + * This filter is a convenience method which allows you to automatically route requests for static + * assets stored within active plugins. For example, given a JavaScript file `bar.js` inside the + * `li3_foo` plugin installed in an application, requests to `http://app/path/li3_foo/js/bar.js` + * will be routed to `/path/to/app/libraries/plugins/li3_foo/webroot/js/bar.js` on the filesystem. + * In production, it is recommended that you disable this filter in favor of symlinking each + * plugin's `webroot` directory into your main application's `webroot` directory, or adding routing + * rules in your web server's configuration. + */ +use \lithium\action\Dispatcher; +use \lithium\core\Libraries; use \lithium\net\http\Media; +Dispatcher::applyFilter('_callable', function($self, $params, $chain) { + list($plugin, $asset) = explode('/', $params['request']->url, 2) + array("", ""); + if ($asset && $library = Libraries::get($plugin)) { + $asset = "{$library['path']}/webroot/{$asset}"; + + if (file_exists($asset)) { + return function () use ($asset) { + $info = pathinfo($asset); + $type = Media::type($info['extension']); + header("Content-type: {$type['content']}"); + return file_get_contents($asset); + }; + } + } + return $chain->next($self, $params, $chain); +}); + $default = Media::type('default'); $text = Media::type('text'); Media::type('xml', null, $default['options']); Media::type('txt', null, $text['options']); -?> \ No newline at end of file +?> diff --git a/config/connections.php b/config/connections.php index a83051f..b8611c2 100644 --- a/config/connections.php +++ b/config/connections.php @@ -8,15 +8,19 @@ use \lithium\data\Connections; -Connections::add('default', 'http', array( +Connections::add('default', array( + 'type' => 'http', 'adapter' => 'CouchDb', 'host' => '127.0.0.1', - 'port' => '5984' + 'port' => '5984', + 'database' => 'lithium_bin' )); -Connections::add('test', 'http', array( +Connections::add('test', array( + 'type' => 'http', 'adapter' => 'CouchDb', 'host' => '127.0.0.1', - 'port' => '5984' + 'port' => '5984', + 'database' => 'test_lithium_bin' )); ?> \ No newline at end of file diff --git a/extensions/command/Bin.php b/extensions/command/Bin.php index 0d06d6f..98ce053 100644 --- a/extensions/command/Bin.php +++ b/extensions/command/Bin.php @@ -2,8 +2,8 @@ namespace app\extensions\command; -use \app\models\Paste; use \app\models\PasteView; +use \lithium\data\Connections; /** * Command to assist in setup and management of Lithium Bin @@ -18,20 +18,21 @@ class Bin extends \lithium\console\Command { */ public function install() { $this->header('Lithium Bin'); - $result = Paste::install(); + $config = Connections::get('default', array('config' => true)); + Connections::get('default')->put($config['database']); PasteView::create()->save(); return $this->checkView(); } - - public function update() { + + public function update() { $view = PasteView::find('_design/paste'); if ($view && !isset($view->error)) { $view->delete(); } - PasteView::create()->save(); - return $this->checkView(); + PasteView::create()->save(); + return $this->checkView(); } - + protected function checkView() { $view = PasteView::find('_design/paste'); if (!empty($view->reason)) { diff --git a/models/Paste.php b/models/Paste.php index f15e463..380f377 100644 --- a/models/Paste.php +++ b/models/Paste.php @@ -26,14 +26,6 @@ class Paste extends \lithium\data\Model { public static $languages = null; /** - * Metadata - * - * @var array array of meta data to link the model with the couchdb datasource - * - source : the name of the table (called database in couchdb) - */ - protected $_meta = array('source' => 'lithium_bin'); - - /** * Schema for Paste * * @var array @@ -145,15 +137,6 @@ class Paste extends \lithium\data\Model { } return static::$languages; } - - /** - * Creates a new database - * - * @return void - */ - public static function install() { - return Connections::get('default')->put(static::meta('source')); - } } ?> \ No newline at end of file diff --git a/models/PasteView.php b/models/PasteView.php index c8e3f80..7bcce4a 100644 --- a/models/PasteView.php +++ b/models/PasteView.php @@ -7,34 +7,23 @@ namespace app\models; * It also defines it. Do not call a 'find' on this model. To view the view, use * the 'design' condition in a 'find' call on the `Paste` model, ie : * {{{ - * $latest = Paste::find('all', array( - 'conditions' => array( - * 'design' => 'latest', - 'view' => 'all' - ), - * 'limit' => 10 - * )); + * $latest = Paste::find('all', array( + * 'conditions' => array( + * 'design' => 'paste', 'view' => 'all' + * ), + * 'limit' => 10 + * )); * }}} * * When the find call in the example above returns a NULL, that means the view does not * exist in the `Paste` database. To insert it use: * {{{ - * PasteView::create()->save(); + * PasteView::create()->save(); * }}} */ class PasteView extends \lithium\data\Model { /** - * Metadata - * - * @var array array of meta data to link the model with the couchdb datasource - * - source : the name of the table (called database in couchdb) - */ - protected $_meta = array( - 'source' => 'lithium_bin' - ); - - /** * Predefined views. Only used to store in db if not already there. */ protected $_schema = array( @@ -42,27 +31,24 @@ class PasteView extends \lithium\data\Model { 'language' => array('default' => 'javascript'), 'views' => array('default' => array( 'all' => array( -'map' => 'function(doc) { - if (doc.permanent == "1") { - emit(doc.created, { - author: doc.author, language: doc.language, - preview: doc.preview, created: doc.created - }); - } -}' + 'map' => 'function(doc) { + if (doc.permanent == "1") { + emit(doc.created, { + author: doc.author, language: doc.language, + preview: doc.preview, created: doc.created + }); + } + }' ), 'count' => array( -'map' => 'function(doc) { - if (doc.permanent == "1") { - emit(doc._id, null); - } -}' + 'map' => 'function(doc) { + if (doc.permanent == "1") { + emit(doc._id, null); + } + }' ), - )) ); - - } ?> \ No newline at end of file diff --git a/webroot/index.php b/webroot/index.php index 99026aa..3b79fa0 100644 --- a/webroot/index.php +++ b/webroot/index.php @@ -5,19 +5,34 @@ * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org) * @license http://opensource.org/licenses/bsd-license.php The BSD License */ + /** - * Welcome to Lithium! This front-controller file is the gateway to your application. It is - * responsible for intercepting requests, and handing them off to the Dispatcher for processing. + * Welcome to Lithium! This front-controller file is the gateway to your application. It is + * responsible for intercepting requests, and handing them off to the `Dispatcher` for processing. * * If you're sharing a single Lithium core install or other libraries among multiple - * applications, you may need to manually set things like LITHIUM_LIBRARY_PATH. You can do that in - * app/config/bootstrap.php, which is loaded below: + * applications, you may need to manually set things like `LITHIUM_LIBRARY_PATH`. You can do that in + * `config/bootstrap.php`, which is loaded below: */ require dirname(__DIR__) . '/config/bootstrap.php'; /** - * Dispatch a new request with the default settings. + * The following will instantiate a new `Request` object and pass it off to the `Dispatcher` class. + * By default, the `Request` will automatically aggregate all the server / environment settings, URL + * and query string parameters, request content (i.e. POST or PUT data), and HTTP method and header + * information. + * + * The `Request` is then used by the `Dispatcher` (in conjunction with the `Router`) to determine + * the correct controller to dispatch to, and the correct response type to render. The response + * information is then encapsulated in a `Response` object, which is returned from the controller + * to the `Dispatcher`, and finally echoed below. Echoing a `Response` object causes its headers to + * be written, and its response body to be written in a buffer loop. + * + * @see lithium\action\Request + * @see lithium\action\Response + * @see lithium\action\Dispatcher + * @see lithium\net\http\Router */ -echo lithium\action\Dispatcher::run(); +echo lithium\action\Dispatcher::run(new lithium\action\Request()); ?> \ No newline at end of file