From b2c16229f7e41120777382e6d86c065df638c9dd Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sat, 25 Sep 2010 00:42:07 +0200 Subject: [PATCH] moved json definitions to capabilities controller --- .../lib/Controller/AggregatorController.php | 4 +- .../lib/Controller/CapabilitiesController.php | 56 +++++++++++++++++++ backend/lib/Controller/ChannelController.php | 4 +- backend/lib/Controller/EntityController.php | 4 +- .../lib/{Model => Definition}/Definition.php | 10 ++-- .../lib/Definition/EntityDefinition.json | 0 .../EntityDefinition.php | 4 +- .../lib/Definition/PropertyDefinition.json | 2 +- .../PropertyDefinition.php | 8 +-- backend/lib/Model/Entity.php | 6 +- backend/lib/Model/Property.php | 6 +- backend/lib/Router.php | 3 +- backend/lib/View/JSON.php | 41 +++++++++++--- backend/lib/View/View.php | 35 +++++------- 14 files changed, 136 insertions(+), 47 deletions(-) create mode 100644 backend/lib/Controller/CapabilitiesController.php rename backend/lib/{Model => Definition}/Definition.php (92%) rename share/definitions/entities.json => backend/lib/Definition/EntityDefinition.json (100%) rename backend/lib/{Model => Definition}/EntityDefinition.php (95%) rename share/definitions/properties.json => backend/lib/Definition/PropertyDefinition.json (99%) rename backend/lib/{Model => Definition}/PropertyDefinition.php (94%) diff --git a/backend/lib/Controller/AggregatorController.php b/backend/lib/Controller/AggregatorController.php index 17f8de6..b8d6746 100644 --- a/backend/lib/Controller/AggregatorController.php +++ b/backend/lib/Controller/AggregatorController.php @@ -29,6 +29,8 @@ namespace Volkszaehler\Controller; * @author Steffen Vogel (info@steffenvogel.de) * @package default */ +use Volkszaehler\Definition; + use Volkszaehler\Model; class AggregatorController extends EntityController { @@ -65,7 +67,7 @@ class AggregatorController extends EntityController { $aggregator = new Model\Aggregator('group'); // TODO support for other aggregator types foreach ($this->view->request->getParameters() as $parameter => $value) { - if (Model\PropertyDefinition::exists($parameter)) { + if (Definition\PropertyDefinition::exists($parameter)) { $aggregator->setProperty($parameter, $value); } } diff --git a/backend/lib/Controller/CapabilitiesController.php b/backend/lib/Controller/CapabilitiesController.php new file mode 100644 index 0000000..29e30d2 --- /dev/null +++ b/backend/lib/Controller/CapabilitiesController.php @@ -0,0 +1,56 @@ +. + */ + +namespace Volkszaehler\Controller; + +use Volkszaehler\Model; +use Volkszaehler\Util; + +/** + * Capabilities controller + * + * @author Steffen Vogel + * @package default + */ +class CapabilitiesController extends Controller { + + /** + * @todo + * @param string $capabilities + * @param string $sub + */ + public function get($capabilities, $sub) { + if ($capabilities == 'definition' && in_array($sub, array('property', 'entity'))) { + $class = 'Volkszaehler\Definition\\' . ucfirst($sub) . 'Definition'; + $json = $class::getJSON(); + $this->view->add(array('definition' => array($sub => $json))); + } + elseif ($capabilities == 'version') { + } + else { + throw new \Exception('Unknown capability information: ' . implode('/', func_get_args())); + } + } +} + +?> diff --git a/backend/lib/Controller/ChannelController.php b/backend/lib/Controller/ChannelController.php index 69dfb33..d1b7dd8 100644 --- a/backend/lib/Controller/ChannelController.php +++ b/backend/lib/Controller/ChannelController.php @@ -23,6 +23,8 @@ namespace Volkszaehler\Controller; +use Volkszaehler\Definition; + use Volkszaehler\Model; /** @@ -53,7 +55,7 @@ class ChannelController extends EntityController { $channel = new Model\Channel($this->view->request->getParameter('type')); foreach ($this->view->request->getParameters() as $parameter => $value) { - if (Model\PropertyDefinition::exists($parameter)) { + if (Definition\PropertyDefinition::exists($parameter)) { $channel->setProperty($parameter, $value); } } diff --git a/backend/lib/Controller/EntityController.php b/backend/lib/Controller/EntityController.php index b3c3a50..683311f 100644 --- a/backend/lib/Controller/EntityController.php +++ b/backend/lib/Controller/EntityController.php @@ -23,6 +23,8 @@ namespace Volkszaehler\Controller; +use Volkszaehler\Definition; + use Volkszaehler\Util; use Volkszaehler\Model; @@ -75,7 +77,7 @@ class EntityController extends Controller { $entity = $this->get($identifier); foreach ($this->view->request->getParameters() as $parameter => $value) { - if (Model\PropertyDefinition::exists($parameter)) { + if (Definition\PropertyDefinition::exists($parameter)) { if ($value == '') { $entity->unsetProperty($parameter, $this->em); } diff --git a/backend/lib/Model/Definition.php b/backend/lib/Definition/Definition.php similarity index 92% rename from backend/lib/Model/Definition.php rename to backend/lib/Definition/Definition.php index ed6a6de..ffc8156 100644 --- a/backend/lib/Model/Definition.php +++ b/backend/lib/Definition/Definition.php @@ -21,7 +21,7 @@ * along with volkszaehler.org. If not, see . */ -namespace Volkszaehler\Model; +namespace Volkszaehler\Definition; use Volkszaehler\Util; @@ -90,14 +90,16 @@ abstract class Definition { * Load JSON definitions from file (via lazy loading from get()) */ protected static function load() { - $json = Util\JSON::decode(file_get_contents(VZ_DIR . static::FILE)); - static::$definitions = array(); - foreach ($json as $property) { + foreach (self::getJSON() as $property) { static::$definitions[$property->name] = new static($property); } } + + public static function getJSON() { + return Util\JSON::decode(file_get_contents(VZ_BACKEND_DIR . static::FILE)); + } } ?> \ No newline at end of file diff --git a/share/definitions/entities.json b/backend/lib/Definition/EntityDefinition.json similarity index 100% rename from share/definitions/entities.json rename to backend/lib/Definition/EntityDefinition.json diff --git a/backend/lib/Model/EntityDefinition.php b/backend/lib/Definition/EntityDefinition.php similarity index 95% rename from backend/lib/Model/EntityDefinition.php rename to backend/lib/Definition/EntityDefinition.php index 85ff2eb..423b619 100644 --- a/backend/lib/Model/EntityDefinition.php +++ b/backend/lib/Definition/EntityDefinition.php @@ -21,7 +21,7 @@ * along with volkszaehler.org. If not, see . */ -namespace Volkszaehler\Model; +namespace Volkszaehler\Definition; use Volkszaehler\Util; @@ -35,7 +35,7 @@ class EntityDefinition extends Definition { * * @var string */ - const FILE = '/share/definitions/entities.json'; + const FILE = '/lib/Definition/EntityDefinition.json'; /** * List of required properties diff --git a/share/definitions/properties.json b/backend/lib/Definition/PropertyDefinition.json similarity index 99% rename from share/definitions/properties.json rename to backend/lib/Definition/PropertyDefinition.json index 8bdcb0c..51e9fb2 100644 --- a/share/definitions/properties.json +++ b/backend/lib/Definition/PropertyDefinition.json @@ -83,7 +83,7 @@ { "name" : "address:state", "type" : "multiple", - "choices" : [ + "options" : [ "Albania", "Algeria", "Andorra", diff --git a/backend/lib/Model/PropertyDefinition.php b/backend/lib/Definition/PropertyDefinition.php similarity index 94% rename from backend/lib/Model/PropertyDefinition.php rename to backend/lib/Definition/PropertyDefinition.php index 4bf87f3..427f5b4 100644 --- a/backend/lib/Model/PropertyDefinition.php +++ b/backend/lib/Definition/PropertyDefinition.php @@ -21,7 +21,7 @@ * along with volkszaehler.org. If not, see . */ -namespace Volkszaehler\Model; +namespace Volkszaehler\Definition; /** * @author Steffen Vogel @@ -33,7 +33,7 @@ class PropertyDefinition extends Definition { * * @var string */ - const FILE = '/share/definitions/properties.json'; + const FILE = '/lib/Definition/PropertyDefinition.json'; /** * One of: string, integer, float, boolean, multiple @@ -71,7 +71,7 @@ class PropertyDefinition extends Definition { * * @var array */ - protected $choices = array(); + protected $options = array(); protected static $definitions = NULL; @@ -103,7 +103,7 @@ class PropertyDefinition extends Definition { break; case 'multiple': - $invalid = !in_array($value, $this->choices, TRUE); + $invalid = !in_array($value, $this->options, TRUE); break; default: diff --git a/backend/lib/Model/Entity.php b/backend/lib/Model/Entity.php index a50e889..f212dd2 100644 --- a/backend/lib/Model/Entity.php +++ b/backend/lib/Model/Entity.php @@ -23,6 +23,8 @@ namespace Volkszaehler\Model; +use Volkszaehler\Definition; + use Doctrine\ORM; use Doctrine\Common\Collections; @@ -80,7 +82,7 @@ abstract class Entity { * @param string $type */ public function __construct($type) { - if (!EntityDefinition::exists($type)) { + if (!Definition\EntityDefinition::exists($type)) { throw new \Exception('Unknown entity type'); } @@ -184,7 +186,7 @@ abstract class Entity { public function getId() { return $this->id; } // read only public function getUuid() { return $this->uuid; } // read only public function getType() { return $this->type; } // read only - public function getDefinition() { return EntityDefinition::get($this->type); } + public function getDefinition() { return Definition\EntityDefinition::get($this->type); } /** * Get interpreter to obtain data and statistical information for a given time interval diff --git a/backend/lib/Model/Property.php b/backend/lib/Model/Property.php index 163ec82..b8ef320 100644 --- a/backend/lib/Model/Property.php +++ b/backend/lib/Model/Property.php @@ -23,6 +23,8 @@ namespace Volkszaehler\Model; +use Volkszaehler\Definition; + use Volkszaehler\Util; use Volkszaehler\Model; @@ -93,7 +95,7 @@ class Property { * @PreUpdate */ public function validate() { - if (!PropertyDefinition::exists($this->key)) { + if (!Definition\PropertyDefinition::exists($this->key)) { throw new \Exception('Invalid property key: ' . $this->key); } @@ -127,7 +129,7 @@ class Property { */ public function getKey() { return $this->key; } public function getValue() { return $this->value; } - public function getDefinition() { return PropertyDefinition::get($this->key); } + public function getDefinition() { return Definition\PropertyDefinition::get($this->key); } public function setValue($value) { $this->value = $value; } } diff --git a/backend/lib/Router.php b/backend/lib/Router.php index a30b52c..7d3db11 100644 --- a/backend/lib/Router.php +++ b/backend/lib/Router.php @@ -76,7 +76,8 @@ class Router { 'group' => 'Volkszaehler\Controller\AggregatorController', 'group' => 'Volkszaehler\Controller\AggregatorController', 'entity' => 'Volkszaehler\Controller\EntityController', - 'data' => 'Volkszaehler\Controller\DataController' + 'data' => 'Volkszaehler\Controller\DataController', + 'capabilities' => 'Volkszaehler\Controller\CapabilitiesController' ); /** diff --git a/backend/lib/View/JSON.php b/backend/lib/View/JSON.php index 629392b..286992f 100644 --- a/backend/lib/View/JSON.php +++ b/backend/lib/View/JSON.php @@ -55,6 +55,7 @@ class JSON extends View { $this->json = new Util\JSON(); $this->json['source'] = 'volkszaehler.org'; $this->json['version'] = VZ_VERSION; + $this->json['component'] = 'backend'; $this->setPadding($request->getParameter('padding')); } @@ -98,14 +99,23 @@ class JSON extends View { * @param Util\Debug $debug */ protected function addDebug(Util\Debug $debug) { - $this->json['debug'] = array( - 'time' => $debug->getExecutionTime(), - 'messages' => $debug->getMessages(), - 'database' => array( + $queries = $debug->getQueries(); + $messages = $debug->getMessages(); + + $jsonDebug['time'] = $debug->getExecutionTime(); + + if (count($messages) > 0) { + $jsonDebug['messages'] = $messages; + } + + if (count($queries) > 0) { + $jsonDebug['database'] = array( 'driver' => Util\Configuration::read('db.driver'), - 'queries' => $debug->getQueries() - ) - ); + 'queries' => $queries + ); + } + + $this->json['debug'] = $jsonDebug; } /** @@ -140,7 +150,13 @@ class JSON extends View { * @param Interpreter\InterpreterInterface $interpreter */ protected function addData(Interpreter\InterpreterInterface $interpreter) { - $this->json['data'][$interpreter->getUuid()] = $interpreter->getValues($this->request->getParameter('groupBy')); + $this->json['data'][$interpreter->getUuid()] = $interpreter->getValues($this->request->getParameter('resolution')); + } + + protected function addArray($data) { + foreach ($data as $index => $value) { + $this->json[$index] = $value; + } } /** @@ -185,6 +201,15 @@ class JSON extends View { return $jsonAggregator; } + public function add($data) { + if ($data instanceof Util\JSON || is_array($data)) { + $this->addArray($data); + } + else { + parent::add($data); + } + } + /* * Setter & getter */ diff --git a/backend/lib/View/View.php b/backend/lib/View/View.php index 524c088..1d6c5db 100644 --- a/backend/lib/View/View.php +++ b/backend/lib/View/View.php @@ -107,28 +107,23 @@ abstract class View { */ public function add($data) { if (isset($data)) { - if (is_array($data)) { - array_walk($data, array($this, 'add')); + if ($data instanceof Interpreter\InterpreterInterface) { + $this->addData($data); + } + elseif ($data instanceof Model\Channel) { + $this->addChannel($data); + } + elseif ($data instanceof Model\Aggregator) { + $this->addAggregator($data); + } + elseif ($data instanceof \Exception) { + $this->addException($data); + } + elseif ($data instanceof Util\Debug) { + $this->addDebug($data); } else { - if ($data instanceof Interpreter\InterpreterInterface) { - $this->addData($data); - } - elseif ($data instanceof Model\Channel) { - $this->addChannel($data); - } - elseif ($data instanceof Model\Aggregator) { - $this->addAggregator($data); - } - elseif ($data instanceof \Exception) { - $this->addException($data); - } - elseif ($data instanceof Util\Debug) { - $this->addDebug($data); - } - else { - throw new \Exception('Can\'t show ' . get_class($data)); - } + throw new \Exception('Can\'t show ' . get_class($data)); } } }