create_example.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * A commandline script to create an example and the needed files
  4. * use like this:
  5. * $ php create_example.php my_new_example
  6. *
  7. * and the folder my_new_example will be created in the examples/ folder containing 3 files
  8. *
  9. */
  10. // some constants
  11. define('USAGE', <<<USAGE
  12. USAGE: php {$argv[0]} example_name
  13. This creates a new example and the corresponding files in the examples/ directory
  14. USAGE
  15. );
  16. define('DS', DIRECTORY_SEPARATOR);
  17. define('EXAMPLE_PATH', dirname(__FILE__) . DS . "examples");
  18. /**
  19. * transform a string to lowercase using underlines.
  20. * Examples:
  21. * String -> string
  22. * AString -> a_string
  23. * SomeStrings -> some_strings
  24. * AStringMore -> a_string_more
  25. *
  26. * @param string $name
  27. * @access public
  28. * @return string
  29. */
  30. function getLowerCaseName($name) {
  31. return preg_replace_callback("/([A-Z])/", create_function (
  32. '$match',
  33. 'return "_" . strtolower($match[1]);'
  34. ), lcfirst($name));
  35. }
  36. /**
  37. * transform a string to Uppercase (camelcase)
  38. * Examples
  39. * string -> String
  40. * a_string -> AString
  41. * some_strings -> SomeStrings
  42. * a_string_more -> AStringMore -> a_string_more
  43. *
  44. * @param string $name
  45. * @access public
  46. * @return string
  47. */
  48. function getUpperCaseName($name) {
  49. return preg_replace_callback("/_([a-z])/", create_function (
  50. '$match',
  51. 'return strtoupper($match{1});'
  52. ), ucfirst($name));
  53. }
  54. /**
  55. * return the given value and echo it out appending "\n"
  56. *
  57. * @param mixed $value
  58. * @access public
  59. * @return mixed
  60. */
  61. function out($value) {
  62. echo $value . "\n";
  63. return $value;
  64. }
  65. /**
  66. * create Path for certain files in an example
  67. * returns the directory name if only $directory is given.
  68. * if an extension is given a complete filename is returned.
  69. * the returned filename will be echoed out
  70. *
  71. * @param string $directory directory without / at the end
  72. * @param string $filename filename without path and extension
  73. * @param string $extension extension of the file without "."
  74. * @access public
  75. * @return string
  76. */
  77. function buildPath($directory, $filename = null, $extension = null) {
  78. return out(EXAMPLE_PATH . DS . $directory.
  79. ($extension !== null && $filename !== null ? DS . $filename. "." . $extension : ""));
  80. }
  81. /**
  82. * creates the directory for the example
  83. * the script die()'s if mkdir() fails
  84. *
  85. * @param string $directory
  86. * @access public
  87. * @return void
  88. */
  89. function createDirectory($directory) {
  90. if(!@mkdir(buildPath($directory))) {
  91. die("FAILED to create directory\n");
  92. }
  93. }
  94. /**
  95. * create a file for the example with the given $content
  96. * the script die()'s if fopen() fails
  97. *
  98. * @param string $directory directory without / at the end
  99. * @param string $filename filename without path and extension
  100. * @param string $extension extension of the file without "."
  101. * @param string $content the content of the file
  102. * @access public
  103. * @return void
  104. */
  105. function createFile($directory, $filename, $extension, $content = "") {
  106. $handle = @fopen(buildPath($directory, $filename, $extension), "w");
  107. if($handle) {
  108. fwrite($handle, $content);
  109. fclose($handle);
  110. } else {
  111. die("FAILED to create file\n");
  112. }
  113. }
  114. /**
  115. * routine to create the example directory and 3 files
  116. *
  117. * if the $example_name is "SomeThing" the following files will be created
  118. * examples/some_thing
  119. * examples/some_thing/some_thing.mustache
  120. * examples/some_thing/some_thing.txt
  121. * examples/some_thing/SomeThing.php
  122. *
  123. * @param mixed $example_name
  124. * @access public
  125. * @return void
  126. */
  127. function main($example_name) {
  128. $lowercase = getLowerCaseName($example_name);
  129. $uppercase = getUpperCaseName($example_name);
  130. createDirectory($lowercase);
  131. createFile($lowercase, $lowercase, "mustache");
  132. createFile($lowercase, $lowercase, "txt");
  133. createFile($lowercase, $uppercase, "php", <<<CONTENT
  134. <?php
  135. class {$uppercase} extends Mustache {
  136. }
  137. CONTENT
  138. );
  139. }
  140. // check if enougth arguments are given
  141. if(count($argv) > 1) {
  142. // get the name of the example
  143. $example_name = $argv[1];
  144. main($example_name);
  145. } else {
  146. echo USAGE;
  147. }