Skip to content
Snippets Groups Projects
Commit 2b8dd974 authored by Patrick Barroca's avatar Patrick Barroca
Browse files

Loading model configuration from ini file

parent d5274b51
Branches
No related merge requests found
......@@ -4,6 +4,7 @@
*/
$base_path = realpath(dirname(__FILE__));
set_include_path($base_path . '/library' . PATH_SEPARATOR .
$base_path . '/library/storm/src' . PATH_SEPARATOR .
$base_path . '/library/storm/zf/library' . PATH_SEPARATOR .
......@@ -12,7 +13,13 @@ set_include_path($base_path . '/library' . PATH_SEPARATOR .
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
$config = new Zend_Config_Ini($base_path . '/config.ini');
set_include_path($config->modelspath . PATH_SEPARATOR .
get_include_path());
Restful_Bootstrap::bootstrap();
Zend_Controller_Front::getInstance()
->run('application/controllers');
\ No newline at end of file
Restful_Model_Configuration::setConfig($config);
var_dump($config->models->get('Class_Album', []));
Zend_Controller_Front::getInstance()->run('application/controllers');
\ No newline at end of file
......@@ -7,15 +7,16 @@ class Restful_Bootstrap {
->throwExceptions(true)
->getRouter()
->removeDefaultRoutes()
->addRoute('restindex',
new Zend_Controller_Router_Route(':model'))
->addRoute('restshow',
new Zend_Controller_Router_Route(':model/:id', ['action' => 'show']))
->addRoute('restedit',
new Zend_Controller_Router_Route(':model/:id/edit', ['action' => 'edit']))
->addRoute('restcatalog',
new Zend_Controller_Router_Route_Static('catalog', ['action' => 'catalog']))
->addRoute('restnew',
new Zend_Controller_Router_Route(':model/new', ['action' => 'new']))
->addRoute('restcatalog',
new Zend_Controller_Router_Route_Static('catalog', ['action' => 'catalog']));
->addRoute('restedit',
new Zend_Controller_Router_Route(':model/:id/edit', ['action' => 'edit']))
->addRoute('restshow',
new Zend_Controller_Router_Route(':model/:id', ['action' => 'show']))
->addRoute('restindex',
new Zend_Controller_Router_Route(':model'))
;
}
}
\ No newline at end of file
......@@ -62,7 +62,7 @@ class Restful_Model {
public function __call($name, $args) {
if ('can' == substr($name, 0, 3))
return $this->_configuration->$name($args);
return call_user_func_array([$this->_configuration, $name], $args);
throw new RuntimeException('Call to unknown method Restful_Model::' . $name);
}
}
\ No newline at end of file
......@@ -5,52 +5,51 @@
class Restful_Model_Configuration {
protected static $_loaded_instances = [];
public static function newFor($model) {
if (!isset(self::$_loaded_instances[$model]))
self::$_loaded_instances[$model] = new self($model);
return self::$_loaded_instances[$model];
}
protected static $_config;
/** @var string */
protected $_model;
protected $_caps = ['index' => false, 'show' => false, 'create' => false,
'update' => false, 'delete' => false];
public function __construct($model) {
$this->_model = $model;
public static function newFor($model) {
if (!isset(self::$_loaded_instances[$model])) {
$caps = self::$_config->models->get($model, null);
$caps = ($caps) ? $caps->toArray() : [];
self::$_loaded_instances[$model] = new self($model, $caps);
}
return self::$_loaded_instances[$model];
}
/** @category testing */
public static function setFor($model, $configuration) {
self::$_loaded_instances[$model] = $configuration;
}
public function canIndex() {
return $this->isInCatalog() && false;
public static function reset() {
self::$_loaded_instances = [];
self::$_config = null;
}
public function canShow() {
return $this->isInCatalog() && false;
public static function setConfig($config) {
self::$_config = $config;
}
public function canCreate() {
return $this->isInCatalog() && false;
}
public function canUpdate() {
return $this->isInCatalog() && false;
}
public function canDelete() {
return $this->isInCatalog() && false;
public function __construct($model, $caps=[]) {
$this->_model = $model;
$this->_caps = array_merge($this->_caps, $caps);
}
public function isInCatalog() {
return false;
public function __call($name, $args) {
if ('can' == substr($name, 0, 3)
&& ($capability = strtolower(substr($name, 3)))
&& isset($this->_caps[$capability]))
return $this->_caps[$capability];
throw new RuntimeException('Call to undefined method Restful_Model_Configuration::' . $name);
}
}
\ No newline at end of file
<?php
abstract class RestfulControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {
use Storm_Test_THelpers;
public function setUp() {
$this->bootstrap = function() {
Restful_Bootstrap::bootstrap();
};
parent::setUp();
Storm_Model_Abstract::unsetLoaders();
}
public function tearDown() {
Storm_Model_Abstract::unsetLoaders();
Restful_Model_Configuration::reset();
parent::tearDown();
}
}
......@@ -14,7 +23,8 @@ class IndexControllerNotInCatalogIndexTest extends RestfulControllerTestCase {
public function setUp() {
parent::setUp();
Model_Testing::beVolatile();
$this->dispatch('/Model_Testing');
Restful_Model_Configuration::setConfig(new Zend_Config(['models' => []]));
$this->dispatch('/Model_Testing', true);
}
......@@ -26,13 +36,31 @@ class IndexControllerNotInCatalogIndexTest extends RestfulControllerTestCase {
/** @test */
public function modelShouldBeModelTesting() {
$this->assertEquals('Model_Testing', $this->getRequest()->getParam('model'));
$this->assertEquals('Model_Testing', $this->_request->getParam('model'));
}
/** @test */
public function shouldReturnEmptyResult() {
$this->assertEquals('[]', $this->getResponse()->getBody());
$this->assertEquals('[]', $this->_response->getBody());
}
}
class IndexControllerInCatalogIndexTest extends RestfulControllerTestCase {
public function setUp() {
parent::setUp();
$this->fixture('Model_Testing', ['id' => 1]);
$this->fixture('Model_Testing', ['id' => 2]);
Restful_Model_Configuration::setConfig(new Zend_Config(['models' => ['Model_Testing' => ['index' => 1]]]));
$this->dispatch('/Model_Testing', true);
}
/** @test */
public function shouldReturn2Results() {
$this->assertEquals(2, count(json_decode($this->_response->getBody())));
}
}
......@@ -41,7 +69,8 @@ class IndexControllerNotInCatalogShowTest extends RestfulControllerTestCase {
public function setUp() {
parent::setUp();
Model_Testing::beVolatile();
$this->dispatch('/Model_Testing/45');
Restful_Model_Configuration::setConfig(new Zend_Config(['models' => []]));
$this->dispatch('/Model_Testing/45', true);
}
......@@ -68,7 +97,8 @@ class IndexControllerNotInCatalogEditTest extends RestfulControllerTestCase {
public function setUp() {
parent::setUp();
Model_Testing::beVolatile();
$this->dispatch('/Model_Testing/45/edit');
Restful_Model_Configuration::setConfig(new Zend_Config(['models' => []]));
$this->dispatch('/Model_Testing/45/edit', true);
}
......
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