Upgrade to 3.0.3

This commit is contained in:
Bastian Allgeier
2019-03-05 10:55:03 +01:00
parent 8e3d86a590
commit 418db4b09b
40 changed files with 704 additions and 144 deletions

View File

@@ -107,6 +107,9 @@ class App
'users'
]);
// set the singleton
Model::$kirby = static::$instance = $this;
// setup the I18n class with the translation loader
$this->i18n();
@@ -120,9 +123,6 @@ class App
// handle those damn errors
$this->handleErrors();
// set the singleton
Model::$kirby = static::$instance = $this;
// bake config
Config::$data = $this->options;
}
@@ -226,15 +226,17 @@ class App
*/
public function call(string $path = null, string $method = null)
{
$path = $path ?? $this->path();
$method = $method ?? $this->request()->method();
$route = $this->router()->find($path, $method);
$router = $this->router();
$this->trigger('route:before', $route, $path, $method);
$result = $route->action()->call($route, ...$route->arguments());
$this->trigger('route:after', $route, $path, $method, $result);
$router::$beforeEach = function ($route, $path, $method) {
$this->trigger('route:before', $route, $path, $method);
};
return $result;
$router::$afterEach = function ($route, $path, $method, $result) {
$this->trigger('route:after', $route, $path, $method, $result);
};
return $router->call($path ?? $this->path(), $method ?? $this->request()->method());
}
/**
@@ -807,33 +809,32 @@ class App
// the site is needed a couple times here
$site = $this->site();
// use the home page
if ($path === null) {
return $site->homePage();
}
if ($page = $site->find($path)) {
return $page;
}
// search for the page by path
$page = $site->find($path);
if ($draft = $site->draft($path)) {
// search for a draft if the page cannot be found
if (!$page && $draft = $site->draft($path)) {
if ($this->user() || $draft->isVerified(get('token'))) {
return $draft;
$page = $draft;
}
}
// try to resolve content representations if the path has an extension
$extension = F::extension($path);
// remove the extension from the path
$path = Str::rtrim($path, '.' . $extension);
// stop when there's no extension
// no content representation? then return the page
if (empty($extension) === true) {
return null;
return $page;
}
// try to find the page for the representation
if ($page = $site->find($path)) {
// only try to return a representation
// when the page has been found
if ($page) {
try {
return $this
->response()
@@ -845,7 +846,7 @@ class App
}
$id = dirname($path);
$filename = basename($path) . '.' . $extension;
$filename = basename($path);
// try to resolve image urls for pages and drafts
if ($page = $site->findPageOrDraft($id)) {

View File

@@ -58,7 +58,8 @@ trait AppTranslations
*/
public function setCurrentLanguage(string $languageCode = null)
{
if ($languageCode === null) {
if ($this->multilang() === false) {
$this->setLocale($this->option('locale', 'en_US.utf-8'));
return $this->language = null;
}
@@ -69,7 +70,7 @@ trait AppTranslations
}
if ($this->language) {
setlocale(LC_ALL, $this->language->locale());
$this->setLocale($this->language->locale());
}
return $this->language;
@@ -86,6 +87,22 @@ trait AppTranslations
I18n::$locale = $translationCode ?? 'en';
}
/**
* Set locale settings
*
* @param string|array $locale
*/
public function setLocale($locale)
{
if (is_array($locale) === true) {
foreach ($locale as $key => $value) {
setlocale($key, $value);
}
} else {
setlocale(LC_ALL, $locale);
}
}
/**
* Load a specific translation by locale
*

View File

@@ -222,23 +222,27 @@ class File extends ModelWithContent
* used in the panel, when the file
* gets dragged onto a textarea
*
* @param string $type
* @param bool $absolute
* @return string
*/
public function dragText($type = 'kirbytext'): string
public function dragText($type = 'kirbytext', bool $absolute = false): string
{
$url = $absolute ? $this->id() : $this->filename();
switch ($type) {
case 'kirbytext':
if ($this->type() === 'image') {
return '(image: ' . $this->filename() . ')';
return '(image: ' . $url . ')';
} else {
return '(file: ' . $this->filename() . ')';
return '(file: ' . $url . ')';
}
// no break
case 'markdown':
if ($this->type() === 'image') {
return '![' . $this->alt() . '](./' . $this->filename() . ')';
return '![' . $this->alt() . '](' . $url . ')';
} else {
return '[' . $this->filename() . '](./' . $this->filename() . ')';
return '[' . $this->filename() . '](' . $url . ')';
}
}
}

View File

@@ -99,12 +99,19 @@ trait FileModifications
* could potentially also be a CDN or any other
* place.
*
* @param array|null $options
* @param array|null|string $options
* @return FileVersion|File
*/
public function thumb(array $options = null)
public function thumb($options = null)
{
// thumb presets
if (empty($options) === true) {
$options = $this->kirby()->option('thumbs.presets.default');
} elseif (is_string($options) === true) {
$options = $this->kirby()->option('thumbs.presets.' . $options);
}
if (empty($options) === true || is_array($options) === false) {
return $this;
}

View File

@@ -510,6 +510,10 @@ class Page extends ModelWithContent
public function is($page): bool
{
if (is_a($page, Page::class) === false) {
if (is_string($page) === false) {
return false;
}
$page = $this->kirby()->page($page);
}
@@ -757,7 +761,7 @@ class Page extends ModelWithContent
*/
public function isUnlisted(): bool
{
return $this->num() === null;
return $this->isListed() === false;
}
/**

View File

@@ -2,6 +2,8 @@
namespace Kirby\Cms;
use Kirby\Toolkit\F;
/**
* The Pages collection contains
* any number and mixture of page objects
@@ -199,18 +201,33 @@ class Pages extends Collection
*/
public function findById($id)
{
$id = trim($id, '/');
$page = $this->get($id);
// remove trailing or leading slashes
$id = trim($id, '/');
// strip extensions from the id
if (strpos($id, '.') !== false) {
$info = pathinfo($id);
if ($info['dirname'] !== '.') {
$id = $info['dirname'] . '/' . $info['filename'];
} else {
$id = $info['filename'];
}
}
// try the obvious way
if ($page = $this->get($id)) {
return $page;
}
$multiLang = App::instance()->multilang();
if ($multiLang === true) {
$page = $this->findBy('slug', $id);
if ($multiLang === true && $page = $this->findBy('slug', $id)) {
return $page;
}
if (!$page) {
$start = is_a($this->parent, 'Kirby\Cms\Page') === true ? $this->parent->id() : '';
$page = $this->findByIdRecursive($id, $start, $multiLang);
}
$start = is_a($this->parent, 'Kirby\Cms\Page') === true ? $this->parent->id() : '';
$page = $this->findByIdRecursive($id, $start, $multiLang);
return $page;
}

View File

@@ -4,6 +4,7 @@ namespace Kirby\Cms;
use Exception;
use Kirby\Http\Response;
use Kirby\Http\Uri;
use Kirby\Toolkit\Dir;
use Kirby\Toolkit\F;
use Kirby\Toolkit\View;
@@ -62,12 +63,15 @@ class Panel
try {
if (static::link($kirby) === true) {
usleep(1);
go($kirby->request()->url());
go($kirby->url('index') . '/' . $kirby->path());
}
} catch (Throwable $e) {
die('The panel assets cannot be installed properly. Please check permissions of your media folder.');
}
// get the uri object for the panel url
$uri = new Uri($url = $kirby->url('panel'));
$view = new View($kirby->root('kirby') . '/views/panel.php', [
'kirby' => $kirby,
'config' => $kirby->option('panel'),
@@ -75,7 +79,7 @@ class Panel
'pluginCss' => $kirby->url('media') . '/plugins/index.css',
'pluginJs' => $kirby->url('media') . '/plugins/index.js',
'icons' => F::read($kirby->root('panel') . '/dist/img/icons.svg'),
'panelUrl' => $url = $kirby->url('panel'),
'panelUrl' => $uri->path()->toString(true) . '/',
'options' => [
'url' => $url,
'site' => $kirby->url('index'),

View File

@@ -13,7 +13,7 @@ class R extends Facade
/**
* @return Request
*/
protected static function instance()
public static function instance()
{
return App::instance()->request();
}

View File

@@ -6,6 +6,7 @@ use Exception;
use Kirby\Data\Data;
use Kirby\Exception\NotFoundException;
use Kirby\Toolkit\F;
use Kirby\Toolkit\I18n;
/**
* Represents a User role with attached
@@ -92,10 +93,8 @@ class Role extends Model
public static function load(string $file, array $inject = []): self
{
$name = F::name($file);
$data = Data::read($file);
$data['name'] = $name;
$data['name'] = F::name($file);
return static::factory($data, $inject);
}
@@ -119,9 +118,9 @@ class Role extends Model
return $this->permissions;
}
protected function setDescription(string $description = null): self
protected function setDescription($description = null): self
{
$this->description = $description;
$this->description = I18n::translate($description, $description);
return $this;
}
@@ -139,7 +138,7 @@ class Role extends Model
protected function setTitle($title = null): self
{
$this->title = $title;
$this->title = I18n::translate($title, $title);
return $this;
}

View File

@@ -13,7 +13,7 @@ class S extends Facade
/**
* @return Session
*/
protected static function instance()
public static function instance()
{
return App::instance()->session();
}

View File

@@ -324,6 +324,17 @@ class Site extends ModelWithContent
);
}
/**
* Compares the current object with the given site object
*
* @param Site $site
* @return bool
*/
public function is(Site $site): bool
{
return $this === $site;
}
/**
* Returns the root to the media folder for the site
*

View File

@@ -334,7 +334,7 @@ class System
*/
public function php(): bool
{
return version_compare(phpversion(), '7.1.0', '>');
return version_compare(phpversion(), '7.1.0', '>=');
}
/**

View File

@@ -10,6 +10,10 @@ class UserBlueprint extends Blueprint
{
public function __construct(array $props)
{
// normalize and translate the description
$props['description'] = $this->i18n($props['description'] ?? null);
// register the other props
parent::__construct($props);
// normalize all available page options

View File

@@ -12,7 +12,7 @@ class Visitor extends Facade
/**
* @return \Kirby\Http\Visitor
*/
protected static function instance()
public static function instance()
{
return App::instance()->visitor();
}

View File

@@ -29,7 +29,7 @@ class Yaml extends Handler
$locale = setlocale(LC_NUMERIC, 0);
// change to english numerics to avoid issues with floats
setlocale(LC_NUMERIC, 'en_US');
setlocale(LC_NUMERIC, 'C');
// $data, $indent, $wordwrap, $no_opening_dashes
$yaml = Spyc::YAMLDump($data, false, false, true);

View File

@@ -0,0 +1,7 @@
<?php
namespace Kirby\Http\Exceptions;
class NextRouteException extends \Exception
{
}

View File

@@ -152,6 +152,17 @@ class Route
return $this->attributes['name'] ?? null;
}
/**
* Throws a specific exception to tell
* the router to jump to the next route
*
* @return void
*/
public function next()
{
throw new Exceptions\NextRouteException('next');
}
/**
* Getter for the pattern
*

View File

@@ -14,6 +14,8 @@ use InvalidArgumentException;
*/
class Router
{
public static $beforeEach;
public static $afterEach;
/**
* Store for the current route,
@@ -84,10 +86,30 @@ class Router
*/
public function call(string $path = '', string $method = 'GET')
{
return $this
->find($path, $method)
->action()
->call($this->route, ...$this->route->arguments());
$ignore = [];
$result = null;
$loop = true;
while ($loop === true) {
$route = $this->find($path, $method, $ignore);
if (is_a(static::$beforeEach, 'Closure') === true) {
(static::$beforeEach)($route, $path, $method);
}
try {
$result = $route->action()->call($route, ...$route->arguments());
$loop = false;
} catch (Exceptions\NextRouteException $e) {
$ignore[] = $route;
}
if (is_a(static::$afterEach, 'Closure') === true) {
(static::$afterEach)($route, $path, $method, $result);
}
}
return $result;
}
/**
@@ -98,9 +120,10 @@ class Router
*
* @param string $path
* @param string $method
* @param array $ignore
* @return Route|null
*/
public function find(string $path, string $method)
public function find(string $path, string $method, array $ignore = null)
{
if (isset($this->routes[$method]) === false) {
throw new InvalidArgumentException('Invalid routing method: ' . $method, 400);
@@ -113,7 +136,9 @@ class Router
$arguments = $route->parse($route->pattern(), $path);
if ($arguments !== false) {
return $this->route = $route;
if (empty($ignore) === true || in_array($route, $ignore) === false) {
return $this->route = $route;
}
}
}

View File

@@ -15,7 +15,7 @@ abstract class Facade
*
* @return mixed
*/
abstract protected static function instance();
abstract public static function instance();
/**
* Proxy for all public instance calls

View File

@@ -336,8 +336,14 @@ class Html
*/
public static function rel(string $rel = null, string $target = null)
{
$rel = trim($rel);
if ($target === '_blank') {
return trim($rel . ' noopener noreferrer');
if (empty($rel) === false) {
return $rel;
}
return trim($rel . ' noopener noreferrer', ' ');
}
return $rel;

View File

@@ -346,8 +346,8 @@ class Str
public static function float($value): string
{
$value = str_replace(',', '.', $value);
$decimal = strlen(substr(strrchr($value, "."), 1));
return number_format((float)$value, $decimal);
$decimal = strlen(substr(strrchr($value, '.'), 1));
return number_format((float)$value, $decimal, '.', false);
}
/**