umask($oldmask); return $path; }, $directories)); $files = array(array('name' => 'database.php', 'path' => $path_config, 'params' => array('hostname' => $hostname, 'username' => $username, 'password' => $password, 'database' => $database)), array('name' => 'query.log', 'path' => $path_logs, 'params' => array()), array('name' => 'delay_job.log', 'path' => $path_logs, 'params' => array())); $results = array_merge($results, array_map(function ($file) use($temp_path) { $date = load_view($temp_path . $file['name'], $file['params']); if (!write_file($path = $file['path'] . $file['name'], $date)) { console_error("寫入 " . $file['name'] . " 失敗!"); } $oldmask = umask(0); @chmod($path, 0777); umask($oldmask); return $path; }, $files)); // ln -sf system/cmd cmd $links = array(array('target' => 'system/cmd', 'link' => 'cmd')); $results = array_merge($results, array_map(function ($link) { if (!symlink(FCPATH . $link['target'], FCPATH . $link['link'])) { console_error("Link " . $link['link'] . color(' → ', 'c') . $link['target'] . " 失敗!"); } $oldmask = umask(0); @chmod($link['link'], 0777); umask($oldmask); return $link['link'] . color(' → ', 'c') . $link['target']; }, $links)); $results = array_map(function ($result) { $count = 1; return color('Create: ', 'g') . str_replace(FCPATH, '', $result, $count); }, $results); array_unshift($results, '初始化成功!'); call_user_func_array('console_log', $results);
case 'controller': $results = create_controller($temp_path, $name, $action); break; case 'model': $params = params($action, array('-p', '-f', '-pic', '-file')); $images = array_merge($images = isset($params['-p']) ? $params['-p'] : array(), isset($params['-pic']) ? $params['-pic'] : array()); $files = array_merge($files = isset($params['-f']) ? $params['-f'] : array(), isset($params['-file']) ? $params['-file'] : array()); $results = create_model($temp_path, $name, $images, $files); break; case 'migration': $results = create_migration($temp_path, $name, $action); break; case 'cell': $results = create_cell($temp_path, $name, array_merge(array($action), $argv)); break; case 'demo': include 'functions/demo.php'; $results = create_demo(); break; case 'search': $results = create_search($temp_path, $name); break; default: return console_error('指令錯誤!', '只接受 controller、model、migration、cell、demo、search 指令。'); } $results = array_map(function ($result) { $count = 1; return color('Create: ', 'g') . str_replace(FCPATH, '', $result, $count); }, $results); array_unshift($results, '新增成功!'); call_user_func_array('console_log', $results);
function create_cell($temp_path, $name, $methods = array()) { $results = array(); $name = strtolower($name); $methods = array_filter($methods); $class_suffix = '_cell'; $method_prefix = '_cache_'; $controllers_path = FCPATH . 'application/cell/controllers/'; $views_path = FCPATH . 'application/cell/views/'; $controllers = array_map(function ($t) { return basename($t, EXT); }, directory_map($controllers_path, 1)); $views = directory_map($views_path, 1); if (!is_writable($controllers_path) || !is_writable($views_path)) { console_error("無法有寫入的權限!"); } if ($controllers && in_array($file_name = $name . $class_suffix, $controllers) || $views && in_array($file_name, $views)) { console_error("名稱錯誤!"); } $oldmask = umask(0); @mkdir($view_path = $views_path . $file_name . '/', 0777, true); umask($oldmask); $date = load_view($temp_path . 'cell.php', array('file_name' => $file_name, 'name' => $name, 'methods' => $methods, 'method_prefix' => $method_prefix)); if (!write_file($controller_path = $controllers_path . $file_name . EXT, $date)) { console_error("新增 controller 失敗!"); } array_push($results, $controller_path); if (!is_writable($view_path)) { delete_file($controller_path); console_error("新增 view 失敗!"); } if (count(array_filter(array_map(function ($method) use($view_path, $temp_path, &$results) { $oldmask = umask(0); @mkdir($view_path . $method . '/', 0777, true); umask($oldmask); if (!is_writable($view_path . $method . '/')) { return null; } $files = array('content.css', 'content.scss', 'content.js', 'content.php'); return count(array_filter(array_map(function ($file) use($view_path, $method, $temp_path, &$results) { $data = !is_readable($path = $temp_path . $method . '/' . $file) ? is_readable($path = $temp_path . 'index' . '/' . $file) ? load_view($path) : '' : load_view($path); if (write_file($view_path . $method . '/' . $file, $data)) { array_push($results, $view_path . $method . '/' . $file); return true; } else { return false; } }, $files))); }, $methods))) != count($methods)) { delete_file($controller_path); directory_delete($view_path, true); console_error("新增 view 失敗!"); } return $results; }
<?php /** * @author OA Wu <*****@*****.**> * @copyright Copyright (c) 2015 OA Wu Design */ include_once 'base.php'; include_once 'functions/clean.php'; // file type name action // ======================================================================= // php clean cache [cell | file | model | [assets | static]] $file = array_shift($argv); $type = array_shift($argv); $name = array_shift($argv); switch (strtolower($type)) { case 'cache': $results = clean_cache($name); break; default: return console_error('指令錯誤!', '只接受 cache 指令。'); } $results = array_map(function ($result) { $count = 1; return color('Clean: ', 'g') . str_replace(FCPATH, '', $result, $count); }, $results); array_unshift($results, '清除成功!'); call_user_func_array('console_log', $results);
// php delete controller controller_name [site | admin | delay] // php delete model model_name // php delete cell cell_name // php delete demo $file = array_shift($argv); $type = array_shift($argv); $name = array_shift($argv); $action = array_shift($argv); switch ($type) { case 'controller': $results = delete_controller($name, $action); break; case 'model': $results = delete_model($name); break; case 'cell': $results = delete_cell($name); break; case 'demo': include 'functions/demo.php'; $results = delete_demo(); break; default: return console_error('指令錯誤!', '只接受 controller、model、cell、demo 三種指令。'); } $results = array_map(function ($result) { $count = 1; return color('Delete: ', 'r') . str_replace(FCPATH, '', $result, $count); }, $results); array_unshift($results, '刪除成功!'); call_user_func_array('console_log', $results);
$results = array(); if (!is_writable($path_config = FCPATH . 'application/config/')) { console_error("無法有 application/config/ 的寫入權限!"); } if (!is_writable($path_logs = FCPATH . 'application/logs/')) { console_error("無法有 application/logs/ 的寫入權限!"); } $directories = array('assets', 'temp', 'upload', 'application/cell/cache', 'application/cache/file', 'application/cache/config', 'application/cache/output', 'application/cache/model'); $results = array_merge($results, array_map(function ($directory) { $oldmask = umask(0); @mkdir($path = FCPATH . $directory . '/', 0777, true); umask($oldmask); return $path; }, $directories)); $files = array(array('name' => 'database.php', 'path' => $path_config, 'params' => array('hostname' => $hostname, 'username' => $username, 'password' => $password, 'database' => $database)), array('name' => 'query.log', 'path' => $path_logs, 'params' => array()), array('name' => 'delay_job.log', 'path' => $path_logs, 'params' => array())); $results = array_merge($results, array_map(function ($file) use($temp_path) { $date = load_view($temp_path . $file['name'], $file['params']); if (!write_file($path = $file['path'] . $file['name'], $date)) { console_error("寫入 " . $file['name'] . " 失敗!"); } $oldmask = umask(0); @chmod($path, 0777); umask($oldmask); return $path; }, $files)); $results = array_map(function ($result) { $count = 1; return color('Create: ', 'g') . str_replace(FCPATH, '', $result, $count); }, $results); array_unshift($results, '初始化成功!'); call_user_func_array('console_log', $results);
function create_search($temp_path, $name) { $results = array(); $name = singularize($name); $searches_path = FCPATH . 'application/searches/'; $searches = array_map(function ($t) { return basename($t, EXT); }, directory_map($searches_path, 1)); $class_suffix = 'Search'; if ($searches && in_array(ucfirst($name), $searches)) { console_error("名稱重複!"); } if (!is_writable($searches_path)) { console_error("無法有寫入的權限!"); } $data = load_view($temp_path . 'search.php', array('name' => $name, 'class_suffix' => $class_suffix)); if (!write_file($model_path = $searches_path . ucfirst(camelize($name . $class_suffix)) . EXT, $data)) { console_error("新增 search 失敗!"); } array_push($results, $model_path); return $results; }