Commit: c5193119023a15b13ac37d5955f2156b635d9754

Author: gwoo | Date: 2010-01-23 17:18:47 -0800
changing models to use type param. Adding LabView to handle setting up couch views.
diff --git a/controllers/ExtensionsController.php b/controllers/ExtensionsController.php index 09fe123..f66a947 100644 --- a/controllers/ExtensionsController.php +++ b/controllers/ExtensionsController.php @@ -22,7 +22,7 @@ class ExtensionsController extends \lithium\action\Controller { */ public function index() { $params = array( - 'conditions'=> array('design' => 'latest', 'view' => 'all'), + 'conditions'=> array('design' => 'latest', 'view' => 'extensions'), 'order' => array('descending' => 'true'), 'limit' => '10', ); diff --git a/controllers/PluginsController.php b/controllers/PluginsController.php index e757400..e495271 100644 --- a/controllers/PluginsController.php +++ b/controllers/PluginsController.php @@ -9,7 +9,6 @@ namespace li3_lab\controllers; use \li3_lab\models\Plugin; -use \li3_lab\models\PluginView; use \li3_lab\models\Formula; /** @@ -23,7 +22,7 @@ class PluginsController extends \lithium\action\Controller { */ public function index() { $params = array( - 'conditions'=> array('design' => 'latest', 'view' => 'all'), + 'conditions'=> array('design' => 'latest', 'view' => 'plugins'), 'order' => array('descending' => 'true'), 'limit' => '10', ); diff --git a/extensions/command/Server.php b/extensions/command/Server.php index 3573f2b..76626d1 100644 --- a/extensions/command/Server.php +++ b/extensions/command/Server.php @@ -3,9 +3,8 @@ namespace li3_lab\extensions\command; use \li3_lab\models\Plugin; -use \li3_lab\models\PluginView; use \li3_lab\models\Extension; -use \li3_lab\models\ExtensionView; +use \li3_lab\models\LabView; /** * Command to assist in setup and management of Lithium Bin @@ -23,15 +22,10 @@ class Server extends \lithium\console\Command { $result = Plugin::install(); $result = $result && Extension::install(); - foreach (array_keys(PluginView::$views) as $view) { - PluginView::create($view)->save(); - $this->_check('\li3_lab\models\PluginView', $view); + foreach (array_keys(LabView::$views) as $view) { + LabView::create($view)->save(); + $this->_check('\li3_lab\models\LabView', $view); } - foreach (array_keys(ExtensionView::$views) as $view) { - ExtensionView::create($view)->save(); - $this->_check('\li3_lab\models\ExtensionView', $view); - } - } protected function _check($model, $name = null) { diff --git a/models/Extension.php b/models/Extension.php index 1ec1c6b..24c389b 100644 --- a/models/Extension.php +++ b/models/Extension.php @@ -25,7 +25,7 @@ class Extension extends \lithium\data\Model { * @var array Meta data to link the model with the couchdb datasource * - source : the name of the table (called database in couchdb) */ - protected $_meta = array('source' => 'li3_lab_extensions', 'connection' => 'li3_lab'); + protected $_meta = array('source' => 'li3_lab', 'connection' => 'li3_lab'); /** * validation rules @@ -45,6 +45,7 @@ class Extension extends \lithium\data\Model { public static function __init($options = array()) { parent::__init($options); static::applyFilter('save', function($self, $params, $chain) { + $params['record']->type = 'extensions'; if (isset($params['record']->created)) { $params['record']->modified = date('Y-m-d h:i:s'); } else { diff --git a/models/ExtensionView.php b/models/ExtensionView.php deleted file mode 100644 index 5260de2..0000000 --- a/models/ExtensionView.php +++ /dev/null @@ -1,75 +0,0 @@ -<?php -/** - * Li3 Lab: consume and distribute plugins for the most rad php framework - * - * @copyright Copyright 2009, Union of RAD (http://union-of-rad.org) - * @license http://opensource.org/licenses/bsd-license.php The BSD License - */ - -namespace li3_lab\models; - -/** - * - */ -class ExtensionView extends \lithium\data\Model { - - /** - * Metadata - * - * @var array Meta data to link the model with the couchdb datasource - * - source : the name of the table (called database in couchdb) - */ - protected $_meta = array('source' => 'li3_lab_extensions', 'connection' => 'li3_lab'); - - - /** - * Predefined views. Only used to store in db if not already there. - */ - public static $views = array( - 'latest' => array( - 'id' => '_design/latest', - 'language' => 'javascript', - 'views' => array( - 'all' => array( - 'map' => 'function(doc) { - emit(doc.created, doc); - }' - ) - ) - ), - 'search' => array( - '_id' => '_design/search', - 'language' => 'javascript', - 'views' => array( - 'words' => array( - 'map' => 'function(doc) { - var txt = doc.name + " " + doc.summary + " " + doc.description; - var words = txt.replace(/[!.,;]+/g,"").replace(/[\n\r]+/g, " ").toLowerCase().split(" "); - var cache = {}; - for (var word in words) { - if (word.length !== 0) { - emit(words[word], doc); - } - } - }', - ) - ) - ) - ); - - /** - * Create a PluginView instance of Document - * Unlike Model::create, this takes a string name of a predefined design view - * - * @param string $data 'lastest' is only valid and default - * @return Document - */ - public static function create($data = 'latest') { - if (!isset(static::$views[$data])) { - return false; - } - return parent::create(static::$views[$data]); - } -} - -?> \ No newline at end of file diff --git a/models/LabView.php b/models/LabView.php new file mode 100644 index 0000000..d2da745 --- /dev/null +++ b/models/LabView.php @@ -0,0 +1,84 @@ +<?php +/** + * Li3 Lab: consume and distribute plugins for the most rad php framework + * + * @copyright Copyright 2009, Union of RAD (http://union-of-rad.org) + * @license http://opensource.org/licenses/bsd-license.php The BSD License + */ + +namespace li3_lab\models; + +/** + * This model is used to store Couch design views to the `Plugin` database + * 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 `Plugin` model, ie : + * {{{ + * $latest = Plugin::find('all', array( + * 'conditions' => array('design' => 'latest', 'view' => 'all'), 'limit' => 10 + * )); + * }}} + * + * When the find call in the example above returns a NULL, that means the view does not + * exist in the `Plugin` database. To insert it use: + * {{{ + * LabView::create()->save(); + * }}} + */ +class LabView extends \lithium\data\Model { + + /** + * Metadata + * + * @var array Meta data to link the model with the couchdb datasource + * - source : the name of the table (called database in couchdb) + */ + protected $_meta = array('source' => 'li3_lab', 'connection' => 'li3_lab'); + + + /** + * Predefined views. Only used to store in db if not already there. + */ + public static $views = array( + 'latest' => array( + 'id' => '_design/latest', + 'language' => 'javascript', + 'views' => array( + 'all' => array( + 'map' => 'function(doc) { + emit(doc.created, doc); + }' + ), + 'plugins' => array( + 'map' => 'function(doc) { + if (doc.type == "plugins") { + emit(doc.created, doc); + } + }' + ), + 'extensions' => array( + 'map' => 'function(doc) { + if (doc.type == "extensions") { + emit(doc.created, doc); + } + }' + ) + ) + ) + ); + + /** + * Create a LabView instance of Document + * Unlike Model::create, this takes a string name of a predefined design view + * + * @param string $data 'lastest' is only valid and default + * @return Document + */ + public static function create($data = 'latest') { + if (!isset(static::$views[$data])) { + return false; + } + return parent::create(static::$views[$data]); + } +} + +?> \ No newline at end of file diff --git a/models/Plugin.php b/models/Plugin.php index 0a414ac..e58ab02 100644 --- a/models/Plugin.php +++ b/models/Plugin.php @@ -26,7 +26,7 @@ class Plugin extends \lithium\data\Model { * @var array Meta data to link the model with the couchdb datasource * - source : the name of the table (called database in couchdb) */ - protected $_meta = array('source' => 'li3_lab_plugins', 'connection' => 'li3_lab'); + protected $_meta = array('source' => 'li3_lab', 'connection' => 'li3_lab'); /** * validation rules @@ -42,6 +42,15 @@ class Plugin extends \lithium\data\Model { public static function __init($options = array()) { parent::__init($options); + static::applyFilter('save', function($self, $params, $chain) { + $params['record']->type = 'plugins'; + if (isset($params['record']->created)) { + $params['record']->modified = date('Y-m-d h:i:s'); + } else { + $params['record']->created = date('Y-m-d h:i:s'); + } + return $chain->next($self, $params, $chain); + }); Validator::add('isSource', function ($data, $params, $options) { $types = array('git', 'phar'); foreach ($data as $type => $source) { @@ -53,19 +62,6 @@ class Plugin extends \lithium\data\Model { } /** - * undocumented function - * - * @param string $data - * @return void - */ - public static function create($data = array()) { - if (!isset($data['created'])) { - $data['created'] = date('Y-m-d h:i:s'); - } - return parent::create($data); - } - - /** * Creates a new database * * @return void diff --git a/models/PluginView.php b/models/PluginView.php deleted file mode 100644 index 4ffbfc1..0000000 --- a/models/PluginView.php +++ /dev/null @@ -1,89 +0,0 @@ -<?php -/** - * Li3 Lab: consume and distribute plugins for the most rad php framework - * - * @copyright Copyright 2009, Union of RAD (http://union-of-rad.org) - * @license http://opensource.org/licenses/bsd-license.php The BSD License - */ - -namespace li3_lab\models; - -/** - * This model is used to store Couch design views to the `Plugin` database - * 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 `Plugin` model, ie : - * {{{ - * $latest = Plugin::find('all', array('conditions' => array( - * 'design' => 'latest, - * 'limit' => 10 - * ))); - * }}} - * - * When the find call in the example above returns a NULL, that means the view does not - * exist in the `Plugin` database. To insert it use: - * {{{ - * PluginView::create()->save(); - * }}} - */ -class PluginView extends \lithium\data\Model { - - /** - * Metadata - * - * @var array Meta data to link the model with the couchdb datasource - * - source : the name of the table (called database in couchdb) - */ - protected $_meta = array('source' => 'li3_lab_plugins', 'connection' => 'li3_lab'); - - - /** - * Predefined views. Only used to store in db if not already there. - */ - public static $views = array( - 'latest' => array( - 'id' => '_design/latest', - 'language' => 'javascript', - 'views' => array( - 'all' => array( - 'map' => 'function(doc) { - emit(doc.created, doc); - }' - ) - ) - ), - 'search' => array( - '_id' => '_design/search', - 'language' => 'javascript', - 'views' => array( - 'words' => array( - 'map' => 'function(doc) { - var txt = doc.name + " " + doc.summary + " " + doc.description; - var words = txt.replace(/[!.,;]+/g,"").replace(/[\n\r]+/g, " ").toLowerCase().split(" "); - var cache = {}; - for (var word in words) { - if (word.length !== 0) { - emit(words[word], doc); - } - } - }', - ) - ) - ) - ); - - /** - * Create a PluginView instance of Document - * Unlike Model::create, this takes a string name of a predefined design view - * - * @param string $data 'lastest' is only valid and default - * @return Document - */ - public static function create($data = 'latest') { - if (!isset(static::$views[$data])) { - return false; - } - return parent::create(static::$views[$data]); - } -} - -?> \ No newline at end of file diff --git a/tests/cases/extensions/command/LabTest.php b/tests/cases/extensions/command/LabTest.php deleted file mode 100644 index 7e6defc..0000000 --- a/tests/cases/extensions/command/LabTest.php +++ /dev/null @@ -1,155 +0,0 @@ -<?php -/** - * Li3 Lab: consume and distribute plugins for the most rad php framework - * - * @copyright Copyright 2009, Union of RAD (http://union-of-rad.org) - * @license http://opensource.org/licenses/bsd-license.php The BSD License - */ - -namespace li3_lab\tests\cases\extensions\command; - -use \Phar; -use \lithium\console\Dispatcher; -use \lithium\console\Request; -use \lithium\action\Request as ActionRequest; -use \li3_lab\extensions\command\Lab; - -class LabTest extends \lithium\test\Unit { - - public $lab = null; - - protected $_conf = null; - - protected $_installPath = null; - - protected $_fixturesPath = null; - - public function setUp() { - $this->_fixturesPath = dirname(dirname(dirname(__DIR__))) . '/fixtures/plugins'; - $this->_installPath = LITHIUM_APP_PATH . '/resources/tmp/tests'; - $this->_conf = LITHIUM_APP_PATH . '/resources/tmp/tests/li3_lab.ini'; - Dispatcher::applyFilter('_call', function($self, $params, $chain) { - $result = $chain->next($self, $params, $chain); - if (!$result) { - return false; - } - return $params['callable']; - }); - - $classes = array( - 'service' => '\li3_lab\tests\mocks\extensions\command\MockService', - 'response' => '\lithium\tests\mocks\console\MockResponse' - ); - $request = new Request(); - $this->lab = new Lab(compact('request', 'classes')); - } - - public function tearDown() { - if (file_exists($this->_conf)) { - // /unlink($this->_conf); - } - } - - public function testBasicInit() { - $lab = Dispatcher::run(new Request(array( - 'args' => array( - '\li3_lab\extensions\command\Lab', - 'init', "--conf={$this->_conf}", - ) - ))); - $result = file_exists($this->_conf); - $this->assertTrue($result); - - $expected = array('servers' => array('lab.lithify.me' => true)); - $result = parse_ini_file($this->_conf, true); - $this->assertEqual($expected, $result); - } - - public function testServerConfig() { - $lab = Dispatcher::run(new Request(array( - 'args' => array( - '\li3_lab\extensions\command\Lab', - 'config', "--conf={$this->_conf}", 'server', 'incubator.lithify.me' - ) - ))); - - $expected = array('servers' => array( - 'lab.lithify.me' => true, - 'incubator.lithify.me' => true - )); - $result = parse_ini_file($this->_conf, true); - $this->assertEqual($expected, $result); - } - - public function testCreate() { - $this->lab->path = $this->_installPath; - $this->lab->original = $this->_fixturesPath; - $result = $this->lab->create('li3_example'); - $this->assertTrue($result); - - $result = file_exists($this->_installPath . '/li3_example.phar.gz'); - $this->assertTrue($result); - - $expected = 2000; - $result = filesize($this->_installPath . '/li3_example.phar.gz'); - $this->assertTrue($expected < $result); - - Phar::unlinkArchive($this->_installPath . '/li3_example.phar'); - Phar::unlinkArchive($this->_installPath . '/li3_example.phar.gz'); - $this->_cleanUp(); - } - - public function testPush() { - $this->lab->path = $this->_installPath; - $this->lab->original = $this->_fixturesPath; - $result = $this->lab->create('li3_example'); - $this->assertTrue($result); - - $result = file_exists($this->_installPath . '/li3_example.phar.gz'); - $this->assertTrue($result); - - $request = new ActionRequest(); - $this->lab->server = $request->env('HTTP_HOST') . $request->env('base'); - $result = $this->lab->push('li3_example'); - - $result = is_dir($this->_installPath . '/li3_example'); - $this->assertTrue($result); - } - - public function testAdd() { - $this->lab->path = $this->_installPath; - $result = $this->lab->add('li3_example'); - $this->assertTrue($result); - - $result = file_exists($this->_installPath . '/li3_example.phar'); - $this->assertTrue($result); - - $result = is_dir($this->_installPath . '/li3_example'); - $this->assertTrue($result); - $this->_cleanUp(); - } - - public function testFind() { - $this->lab->run(); - -$expected = <<<EOD --------------------------------------------------------------------------------- -lab.lithify.me > li3_lab --------------------------------------------------------------------------------- -the li3 plugin client/server -Version: 1.0 -Created: 2009-11-30 --------------------------------------------------------------------------------- -lab.lithify.me > li3_example --------------------------------------------------------------------------------- -an li3 plugin example -Version: 1.0 -Created: 2009-11-30 - -EOD; - $result = $this->lab->response->output; - $this->assertEqual($expected, $result); - } -} - -?> \ No newline at end of file