create_example.php 4.0 KB

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