|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\scaffolding\creators; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\scaffolding\ScaffoldController; |
|
6
|
|
|
use Ubiquity\utils\base\UString; |
|
7
|
|
|
use Ubiquity\controllers\rest\RestServer; |
|
8
|
|
|
use Ubiquity\utils\base\UFileSystem; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Ubiquity\scaffolding\creators$RestControllerCreator |
|
12
|
|
|
* This class is part of Ubiquity |
|
13
|
|
|
* |
|
14
|
|
|
* @author jcheron <[email protected]> |
|
15
|
|
|
* @version 1.0.0 |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
class RestControllerCreator extends BaseControllerCreator { |
|
19
|
|
|
private $resource; |
|
20
|
|
|
|
|
21
|
1 |
|
public function __construct($restControllerName, $resource, $routePath = '') { |
|
22
|
1 |
|
$this->controllerName = $restControllerName; |
|
23
|
1 |
|
$this->routePath = $routePath; |
|
24
|
1 |
|
$this->controllerNS = RestServer::getRestNamespace (); |
|
25
|
1 |
|
$this->resource = $resource; |
|
26
|
1 |
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
public function create(ScaffoldController $scaffoldController, $reInit = null) { |
|
29
|
1 |
|
$this->scaffoldController = $scaffoldController; |
|
30
|
1 |
|
$resource = $this->resource; |
|
31
|
1 |
|
$controllerName = $this->controllerName; |
|
32
|
1 |
|
$controllerNS = $this->controllerNS; |
|
33
|
1 |
|
$messages = [ ]; |
|
34
|
|
|
|
|
35
|
1 |
|
$restControllersDir = \ROOT . \DS . str_replace ( "\\", \DS, $controllerNS ); |
|
36
|
1 |
|
UFileSystem::safeMkdir ( $restControllersDir ); |
|
37
|
|
|
|
|
38
|
1 |
|
$filename = $restControllersDir . \DS . $controllerName . ".php"; |
|
39
|
1 |
|
if (! \file_exists ( $filename )) { |
|
40
|
1 |
|
$templateDir = $scaffoldController->getTemplateDir (); |
|
41
|
1 |
|
$namespace = ''; |
|
42
|
1 |
|
if ($controllerNS != null) |
|
43
|
1 |
|
$namespace = "namespace " . $controllerNS . ";"; |
|
44
|
1 |
|
if ($this->routePath != null) { |
|
45
|
1 |
|
$this->addRoute ( $this->routePath ); |
|
46
|
|
|
} |
|
47
|
1 |
|
UFileSystem::openReplaceWriteFromTemplateFile ( $templateDir . "restController.tpl", $filename, [ "%resource%" => $resource,"%route%" => $this->routePath,"%controllerName%" => $controllerName,"%namespace%" => $namespace ] ); |
|
48
|
1 |
|
$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" ); |
|
49
|
1 |
|
if (isset ( $reInit )) { |
|
50
|
1 |
|
$this->scaffoldController->initRestCache ( false ); |
|
51
|
|
|
} |
|
52
|
|
|
} else { |
|
53
|
|
|
$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" ); |
|
54
|
|
|
} |
|
55
|
1 |
|
$this->scaffoldController->_refreshRest ( true ); |
|
56
|
1 |
|
echo implode ( "\n", $messages ); |
|
57
|
1 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function addViews(&$uses, &$messages, &$classContent) { |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|