entity_repository_generator.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Tools;
  22. use Doctrine\ORM\Mapping\ClassMetadataInfo,
  23. Doctrine\ORM\Mapping\AssociationMapping,
  24. Doctrine\Common\Util\Inflector;
  25. /**
  26. * Class to generate entity repository classes
  27. *
  28. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  29. * @link www.doctrine-project.org
  30. * @since 2.0
  31. * @version $Revision$
  32. * @author Benjamin Eberlei <kontakt@beberlei.de>
  33. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  34. * @author Jonathan Wage <jonwage@gmail.com>
  35. * @author Roman Borschel <roman@code-factory.org>
  36. */
  37. class EntityRepositoryGenerator
  38. {
  39. protected static $_template =
  40. '<?php
  41. namespace Entity\Repository;
  42. use \db;
  43. /**
  44. *
  45. * @license see /license.txt
  46. * @author autogenerated
  47. */
  48. class <className> extends <extends>
  49. {
  50. /**
  51. * @return \Entity\Repository\<className>
  52. */
  53. public static function instance(){
  54. static $result = false;
  55. if($result === false){
  56. $result = db::instance()->get_repository(\'\\Entity\\<entityName>\');
  57. }
  58. return $result;
  59. }
  60. /**
  61. *
  62. * @param EntityManager $em The EntityManager to use.
  63. * @param ClassMetadata $class The class descriptor.
  64. */
  65. public function __construct($em, $class){
  66. parent::__construct($em, $class);
  67. }
  68. }';
  69. public function generateEntityRepositoryClass($name)
  70. {
  71. $name = Inflector::tableize($name);
  72. $is_course_table = (strpos($name, 'c_') === 0);
  73. if ($is_course_table) {
  74. $name = substr($name, 2, strlen($name) - 2);
  75. }
  76. $name = Inflector::classify($name);
  77. $className = $name;
  78. //$namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\'));
  79. //$className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName));
  80. $is_course_table = $metadata->is_course_table;
  81. $variables = array(
  82. '<namespace>' => $namespace,
  83. '<className>' => $className,
  84. '<entityName>' => str_replace('Repository', '', $className),
  85. '<extends>' => $is_course_table ? '\CourseEntityRepository' : '\EntityRepository'
  86. );
  87. return str_replace(array_keys($variables), array_values($variables), self::$_template);
  88. }
  89. /**
  90. *
  91. * @param type $name
  92. * @param type $outputDirectory
  93. */
  94. public function writeEntityRepositoryClass($name, $outputDirectory)
  95. {
  96. $name = explode('\\', $name);
  97. $name = end($name);
  98. $name = Inflector::tableize($name);
  99. $is_course_table = (strpos($name, 'c_') === 0);
  100. if ($is_course_table) {
  101. $name = substr($name, 2, strlen($name) - 2);
  102. }
  103. $name = Inflector::classify($name) . 'Repository';
  104. $fullClassName = $name;
  105. $file_name = Inflector::tableize($name);
  106. $code = $this->generateEntityRepositoryClass($fullClassName);
  107. $path = $outputDirectory . DIRECTORY_SEPARATOR
  108. . str_replace('\\', \DIRECTORY_SEPARATOR, $file_name) . '.class.php';
  109. $dir = dirname($path);
  110. if (!is_dir($dir)) {
  111. mkdir($dir, 0777, true);
  112. }
  113. if (!file_exists($path)) {
  114. file_put_contents($path, $code);
  115. }
  116. }
  117. }