/knife/engine/base_generator.php

https://github.com/siphoc/knife · PHP · 360 lines · 146 code · 48 blank · 166 comment · 22 complexity · f8db4b031212df59ddea326daebf9071 MD5 · raw file

  1. <?php
  2. /**
  3. * This source file is a part of the Knife CLI Tool for Fork CMS.
  4. * More information can be found on http://www.fork-cms.com
  5. *
  6. * @author Jelmer Snoeck <jelmer.snoeck@netlash.com>
  7. */
  8. class KnifeBaseGenerator
  9. {
  10. /**
  11. * The arguments
  12. *
  13. * @var array
  14. */
  15. protected $arg;
  16. /**
  17. * Options
  18. *
  19. * @var array
  20. */
  21. protected $options;
  22. /**
  23. * The location
  24. *
  25. * @var string
  26. */
  27. protected $location;
  28. /**
  29. * The module
  30. *
  31. * @var string
  32. */
  33. protected $module, $moduleFolder;
  34. /**
  35. * Constructor
  36. *
  37. * @param array $arguments
  38. */
  39. public function __construct(array $arguments = array(), array $options = array())
  40. {
  41. // set the arguments
  42. $this->arg = $arguments;
  43. // the options
  44. $this->options = $options;
  45. // initiate
  46. if(!empty($arguments)) $this->init();
  47. else $this->execute();
  48. }
  49. /**
  50. * Creates a valid file name
  51. *
  52. * @return string
  53. * @param string $name The given name.
  54. */
  55. public function buildDirName($name)
  56. {
  57. // lowercase
  58. $name = strtolower($name);
  59. // remove all non alphabetical or underscore characters
  60. $name = preg_replace("/[^a-z_\s]/", "", $name);
  61. // return
  62. return $name;
  63. }
  64. /**
  65. * Creates a valid file name
  66. *
  67. * @return string
  68. * @param string $name The given name.
  69. * @param string[optional] $ext The file extension.
  70. */
  71. public function buildFileName($name, $ext = 'php')
  72. {
  73. // lowercase
  74. $name = strtolower($name);
  75. $ext = strtolower($ext);
  76. // remove all non alphabetical or underscore characters
  77. $name = preg_replace("/[^a-z0-9_\s]/", "", $name);
  78. $ext = preg_replace("/[^a-z0-9\s]/", "", $ext);
  79. // need extension?
  80. $newName = $name;
  81. $newName.= ($ext != '') ? '.' . $ext : '';
  82. // return
  83. return $newName;
  84. }
  85. /**
  86. * Creates a valid class name
  87. *
  88. * @return string
  89. * @param string $name The given name.
  90. */
  91. public function buildName($name)
  92. {
  93. // lowercase
  94. $name = strtolower($name);
  95. // remove all non alphabetical or underscore characters
  96. $name = preg_replace("/[^a-zA-Z0-9_\s]/", "", $name);
  97. // split the name on _
  98. $parts = explode('_', $name);
  99. // the new name
  100. $newName = '';
  101. // loop trough the parts to ucfirst it
  102. foreach($parts as $part) $newName.= ucfirst($part);
  103. // return
  104. return $newName;
  105. }
  106. /**
  107. * Removes the spaces from a string
  108. *
  109. * @return string
  110. * @param string $string The string to edit.
  111. */
  112. public function cleanString($string)
  113. {
  114. return str_replace(' ', '', $string);
  115. }
  116. /**
  117. * The error handler. This gets the PHPDoc from the create function and prints
  118. * it in a proper way.
  119. *
  120. * @param string $class The class to search in.
  121. * @param string $function The function to search.
  122. */
  123. protected function errorHandler($class, $function)
  124. {
  125. // get the documentation
  126. $reflectionClass = new ReflectionClass($class);
  127. $reflectionFunction = $reflectionClass->getMethod($function);
  128. $reflectionDocumentation = $reflectionFunction->getDocComment();
  129. // clean the documentation
  130. // @todo propper cleaning function
  131. $reflectionDocumentation = str_replace(' ', '', $reflectionDocumentation);
  132. $reflectionDocumentation = str_replace('/**', '', $reflectionDocumentation);
  133. $reflectionDocumentation = str_replace(' * ', '', $reflectionDocumentation);
  134. $reflectionDocumentation = str_replace(' */', '', $reflectionDocumentation);
  135. $reflectionDocumentation = str_replace(' *', '', $reflectionDocumentation);
  136. // throw new exception
  137. throw new Exception($reflectionDocumentation);
  138. }
  139. /**
  140. * The execute action, this will be used for a read action of an object.
  141. */
  142. protected function execute() {}
  143. /**
  144. * Fetches all the module names
  145. *
  146. * @return array
  147. */
  148. protected function getAllActiveModules()
  149. {
  150. try
  151. {
  152. // get the active modules
  153. $modules = Knife::getDB()->getColumn('SELECT m.name
  154. FROM modules AS m
  155. WHERE m.active = ?',
  156. array('Y'));
  157. }
  158. catch(Exception $e)
  159. {
  160. if(DEV_MODE) throw $e;
  161. else throw new Exception('Something went wrong while connecting to the database.');
  162. }
  163. // return
  164. return $modules;
  165. }
  166. /**
  167. * Gets the location
  168. *
  169. * @return string
  170. */
  171. protected function getLocation()
  172. {
  173. return $this->location;
  174. }
  175. /**
  176. * Gets the module
  177. *
  178. * @return string
  179. */
  180. protected function getModule()
  181. {
  182. return $this->module;
  183. }
  184. /**
  185. * Gets the module
  186. *
  187. * @return string
  188. */
  189. protected function getModuleFolder()
  190. {
  191. return $this->moduleFolder;
  192. }
  193. /**
  194. * The init function, this sets the needed variable names
  195. * and calls the required actions.
  196. */
  197. protected function init() {}
  198. /**
  199. * Creates the directories from a given array
  200. *
  201. * @param array $dirs The directories to create
  202. */
  203. protected function makeDirs(array $dirs)
  204. {
  205. // the main dir
  206. $mainDir = '';
  207. // loop the directories
  208. foreach($dirs as $type => $dir)
  209. {
  210. // create a new dir if this is the main dir
  211. if($type == 'main')
  212. {
  213. mkdir($dir);
  214. $mainDir = $dir . '/';
  215. continue;
  216. }
  217. // loob the dir to check for subdirs if this isn't the main
  218. foreach($dir as $name => $subdir)
  219. {
  220. // no subdirs
  221. if(!is_array($subdir)) mkdir($mainDir . $subdir);
  222. // more subdirs
  223. else
  224. {
  225. // create new array to pass
  226. $tmpArray = array(
  227. 'main' => $mainDir . $name,
  228. 'sub' => $subdir
  229. );
  230. // make the dir
  231. $this->makeDirs($tmpArray);
  232. }
  233. }
  234. }
  235. }
  236. /**
  237. * Creates a file in a specific directory
  238. *
  239. * @param string $file The file name.
  240. * @param string[optional] $input The input for the file.
  241. */
  242. protected function makeFile($file, $input = null)
  243. {
  244. // create the file
  245. $oFile = fopen($file, 'w');
  246. // input?
  247. if($input !== null) fwrite($oFile, $input);
  248. // close the file
  249. fclose($oFile);
  250. }
  251. /**
  252. * Reads the content of a file
  253. *
  254. * @return string
  255. * @param string $file The file path.
  256. */
  257. protected function readFile($file)
  258. {
  259. // file exists?
  260. if(!file_exists($file)) throw new Exception('The given file(' . $file .') does not exist.');
  261. // open the file
  262. $oFile = fopen($file, 'r');
  263. // read the file
  264. $rFile = fread($oFile, filesize($file));
  265. // close the file
  266. fclose($oFile);
  267. // return
  268. return $rFile;
  269. }
  270. /**
  271. * Sets the location
  272. *
  273. * @param string $location The location.
  274. */
  275. protected function setLocation($location)
  276. {
  277. // set short terms
  278. if($location == 'f') $location = 'frontend';
  279. if($location == 'b') $location = 'backend';
  280. // set the location
  281. if($location == 'frontend' || $location == 'backend') $this->location = (string) strtolower($location);
  282. // not a valid location
  283. else throw new Exception('This(' . $location . ') is not a valid location');
  284. }
  285. /**
  286. * Sets the module
  287. *
  288. * @param string $module The module.
  289. */
  290. protected function setModule($module)
  291. {
  292. // the location path
  293. $locationPath = ($this->location == 'frontend') ? FRONTENDPATH : BACKENDPATH;
  294. // module exists?
  295. if(is_dir($locationPath . 'modules/' . $module))
  296. {
  297. $this->module = (string) $this->buildName($module);
  298. $this->moduleFolder = $this->buildDirName($module);
  299. }
  300. // doesnt exist
  301. else throw new Exception('The given module(' . $module . ') does not exist.');
  302. }
  303. /**
  304. * The success handler. This shows a success message.
  305. *
  306. * @param string $message The message to show.
  307. */
  308. protected function successHandler($message)
  309. {
  310. echo $message . "\n";
  311. }
  312. }