public function run($path = '/')
{
if ($this->kirby->option('patterns.lock') && !$this->kirby->site()->user()) {
go($this->kirby->option('error'));
}
// error handling
$whoops = new \Whoops\Run();
$whoops->pushHandler(function ($e) {
throw $e;
return \Whoops\Handler\Handler::QUIT;
});
$whoops->register();
tpl::$data = ['site' => $this->kirby->site(), 'pages' => $this->kirby->site()->children(), 'page' => $this->kirby->site()->find($this->kirby->option('home')), 'lab' => $this];
$router = new Router();
$router->register([['pattern' => '/', 'action' => function () {
$readme = $this->root() . DS . 'readme.md';
if (!is_dir($this->root())) {
$modal = $this->view('modals/folder');
} else {
$modal = null;
}
if (is_file($readme)) {
$markdown = kirbytext(f::read($readme));
} else {
$markdown = null;
}
return $this->view('layouts/main', ['title' => $this->title(), 'menu' => $this->menu(), 'content' => $this->view('views/dashboard', ['markdown' => $markdown]), 'modal' => $modal]);
}], ['pattern' => 'assets/(:any)', 'action' => function ($file) {
switch ($file) {
case 'index.js':
case 'index.min.js':
$mime = 'text/javascript';
break;
case 'index.css':
case 'index.min.css':
$mime = 'text/css';
break;
default:
return new Response('Not found', 'text/html', 404);
break;
}
// build the root for the file
$file = dirname(__DIR__) . DS . 'assets/dist/' . $file;
return new Response(f::read($file), $mime);
}], ['pattern' => '(:all)/preview', 'action' => function ($path) {
lab::$mode = 'preview';
$pattern = new Pattern($path);
$config = $pattern->config();
try {
$html = $pattern->render();
} catch (Exception $e) {
$html = '';
}
return $this->view('views/preview', ['pattern' => $pattern, 'html' => $html, 'background' => a::get($config, 'background', $this->kirby->option('patterns.preview.background')), 'css' => $this->kirby->option('patterns.preview.css', 'assets/css/index.css'), 'js' => $this->kirby->option('patterns.preview.js', 'assets/js/index.js')]);
}], ['pattern' => '(:all)', 'action' => function ($path) {
$pattern = new Pattern($path);
$file = null;
if (!$pattern->exists()) {
$filename = basename($path);
$path = dirname($path);
if ($path == '.') {
$preview = $this->view('previews/error', ['error' => 'The pattern could not be found']);
} else {
$pattern = new Pattern($path);
$file = $pattern->files()->get($filename);
if ($file) {
$preview = $this->preview($pattern, $file);
} else {
$preview = $this->view('previews/error', ['error' => 'The file could not be found']);
}
}
} else {
if ($file = $pattern->files()->get($pattern->name() . '.html.php')) {
go($pattern->url() . '/' . $file->filename());
} else {
if ($file = $pattern->files()->first()) {
go($pattern->url() . '/' . $file->filename());
} else {
$preview = $this->view('previews/empty');
}
}
}
if ($pattern->isHidden()) {
go($this->url());
}
return $this->view('layouts/main', ['title' => $this->title() . ' / ' . $pattern->title(), 'menu' => $this->menu(null, $path), 'content' => $this->view('views/pattern', ['preview' => $preview, 'info' => $this->view('snippets/info', ['pattern' => $pattern, 'file' => $file])])]);
}]]);
if ($route = $router->run($path ? $path : '/')) {
return new Response(call($route->action(), $route->arguments()));
} else {
go('error');
}
}