moved json definitions to capabilities controller
This commit is contained in:
parent
1a2977e345
commit
b2c16229f7
14 changed files with 136 additions and 47 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
56
backend/lib/Controller/CapabilitiesController.php
Normal file
56
backend/lib/Controller/CapabilitiesController.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2010, The volkszaehler.org project
|
||||
* @package default
|
||||
* @license http://www.opensource.org/licenses/gpl-license.php GNU Public License
|
||||
*/
|
||||
/*
|
||||
* This file is part of volkzaehler.org
|
||||
*
|
||||
* volkzaehler.org is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* volkzaehler.org is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Volkszaehler\Controller;
|
||||
|
||||
use Volkszaehler\Model;
|
||||
use Volkszaehler\Util;
|
||||
|
||||
/**
|
||||
* Capabilities controller
|
||||
*
|
||||
* @author Steffen Vogel <info@steffenvogel.de>
|
||||
* @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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -21,7 +21,7 @@
|
|||
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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
|
|
@ -83,7 +83,7 @@
|
|||
{
|
||||
"name" : "address:state",
|
||||
"type" : "multiple",
|
||||
"choices" : [
|
||||
"options" : [
|
||||
"Albania",
|
||||
"Algeria",
|
||||
"Andorra",
|
|
@ -21,7 +21,7 @@
|
|||
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Volkszaehler\Model;
|
||||
namespace Volkszaehler\Definition;
|
||||
|
||||
/**
|
||||
* @author Steffen Vogel <info@steffenvogel.de>
|
||||
|
@ -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:
|
|
@ -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
|
||||
|
|
|
@ -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; }
|
||||
}
|
||||
|
|
|
@ -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'
|
||||
);
|
||||
|
||||
/**
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue