Compiler.php 4.89 KB
Newer Older
lsmith's avatar
lsmith committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
/**
 * Doctrine_Compiler
 * This class can be used for compiling the entire Doctrine framework into a single file
 *
 * @package     Doctrine
26
 * @subpackage  Compiler
lsmith's avatar
lsmith committed
27 28 29 30 31 32
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
lsmith's avatar
lsmith committed
33 34
class Doctrine_Compiler
{
lsmith's avatar
lsmith committed
35 36 37 38 39 40 41 42
    /**
     * method for making a single file of most used doctrine runtime components
     * including the compiled file instead of multiple files (in worst
     * cases dozens of files) can improve performance by an order of magnitude
     *
     * @throws Doctrine_Compiler_Exception      if something went wrong during the compile operation
     * @return void
     */
43
    public static function compile($target = null, $includedDrivers = array())
lsmith's avatar
lsmith committed
44
    {
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        if (!is_array($includedDrivers)) {
            $includedDrivers = array($includedDrivers);
        }
        
        $excludedDrivers = array();
        
        // If we have an array of specified drivers then lets determine which drivers we should exclude
        if (!empty($includedDrivers)) {
            $drivers = array('db2',
                             'firebird',
                             'informix',
                             'mssql',
                             'mysql',
                             'oracle',
                             'pgsql',
                             'sqlite');
            
            $excludedDrivers = array_diff($drivers, $includedDrivers);
        }
        
lsmith's avatar
lsmith committed
65
        $path = Doctrine::getPath();
zYne's avatar
zYne committed
66 67 68 69 70 71
        $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::LEAVES_ONLY);

        foreach ($it as $file) {
            $e = explode('.', $file->getFileName());
            
            // we don't want to require versioning files
zYne's avatar
zYne committed
72
            if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
zYne's avatar
zYne committed
73 74 75
                require_once $file->getPathName();
            }
        }
zYne's avatar
zYne committed
76

zYne's avatar
zYne committed
77
        $classes = array_merge(get_declared_classes(), get_declared_interfaces());
lsmith's avatar
lsmith committed
78 79 80 81

        $ret     = array();

        foreach ($classes as $class) {
zYne's avatar
zYne committed
82
            $e = explode('_', $class);
lsmith's avatar
lsmith committed
83

zYne's avatar
zYne committed
84 85
            if ($e[0] !== 'Doctrine') {
                continue;
lsmith's avatar
lsmith committed
86
            }
87 88 89 90 91 92 93 94 95 96 97 98
            
            // Exclude drivers
            if (!empty($excludedDrivers)) {
                foreach ($excludedDrivers as $excludedDriver) {
                    $excludedDriver = ucfirst($excludedDriver);
                    
                    if (in_array($excludedDriver, $e)) {
                        continue(2);
                    }
                }
            }
            
zYne's avatar
zYne committed
99 100 101 102
            $refl  = new ReflectionClass($class);
            $file  = $refl->getFileName();
            
            $lines = file($file);
lsmith's avatar
lsmith committed
103

zYne's avatar
zYne committed
104 105
            $start = $refl->getStartLine() - 1;
            $end   = $refl->getEndLine();
lsmith's avatar
lsmith committed
106

zYne's avatar
zYne committed
107
            $ret = array_merge($ret, array_slice($lines, $start, ($end - $start)));
lsmith's avatar
lsmith committed
108 109 110
        }

        if ($target == null) {
zYne's avatar
zYne committed
111
            $target = $path . DIRECTORY_SEPARATOR . 'Doctrine.compiled.php';
lsmith's avatar
lsmith committed
112 113 114 115 116
        }

        // first write the 'compiled' data to a text file, so
        // that we can use php_strip_whitespace (which only works on files)
        $fp = @fopen($target, 'w');
lsmith's avatar
lsmith committed
117

lsmith's avatar
lsmith committed
118 119 120
        if ($fp === false) {
            throw new Doctrine_Compiler_Exception("Couldn't write compiled data. Failed to open $target");
        }
121
        
zYne's avatar
zYne committed
122
        fwrite($fp, "<?php ". implode('', $ret));
lsmith's avatar
lsmith committed
123 124 125 126 127 128 129
        fclose($fp);

        $stripped = php_strip_whitespace($target);
        $fp = @fopen($target, 'w');
        if ($fp === false) {
            throw new Doctrine_Compiler_Exception("Couldn't write compiled data. Failed to open $file");
        }
130
        
lsmith's avatar
lsmith committed
131 132 133
        fwrite($fp, $stripped);
        fclose($fp);
    }
134
}