|
7 | 7 | use Ubiquity\scaffolding\creators\CrudControllerCreator;
|
8 | 8 | use Ubiquity\scaffolding\ScaffoldController;
|
9 | 9 |
|
10 |
| -class IndexCrudControllerCreator extends CrudControllerCreator{ |
11 |
| - |
12 |
| - public function __construct($crudControllerName, $crudDatas = null, $crudViewer = null, $crudEvents = null, $crudViews = null, $routePath = '', $useViewInheritance = false,$style='') { |
13 |
| - parent::__construct($crudControllerName, null,$crudDatas,$crudViewer,$crudEvents,$crudViews,$routePath,$useViewInheritance,$style); |
14 |
| - $this->templateName='indexCrudController.tpl'; |
15 |
| - $this->viewKey='indexCRUD'; |
16 |
| - } |
17 |
| - |
18 |
| - public function create(ScaffoldController $scaffoldController) { |
19 |
| - $this->scaffoldController = $scaffoldController; |
20 |
| - $crudControllerName = $this->controllerName; |
21 |
| - $classContent = ''; |
22 |
| - $nsc=\trim($this->controllerNS,'\\'); |
23 |
| - $messages = [ ]; |
24 |
| - $domain=DDDManager::getActiveDomain(); |
25 |
| - $initializeContent=''; |
26 |
| - if($domain!=''){ |
27 |
| - $initializeContent="\t\t\Ubiquity\domains\DDDManager::setDomain('".$domain."');"; |
| 10 | +class IndexCrudControllerCreator extends CrudControllerCreator { |
| 11 | + |
| 12 | + public function __construct($crudControllerName, $crudDatas = null, $crudViewer = null, $crudEvents = null, $crudViews = null, $routePath = '', $useViewInheritance = false, $style = '') { |
| 13 | + parent::__construct($crudControllerName, null, $crudDatas, $crudViewer, $crudEvents, $crudViews, $routePath, $useViewInheritance, $style); |
| 14 | + $this->templateName = 'indexCrudController.tpl'; |
| 15 | + $this->viewKey = 'indexCRUD'; |
| 16 | + } |
| 17 | + |
| 18 | + public function create(ScaffoldController $scaffoldController) { |
| 19 | + $this->scaffoldController = $scaffoldController; |
| 20 | + $crudControllerName = $this->controllerName; |
| 21 | + $classContent = ''; |
| 22 | + $nsc = \trim($this->controllerNS, '\\'); |
| 23 | + $messages = []; |
| 24 | + $domain = DDDManager::getActiveDomain(); |
| 25 | + $initializeContent = ''; |
| 26 | + $routeNamePrefix = ''; |
| 27 | + if ($domain != '') { |
| 28 | + $initializeContent = "\t\t\Ubiquity\domains\DDDManager::setDomain('" . $domain . "');"; |
| 29 | + $routeNamePrefix = $domain . '.'; |
28 | 30 | }
|
29 |
| - if(($aDb=$this->scaffoldController->getActiveDb())!=null){ |
30 |
| - if($aDb!=='default') { |
31 |
| - $ns=CacheManager::getModelsNamespace($aDb); |
32 |
| - if(isset($ns)) { |
33 |
| - $classContent .= $scaffoldController->_createMethod('protected', 'getModelName', '', '', "\t\treturn '".$ns."\\\\'.\ucfirst(\$this->resource);"); |
34 |
| - $classContent .= $scaffoldController->_createMethod('protected', 'getIndexModels', '', ': array ', "\t\treturn \Ubiquity\orm\DAO::getModels('".$aDb."');"); |
| 31 | + if (($aDb = $this->scaffoldController->getActiveDb()) != null) { |
| 32 | + if ($aDb !== 'default') { |
| 33 | + $ns = CacheManager::getModelsNamespace($aDb); |
| 34 | + $routeNamePrefix .= $aDb . '.'; |
| 35 | + if (isset($ns)) { |
| 36 | + $classContent .= $scaffoldController->_createMethod('protected', 'getModelName', '', '', "\t\treturn '" . $ns . "\\\\'.\ucfirst(\$this->resource);"); |
| 37 | + $classContent .= $scaffoldController->_createMethod('protected', 'getIndexModels', '', ': array ', "\t\treturn \Ubiquity\orm\DAO::getModels('" . $aDb . "');"); |
35 | 38 | $initializeContent .= "\n\t\t\Ubiquity\orm\DAO::start();";
|
36 | 39 | }
|
37 | 40 | }
|
38 | 41 | }
|
39 |
| - if($initializeContent!==''){ |
40 |
| - $classContent.=$scaffoldController->_createMethod ( 'public','initialize','','',$initializeContent."\n\t\tparent::initialize();"); |
| 42 | + if ($routeNamePrefix != '') { |
| 43 | + $classContent .= $scaffoldController->_createMethod('protected', 'getRouteNamePrefix', '', ': string ', "\t\treturn '" . $routeNamePrefix . "';"); |
| 44 | + } |
| 45 | + if ($initializeContent !== '') { |
| 46 | + $classContent .= $scaffoldController->_createMethod('public', 'initialize', '', '', $initializeContent . "\n\t\tparent::initialize();"); |
41 | 47 | }
|
42 |
| - $this->createElements($nsc, $crudControllerName, $scaffoldController, $messages,$classContent); |
43 |
| - |
44 |
| - $this->routePath ??= '{resource}'; |
45 |
| - $routePath=$this->routePath; |
46 |
| - $routeAnnotation=$this->getRouteAnnotation($routePath); |
47 |
| - |
48 |
| - $uses = $this->getUsesStr(); |
49 |
| - $messages [] = $scaffoldController->_createController ( $crudControllerName, [ |
50 |
| - '%indexRoute%'=>$this->getAnnotation('route', ['name'=>'crud.index','priority'=>-1]), |
51 |
| - '%homeRoute%'=>$this->getAnnotation('route', ['path'=>$this->getHome($routePath),'name'=>'crud.home','priority'=>100]), |
52 |
| - '%routePath%' => '"'.\str_replace('{resource}', '".$this->resource."', $routePath).'"', |
53 |
| - '%route%' => $routeAnnotation, |
54 |
| - '%uses%' => $uses, |
55 |
| - '%namespace%' => $this->getNamespaceStr(), |
56 |
| - '%baseClass%' => "\\Ubiquity\\controllers\\crud\\MultiResourceCRUDController", |
57 |
| - '%content%' => $classContent |
58 |
| - ] |
59 |
| - , $this->templateName ); |
60 |
| - echo implode ( "\n", $messages ); |
61 |
| - } |
62 |
| - |
63 |
| - protected function getHome($path) { |
64 |
| - return '#/'.\str_replace('{resource}', 'home',$path); |
65 |
| - } |
| 48 | + $this->createElements($nsc, $crudControllerName, $scaffoldController, $messages, $classContent); |
| 49 | + |
| 50 | + $this->routePath ??= '{resource}'; |
| 51 | + $routePath = \rtrim($this->routePath, '/'); |
| 52 | + $routeAnnotation = $this->getRouteAnnotation($routePath); |
| 53 | + |
| 54 | + $uses = $this->getUsesStr(); |
| 55 | + $messages[] = $scaffoldController->_createController($crudControllerName, [ |
| 56 | + '%indexRoute%' => $this->getAnnotation('route', [ |
| 57 | + 'name' => $routeNamePrefix . 'crud.index', |
| 58 | + 'priority' => - 1 |
| 59 | + ]), |
| 60 | + '%homeRoute%' => $this->getAnnotation('route', [ |
| 61 | + 'path' => $this->getHome($routePath), |
| 62 | + 'name' => $routeNamePrefix . 'crud.home', |
| 63 | + 'priority' => 100 |
| 64 | + ]), |
| 65 | + '%routePath%' => '"' . \str_replace('{resource}', '".$this->resource."', $routePath) . '"', |
| 66 | + '%route%' => $routeAnnotation, |
| 67 | + '%uses%' => $uses, |
| 68 | + '%namespace%' => $this->getNamespaceStr(), |
| 69 | + '%baseClass%' => "\\Ubiquity\\controllers\\crud\\MultiResourceCRUDController", |
| 70 | + '%content%' => $classContent |
| 71 | + ], $this->templateName); |
| 72 | + echo implode("\n", $messages); |
| 73 | + } |
| 74 | + |
| 75 | + protected function getHome($path) { |
| 76 | + return '#/' . \str_replace('{resource}', 'home', $path); |
| 77 | + } |
66 | 78 | }
|
67 | 79 |
|
0 commit comments