-
Notifications
You must be signed in to change notification settings - Fork 82
/
Controller.php
122 lines (107 loc) · 2.87 KB
/
Controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* @copyright Copyright (c) 2011, 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 Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ParameterBag;
use Doctrine\ORM\EntityManager;
use Volkszaehler\View\View;
/**
* Controller superclass for all controllers
*
* @author Steffen Vogel <info@steffenvogel.de>
* @author Andreas Goetz <cpuidle@gmx.de>
* @package default
*/
abstract class Controller {
/**
* @var Doctrine\ORM\EntityManager
*/
protected $em;
/**
* @var Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* @var Symfony\Component\HttpFoundation\ParameterBag
*/
protected $parameters;
/**
* @var Volkszaehler\View
*/
protected $view;
/**
* Constructor
*
* @param Request $request
* @param EntityManager $em
* @param View $view
*/
public function __construct(Request $request, EntityManager $em, View $view) {
$this->request = $request;
$this->em = $em;
$this->view = $view;
}
/**
* Return request parameter from query or post body
*/
public function getParameters() {
if ($this->parameters === null) {
$this->parameters = new ParameterBag($this->request->query->all());
if ($this->request->getMethod() !== Request::METHOD_GET) {
$this->parameters->add($this->request->request->all());
}
}
return $this->parameters;
}
/**
* Run operation
*
* @param $op Operation to run
* @param null $uuid Uuid to operate on
* @return operation result
* @throws \Exception
*/
public function run($op, $uuid = null) {
if (!method_exists($this, $op)) {
throw new \Exception('Invalid context operation: \'' . $op . '\'');
}
// one or more uuid(s) as query parameters?
if (null == $uuid) {
$uuid = $this->getParameters()->get('uuid');
}
// call the operation
return $this->{$op}($uuid);
}
/**
* Helper function to convert single/multiple parameters to array format
* @param $data
* @return array
*/
protected static function makeArray($data) {
if (!is_array($data)) {
$data = isset($data) ? array($data) : array();
}
return $data;
}
}
?>