Add namespaced aliases (#456)

* Add namespaced aliases

* Move aliases regouping logic in Generator

* Fix getValidAliases return type
This commit is contained in:
Florian Lefèvre
2017-03-05 20:04:42 +01:00
committed by Barry vd. Heuvel
parent e82de98cef
commit b611cb5eff
2 changed files with 64 additions and 42 deletions
+29 -28
View File
@@ -11,16 +11,15 @@ namespace {
exit("This file should not be included, only analyzed by your IDE"); exit("This file should not be included, only analyzed by your IDE");
} }
<?php foreach($namespaces as $namespace => $aliases): ?> <?php foreach($namespaces_by_extends_ns as $namespace => $aliases): ?>
<?php if ($namespace == '\Illuminate\Database\Eloquent'): continue; endif; ?> <?php if ($namespace == '\Illuminate\Database\Eloquent'): continue; endif; ?>
namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> { namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> {
<?php foreach($aliases as $alias): ?> <?php foreach($aliases as $alias): ?>
<?= $alias->getClassType() ?> <?= $alias->getExtendsClass() ?> { <?= $alias->getClassType() ?> <?= $alias->getExtendsClass() ?> {
<?php foreach($alias->getMethods() as $method): ?> <?php foreach($alias->getMethods() as $method): ?>
<?= trim($method->getDocComment(' ')) ?> <?= trim($method->getDocComment(' ')) ?>
public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefault() ?>) public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefault() ?>)
{<?php if($method->getDeclaringClass() !== $method->getRoot()): ?> {<?php if($method->getDeclaringClass() !== $method->getRoot()): ?>
@@ -29,39 +28,41 @@ namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> {
<?= $method->shouldReturn() ? 'return ': '' ?><?= $method->getRoot() ?>::<?= $method->getName() ?>(<?= $method->getParams() ?>); <?= $method->shouldReturn() ? 'return ': '' ?><?= $method->getRoot() ?>::<?= $method->getName() ?>(<?= $method->getParams() ?>);
} }
<?php endforeach; ?> <?php endforeach; ?>
}
} <?php endforeach; ?>
<?php endforeach; ?>
} }
<?php endforeach; ?> <?php endforeach; ?>
namespace {
<?= $helpers ?>
<?php foreach($namespaces as $namespace => $aliases): ?> <?php foreach($namespaces_by_alias_ns as $namespace => $aliases): ?>
namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> {
<?php foreach($aliases as $alias): ?> <?php foreach($aliases as $alias): ?>
<?= $alias->getClassType() ?> <?= $alias->getShortName() ?> extends <?= $alias->getExtends() ?> {<?php if ($namespace == '\Illuminate\Database\Eloquent'): ?>
<?php foreach($alias->getMethods() as $method): ?>
<?= trim($method->getDocComment(' ')) ?> <?= $alias->getClassType() ?> <?= $alias->getShortName() ?> extends <?= $alias->getExtends() ?> {<?php if ($alias->getExtendsNamespace() == '\Illuminate\Database\Eloquent'): ?>
<?php foreach($alias->getMethods() as $method): ?>
public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefault() ?>) <?= trim($method->getDocComment(' ')) ?>
{<?php if($method->getDeclaringClass() !== $method->getRoot()): ?> public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefault() ?>)
{<?php if($method->getDeclaringClass() !== $method->getRoot()): ?>
//Method inherited from <?= $method->getDeclaringClass() ?>
<?php endif; ?> //Method inherited from <?= $method->getDeclaringClass() ?>
<?php endif; ?>
<?= $method->shouldReturn() ? 'return ': '' ?><?= $method->getRoot() ?>::<?= $method->getName() ?>(<?= $method->getParams() ?>);
} <?= $method->shouldReturn() ? 'return ': '' ?><?= $method->getRoot() ?>::<?= $method->getName() ?>(<?= $method->getParams() ?>);
}
<?php endforeach; ?> <?php endforeach; ?>
<?php endif; ?>} <?php endif; ?>}
<?php endforeach; ?>
<?php endforeach; ?>
<?php endforeach; ?>
} }
<?php endforeach; ?>
<?php if($helpers): ?>
namespace {
<?= $helpers ?>
}
<?php endif; ?>
<?php if($include_fluent): ?> <?php if($include_fluent): ?>
namespace Illuminate\Support { namespace Illuminate\Support {
/** /**
+35 -14
View File
@@ -12,7 +12,7 @@ namespace Barryvdh\LaravelIdeHelper;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Illuminate\Foundation\AliasLoader; use Illuminate\Foundation\AliasLoader;
use Illuminate\Config\Repository as ConfigRepository; use Illuminate\Support\Collection;
use ReflectionClass; use ReflectionClass;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@@ -81,7 +81,8 @@ class Generator
{ {
$app = app(); $app = app();
return $this->view->make('ide-helper::helper') 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('helpers', $this->helpers)
->with('version', $app->version()) ->with('version', $app->version())
->with('include_fluent', $this->config->get('ide-helper.include_fluent', false)) ->with('include_fluent', $this->config->get('ide-helper.include_fluent', false))
@@ -91,7 +92,7 @@ class Generator
public function generateJsonHelper() public function generateJsonHelper()
{ {
$classes = array(); $classes = array();
foreach ($this->getNamespaces() as $aliases) { foreach ($this->getValidAliases() as $aliases) {
foreach ($aliases as $alias) { foreach ($aliases as $alias) {
$functions = array(); $functions = array();
foreach ($alias->getMethods() as $method) { 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 // Get all aliases
foreach ($this->getAliases() as $name => $facade) { foreach ($this->getAliases() as $name => $facade) {
@@ -208,16 +209,36 @@ class Generator
if (array_key_exists($name, $this->extra)) { if (array_key_exists($name, $this->extra)) {
$alias->addClass($this->extra[$name]); $alias->addClass($this->extra[$name]);
} }
$namespace = $alias->getExtendsNamespace() ?: $alias->getNamespace(); $aliases[] = $alias;
if (!isset($namespaces[$namespace])) {
$namespaces[$namespace] = array();
}
$namespaces[$namespace][] = $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() protected function getAliases()