Commit: 603547f12d62b8a3b818affc6d66efe179222925

Author: Nate Abele | Date: 2011-03-12 03:56:15 -0500
Beginning refactor of Lab code for latest version of Lithium.
diff --git a/config/bootstrap.php b/config/bootstrap.php index b5f2a18..a64e389 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -6,7 +6,7 @@ * @license http://opensource.org/licenses/bsd-license.php The BSD License */ -use \lithium\data\Connections; +use lithium\data\Connections; Connections::add('li3_lab', array( 'type' => 'http', 'adapter' => 'CouchDb', 'port' => 5984, diff --git a/config/routes.php b/config/routes.php index 3948f23..c41da9b 100644 --- a/config/routes.php +++ b/config/routes.php @@ -6,18 +6,22 @@ * @license http://opensource.org/licenses/bsd-license.php The BSD License */ -use \lithium\net\http\Router; +use lithium\net\http\Router; Router::connect('/lab', array( - 'plugin' => 'li3_lab', 'controller' => 'home', 'action' => 'index' + 'library' => 'li3_lab', 'controller' => 'home', 'action' => 'index' )); + Router::connect('/lab/{:args}.json', array( - 'plugin' => 'li3_lab', 'controller' => 'plugins', 'action' => 'view', 'type' => 'json' + 'library' => 'li3_lab', 'controller' => 'plugins', 'action' => 'view', 'type' => 'json' )); + Router::connect('/lab/download/{:args}.phar.gz', array( - 'plugin' => 'li3_lab', 'controller' => 'plugins', 'action' => 'download' + 'library' => 'li3_lab', 'controller' => 'plugins', 'action' => 'download' )); + Router::connect('/lab/{:controller}/{:action}/{:args}', array( - 'plugin' => 'li3_lab', 'controller' => 'home' + 'library' => 'li3_lab', 'controller' => 'home' )); -?> + +?> \ No newline at end of file diff --git a/controllers/ExtensionsController.php b/controllers/ExtensionsController.php index f66a947..2ed3641 100644 --- a/controllers/ExtensionsController.php +++ b/controllers/ExtensionsController.php @@ -8,8 +8,8 @@ namespace li3_lab\controllers; -use \li3_lab\models\Extension; -use \li3_lab\models\ExtensionView; +use li3_lab\models\Extension; +use li3_lab\models\ExtensionView; /** * Extensions Controller @@ -21,15 +21,11 @@ class ExtensionsController extends \lithium\action\Controller { * Index */ public function index() { - $params = array( + $latest = Extension::all(array( 'conditions'=> array('design' => 'latest', 'view' => 'extensions'), 'order' => array('descending' => 'true'), 'limit' => '10', - ); - $latest = Extension::all($params); - if ($this->request->type == 'json') { - $this->render(array('json' => $latest->to('array'))); - } + )); return compact('latest'); } @@ -40,13 +36,14 @@ class ExtensionsController extends \lithium\action\Controller { */ public function view($id = null) { $conditions = array('id' => $id, 'revs' => 'true'); - if (!empty($this->request->data)) { + + if ($this->request->data) { $conditions['rev'] = $this->request->data['revision']; } $extension = Extension::find('first', compact('conditions')); - if (empty($extension)) { - $this->redirect(array('controller' => 'extensions', 'action' => 'error')); + if (!$extension) { + return $this->redirect(array('controller' => 'extensions', 'action' => 'error')); } if ($this->request->type == 'json') { $this->render(array('json' => compact('extension'))); @@ -55,18 +52,13 @@ class ExtensionsController extends \lithium\action\Controller { } public function add() { - if (empty($this->request->data)) { - $extension = Extension::create(); - } else { - $extension = Extension::create($this->request->data); - if ($extension->save()) { - $this->redirect(array( - 'plugin' => 'li3_lab', 'controller' => 'extensions','action' => 'index')); - } + $extension = Extension::create(); + + if ($this->request->data && $extension->save($this->request->data)) { + $this->redirect(array('library' => 'li3_lab', 'Extensions::index')); } - $url = array('plugin' => 'li3_lab', 'controller' => 'extensions', 'action' => 'add'); - $this->set(compact('url', 'extension')); - $this->render('form'); + $url = array('library' => 'li3_lab', 'Extensions::add'); + $this->render(array('template' => 'form', 'data' => compact('url', 'extension'))); } /** @@ -74,32 +66,15 @@ class ExtensionsController extends \lithium\action\Controller { * @param string $id */ public function edit($id = null) { - if (empty($this->request->data)) { - $conditions = array('id' => $id); - $extension = Extension::find('first', compact('conditions')); - if (isset($extension->error) && $extension->error == 'not_found') { - $this->redirect(array( - 'plugin' => 'li3_lab', 'controller' => 'extensions','action' => 'index' - )); - } - } else { - $extension = Extension::find($this->request->data['id']); - if (isset($extension->error) && $extension->error == 'not_found') { - $this->redirect(array( - 'plugin' => 'li3_lab', 'controller' => 'extensions','action' => 'index' - )); - } elseif ($extension->save($this->request->data)) { - $this->redirect(array( - 'plugin' => 'li3_lab', 'controller' => 'extensions','action' => 'index' - )); - } + if ((!$id) || !($extension = Extension::first($id))) { + return $this->redirect(array('library' => 'li3_lab', 'Extensions::index')); + } + + if ($this->request->data && $extension->save($this->request->data)) { + return $this->redirect(array('library' => 'li3_lab', 'Extensions::index')); } - $url = array( - 'plugin' => 'li3_lab', 'controller' => 'extensions', - 'action' => 'edit', 'args' => array($extension->id) - ); - $this->set(compact('extension', 'url')); - $this->render('form'); + $url = array('library' => 'li3_lab', 'Extensions::edit', 'args' => array($extension->id)); + $this->render(array('template' => 'form', 'data' => compact('extension', 'url'))); } } diff --git a/controllers/PluginsController.php b/controllers/PluginsController.php index 48f49a1..52c3e7c 100644 --- a/controllers/PluginsController.php +++ b/controllers/PluginsController.php @@ -100,6 +100,8 @@ class PluginsController extends \lithium\action\Controller { * @return void */ public function add() { + $plugin = Plugin::create(); + if (!empty($this->request->data['formula'])) { $formula = Formula::create($this->request->data['formula']); } @@ -116,10 +118,7 @@ class PluginsController extends \lithium\action\Controller { $this->verify(); } } - if (empty($plugin)) { - $plugin = Plugin::create(); - } - $url = array('plugin' => 'li3_lab', 'controller' => 'plugins', 'action' => 'add'); + $url = array('library' => 'li3_lab', 'Plugins::add'); return compact('plugin', 'url'); } diff --git a/models/Extension.php b/models/Extension.php index bd92590..32dfdd3 100644 --- a/models/Extension.php +++ b/models/Extension.php @@ -13,13 +13,6 @@ use \lithium\util\Validator; class Extension extends \lithium\data\Model { /** - * public name of the model - * - * @var string - */ - public $alias = 'Extension'; - - /** * Metadata * * @var array Meta data to link the model with the couchdb datasource @@ -84,9 +77,7 @@ class Extension extends \lithium\data\Model { * @return void */ public static function install() { - return \lithium\data\Connections::get( - static::meta('connection'))->put(static::meta('source') - ); + return static::connection()->put(static::meta('source')); } } diff --git a/models/Plugin.php b/models/Plugin.php index f8313f6..fb113e7 100644 --- a/models/Plugin.php +++ b/models/Plugin.php @@ -14,13 +14,6 @@ use \lithium\data\Connections; class Plugin extends \lithium\data\Model { /** - * public name of the model - * - * @var string - */ - public $alias = 'Plugin'; - - /** * Metadata * * @var array Meta data to link the model with the couchdb datasource @@ -46,8 +39,9 @@ class Plugin extends \lithium\data\Model { ) ); - public static function __init(array $options = array()) { - parent::__init($options); + public static function __init() { + parent::__init(); + static::applyFilter('save', function($self, $params, $chain) { $params['record']->type = 'plugins'; if (isset($params['record']->created)) { @@ -57,6 +51,7 @@ class Plugin extends \lithium\data\Model { } return $chain->next($self, $params, $chain); }); + Validator::add('isSource', function ($data, $params, $options) { foreach ($data as $type => $source) { if (in_array($type, $options['types'])) { @@ -65,7 +60,9 @@ class Plugin extends \lithium\data\Model { return false; } }); - $self = static::_instance(); + + $self = static::_object(); + Validator::add('isUnique', function ($data, $params, $options) use ($self) { $plugin = $self::find('first', array( 'conditions' => array( @@ -86,7 +83,7 @@ class Plugin extends \lithium\data\Model { * @return void */ public static function install() { - return Connections::get(static::meta('connection'))->put(static::meta('source')); + return static::connection()->put(static::meta('source')); } } diff --git a/tests/cases/controllers/ServerControllerTest.php b/tests/cases/controllers/ServerControllerTest.php index b3b2058..68546e5 100644 --- a/tests/cases/controllers/ServerControllerTest.php +++ b/tests/cases/controllers/ServerControllerTest.php @@ -2,13 +2,13 @@ namespace li3_lab\tests\cases\controllers; -use \lithium\data\Connections; -use \li3_lab\controllers\ServerController; -use \lithium\action\Request; -use \lithium\action\Response; -use \li3_lab\models\Repo; -use \li3_lab\models\Formula; -use \li3_lab\models\Plugin; +use lithium\data\Connections; +use li3_lab\controllers\ServerController; +use lithium\action\Request; +use lithium\action\Response; +use li3_lab\models\Repo; +use li3_lab\models\Formula; +use li3_lab\models\Plugin; class ServerControllerTest extends \lithium\test\Unit { @@ -25,26 +25,31 @@ class ServerControllerTest extends \lithium\test\Unit { $this->server->response = new Response(); Repo::meta('connection', 'test_resources'); - Repo::invokeMethod('_connection')->describe(Repo::meta('source')); + Repo::connection()->describe(Repo::meta('source')); + Formula::meta('connection', 'test_resources'); - Formula::invokeMethod('_connection')->describe(Formula::meta('source')); + Formula::connection()->describe(Formula::meta('source')); + Plugin::meta('source', 'test_li3_lab'); - Plugin::invokeMethod('_connection')->describe(Plugin::meta('source')); + Plugin::connection()->describe(Plugin::meta('source')); } public function tearDown() { Repo::meta('connection', 'resources'); - Repo::invokeMethod('_connection')->describe(Repo::meta('source')); + Repo::connection()->describe(Repo::meta('source')); + Formula::meta('connection', 'resources'); - Formula::invokeMethod('_connection')->describe(Formula::meta('source')); + Formula::connection()->describe(Formula::meta('source')); + Plugin::meta('source', 'li3_lab'); - Plugin::invokeMethod('_connection')->describe(Plugin::meta('source')); + Plugin::connection()->describe(Plugin::meta('source')); } public function testReceive() { $this->request->data['phar'] = array( - 'name' => 'li3_example.phar.gz', 'type' => 'application/phar', - 'tmp_name' => $this->_fixturesPath . '/fixtures/plugins/li3_example.phar.gz', + 'name' => 'li3_example.phar.gz', + 'type' => 'application/phar', + 'tmp_name' => "{$this->_fixturesPath}/fixtures/plugins/li3_example.phar.gz", ); $this->server->receive(); diff --git a/views/extensions/form.html.php b/views/extensions/form.html.php index f7391ce..a2d2ce6 100644 --- a/views/extensions/form.html.php +++ b/views/extensions/form.html.php @@ -1,11 +1,11 @@ -<h2><?=$this->title('Add Extension');?></h2> +<h2><?=$this->title('Add Extension'); ?></h2> <?php $errors = $extension->errors(); ?> <div id="add-form" class="tab-page"> <?php -echo $this->form->create($extension, array('method' => 'POST','url' => $url)); +echo $this->form->create($extension, array('method' => 'POST', 'url' => $url)); $this->form->config(array('templates' => array('checkbox' => '<input type="hidden" name="{:name}" value="0" /> diff --git a/views/home/index.html.php b/views/home/index.html.php index 120a4d9..29839d5 100644 --- a/views/home/index.html.php +++ b/views/home/index.html.php @@ -4,8 +4,7 @@ <div class="column latest-plugins"> <h2> <?php echo $this->html->link('Latest Plugins', array( - 'plugin' => 'li3_lab', - 'controller' => 'plugins', + 'controller' => 'li3_lab.Plugins', 'action' => 'index' )); ?> </h2> @@ -14,8 +13,7 @@ <?php foreach ($latestPlugins as $plugin) { ?> <li> <?php echo $this->html->link($plugin->name, array( - 'plugin' => 'li3_lab', - 'controller' => 'plugins', + 'controller' => 'li3_lab.Plugins', 'action' => 'view', 'args' => ($plugin->id) ));?> @@ -23,8 +21,7 @@ <?php } ?> <li> <?php echo $this->html->link('view all plugins', array( - 'plugin' => 'li3_lab', - 'controller' => 'plugins', + 'controller' => 'li3_lab.plugins', 'action' => 'index' )); ?> </li> @@ -35,27 +32,25 @@ <div class="column latest-extensions"> <h2> <?php echo $this->html->link('Latest Extensions', array( - 'plugin' => 'li3_lab', - 'controller' => 'extensions', - 'action' => 'index' + 'controller' => 'li3_lab.Extensions', + 'action' => 'index', + 'args' => array() )); ?> </h2> - <?php if (!empty($latestExtensions)) { ?> + <?php if ($latestExtensions) { ?> <ul> <?php foreach ($latestExtensions as $extension) { ?> <li> <?php echo $this->html->link($extension->name, array( - 'plugin' => 'li3_lab', - 'controller' => 'extensions', - 'action' => 'view', - 'args' => ($extension->id) - ));?> + 'controller' => 'li3_lab.Extensions', + 'action' => 'view', + 'args' => ($extension->id) + )); ?> </li> <?php } ?> <li> <?php echo $this->html->link('view all extensions', array( - 'plugin' => 'li3_lab', - 'controller' => 'extensions', + 'controller' => 'li3_lab.Extensions', 'action' => 'index' )); ?> </li> diff --git a/views/layouts/default.html.php b/views/layouts/default.html.php index f9ad458..545796a 100644 --- a/views/layouts/default.html.php +++ b/views/layouts/default.html.php @@ -19,16 +19,14 @@ 'http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js', 'http://lithify.me/js/jquery.input_list.js' ));?> - <script type="text/javascript"> - <?=$this->scripts();?> - </script> + <?=$this->scripts(); ?> </head> <body> <div id="wrapper"> - <div id="container" class="<?php echo (! empty($home))? 'home' : 'internal' ; ?>"> + <div id="container" class="<?php echo (!empty($home))? 'home' : 'internal' ; ?>"> <div id="header"> <h1><?php echo $this->html->link('Lithium Laboratory', array( - 'plugin' => 'li3_lab', + 'library' => 'li3_lab', 'controller' => 'home', 'action' => 'index' )); ?></h1> @@ -36,23 +34,23 @@ <ul> <li class="add-plugin"> <?php echo $this->html->link('Add Plugin', array( - 'plugin' => 'li3_lab', 'controller' => 'plugins', 'action' => 'add' - ));?> + 'library' => 'li3_lab', 'Plugins::add' + )); ?> </li> <li class="add-extension"> <?php echo $this->html->link('Add Extension', array( - 'plugin' => 'li3_lab', 'controller' => 'extensions', 'action' => 'add' - ));?> + 'library' => 'li3_lab', 'Extensions::add' + )); ?> </li> <li class="all-plugins"> <?php echo $this->html->link('View Plugins', array( - 'plugin' => 'li3_lab', 'controller' => 'plugins', 'action' => 'index' - ));?> + 'library' => 'li3_lab', 'Plugins::index' + )); ?> </li> <li class="all-extensions"> <?php echo $this->html->link('View Extensions', array( - 'plugin' => 'li3_lab', 'controller' => 'extensions', 'action' => 'index' - ));?> + 'library' => 'li3_lab', 'Extensions::index' + )); ?> </li> </ul> </div> diff --git a/views/plugins/add.html.php b/views/plugins/add.html.php index 024c6da..dcd0b9f 100644 --- a/views/plugins/add.html.php +++ b/views/plugins/add.html.php @@ -19,10 +19,8 @@ } ?> </div> - <?php - echo $this->form->submit('save'); - echo $this->form->submit('cancel', array('value' => 'cancel')); - ?> + <?=$this->form->submit('save'); ?> + <?=$this->form->submit('cancel', array('value' => 'cancel')); ?> </form> </div> @@ -30,18 +28,16 @@ <h3>Upload JSON</h3> <?php echo $this->form->create($plugin, array( 'method' => 'POST', 'url' => $url, 'enctype' => 'multipart/form-data' - ));?> + )); ?> <input type="file" name="formula"> - <?php - echo $this->form->submit('upload'); - echo $this->form->submit('cancel', array('value' => 'cancel')); - ?> + <?=$this->form->submit('upload'); ?> + <?=$this->form->submit('cancel', array('value' => 'cancel')); ?> </form> </div> <div id="add-form" class="tab-page"> <h3>New Plugin Form</h3> -<?php echo $this->form->create($plugin, array('method' => 'POST', 'url' => $url));?> +<?php echo $this->form->create($plugin, array('method' => 'POST', 'url' => $url)); ?> <div class="input"> <?php @@ -107,25 +103,25 @@ </form> </div> -<?php -$this->scripts( -<<<'script' - $(document).ready(function() { - $(".tab-page").hide().addClass("hidden"); - $("ul.tabs li a").click(function() { - var id = $(this).attr("id"); - var page = id.match(/(.*)\-tab/); - page = "#"+page[1]; - if ($(page).hasClass("hidden")) { - $(".tab-page").hide().addClass("hidden"); - $(page).show().removeClass("hidden"); - } - return false; - }); - $("#paste-json").show().removeClass("hidden"); - $(".sources").input_list(); - $(".maintainers").input_list(); + +<?php ob_start(); ?> +<script type="text/javascript"> +$(document).ready(function() { + $(".tab-page").hide().addClass("hidden"); + $("ul.tabs li a").click(function() { + var id = $(this).attr("id"); + var page = id.match(/(.*)\-tab/); + page = "#"+page[1]; + if ($(page).hasClass("hidden")) { + $(".tab-page").hide().addClass("hidden"); + $(page).show().removeClass("hidden"); + } + return false; }); -script -); -?> \ No newline at end of file + $("#paste-json").show().removeClass("hidden"); + $(".sources").input_list(); + $(".maintainers").input_list(); +}); +</script> + +<?php $this->scripts(ob_get_clean()); ?>