/framework/core/build/functions.php

http://zoop.googlecode.com/ · PHP · 208 lines · 138 code · 28 blank · 42 comment · 16 complexity · 51bf1636023020f6b3c131645976872d MD5 · raw file

  1. <?php
  2. define('_n', "\n");
  3. function _en()
  4. {
  5. echo _n;
  6. }
  7. function _mkdir($path, $mode = 0775)
  8. {
  9. if(is_dir($path))
  10. {
  11. echo "notice: '$path' already exists\n";
  12. return;
  13. }
  14. _status("creating directory '$path'");
  15. mkdir($path, $mode, true);
  16. }
  17. function _chgrp($path, $group, $recursive = false)
  18. {
  19. $r = $recursive ? 'recursively' : '';
  20. _status("changing group of '$path' to '$group' $r");
  21. if($recursive)
  22. _chgrp_r($path, $group);
  23. else
  24. chgrp($path, $group);
  25. }
  26. function _chgrp_r($path, $group)
  27. {
  28. chgrp($path, $group);
  29. if(!is_dir($path))
  30. return;
  31. $dir = new DirectoryIterator($path);
  32. foreach($dir as $fileinfo)
  33. if(!$fileinfo->isDot())
  34. _chgrp_r($path . '/' . $fileinfo->getFilename(), $group);
  35. }
  36. function _chmod($path, $mode, $recursive = false)
  37. {
  38. $r = $recursive ? 'recursively' : '';
  39. $m = decoct($mode);
  40. _status("setting mode of '$path' to '$m' $r");
  41. if($recursive)
  42. _chmod_r($path, $mode);
  43. else
  44. chmod($path, $mode);
  45. }
  46. function _chmod_r($path, $mode)
  47. {
  48. chmod($path, $mode);
  49. if(!is_dir($path))
  50. return;
  51. $dir = new DirectoryIterator($path);
  52. foreach($dir as $fileinfo)
  53. if(!$fileinfo->isDot())
  54. _chmod_r($path . '/' . $fileinfo->getFilename(), $mode);
  55. }
  56. /*
  57. function _chmod($path, $mode, $recursive = false)
  58. {
  59. $m = decoct($mode);
  60. if($recursive)
  61. {
  62. _status("setting mode of '$path' to '$m' recursively");
  63. _chmod_r($path, $mode);
  64. }
  65. else
  66. {
  67. echo "path = $path\n";
  68. $curmodString = substr(decoct(fileperms ($path)), -3, 3);
  69. var_dump($curmodString);
  70. $newmodString = substr(decoct($mode), -3, 3);
  71. if($curmodString != $newmodString)
  72. {
  73. _status("mode of '$path' is already '$m'");
  74. }
  75. else
  76. {
  77. _status("setting mode of '$path' to '$m'");
  78. chmod($path, $mode);
  79. }
  80. }
  81. }
  82. function _chmod_r($path, $mode)
  83. {
  84. chmod($path, $mode);
  85. if(!is_dir($path))
  86. return;
  87. $dir = new DirectoryIterator($path);
  88. foreach($dir as $fileinfo)
  89. if(!$fileinfo->isDot())
  90. _chmod_r($path . '/' . $fileinfo->getFilename(), $mode);
  91. }
  92. */
  93. // this is a hack, we need to just set this up with proper OOP and conveninece functions
  94. function _forcegen()
  95. {
  96. global $FORCEGEN;
  97. $FORCEGEN = true;
  98. }
  99. function _fetch($path, $params = array())
  100. {
  101. global $_assigns;
  102. $templatePath = $path;
  103. $gui = new gui();
  104. if($_assigns)
  105. foreach($_assigns as $name => $value)
  106. $gui->assign($name, $value);
  107. foreach($params as $name => $value)
  108. {
  109. // echo "param: $name => $value\n";
  110. $gui->assign($name, $value);
  111. }
  112. return $gui->fetch($templatePath . '.tpl');
  113. }
  114. function _gen($path, $filePath = '', $params = array())
  115. {
  116. global $FORCEGEN;
  117. if(!$filePath)
  118. $filePath = $path;
  119. $content = _fetch($path, $params);
  120. _status("creating generated file '" . getcwd() . '/' . $filePath . "'");
  121. if(isset($FORCEGEN) && $FORCEGEN)
  122. $forcegen = true;
  123. else
  124. $forcegen = false;
  125. if(file_exists($filePath) && !$forcegen)
  126. _status("There is already a file at $filePath");
  127. file_put_contents($filePath, $content);
  128. }
  129. function _cd($path)
  130. {
  131. _status("changing directory to '$path'");
  132. chdir($path);
  133. }
  134. function _ln($target, $link)
  135. {
  136. global $FORCEGEN;
  137. _status("trying to link '$link' to '$target'");
  138. if(file_exists($link))
  139. {
  140. if($FORCEGEN)
  141. {
  142. if(is_link($link))
  143. {
  144. _status("Removing existing symlink at at $link");
  145. unlink($link);
  146. }
  147. else
  148. {
  149. _status("There is already a non-symlink file at $link");
  150. return;
  151. }
  152. }
  153. else
  154. {
  155. _status("There is already a file at $link");
  156. return;
  157. }
  158. }
  159. _status("linking '$link' to '$target'");
  160. symlink($target, $link);
  161. }
  162. function _status($message)
  163. {
  164. echo "status: $message" . _n;
  165. }
  166. function _assign($name, $value)
  167. {
  168. global $_assigns;
  169. _status("assigning '$value' to '$name'");
  170. return $_assigns[$name] = $value;
  171. }
  172. function _run($command, $params)
  173. {
  174. Ex::echoOn();
  175. return Ex::pass($command, $params);
  176. Ex::echoOff();
  177. }