public function sortRoutes() { if (empty($this->routes)) { if ($files = $this->resolver->getRoutes()) { foreach ($files as $file) { $dir = dirname($file, 2); /** @var Router $router */ $router = (new Injector())->make('Minute\\Routing\\Router'); require_once $file; $routes = $router->getRouteCollection(); /** @var Route $route */ foreach ($routes as $route) { $method = $route->getMethods()[0]; $defaults = $route->getDefaults(); if ($controller = $defaults['controller']) { $parts = explode('@', $controller, 2); list($classPath, $fn) = [$this->utils->unixPath($parts[0]), $parts[1] ?? 'index']; } else { list($classPath, $fn) = [null, 'index']; } $classPath = preg_replace('/\\.php$/', '', $classPath); $path = $this->utils->unixPath(sprintf('%s/Controller/%s.php', $dir, $classPath)); $action = [$this->resolver->getController($classPath), $fn]; $this->routes[] = array_merge($defaults, ['route' => $route, 'controller' => $controller, 'dir' => $dir, 'path' => $path, 'classPath' => $classPath, 'fn' => $fn, 'action' => $action, 'method' => $method]); } } } } return $this->routes; }
public function getViewPath($path) { $path = $this->utils->unixPath($path); $path = preg_replace('/\\{.*?\\}/', '', $path); $path = trim($path, '/'); $parts = explode('/', $path); $depth = min(count($parts) - 1, self::MAX_DEPTH); $capitalize = function ($f) { return ucwords(Str::camel($f)); }; $dirname = $depth > 0 ? join('/', array_map($capitalize, array_slice($parts, 0, $depth))) : null; $filename = join('', array_map($capitalize, array_slice($parts, $depth))); //throw new AwsError("hohohoo"); return !empty($dirname) ? sprintf('%s/%s', $dirname, $filename) : $filename; }
/** * Returns all matching files by namespace * * Example: find all route.php files in Routes folders, * $loader->find('App\Route\routes.php') * * Example: find all Cron directories * $loader->find('App\Controller\Cron') * * @return array */ public function find($glob) : array { @(list($prefix, $match) = explode('/', $this->utils->unixPath(trim($glob, '\\/')), 2)); foreach ($folders = $this->loader->getPrefixesPsr4()["{$prefix}\\"] ?? [] as $folder) { if (is_readable($path = sprintf('%s/%s', $folder, $match))) { $matches[] = realpath($path); } } return $matches ?? []; }
protected function compress($regex, $content, $type = 'js', $version, $excludes) { $files = []; $compressed = preg_replace_callback($regex, function ($matches) use(&$files, $type, $excludes) { if (!empty($excludes) && preg_match("~{$excludes}~", basename($matches[1] ?? ''))) { return $matches[0]; } if ($type === 'css' && !preg_match('/rel="stylesheet"/i', $matches[0])) { return $matches[0]; } else { $files[] = $matches[1]; } return ''; }, $content); if (!empty($files)) { MMinifiedDatum::unguard(true); $name = sprintf('%s.%s', md5(json_encode($files)), $type); $count = MMinifiedDatum::where('name', '=', $name)->where('version', '=', $version)->count(); $baseDir = $this->bootLoader->getBaseDir(); if (empty($count)) { set_time_limit(120); $asset = new AssetCollection(array_map(function ($f) use($type, $baseDir) { $file = realpath(sprintf('%s/public/%s', $baseDir, $f)); $minified = preg_match('/\\.min\\./', $file); if ($type === 'css') { $url = preg_replace('#.*/static/#', '/static/', $this->utils->unixPath($file)); $filters = array_merge([new CssImportFilter(), new CssRewriteFilter()], !$minified ? [new CssMinFilter()] : []); } elseif (!$minified) { $filters = [new JSMinFilter()]; } return new FileAsset($file, !empty($filters) ? $filters : [], $baseDir . '/public', !empty($url) ? $url : ''); }, $files)); $asset->setTargetPath(sprintf('/static/cache/%s/', $type)); if (MMinifiedDatum::create(['name' => $name, 'version' => $version, 'content' => $asset->dump(), 'created_at' => Carbon::now()])) { $count = 1; } } } if (!empty($count) && !empty($name)) { $tag = sprintf($type == 'js' ? '<scrip' . 't src="%s"></script>' : '<lin' . 'k href="%s" type="text/css" rel="stylesheet" media="all">', "/static/cache/{$version}/{$name}"); $compressed = $type === 'css' ? preg_replace('#(<style|</head)#ms', "{$tag}\n\\1", $compressed, 1) : preg_replace('#(<script|</head)#ms', "{$tag}\n\\1", $compressed, 1); return $compressed; } return $content; }