Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Compiler.php 7677 2010-07-28 03:37:42Z kriswallsmith $
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
/**
23
 * Doctrine_Compiler
24
 * This class can be used for compiling the entire Doctrine framework into a single file
25
 *
26
 * @package     Doctrine
27
 * @subpackage  Compiler
28
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
29
 * @license     http://www.opensource.org/licenses/lgpllicense.php LGPL
30
 * @link        www.phpdoctrine.
31
 * @since       1.0
32
 * @version     $Revision: 7677 $
33
 */
34
class Doctrine_Compiler
35
{
36
    /**
37
     * method for making a single file of most used doctrine runtime components
38
     * including the compiled file instead of multiple files (in worst
39
     * cases dozens of files) can improve performance by an order of magnitude
40
     *
41
     * @throws Doctrine_Compiler_Exception      if something went wrong during the compile operation
42
     * @return $target Path the compiled file was written to
43
     */
44
    public static function compile($target = null, $includedDrivers = array())
45
    {
46
        if ( ! is_array($includedDrivers)) {
47
            $includedDrivers = array($includedDrivers);
48
        }
49
 
50
        $excludedDrivers = array();
51
 
52
        // If we have an array of specified drivers then lets determine which drivers we should exclude
53
        if ( ! empty($includedDrivers)) {
54
            $drivers = array('db2',
55
                             'mssql',
56
                             'mysql',
57
                             'oracle',
58
                             'pgsql',
59
                             'sqlite');
60
 
61
            $excludedDrivers = array_diff($drivers, $includedDrivers);
62
        }
63
 
64
        $path = Doctrine_Core::getPath();
65
        $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path . '/Doctrine'), RecursiveIteratorIterator::LEAVES_ONLY);
66
 
67
        foreach ($it as $file) {
68
            $e = explode('.', $file->getFileName());
69
 
70
            //@todo what is a versioning file? do we have these anymore? None
71
            //exists in my version of doctrine from svn.
72
            // we don't want to require versioning files
73
            if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false
74
                && strpos($file->getFileName(), 'sfYaml') === false) {
75
                require_once $file->getPathName();
76
            }
77
        }
78
 
79
        $classes = array_merge(get_declared_classes(), get_declared_interfaces());
80
 
81
        $ret     = array();
82
 
83
        foreach ($classes as $class) {
84
            $e = explode('_', $class);
85
 
86
            if ($e[0] !== 'Doctrine') {
87
                continue;
88
            }
89
 
90
            // Exclude drivers
91
            if ( ! empty($excludedDrivers)) {
92
                foreach ($excludedDrivers as $excludedDriver) {
93
                    $excludedDriver = ucfirst($excludedDriver);
94
 
95
                    if (in_array($excludedDriver, $e)) {
96
                        continue(2);
97
                    }
98
                }
99
            }
100
 
101
            $refl  = new ReflectionClass($class);
102
            $file  = $refl->getFileName();
103
 
104
            $lines = file($file);
105
 
106
            $start = $refl->getStartLine() - 1;
107
            $end   = $refl->getEndLine();
108
 
109
            $ret = array_merge($ret, array_slice($lines, $start, ($end - $start)));
110
        }
111
 
112
        if ($target == null) {
113
            $target = $path . DIRECTORY_SEPARATOR . 'Doctrine.compiled.php';
114
        }
115
 
116
        // first write the 'compiled' data to a text file, so
117
        // that we can use php_strip_whitespace (which only works on files)
118
        $fp = @fopen($target, 'w');
119
 
120
        if ($fp === false) {
121
            throw new Doctrine_Compiler_Exception("Couldn't write compiled data. Failed to open $target");
122
        }
123
 
124
        fwrite($fp, "<?php ". implode('', $ret));
125
        fclose($fp);
126
 
127
        $stripped = php_strip_whitespace($target);
128
        $fp = @fopen($target, 'w');
129
        if ($fp === false) {
130
            throw new Doctrine_Compiler_Exception("Couldn't write compiled data. Failed to open $file");
131
        }
132
 
133
        fwrite($fp, $stripped);
134
        fclose($fp);
135
 
136
        return $target;
137
    }
138
}