create_example.php 4.0 KB

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