create_example.php 4.1 KB

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