Ubiquity  2.0.3
php rapid development framework
Maker.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
10 class Maker {
11  private static $baseDir;
12 
13  public static function init($baseDir) {
14  self::$baseDir = $baseDir;
15  }
16 
17  public static function createAction($controller, $action, $parameters = "", $content = "", $createView = false, $route = []) {
18  $msgContent = [ ];
19  $hasErrors = false;
20  $r = new \ReflectionClass ( $controller );
21  $ctrlFilename = $r->getFileName ();
22  $content = CodeUtils::indent ( $content, 2 );
23  $fileContent = \implode ( "", UIntrospection::getClassCode ( $controller ) );
24  $fileContent = \trim ( $fileContent );
25  $posLast = \strrpos ( $fileContent, "}" );
26  if ($posLast !== false) {
27  if ($createView) {
28  $viewname = self::createView ( ClassUtils::getClassSimpleName ( $controller ), $action );
29  $content .= "\n\t\t\$this->loadView('" . $viewname . "');\n";
30  $msgContent [] = "<br>Created view : <b>" . $viewname . "</b>";
31  }
32  $routeAnnotation = "";
33  if (isset ( $route ["path"] )) {
34  $name = "route";
35  $path = $route ["path"];
36  $routeProperties = [ '"' . $path . '"' ];
37  $methods = $route ["methods"];
38  if (UString::isNotNull ( $methods )) {
39  $routeProperties [] = '"methods"=>' . self::getMethods ( $methods );
40  }
41  if ($route ["cache"]) {
42  $routeProperties [] = '"cache"=>true';
43  if (isset ( $route ["duration"] )) {
44  $duration = $route ["duration"];
45  if (\ctype_digit ( $duration )) {
46  $routeProperties [] = '"duration"=>' . $duration;
47  }
48  }
49  }
50  $routeProperties = \implode ( ",", $routeProperties );
51  $routeAnnotation = UFileSystem::openReplaceInTemplateFile ( self::$baseDir . "/admin/templates/annotation.tpl", [ "%name%" => $name,"%properties%" => $routeProperties ] );
52  }
53  $parameters = CodeUtils::cleanParameters ( $parameters );
54  $actionContent = UFileSystem::openReplaceInTemplateFile ( self::$baseDir . "/admin/templates/action.tpl", [ "%route%" => "\n" . $routeAnnotation,"%actionName%" => $action,"%parameters%" => $parameters,"%content%" => $content ] );
55  $fileContent = \substr_replace ( $fileContent, "\n%content%", $posLast - 1, 0 );
56  if (! CodeUtils::isValidCode ( '<?php ' . $content )) {
57  $msgContent = [ "Errors parsing action content!" ];
58  $hasErrors = true;
59  } else {
60  if (UFileSystem::replaceWriteFromContent ( $fileContent . "\n", $ctrlFilename, [ '%content%' => $actionContent ] )) {
61  $msgContent [] = "The action <b>{$action}</b> is created in controller <b>{$controller}</b>";
62  }
63  }
64  }
65  return compact ( $msgContent, $hasErrors );
66  }
67 
68  public static function createView($controller, $action) {
69  $viewName = $controller . "/" . $action . ".html";
70  UFileSystem::safeMkdir ( ROOT . DS . "views" . DS . $controller );
71  UFileSystem::openReplaceWriteFromTemplateFile ( self::$baseDir . "/admin/templates/view.tpl", ROOT . DS . "views" . DS . $viewName, [ "%controllerName%" => $controller,"%actionName%" => $action ] );
72  return $viewName;
73  }
74 
75  private static function getMethods($strMethods) {
76  $methods = \explode ( ",", $strMethods );
77  $result = [ ];
78  foreach ( $methods as $method ) {
79  $result [] = '"' . $method . '"';
80  }
81  return "[" . \implode ( ",", $result ) . "]";
82  }
83 }
static createAction($controller, $action, $parameters="", $content="", $createView=false, $route=[])
Definition: Maker.php:17
static createView($controller, $action)
Definition: Maker.php:68
static openReplaceWriteFromTemplateFile($source, $destination, $keyAndValues)
Definition: UFileSystem.php:73
static openReplaceInTemplateFile($source, $keyAndValues)
Definition: UFileSystem.php:65
static getMethods($strMethods)
Definition: Maker.php:75
static replaceWriteFromContent($content, $destination, $keyAndValues)
Definition: UFileSystem.php:89
static getClassSimpleName($classnameWithNamespace)
Definition: ClassUtils.php:135