PageRenderTime 61ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/mustache/mustache/bin/create_example.php

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