create_example.php 4.0 KB

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