private static function compile($templateName) { $headers = ''; $fp = @fopen(self::getPath($templateName), 'rb'); if (!$fp) { return; } $sourceCode = ''; while (!feof($fp)) { $sourceCode .= fread($fp, 8192); } $lock = new PHPLock($sourceCode); $lock->acquire(); // i18n replace $sourceCode = preg_replace_callback('/<(i18n|lang)(.*?)>(.*?)<\\/\\1>/', array(__CLASS__, 'parseI18nTags'), $sourceCode); $sourceCode = preg_replace_callback('/<(i18n|lang)(.*?) ?\\/?>/', array(__CLASS__, 'parseI18nTags'), $sourceCode); $sourceCode = preg_replace_callback('/#i18n\\((.+?)\\)/', function ($match) { return I18N::parse($match[1]); }, $sourceCode); $lock->acquire(); // variable with braces: $sourceCode = preg_replace('/\\{\\$([A-Za-z0-9_\\\\[\\]\'"]+)(->|::)(\\$?[A-Za-z0-9_\\-]+)(\\(.*\\))?}/', '<?php echo \\$\\1\\2\\3\\4; ?>', $sourceCode); $sourceCode = preg_replace('/\\{\\$([A-Za-z0-9_\\[\\]\'"]+)}/', '<?php echo \\$\\1; ?>', $sourceCode); $sourceCode = preg_replace('/\\{([A-Z][A-Z0-9_\\[\\]]*)\\}/', '<?php echo \\1; ?>', $sourceCode); $lock->acquire(); // PHP code: $sourceCode = preg_replace('/<php>(.+?)<\\/php>/is', '<?php \\1; ?>', $sourceCode); $lock->acquire(); // import: $sourceCode = preg_replace('/\\<import template="([A-z0-9_\\-\\/]+)"[\\/ ]*\\>(<\\/import>)?/i', '<?php include \\Core\\Template::load(\'\\1\'); ?>', $sourceCode); $lock->acquire(); // loop: $sourceCode = preg_replace_callback('/\\<loop(.*?)\\>/is', array(__CLASS__, 'parseLoop'), $sourceCode); $sourceCode = preg_replace('/\\<\\/loop\\>/i', '<?php } ?>', $sourceCode); $lock->acquire(); // if: $sourceCode = preg_replace('/\\<if (?:condition=)?"(.+?)"[\\/ ]*\\>/i', '<?php if(\\1) { ?>', $sourceCode); $sourceCode = preg_replace('/\\<elseif (?:condition=)?"(.+?)"[\\/ ]*\\>/i', '<?php } elseif(\\1) { ?>', $sourceCode); $sourceCode = preg_replace('/\\<else[\\/ ]*\\>/i', '<?php } else { ?>', $sourceCode); $sourceCode = preg_replace('/\\<\\/if\\>/i', '<?php } ?>', $sourceCode); $lock->acquire(); // header: preg_match_all('/\\<meta header="(.+?)" content="(.+?)"[ \\/]*\\>/i', $sourceCode, $matches); foreach ($matches[0] as $offset => $string) { $headers .= "header('{$matches[1][$offset]}: {$matches[2][$offset]}');" . PHP_EOL; $sourceCode = str_replace($string, '', $sourceCode); } $lock->acquire(); // variable without braces $sourceCode = preg_replace('/\\$([a-z][A-Za-z0-9_]+)/', '<?php echo \\$\\1; ?>', $sourceCode); // unlock PHP code $lock->release(); // rewrite link if (!defined('USE_REWRITE') || !USE_REWRITE) { $sourceCode = preg_replace_callback('/(href|action)="([A-Z0-9_\\.\\-\\/%\\?=&]*?)"/is', array(__CLASS__, 'parseUrlRewrite'), $sourceCode); } // clear space and tab $sourceCode = preg_replace('/^[ \\t]*(.+)[ \\t]*$/m', '\\1', $sourceCode); $output = '<?php' . PHP_EOL; $output .= 'if(!defined(\'ROOT_PATH\'))'; $output .= ' exit(\'This file could not be access directly.\');' . PHP_EOL; if ($headers) { $output .= $headers; } $output .= '?>' . PHP_EOL; $output .= trim($sourceCode); $output = preg_replace('/\\s*\\?\\>\\s*\\<\\?php\\s*/is', PHP_EOL, $output); self::createDir(dirname(DATA_PATH . "Template/{$templateName}.php")); if (!file_exists(DATA_PATH . "Template/{$templateName}.php")) { @touch(DATA_PATH . "Template/{$templateName}.php"); } if (!is_writable(DATA_PATH . "Template/{$templateName}.php")) { throw new Error('Cannot write template file: ' . DATA_PATH . "Template/{$templateName}.php", 8); } file_put_contents(DATA_PATH . "Template/{$templateName}.php", $output); }
private static function compile($templateName) { $headers = ''; $fp = @fopen(self::getPath($templateName), 'rb'); if (!$fp) { return; } $sourceCode = ''; while (!feof($fp)) { $sourceCode .= fread($fp, 8192); } $lock = new PHPLock($sourceCode); $lock->acquire(); // variable with braces: /* $sourceCode = preg_replace('/\{\$([A-Za-z0-9_\[\]\->]+)\}/', '<?php echo \$\\1; ?>', $sourceCode); */ $sourceCode = preg_replace('/\\{\\$([A-Za-z0-9_\\[\\]\\->\\(\\)]+)\\}/', '<?php echo \\$\\1; ?>', $sourceCode); $sourceCode = preg_replace('/\\{([A-Z][A-Z0-9_\\[\\]]*)\\}/', '<?php echo \\1; ?>', $sourceCode); $sourceCode = preg_replace('/\\{(\\(.*\\)*)\\}/', '<?php echo \\1; ?>', $sourceCode); $lock->acquire(); // PHP code: $sourceCode = preg_replace('/<php>(.+?)<\\/php>/is', '<?php \\1; ?>', $sourceCode); $lock->acquire(); // import: $sourceCode = preg_replace('/\\<import template="([A-z0-9_\\-\\/]+)"[\\/ ]*\\>/i', '<?php include \\Core\\Template::load(\'\\1\'); ?>', $sourceCode); $lock->acquire(); // loop: $sourceCode = preg_replace_callback('/\\<loop(.*?)\\>/is', array('\\Core\\Template', 'parseLoop'), $sourceCode); $sourceCode = preg_replace('/\\<\\/loop\\>/i', '<?php } ?>', $sourceCode); $lock->acquire(); // if: $sourceCode = preg_replace('/\\<if (?:condition=)?"(.+?)"[\\/ ]*\\>/i', '<?php if(\\1) { ?>', $sourceCode); $sourceCode = preg_replace('/\\<elseif (?:condition=)?"(.+?)"[\\/ ]*\\>/i', '<?php } elseif(\\1) { ?>', $sourceCode); $sourceCode = preg_replace('/\\<else[\\/ ]*\\>/i', '<?php } else { ?>', $sourceCode); $sourceCode = preg_replace('/\\<\\/if\\>/i', '<?php } ?>', $sourceCode); $lock->acquire(); // header: preg_match_all('/\\<meta header="(.+?)" content="(.+?)"[ \\/]*\\>/i', $sourceCode, $matches); foreach ($matches[0] as $offset => $string) { $headers .= "header('{$matches[1][$offset]}: {$matches[2][$offset]}');" . PHP_EOL; $sourceCode = str_replace($string, '', $sourceCode); } $lock->acquire(); // variable without braces $sourceCode = preg_replace('/\\$([a-z][A-Za-z0-9_]+)/', '<?php echo \\$\\1; ?>', $sourceCode); // unlock PHP code $lock->release(); // rewrite link if (!defined('USE_REWRITE') || !USE_REWRITE) { $sourceCode = preg_replace_callback('/href="([A-Z0-9_\\.\\-\\/%\\?=&]*?)"/is', array('\\Core\\Template', 'parseUrlRewrite'), $sourceCode); } // clear space and tab $sourceCode = preg_replace('/^[ \\t]*(.+)[ \\t]*$/m', '\\1', $sourceCode); // Compress CSS $sourceCode = preg_replace_callback('/<link rel="stylesheet" (.+?)>[\\r\\n]*/is', array('\\Core\\Template', 'compressCss'), $sourceCode); // Compress JavaScript $sourceCode = preg_replace_callback('/<script(.+?)>[\\r\\n\\t ]*<\\/script>/is', array('\\Core\\Template', 'compressJs'), $sourceCode); $output = '<?php' . PHP_EOL; $output .= 'if(!defined(\'ROOT_PATH\'))'; $output .= ' exit(\'This file could not be access directly.\');' . PHP_EOL; if ($headers) { $output .= $headers; } $output .= '?>' . PHP_EOL; $output .= $sourceCode; $output = preg_replace('/\\s*\\?\\>\\s*\\<\\?php\\s*/is', PHP_EOL, $output); self::createDir(dirname(DATA_PATH . "Template/{$templateName}.php")); if (!file_exists(DATA_PATH . "Template/{$templateName}.php")) { @touch(DATA_PATH . "Template/{$templateName}.php"); } if (!is_writable(DATA_PATH . "Template/{$templateName}.php")) { throw new Error('Cannot write template file: ' . DATA_PATH . "Template/{$templateName}.php", 8); } file_put_contents(DATA_PATH . "Template/{$templateName}.php", $output); }