From f2f1ef847bfb69fab759445c542163697ceb7be6 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sun, 17 Feb 2019 16:13:29 +0800 Subject: [PATCH] Improve performance by prefixing all global functions calls with \ to skip the look up and resolve process and go straight to the global function Signed-off-by: Mior Muhammad Zaki --- src/Console/ActivateCommand.php | 6 +++--- src/Decorator.php | 2 +- src/FileViewFinder.php | 9 ++++----- src/Theme/Finder.php | 10 +++++----- src/Theme/Manifest.php | 12 ++++++------ src/Theme/Theme.php | 12 ++++++------ 6 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/Console/ActivateCommand.php b/src/Console/ActivateCommand.php index dd44c4b..b7480fb 100644 --- a/src/Console/ActivateCommand.php +++ b/src/Console/ActivateCommand.php @@ -91,11 +91,11 @@ public function handle() */ protected function validateProvidedTheme(string $group, $id, $theme): bool { - if (! in_array($group, $this->type)) { + if (! \in_array($group, $this->type)) { throw new InvalidArgumentException("Invalid theme name [{$group}], should either be 'frontend' or 'backend'."); } - if (is_null($theme)) { + if (\is_null($theme)) { throw new InvalidArgumentException("Invalid Theme ID [{$id}], or is not available for '{$group}'."); } @@ -116,7 +116,7 @@ protected function getAvailableTheme(string $type): Collection return $themes->filter(function (Manifest $manifest) use ($type) { $group = $manifest->get('type'); - if (! empty($group) && ! in_array($type, $group)) { + if (! empty($group) && ! \in_array($type, $group)) { return; } diff --git a/src/Decorator.php b/src/Decorator.php index e359acc..7cb604f 100644 --- a/src/Decorator.php +++ b/src/Decorator.php @@ -55,7 +55,7 @@ public function render(string $name, ...$parameters) */ public function __call(string $method, array $parameters) { - array_unshift($parameters, $method); + \array_unshift($parameters, $method); return $this->render($method, ...$parameters); } diff --git a/src/FileViewFinder.php b/src/FileViewFinder.php index 8800aef..fb25369 100644 --- a/src/FileViewFinder.php +++ b/src/FileViewFinder.php @@ -17,13 +17,12 @@ protected function findNamespacedView($name) // Prepend global view paths to namespace hints path. This would // allow theme to take priority if such view exist. - $generatePath = function ($path) use ($namespace) { - return "{$path}/packages/{$namespace}"; - }; - $paths = array_map($generatePath, $this->paths); + $paths = \array_map(function ($path) use ($namespace) { + return "{$path}/packages/{$namespace}"; + }, $this->paths); - return $this->findInPaths($view, array_merge($paths, $this->hints[$namespace])); + return $this->findInPaths($view, \array_merge($paths, $this->hints[$namespace])); } /** diff --git a/src/Theme/Finder.php b/src/Theme/Finder.php index 6bce53c..937ab29 100644 --- a/src/Theme/Finder.php +++ b/src/Theme/Finder.php @@ -36,13 +36,13 @@ public function detect(): Collection { $themes = new Collection(); $file = $this->app->make('files'); - $path = rtrim($this->app['path.public'], '/').'/themes/'; + $path = \rtrim($this->app['path.public'], '/').'/themes/'; $folders = $file->directories($path); foreach ($folders as $folder) { $name = $this->parseThemeNameFromPath($folder); - $themes[$name] = new Manifest($file, rtrim($folder, '/').'/'); + $themes[$name] = new Manifest($file, \rtrim($folder, '/').'/'); } return $themes; @@ -57,9 +57,9 @@ public function detect(): Collection */ protected function parseThemeNameFromPath(string $path): string { - $path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path); - $path = explode(DIRECTORY_SEPARATOR, $path); + $path = \str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path); + $path = \explode(DIRECTORY_SEPARATOR, $path); - return array_pop($path); + return \array_pop($path); } } diff --git a/src/Theme/Manifest.php b/src/Theme/Manifest.php index 929a1b7..1d31bb8 100644 --- a/src/Theme/Manifest.php +++ b/src/Theme/Manifest.php @@ -46,13 +46,13 @@ class Manifest */ public function __construct(Filesystem $files, string $path) { - $path = rtrim($path, '/'); + $path = \rtrim($path, '/'); $this->files = $files; if ($files->exists($manifest = "{$path}/theme.json")) { - $jsonable = json_decode($files->get($manifest), true); + $jsonable = \json_decode($files->get($manifest), true); - if (is_null($jsonable)) { + if (\is_null($jsonable)) { // json_decode couldn't parse, throw an exception. throw new RuntimeException( "Theme [{$path}]: cannot decode theme.json file" @@ -120,10 +120,10 @@ protected function generateManifestConfig(array $jsonable): array */ protected function parseThemeNameFromPath(string $path): string { - $path = str_replace('\\', DIRECTORY_SEPARATOR, $path); - $path = explode(DIRECTORY_SEPARATOR, $path); + $path = \str_replace('\\', DIRECTORY_SEPARATOR, $path); + $path = \explode(DIRECTORY_SEPARATOR, $path); - return array_pop($path); + return \array_pop($path); } /** diff --git a/src/Theme/Theme.php b/src/Theme/Theme.php index b1ffb70..d1482d6 100644 --- a/src/Theme/Theme.php +++ b/src/Theme/Theme.php @@ -103,8 +103,8 @@ public function initiate() $baseUrl = $this->app->make('request')->root(); // Register relative and absolute URL for theme usage. - $this->absoluteUrl = rtrim($baseUrl, '/').'/themes'; - $this->relativeUrl = trim(str_replace($baseUrl, '/', $this->absoluteUrl), '/'); + $this->absoluteUrl = \rtrim($baseUrl, '/').'/themes'; + $this->relativeUrl = \trim(\str_replace($baseUrl, '/', $this->absoluteUrl), '/'); return $this; } @@ -118,7 +118,7 @@ public function initiate() */ public function setTheme(?string $theme): void { - if (! is_null($this->theme)) { + if (! \is_null($this->theme)) { $this->resolved && $this->resetViewPaths(); $this->dispatcher->dispatch("orchestra.theme.unset: {$this->theme}"); @@ -160,7 +160,7 @@ public function boot(): bool $autoload = $this->getThemeAutoloadFiles($themePath); foreach ($autoload as $file) { - $file = ltrim($file, '/'); + $file = \ltrim($file, '/'); $this->files->requireOnce("{$themePath}/{$file}"); } @@ -289,7 +289,7 @@ protected function setViewPaths(): void $themePaths = $this->getAvailableThemePaths(); if (! empty($themePaths)) { - $viewFinder->setPaths(array_merge($themePaths, $viewFinder->getPaths())); + $viewFinder->setPaths(\array_merge($themePaths, $viewFinder->getPaths())); } } @@ -305,7 +305,7 @@ protected function resetViewPaths(): void $paths = $viewFinder->getPaths(); foreach ($this->getThemePaths() as $themePath) { - ($paths[0] === $themePath) && array_shift($paths); + ($paths[0] === $themePath) && \array_shift($paths); } $viewFinder->setPaths($paths);