Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
CrudControllerCreator.php
Go to the documentation of this file.
1<?php
3
5use Ubiquity\scaffolding\ScaffoldController;
7
19
20 private $resource;
21
22 private $crudDatas;
23
24 private $crudViewer;
25
26 private $crudEvents;
27
28 private $style;
29
30 protected $viewKey;
31
32 public function __construct($crudControllerName, $resource, $crudDatas = null, $crudViewer = null, $crudEvents = null, $crudViews = null, $routePath = '', $useViewInheritance = false, $style = '') {
33 parent::__construct($crudControllerName, $routePath, $crudViews, $useViewInheritance);
34 $this->resource = $resource;
35 $this->crudDatas = $crudDatas;
36 $this->crudViewer = $crudViewer;
37 $this->crudEvents = $crudEvents;
38 $this->templateName = 'crudController.tpl';
39 $this->style = $style;
40 $this->viewKey = 'CRUD';
41 }
42
43 public function create(ScaffoldController $scaffoldController) {
44 $this->scaffoldController = $scaffoldController;
46 $crudControllerName = $this->controllerName;
47 $classContent = '';
48 $nsc = \trim($this->controllerNS, '\\');
49 $messages = [];
50
51 // $scaffoldController->_createMethod ( 'public', '__construct', '', '', "\n\t\tparent::__construct();\n\$this->model=\"{$resource}\";" );
52
53 $domain = DDDManager::getActiveDomain();
54 if ($domain != '') {
55 $classContent .= $scaffoldController->_createMethod('public', 'initialize', '', '', "\t\tparent::initialize();\n\t\t\Ubiquity\domains\DDDManager::setDomain('" . $domain . "');");
56 }
57 $this->createElements($nsc, $crudControllerName, $scaffoldController, $messages, $classContent);
58
60 $routeAnnot = '';
61 if ($this->routePath != null) {
63 $routeAnnot = $this->getRouteAnnotation($this->routePath);
64 }
65
66 $uses = $this->getUsesStr();
67 $messages[] = $scaffoldController->_createController($crudControllerName, [
68 '%routePath%' => $routePath,
69 '%route%' => $routeAnnot,
70 '%resource%' => $resource,
71 '%uses%' => $uses,
72 '%namespace%' => $this->getNamespaceStr(),
73 '%baseClass%' => "\\Ubiquity\\controllers\\crud\\CRUDController",
74 '%content%' => $classContent,
75 '%style%' => $this->style
76 ], $this->templateName);
77 echo \implode("\n", $messages);
78 }
79
80 protected function createElements(string $nsc, string $crudControllerName, ScaffoldController $scaffoldController, array &$messages, string &$classContent) {
81 if (isset($this->crudDatas)) {
82 $this->addUses("{$nsc}\\crud\\datas\\{$crudControllerName}Datas", "Ubiquity\\controllers\\crud\\CRUDDatas");
83
84 $classContent .= $scaffoldController->_createMethod('protected', 'getAdminData', '', ': CRUDDatas', "\t\treturn new {$crudControllerName}Datas(\$this);");
85 $messages[] = $this->createCRUDDatasClass();
86 }
87
88 if (isset($this->crudViewer)) {
89 $this->addUses("{$nsc}\\crud\\viewers\\{$crudControllerName}Viewer", "Ubiquity\\controllers\\crud\\viewers\\ModelViewer");
90
91 $classContent .= $scaffoldController->_createMethod('protected', 'getModelViewer', '', ': ModelViewer', "\t\treturn new {$crudControllerName}Viewer(\$this,\$this->style);");
92 $messages[] = $this->createModelViewerClass();
93 }
94 if (isset($this->crudEvents)) {
95 $this->addUses("{$nsc}\\crud\\events\\{$crudControllerName}Events", "Ubiquity\\controllers\\crud\\CRUDEvents");
96
97 $classContent .= $scaffoldController->_createMethod('protected', 'getEvents', '', ': CRUDEvents', "\t\treturn new {$crudControllerName}Events(\$this);");
98 $messages[] = $this->createEventsClass();
99 }
100
101 if (isset($this->views)) {
102 $this->addViews($messages, $classContent);
103 }
104 }
105
106 protected function addViews(&$messages, &$classContent) {
107 $crudControllerName = $this->controllerName;
108 $crudViews = \explode(',', $this->views);
109 $nsc = \trim($this->controllerNS, '\\');
110 $this->addUses("{$nsc}\\crud\\files\\{$crudControllerName}Files", "Ubiquity\\controllers\\crud\\CRUDFiles");
111 $classContent .= $this->scaffoldController->_createMethod('protected', 'getFiles', '', ': CRUDFiles', "\t\treturn new {$crudControllerName}Files();");
112 $classFilesContent = [];
113 $viewNamespace = DDDManager::getViewNamespace();
114 foreach ($crudViews as $file) {
115 if (isset(ScaffoldController::$views[$this->viewKey][$file])) {
116 $frameworkViewname = ScaffoldController::$views[$this->viewKey][$file];
117 $this->scaffoldController->createAuthCrudView($frameworkViewname, $crudControllerName, $file, $this->useViewInheritance);
118 $classFilesContent[] = $this->scaffoldController->_createMethod('public', 'getView' . \ucfirst($file), '', ': string', "\t\treturn \"" . $viewNamespace . $crudControllerName . "/" . $file . ".html\";");
119 }
120 }
121 $messages[] = $this->createCRUDFilesClass(\implode('', $classFilesContent));
122 }
123
124 protected function createCRUDDatasClass() {
125 $ns = $this->controllerNS . "crud\\datas";
126 $uses = "\nuse Ubiquity\\controllers\\crud\\CRUDDatas;";
127 return $this->scaffoldController->_createClass('class.tpl', $this->controllerName . 'Datas', $ns, $uses, 'extends CRUDDatas', "\t//use override/implement Methods");
128 }
129
130 protected function createModelViewerClass() {
131 $ns = $this->controllerNS . "crud\\viewers";
132 $uses = "\nuse Ubiquity\\controllers\\crud\\viewers\\ModelViewer;";
133 return $this->scaffoldController->_createClass('class.tpl', $this->controllerName . 'Viewer', $ns, $uses, 'extends ModelViewer', "\t//use override/implement Methods");
134 }
135
136 protected function createEventsClass() {
137 $ns = $this->controllerNS . "crud\\events";
138 $uses = "\nuse Ubiquity\\controllers\\crud\\CRUDEvents;";
139 return $this->scaffoldController->_createClass('class.tpl', $this->controllerName . 'Events', $ns, $uses, 'extends CRUDEvents', "\t//use override/implement Methods");
140 }
141
142 public function createCRUDFilesClass($classContent = "") {
143 $ns = $this->controllerNS . "crud\\files";
144 $uses = "\nuse Ubiquity\\controllers\\crud\\CRUDFiles;";
145 return $this->scaffoldController->_createClass('class.tpl', $this->controllerName . 'Files', $ns, $uses, 'extends CRUDFiles', $classContent);
146 }
147}
148
Starts the framework.
Definition Startup.php:19
Manager for a Domain Driven Design approach.
getRouteAnnotation($path, $automated=true, $inherited=true)
__construct($crudControllerName, $resource, $crudDatas=null, $crudViewer=null, $crudEvents=null, $crudViews=null, $routePath='', $useViewInheritance=false, $style='')
createElements(string $nsc, string $crudControllerName, ScaffoldController $scaffoldController, array &$messages, string &$classContent)