Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
RestControllerCreator.php
Go to the documentation of this file.
1<?php
2
4
6use Ubiquity\scaffolding\ScaffoldController;
11
23 private $resource;
24 private $baseClass;
25 public function __construct($restControllerName, $baseClass,$resource='', $routePath = '') {
26 $this->controllerName = $restControllerName;
27 if($routePath!=null){
28 $this->routePath = '/'.\ltrim($routePath,'/');
29 }
30 $this->baseClass="\\".$baseClass;
31 if (is_subclass_of($this->baseClass, HasResourceInterface::class, true)) {
32 $this->resource = $resource;
33 }
34 $this->controllerNS = RestServer::getRestNamespace ();
35 $this->templateName = \call_user_func($baseClass.'::_getTemplateFile');
36 }
37
38 public function create(ScaffoldController $scaffoldController, $reInit = null) {
39 $this->scaffoldController = $scaffoldController;
42 $messages = [ ];
43
44 $restControllersDir = \ROOT . \DS . str_replace ( "\\", \DS, $controllerNS );
45 UFileSystem::safeMkdir ( $restControllersDir );
46
47 $filename = $restControllersDir . \DS . $controllerName . ".php";
48 if (! \file_exists ( $filename )) {
49 $attrFolder='';
50 if(\class_exists('\\Ubiquity\\attributes\\AttributesEngine')){
51 $attrFolder='attributes/';
52 }
53 $templateDir = $scaffoldController->getTemplateDir ().$attrFolder;
54 $namespace = '';
55 if ($controllerNS != null){
56 $namespace = "namespace " . $controllerNS . ";";
57 }
59 $routeAnnot='';
60 if ($this->routePath != null) {
62 $automatedAndInherited=$this->baseClass===RestBaseController::class;
63 $routeAnnot=$this->getRouteAnnotation($this->routePath,$automatedAndInherited,$automatedAndInherited);
64 }
65 $variables = [ '%route%' => $routeAnnot,'%controllerName%' => $controllerName,'%namespace%' => $namespace,'%routePath%' => $routePath ,'%baseClass%'=>$this->baseClass];
66 $this->addVariablesForReplacement ( $variables );
67 $variables ['%uses%'] = $this->getUsesStr();
68 UFileSystem::openReplaceWriteFromTemplateFile ( $templateDir . $this->templateName, $filename, $variables );
69 $messages [] = $scaffoldController->showSimpleMessage ( "The <b>" . $controllerName . "</b> Rest controller has been created in <b>" . UFileSystem::cleanPathname ( $filename ) . "</b>.", "success", "Rest creation", "checkmark circle", 30000, "msgGlobal" );
70 if (isset ( $reInit )) {
71 $this->scaffoldController->initRestCache ( false );
72 }
73 } else {
74 $messages [] = $scaffoldController->showSimpleMessage ( "The file <b>" . $filename . "</b> already exists.<br>Can not create the <b>" . $controllerName . "</b> Rest controller!", "warning", "Rest error", "warning circle", 30000, "msgGlobal" );
75 }
76 $this->scaffoldController->_refreshRest ( true );
77 echo implode ( "\n", $messages );
78 }
79
80 protected function addVariablesForReplacement(&$variables) {
81 $values=[];
82 if($this->resource!=null){
83 $values['resource']=$this->resource;
84 }
85 $variables ['%restAnnot%'] = CacheManager::getAnnotationsEngineInstance()->getAnnotation($this,'rest',$values)->asAnnotation();
86 }
87
88 protected function addViews(&$messages, &$classContent) {
89 }
90}
91
Manager for caches (Router, Rest, models).
Abstract base class for Rest controllers.
getRouteAnnotation($path, $automated=true, $inherited=true)
__construct($restControllerName, $baseClass, $resource='', $routePath='')
create(ScaffoldController $scaffoldController, $reInit=null)
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.
Ubiquity\controllers\rest$HasResourceInterface This class is part of Ubiquity.