From b611cb5eff09d656f392bba9ec1f66cde5a013fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Lef=C3=A8vre?= Date: Sun, 5 Mar 2017 20:04:42 +0100 Subject: [PATCH] Add namespaced aliases (#456) * Add namespaced aliases * Move aliases regouping logic in Generator * Fix getValidAliases return type --- resources/views/helper.php | 57 +++++++++++++++++++------------------- src/Generator.php | 49 ++++++++++++++++++++++---------- 2 files changed, 64 insertions(+), 42 deletions(-) diff --git a/resources/views/helper.php b/resources/views/helper.php index cf73204..89e69ad 100644 --- a/resources/views/helper.php +++ b/resources/views/helper.php @@ -11,16 +11,15 @@ namespace { exit("This file should not be included, only analyzed by your IDE"); } - $aliases): ?> + $aliases): ?> -namespace { +namespace { getClassType() ?> getExtendsClass() ?> { getMethods() as $method): ?> - getDocComment(' ')) ?> - + getDocComment(' ')) ?> public static function getName() ?>(getParamsWithDefault() ?>) {getDeclaringClass() !== $method->getRoot()): ?> @@ -29,39 +28,41 @@ namespace { shouldReturn() ? 'return ': '' ?>getRoot() ?>::getName() ?>(getParams() ?>); } - - - } - + + } + } - + - -namespace { - - $aliases): ?> + $aliases): ?> +namespace { - getClassType() ?> getShortName() ?> extends getExtends() ?> { - getMethods() as $method): ?> - getDocComment(' ')) ?> - - public static function getName() ?>(getParamsWithDefault() ?>) - {getDeclaringClass() !== $method->getRoot()): ?> - - //Method inherited from getDeclaringClass() ?> - - - shouldReturn() ? 'return ': '' ?>getRoot() ?>::getName() ?>(getParams() ?>); - } + getClassType() ?> getShortName() ?> extends getExtends() ?> {getExtendsNamespace() == '\Illuminate\Database\Eloquent'): ?> + getMethods() as $method): ?> + getDocComment(' ')) ?> + public static function getName() ?>(getParamsWithDefault() ?>) + {getDeclaringClass() !== $method->getRoot()): ?> + + //Method inherited from getDeclaringClass() ?> + + + shouldReturn() ? 'return ': '' ?>getRoot() ?>::getName() ?>(getParams() ?>); + } } - - - + } + + + +namespace { + +} + + namespace Illuminate\Support { /** diff --git a/src/Generator.php b/src/Generator.php index 2633f3d..eb50c95 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -12,7 +12,7 @@ namespace Barryvdh\LaravelIdeHelper; use Illuminate\Foundation\Application; use Illuminate\Foundation\AliasLoader; -use Illuminate\Config\Repository as ConfigRepository; +use Illuminate\Support\Collection; use ReflectionClass; use Symfony\Component\Console\Output\OutputInterface; @@ -81,7 +81,8 @@ class Generator { $app = app(); return $this->view->make('ide-helper::helper') - ->with('namespaces', $this->getNamespaces()) + ->with('namespaces_by_extends_ns', $this->getAliasesByExtendsNamespace()) + ->with('namespaces_by_alias_ns', $this->getAliasesByAliasNamespace()) ->with('helpers', $this->helpers) ->with('version', $app->version()) ->with('include_fluent', $this->config->get('ide-helper.include_fluent', false)) @@ -91,7 +92,7 @@ class Generator public function generateJsonHelper() { $classes = array(); - foreach ($this->getNamespaces() as $aliases) { + foreach ($this->getValidAliases() as $aliases) { foreach ($aliases as $alias) { $functions = array(); foreach ($alias->getMethods() as $method) { @@ -186,13 +187,13 @@ class Generator } /** - * Find all namespaces/aliases that are valid for us to render + * Find all aliases that are valid for us to render * - * @return array + * @return Collection */ - protected function getNamespaces() + protected function getValidAliases() { - $namespaces = array(); + $aliases = new Collection(); // Get all aliases foreach ($this->getAliases() as $name => $facade) { @@ -208,16 +209,36 @@ class Generator if (array_key_exists($name, $this->extra)) { $alias->addClass($this->extra[$name]); } - - $namespace = $alias->getExtendsNamespace() ?: $alias->getNamespace(); - if (!isset($namespaces[$namespace])) { - $namespaces[$namespace] = array(); - } - $namespaces[$namespace][] = $alias; + + $aliases[] = $alias; } } - return $namespaces; + return $aliases; + } + + /** + * Regroup aliases by namespace of extended classes + * + * @return Collection + */ + protected function getAliasesByExtendsNamespace() + { + return $this->getValidAliases()->groupBy(function (Alias $alias) { + return $alias->getExtendsNamespace(); + }); + } + + /** + * Regroup aliases by namespace of alias + * + * @return Collection + */ + protected function getAliasesByAliasNamespace() + { + return $this->getValidAliases()->groupBy(function (Alias $alias) { + return $alias->getNamespace(); + }); } protected function getAliases()