📜 ⬆️ ⬇️

Cumulative build ZF in one file

In the subject of combining PHP-Zend Framework'e (and other, following the same naming conventions and accommodation) publish their decision that implements __autoload with the cumulative assembly of autoload classes.

It takes into account two known problems:
  1. In files containing the variable __FILE__, this variable is substituted.
  2. Handled the case of parallel execution of scripts.

The solution is based on the example of __autoload implementation from Dmitry Koterov’s PHP5 book and the idea described here .

Application


It all applies this way:
1. Somewhere at the beginning of the script, but after initializing the paths to the libraries (set_include_path (...)) we add:
require_once "My/NameScheme/Autoload.php" ;
if (@fopen( 'All.php' , 'r' , true )) {
include_once( 'All.php' );
}

2. Somewhere at the completion of the script (you can put it in the register_shutdown_function, but I did not try this) add:
My_NameScheme_Autoload::compileTo(APP_ROOT . '/includes/All.php' ); //


Of course, in order for all this to work, you first need to cut all the require | include in Zend Framework
I use this technique for this:
$file = preg_replace( '/((?:require|include)_once\s*\(?[\'"]Zend\/(.*)[\'"]\)?\s*;)/smiU' , '//*** $1' , $file);

')
To reset the cache, you just need to delete All.php, a new one will be created and it will grow until users bypass all the “nooks” of the site. However, here I sometimes encounter one problem: due to frequent changes (in the process of growth) All.php FastCGI processes begin to load the system heavily, so after deletion, sometimes FastCGI processes must be overloaded manually.

Algorithm


If any new files were automatically loaded, the algorithm for the compileTo method works is as follows:
  1. Block All.php
  2. We check if there are already any classes in All.php that were also auto-loaded (for example, while this script was being processed, another script successfully worked in parallel and added something to All.php). If this happens, then to avoid problems with dependencies, the work of the method will be interrupted.
  3. We add new classes in All.php.
  4. Unlock All.php.

Download


autoload.zip (6 Kb)

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


All Articles