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
+20 -19
View File
@@ -11,7 +11,7 @@ namespace {
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; ?>
namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> {
<?php foreach($aliases as $alias): ?>
@@ -20,7 +20,6 @@ namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> {
<?php foreach($alias->getMethods() as $method): ?>
<?= trim($method->getDocComment(' ')) ?>
public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefault() ?>)
{<?php if($method->getDeclaringClass() !== $method->getRoot()): ?>
@@ -30,38 +29,40 @@ namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> {
<?= $method->shouldReturn() ? 'return ': '' ?><?= $method->getRoot() ?>::<?= $method->getName() ?>(<?= $method->getParams() ?>);
}
<?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): ?>
<?= $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): ?>
<?= trim($method->getDocComment(' ')) ?>
public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefault() ?>)
{<?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 endif; ?>}
<?php endforeach; ?>
<?php endforeach; ?>
}
<?php endforeach; ?>
<?php if($helpers): ?>
namespace {
<?= $helpers ?>
}
<?php endif; ?>
<?php if($include_fluent): ?>
namespace Illuminate\Support {
/**
+34 -13
View File
@@ -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) {
@@ -209,15 +210,35 @@ class Generator
$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()