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