Compiler.php 5.1 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
<?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
19
 * <http://www.phpdoctrine.org>.
lsmith's avatar
lsmith committed
20
 */
21

lsmith's avatar
lsmith committed
22 23 24 25 26
/**
 * Doctrine_Compiler
 * This class can be used for compiling the entire Doctrine framework into a single file
 *
 * @package     Doctrine
27
 * @subpackage  Compiler
lsmith's avatar
lsmith committed
28
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
29 30
 * @license     http://www.opensource.org/licenses/lgpllicense.php LGPL
 * @link        www.phpdoctrine.
lsmith's avatar
lsmith committed
31 32 33
 * @since       1.0
 * @version     $Revision$
 */
lsmith's avatar
lsmith committed
34 35
class Doctrine_Compiler
{
lsmith's avatar
lsmith committed
36 37 38 39 40 41
    /**
     * 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
42
     * @return $target Path the compiled file was written to
lsmith's avatar
lsmith committed
43
     */
44
    public static function compile($target = null, $includedDrivers = array())
lsmith's avatar
lsmith committed
45
    {
46
        if ( ! is_array($includedDrivers)) {
47 48 49 50 51 52
            $includedDrivers = array($includedDrivers);
        }
        
        $excludedDrivers = array();
        
        // If we have an array of specified drivers then lets determine which drivers we should exclude
53
        if ( ! empty($includedDrivers)) {
54 55 56 57 58 59 60 61 62 63 64 65
            $drivers = array('db2',
                             'firebird',
                             'informix',
                             'mssql',
                             'mysql',
                             'oracle',
                             'pgsql',
                             'sqlite');
            
            $excludedDrivers = array_diff($drivers, $includedDrivers);
        }
        
lsmith's avatar
lsmith committed
66
        $path = Doctrine::getPath();
zYne's avatar
zYne committed
67 68 69 70 71
        $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::LEAVES_ONLY);

        foreach ($it as $file) {
            $e = explode('.', $file->getFileName());
            
72 73
            //@todo what is a versioning file? do we have these anymore? None 
            //exists in my version of doctrine from svn.
zYne's avatar
zYne committed
74
            // we don't want to require versioning files
zYne's avatar
zYne committed
75
            if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
zYne's avatar
zYne committed
76 77 78
                require_once $file->getPathName();
            }
        }
zYne's avatar
zYne committed
79

zYne's avatar
zYne committed
80
        $classes = array_merge(get_declared_classes(), get_declared_interfaces());
lsmith's avatar
lsmith committed
81 82 83 84

        $ret     = array();

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

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

zYne's avatar
zYne committed
107 108
            $start = $refl->getStartLine() - 1;
            $end   = $refl->getEndLine();
lsmith's avatar
lsmith committed
109

zYne's avatar
zYne committed
110
            $ret = array_merge($ret, array_slice($lines, $start, ($end - $start)));
lsmith's avatar
lsmith committed
111 112 113
        }

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

        // 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
120

lsmith's avatar
lsmith committed
121 122 123
        if ($fp === false) {
            throw new Doctrine_Compiler_Exception("Couldn't write compiled data. Failed to open $target");
        }
124
        
zYne's avatar
zYne committed
125
        fwrite($fp, "<?php ". implode('', $ret));
lsmith's avatar
lsmith committed
126 127 128 129 130 131 132
        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");
        }
133
        
lsmith's avatar
lsmith committed
134 135
        fwrite($fp, $stripped);
        fclose($fp);
136 137

        return $target;
lsmith's avatar
lsmith committed
138
    }
139
}