create_example.php 4.1 KB

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