Compare commits

...
20 Commits
Author SHA1 Message Date
Barry vd. Heuvel bb3bba2c5b Catch non-existant class fatal exceptions
Add autoloader that throws an error, so we can catch errors instead of fatal crashes.
Fixes #220
2015-06-08 09:04:31 +02:00
Barry vd. Heuvel 5aa0f55d2f Update composer.json 2015-06-07 15:09:00 +02:00
Barry vd. Heuvel 89b585b242 Merge pull request #212 from modder2/modder2-patch-1
Bug fix with model relations
2015-05-15 12:26:15 +02:00
Alexander f5b1248a37 Bug fix with model relations
If relation has other methods, generating doc is fail.
E.g.
$this->belongsToMany('App\User')->withTimestamps();
generate this:
@property-read \Illuminate\Database\Eloquent\Collection|\App\User')->withTimestamps([] $drinks
2015-05-14 14:44:54 +03:00
Barry vd. Heuvel 8b190ac295 Merge pull request #211 from MrCue/master
Update ModelsCommand.php
2015-05-14 13:40:20 +02:00
MrCue a861bcaf1b Update ModelsCommand.php
Work with tables in different databases
2015-05-13 16:46:47 +01:00
Barry vd. Heuvel bb3bd2645d Merge pull request #190 from MalikinSergey/patch-1
Add Query\Builder return type to magic scope* methods
2015-05-06 12:08:42 +02:00
Barry vd. Heuvel 687a8c9b47 Merge pull request #178 from fhteam/master_custom_types_support
Custom database types support for L5 branch
2015-05-06 12:02:57 +02:00
Barry vd. Heuvel b1dedc3ee6 5.1 support 2015-05-03 16:00:11 +02:00
Barry vd. Heuvel dac46a081d Merge pull request #201 from elliotwms/patch-1
Indefinite article
2015-04-22 17:46:01 +02:00
Elliot Williams 0c38360317 Indefinite article
I'm pretty sure it should be `a helper file` as opposed to `an helper file`
2015-04-22 16:42:49 +01:00
Malikin Sergey 1d3a98f0e5 Add Query\Builder return type to magic scope* methods 2015-03-29 13:30:50 +03:00
Barry vd. Heuvel d9f79eb128 Merge pull request #188 from grrnikos/master
update readme.md
2015-03-25 21:00:56 +01:00
Nikos Gr 7ef2d2f398 update readme.md 2015-03-25 21:56:47 +02:00
Barry vd. Heuvel 5afcbdc7c8 Supress FQN warning 2015-03-17 10:13:32 +01:00
Barry vd. Heuvel d8d5517f2c Tweak @noinspection 2015-03-17 09:00:28 +01:00
Barry vd. Heuvel f518de4ae1 Filter abstracts for common errors
Fixes #184
2015-03-17 08:57:14 +01:00
Barry vd. Heuvel a89273d7ad Add non-static 2015-03-16 14:56:44 +01:00
Barry vd. Heuvel d9004f98cc Update readme 2015-03-16 14:55:13 +01:00
Vladislav Rastrusny 8378f1bd67 Adding custom DB types support for models 2015-03-03 13:02:47 +03:00
7 changed files with 115 additions and 46 deletions
+4 -4
View File
@@ -11,10 +11,10 @@
], ],
"require": { "require": {
"php": ">=5.4.0", "php": ">=5.4.0",
"illuminate/support": "5.0.x", "illuminate/support": "5.0.x|5.1.x",
"illuminate/console": "5.0.x", "illuminate/console": "5.0.x|5.1.x",
"illuminate/filesystem": "5.0.x", "illuminate/filesystem": "5.0.x|5.1.x",
"phpdocumentor/reflection-docblock": "2.0.x", "phpdocumentor/reflection-docblock": "2.0.4",
"symfony/class-loader": "~2.3" "symfony/class-loader": "~2.3"
}, },
"require-dev": { "require-dev": {
+31 -1
View File
@@ -84,6 +84,36 @@ return array(
'interfaces' => array( 'interfaces' => array(
'\Illuminate\Contracts\Auth\Authenticatable' => config('auth.model', 'App\User'), '\Illuminate\Contracts\Auth\Authenticatable' => config('auth.model', 'App\User'),
) ),
/*
|--------------------------------------------------------------------------
| Support for custom DB types
|--------------------------------------------------------------------------
|
| This setting allow you to map any custom database type (that you may have
| created using CREATE TYPE statement or imported using database plugin
| / extension to a Doctrine type.
|
| Each key in this array is a name of the Doctrine2 DBAL Platform. Currently valid names are:
| 'postgresql', 'db2', 'drizzle', 'mysql', 'oracle', 'sqlanywhere', 'sqlite', 'mssql'
|
| This name is returned by getName() method of the specific Doctrine/DBAL/Platforms/AbstractPlatform descendant
|
| The value of the array is an array of type mappings. Key is the name of the custom type,
| (for example, "jsonb" from Postgres 9.4) and the value is the name of the corresponding Doctrine2 type (in
| our case it is 'json_array'. Doctrine types are listed here:
| http://doctrine-dbal.readthedocs.org/en/latest/reference/types.html
|
| So to support jsonb in your models when working with Postgres, just add the following entry to the array below:
|
| "postgresql" => array(
| "jsonb" => "json_array",
| ),
|
*/
'custom_db_types' => array(
),
); );
+24 -22
View File
@@ -7,8 +7,8 @@
_Checkout [this Laracasts video](https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/15) for a quick introduction/explanation!_ _Checkout [this Laracasts video](https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/15) for a quick introduction/explanation!_
This packages generates a file that your IDE can understand, so it can provide accurate autocompletion. Generation is done, based on the files in your project, so they are alway 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: https://gist.github.com/barryvdh/5227822
@@ -16,18 +16,20 @@ Note: You do need CodeIntel for Sublime Text: https://github.com/SublimeCodeInte
### New: PhpStorm Meta for Container instances ### New: PhpStorm Meta for Container instances
It's possible to generate a PhpStorm meta file, to [add support for factory design pattern](https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata). For Laravel, this means we can make PhpStorm understand what kind of object we are resolving from the IoC Container. For example, `events` will return ann `Illuminate\Events\Dispatcher` object, so with the meta file you can call `app('events')` and it will autocomplete the Dispatcher methods. It's possible to generate a PhpStorm meta file, to [add support for factory design pattern](https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata). For Laravel, this means we can make PhpStorm understand what kind of object we are resolving from the IoC Container. For example, `events` will return an `Illuminate\Events\Dispatcher` object, so with the meta file you can call `app('events')` and it will autocomplete the Dispatcher methods.
php artisan ide-helper:meta php artisan ide-helper:meta
app('events')->fire(); app('events')->fire();
\App::make('events')->.. \App::make('events')->fire();
/** @var \Illuminate\Foundation\Application $app */
$app->make('events')->fire();
Pre-generated example: https://gist.github.com/barryvdh/bb6ffc5d11e0a75dba67 Pre-generated example: https://gist.github.com/barryvdh/bb6ffc5d11e0a75dba67
> Note: This is only available in the `@dev` stability, until a new release is tagged.
> 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.
> Note: When you receive a FatalException about a class that is not found, check your config (For example, remove S3 as cloud driver when you don't have S3 configured. Remove Redis ServiceProvider when you don't use it.) > Note: When you receive a FatalException about a class that is not found, check your config (for example, remove S3 as cloud driver when you don't have S3 configured. Remove Redis ServiceProvider when you don't use it).
### Automatic phpDoc generation for Laravel Facades ### Automatic phpDoc generation for Laravel Facades
@@ -35,47 +37,47 @@ Require this package with composer using the following command:
composer require barryvdh/laravel-ide-helper composer require barryvdh/laravel-ide-helper
After updating composer, add the ServiceProvider to the providers array in config/app.php After updating composer, add the service provider to the `providers` array in `config/app.php`
'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider', 'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider',
You can now re-generate the docs yourself (for future updates) in artisan You can now re-generate the docs yourself (for future updates)
php artisan ide-helper:generate php artisan ide-helper:generate
Note: bootstrap/compiled.php has to be cleared first, so run `php artisan clear-compiled` before generating (and `php artisan optimize` after..) Note: `bootstrap/compiled.php` has to be cleared first, so run `php artisan clear-compiled` before generating (and `php artisan optimize` after).
You can configure your composer.json to do this after each commit: You can configure your composer.json to do this after each commit:
"scripts":{ "scripts":{
"post-update-cmd":[ "post-update-cmd": [
"php artisan clear-compiled", "php artisan clear-compiled",
"php artisan ide-helper:generate", "php artisan ide-helper:generate",
"php artisan optimize" "php artisan optimize"
] ]
}, },
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/laravel-ide-helper --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.
Some classes need a working database connection. If you do not have a working default connection, some facades will not be included. Some classes need a working database connection. If you do not have a default working connection, some facades will not be included.
You can use a in-memory sqlite driver, using the -M option. You can use an in-memory SQLite driver, using the -M option.
You can choose to include helper files. This is not enabled by default, but you can override this with the --helpers (-H) option. You can choose to include helper files. This is not enabled by default, but you can override it with the `--helpers (-H)` option.
The Illuminate/Support/helpers.php is already set-up, but you can add/remove your own files in the config file. The `Illuminate/Support/helpers.php` is already set-up, but you can add/remove your own files in the config file.
### Automatic phpDocs for Models ### Automatic phpDocs for models
> You need to require `doctrine/dbal: ~2.3` in your own composer.json to get database columns. > You need to require `doctrine/dbal: ~2.3` in your own composer.json to get database columns.
If you don't want to write your properties yourself, you can use the command `ide-helper:models` to generate If you don't want to write your properties yourself, you can use the command `php artisan ide-helper:models` to generate
phpDocs, based on table columns, relations and getters/setters. You can write the comments directly to your Model file, using the `--write (-W)` option. By default, you are asked to overwrite or write to a separate file (\_ide\_helper\_models.php) (You can force No with `--nowrite (-N)`). phpDocs, based on table columns, relations and getters/setters. You can write the comments directly to your Model file, using the `--write (-W)` option. By default, you are asked to overwrite or write to a separate file (`_ide_helper_models.php`). You can force No with `--nowrite (-N)`.
Please make sure to backup your models, before writing the info. Please make sure to backup your models, before writing the info.
It should keep the existing comments and only append new properties/methods. The existing phpdoc is replaced, or added if not found. It should keep the existing comments and only append new properties/methods. The existing phpdoc is replaced, or added if not found.
With the `--reset (-R)` option, the existing phpdocs are ignored, only the newly found columns/relations are saved as phpdocs. With the `--reset (-R)` option, the existing phpdocs are ignored, and only the newly found columns/relations are saved as phpdocs.
php artisan ide-helper:models Post php artisan ide-helper:models Post
@@ -92,17 +94,17 @@ With the `--reset (-R)` option, the existing phpdocs are ignored, only the newly
* @property-read \Illuminate\Database\Eloquent\Collection|\Comment[] $comments * @property-read \Illuminate\Database\Eloquent\Collection|\Comment[] $comments
*/ */
By default, models in app/models are scanned. The optional argument tells what models to use (also outside app/models). By default, models in `app/models` are scanned. The optional argument tells what models to use (also outside app/models).
php artisan ide-helper:models Post User php artisan ide-helper:models Post User
You can also scan a different directory, using the --dir option (relative from the base path): You can also scan a different directory, using the `--dir` option (relative from the base path):
php artisan ide-helper:models --dir="path/to/models" --dir="app/src/Model" php artisan ide-helper:models --dir="path/to/models" --dir="app/src/Model"
You can publish the config file (`php artisan vendor:publish`) and set the default directories. You can publish the config file (`php artisan vendor:publish`) and set the default directories.
Models can be ignored using the --ignore (-I) option Models can be ignored using the `--ignore (-I)` option
php artisan ide-helper:models --ignore="Post,User" php artisan ide-helper:models --ignore="Post,User"
+1 -1
View File
@@ -1,7 +1,7 @@
<?= '<?php' ?> <?= '<?php' ?>
/** /**
* An helper file for Laravel 5, to provide autocomplete information to your IDE * A helper file for Laravel 5, to provide autocomplete information to your IDE
* Generated for Laravel <?= $version ?> on <?= date("Y-m-d") ?>. * Generated for Laravel <?= $version ?> on <?= date("Y-m-d") ?>.
* *
* @author Barry vd. Heuvel <[email protected]> * @author Barry vd. Heuvel <[email protected]>
+4 -2
View File
@@ -6,9 +6,11 @@
* *
* @author Barry vd. Heuvel <[email protected]> * @author Barry vd. Heuvel <[email protected]>
* @see https://github.com/barryvdh/laravel-ide-helper * @see https://github.com/barryvdh/laravel-ide-helper
* @noinspection PhpUnusedLocalVariableInspection
* @noinspection PhpIllegalArrayKeyTypeInspection
*/ */
/** @noinspection PhpIllegalArrayKeyTypeInspection */
/** @noinspection PhpUnusedLocalVariableInspection */
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$STATIC_METHOD_TYPES = [ $STATIC_METHOD_TYPES = [
<?php foreach($methods as $method): ?> <?php foreach($methods as $method): ?>
<?= $method ?>('') => [ <?= $method ?>('') => [
+31 -8
View File
@@ -35,10 +35,10 @@ class MetaCommand extends Command {
*/ */
protected $description = 'Generate metadata for PhpStorm'; protected $description = 'Generate metadata for PhpStorm';
/** @var \Illuminate\Filesystem\Filesystem */ /** @var \Illuminate\Contracts\Filesystem\Filesystem */
protected $files; protected $files;
/** @var \Illuminate\View\Factory */ /** @var \Illuminate\Contracts\View\Factory */
protected $view; protected $view;
protected $methods = [ protected $methods = [
@@ -52,8 +52,8 @@ class MetaCommand extends Command {
/** /**
* *
* @param \Illuminate\Filesystem\Filesystem $files * @param \Illuminate\Contracts\Filesystem\Filesystem $files
* @param \Illuminate\View\Factory $view * @param \Illuminate\Contracts\View\Factory $view
*/ */
public function __construct($files, $view) { public function __construct($files, $view) {
$this->files = $files; $this->files = $files;
@@ -68,18 +68,17 @@ class MetaCommand extends Command {
*/ */
public function fire() public function fire()
{ {
$filename = $this->option('filename'); $this->registerClassAutoloadExceptions();
$bindings = array(); $bindings = array();
foreach ($this->getAbstracts() as $abstract) {
foreach ($this->laravel->getBindings() as $abstract => $options) {
try { try {
$concrete = $this->laravel->make($abstract); $concrete = $this->laravel->make($abstract);
if (is_object($concrete)) { if (is_object($concrete)) {
$bindings[$abstract] = get_class($concrete); $bindings[$abstract] = get_class($concrete);
} }
}catch (\Exception $e) { }catch (\Exception $e) {
$this->error("Cannot make $abstract: ".$e->getMessage()); $this->comment("Cannot make '$abstract': ".$e->getMessage());
} }
} }
@@ -88,6 +87,7 @@ class MetaCommand extends Command {
'methods' => $this->methods, 'methods' => $this->methods,
])->render(); ])->render();
$filename = $this->option('filename');
$written = $this->files->put($filename, $content); $written = $this->files->put($filename, $content);
if ($written !== false) { if ($written !== false) {
@@ -97,6 +97,29 @@ class MetaCommand extends Command {
} }
} }
/**
* Get a list of abstracts from the Laravel Application.
*
* @return array
*/
protected function getAbstracts()
{
$abstracts = $this->laravel->getBindings();
// Return the abstract names only
return array_keys($abstracts);
}
/**
* Register an autoloader the throws exceptions when a class is not found.
*/
protected function registerClassAutoloadExceptions()
{
spl_autoload_register(function ($class) {
throw new \Exception("Class '$class' not found.");
});
}
/** /**
* Get the console command options. * Get the console command options.
* *
+16 -4
View File
@@ -217,9 +217,21 @@ class ModelsCommand extends Command
{ {
$table = $model->getConnection()->getTablePrefix() . $model->getTable(); $table = $model->getConnection()->getTablePrefix() . $model->getTable();
$schema = $model->getConnection()->getDoctrineSchemaManager($table); $schema = $model->getConnection()->getDoctrineSchemaManager($table);
$schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); $databasePlatform = $schema->getDatabasePlatform();
$databasePlatform->registerDoctrineTypeMapping('enum', 'string');
$columns = $schema->listTableColumns($table); $platformName = $databasePlatform->getName();
$customTypes = $this->laravel['config']->get("ide-helper.custom_db_types.{$platformName}", array());
foreach ($customTypes as $yourTypeName => $doctrineTypeName) {
$databasePlatform->registerDoctrineTypeMapping($yourTypeName, $doctrineTypeName);
}
$database = null;
if (strpos($table, '.')) {
list($database, $table) = explode('.', $table);
}
$columns = $schema->listTableColumns($table, $database);
if ($columns) { if ($columns) {
foreach ($columns as $column) { foreach ($columns as $column) {
@@ -303,7 +315,7 @@ class ModelsCommand extends Command
$args = $this->getParameters($reflection); $args = $this->getParameters($reflection);
//Remove the first ($query) argument //Remove the first ($query) argument
array_shift($args); array_shift($args);
$this->setMethod($name, '\\' . $reflection->class, $args); $this->setMethod($name, '\Illuminate\Database\Query\Builder|\\' . $reflection->class, $args);
} }
} elseif (!method_exists('Eloquent', $method) && !Str::startsWith($method, 'get')) { } elseif (!method_exists('Eloquent', $method) && !Str::startsWith($method, 'get')) {
@@ -333,7 +345,7 @@ 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, ');'))); $arguments = explode(',', substr($code, 0, strpos($code, ')')));
//Remove quotes, ensure 1 \ in front of the model //Remove quotes, ensure 1 \ in front of the model
$returnModel = $this->getClassName($arguments[0], $model); $returnModel = $this->getClassName($arguments[0], $model);
if ($relation === "belongsToMany" or $relation === 'hasMany' or $relation === 'morphMany' or $relation === 'morphToMany') { if ($relation === "belongsToMany" or $relation === 'hasMany' or $relation === 'morphMany' or $relation === 'morphToMany') {