PHP class for assembling inclusions in one file

    based on mocksoul comment from PHP topic : Introduction to Zend Framework

    Problem


    In short, it was said that one of the drawbacks of the framework is its concept of "one class - one file." And although, from my point of view, this is not a drawback of the ZF architecture, it is a drawback of PHP. Problems begin when PHP begins to include dozens and hundreds of files, and even check them for once.

    The proposed solution is the assembly of all inclusions into one file. The class presented below does not claim to be the final universal solution to the problem. It just demonstrates one of the incllud assembly methods. In this case, it only collects the framework files, ignoring all the others.

    Class


    class ZendMake
    {
    / **
    * Assembly of excluded files with removal of comments, require / include, etc.
    *
    * param string $ dest Absolute name of the compiled file
    * param string $ includes Array of ZF files for assembly
    * return array
    * /
    public static function make ( $ dest, $ includes = null)
    {
    $ includes = is_array ($ includes)? $ includes: self :: _ getZendIncludes ();

    // Remove tags ' ', comments, empty string originals and require / include [_once]
    $ pattern [] = '% (^ \ <\? Php | \? \> $)% M';
    $ replacement [] = '';
    $ pattern [] = '% / \ *. *? \ * /% sm';
    $ replacement [] = '';
    $ pattern [] = '% //. * $% m';
    $ replacement [] = '';
    $ pattern [] = '% (require_once | include_once | require | include) [("\'] (. *?) [)" \ '];% sm';
    $ replacement [] = '';
    $ pattern [] = '% (\ n) {2,}%';
    $ replacement [] = "\ n";

    $ body = " foreach ($ includes as $ fname) {

    $ body. = preg_replace ($ pattern, $ replacement, file_get_contents ($ fname, true));
    }

    $ size = file_put_contents ($ dest, $ body);

    return array ( 'includes' => $ includes, 'compiledBody' => $ body, 'compiledSize' => $ size);








    {
    $ required = array ();
    $ included_files = get_included_files ();
    foreach ($ included_files as $ fname) {
    if (! (strpos ($ fname, 'Zend')> 0) || (strstr ($ fname, __CLASS__. '.php'))) {
    continue;
    }

    $ required [] = str_replace ('\\', '/', substr ($ fname, strpos ($ fname, 'Zend'), strlen ($ fname)));

    // Everything requires
    $ pattern = '% (require_once | include_once | require | include) [("\'] (. *?) [)" \ '];% Sm';
    preg_match_all ($ pattern, file_get_contents ($ fname), $ pocket);

    $ required = array_merge ($ pocket [2], $ required);
    }

    return array_unique ($ required);
    }
    }


    Using


    Insert lines at the end of the script
    require 'ZendMake.php';
    $ result = ZendMake :: make ($ libDir. 'Zend.Make.php');
    Zend_Debug :: dump ($ result ['includes']);
    Zend_Debug :: dump ($ result ['compiledSize']);


    results


    Using this class, I collected more than 60 ZF files from my project (modules Controller, Session, Auth, View, Uri, Db, Config, Version, Debug, Registry). The collected file took 231 kb.

    The script running time using the collected file is faster than the script using a large number of inclusions more than 8 times.

    Also popular now: