$autoload = (require getcwd() . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'); require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'sauce.php'; require __DIR__ . DIRECTORY_SEPARATOR . 'functions.php'; \Sauce\Base::bind(function ($bootstrap) { die($bootstrap()); }); run(function () { \Sauce\Shell\CLI::initialize(); $xargs = \Sauce\Shell\CLI::values(); $command = array_shift($xargs); $paths = array(); $paths[] = path(__DIR__, 'tasks'); is_dir($app_tasks = path(APP_PATH, 'tasks')) && ($paths[] = $app_tasks); \IO\Dir::open($paths, function ($file) { if (is_dir($file)) { require path($file, 'initialize.php'); } else { require $file; } }); if (!$command) { help(arg('help')); } else { try { \Sauce\Shell\Task::exec($command, $xargs); } catch (\Exception $e) { \Sauce\Shell\CLI::error("\n \\cred,black({$e->getMessage()})\\c\n"); } } });
<?php $path = array_shift($params); if (!$path) { error("\n Missing model path\n"); } elseif (is_file($path)) { hydrate_model($path); } else { $mod_path = path(APP_PATH, $path); if (!is_dir($mod_path)) { error("\n Model path '{$path}' does not exists\n"); } else { $crawl = function ($file) { hydrate_model($file); }; if (arg('R recursive')) { \IO\Dir::each($mod_path, '*.php', $crawl); } else { \IO\Dir::open($mod_path, $crawl); } } }
private static function extract($from, $on) { $out = array(); is_file($from) or $from = \Tailor\Helpers::resolve($from, $on); if (!is_file($from)) { throw new \Exception("The file '{$from}' does not exists"); } $ext = static::extension($on); $dir = \Tailor\Config::get($on); $url = \Tailor\Config::get(str_replace('_dir', '_url', $on)); // TODO: accept other formats? if (preg_match_all('/\\s+\\*=\\s+(\\w+)\\s+(\\S+)/m', read($from), $match)) { foreach ($match[1] as $i => $key) { $tmp = path($dir, $match[2][$i]); $old = \Tailor\Helpers::findfile("{$tmp}*", 0); if (is_dir($old)) { \IO\Dir::open($old, function ($file) use(&$out, $key) { $out[$key][] = $file; }); } else { if (!is_file($old)) { $old = "{$url}/{$match[2][$i]}"; substr($old, -strlen($ext)) != $ext && ($old .= ".{$ext}"); } $out[$key][] = $old; } } } return $out; }
\IO\Dir::open(path($base_path, $type), function ($file) use($base_path, $static_dir, $type, $cache_dir, $cache) { if (is_file($file)) { $out = array(); $tmp = \Sauce\App\Assets::parse($file); $test = array(); isset($tmp['head']) && ($test = array_merge($test, $tmp['head'])); isset($tmp['require']) && ($test = array_merge($test, $tmp['require'])); // required scripts, stand-alone if ($test) { foreach ($test as $old) { $key = str_replace($base_path . DIRECTORY_SEPARATOR, '', $old); $new = path($static_dir, $key); if ($type === 'js' && !preg_match('/\\.(min|pack)\\.js$/', $key)) { $min = path(TMP, md5($key)); write($min, js_min(read($old))); $old = $min; } if (!isset($cache[$key])) { if (s3_handle()) { s3_upload_asset($old, $key); } elseif (!is_file($new)) { is_dir(dirname($new)) or mkdir(dirname($new), 0777, TRUE); copy($old, $new); } $cache[$key] = 1; status('prepare', $key); } } } // asset mashup, grouped if (!empty($tmp['include'])) { foreach ($tmp['include'] as $test) { $ext = \IO\File::ext($test); $key = str_replace(APP_PATH . DIRECTORY_SEPARATOR, '', $test); $name = join('.', array_slice(explode('.', basename($test)), 0, 2)); $path = str_replace(path(APP_PATH, 'app', 'assets', $type), '__', dirname($test)); if ($ext != $type) { $cache_file = path($cache_dir, strtr("{$path}/{$name}", '\\/', '__')); if (is_file($cache_file)) { $out[$key] = !empty($cache[$key]) ? $cache[$key] : ($cache[$key] = read($cache_file)); } else { if (!empty($cache[$key])) { $out[$key] = $cache[$key]; } else { $out[$key] = \Tailor\Base::compile($test); write($cache_file, $out[$key]); } } } else { $out[$key] = read($test); } if ($type === 'css') { $out[$key] = css_min(solve_paths($out[$key])); } else { if (!preg_match('/\\.(min|pack)\\.js$/', $test)) { $out[$key] = js_min($out[$key]); } } status('prepare', "{$key}"); } } // final integration if (!empty($out)) { $set = array_keys($out); write($tmp = path(TMP, md5($file)), join("\n", $out)); $hash = md5(md5_file($tmp) . filesize($tmp)); $name = str_replace($base_path . DIRECTORY_SEPARATOR, '', $file); $min_file = path($static_dir, \IO\File::extn($name) . "{$hash}.{$type}"); if (s3_handle()) { s3_upload_asset($tmp, str_replace($static_dir . DIRECTORY_SEPARATOR, '', $min_file)); } else { is_dir(dirname($min_file)) or mkdir(dirname($min_file), 0777, TRUE); rename($tmp, $min_file); } \Sauce\App\Assets::assign($path = str_replace(path($base_path, $type) . DIRECTORY_SEPARATOR, '', $file), $hash); status('hashing', "{$path} [{$hash}]"); } } });
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); }