Commit 4aa4570e authored by Ad Schellevis's avatar Ad Schellevis

(mvc) merge ApiModelControllerBase and ApiMutableModelControllerBase + define...

(mvc) merge ApiModelControllerBase and ApiMutableModelControllerBase + define class statics properly. as discussed at https://github.com/opnsense/core/pull/1124
parent beff7772
<?php
/**
* Copyright (C) 2016 IT-assistans Sverige AB
* Copyright (C) 2016 Deciso B.V.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
namespace OPNsense\Base;
/**
* Class ApiModelControllerBase, inherit this class to implement
* an API that exposes a model with a get action.
* You need to implement a method to create new blank model
* objecs (newModelObject) as well as a method to return
* the name of the model.
* @package OPNsense\Base
*/
abstract class ApiModelControllerBase extends ApiControllerBase
{
/**
* @var string this implementations internal model name to use (in set/get output)
*/
protected $internalModelName = null;
/**
* @var string model class name to use
*/
protected $internalModelClass = null;
/**
* @var null|BaseModel model object to work on
*/
private $modelHandle = null;
/**
* validate on initialization
* @throws Exception
*/
public function initialize()
{
parent::initialize();
if (empty($this->internalModelClass)) {
throw new \Exception('cannot instantiate without internalModelClass defined.');
}
if (empty($this->internalModelName)) {
throw new \Exception('cannot instantiate without internalModelName defined.');
}
}
/**
* retrieve model settings
* @return array settings
*/
public function getAction()
{
// define list of configurable settings
$result = array();
if ($this->request->isGet()) {
$mdl = $this->getModel();
$result[$this->internalModelName] = $this->getModelNodes();
}
return $result;
}
/**
* override this to customize what part of the model gets exposed
* @return array
*/
protected function getModelNodes()
{
return $this->getModel()->getNodes();
}
/**
* override this to customize the model binding behavior
* @return null|BaseModel
*/
protected function getModel()
{
if ($this->modelHandle == null) {
$this->modelHandle = (new \ReflectionClass($this->internalModelClass))->newInstance();
}
return $this->modelHandle;
}
}
...@@ -39,8 +39,74 @@ use \OPNsense\Core\Config; ...@@ -39,8 +39,74 @@ use \OPNsense\Core\Config;
* the name of the model. * the name of the model.
* @package OPNsense\Base * @package OPNsense\Base
*/ */
abstract class ApiMutableModelControllerBase extends ApiModelControllerBase abstract class ApiMutableModelControllerBase extends ApiControllerBase
{ {
/**
* @var string this implementations internal model name to use (in set/get output)
*/
static protected $internalModelName = null;
/**
* @var string model class name to use
*/
static protected $internalModelClass = null;
/**
* @var null|BaseModel model object to work on
*/
private $modelHandle = null;
/**
* validate on initialization
* @throws Exception
*/
public function initialize()
{
parent::initialize();
if (empty(static::$internalModelClass)) {
throw new \Exception('cannot instantiate without internalModelClass defined.');
}
if (empty(static::$internalModelName)) {
throw new \Exception('cannot instantiate without internalModelName defined.');
}
}
/**
* retrieve model settings
* @return array settings
*/
public function getAction()
{
// define list of configurable settings
$result = array();
if ($this->request->isGet()) {
$result[static::$internalModelName] = $this->getModelNodes();
}
return $result;
}
/**
* override this to customize what part of the model gets exposed
* @return array
*/
protected function getModelNodes()
{
return $this->getModel()->getNodes();
}
/**
* override this to customize the model binding behavior
* @return null|BaseModel
*/
protected function getModel()
{
if ($this->modelHandle == null) {
$this->modelHandle = (new \ReflectionClass(static::$internalModelClass))->newInstance();
}
return $this->modelHandle;
}
/** /**
* update model settings * update model settings
* @return array status / validation errors * @return array status / validation errors
...@@ -51,7 +117,7 @@ abstract class ApiMutableModelControllerBase extends ApiModelControllerBase ...@@ -51,7 +117,7 @@ abstract class ApiMutableModelControllerBase extends ApiModelControllerBase
if ($this->request->isPost()) { if ($this->request->isPost()) {
// load model and update with provided data // load model and update with provided data
$mdl = $this->getModel(); $mdl = $this->getModel();
$mdl->setNodes($this->request->getPost($this->internalModelName)); $mdl->setNodes($this->request->getPost(static::$internalModelName));
// perform validation // perform validation
$valMsgs = $mdl->performValidation(); $valMsgs = $mdl->performValidation();
...@@ -59,7 +125,7 @@ abstract class ApiMutableModelControllerBase extends ApiModelControllerBase ...@@ -59,7 +125,7 @@ abstract class ApiMutableModelControllerBase extends ApiModelControllerBase
if (!array_key_exists("validations", $result)) { if (!array_key_exists("validations", $result)) {
$result["validations"] = array(); $result["validations"] = array();
} }
$result["validations"][$this->internalModelName.".".$msg->getField()] = $msg->getMessage(); $result["validations"][static::$internalModelName.".".$msg->getField()] = $msg->getMessage();
} }
// serialize model to config and save // serialize model to config and save
......
Markdown is supported
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