Skip to content

Commit

Permalink
Improve performance by prefixing all global functions calls with \ to
Browse files Browse the repository at this point in the history
skip the look up and resolve process and go straight to the global
function

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Feb 17, 2019
1 parent cc91d49 commit f2f1ef8
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/Console/ActivateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}'.");
}

Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Decorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 4 additions & 5 deletions src/FileViewFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Theme/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
12 changes: 6 additions & 6 deletions src/Theme/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Theme/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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}");
Expand Down Expand Up @@ -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}");
}

Expand Down Expand Up @@ -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()));
}
}

Expand All @@ -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);
Expand Down

0 comments on commit f2f1ef8

Please sign in to comment.