/RestAPI/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php

https://gitlab.com/martinstti/silex-microframework-rest · PHP · 171 lines · 77 code · 24 blank · 70 comment · 5 complexity · 0879adee402200e0e5e1fcb279ca3762 MD5 · raw file

  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Tools;
  20. /**
  21. * Class to generate entity repository classes
  22. *
  23. *
  24. * @link www.doctrine-project.org
  25. * @since 2.0
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  28. * @author Jonathan Wage <jonwage@gmail.com>
  29. * @author Roman Borschel <roman@code-factory.org>
  30. */
  31. class EntityRepositoryGenerator
  32. {
  33. private $repositoryName;
  34. protected static $_template =
  35. '<?php
  36. <namespace>
  37. /**
  38. * <className>
  39. *
  40. * This class was generated by the Doctrine ORM. Add your own custom
  41. * repository methods below.
  42. */
  43. class <className> extends <repositoryName>
  44. {
  45. }
  46. ';
  47. /**
  48. * @param string $fullClassName
  49. *
  50. * @return string
  51. */
  52. public function generateEntityRepositoryClass($fullClassName)
  53. {
  54. $variables = array(
  55. '<namespace>' => $this->generateEntityRepositoryNamespace($fullClassName),
  56. '<repositoryName>' => $this->generateEntityRepositoryName($fullClassName),
  57. '<className>' => $this->generateClassName($fullClassName)
  58. );
  59. return str_replace(array_keys($variables), array_values($variables), self::$_template);
  60. }
  61. /**
  62. * Generates the namespace, if class do not have namespace, return empty string instead.
  63. *
  64. * @param string $fullClassName
  65. *
  66. * @return string $namespace
  67. */
  68. private function getClassNamespace($fullClassName)
  69. {
  70. $namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\'));
  71. return $namespace;
  72. }
  73. /**
  74. * Generates the class name
  75. *
  76. * @param string $fullClassName
  77. *
  78. * @return string
  79. */
  80. private function generateClassName($fullClassName)
  81. {
  82. $namespace = $this->getClassNamespace($fullClassName);
  83. $className = $fullClassName;
  84. if ($namespace) {
  85. $className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName));
  86. }
  87. return $className;
  88. }
  89. /**
  90. * Generates the namespace statement, if class do not have namespace, return empty string instead.
  91. *
  92. * @param string $fullClassName The full repository class name.
  93. *
  94. * @return string $namespace
  95. */
  96. private function generateEntityRepositoryNamespace($fullClassName)
  97. {
  98. $namespace = $this->getClassNamespace($fullClassName);
  99. return $namespace ? 'namespace ' . $namespace . ';' : '';
  100. }
  101. /**
  102. * @param string $fullClassName
  103. *
  104. * @return string $repositoryName
  105. */
  106. private function generateEntityRepositoryName($fullClassName)
  107. {
  108. $namespace = $this->getClassNamespace($fullClassName);
  109. $repositoryName = $this->repositoryName ?: 'Doctrine\ORM\EntityRepository';
  110. if ($namespace && $repositoryName[0] !== '\\') {
  111. $repositoryName = '\\' . $repositoryName;
  112. }
  113. return $repositoryName;
  114. }
  115. /**
  116. * @param string $fullClassName
  117. * @param string $outputDirectory
  118. *
  119. * @return void
  120. */
  121. public function writeEntityRepositoryClass($fullClassName, $outputDirectory)
  122. {
  123. $code = $this->generateEntityRepositoryClass($fullClassName);
  124. $path = $outputDirectory . DIRECTORY_SEPARATOR
  125. . str_replace('\\', \DIRECTORY_SEPARATOR, $fullClassName) . '.php';
  126. $dir = dirname($path);
  127. if ( ! is_dir($dir)) {
  128. mkdir($dir, 0775, true);
  129. }
  130. if ( ! file_exists($path)) {
  131. file_put_contents($path, $code);
  132. chmod($path, 0664);
  133. }
  134. }
  135. /**
  136. * @param string $repositoryName
  137. *
  138. * @return \Doctrine\ORM\Tools\EntityRepositoryGenerator
  139. */
  140. public function setDefaultRepositoryName($repositoryName)
  141. {
  142. $this->repositoryName = $repositoryName;
  143. return $this;
  144. }
  145. }