mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-19 10:33:11 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
83999f8467 | ||
|
|
fab54d90dd | ||
|
|
5d4056fe87 | ||
|
|
3fbe330998 | ||
|
|
78d233fea7 | ||
|
|
0d13a51ff5 | ||
|
|
326e8ea25f | ||
|
|
e7ee934bb1 | ||
|
|
99953adb73 | ||
|
|
0d333c6228 | ||
|
|
0a717d1e79 | ||
|
|
dc62d35bed | ||
|
|
2b7733c6b9 | ||
|
|
76674be735 | ||
|
|
e34b7fb84b | ||
|
|
e77a7b5a9c | ||
|
|
19f7d75c10 |
+1
-1
@@ -30,7 +30,7 @@
|
|||||||
},
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"branch-alias": {
|
"branch-alias": {
|
||||||
"dev-master": "2.0-dev"
|
"dev-master": "2.1-dev"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ _Checkout [this Laracasts video](https://laracasts.com/series/how-to-be-awesome-
|
|||||||
This package generates a file that your IDE understands, so it can provide accurate autocompletion. Generation is done based on the files in your project, so they are always up-to-date.
|
This package generates a file that your IDE understands, so it can provide accurate autocompletion. Generation is done based on the files in your project, so they are always up-to-date.
|
||||||
If you don't want to generate it, you can add a pre-generated file to the root folder of your Laravel project (but this isn't as up-to-date as self generated files).
|
If you don't want to generate it, you can add a pre-generated file to the root folder of your Laravel project (but this isn't as up-to-date as self generated files).
|
||||||
|
|
||||||
* Generated version: https://gist.github.com/barryvdh/5227822
|
* Generated version for L5: https://gist.github.com/barryvdh/5227822
|
||||||
|
* Generated version for Lumen: https://gist.github.com/barryvdh/be17164b0ad51f832f20
|
||||||
|
* Generated Phpstorm Meta file: https://gist.github.com/barryvdh/bb6ffc5d11e0a75dba67
|
||||||
|
|
||||||
Note: You do need CodeIntel for Sublime Text: https://github.com/SublimeCodeIntel/SublimeCodeIntel
|
Note: You do need CodeIntel for Sublime Text: https://github.com/SublimeCodeIntel/SublimeCodeIntel
|
||||||
|
|
||||||
@@ -26,6 +28,9 @@ It's possible to generate a PhpStorm meta file, to [add support for factory desi
|
|||||||
/** @var \Illuminate\Foundation\Application $app */
|
/** @var \Illuminate\Foundation\Application $app */
|
||||||
$app->make('events')->fire();
|
$app->make('events')->fire();
|
||||||
|
|
||||||
|
// When the key is not found, it uses the argument as class name
|
||||||
|
app('App\SomeClass');
|
||||||
|
|
||||||
Pre-generated example: https://gist.github.com/barryvdh/bb6ffc5d11e0a75dba67
|
Pre-generated example: https://gist.github.com/barryvdh/bb6ffc5d11e0a75dba67
|
||||||
|
|
||||||
> Note: You might need to restart PhpStorm and make sure `.phpstorm.meta.php` is indexed.
|
> Note: You might need to restart PhpStorm and make sure `.phpstorm.meta.php` is indexed.
|
||||||
@@ -59,7 +64,7 @@ You can configure your composer.json to do this after each commit:
|
|||||||
|
|
||||||
You can also publish the config file to change implementations (ie. interface to specific class) or set defaults for `--helpers` or `--sublime`.
|
You can also publish the config file to change implementations (ie. interface to specific class) or set defaults for `--helpers` or `--sublime`.
|
||||||
|
|
||||||
php artisan vendor:publish --provider=barryvdh/laravel-ide-helper --tag=config
|
php artisan vendor:publish --provider="Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider" --tag=config
|
||||||
|
|
||||||
The generator tries to identify the real class, but if it cannot be found, you can define it in the config file.
|
The generator tries to identify the real class, but if it cannot be found, you can define it in the config file.
|
||||||
|
|
||||||
|
|||||||
@@ -71,6 +71,11 @@ class MetaCommand extends Command {
|
|||||||
|
|
||||||
$bindings = array();
|
$bindings = array();
|
||||||
foreach ($this->getAbstracts() as $abstract) {
|
foreach ($this->getAbstracts() as $abstract) {
|
||||||
|
// Validator causes problems in Lumen
|
||||||
|
if ($abstract == 'validator') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$concrete = $this->laravel->make($abstract);
|
$concrete = $this->laravel->make($abstract);
|
||||||
if (is_object($concrete)) {
|
if (is_object($concrete)) {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ namespace Barryvdh\LaravelIdeHelper\Console;
|
|||||||
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\Filesystem\Filesystem;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
@@ -28,6 +29,10 @@ use phpDocumentor\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
|||||||
*/
|
*/
|
||||||
class ModelsCommand extends Command
|
class ModelsCommand extends Command
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var Filesystem $files
|
||||||
|
*/
|
||||||
|
protected $files;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The console command name.
|
* The console command name.
|
||||||
@@ -50,6 +55,14 @@ class ModelsCommand extends Command
|
|||||||
protected $dirs = array();
|
protected $dirs = array();
|
||||||
protected $reset;
|
protected $reset;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Filesystem $files
|
||||||
|
*/
|
||||||
|
public function __construct(Filesystem $files)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
$this->files = $files;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the console command.
|
* Execute the console command.
|
||||||
@@ -81,7 +94,7 @@ class ModelsCommand extends Command
|
|||||||
$content = $this->generateDocs($model, $ignore);
|
$content = $this->generateDocs($model, $ignore);
|
||||||
|
|
||||||
if (!$this->write) {
|
if (!$this->write) {
|
||||||
$written = \File::put($filename, $content);
|
$written = $this->files->put($filename, $content);
|
||||||
if ($written !== false) {
|
if ($written !== false) {
|
||||||
$this->info("Model information was written to $filename");
|
$this->info("Model information was written to $filename");
|
||||||
} else {
|
} else {
|
||||||
@@ -181,6 +194,7 @@ class ModelsCommand extends Command
|
|||||||
|
|
||||||
$this->getPropertiesFromMethods($model);
|
$this->getPropertiesFromMethods($model);
|
||||||
$output .= $this->createPhpDocs($name);
|
$output .= $this->createPhpDocs($name);
|
||||||
|
$ignore[] = $name;
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$this->error("Exception: " . $e->getMessage() . "\nCould not analyze class $name.");
|
$this->error("Exception: " . $e->getMessage() . "\nCould not analyze class $name.");
|
||||||
}
|
}
|
||||||
@@ -335,6 +349,7 @@ class ModelsCommand extends Command
|
|||||||
$code .= $file->current();
|
$code .= $file->current();
|
||||||
$file->next();
|
$file->next();
|
||||||
}
|
}
|
||||||
|
$code = trim(preg_replace('/\s\s+/', '', $code));
|
||||||
$begin = strpos($code, 'function(');
|
$begin = strpos($code, 'function(');
|
||||||
$code = substr($code, $begin, strrpos($code, '}') - $begin + 1);
|
$code = substr($code, $begin, strrpos($code, '}') - $begin + 1);
|
||||||
|
|
||||||
@@ -350,24 +365,29 @@ class ModelsCommand extends Command
|
|||||||
$search = '$this->' . $relation . '(';
|
$search = '$this->' . $relation . '(';
|
||||||
if ($pos = stripos($code, $search)) {
|
if ($pos = stripos($code, $search)) {
|
||||||
$code = substr($code, $pos + strlen($search));
|
$code = substr($code, $pos + strlen($search));
|
||||||
$arguments = explode(',', substr($code, 0, strpos($code, ')')));
|
$end = strpos($code, ')->') ?:strpos($code, ');');
|
||||||
//Remove quotes, ensure 1 \ in front of the model
|
if (false !== $end) {
|
||||||
$returnModel = $this->getClassName($arguments[0], $model);
|
$arguments = substr($code, 0, $end);
|
||||||
if ($relation === "belongsToMany" or $relation === 'hasMany' or $relation === 'morphMany' or $relation === 'morphToMany') {
|
$arguments = array_map(function($item) {
|
||||||
//Collection or array of models (because Collection is Arrayable)
|
return trim($item, ' \'\"');
|
||||||
$this->setProperty(
|
}, explode(',', $arguments));
|
||||||
$method,
|
//Remove quotes, ensure 1 \ in front of the model
|
||||||
'\Illuminate\Database\Eloquent\Collection|' . $returnModel . '[]',
|
$returnModel = $this->getClassName($arguments[0], $model);
|
||||||
true,
|
if ($relation === "belongsToMany" or $relation === 'hasMany' or $relation === 'morphMany' or $relation === 'morphToMany') {
|
||||||
null
|
//Collection or array of models (because Collection is Arrayable)
|
||||||
);
|
$this->setProperty(
|
||||||
} else {
|
$method,
|
||||||
//Single model is returned
|
'\Illuminate\Database\Eloquent\Collection|' . $returnModel . '[]',
|
||||||
$this->setProperty($method, $returnModel, true, null);
|
true,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
//Single model is returned
|
||||||
|
$this->setProperty($method, $returnModel, true, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -454,7 +474,8 @@ class ModelsCommand extends Command
|
|||||||
} else {
|
} else {
|
||||||
$attr = 'property-read';
|
$attr = 'property-read';
|
||||||
}
|
}
|
||||||
$tag = Tag::createInstance("@{$attr} {$property['type']} {$name} {$property['comment']}", $phpdoc);
|
$tagLine = trim("@{$attr} {$property['type']} {$name} {$property['comment']}");
|
||||||
|
$tag = Tag::createInstance($tagLine, $phpdoc);
|
||||||
$phpdoc->appendTag($tag);
|
$phpdoc->appendTag($tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -474,7 +495,7 @@ class ModelsCommand extends Command
|
|||||||
|
|
||||||
if ($this->write) {
|
if ($this->write) {
|
||||||
$filename = $reflection->getFileName();
|
$filename = $reflection->getFileName();
|
||||||
$contents = \File::get($filename);
|
$contents = $this->files->get($filename);
|
||||||
if ($originalDoc) {
|
if ($originalDoc) {
|
||||||
$contents = str_replace($originalDoc, $docComment, $contents);
|
$contents = str_replace($originalDoc, $docComment, $contents);
|
||||||
} else {
|
} else {
|
||||||
@@ -485,7 +506,7 @@ class ModelsCommand extends Command
|
|||||||
$contents = substr_replace($contents, $replace, $pos, strlen($needle));
|
$contents = substr_replace($contents, $replace, $pos, strlen($needle));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (\File::put($filename, $contents)) {
|
if ($this->files->put($filename, $contents)) {
|
||||||
$this->info('Written new phpDocBlock to ' . $filename);
|
$this->info('Written new phpDocBlock to ' . $filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -537,7 +558,7 @@ class ModelsCommand extends Command
|
|||||||
{
|
{
|
||||||
// If the class name was resolved via get_class($this) or static::class
|
// If the class name was resolved via get_class($this) or static::class
|
||||||
if (strpos($className, 'get_class($this)') !== false || strpos($className, 'static::class') !== false) {
|
if (strpos($className, 'get_class($this)') !== false || strpos($className, 'static::class') !== false) {
|
||||||
return get_class($model);
|
return "\\" . get_class($model);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the class name was resolved via ::class (PHP 5.5+)
|
// If the class name was resolved via ::class (PHP 5.5+)
|
||||||
|
|||||||
+35
-3
@@ -80,7 +80,7 @@ class Generator
|
|||||||
return $this->view->make('ide-helper::helper')
|
return $this->view->make('ide-helper::helper')
|
||||||
->with('namespaces', $this->getNamespaces())
|
->with('namespaces', $this->getNamespaces())
|
||||||
->with('helpers', $this->helpers)
|
->with('helpers', $this->helpers)
|
||||||
->with('version', $app::VERSION)
|
->with('version', $app->version())
|
||||||
->render();
|
->render();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,8 +166,7 @@ class Generator
|
|||||||
$namespaces = array();
|
$namespaces = array();
|
||||||
|
|
||||||
// Get all aliases
|
// Get all aliases
|
||||||
foreach (AliasLoader::getInstance()->getAliases() as $name => $facade) {
|
foreach ($this->getAliases() as $name => $facade) {
|
||||||
|
|
||||||
// Skip the Redis facade, if not available (otherwise Fatal PHP Error)
|
// Skip the Redis facade, if not available (otherwise Fatal PHP Error)
|
||||||
if ($facade == 'Illuminate\Support\Facades\Redis' && !class_exists('Predis\Client')) {
|
if ($facade == 'Illuminate\Support\Facades\Redis' && !class_exists('Predis\Client')) {
|
||||||
continue;
|
continue;
|
||||||
@@ -194,6 +193,39 @@ class Generator
|
|||||||
return $namespaces;
|
return $namespaces;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getAliases()
|
||||||
|
{
|
||||||
|
// For Laravel, use the AliasLoader
|
||||||
|
if (class_exists('Illuminate\Foundation\AliasLoader')) {
|
||||||
|
return AliasLoader::getInstance()->getAliases();
|
||||||
|
}
|
||||||
|
|
||||||
|
$facades = [
|
||||||
|
'Illuminate\Support\Facades\App' => 'App',
|
||||||
|
'Illuminate\Support\Facades\Auth' => 'Auth',
|
||||||
|
'Illuminate\Support\Facades\Bus' => 'Bus',
|
||||||
|
'Illuminate\Support\Facades\DB' => 'DB',
|
||||||
|
'Illuminate\Support\Facades\Cache' => 'Cache',
|
||||||
|
'Illuminate\Support\Facades\Cookie' => 'Cookie',
|
||||||
|
'Illuminate\Support\Facades\Crypt' => 'Crypt',
|
||||||
|
'Illuminate\Support\Facades\Event' => 'Event',
|
||||||
|
'Illuminate\Support\Facades\Hash' => 'Hash',
|
||||||
|
'Illuminate\Support\Facades\Log' => 'Log',
|
||||||
|
'Illuminate\Support\Facades\Mail' => 'Mail',
|
||||||
|
'Illuminate\Support\Facades\Queue' => 'Queue',
|
||||||
|
'Illuminate\Support\Facades\Request' => 'Request',
|
||||||
|
'Illuminate\Support\Facades\Schema' => 'Schema',
|
||||||
|
'Illuminate\Support\Facades\Session' => 'Session',
|
||||||
|
'Illuminate\Support\Facades\Storage' => 'Storage',
|
||||||
|
// 'Illuminate\Support\Facades\Validator' => 'Validator',
|
||||||
|
];
|
||||||
|
|
||||||
|
// Only return the ones that actually exist
|
||||||
|
return array_filter($facades, function($alias){
|
||||||
|
return class_exists($alias);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the driver/connection/store from the managers
|
* Get the driver/connection/store from the managers
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -36,7 +36,12 @@ class IdeHelperServiceProvider extends ServiceProvider
|
|||||||
$this->loadViewsFrom($viewPath, 'ide-helper');
|
$this->loadViewsFrom($viewPath, 'ide-helper');
|
||||||
|
|
||||||
$configPath = __DIR__ . '/../config/ide-helper.php';
|
$configPath = __DIR__ . '/../config/ide-helper.php';
|
||||||
$this->publishes([$configPath => config_path('ide-helper.php')], 'config');
|
if (function_exists('config_path')) {
|
||||||
|
$publishPath = config_path('ide-helper.php');
|
||||||
|
} else {
|
||||||
|
$publishPath = base_path('config/ide-helper.php');
|
||||||
|
}
|
||||||
|
$this->publishes([$configPath => $publishPath], 'config');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -56,8 +61,8 @@ class IdeHelperServiceProvider extends ServiceProvider
|
|||||||
);
|
);
|
||||||
|
|
||||||
$this->app['command.ide-helper.models'] = $this->app->share(
|
$this->app['command.ide-helper.models'] = $this->app->share(
|
||||||
function () {
|
function ($app) {
|
||||||
return new ModelsCommand();
|
return new ModelsCommand($app['files']);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user