Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
7a67a2c0
Commit
7a67a2c0
authored
Aug 08, 2016
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(mvc) some refactoring for
https://github.com/opnsense/core/pull/1124
parent
3235ee26
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
9 deletions
+63
-9
ApiModelControllerBase.php
.../app/controllers/OPNsense/Base/ApiModelControllerBase.php
+55
-6
ApiMutableModelControllerBase.php
...ntrollers/OPNsense/Base/ApiMutableModelControllerBase.php
+8
-3
No files found.
src/opnsense/mvc/app/controllers/OPNsense/Base/ApiModelControllerBase.php
View file @
7a67a2c0
<?php
/**
* Copyright (C) 2016 IT-assistans Sverige AB
* Copyright (C) 2016 Deciso B.V.
*
* All rights reserved.
*
...
...
@@ -28,7 +29,6 @@
*/
namespace
OPNsense\Base
;
use
OPNsense\Base\ApiControllerBase
;
/**
* Class ApiModelControllerBase, inherit this class to implement
...
...
@@ -40,22 +40,71 @@ use OPNsense\Base\ApiControllerBase;
*/
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
->
getModelName
()]
=
$this
->
getModelNodes
(
$mdl
);
$result
[
$this
->
internalModelName
]
=
$this
->
getModelNodes
(
);
}
return
$result
;
}
abstract
protected
function
getModel
();
abstract
protected
function
getModelName
();
/**
* 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
getModelNodes
(
$mdl
)
{
return
$mdl
->
getNodes
();
protected
function
getModel
()
{
if
(
$this
->
modelHandle
==
null
)
{
$this
->
modelHandle
=
(
new
\ReflectionClass
(
$this
->
internalModelClass
))
->
newInstance
();
}
return
$this
->
modelHandle
;
}
}
src/opnsense/mvc/app/controllers/OPNsense/Base/ApiMutableModelControllerBase.php
View file @
7a67a2c0
<?php
/**
* Copyright (C) 2016 IT-assistans Sverige AB
* Copyright (C) 2016 Deciso B.V.
*
* All rights reserved.
*
...
...
@@ -28,7 +29,7 @@
*/
namespace
OPNsense\Base
;
use
OPNsense\Base\ApiModelControllerBase
;
use
\OPNsense\Core\Config
;
/**
* Class ApiMutableModelControllerBase, inherit this class to implement
...
...
@@ -40,13 +41,17 @@ use OPNsense\Base\ApiModelControllerBase;
*/
abstract
class
ApiMutableModelControllerBase
extends
ApiModelControllerBase
{
/**
* update model settings
* @return array status / validation errors
*/
public
function
setAction
()
{
$result
=
array
(
"result"
=>
"failed"
);
if
(
$this
->
request
->
isPost
())
{
// load model and update with provided data
$mdl
=
$this
->
getModel
();
$mdl
->
setNodes
(
$this
->
request
->
getPost
(
getModelName
()
));
$mdl
->
setNodes
(
$this
->
request
->
getPost
(
$this
->
internalModelName
));
// perform validation
$valMsgs
=
$mdl
->
performValidation
();
...
...
@@ -54,7 +59,7 @@ abstract class ApiMutableModelControllerBase extends ApiModelControllerBase
if
(
!
array_key_exists
(
"validations"
,
$result
))
{
$result
[
"validations"
]
=
array
();
}
$result
[
"validations"
][
$this
->
getModelName
()
.
"."
.
$msg
->
getField
()]
=
$msg
->
getMessage
();
$result
[
"validations"
][
$this
->
internalModelName
.
"."
.
$msg
->
getField
()]
=
$msg
->
getMessage
();
}
// serialize model to config and save
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment