Compiler.php 15.7 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 26 27 28 29 30 31 32
<?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
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @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
    /**
     * @var array $classes          an array containing all runtime classes of Doctrine framework
     */
    private static $classes = array(
zYne's avatar
zYne committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
                                   'Access',
                                   'Adapter_Interface',
                                   'Adapter_Statement',
                                   'Adapter',
                                   'Cache_Query_Sqlite',
                                   'Collection_Batch',
                                   'Collection_Exception',
                                   'Collection_Immediate',
                                   'Collection_Iterator_Expandable',
                                   'Collection_Iterator_Normal',
                                   'Collection_Iterator_Offset',
                                   'Collection_Iterator',
                                   'Collection_Lazy',
                                   'Collection_Offset',
                                   'Collection',
                                   'Compiler_Exception',
                                   'Compiler',
                                   'Configurable',
                                   'Connection_Common',
                                   'Connection_Db2',
                                   'Connection_Exception',
                                   'Connection_Firebird_Exception',
                                   'Connection_Firebird',
                                   'Connection_Informix_Exception',
                                   'Connection_Informix',
                                   'Connection_Mock',
                                   'Connection_Module',
                                   'Connection_Mssql_Exception',
                                   'Connection_Mssql',
                                   'Connection_Mysql_Exception',
                                   'Connection_Mysql',
                                   'Connection_Oracle_Exception',
                                   'Connection_Oracle',
                                   'Connection_Pgsql_Exception',
                                   'Connection_Pgsql',
                                   'Connection_Sqlite_Exception',
                                   'Connection_Sqlite',
                                   'Connection_UnitOfWork',
                                   'Connection',
                                   'DataDict_Exception',
                                   'DataDict_Firebird_Exception',
                                   'DataDict_Firebird',
                                   'DataDict_Informix_Exception',
                                   'DataDict_Informix',
                                   'DataDict_Mssql_Exception',
                                   'DataDict_Mssql',
                                   'DataDict_Mysql_Exception',
                                   'DataDict_Mysql',
                                   'DataDict_Oracle_Exception',
                                   'DataDict_Oracle',
                                   'DataDict_Pgsql_Exception',
                                   'DataDict_Pgsql',
                                   'DataDict_Sqlite_Exception',
                                   'DataDict_Sqlite',
                                   'DataDict',
                                   'Db_Event',
                                   'Db_EventListener_Chain',
                                   'Db_EventListener_Interface',
                                   'Db_EventListener',
                                   'Db_Exception',
                                   'Db_Mock',
                                   'Db_Profiler_Exception',
                                   'Db_Profiler_Query',
                                   'Db_Profiler',
                                   'Db_Statement',
                                   'Db',
                                   'EventListener_AccessorInvoker',
                                   'EventListener_Chain',
                                   'EventListener_Debugger',
                                   'EventListener_Empty',
                                   'EventListener_Interface',
                                   'EventListener',
                                   'Exception',
                                   'Export_Exception',
                                   'Export_Firebird_Exception',
                                   'Export_Firebird',
                                   'Export_Informix_Exception',
                                   'Export_Mssql_Exception',
                                   'Export_Mssql',
                                   'Export_Mysql_Exception',
                                   'Export_Mysql',
                                   'Export_Oracle_Exception',
                                   'Export_Oracle',
                                   'Export_Pgsql_Exception',
                                   'Export_Pgsql',
                                   'Export_Reporter',
                                   'Export_Sqlite_Exception',
                                   'Export_Sqlite',
                                   'Export',
                                   'Expression_Exception',
                                   'Expression_Firebird',
                                   'Expression_Informix',
                                   'Expression_Mssql',
                                   'Expression_Mysql',
                                   'Expression_Oracle',
                                   'Expression_Pgsql',
                                   'Expression_Sqlite',
                                   'Expression',
                                   'Hook_Equal',
                                   'Hook_Integer',
                                   'Hook_Parser_Complex',
                                   'Hook_Parser',
                                   'Hook_WordLike',
                                   'Hook',
                                   'Hydrate_Alias',
                                   'Hydrate',
                                   'Identifier',
                                   'Import_Builder_BaseClass',
                                   'Import_Builder_Exception',
                                   'Import_Builder',
                                   'Import_Exception',
                                   'Import_Firebird_Exception',
                                   'Import_Firebird',
                                   'Import_Informix_Exception',
                                   'Import_Informix',
                                   'Import_Mssql_Exception',
                                   'Import_Mssql',
                                   'Import_Mysql_Exception',
                                   'Import_Mysql',
                                   'Import_Oracle_Exception',
                                   'Import_Oracle',
                                   'Import_Pgsql_Exception',
                                   'Import_Pgsql',
                                   'Import_Reader_Db',
                                   'Import_Reader_Exception',
                                   'Import_Reader',
                                   'Import_Sqlite_Exception',
                                   'Import_Sqlite',
                                   'Import',
                                   'Lib',
                                   'Locking_Exception',
                                   'Locking_Manager_Pessimistic',
                                   'Manager_Exception',
                                   'Manager',
                                   'Null',
                                   'Overloadable',
                                   'Query_Condition',
                                   'Query_Exception',
                                   'Query_From',
                                   'Query_Groupby',
                                   'Query_Having',
                                   'Query_JoinCondition',
                                   'Query_Orderby',
                                   'Query_Part',
                                   'Query_Set',
                                   'Query_Where',
                                   'Query',
                                   'RawSql_Exception',
                                   'RawSql',
                                   'Record_Exception',
                                   'Record_Iterator',
                                   'Record_State_Exception',
                                   'Record',
                                   'Relation_Association_Self',
                                   'Relation_Association',
                                   'Relation_ForeignKey',
                                   'Relation_LocalKey',
                                   'Relation',
                                   'Reporter',
                                   'Schema_Column',
                                   'Schema_Database',
                                   'Schema_Exception',
                                   'Schema_Object',
                                   'Schema_Relation',
                                   'Schema_Table',
                                   'Schema',
                                   'Sequence_Db2_Exception',
                                   'Sequence_Db2',
                                   'Sequence_Exception',
                                   'Sequence_Firebird',
                                   'Sequence_Informix',
                                   'Sequence_Mssql',
                                   'Sequence_Mysql',
                                   'Sequence_Oracle',
                                   'Sequence_Pgsql',
                                   'Sequence_Sqlite',
                                   'Sequence',
                                   'Table_Exception',
                                   'Table_Repository_Exception',
                                   'Table_Repository',
                                   'Table',
                                   'Transaction_Exception',
                                   'Transaction_Firebird',
                                   'Transaction_Informix',
                                   'Transaction_Mssql',
                                   'Transaction_Mysql',
                                   'Transaction_Oracle',
                                   'Transaction_Pgsql',
                                   'Transaction_Sqlite',
                                   'Transaction',
                                   'Validator_Country',
                                   'Validator_Creditcard',
                                   'Validator_Date',
                                   'Validator_Email',
                                   'Validator_Enum',
                                   'Validator_ErrorStack',
                                   'Validator_Exception',
                                   'Validator_Htmlcolor',
                                   'Validator_Ip',
                                   'Validator_Nospace',
                                   'Validator_Notblank',
                                   'Validator_Notnull',
                                   'Validator_Range',
                                   'Validator_Regexp',
                                   'Validator_Unique',
                                   'Validator_Usstate',
                                   'Validator',
                                   'ValueHolder',
                                   'View_Exception',
                                   'View',
zYne's avatar
zYne committed
249
                                              );
lsmith's avatar
lsmith committed
250 251 252 253 254 255 256

    /**
     * getRuntimeClasses
     * returns an array containing all runtime classes of Doctrine framework
     *
     * @return array
     */
lsmith's avatar
lsmith committed
257 258
    public static function getRuntimeClasses()
    {
lsmith's avatar
lsmith committed
259 260 261 262 263 264 265 266 267 268
        return self::$classes;
    }
    /**
     * 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
     */
lsmith's avatar
lsmith committed
269 270
    public static function compile($target = null)
    {
lsmith's avatar
lsmith committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
        $path = Doctrine::getPath();

        $classes = self::$classes;

        $ret     = array();

        foreach ($classes as $class) {
            if ($class !== 'Doctrine')
                $class = 'Doctrine_'.$class;

            $file  = $path.DIRECTORY_SEPARATOR.str_replace("_",DIRECTORY_SEPARATOR,$class).".php";

            echo "Adding $file" . PHP_EOL;

            if ( ! file_exists($file)) {
                throw new Doctrine_Compiler_Exception("Couldn't compile $file. File $file does not exists.");
            }
            Doctrine::autoload($class);
            $refl  = new ReflectionClass ( $class );
            $lines = file( $file );

            $start = $refl -> getStartLine() - 1;
            $end   = $refl -> getEndLine();

            $ret = array_merge($ret,
                               array_slice($lines,
                               $start,
                              ($end - $start)));

        }

        if ($target == null) {
            $target = $path.DIRECTORY_SEPARATOR.'Doctrine.compiled.php';
        }

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

lsmith's avatar
lsmith committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
        if ($fp === false) {
            throw new Doctrine_Compiler_Exception("Couldn't write compiled data. Failed to open $target");
        }
        fwrite($fp, "<?php".
                    " class InvalidKeyException extends Exception { }".
                    implode('', $ret)
              );
        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");
        }
        fwrite($fp, $stripped);
        fclose($fp);
    }
}