/** * Create static HTML site version */ public function compress() { // If no output path specified if (!isset($this->output[0])) { $this->output = __SAMSON_PUBLIC_PATH . '/out/'; } // Create output directory and clear old HTML data if (\samson\core\File::mkdir($this->output)) { \samson\core\File::clear($this->output); } // Save original output path $o_output = $this->output; elapsed('Creating static HTML web-application from: ' . $this->input . ' to ' . $this->output); // Collection of existing generated views $views = '<h1>#<i>' . $_SERVER['HTTP_HOST'] . '</i> .html pages:</h1>'; $views .= 'Generated on ' . date('d M Y h:i'); // Path to new resources $cssPath = ''; $jsPath = ''; // Copy generated css & js resources to root folder if (class_exists('\\samson\\resourcer\\ResourceRouter')) { $rr = m('resourcer'); // Get resourcer CSS generated files $cssPath = $this->input . $rr->cached['css']; if (isset($cssPath)) { elapsed('Creating CSS resource file from:' . $cssPath); // Read CSS file $css = file_get_contents($cssPath); // Perform URL rewriting $css = preg_replace_callback('/url\\s*\\(\\s*(\'|\\")?([^\\)\\s\'\\"]+)(\'|\\")?\\s*\\)/i', array($this, 'srcReplaceCallback'), $css); //$css = preg_replace('url((.*?)=si', '\\www', $css); $css = str_replace('url("fonts/', 'url("www/fonts/', $css); $css = str_replace('url("img/', 'url("www/img/', $css); // Write new CSS file file_put_contents($this->output . 'style.css', $css); } // Get resourcer JS generated files $jsPath = $rr->cached['js']; if (isset($jsPath)) { elapsed('Creating JavaScript resource file from:' . $jsPath); $this->copy_resource($this->input . $jsPath, $this->output . 'index.js'); } } // Iterate all site supported locales foreach (\samson\core\SamsonLocale::$locales as $locale) { // Generate localized path to cached html pages $pages_path = $this->cache_path . locale_path($locale); // Set views locale description $views .= '<h2>Locale <i>' . ($locale == \samson\core\SamsonLocale::DEF ? 'default' : $locale) . '</i>:<h2>'; // Get original output path $this->output = $o_output; //создаем набор дескрипторов cURL $mh = curl_multi_init(); //$__modules = array('local', 'account', 'clients', 'dashboard', 'login', 'main', 'notification', 'project', 'sidebar', 'translators'); $system_methods = array('__construct', '__sleep', '__destruct', '__get', '__set', '__call', '__wakeup'); //handler // TODO: this is not should be rewritten to support new router // Perform generation of every controller foreach (s()->module_stack as $id => $ctrl) { $rmController = sizeof($ctrl->resourceMap->controllers) ? $ctrl->resourceMap->controllers : $ctrl->resourceMap->module; //trace($rmController, 1); $controller = array(); //trace($controller, true); if (isset($rmController[0]) && class_exists($rmController[0])) { if (!substr_count($rmController[1], 'vendor')) { $methods = get_class_methods($rmController[0]); foreach ($methods as $method) { if (!in_array($method, $system_methods)) { if ($method == '__handler') { $controller[] = '/' . $id; } elseif (substr_count($method, '__') && !substr_count($method, '__async')) { $new_method = str_replace('__', '', $method); $controller[] = '/' . $id . '/' . $new_method; } } } } } else { $controller = $rmController; } foreach ($controller as $cntrl) { if (strpos($cntrl, '.php')) { // generate controller URL $cntrl = '/' . locale_path('ru') . strtolower(basename($cntrl, '.php')); } elapsed('Generating HTML snapshot for: ' . $cntrl); // Create curl instance $ch = \curl_init('127.0.0.1' . $cntrl); // Set base request options \curl_setopt_array($ch, array(CURLOPT_VERBOSE => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array('Host: ' . $_SERVER['HTTP_HOST']))); // Add curl too multi request curl_multi_add_handle($mh, $ch); } } // TODO: Create function\module for this // Curl multi-request $active = null; do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active && $mrc == CURLM_OK) { if (curl_multi_select($mh) != -1) { do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } } curl_multi_close($mh); // Files array $files = array(); // Iterate generated pages foreach (\samson\core\File::dir($pages_path, 'html', null, $files, 0) as $f) { // Ignore default controller index page as it will be included in this controller if (strpos($f, '/index.html') != false) { continue; } // Read HTML file $html = file_get_contents($f); // If we have resourcer CSS resource if (isset($cssPath[0])) { //$html = str_replace(basename($cssPath, '.css'), 'style', $html); //$html = str_replace('/cache/resourcer/style.css', 'style.css', $html); $html = preg_replace("'<link type=\"text/css\"[^>]*?>'si", '<link type="text/css" rel="stylesheet" href="style.css">', $html); } // If we have resourcer JS resource if (isset($jsPath[0])) { $html = preg_replace("'<script[^>]*?></script>'si", '<script type="text/javascript" src="index.js"></script>', $html); } // Change path in all img SRC attributes if (preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/i', $html, $matches)) { if (isset($matches['url'])) { foreach ($matches['url'] as $match) { trace($match . '-' . (__SAMSON_PUBLIC_PATH . ltrim($match, '/'))); $html = str_ireplace($match, __SAMSON_PUBLIC_PATH . ltrim($match, '/'), $html); } } } // Fix images inside modules $html = str_replace('<img src="img', '<img src="www/img', $html); // Remove relative links $html = str_ireplace('<base href="/">', '', $html); // Get just file name $view_path = ($locale != \samson\core\SamsonLocale::DEF ? $locale . '_' : '') . basename($f); // Save HTML file file_put_contents($this->output . $view_path, $html); // Create index.html record $views .= '<a href="' . $view_path . '">' . basename($f) . '</a><br>'; } } // Write index file file_put_contents($this->output . 'index.html', $views); // TODO: Add support to external-internal modules // Iterate other resources foreach (\samson\core\ResourceMap::get($this->input, false, array(__SAMSON_PUBLIC_PATH . 'out/'))->resources as $type => $files) { if (!in_array($type, $this->restricted)) { foreach ($files as $f) { $this->copy_resource($f, str_replace($this->input, $this->output, $f)); } } } $imagesArray = array('png', 'jpg', 'jpeg', 'gif'); foreach (s()->module_stack as $module) { if (isset($module->resourceMap->module[1]) && !substr_count($module->resourceMap->module[1], 'vendor')) { foreach ($imagesArray as $format) { if (isset($module->resourceMap->resources[$format]) && sizeof($module->resourceMap->resources[$format])) { foreach ($module->resourceMap->resources[$format] as $file) { $this->copy_resource($file, $this->output . 'www/img/' . basename($file)); } } } } } // Generate zip file name $zip_file = $o_output . 'www.zip'; elapsed('Creating ZIP file: ' . $zip_file); // Create zip archieve $zip = new \ZipArchive(); if ($zip->open($zip_file, \ZipArchive::CREATE) === true) { foreach (\samson\core\File::dir($this->output) as $file) { $zip->addFile($file, str_replace($this->output, '', $file)); } $zip->close(); } else { elapsed('Cannot create zip file'); } }
/** Require composer autoloader */ if (!class_exists('samson\\core\\Core')) { require_once '../vendor/autoload.php'; } /** Automatic parent web-application configuration read */ if (file_exists('../../../app/config')) { /** Special constant to disable local ActiveRecord configuration */ define('EXTERNAL_CONFIG', true); // Signal core configure event \samsonphp\event\Event::signal('core.configure', array('../../../' . __SAMSON_CONFIG_PATH, __SAMSON_PUBLIC_PATH . __SAMSON_BASE__)); } // Set supported locales setlocales('en', 'ua', 'ru'); // Start SamsonPHP application s()->composer()->subscribe('core.e404', 'default_e404')->subscribe('core.routing', array(url(), 'router')); /** Automatic external SamsonCMS Application searching */ if (file_exists('../../../src/')) { // Get reource map to find all modules in src folder foreach (\samson\core\ResourceMap::get('../../../src/')->modules as $module) { // We are only interested in SamsonCMS application ancestors if (in_array('samson\\cms\\App', class_parents($module[2])) !== false) { // Remove possible '/src/' path from module path if (($pos = strripos($module[1], '/src/')) !== false) { $module[1] = substr($module[1], 0, $pos); } // Load s()->load($module[1]); } } } s()->start('default');