mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-22 03:53:08 +00:00
Extend instead of redefining functions
For methods that are not a facade (like Eloquent/Controller), avoids duplicate code and better autocomplete (also variables etc). No more need for extra config settings, only the magic calls still.
This commit is contained in:
@@ -39,7 +39,7 @@ class GeneratorCommand extends Command {
|
|||||||
protected $description = 'Generate a new IDE Helper file.';
|
protected $description = 'Generate a new IDE Helper file.';
|
||||||
|
|
||||||
protected $extra;
|
protected $extra;
|
||||||
protected $nonstatic;
|
protected $extra_nonstatic;
|
||||||
protected $onlyExtend;
|
protected $onlyExtend;
|
||||||
protected $helpers;
|
protected $helpers;
|
||||||
|
|
||||||
@@ -61,8 +61,7 @@ class GeneratorCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->extra = \Config::get('laravel-ide-helper::extra');
|
$this->extra = \Config::get('laravel-ide-helper::extra');
|
||||||
$this->nonstatic = \Config::get('laravel-ide-helper::nonstatic');
|
$this->extra_nonstatic = \Config::get('laravel-ide-helper::extra_nonstatic');
|
||||||
$this->onlyExtend = \Config::get('laravel-ide-helper::only_extend');
|
|
||||||
|
|
||||||
if( $this->option('helpers') || (\Config::get('laravel-ide-helper::include_helpers') )){
|
if( $this->option('helpers') || (\Config::get('laravel-ide-helper::include_helpers') )){
|
||||||
$this->helpers = \Config::get('laravel-ide-helper::helper_files');
|
$this->helpers = \Config::get('laravel-ide-helper::helper_files');
|
||||||
@@ -179,66 +178,70 @@ namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
|
|||||||
$output .= "namespace $namespace {\n";
|
$output .= "namespace $namespace {\n";
|
||||||
|
|
||||||
//Some classes extend the facade
|
//Some classes extend the facade
|
||||||
if($root !== $facade or in_array($alias, $this->onlyExtend)){
|
$output .= " class $alias extends $facade{\n";
|
||||||
//If the root class is not the same as the facade extend it.
|
|
||||||
$output .= " class $alias extends $facade{\n";
|
|
||||||
}else{
|
|
||||||
$output .= " class $alias{\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//If they extend, don't include them.
|
$usedMethods = array();
|
||||||
if(!in_array($alias, $this->onlyExtend))
|
|
||||||
|
//Get all public methods for this class
|
||||||
|
$methods = $reflection->getMethods();
|
||||||
|
if($methods)
|
||||||
{
|
{
|
||||||
$usedMethods = array();
|
foreach ($methods as $method)
|
||||||
|
|
||||||
//Check if there are non-static methods for this alias
|
|
||||||
if(array_key_exists($alias, $this->nonstatic) ){
|
|
||||||
$nonstaticMethods = $this->nonstatic[$alias];
|
|
||||||
}else{
|
|
||||||
$nonstaticMethods = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
//Get all public methods for this class
|
|
||||||
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
|
|
||||||
if($methods)
|
|
||||||
{
|
{
|
||||||
foreach ($methods as $method)
|
//Skip methods that are already used
|
||||||
{
|
if(!in_array($method->name, $usedMethods)){
|
||||||
//Skip methods that are already used
|
if($method->isPublic() && $root !== $facade){
|
||||||
if(!in_array($method->name, $usedMethods)){
|
$output .= $this->parseMethod($method, $alias);
|
||||||
$static = !in_array($method->name, $nonstaticMethods);
|
|
||||||
$declaringClass = $method->getDeclaringClass();
|
|
||||||
$output .= $this->parseMethod($method, $alias, $static);
|
|
||||||
$usedMethods[] = $method->name;
|
|
||||||
}
|
}
|
||||||
|
$usedMethods[] = $method->name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Add extra methods, from other classes (magic calls)
|
//Add extra methods, from other classes (magic static calls)
|
||||||
if(array_key_exists($alias, $this->extra)){
|
if(array_key_exists($alias, $this->extra)){
|
||||||
foreach($this->extra[$alias] as $extraClass){
|
foreach($this->extra[$alias] as $extraClass){
|
||||||
if(!class_exists($extraClass) && !interface_exists($extraClass)){
|
if(!class_exists($extraClass) && !interface_exists($extraClass)){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$reflection = new \ReflectionClass($extraClass);
|
$reflection = new \ReflectionClass($extraClass);
|
||||||
|
|
||||||
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
|
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
|
||||||
if($methods)
|
if($methods)
|
||||||
|
{
|
||||||
|
foreach ($methods as $method)
|
||||||
{
|
{
|
||||||
foreach ($methods as $method)
|
if(!in_array($method->name, $usedMethods)){
|
||||||
{
|
$output .= $this->parseMethod($method, $alias);
|
||||||
if(!in_array($method->name, $usedMethods)){
|
$usedMethods[] = $method->name;
|
||||||
$static = !in_array($method->name, $nonstaticMethods);
|
|
||||||
$output .= $this->parseMethod($method, $alias, $static);
|
|
||||||
$usedMethods[] = $method->name;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Add extra methods, from other classes (magic calls)
|
||||||
|
if(array_key_exists($alias, $this->extra_nonstatic)){
|
||||||
|
foreach($this->extra_nonstatic[$alias] as $extraClass){
|
||||||
|
if(!class_exists($extraClass) && !interface_exists($extraClass)){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$reflection = new \ReflectionClass($extraClass);
|
||||||
|
|
||||||
|
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
|
||||||
|
if($methods)
|
||||||
|
{
|
||||||
|
foreach ($methods as $method)
|
||||||
|
{
|
||||||
|
if(!in_array($method->name, $usedMethods)){
|
||||||
|
$output .= $this->parseMethod($method, $alias, false);
|
||||||
|
$usedMethods[] = $method->name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$output .= " }\n}\n\n";
|
$output .= " }\n}\n\n";
|
||||||
|
|
||||||
}catch(\Exception $e){
|
}catch(\Exception $e){
|
||||||
@@ -331,7 +334,7 @@ namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Write the output, using the DocBlock serializer
|
//Write the output, using the DocBlock serializer
|
||||||
$output .= $serializer->getDocComment($phpdoc) ."\n\t public ".($static ? 'static' : '')." function ".$method->name."(";
|
$output .= $serializer->getDocComment($phpdoc) ."\n\t public ".($static ? 'static ' : '')."function ".$method->name."(";
|
||||||
|
|
||||||
//Loop through the default values for paremeters, and make the correct output string
|
//Loop through the default values for paremeters, and make the correct output string
|
||||||
$params = array();
|
$params = array();
|
||||||
|
|||||||
+2
-39
@@ -13,18 +13,6 @@ return array(
|
|||||||
|
|
||||||
'filename' => '_ide_helper.php',
|
'filename' => '_ide_helper.php',
|
||||||
|
|
||||||
/*
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
| Sublime version
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
|
|
|
||||||
| Use a different code format, better for SublimeText (instead of Netbeans/phpStorm)
|
|
||||||
| Can also be used with the --sublime (-S) option.
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
'sublime' => false,
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Helper files to include
|
| Helper files to include
|
||||||
@@ -55,35 +43,10 @@ return array(
|
|||||||
'Auth' => array('Illuminate\Auth\Guard'),
|
'Auth' => array('Illuminate\Auth\Guard'),
|
||||||
'Cache' => array('Illuminate\Cache\StoreInterface', 'Illuminate\Cache\Repository'),
|
'Cache' => array('Illuminate\Cache\StoreInterface', 'Illuminate\Cache\Repository'),
|
||||||
'DB' => array('Illuminate\Database\Connection'),
|
'DB' => array('Illuminate\Database\Connection'),
|
||||||
'Eloquent' => array('Illuminate\Database\Eloquent\Builder', 'Illuminate\Database\Query\Builder'),
|
|
||||||
'Queue' => array('Illuminate\Queue\QueueInterface'),
|
'Queue' => array('Illuminate\Queue\QueueInterface'),
|
||||||
),
|
),
|
||||||
|
'extra_nonstatic' => array(
|
||||||
|
'Eloquent' => array('Illuminate\Database\Eloquent\Builder', 'Illuminate\Database\Query\Builder'),
|
||||||
/*
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
| Non-static methods
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
|
|
|
||||||
| These functions aren't actually facade calls, so don't make them static
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
'nonstatic' => array(
|
|
||||||
'Eloquent' => array('freshTimestamp', 'newCollection', 'toArray', 'toJson', 'toSql', 'delete'),
|
|
||||||
),
|
|
||||||
|
|
||||||
/*
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
| Only extend
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
|
|
|
||||||
| These implementations aren't called static, so only extend them.
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
'only_extend' => array(
|
|
||||||
'Seeder',
|
|
||||||
),
|
),
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user