ZCompile::make( 'd:/www/project/lib/zendframework-1.7.1/Zend.compiled.phplib' ,
array(),
'd:/www/project/lib/zendframework-1.7.1/'
);
* This source code was highlighted with Source Code Highlighter .
if (FRAMEWORK_LOAD_COMPILED_ZEND==1) {
require 'Zend.compiled.phplib' ;
} else {
require 'Zend/Loader.php' ;
}
Zend_Loader::registerAutoload( 'Zend_Loader' );
* This source code was highlighted with Source Code Highlighter .
array(
'Zend/Auth/' , // ,
'Zend/Acl/' , // ,
'Zend/View/Helper/HeadTitle.php' , //
'Zend/View/Helper/Url.php' , //
)
* This source code was highlighted with Source Code Highlighter .
<?php
set_include_path(
'd:/www/ksystem/lib/zendframework-1.7.1/'
. PATH_SEPARATOR . get_include_path());
ZCompile::make( 'd:/www/project/lib/zendframework-1.7.1/Zend.compiled.phplib' ,
array( 'Zend/Auth/' , 'Zend/Acl/' ),
'd:/www/ksystem/lib/zendframework-1.7.1/'
);
?>
* This source code was highlighted with Source Code Highlighter .
<?php
/**
* ZendFramwork .
*
* @author Nod nodkz.at.mail.ru
*/
class ZCompile
{
static private $path;
/**
* , require/include,
*
* @param string $dest
* @param string $includes ZF
* @return array
*/
public static function make($dest, array $add_includes = array(), $path = '' )
{
self::$path=$path;
$includes=array_merge($add_includes, self::_scanFolderFiles($add_includes), self::_getZendIncludes());
//
foreach ($includes as $key=>&$ value ) {
if (substr($ value ,0,1)== '/' ) {
$includes[$key] = substr($ value ,1);
}
}
//
$ordered_include=Array();
foreach ($includes as $class_file) {
self::_getClassOrderIncludes($class_file, $ordered_include);
}
// '<?php' '? >', , 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" ;
$worked_classes = Array();
foreach ($ordered_include as &$fname) {
if (!in_array($fname, $worked_classes)) {
$worked_classes[] = $fname;
$fname = self::$path.$fname;
if (@file_exists($fname)&&is_file($fname)) {
$body.= "/*** FILE: " .$fname. " ***/ \r\n" ;
$body .= preg_replace($pattern, $replacement, file_get_contents($fname, true ));
}
}
}
$size = file_put_contents($dest, $body);
return array( 'includes' => $includes, 'compiledBody' => $body, 'compiledSize' => $size);
}
/**
* (.. /).
* php , ( ).
*
* @param array $add_includes
* @return array
*/
private static function _scanFolderFiles(&$add_includes) {
$add_includes_dirs=array();
foreach ($add_includes as $key=>$elem) {
if (substr($elem,-1)== '/' ) {
if (is_dir(self::$path.$elem)) {
if ($dh = opendir(self::$path.$elem)) {
while (($file = readdir($dh)) !== false ) {
if (strpos(strtolower($file), '.php' )!== false ) {
$add_includes_dirs[]=$elem.$file;
}
}
closedir($dh);
}
}
$add_includes[$key]= '' ;
}
}
return $add_includes_dirs;
}
/**
* , .
*
* @param string $fname
* @param array &$already_included
*/
private static function _getClassOrderIncludes($class_filename, array &$already_included=Array(), array &$stack=Array()) {
//
if (!in_array($class_filename, $stack)) {
array_push($stack, $class_filename);
} else {
if (!in_array($class_filename, $already_included)) {
$already_included[]=$class_filename;
}
return ;
}
//
if (is_file(self::$path.$class_filename) && !in_array($class_filename, $already_included)) {
$class_file_content = file_get_contents(self::$path.$class_filename, true );
//
// .. extends implements,
//
if (preg_match_all( '/class\s+[_\w]+\s+(extends|implements)\s+([_\w]+)/i' , $class_file_content, $arr)) {
foreach ($arr[2] as $new_class_name) {
$new_class_path = str_replace( '_' , '/' ,$new_class_name). '.php' ;
if (!in_array($new_class_path, $already_included)) {
self::_getClassOrderIncludes($new_class_path, $already_included, $stack);
}
}
}
//
if (preg_match_all( '%(require_once|include_once|require|include) [("\'](.*?)[)"\'];%sm' , $class_file_content, $arr)) {
//
foreach ($arr[2] as $new_class_path) {
// , ,
if (!in_array($new_class_path, $already_included) && !in_array($new_class_path, $stack)) {
//
if (strpos($new_class_path, '$' )=== false ) {
// ,
self::_getClassOrderIncludes($new_class_path, $already_included, $stack);
}
}
}
}
if (!in_array($class_filename, $already_included)) {
$already_included[]=$class_filename;
}
}
array_pop($stack);
}
/**
* ZF
*
* @return array
*/
private static function _getZendIncludes()
{
$required = array();
$included_files = get_included_files();
$included_files;
foreach ($included_files as $fname) {
$fname = str_replace( '/' , '\\',$fname);
if (!(strpos($fname, ' \\Zend\\ ') > 0) || (strstr($fname, __CLASS__ . ' .php '))) {
continue;
}
$required[] = str_replace(' \\ ', ' / ', substr($fname, strpos($fname, ' \\Zend\\'), strlen($fname)));
}
return array_unique($required);
}
}
?>
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/46742/
All Articles