mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57:13 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4790413752 | ||
|
|
4f31e98467 | ||
|
|
17ae13ce56 | ||
|
|
fedae6542b | ||
|
|
e8b1c367d0 | ||
|
|
66feb564e1 | ||
|
|
15bf867658 |
@@ -1,4 +1,5 @@
|
|||||||
## Laravel IDE Helper Generator
|
## Laravel IDE Helper Generator
|
||||||
|
[](https://packagist.org/packages/barryvdh/laravel-ide-helper) [](https://packagist.org/packages/barryvdh/laravel-ide-helper)
|
||||||
|
|
||||||
### Complete phpDocs, directly from the source
|
### Complete phpDocs, directly from the source
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ 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 $onlyExtend;
|
protected $onlyExtend;
|
||||||
protected $helpers;
|
protected $helpers;
|
||||||
|
|
||||||
@@ -61,9 +60,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->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');
|
||||||
}else{
|
}else{
|
||||||
@@ -133,113 +130,40 @@ class GeneratorCommand extends Command {
|
|||||||
*
|
*
|
||||||
* @author Barry vd. Heuvel <[email protected]>
|
* @author Barry vd. Heuvel <[email protected]>
|
||||||
*/
|
*/
|
||||||
namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
|
exit('Only to be used as an helper for your IDE');\n\n";
|
||||||
|
|
||||||
//Get all aliases
|
//Get all aliases
|
||||||
$aliases = $aliasLoader->getAliases();
|
$aliases = $aliasLoader->getAliases();
|
||||||
|
|
||||||
foreach($aliases as $alias => $facade){
|
foreach($aliases as $alias => $facade){
|
||||||
|
|
||||||
try{
|
$root = $this->getRoot($facade);
|
||||||
//If possible, get the facade root
|
if(!$root){
|
||||||
if(method_exists($facade, 'getFacadeRoot')){
|
|
||||||
$root = get_class($facade::getFacadeRoot());
|
|
||||||
}else{
|
|
||||||
$root = $facade;
|
|
||||||
}
|
|
||||||
|
|
||||||
//If it doesn't exist, skip it
|
|
||||||
if(!class_exists($root) && !interface_exists($root)){
|
|
||||||
$this->error("Class $root is not found.");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
//When the database connection is not set, some classes will be skipped
|
|
||||||
}catch(\PDOException $e){
|
|
||||||
$this->error("PDOException: ".$e->getMessage()."\nPlease configure your database connection correctly, or use the sqlite memory driver (-M). Skipping $facade.");
|
|
||||||
continue;
|
|
||||||
}catch(\Exception $e){
|
|
||||||
$this->error("Exception: ".$e->getMessage()."\nSkipping $facade.");
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Get the namespace from the alias.
|
|
||||||
if(strpos($alias, '\\') !== false){
|
|
||||||
$parts = explode('\\', $alias);
|
|
||||||
$alias = array_pop($parts);
|
|
||||||
$namespace = implode($parts, '\\');
|
|
||||||
}else{
|
|
||||||
$namespace = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
try{
|
try{
|
||||||
//Reflect on the class
|
|
||||||
$reflection = new \ReflectionClass($root);
|
|
||||||
|
|
||||||
$output .= "namespace $namespace {\n";
|
|
||||||
|
|
||||||
//Some classes extend the facade
|
//Some classes extend the facade
|
||||||
if($root !== $facade or in_array($alias, $this->onlyExtend)){
|
if(class_exists($facade)){
|
||||||
//If the root class is not the same as the facade extend it.
|
$output .= "class $alias extends $facade{\n";
|
||||||
$output .= " class $alias extends $facade{\n";
|
|
||||||
}else{
|
}else{
|
||||||
$output .= " class $alias{\n";
|
$output .= "class $alias{\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
//If they extend, don't include them.
|
$usedMethods = array();
|
||||||
if(!in_array($alias, $this->onlyExtend))
|
|
||||||
{
|
|
||||||
$usedMethods = array();
|
|
||||||
|
|
||||||
//Check if there are non-static methods for this alias
|
//Only add the methods to the output when the root is not the same as the facade.
|
||||||
if(array_key_exists($alias, $this->nonstatic) ){
|
$addOutput = ($root !== $facade);
|
||||||
$nonstaticMethods = $this->nonstatic[$alias];
|
$output .= $this->getMethods($root, $alias, $usedMethods, $addOutput);
|
||||||
}else{
|
|
||||||
$nonstaticMethods = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
//Get all public methods for this class
|
//Add extra methods, from other classes (magic static calls)
|
||||||
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
|
if(array_key_exists($alias, $this->extra)){
|
||||||
if($methods)
|
$output .= $this->getMethods($this->extra[$alias], $alias, $usedMethods);
|
||||||
{
|
|
||||||
foreach ($methods as $method)
|
|
||||||
{
|
|
||||||
//Skip methods that are already used
|
|
||||||
if(!in_array($method->name, $usedMethods)){
|
|
||||||
$static = !in_array($method->name, $nonstaticMethods);
|
|
||||||
$declaringClass = $method->getDeclaringClass();
|
|
||||||
$output .= $this->parseMethod($method, $alias, $static);
|
|
||||||
$usedMethods[] = $method->name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Add extra methods, from other classes (magic calls)
|
|
||||||
if(array_key_exists($alias, $this->extra)){
|
|
||||||
foreach($this->extra[$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)){
|
|
||||||
$static = !in_array($method->name, $nonstaticMethods);
|
|
||||||
$output .= $this->parseMethod($method, $alias, $static);
|
|
||||||
$usedMethods[] = $method->name;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$output .= " }\n}\n\n";
|
|
||||||
|
|
||||||
|
$output .= "}\n\n";
|
||||||
|
|
||||||
}catch(\Exception $e){
|
}catch(\Exception $e){
|
||||||
$this->error("Exception: ".$e->getMessage()."\nCould not analyze $root.");
|
$this->error("Exception: ".$e->getMessage()."\nCould not analyze $root.");
|
||||||
@@ -259,6 +183,60 @@ namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
|
|||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getRoot($facade){
|
||||||
|
try{
|
||||||
|
//If possible, get the facade root
|
||||||
|
if(method_exists($facade, 'getFacadeRoot')){
|
||||||
|
$root = get_class($facade::getFacadeRoot());
|
||||||
|
}else{
|
||||||
|
$root = $facade;
|
||||||
|
}
|
||||||
|
|
||||||
|
//If it doesn't exist, skip it
|
||||||
|
if(!class_exists($root) && !interface_exists($root)){
|
||||||
|
$this->error("Class $root is not found.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $root;
|
||||||
|
|
||||||
|
//When the database connection is not set, some classes will be skipped
|
||||||
|
}catch(\PDOException $e){
|
||||||
|
$this->error("PDOException: ".$e->getMessage()."\nPlease configure your database connection correctly, or use the sqlite memory driver (-M). Skipping $facade.");
|
||||||
|
return false;
|
||||||
|
}catch(\Exception $e){
|
||||||
|
$this->error("Exception: ".$e->getMessage()."\nSkipping $facade.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
protected function getMethods($classes, $alias, &$usedMethods, $addOutput = true){
|
||||||
|
if(!is_array($classes)){
|
||||||
|
$classes = array($classes);
|
||||||
|
}
|
||||||
|
$output = '';
|
||||||
|
foreach($classes as $class){
|
||||||
|
if(!class_exists($class) && !interface_exists($class)){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$reflection = new \ReflectionClass($class);
|
||||||
|
|
||||||
|
$methods = $reflection->getMethods();
|
||||||
|
if($methods)
|
||||||
|
{
|
||||||
|
foreach ($methods as $method)
|
||||||
|
{
|
||||||
|
if(!in_array($method->name, $usedMethods)){
|
||||||
|
if($method->isPublic() && $addOutput){
|
||||||
|
$output .= $this->parseMethod($method, $alias);
|
||||||
|
}
|
||||||
|
$usedMethods[] = $method->name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @param \ReflectionMethod $method
|
* @param \ReflectionMethod $method
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
@@ -267,7 +245,7 @@ namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
|
|||||||
* @param string $rootParam
|
* @param string $rootParam
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function parseMethod($method, $alias, $static = true){
|
protected function parseMethod($method, $alias){
|
||||||
$output = '';
|
$output = '';
|
||||||
|
|
||||||
// Don't add the __clone() functions
|
// Don't add the __clone() functions
|
||||||
@@ -281,6 +259,39 @@ namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
|
|||||||
$phpdoc = new DocBlock($method, new Context($namespace));
|
$phpdoc = new DocBlock($method, new Context($namespace));
|
||||||
$serializer = new DocBlockSerializer(1, "\t");
|
$serializer = new DocBlockSerializer(1, "\t");
|
||||||
|
|
||||||
|
//Normalize the description and inherit the docs from parents/interfaces
|
||||||
|
$this->normalizeDescription($phpdoc, $method);
|
||||||
|
|
||||||
|
//Correct the return values
|
||||||
|
$returnValue = $this->normalizeReturn($phpdoc, $method, $alias);
|
||||||
|
|
||||||
|
//Get the parameters, including formatted default values
|
||||||
|
list($params, $paramsWithDefault) = $this->getParameters($method);
|
||||||
|
|
||||||
|
//Make the method static
|
||||||
|
$phpdoc->appendTag(Tag::createInstance('@static', $phpdoc));
|
||||||
|
|
||||||
|
//Write the output, using the DocBlock serializer
|
||||||
|
$output .= $serializer->getDocComment($phpdoc) ."\n\t public static function ".$method->name."(";
|
||||||
|
|
||||||
|
$output .= implode($paramsWithDefault, ", ");
|
||||||
|
$output .= "){\r\n";
|
||||||
|
|
||||||
|
//Only return when not a constructor and not void.
|
||||||
|
$return = ($returnValue && $returnValue !== "void" && $method->name !== "__construct") ? 'return' : '';
|
||||||
|
|
||||||
|
//Reference the 'real' function in the declaringclass
|
||||||
|
$root = $method->getDeclaringClass()->getName();
|
||||||
|
$output .= "\t\t$return \\$root::";
|
||||||
|
|
||||||
|
//Write the default parameters in the function call
|
||||||
|
$output .= $method->name."(".implode($params, ", ").");\r\n";
|
||||||
|
$output .= "\t }\n\n";
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function normalizeDescription(&$phpdoc, $method){
|
||||||
//Get the short + long description from the DocBlock
|
//Get the short + long description from the DocBlock
|
||||||
$description = $phpdoc->getText();
|
$description = $phpdoc->getText();
|
||||||
|
|
||||||
@@ -301,11 +312,9 @@ namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Make the method static
|
protected function normalizeReturn(&$phpdoc, $method, $alias){
|
||||||
$phpdoc->appendTag(Tag::createInstance('@static', $phpdoc));
|
|
||||||
|
|
||||||
|
|
||||||
//Get the return type and adjust them for beter autocomplete
|
//Get the return type and adjust them for beter autocomplete
|
||||||
$returnTags = $phpdoc->getTagsByName('return');
|
$returnTags = $phpdoc->getTagsByName('return');
|
||||||
if($returnTags){
|
if($returnTags){
|
||||||
@@ -329,10 +338,10 @@ namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
|
|||||||
}else{
|
}else{
|
||||||
$returnValue = null;
|
$returnValue = null;
|
||||||
}
|
}
|
||||||
|
return $returnValue;
|
||||||
|
}
|
||||||
|
|
||||||
//Write the output, using the DocBlock serializer
|
public function getParameters($method){
|
||||||
$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();
|
||||||
$paramsWithDefault = array();
|
$paramsWithDefault = array();
|
||||||
@@ -356,21 +365,7 @@ namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
|
|||||||
}
|
}
|
||||||
$paramsWithDefault[] = $paramStr;
|
$paramsWithDefault[] = $paramStr;
|
||||||
}
|
}
|
||||||
$output .= implode($paramsWithDefault, ", ");
|
return array($params, $paramsWithDefault);
|
||||||
$output .= "){\r\n";
|
|
||||||
|
|
||||||
//Only return when not a constructor and not void.
|
|
||||||
$return = ($returnValue && $returnValue !== "void" && $method->name !== "__construct") ? 'return' : '';
|
|
||||||
|
|
||||||
//Reference the 'real' function in the declaringclass
|
|
||||||
$root = $method->getDeclaringClass()->getName();
|
|
||||||
$output .= "\t\t$return \\$root::";
|
|
||||||
|
|
||||||
//Write the default parameters in the function call
|
|
||||||
$output .= $method->name."(".implode($params, ", ").");\r\n";
|
|
||||||
$output .= "\t }\n\n";
|
|
||||||
|
|
||||||
return $output;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -313,7 +313,7 @@ class ModelsCommand extends Command {
|
|||||||
$phpdoc = new DocBlock($reflection, new Context($namespace));
|
$phpdoc = new DocBlock($reflection, new Context($namespace));
|
||||||
|
|
||||||
if(!$phpdoc->getText()){
|
if(!$phpdoc->getText()){
|
||||||
$phpdoc->setText("Generated properties for $class");
|
$phpdoc->setText("An Eloquent Model: '$class'");
|
||||||
}
|
}
|
||||||
|
|
||||||
$properties = array();
|
$properties = array();
|
||||||
|
|||||||
+1
-40
@@ -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,8 @@ 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'),
|
||||||
),
|
'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