PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/public/demo/renameTest.php

http://github.com/egeniq/adapto
PHP | 471 lines | 336 code | 69 blank | 66 comment | 56 complexity | 5f28484362177b08248c0572894b56f5 MD5 | raw file
  1. <?php
  2. include('atk.inc');
  3. $callback = new renameAtk();
  4. $traverser = new Adapto_DirectoryTraverser();
  5. $traverser->addCallbackObject($callback);
  6. $traverser->traverse('atk');
  7. class renameAtk
  8. {
  9. public $_newDir = "Adapto_new"; // defaulted to public
  10. public $_compatDir = "Adapto_compatibility"; // defaulted to public
  11. public $_renameRules = array("DG", 'cache_'); // defaulted to public
  12. public $_ignoredMethods = array("atkconfig","atkMetaEntity"," // defaulted to public
  13. public $_refactoredClassNames = array( // defaulted to public
  14. "Adapto_menuinterface" => "Adapto_Menu_Interface",
  15. "MenuInterface" => "Adapto_Menu_Interface",
  16. "Adapto_Menu_Adapto_menuinterface" => "Adapto_Menu_Interface",
  17. "Adapto_menuinterface" => "Adapto_Menu_Interface",
  18. "Adapto_Menu_menuinterface" => "Adapto_Menu_Interface",
  19. "menuinterface" => "Adapto_Menu_Interface",
  20. "Adapto_PlainMenu" =>"Adapto_Menu_Plain",
  21. "Adapto_Relation_Attribute" => "Adapto_Attribute"
  22. );
  23. public $_classType; // defaulted to public
  24. public function __construct()
  25. {
  26. //Create new and compat dir when they're not there
  27. if(!is_dir($this->_newDir) || !is_dir($this->_compatDir))
  28. {
  29. mkdir($this->_newDir, 0777);
  30. mkdir($this->_compatDir, 0777);
  31. }
  32. }
  33. /*
  34. * Convert fullpath to :
  35. * 2 - New dir
  36. * 3 - Compat dir
  37. * 4 - Include dir
  38. * 5 - Name of class
  39. */
  40. public function convertDir($fullPath, $mode)
  41. {
  42. //Get parts of path
  43. $fullPathParts = explode("/", $fullPath);
  44. $fullPathParts[0] = "Atk";
  45. switch($mode)
  46. {
  47. case 2:
  48. //Get path to new dir
  49. $fullPathParts[0] = $this->_newDir;
  50. if(strstr(end($fullPathParts),".")){
  51. unset($fullPathParts[count($fullPathParts) -1] );
  52. }
  53. return implode("/", $this->cleanDirName($fullPathParts));
  54. break;
  55. case 3:
  56. //Get path to compat dir
  57. $fullPathParts[0] = $this->_compatDir;
  58. return implode("/", $fullPathParts);
  59. break;
  60. case 4:
  61. //Get path to Include dir
  62. unset($fullPathParts[count($fullPathParts) -1]);
  63. $fullPathParts = array_map(
  64. function($item) { return ucfirst($item); },
  65. $fullPathParts
  66. );
  67. return implode("/", $this->cleanDirName($fullPathParts));
  68. break;
  69. case 5:
  70. unset($fullPathParts[count($fullPathParts) -1]);
  71. $fullPathParts = array_map(
  72. function($item) { return ucfirst($item); },
  73. $fullPathParts
  74. );
  75. $className = implode("_", $this->cleanDirName($fullPathParts));
  76. // echo $className ."\n";
  77. return $className;
  78. break;
  79. }
  80. }
  81. public function cleanDirName($fullPathParts)
  82. {
  83. $lastItem = ucfirst(end($fullPathParts));
  84. //Simple hack to convert names like 'attributes' to 'Attribute'
  85. if(substr($lastItem, strlen($lastItem) -1, strlen($lastItem)) == "s")
  86. {
  87. $lastItem = substr($lastItem, 0, strlen($lastItem) -1);
  88. }
  89. $fullPathParts[count($fullPathParts) -1] = $lastItem;
  90. return $fullPathParts;
  91. }
  92. /**
  93. * Rename all visited directory's to ATK style names if name not is existing
  94. *
  95. * @param string $fullPath
  96. */
  97. public function visitDir($fullPath)
  98. {
  99. //Get path to compat dir
  100. $compatDir = trim($this->convertDir($fullPath, 3));
  101. //Get path to new dir
  102. $newDir = trim($this->convertDir($fullPath, 2));
  103. //if folder in compat dir not exists create dir
  104. if(!is_dir($compatDir)){
  105. mkdir( $compatDir, 0777);
  106. }
  107. //if folder in new dir not exists, create dir
  108. if(! is_dir($newDir) &&
  109. ($this->_containsFilesWithExt($this->getDirContents($fullPath), "php")
  110. || $this->_containsFilesWithExt($this->getDirContents($fullPath), "inc")))
  111. {
  112. mkdir($newDir, 0777);
  113. }
  114. }
  115. public function visitFile($fullPath)
  116. {
  117. $folder= explode("/",trim($fullPath));
  118. if(preg_match('/.(?P<filename>atk.+)(?P<suffix>.inc)/', $fullPath, $matches)
  119. && preg_match('/.(?P<prefix>class.)/', $fullPath, $classmatches))
  120. {
  121. //If true this is an ATK class
  122. /*Example of input:
  123. /atk/ui/class.atkpage.inc
  124. */
  125. $oldClassName = $this->extractClassname($fullPath);
  126. $newDir = $this->convertDir($fullPath, 2);
  127. $newClassName = str_replace("atk","", $oldClassName);
  128. if($oldClassName == "atkStatement"){
  129. $this->_classType = "abstract";
  130. }
  131. $newClassName = $this->generateClassName($newClassName, $newDir);
  132. $compatDir = $this->convertDir($fullPath, 3);
  133. if(!strstr(strtolower(basename($newDir)), strtolower($newClassName)) == false && count(explode("/",$newDir)) < 3)
  134. {
  135. $newDir = $this->_newDir . "/";
  136. }
  137. else if(!strstr(strtolower(basename($newDir)), strtolower($newClassName)) == false && count(explode("/",$newDir)) > 2)
  138. {
  139. $dirParts = explode("/",$newDir);
  140. $newDir = $this->_newDir . "/" . $dirParts[1] ."/";
  141. }
  142. $this->createCompatStyleClass($oldClassName, $newClassName, $compatDir, $newDir);
  143. $this->createNewStyleClass($oldClassName,$newClassName, $fullPath, $newDir);
  144. }
  145. else if(substr( $fullPath, strlen( $fullPath ) - 4) == ".php" && in_array(strtolower($folder[count($folder)-2]), array("ui","helper","exceptions","placeholder")))
  146. {
  147. $newClassName = end($folder);
  148. $newDir = $this->convertDir($fullPath, 2) . "/" . $newClassName;
  149. $this->copyFile($fullPath,$newDir);
  150. }
  151. else
  152. {
  153. $compatDir = $this->convertDir($fullPath, 3);
  154. $this->copyFile($fullPath,$compatDir);
  155. // echo "No match on filename " . $fullPath . "\n";
  156. }
  157. }
  158. public function copyFile($oldLocation, $newLocation)
  159. {
  160. $content = $this->getFileContents($oldLocation);
  161. $this->createFile($content, $newLocation);
  162. }
  163. public function extractClassname($fullPath)
  164. {
  165. try
  166. {
  167. $className = "";
  168. $sData = file_get_contents($fullPath);
  169. if(preg_match('/.(\sclass+)\s(atk.+)\s(extends|implements.+)/', $sData, $matches)
  170. || preg_match('/.(\sabstract class|\sclass|\sinterface+)\s(atk.+)/', $sData, $matches)
  171. || preg_match('/.(class+)\s(atk.+)\s(extends|implements.+)/', $sData, $matches)
  172. || preg_match('/.(class|interface+)\s(atk.+)/', $sData, $matches))
  173. {
  174. $this->_classType= $matches[0];
  175. $className = trim($matches[2]);
  176. }
  177. else
  178. {
  179. echo "No match: " . $fullPath . "\n";
  180. }
  181. }
  182. catch(Exception $e)
  183. {
  184. die( "Exception: ". $e->getMessage());
  185. }
  186. return $className;
  187. }
  188. public function generateClassName($className, $location)
  189. {
  190. $dir = end(explode("/", $location));
  191. if(! strstr($dir,".php") == false){
  192. $tmpLocation = explode("/", $location);
  193. $dir = $tmpLocation[sizeof($tmpLocation) -2];
  194. }
  195. if(count(explode("_",$className)) > 1)
  196. {
  197. return $className;
  198. }
  199. foreach($this->_renameRules as $rule){
  200. $className = str_replace($rule,"",$className);
  201. }
  202. if(strlen(str_replace($dir,"",$className)) > 0){
  203. $className = str_replace($dir,"",$className);
  204. }
  205. return $className;
  206. }
  207. public function generateFileLocation($className, $location)
  208. {
  209. if(count(explode("_", $className)) < 1){
  210. $className = $this->generateClassName($className, $location);
  211. }
  212. else
  213. {
  214. $className = end(explode("_",$className));
  215. }
  216. //Simple hack to convert names like 'attributes' to 'Attribute'
  217. if(substr($location, strlen($location) -1, strlen($location)) == "s")
  218. {
  219. $location = substr($location, 0, strlen($location) -1);
  220. }
  221. $fileLocation = $location . "/" . $className . ".php";
  222. return $fileLocation;
  223. }
  224. /**
  225. * Read all the entries of a directory.
  226. * @param String $path The path to read the contents from.
  227. * @return array Array containing the contents of the directory.
  228. */
  229. function getDirContents($path)
  230. {
  231. $result = array();
  232. $dir = @opendir($path);
  233. while (($file = @readdir($dir)) !== false)
  234. {
  235. $result[] = $file;
  236. }
  237. @closedir($dir);
  238. sort($result);
  239. return $result;
  240. }
  241. public function getFileContents($fullPath)
  242. {
  243. try
  244. {
  245. $content = file_get_contents($fullPath);
  246. }
  247. catch(Exception $e)
  248. {
  249. die( "Exception: ". $e->getMessage());
  250. }
  251. return $content;
  252. }
  253. public function createNewStyleClass($oldClassName, $newClassName, $oldLocation, $newLocation)
  254. {
  255. //TODO a test to match the constructor /\batkBlaat\b/
  256. $content = $this->getFileContents($oldLocation);
  257. if(array_key_exists($newClassName, $this->_refactoredClassNames))
  258. {
  259. $newClassName = $this->_refactoredClassNames[$newClassName];
  260. }
  261. else
  262. {
  263. if(strstr(strtolower($this->convertDir($oldLocation, 5)), strtolower($newClassName)) == false)
  264. {
  265. $newClassName = $this->convertDir($oldLocation, 5) . "_" . $newClassName;
  266. }
  267. else
  268. {
  269. $newClassName = $this->convertDir($oldLocation, 5);
  270. }
  271. }
  272. $newLocation= $this->generateFileLocation($newClassName, $newLocation);//$newLocation . "/" . $newClassName . ".php";
  273. // $content = preg_replace("/\batk[A-Z].\b/", bst)
  274. if(!in_array($oldClassName,$this->_ignoredMethods) && !in_array(strtolower($oldClassName),$this->_ignoredMethods) ){
  275. $content = preg_replace('/(public\sfunction\s|protected\sfunction\s|function\s)('.$oldClassName.'\()/', "public function __construct(",$content);
  276. $content = preg_replace('/(public\sfunction\s|function\s)('.strtolower($oldClassName).'\()/', "public function __construct(",$content);
  277. }
  278. $content = preg_replace('/\b'.$oldClassName.'\b/', $newClassName,$content);
  279. //Covers current class constructor
  280. $content = preg_replace('/(this->)('.$oldClassName.')(\()/', "\1__construct",$content);
  281. //Covers parent class constructor
  282. if(preg_match('/.(extends|implements).+/', $content, $matches))
  283. {
  284. $extendedClass = end(explode(" ",trim($matches[0])));
  285. $content = preg_replace('/(\$this->)('.$extendedClass.')/', "parent::__construct", $content);
  286. //
  287. // $extendedClassNew = str_replace("atk","",$extendedClass);
  288. // $tmpExtClass = $this->convertDir($oldLocation, 5) . "_" . $this->generateClassName($extendedClassNew, $newLocation);
  289. // $extendedClassNew = str_replace("atk","Adapto_",$extendedClass);
  290. //
  291. // $boolExtClass = strstr($tmpExtClass,"__");
  292. // if($boolExtClass == false && count(explode("/",$oldLocation)) > 2){
  293. // $extendedClassNew = $tmpExtClass;
  294. // }
  295. // if(array_key_exists($extendedClassNew, $this->_refactoredClassNames))
  296. // {
  297. // $extendedClassNew = $this->_refactoredClassNames[$extendedClassNew];
  298. // }
  299. //
  300. // $extendedClassNew = preg_replace("/(_)$/","",$extendedClassNew);
  301. // $extendedClassNew = implode("_",array_unique(explode("_", $extendedClassNew)));
  302. //
  303. // //fix for relations classes
  304. // if(preg_match('/(Meta)(.*?)(Relation)/', $extendedClassNew,$matches )
  305. // && !preg_match('/(Meta)(.*?)(Relation)(.*?)(Meta.+)/',$extendedClassNew,$matchesTwo))
  306. // {
  307. // $extendedClassNew = preg_replace("/(Meta_)/","",$extendedClassNew);
  308. //// echo $extendedClassNew . "\n";
  309. // }
  310. //
  311. // $extendedClassNew = preg_replace("/(_)$/","",$extendedClassNew);
  312. // $content = preg_replace('/\b'.$extendedClass.'\b/', $extendedClassNew, $content);
  313. }
  314. if(!$this->createFile($content, $newLocation))
  315. {
  316. echo "Something went wrong while writing this file" . $location. "\n";
  317. }
  318. }
  319. public function createCompatStyleClass($oldStyleName, $newStyleName, $oldLocation, $newLocation)
  320. {
  321. $newClassLocation = $this->convertDir($oldLocation, 4) . "/". $this->generateClassName($newStyleName, $newLocation) .".php";
  322. if($newLocation == $this->_newDir . "/"){
  323. $newClassLocation = "Atk" ."/" . $this->generateClassName($newStyleName, $newLocation) .".php";
  324. }
  325. if(strstr(strtolower($this->convertDir($oldLocation, 5)), strtolower($newStyleName)) == false){
  326. $newStyleName = $this->convertDir($oldLocation, 5) . "_" . $newStyleName;
  327. }
  328. else
  329. {
  330. $newStyleName = $this->convertDir($oldLocation, 5);
  331. }
  332. if(array_key_exists($newStyleName, $this->_refactoredClassNames))
  333. {
  334. $newStyleName = $this->_refactoredClassNames[$newStyleName];
  335. $newClassLocation = str_replace("_","/", $newStyleName) .".php";
  336. }
  337. $newClassLocation = str_replace("UI","Ui", $newClassLocation);
  338. $newClassLocation = str_replace("DataGrid","Datagrid", $newClassLocation);
  339. $newClassLocation = str_replace("RecordList","Recordlist", $newClassLocation);
  340. //Create an compat class with provided template
  341. $type = $this->_getClassType();
  342. extract(array($newClassLocation, $oldStyleName, $newStyleName));
  343. ob_start();
  344. switch($type)
  345. {
  346. case 'interface':
  347. include 'compatInterfaceTemplate.php';
  348. break;
  349. case 'abstract':
  350. include 'compatAbstractClassTemplate.php';
  351. break;
  352. default:
  353. case 'class':
  354. include 'compatClassTemplate.php';
  355. break;
  356. }
  357. $content = ob_get_clean();
  358. ob_end_clean();
  359. if(!$this->createFile($content, $oldLocation))
  360. {
  361. echo "Something went wrong while writing this file" . $oldLocation. "\n";
  362. }
  363. }
  364. public function createFile($content, $location)
  365. {
  366. try
  367. {
  368. $fp = fopen($location, 'w');
  369. fwrite($fp, $content);
  370. fclose($fp);
  371. }
  372. catch(Exception $e)
  373. {
  374. echo "Write exception: " . $e->getMessage();
  375. return false;
  376. }
  377. return true;
  378. }
  379. private function _getClassType()
  380. {
  381. if(!strstr($this->_classType, "abstract")==false)
  382. {
  383. return "abstract";
  384. }
  385. else if(!strstr($this->_classType, "interface")==false && strstr($this->_classType, "menuinterface")==false )
  386. {
  387. return "interface";
  388. }
  389. return "class";
  390. }
  391. private function _containsFilesWithExt(array $dirContents, $ext)
  392. {
  393. foreach($dirContents as $file){
  394. $pathParts = pathinfo($file);
  395. if($pathParts['extension'] == $ext){
  396. return true;
  397. }
  398. }
  399. return false;
  400. }
  401. }
  402. ?>