function copy_dir($to, $from) { status('copy', path(rtrim($to, DIRECTORY_SEPARATOR), basename($from))); \IO\Dir::cpfiles($from, path($to, basename($from)), '*', TRUE); }
$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"); } } });
\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}]"); } } });
<?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); } } }
$set = array('font' => arg('f fonts all'), 'img' => arg('i images all'), 'css' => arg('c styles all'), 'js' => arg('j scripts all')); if (s3_handle()) { s3_clean_bucket($set); } else { $static_dir = path(APP_PATH, 'static'); is_dir($static_dir) or mkdir($static_dir, 0777); foreach ($set as $type => $ok) { $files_dir = path($static_dir, $type); if (is_dir($files_dir) && $ok) { status('remove', "static/{$type}"); \IO\Dir::unfile($files_dir, '*', TRUE); } is_dir($files_dir) or mkdir($files_dir, 0777); } } if (arg('v views all')) { status('remove', 'cache'); $cache_dir = path(APP_PATH, 'app', 'cache'); is_dir($cache_dir) && \IO\Dir::unfile($cache_dir, '*.*', TRUE); is_dir($cache_dir) or mkdir($cache_dir); chmod($cache_dir, 0777); } // TODO: unset from cache? if (arg('r reset all')) { $res_file = path(APP_PATH, 'config', 'resources.php'); write($res_file, "<?php return array();\n"); status('update', 'config/resources.php'); } } else { error("\n Nothing to do\n"); }
<?php create_dir($target_dir); $out = array(); $tmp_dir = path(dirname(__DIR__), 'assets'); $base_dir = path($tmp_dir, 'static'); \IO\Dir::each($base_dir, '*', function ($file) use($target_dir, $base_dir) { $new = str_replace($base_dir . DIRECTORY_SEPARATOR, '', $file); $out = path($target_dir, $new); if (!is_dir($file)) { $path = dirname($out); if (!is_dir($path)) { status('create', $path); mkdir($path, 0755, TRUE); } status('copy', $out); copy($file, $out); } }); copy_file(path($target_dir, 'assets', 'css'), path($tmp_dir, 'sauce.less')); copy_file(path($target_dir, 'assets', 'css'), path($tmp_dir, 'base.less')); copy_file(path($target_dir, 'assets', 'css'), path($tmp_dir, 'media.less')); copy_file(path($target_dir, 'assets', 'css'), path($tmp_dir, 'styles.css.less')); copy_file(path($target_dir, 'assets', 'js', 'lib'), path($tmp_dir, 'console.js')); copy_file(path($target_dir, 'assets', 'js', 'lib'), path($tmp_dir, 'jquery.min.js')); copy_file(path($target_dir, 'assets', 'js', 'lib'), path($tmp_dir, 'modernizr.min.js')); copy_file(path($target_dir, 'assets', 'js'), path($tmp_dir, 'script.js.coffee')); $name = camelcase(basename($target_dir)); create_file(path($target_dir, '.gitignore'), ".cache\nstatic/*"); create_file(path($target_dir, 'config.php'), '<' . "?php\n\n\$config['title'] = '{$name}';\n\$config['base_url'] = '';\n");
\IO\Dir::each($from, '*', function ($file) use(&$cache, $source_dir, $assets_dir, $output_dir) { $key = md5_file($file); $key .= filemtime($file); if (in_array($key, $cache)) { return; } $cache[] = $key; $path = trim(dirname(str_replace(array($source_dir, $assets_dir), '', $file)), '.\\/'); if (preg_match('/\\.((?:ht|x)ml|rss|txt|php|css|js)(?:\\.\\w+)*?$/', $file, $match)) { $type = $match[1]; @(list($name) = explode(".{$type}", basename($file))); $base = str_replace("{$name}.{$type}", '', basename($file)) ?: FALSE; $out = path($output_dir, $path, "{$name}.{$type}"); $dir = dirname($out); is_dir($dir) or mkdir($dir, 0777, TRUE); if (!is_file($out) or filemtime($file) > filemtime($out)) { switch ($type) { case 'html': $uri = $path ? "/{$path}" : ''; $uri .= $name != 'index' ? "/{$name}.{$type}" : '/'; assign('current_url', $uri); if ($base) { $view = \Tailor\Base::compile($file); } else { $view = read($file); } $layout = fetch('layout') ?: 'default'; $layout = \Tailor\Helpers::resolve("layouts/{$layout}", 'views_dir'); $hash = "{$layout}@"; $hash .= md5_file($file); $hash .= filemtime($file); if (is_file($layout)) { $view = adjust_tags($view, \Tailor\Base::compile($layout)); } status('partial', $out); write($out, $view); break; case 'css': case 'js': default: if ($base) { $view = \Tailor\Base::compile($file); status('partial', $out); write($out, $view); } else { status('copy', $out); copy($file, $out); } break; } } // TODO: more formats or better check-up? } elseif (preg_match('/\\.(?:jpe?g|png|gif|woff|eot|ttf|svg|ico|xml|rss|txt|webm|mp[34]|og[gv])$/', $file)) { $out = path($output_dir, $path, basename($file)); $dir = dirname($out); is_dir($dir) or mkdir($dir, 0777, TRUE); status('copy', $out); copy($file, $out); } });
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; }
\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"); } say("\n *From*: {$source_dir}\n *To*: {$output_dir}\n"); $cache_file = path($target_dir, '.cache'); $cache = explode("\n", read($cache_file)) ?: array(); $timeout = (int) arg('t to') ?: 3; if ($recreate) { require path(__DIR__, 'create_skeleton.php'); } elseif (arg('r reset')) { $cache = array(); \IO\Dir::unfile($output_dir, '*', TRUE); is_dir($output_dir) or mkdir($output_dir, 0755, TRUE); status('reset'); } if (arg('b build')) { $a = microtime(TRUE); $cache = array(); $timeout = 0; require path(__DIR__, 'do_compile.php'); $diff = round(microtime(TRUE) - $a, 4); say("\n *Done*: {$diff}s\n"); } else { \Sauce\Shell\CLI::main(function () use(&$cache, $target_dir, $assets_dir, $source_dir, $output_dir, $timeout) { require path(__DIR__, 'do_compile.php'); }); }
<?php $name = array_shift($params); $test = realpath(dirname($name)); if (!$name) { error("\n Missing application name\n"); } elseif (!is_dir($test)) { error("\n Can't create the directory\n"); } else { $app_path = path($test, basename($name)); if (!arg('f force') && is_dir($app_path)) { error("\n Directory '{$name}' already exists\n"); } else { arg('D delete-all') && \IO\Dir::unfile($app_path, '*', TRUE); require path(__DIR__, 'create_application.php'); } }
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); }