⬆️ ⬇️

PHP class for building in one file

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



Problem



In short, it was about the fact that one of the flaws of the framework is its concept of “one class - one file”. And although, from my point of view, this is not a flaw in the ZF architecture, it is a flaw in PHP. Problems begin when PHP starts to include dozens and hundreds of files, and even check them once.



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

')

Class



<? php

class ZendMake

{

/ **

* Build files with deletion of comments, require / include, etc.

*

* param string $ dest Absolute name of compiled file

* param string $ includes Array of ZF files to build

* return array

* /

public static function make ($ dest, $ includes = null)

{

$ includes = is_array ($ includes)? $ includes: self :: _ getZendIncludes ();



// Remove tags' <? Php ''? > ', comments, blank lines 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 = "<? php \ n";

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);

}



/ **

* Select unique ZF files that are stuck in the project

*

* return array

* /

private static function _getZendIncludes ()

{

$ 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)));



// All 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 (Controller, Session, Auth, View, Uri, Db, Config, Version, Debug, Registry modules). The collected file took 231 kb.



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

Source: https://habr.com/ru/post/12395/



All Articles