public function create($name = null, $type = null, $params = array()) { if (is_null($name) and !is_null($type)) { $name = $type; } $name = 'h' . date('Ymd_His') . (!is_null($name) ? "_{$name}" : ''); // get code for before if (!is_null($type) and method_exists($this, "create_before_for_{$type}")) { $code_before = call_user_func(array($this, "create_before_for_{$type}"), $params); } else { $code_before = ''; } // get code for after if (!is_null($type) and method_exists($this, "create_after_for_{$type}")) { $code_after = call_user_func(array($this, "create_after_for_{$type}"), $params); } else { $code_after = ''; } $content = "<?php\n class {$name} {\n\n // Run before patch\n public function before() {\n {$code_before}\n }\n\n // Run after patch\n public function after() {\n {$code_after}\n }\n\n }"; $dir = fx::path('@floxim/update/hook'); try { if (file_exists($dir)) { fx::files()->mkdir($dir); } fx::files()->writefile($dir . '/' . $name . '.php', $content); return true; } catch (Exception $e) { return false; } }
public function getAll() { static $all_modules_data = false; if ($all_modules_data === false) { $all_modules_data = fx::collection(); $vendor_dirs = glob(fx::path('@module/*')); foreach ($vendor_dirs as $vendor_dir) { if (!is_dir($vendor_dir)) { continue; } $vendor_name = fx::path()->fileName($vendor_dir); $vendor_modules = glob($vendor_dir . '/*'); foreach ($vendor_modules as $mod_dir) { if (!is_dir($mod_dir)) { continue; } $module_name = fx::path()->fileName($mod_dir); //fx::debug($vendor_name.'/'.$module_name); $module = array('vendor' => $vendor_name, 'name' => $module_name); $module_class = $vendor_name . "\\" . $module_name . "\\Module"; if (class_exists($module_class)) { $module['object'] = new $module_class(); } $all_modules_data[] = $module; } } } return $all_modules_data; }
public function find($name) { $dir = fx::path('@floxim/update/migration'); // get migrations $migration_files = glob($dir . '/m*_' . $name . '.php'); if (!$migration_files) { return null; } foreach ($migration_files as $migration_file) { $info = pathinfo($migration_file); require_once $migration_file; $class_name = $info['filename']; $m = new $class_name(); return $m; } }
public function redirect($target_url, $status = 301) { $target_url = fx::path()->http($target_url); if (fx::env('ajax')) { ob_start(); ?> <script type="text/javascript"> document.location.href = '<?php echo $target_url; ?> '; </script> <?php echo trim(ob_get_clean()); fx::complete(); die; } $this->status($status); header("Location: " . $target_url); fx::complete(); die; }
public function load(array $config = array()) { static $loaded = false; if (isset($config['disable'])) { $config['disable'] = $this->prepareDisableConfig($config['disable']); } $this->config = array_merge($this->config, $config); if (!$loaded) { if (!$this->config['dev.on'] && !defined("FX_ALLOW_DEBUG")) { define("FX_ALLOW_DEBUG", false); } if (!isset($this->config['db.dsn'])) { $this->config['db.dsn'] = 'mysql:dbname=' . $this->config['db.name'] . ';host=' . $this->config['db.host']; } define('FX_JQUERY_PATH', $this->config['path.jquery']); define('FX_JQUERY_PATH_HTTP', $this->config['path.jquery.http']); define('FX_JQUERY_UI_PATH', $this->config['path.jquery-ui']); } ini_set('date.timezone', $this->config['date.timezone']); fx::template()->registerSource('admin', fx::path('@floxim/Admin/templates')); $loaded = true; return $this; }
public function getConfig($searched_action = null) { if ($searched_action === true) { $searched_action = fx::util()->camelToUnderscore($this->action); } if (!is_null($this->_config_cache)) { return $searched_action ? $this->_config_cache['actions'][$searched_action] : $this->_config_cache; } $sources = $this->getConfigSources(); $actions = $this->getRealActions(); $blocks = array(); $meta = array(); $my_name = $this->getControllerName(); foreach ($sources as $src) { $src_hash = md5($src); $is_own = $my_name && fx::getComponentFullNameByPath(fx::path()->http($src)) === $my_name; $src = (include $src); if (!isset($src['actions'])) { continue; } $src_actions = $this->prepareActionConfig($src['actions']); foreach ($src_actions as $k => $props) { $action_codes = preg_split("~\\s*,\\s*~", $k); foreach ($action_codes as $ak) { $inherit_vertical = preg_match("~^\\*~", $ak); // parent blocks without vertical inheritance does not use if (!$is_own && !$inherit_vertical) { continue; } $inherit_horizontal = preg_match("~\\*\$~", $ak); $action_code = trim($ak, '*'); foreach (array('install', 'delete', 'save') as $cb_name) { if (isset($props[$cb_name])) { if (!is_array($props[$cb_name]) || is_callable($props[$cb_name])) { $props[$cb_name] = array($src_hash => $props[$cb_name]); } } } $blocks[] = $props; $meta[] = array($inherit_horizontal, $action_code); if (!isset($actions[$action_code])) { $actions[$action_code] = array(); } } } } foreach ($blocks as $bn => $block) { list($inherit, $bk) = $meta[$bn]; foreach ($actions as $ak => &$action_props) { if ($ak === $bk || $inherit && ($bk === '.' || substr($ak, 0, strlen($bk)) === $bk)) { $action_props = array_replace_recursive($action_props, $block); if (isset($action_props['settings'])) { foreach ($action_props['settings'] as $s_key => $s) { if (is_array($s) && !isset($s['name'])) { $action_props['settings'][$s_key]['name'] = $s_key; } } } } } } foreach ($actions as $action => &$action_props) { $action_name = fx::util()->underscoreToCamel($action); $settings_method = 'settings' . $action_name; if (method_exists($this, $settings_method)) { $action_props['settings'] = call_user_func(array($this, $settings_method), isset($action_props['settings']) ? $action_props['settings'] : array()); } $config_method = 'config' . $action_name; if (method_exists($this, $config_method)) { $action_props = call_user_func(array($this, $config_method), $action_props); } } unset($actions['.']); $this->_config_cache = array('actions' => $actions); return $searched_action ? $actions[$searched_action] : $this->_config_cache; }
/** * Импортирует файл из каталога импорта * Дополнительно проверяется соответствие по карте маппинга * * @param $file * @return string */ protected function importFile($file) { $fileFullPath = $this->currentDir . DIRECTORY_SEPARATOR . $this->pathRelDataFile . $file; if (array_key_exists($fileFullPath, $this->mapFiles)) { return $this->mapFiles[$fileFullPath]; } if (file_exists($fileFullPath)) { $fileName = pathinfo($file, PATHINFO_FILENAME); $filePath = pathinfo($file, PATHINFO_DIRNAME); $fileExt = pathinfo($file, PATHINFO_EXTENSION); $pathDest = fx::path($filePath) . DIRECTORY_SEPARATOR; $i = 0; $fileNameUniq = $fileName . '.' . $fileExt; /** * Формируем уникальное имя с проверкой на существование */ while (file_exists($pathDest . $fileNameUniq)) { $i++; $fileNameUniq = $fileName . '_' . $i . '.' . $fileExt; } fx::files()->copy($fileFullPath, $pathDest . $fileNameUniq); /** * Возвращать нужно относительный путь */ return $this->mapFiles[$fileFullPath] = $filePath . DIRECTORY_SEPARATOR . $fileNameUniq; } return null; }
protected function exportFile($fileRel) { $pathSource = fx::path($fileRel); $pathDist = $this->pathExportTmp . DIRECTORY_SEPARATOR . $this->pathRelDataFile . $fileRel; if (file_exists($pathSource) and !file_exists($pathDist)) { fx::files()->copy($pathSource, $pathDist); } }
public static function console($command) { ob_start(); $manager = new \Floxim\Floxim\System\Console\Manager(); $manager->addCommands(fx::config('console.commands')); $manager->addPath(fx::path()->abs('/vendor/Floxim/Floxim/System/Console/Command')); $manager->run($command); return ob_get_clean(); }
/** * */ public function dumpSiteData($site_id = null, $target_file = null) { if (is_null($site_id)) { $site_id = fx::env('site_id'); } if (is_null($target_file)) { $dir = '@files/export/site_' . $site_id; fx::files()->mkdir($dir); $target_file = fx::path()->abs($dir . '/data.sql'); } // export the site fx::db()->dump(array('tables' => array('site'), 'where' => 'id = ' . $site_id, 'schema' => false, 'file' => $target_file)); // export infoblocks fx::db()->dump(array('tables' => array('infoblock'), 'where' => 'site_id = ' . $site_id, 'schema' => false, 'file' => $target_file, 'add' => true)); // export URL aliases fx::db()->dump(array('tables' => array('url_alias'), 'where' => 'site_id = ' . $site_id, 'schema' => false, 'file' => $target_file, 'add' => true)); // export infoblock_visual $infoblock_ids = fx::data('infoblock')->where('site_id', $site_id)->all()->getValues('id'); fx::db()->dump(array('tables' => array('infoblock_visual'), 'where' => 'infoblock_id IN (' . join(", ", $infoblock_ids) . ')', 'schema' => false, 'file' => $target_file, 'add' => true)); // export main content table fx::db()->dump(array('tables' => array('floxim_main_content'), 'where' => 'site_id = ' . $site_id, 'schema' => false, 'file' => $target_file, 'add' => true)); // get existing content items $items = fx::db()->getResults('select id, type from {{floxim_main_content}} where site_id = ' . $site_id); $tables = $this->getContentDumpTables(fx::collection($items)); foreach ($tables as $t => $item_ids) { if ($t === 'floxim_main_content') { continue; } // export content table fx::db()->dump(array('tables' => array($t), 'where' => 'id IN (' . join(',', $item_ids) . ')', 'schema' => false, 'file' => $target_file, 'add' => true)); } }
public static function findThumbs($source_path) { $res = array(); $rel_path = fx::path()->http($source_path); $dir = glob(fx::path('@thumbs') . '/*'); if (!$dir) { return $res; } foreach ($dir as $sub) { if (is_dir($sub)) { $check_path = fx::path()->abs($sub . $rel_path); if (fx::path()->isFile($check_path)) { $res[] = $check_path; } } } return $res; }
public function addJsBundle($files, $params = array()) { // for dev mode if (fx::config('dev.on')) { foreach ($files as $f) { $this->addJsFile($f); } return; } if (!isset($params['name'])) { $params['name'] = md5(join($files)); } $params['name'] .= '.js.gz'; $http_path = fx::path()->http('@files/asset_cache/' . $params['name']); $full_path = fx::path()->abs($http_path); $http_files = array(); foreach ($files as $f) { if (!empty($f)) { $http_files[] = fx::path()->http($f); } } $this->all_js = array_merge($this->all_js, $http_files); if (!file_exists($full_path)) { $bundle_content = ''; foreach ($files as $i => $f) { if (!preg_match("~^http://~i", $f)) { $f = fx::path()->abs($f); } $file_content = file_get_contents($f); if (!preg_match("~\\.min~", $f)) { $minified = \JSMin::minify($file_content); $file_content = $minified; } $bundle_content .= $file_content . ";\n"; } $plain_path = preg_replace("~\\.js\\.gz\$~", ".js", $full_path); fx::files()->writefile($plain_path, $bundle_content); $fh = gzopen($full_path, 'wb5'); gzwrite($fh, $bundle_content); gzclose($fh); } if (!$this->acceptGzip()) { $http_path = preg_replace("~\\.js\\.gz\$~", ".js", $http_path); } $this->files_js[] = $http_path; }
public function dump($params) { $dump_path = fx::config('dev.mysqldump_path'); if (!$dump_path) { return; } if (is_string($params)) { $params = array('file' => $params); } if (!$params['file']) { return; } $target_file = fx::path($params['file']); $params = array_merge(array('data' => true, 'schema' => true, 'add' => false, 'where' => false, 'tables' => array()), $params); $command = $dump_path . ' -u' . fx::config('db.user') . ' -p' . fx::config('db.password') . ' --host=' . fx::config('db.host'); $command .= ' ' . fx::config('db.name'); if (!$params['schema']) { $command .= ' --no-create-info'; } if (!$params['data']) { $command .= ' --no-data'; } $command .= ' --skip-comments'; if ($params['where']) { $command .= ' --where="' . $params['where'] . '"'; } foreach ($params['tables'] as $t) { $command .= ' ' . $this->replacePrefix('{{' . $t . '}}'); } $do_gzip = isset($params['gzip']) && $params['gzip'] || preg_match("~\\.gz\$~", $target_file); if ($do_gzip) { $target_file = preg_replace("~\\.gz\$~", '', $target_file); } $command .= ($params['add'] ? ' >> ' : ' > ') . $target_file; exec($command); if ($do_gzip && file_exists($target_file)) { $gzipped_file = $target_file . '.gz'; $gzipped = gzopen($gzipped_file, 'w'); $raw = fopen($target_file, 'r'); while (!feof($raw)) { $s = fgets($raw, 4096); gzputs($gzipped, $s, 4096); } fclose($raw); gzclose($gzipped); unlink($target_file); return $gzipped_file; } }
/** * Print args to the output */ public function debug() { $e = call_user_func_array(array($this, 'entry'), func_get_args()); $this->printEntry($e); static $head_files_added = false; if (!$head_files_added) { fx::page()->addCssFile(fx::path('@floxim/Admin/style/debug.less')); fx::page()->addJsFile(FX_JQUERY_PATH); fx::page()->addJsFile(fx::path('@floxim/Admin/js/fxj.js')); fx::page()->addJsFile(fx::path('@floxim/Admin/js/debug.js')); register_shutdown_function(function () { if (!fx::env()->get('complete_ok')) { echo fx::page()->getAssetsCode(); } }); $head_files_added = true; } }
public function getPutFilePath($path) { $path = fx::path($path); $parts = null; $attempt = 0; $name = null; while (file_exists($path)) { if (is_null($parts)) { $parts = fx::path()->parse($path); $name = $parts['name']; } $parts['name'] = $name . '_' . $attempt++; $path = fx::path()->build($parts); } return $path; }
protected function getPutFilename($dir, $name) { $name = fx::util()->strToLatin($name); $name = preg_replace("~[^a-z0-9_\\.-]~i", '_', $name); $name = trim($name, "_"); $name = preg_replace("~_+~", "_", $name); $path = fx::path('@files/' . $dir . '/' . $name); $try = 0; while (fx::path()->exists($path)) { $c_name = preg_replace("~(\\.[^\\.]+)\$~", "_" . $try . "\$1", $name); $try++; $path = fx::path('@files/' . $dir . '/' . $c_name); } return fx::path()->fileName($path); }