Skip to content

Commit

Permalink
PHP 7.4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Oct 28, 2020
1 parent 5664e1a commit dc67783
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 82 deletions.
2 changes: 1 addition & 1 deletion common.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ services:
assetsLoader:
routing:
'Front:Test:detail':
- test.js
- test.js
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
}
],
"require": {
"php": ">=7.1.0",
"php": ">=7.4.0",
"nette/di": "^3.0",
"nette/http": "^3.0",
"nette/application": "^3.0"
},
"require-dev": {
"phpstan/phpstan": "^0.12.18",
"tracy/tracy": "^2.7",
"phpstan/phpstan-nette": "^0.12.6"
"phpstan/phpstan-nette": "^0.12.6",
"symplify/easy-coding-standard": "^7.2"
},
"autoload": {
"classmap": [
Expand Down
31 changes: 6 additions & 25 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,41 @@

final class Api
{

/** @var string[] */
private static $formatHeaders = [
private static array $formatHeaders = [
'js' => 'application/json',
'css' => 'text/css',
];

/** @var string[] */
private static $formatHtmlInjects = [
private static array $formatHtmlInjects = [
'js' => '<script src="%path%"></script>',
'css' => '<link href="%path%" rel="stylesheet">',
];

/** @var string */
private $wwwDir;
private string $wwwDir;

/** @var mixed[]|null */
private $data;
private ?array $data;


/**
* @param string $wwwDir
*/
public function __construct(string $wwwDir)
{
$this->wwwDir = $wwwDir;
}


/**
* @param string $route
* @return bool
*/
public function isAssetsAvailable(string $route): bool
{
return $this->findData(trim($route, ':')) !== [];
}


/**
* @param string $route
* @return string
*/
public function getHtmlInit(string $route): string
{
$return = [];
$routePath = Helpers::formatRouteToPath($route = trim($route, ':'));

$return = [];
foreach (array_keys($data = $this->findData($route)) as $format) {
foreach ($data[$format] ?? [] as $item) {
if (preg_match('/^((?:https?\:)?\/\/)(.+)$/', $item, $itemParser)) {
Expand All @@ -70,9 +57,6 @@ public function getHtmlInit(string $route): string
}


/**
* @param string $path
*/
public function run(string $path): void
{
if (preg_match('/^assets\/web-loader\/(.+)$/', $path, $parser)
Expand Down Expand Up @@ -106,8 +90,8 @@ public function run(string $path): void


/**
* @internal used by DIC.
* @param mixed[] $data
* @internal used by DIC.
*/
public function setData(array $data): void
{
Expand All @@ -116,18 +100,15 @@ public function setData(array $data): void


/**
* @param string|null $route
* @return string[]|string[][]
*/
private function findData(?string $route = null): array
{
if ($this->data === null) {
AssetLoaderException::dataIsEmpty();
}

if ($route !== null) {
$selectors = [];

if (preg_match('/^([^:]+):([^:]+):([^:]+)$/', trim($route, ':'), $routeParser)) {
$selectors[] = '*';
$selectors[] = $routeParser[1] . ':*';
Expand Down
9 changes: 1 addition & 8 deletions src/AssetLoaderException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@

final class AssetLoaderException extends \RuntimeException
{

/**
* @param string $name
*/
public static function invalidFileName(string $name): void
{
throw new self('Invalid asset filename "' . $name . '". Did you mean "' . $name . '.js"?');
Expand All @@ -23,9 +19,6 @@ public static function dataIsEmpty(): void
}


/**
* @param string $route
*/
public static function routeIsInInvalidFormat(string $route): void
{
throw new self(
Expand All @@ -34,4 +27,4 @@ public static function routeIsInInvalidFormat(string $route): void
. 'with dynamic part in format "Module:*" or "Module:Presenter:*".'
);
}
}
}
41 changes: 2 additions & 39 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
final class Helpers
{

/**
* @throws \Error
*/
/** @throws \Error */
public function __construct()
{
throw new \Error('Class ' . get_class($this) . ' is static and cannot be instantiated.');
Expand All @@ -22,9 +20,6 @@ public function __construct()
/**
* Return current API path by current HTTP URL.
* In case of CLI return empty string.
*
* @param Request $httpRequest
* @return string
*/
public static function processPath(Request $httpRequest): string
{
Expand All @@ -35,8 +30,6 @@ public static function processPath(Request $httpRequest): string
/**
* Return current absolute URL.
* Return null, if current URL does not exist (for example in CLI mode).
*
* @return string|null
*/
public static function getCurrentUrl(): ?string
{
Expand All @@ -49,22 +42,12 @@ public static function getCurrentUrl(): ?string
}


/**
* @param string $module
* @param string $presenter
* @param string $action
* @return string
*/
public static function formatRoute(string $module, string $presenter = 'Homepage', string $action = 'default'): string
{
return self::firstUpper($module) . ':' . self::firstUpper($presenter) . ':' . $action;
}


/**
* @param string $route
* @return string|null
*/
public static function formatRouteToPath(string $route): ?string
{
if (preg_match('/^(?<module>[^:]+):(?<presenter>[^:]+):(?<action>[^:]+)$/', $route, $parser)) {
Expand All @@ -75,12 +58,6 @@ public static function formatRouteToPath(string $route): ?string
}


/**
* Converts first character to lower case.
*
* @param string $s
* @return string
*/
public static function firstUpper(string $s): string
{
return strtoupper($s[0] ?? '') . (function_exists('mb_substr')
Expand All @@ -90,12 +67,6 @@ public static function firstUpper(string $s): string
}


/**
* Converts first character to lower case.
*
* @param string $s
* @return string
*/
public static function firstLower(string $s): string
{
return strtolower($s[0] ?? '') . (function_exists('mb_substr')
Expand All @@ -108,23 +79,16 @@ public static function firstLower(string $s): string
/**
* Returns number of characters (not bytes) in UTF-8 string.
* That is the number of Unicode code points which may differ from the number of graphemes.
*
* @param string $s
* @return int
*/
public static function length(string $s): int
{
return function_exists('mb_strlen') ? mb_strlen($s, 'UTF-8') : strlen(utf8_decode($s));
}


/**
* @return string|null
*/
public static function getBaseUrl(): ?string
{
static $return;

if ($return === null) {
if (($currentUrl = self::getCurrentUrl()) !== null) {
if (preg_match('/^(https?:\/\/.+)\/www\//', $currentUrl, $localUrlParser)) {
Expand All @@ -133,12 +97,11 @@ public static function getBaseUrl(): ?string
$return = $publicUrlParser[1];
}
}

if ($return !== null) {
$return = rtrim($return, '/');
}
}

return $return;
}
}
}
7 changes: 0 additions & 7 deletions src/LoaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public function beforeCompile(): void
}


/**
* @param ClassType $class
*/
public function afterCompile(ClassType $class): void
{
$class->getMethod('initialize')->addBody(
Expand All @@ -57,15 +54,11 @@ public function afterCompile(ClassType $class): void
}


/**
* @param string $route
*/
private function validateRouteFormat(string $route): void
{
if ($route === '*') { // special case for global assets
return;
}

if (preg_match('/^[A-Z0-9][A-Za-z0-9]*:(?:\*|[A-Z0-9][A-Za-z0-9]*:(?:\*|[a-z0-9][A-Za-z0-9]*))$/', trim($route, ':')) === 0) {
AssetLoaderException::routeIsInInvalidFormat($route);
}
Expand Down

0 comments on commit dc67783

Please sign in to comment.