Allow to include multiple classes instead of extending, so it is easier
to maintain.
Also added option to skip methods that don't belong in the facade helper
(fixes #9)
This commit is contained in:
Barry vd. Heuvel
2013-05-09 21:30:13 +02:00
parent 440dcb1d65
commit 101cf480b1
2 changed files with 70 additions and 27 deletions
@@ -39,7 +39,8 @@ class GeneratorCommand extends Command {
$this->useMemoryDriver(); $this->useMemoryDriver();
} }
$replace = \Config::get('laravel-ide-helper::replace'); $extra = \Config::get('laravel-ide-helper::extra');
$skip = \Config::get('laravel-ide-helper::skip');
$onlyExtend = \Config::get('laravel-ide-helper::only_extend'); $onlyExtend = \Config::get('laravel-ide-helper::only_extend');
if( $this->option('helpers') || (\Config::get('laravel-ide-helper::include_helpers') && ! $this->option('nohelpers'))){ if( $this->option('helpers') || (\Config::get('laravel-ide-helper::include_helpers') && ! $this->option('nohelpers'))){
@@ -50,7 +51,7 @@ class GeneratorCommand extends Command {
$sublime = $this->option('sublime') || \Config::get('laravel-ide-helper::sublime'); $sublime = $this->option('sublime') || \Config::get('laravel-ide-helper::sublime');
$content = $this->generateDocs($replace, $onlyExtend, $helpers, $sublime); $content = $this->generateDocs($extra, $skip, $onlyExtend, $helpers, $sublime);
$written = \File::put($filename, $content); $written = \File::put($filename, $content);
@@ -97,7 +98,7 @@ class GeneratorCommand extends Command {
); );
} }
protected function generateDocs($replace = array(), $onlyExtend = array(), $helpers = array(), $sublime = false){ protected function generateDocs($extra = array(), $skip = array(), $onlyExtend = array(), $helpers = array(), $sublime = false){
$aliasLoader = AliasLoader::getInstance(); $aliasLoader = AliasLoader::getInstance();
@@ -116,8 +117,6 @@ class GeneratorCommand extends Command {
namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n"; namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
$aliases = $aliasLoader->getAliases(); $aliases = $aliasLoader->getAliases();
$aliases += array('QueryBuilder' => "Illuminate\Database\Query\Builder");
$aliases += array('Manager' => "Illuminate\Support\Manager");
foreach($aliases as $alias => $facade){ foreach($aliases as $alias => $facade){
@@ -128,10 +127,6 @@ namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
$root = $facade; $root = $facade;
} }
if(array_key_exists($root, $replace)){
$root = $replace[$root];
}
if(!class_exists($root) && !interface_exists($root)){ if(!class_exists($root) && !interface_exists($root)){
$this->error("Class $root is not found."); $this->error("Class $root is not found.");
continue; continue;
@@ -157,11 +152,7 @@ namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
$output .= "namespace $namespace {\n"; $output .= "namespace $namespace {\n";
if($alias === "Eloquent"){ if($root !== $facade or in_array($alias, $onlyExtend)){
$output .= " class $alias extends QueryBuilder{\n";
}elseif($alias === "Auth" or $alias === "Cache"){
$output .= " class $alias extends Manager{\n";
}elseif($root !== $facade or in_array($alias, $onlyExtend)){
//If the root class is not the same as the facade extend it. //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{
@@ -171,12 +162,47 @@ namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
if(!in_array($alias, $onlyExtend)) if(!in_array($alias, $onlyExtend))
{ {
if(array_key_exists($alias, $skip)){
$skipMethods = $skip[$alias];
}else{
$skipMethods = array();
}
$methods = $d->getMethods(); $methods = $d->getMethods();
if($methods) if($methods)
{ {
foreach ($methods as $method) foreach ($methods as $method)
{ {
$output .= $this->parseMethod($method, $root, $sublime); if(!in_array($method->name, $skipMethods)){
$output .= $this->parseMethod($method, $alias, $sublime);
$skipMethods[] = $method->name;
}
}
}
if(array_key_exists($alias, $extra)){
foreach($extra[$alias] as $extraClass){
$i =2;
if(!class_exists($extraClass) && !interface_exists($extraClass)){
continue;
}
$d->analyze($extraClass);
$methods = $d->getMethods();
if($methods)
{
$rootParam = "root".$i++;
$output .= "\t/**\n\t * @var \\$extraClass \$$rootParam\n\t */\n\t static private \$$rootParam;\n\n";
foreach ($methods as $method)
{
if(!in_array($method->name, $skipMethods)){
$output .= $this->parseMethod($method, $alias, $sublime, $rootParam);
$skipMethods[] = $method->name;
}
}
}
} }
} }
} }
@@ -199,7 +225,7 @@ namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
return $output; return $output;
} }
protected function parseMethod($method, $root, $sublime){ protected function parseMethod($method, $alias, $sublime, $rootParam = 'root'){
if($method->name === '__clone'){ if($method->name === '__clone'){
return ''; return '';
} }
@@ -226,11 +252,13 @@ namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
$output .="\t * @param\t".implode($annotation->values, "\t")."\n"; $output .="\t * @param\t".implode($annotation->values, "\t")."\n";
} }
} }
if(!$sublime and ($root == 'Illuminate\Database\Eloquent\Model' or $root == 'Illuminate\Database\Query\Builder') and if(!$sublime and $alias == 'Eloquent' and
(in_array($method->name, array('pluck', 'first', 'fill', 'newInstance', 'newFromBuilder', 'create', 'find', 'findOrFail')) (in_array($method->name, array('pluck', 'first', 'fill', 'newInstance', 'newFromBuilder', 'create', 'find', 'findOrFail'))
or $returnValue === '\Illuminate\Database\Query\Builder')){ or $returnValue === '\Illuminate\Database\Query\Builder')){
//Reference the calling class, to provide more accurate auto-complete //Reference the calling class, to provide more accurate auto-complete
$output .= "\t * @return static\n"; $output .= "\t * @return static\n";
}elseif(!$sublime and $alias == 'Eloquent' and in_array($method->name, array('all', 'get'))){
$output .= "\t * @return array|Eloquent[]|static[]\n";
}else{ }else{
$output .= "\t * @return ".$returnValue."\n"; $output .= "\t * @return ".$returnValue."\n";
} }
@@ -270,9 +298,9 @@ namespace {\n\tdie('Only to be used as an helper for your IDE');\n}\n\n";
if($sublime){ if($sublime){
$output .= "\t\t\$root = new $root();\r\n"; $output .= "\t\t\$root = new $root();\r\n";
$output .= "\t\t$return \$root->"; $output .= "\t\t$return \$$rootParam->";
}else{ }else{
$output .= "\t\t$return static::\$root->"; $output .= "\t\t$return static::\$$rootParam->";
} }
$output .= $method->name."(".implode($params, ", ").");\r\n"; $output .= $method->name."(".implode($params, ", ").");\r\n";
+23 -8
View File
@@ -41,21 +41,36 @@ return array(
base_path().'/vendor/laravel/framework/src/Illuminate/Support/helpers.php', base_path().'/vendor/laravel/framework/src/Illuminate/Support/helpers.php',
), ),
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Replaced classes (Managers) | Extra classes
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| These implementations cannot be found directly, because of a Manager class. | These implementations are not really extended, but called with magic functions
| |
*/ */
'replace' => array( 'extra' => array(
'Illuminate\Auth\AuthManager' => 'Illuminate\Auth\Guard', 'Auth' => array('Illuminate\Auth\Guard'),
'Illuminate\Cache\CacheManager' => 'Illuminate\Cache\StoreInterface', 'Cache' => array('Illuminate\Cache\StoreInterface'),
'Illuminate\Database\DatabaseManager' => 'Illuminate\Database\Connection', 'DB' => array('Illuminate\Database\Connection'),
'Illuminate\Queue\QueueManager' => 'Illuminate\Queue\QueueInterface', 'Eloquent' => array('Illuminate\Database\Query\Builder'),
'Illuminate\Redis\RedisManager' => 'Illuminate\Redis\Database', 'Queue' => array('Illuminate\Queue\QueueInterface'),
),
/*
|--------------------------------------------------------------------------
| Skipped methods
|--------------------------------------------------------------------------
|
| These functions aren't actually facade calls, so don't include them as helper
|
*/
'skip' => array(
'Eloquent' => array('freshTimestamp'),
), ),
/* /*