EntityRepositoryGenerator.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 Doctrine\ORM\Tools;
  22. /**
  23. * Class to generate entity repository classes
  24. *
  25. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  26. * @link www.doctrine-project.org
  27. * @since 2.0
  28. * @version $Revision$
  29. * @author Benjamin Eberlei <kontakt@beberlei.de>
  30. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  31. * @author Jonathan Wage <jonwage@gmail.com>
  32. * @author Roman Borschel <roman@code-factory.org>
  33. */
  34. class EntityRepositoryGenerator
  35. {
  36. protected static $_template =
  37. '<?php
  38. namespace <namespace>;
  39. use Doctrine\ORM\EntityRepository;
  40. /**
  41. * <className>
  42. *
  43. * This class was generated by the Doctrine ORM. Add your own custom
  44. * repository methods below.
  45. */
  46. class <className> extends EntityRepository
  47. {
  48. }';
  49. public function generateEntityRepositoryClass($fullClassName)
  50. {
  51. $namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\'));
  52. $className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName));
  53. $variables = array(
  54. '<namespace>' => $namespace,
  55. '<className>' => $className
  56. );
  57. return str_replace(array_keys($variables), array_values($variables), self::$_template);
  58. }
  59. public function writeEntityRepositoryClass($fullClassName, $outputDirectory)
  60. {
  61. $code = $this->generateEntityRepositoryClass($fullClassName);
  62. $path = $outputDirectory . DIRECTORY_SEPARATOR
  63. . str_replace('\\', \DIRECTORY_SEPARATOR, $fullClassName) . '.php';
  64. $dir = dirname($path);
  65. if ( ! is_dir($dir)) {
  66. mkdir($dir, 0777, true);
  67. }
  68. if ( ! file_exists($path)) {
  69. file_put_contents($path, $code);
  70. }
  71. }
  72. }