<?php $source_dir = path($target_dir, 'views', 'pages'); $assets_dir = path($target_dir, 'assets'); $output_dir = path($target_dir, 'static'); if (!arg('c create') && !is_dir($source_dir)) { return error("\n No source directory especified\n"); } $config_file = path($target_dir, 'config.php'); is_file($config_file) && config($config_file); $base_url = option('base_url'); // routing \Broil\Config::set('rewrite', TRUE); \Broil\Config::set('server_base', $base_url); \Broil\Config::set('tld_size', substr_count($base_url, '.')); // assets \Tailor\Config::set('fonts_url', "{$base_url}/font"); \Tailor\Config::set('images_url', "{$base_url}/img"); \Tailor\Config::set('styles_url', "{$base_url}/css"); \Tailor\Config::set('scripts_url', "{$base_url}/js"); // templating \Tailor\Config::set('cache_dir', TMP); \Tailor\Config::set('views_dir', path($target_dir, 'views')); \Tailor\Config::set('fonts_dir', path($assets_dir, 'font')); \Tailor\Config::set('images_dir', path($assets_dir, 'img')); \Tailor\Config::set('styles_dir', path($assets_dir, 'css')); \Tailor\Config::set('scripts_dir', path($assets_dir, 'js')); if (arg('b build')) { say("\n \\cwhite,blue(**COMPILING**)\\c"); } else { say("\n \\cwhite,magenta(**WATCHING**)\\c\n Press \\bwhite(CTRL+C)\\b to exit");
public static function initialize(\Closure $lambda) { $test = strtoupper(PHP_SAPI); if (strpos($test, 'CLI') === FALSE or $test === 'CLI-SERVER') { // static assets if ($path = \Postman\Request::value('@')) { $test = \Sauce\App\Assets::read($path); $output = new \Postman\Response(200, array('Content-Type' => $test['type']), $test['output']); \Sauce\Logger::debug("Serve {$path}"); return $output; } // settings \Labourer\Web\Session::initialize(); ignore_user_abort(FALSE); // locales if (!($key = option('language'))) { $set = \Locale\Base::all(); $key = key($set); } @setlocale(LC_ALL, "{$key}.UTF-8"); \Locale\Config::set('default', $key); \Locale\Base::load_path(path(APP_PATH, 'app', 'locale')); } \Sauce\Logger::debug('Ready'); // defaults $out = \Sauce\Base::$response; $lambda($out); if ($action = \Broil\Routing::run()) { $uri = \Broil\Config::get('request_uri'); $method = \Broil\Config::get('request_method'); \Sauce\Logger::debug("{$method} {$uri}"); \Sauce\Logger::debug("Route {$action['match']}"); if (!empty($action['before'])) { foreach ((array) $action['before'] as $callback) { $action = call_user_func($callback, $action); } } params($action['params']); if (is_string($action['to'])) { if (strpos($action['to'], '://') !== FALSE) { $out = new \Postman\Response(redirect($action)); } elseif (strpos($action['to'], '#') !== FALSE) { $cache = empty($action['no-cache']) ? isset($action['expires']) ? $action['expires'] : option('expires') : 0; $cache = APP_ENV === 'production' && \Postman\Request::method() === 'GET' ? $cache : 0; @(list($controller, $method) = explode('#', (string) $action['to'])); \Sauce\App\Handler::execute($controller, $method, $cache); } else { throw new \Exception("Unknown '{$action['to']}' action"); } } elseif (is_callable($action['to'])) { ob_start(); $tmp = call_user_func($action['to']); $old = ob_get_clean(); if (is_array($tmp)) { @(list($out->status, $out->headers, $out->response) = $tmp); } else { $out->status = is_numeric($tmp) ? (int) $tmp : 200; $out->response = is_string($tmp) ? $tmp : $old; } } elseif (is_array($action['to'])) { $out = new \Postman\Response($action['to']); } else { throw new \Exception("Cannot execute '{$action['to']}'"); } if (!empty($action['after'])) { foreach ((array) $action['after'] as $callback) { $out = call_user_func($callback, $out); } } \Sauce\Logger::debug('Done ', json_encode((array) $out->headers)); return $out; } else { throw new \Exception("Route not reach"); } }
public static function initialize(\Closure $lambda) { if (static::$loaded) { throw new \Exception("Application already loaded"); } // request vars params($_REQUEST); // configuration config(path(APP_PATH, 'config.php')); config(path(APP_PATH, 'config', 'application.php')); config(path(APP_PATH, 'config', 'environments', APP_ENV . '.php')); // timezone date_default_timezone_set(option('timezone', 'UTC')); // setup $test = strtoupper(PHP_SAPI); if (strpos($test, 'CLI') === FALSE or $test === 'CLI-SERVER') { define('INDEX', basename(APP_LOADER)); // root+uri $url = array(); $url['ORIG_PATH_INFO'] = FALSE; $url['REQUEST_URI'] = FALSE; $url['SCRIPT_URL'] = TRUE; $url['PATH_INFO'] = FALSE; $url['PHP_SELF'] = TRUE; foreach ($url as $key => $val) { if (!isset($_SERVER[$key])) { continue; } if (strpos($_SERVER[$key], INDEX) && $val === FALSE) { continue; } $url = $_SERVER[$key]; break; } $base = array(); $base['ORIG_SCRIPT_NAME'] = TRUE; #$base['SCRIPT_FILENAME'] = TRUE; $base['SCRIPT_NAME'] = TRUE; $base['PHP_SELF'] = FALSE; foreach ($base as $key => $val) { if (!isset($_SERVER[$key])) { continue; } if (strpos($_SERVER[$key], INDEX) && $val === FALSE) { continue; } $base = $_SERVER[$key]; break; } // site root $base = preg_replace('/' . preg_quote(INDEX) . '.*$/', '', $base); if (($root = server('DOCUMENT_ROOT')) != '/') { $base = str_replace($root, '.', $base); } define('ROOT', ltrim(str_replace(INDEX, '', $base), '.')); // URL cleanup $root = preg_quote(ROOT, '/'); $index = preg_quote(INDEX, '/'); $parts = explode('?', $url); $parts = preg_replace("/^(?:{$root}(?:{$index})?)?\$/", '', array_shift($parts)); define('URI', '/' . trim($parts, '/')); if (empty($_SERVER['REQUEST_URI'])) { $_SERVER['REQUEST_URI'] = server('SCRIPT_NAME', server('PHP_SELF')); $_SERVER['REQUEST_URI'] .= $query = server('QUERY_STRING') ? "?{$query}" : ''; } $base_url = \Postman\Request::host(); } else { $parts = explode('/', option('base_url')); $root = '/' . join('/', array_slice($parts, 3)); $base_url = join('/', array_slice($parts, 0, 3)); // TODO: set URI/REQUEST_METHOD from CLI arguments... define('INDEX', option('index_file') ?: 'index.php'); define('ROOT', $root ?: '/'); define('URI', '/'); $_SERVER['REQUEST_URI'] = URI; $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['DOCUMENT_ROOT'] = APP_PATH; } // assets if (APP_ENV != 'production') { $doc_root = $base_url . ROOT . '?@='; } elseif (!($doc_root = option('asset_host'))) { if ($doc_root = option('asset_subdomain')) { $doc_root = \Broil\Helpers::reduce($base_url, $doc_root); } else { $doc_root = $base_url . ROOT . 'static'; } } \Tailor\Config::set('fonts_url', "{$doc_root}/font"); \Tailor\Config::set('images_url', "{$doc_root}/img"); \Tailor\Config::set('styles_url', "{$doc_root}/css"); \Tailor\Config::set('scripts_url', "{$doc_root}/js"); // templating \Tailor\Config::set('cache_dir', path(APP_PATH, 'app', 'cache')); \Tailor\Config::set('views_dir', path(APP_PATH, 'app', 'views')); \Tailor\Config::set('fonts_dir', path(APP_PATH, 'app', 'assets', 'font')); \Tailor\Config::set('images_dir', path(APP_PATH, 'app', 'assets', 'img')); \Tailor\Config::set('styles_dir', path(APP_PATH, 'app', 'assets', 'css')); \Tailor\Config::set('scripts_dir', path(APP_PATH, 'app', 'assets', 'js')); // web goodies \Labourer\Config::set('csrf_salt', ''); \Labourer\Config::set('csrf_token', ''); \Labourer\Config::set('csrf_expire', 300); \Labourer\Config::set('session_path', ROOT); \Labourer\Config::set('session_expire', 3600); \Labourer\Config::set('upload_path', path(APP_PATH, 'static', 'uploads')); \Labourer\Config::set('upload_type', 'image/*'); \Labourer\Config::set('upload_min_size', 96); \Labourer\Config::set('upload_max_size', 2097152); \Labourer\Config::set('upload_extension', array('jpeg', 'jpg', 'png', 'gif', 'ico')); \Labourer\Config::set('upload_skip_error', FALSE); \Labourer\Config::set('upload_multiple', TRUE); \Labourer\Config::set('upload_unique', TRUE); \Labourer\Config::set('s3_key', ''); \Labourer\Config::set('s3_secret', ''); \Labourer\Config::set('s3_bucket', ''); \Labourer\Config::set('s3_location', FALSE); \Labourer\Config::set('s3_permission', 'public_read'); // database \Grocery\Config::set('unserialize', APP_ENV === 'production' ? 'ignore' : 'reset'); \Servant\Config::set('default', 'sqlite::memory:'); // caching \Cashier\Config::set('cache_dir', TMP); \Cashier\Config::set('driver', option('cache', 'php')); // connections if ($test = option('database')) { foreach ($test as $key => $val) { \Servant\Config::set($key, $val, APP_ENV === 'production'); } } // debug sql \Grocery\Config::set('logger', function ($sql, $ms) { $sql = preg_replace('/\\s+/', ' ', $sql); \Sauce\Logger::info("{$sql} ({$ms})"); }); // start up \Tailor\Base::initialize(); \Labourer\Base::initialize(); // routing \Broil\Config::set('root', ROOT); \Broil\Config::set('index_file', INDEX); \Broil\Config::set('rewrite', option('rewrite')); \Broil\Config::set('request_uri', URI); \Broil\Config::set('request_method', method()); \Broil\Config::set('server_base', $base_url); \Broil\Config::set('tld_size', option('tld_size')); // load routes $routes_file = path(APP_PATH, 'config', 'routes.php'); is_file($routes_file) && (require $routes_file); // before any initializer? foreach (static::$middleware as $callback) { $lambda = $callback($lambda); } // scripts $init_path = path(APP_PATH, 'config', 'initializers'); if (is_dir($init_path)) { \IO\Dir::open($init_path, function ($path) { require is_dir($path) ? path($path, 'initialize.php') : $path; }); } // go! static::$loaded = TRUE; static::$response = new \Postman\Response(); return \Sauce\App\Bootstrap::initialize($lambda); }