mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 10:07:12 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
28af7cd19c | ||
|
|
8f004f5d6b | ||
|
|
da677f1ba5 | ||
|
|
321844d9a5 | ||
|
|
928389d7a4 | ||
|
|
dbfcfd66c5 | ||
|
|
a9484c67d1 | ||
|
|
3064296216 | ||
|
|
f057a4f820 | ||
|
|
091f78508c | ||
|
|
cf4fcd810c | ||
|
|
d3203de671 | ||
|
|
093171e01f | ||
|
|
2d82419167 | ||
|
|
0e84805e37 | ||
|
|
7a07515149 | ||
|
|
bae462604e | ||
|
|
b28c8cbae1 | ||
|
|
d734ecabcc | ||
|
|
4f227d5c57 | ||
|
|
35ebf3a2ba | ||
|
|
f7b31bdbdc | ||
|
|
8de1673535 | ||
|
|
d818b72f82 | ||
|
|
e97ed532f0 | ||
|
|
606183ea12 | ||
|
|
a72cd81e5f | ||
|
|
f1ebd847aa | ||
|
|
83597196f9 | ||
|
|
19553f63e4 | ||
|
|
f50470ba49 | ||
|
|
7762e07f0a | ||
|
|
467b5f5b5f | ||
|
|
c213be3d3c | ||
|
|
4b8ba85b34 | ||
|
|
d4c0ae5851 | ||
|
|
3a01e351de | ||
|
|
add34665a8 | ||
|
|
c77752bb58 | ||
|
|
aa8f772a46 | ||
|
|
0c4c55f349 | ||
|
|
ed972acf19 | ||
|
|
60e642e881 | ||
|
|
4069863d0c | ||
|
|
da05897cb7 | ||
|
|
74bac1a4e9 | ||
|
|
33cee80609 | ||
|
|
dee3878566 | ||
|
|
eb5736a7bd | ||
|
|
cc794a2fba | ||
|
|
d82e8f191f | ||
|
|
5497e7d506 | ||
|
|
2f83db311f | ||
|
|
d56bbb2356 | ||
|
|
2fb1135fc5 | ||
|
|
aec17e3327 | ||
|
|
0c560cb375 | ||
|
|
5708012a77 | ||
|
|
af6d9a7fa1 | ||
|
|
f708b9a46c | ||
|
|
e3de462538 | ||
|
|
76f09aaa65 | ||
|
|
56f89502e3 | ||
|
|
11037acfe6 | ||
|
|
e48974e545 | ||
|
|
38a97cf07c | ||
|
|
74ee804dde | ||
|
|
83999f8467 | ||
|
|
fab54d90dd | ||
|
|
5d4056fe87 | ||
|
|
3fbe330998 | ||
|
|
78d233fea7 | ||
|
|
0d13a51ff5 | ||
|
|
326e8ea25f | ||
|
|
e7ee934bb1 | ||
|
|
99953adb73 | ||
|
|
0d333c6228 | ||
|
|
0a717d1e79 | ||
|
|
dc62d35bed | ||
|
|
2b7733c6b9 | ||
|
|
76674be735 | ||
|
|
e34b7fb84b | ||
|
|
e77a7b5a9c | ||
|
|
19f7d75c10 |
@@ -0,0 +1,15 @@
|
|||||||
|
; This file is for unifying the coding style for different editors and IDEs.
|
||||||
|
; More information at http://editorconfig.org
|
||||||
|
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
indent_size = 4
|
||||||
|
indent_style = space
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
|
[*.md]
|
||||||
|
trim_trailing_whitespace = false
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
# Path-based git attributes
|
||||||
|
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
|
||||||
|
|
||||||
|
# Ignore all test and documentation with "export-ignore".
|
||||||
|
/.gitattributes export-ignore
|
||||||
|
/.gitignore export-ignore
|
||||||
|
/.travis.yml export-ignore
|
||||||
|
/phpunit.xml.dist export-ignore
|
||||||
|
/.scrutinizer.yml export-ignore
|
||||||
|
/tests export-ignore
|
||||||
+2
-1
@@ -1,3 +1,4 @@
|
|||||||
/vendor
|
build
|
||||||
|
vendor
|
||||||
composer.lock
|
composer.lock
|
||||||
.idea
|
.idea
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
filter:
|
||||||
|
excluded_paths: [tests/*]
|
||||||
|
|
||||||
|
checks:
|
||||||
|
php:
|
||||||
|
remove_extra_empty_lines: true
|
||||||
|
remove_php_closing_tag: true
|
||||||
|
remove_trailing_whitespace: true
|
||||||
|
fix_use_statements:
|
||||||
|
remove_unused: true
|
||||||
|
preserve_multiple: false
|
||||||
|
preserve_blanklines: true
|
||||||
|
order_alphabetically: true
|
||||||
|
fix_php_opening_tag: true
|
||||||
|
fix_linefeed: true
|
||||||
|
fix_line_ending: true
|
||||||
|
fix_identation_4spaces: true
|
||||||
|
fix_doc_comments: true
|
||||||
|
|
||||||
|
tools:
|
||||||
|
external_code_coverage:
|
||||||
|
timeout: 600
|
||||||
|
runs: 3
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
preset: psr2
|
||||||
+31
@@ -0,0 +1,31 @@
|
|||||||
|
language: php
|
||||||
|
|
||||||
|
php:
|
||||||
|
- 5.5
|
||||||
|
- 5.6
|
||||||
|
- 7.0
|
||||||
|
- hhvm
|
||||||
|
|
||||||
|
# This triggers builds to run on the new TravisCI infrastructure.
|
||||||
|
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
|
||||||
|
sudo: false
|
||||||
|
|
||||||
|
## Cache composer
|
||||||
|
cache:
|
||||||
|
directories:
|
||||||
|
- $HOME/.composer/cache
|
||||||
|
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- php: 5.5
|
||||||
|
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist
|
||||||
|
|
||||||
|
script:
|
||||||
|
- vendor/bin/phpcs --standard=psr2 src/
|
||||||
|
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
|
||||||
|
|
||||||
|
after_script:
|
||||||
|
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' && $TRAVIS_PHP_VERSION != '7.0' ]]; then php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover; fi
|
||||||
+21
@@ -0,0 +1,21 @@
|
|||||||
|
# The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Barry vd. Heuvel <barryvdh@gmail.com>
|
||||||
|
|
||||||
|
> Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
> of this software and associated documentation files (the "Software"), to deal
|
||||||
|
> in the Software without restriction, including without limitation the rights
|
||||||
|
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
> copies of the Software, and to permit persons to whom the Software is
|
||||||
|
> furnished to do so, subject to the following conditions:
|
||||||
|
>
|
||||||
|
> The above copyright notice and this permission notice shall be included in
|
||||||
|
> all copies or substantial portions of the Software.
|
||||||
|
>
|
||||||
|
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
> THE SOFTWARE.
|
||||||
+18
-6
@@ -11,13 +11,16 @@
|
|||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.4.0",
|
"php": ">=5.4.0",
|
||||||
"illuminate/support": "5.0.x|5.1.x",
|
"illuminate/support": "^5.0,<5.4",
|
||||||
"illuminate/console": "5.0.x|5.1.x",
|
"illuminate/console": "^5.0,<5.4",
|
||||||
"illuminate/filesystem": "5.0.x|5.1.x",
|
"illuminate/filesystem": "^5.0,<5.4",
|
||||||
"phpdocumentor/reflection-docblock": "2.0.4",
|
"barryvdh/reflection-docblock": "^2.0.4",
|
||||||
"symfony/class-loader": "~2.3"
|
"symfony/class-loader": "^2.3|^3.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
"phpunit/phpunit" : "4.*",
|
||||||
|
"scrutinizer/ocular": "~1.1",
|
||||||
|
"squizlabs/php_codesniffer": "~2.3",
|
||||||
"doctrine/dbal": "~2.3"
|
"doctrine/dbal": "~2.3"
|
||||||
},
|
},
|
||||||
"suggest": {
|
"suggest": {
|
||||||
@@ -28,9 +31,18 @@
|
|||||||
"Barryvdh\\LaravelIdeHelper\\": "src"
|
"Barryvdh\\LaravelIdeHelper\\": "src"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
":vendor\\:package_name\\": "tests"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "phpunit",
|
||||||
|
"cs": "phpcs --standard=psr2 src/"
|
||||||
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"branch-alias": {
|
"branch-alias": {
|
||||||
"dev-master": "2.0-dev"
|
"dev-master": "2.2-dev"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-1
@@ -83,7 +83,7 @@ return array(
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
'interfaces' => array(
|
'interfaces' => array(
|
||||||
'\Illuminate\Contracts\Auth\Authenticatable' => config('auth.model', 'App\User'),
|
|
||||||
),
|
),
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -116,4 +116,30 @@ return array(
|
|||||||
|
|
||||||
),
|
),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Support for camel cased models
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| There are some Laravel packages (such as Eloquence) that allow for accessing
|
||||||
|
| Eloquent model properties via camel case, instead of snake case.
|
||||||
|
|
|
||||||
|
| Enabling this option will support these packages by saving all model
|
||||||
|
| properties as camel case, instead of snake case.
|
||||||
|
|
|
||||||
|
| For example, normally you would see this:
|
||||||
|
|
|
||||||
|
| * @property \Carbon\Carbon $created_at
|
||||||
|
| * @property \Carbon\Carbon $updated_at
|
||||||
|
|
|
||||||
|
| With this enabled, the properties will be this:
|
||||||
|
|
|
||||||
|
| * @property \Carbon\Carbon $createdAt
|
||||||
|
| * @property \Carbon\Carbon $updatedAt
|
||||||
|
|
|
||||||
|
| Note, it is currently an all-or-nothing option.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
'model_camel_case_properties' => false,
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<phpunit bootstrap="vendor/autoload.php"
|
||||||
|
backupGlobals="false"
|
||||||
|
backupStaticAttributes="false"
|
||||||
|
colors="true"
|
||||||
|
verbose="true"
|
||||||
|
convertErrorsToExceptions="true"
|
||||||
|
convertNoticesToExceptions="true"
|
||||||
|
convertWarningsToExceptions="true"
|
||||||
|
processIsolation="false"
|
||||||
|
stopOnFailure="false">
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name=":vendor Test Suite">
|
||||||
|
<directory>tests</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
<filter>
|
||||||
|
<whitelist>
|
||||||
|
<directory suffix=".php">src/</directory>
|
||||||
|
</whitelist>
|
||||||
|
</filter>
|
||||||
|
<logging>
|
||||||
|
<log type="tap" target="build/report.tap"/>
|
||||||
|
<log type="junit" target="build/report.junit.xml"/>
|
||||||
|
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
|
||||||
|
<log type="coverage-text" target="build/coverage.txt"/>
|
||||||
|
<log type="coverage-clover" target="build/logs/clover.xml"/>
|
||||||
|
</logging>
|
||||||
|
</phpunit>
|
||||||
@@ -1,65 +1,72 @@
|
|||||||
## Laravel 5 IDE Helper Generator
|
## Laravel 5 IDE Helper Generator
|
||||||
[](https://packagist.org/packages/barryvdh/laravel-ide-helper) [](https://packagist.org/packages/barryvdh/laravel-ide-helper)
|
|
||||||
|
|
||||||
### For Laravel 4.x, check [version 1.11](https://github.com/barryvdh/laravel-ide-helper/tree/1.11)
|
[![Latest Version on Packagist][ico-version]][link-packagist]
|
||||||
|
[![Software License][ico-license]](LICENSE.md)
|
||||||
|
[![Build Status][ico-travis]][link-travis]
|
||||||
|
[![Total Downloads][ico-downloads]][link-downloads]
|
||||||
|
|
||||||
|
__For Laravel 4.x, check [version 1.11](https://github.com/barryvdh/laravel-ide-helper/tree/1.11)__
|
||||||
|
|
||||||
### Complete phpDocs, directly from the source
|
### Complete phpDocs, directly from the source
|
||||||
|
|
||||||
_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!_
|
||||||
|
|
||||||
|
* [`php artisan ide-helper:generate` - phpDoc generation for Laravel Facades ](#automatic-phpdoc-generation-for-laravel-facades)
|
||||||
|
* [`php artisan ide-helper:models` - phpDocs for models](#automatic-phpdocs-for-models)
|
||||||
|
* [`php artisan ide-helper:meta` - PhpStorm Meta file](#phpstorm-meta-for-container-instances)
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
### New: PhpStorm Meta for Container instances
|
### Install
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
app('events')->fire();
|
|
||||||
\App::make('events')->fire();
|
|
||||||
|
|
||||||
/** @var \Illuminate\Foundation\Application $app */
|
|
||||||
$app->make('events')->fire();
|
|
||||||
|
|
||||||
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: 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
|
|
||||||
|
|
||||||
Require this package with composer using the following command:
|
Require this package with composer using the following command:
|
||||||
|
|
||||||
composer require barryvdh/laravel-ide-helper
|
```bash
|
||||||
|
composer require barryvdh/laravel-ide-helper
|
||||||
|
```
|
||||||
|
|
||||||
After updating composer, add the service provider 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::class,
|
```php
|
||||||
|
Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
|
||||||
|
```
|
||||||
|
|
||||||
|
### Automatic phpDoc generation for Laravel Facades
|
||||||
|
|
||||||
|
|
||||||
You can now re-generate the docs yourself (for future updates)
|
You can now re-generate the docs yourself (for future updates)
|
||||||
|
|
||||||
php artisan ide-helper:generate
|
```bash
|
||||||
|
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":{
|
```js
|
||||||
"post-update-cmd": [
|
"scripts":{
|
||||||
"php artisan clear-compiled",
|
"post-update-cmd": [
|
||||||
"php artisan ide-helper:generate",
|
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
|
||||||
"php artisan optimize"
|
"php artisan ide-helper:generate",
|
||||||
]
|
"php artisan ide-helper:meta",
|
||||||
},
|
"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
|
```bash
|
||||||
|
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.
|
||||||
|
|
||||||
@@ -73,43 +80,98 @@ The `Illuminate/Support/helpers.php` is already set-up, but you can add/remove y
|
|||||||
|
|
||||||
> 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.
|
||||||
|
|
||||||
|
|
||||||
|
```bash
|
||||||
|
composer require doctrine/dbal
|
||||||
|
```
|
||||||
|
|
||||||
If you don't want to write your properties yourself, you can use the command `php artisan 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, and 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
|
```bash
|
||||||
|
php artisan ide-helper:models Post
|
||||||
|
```
|
||||||
|
|
||||||
/**
|
```php
|
||||||
* An Eloquent Model: 'Post'
|
/**
|
||||||
*
|
* An Eloquent Model: 'Post'
|
||||||
* @property integer $id
|
*
|
||||||
* @property integer $author_id
|
* @property integer $id
|
||||||
* @property string $title
|
* @property integer $author_id
|
||||||
* @property string $text
|
* @property string $title
|
||||||
* @property \Carbon\Carbon $created_at
|
* @property string $text
|
||||||
* @property \Carbon\Carbon $updated_at
|
* @property \Carbon\Carbon $created_at
|
||||||
* @property-read \User $author
|
* @property \Carbon\Carbon $updated_at
|
||||||
* @property-read \Illuminate\Database\Eloquent\Collection|\Comment[] $comments
|
* @property-read \User $author
|
||||||
*/
|
* @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
|
```bash
|
||||||
|
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"
|
```bash
|
||||||
|
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"
|
```bash
|
||||||
|
php artisan ide-helper:models --ignore="Post,User"
|
||||||
|
```
|
||||||
|
|
||||||
Note: With namespaces, wrap your model name in " signs: `php artisan ide-helper:models "API\User"`, or escape the slashes (`Api\\User`)
|
Note: With namespaces, wrap your model name in " signs: `php artisan ide-helper:models "API\User"`, or escape the slashes (`Api\\User`)
|
||||||
|
|
||||||
|
## 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 an `Illuminate\Events\Dispatcher` object, so with the meta file you can call `app('events')` and it will autocomplete the Dispatcher methods.
|
||||||
|
|
||||||
|
``` bash
|
||||||
|
php artisan ide-helper:meta
|
||||||
|
```
|
||||||
|
|
||||||
|
```php
|
||||||
|
app('events')->fire();
|
||||||
|
\App::make('events')->fire();
|
||||||
|
|
||||||
|
/** @var \Illuminate\Foundation\Application $app */
|
||||||
|
$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
|
||||||
|
|
||||||
|
> 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).
|
||||||
|
|
||||||
### License
|
### License
|
||||||
|
|
||||||
The Laravel IDE Helper Generator is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
|
The Laravel IDE Helper Generator is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
|
||||||
|
[ico-version]: https://img.shields.io/packagist/v/barryvdh/laravel-ide-helper.svg?style=flat-square
|
||||||
|
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
|
||||||
|
[ico-travis]: https://img.shields.io/travis/barryvdh/laravel-ide-helper/master.svg?style=flat-square
|
||||||
|
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/barryvdh/laravel-ide-helper.svg?style=flat-square
|
||||||
|
[ico-code-quality]: https://img.shields.io/scrutinizer/g/barryvdh/laravel-ide-helper.svg?style=flat-square
|
||||||
|
[ico-downloads]: https://img.shields.io/packagist/dt/barryvdh/laravel-ide-helper.svg?style=flat-square
|
||||||
|
|
||||||
|
[link-packagist]: https://packagist.org/packages/barryvdh/laravel-ide-helper
|
||||||
|
[link-travis]: https://travis-ci.org/barryvdh/laravel-ide-helper
|
||||||
|
[link-scrutinizer]: https://scrutinizer-ci.com/g/barryvdh/laravel-ide-helper/code-structure
|
||||||
|
[link-code-quality]: https://scrutinizer-ci.com/g/barryvdh/laravel-ide-helper
|
||||||
|
[link-downloads]: https://packagist.org/packages/barryvdh/laravel-ide-helper
|
||||||
|
[link-author]: https://github.com/barryvdh
|
||||||
|
[link-contributors]: ../../contributors
|
||||||
|
|||||||
+293
-284
@@ -1,284 +1,293 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Laravel IDE Helper Generator
|
* Laravel IDE Helper Generator
|
||||||
*
|
*
|
||||||
* @author Barry vd. Heuvel <[email protected]>
|
* @author Barry vd. Heuvel <[email protected]>
|
||||||
* @copyright 2014 Barry vd. Heuvel / Fruitcake Studio (http://www.fruitcakestudio.nl)
|
* @copyright 2014 Barry vd. Heuvel / Fruitcake Studio (http://www.fruitcakestudio.nl)
|
||||||
* @license http://www.opensource.org/licenses/mit-license.php MIT
|
* @license http://www.opensource.org/licenses/mit-license.php MIT
|
||||||
* @link https://github.com/barryvdh/laravel-ide-helper
|
* @link https://github.com/barryvdh/laravel-ide-helper
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Barryvdh\LaravelIdeHelper;
|
namespace Barryvdh\LaravelIdeHelper;
|
||||||
|
|
||||||
class Alias
|
class Alias
|
||||||
{
|
{
|
||||||
protected $alias;
|
protected $alias;
|
||||||
protected $facade;
|
protected $facade;
|
||||||
protected $extends = null;
|
protected $extends = null;
|
||||||
protected $classType = 'class';
|
protected $classType = 'class';
|
||||||
protected $short;
|
protected $short;
|
||||||
protected $namespace = '__root';
|
protected $namespace = '__root';
|
||||||
protected $root = null;
|
protected $root = null;
|
||||||
protected $classes = array();
|
protected $classes = array();
|
||||||
protected $methods = array();
|
protected $methods = array();
|
||||||
protected $usedMethods = array();
|
protected $usedMethods = array();
|
||||||
protected $valid = false;
|
protected $valid = false;
|
||||||
protected $magicMethods = array();
|
protected $magicMethods = array();
|
||||||
protected $interfaces = array();
|
protected $interfaces = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
* @param string $facade
|
* @param string $facade
|
||||||
* @param array $magicMethods
|
* @param array $magicMethods
|
||||||
* @param array $interfaces
|
* @param array $interfaces
|
||||||
*/
|
*/
|
||||||
public function __construct($alias, $facade, $magicMethods = array(), $interfaces = array())
|
public function __construct($alias, $facade, $magicMethods = array(), $interfaces = array())
|
||||||
{
|
{
|
||||||
$this->alias = $alias;
|
$this->alias = $alias;
|
||||||
$this->magicMethods = $magicMethods;
|
$this->magicMethods = $magicMethods;
|
||||||
$this->interfaces = $interfaces;
|
$this->interfaces = $interfaces;
|
||||||
|
|
||||||
// Make the class absolute
|
// Make the class absolute
|
||||||
$facade = '\\' . ltrim($facade, '\\');
|
$facade = '\\' . ltrim($facade, '\\');
|
||||||
$this->facade = $facade;
|
$this->facade = $facade;
|
||||||
|
|
||||||
$this->detectRoot();
|
$this->detectRoot();
|
||||||
|
|
||||||
if ((!$this->isTrait() && $this->root)) {
|
if ((!$this->isTrait() && $this->root)) {
|
||||||
$this->valid = true;
|
$this->valid = true;
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->addClass($this->root);
|
$this->addClass($this->root);
|
||||||
$this->detectNamespace();
|
$this->detectNamespace();
|
||||||
$this->detectClassType();
|
$this->detectClassType();
|
||||||
|
|
||||||
if($facade === '\Illuminate\Database\Eloquent\Model'){
|
if ($facade === '\Illuminate\Database\Eloquent\Model') {
|
||||||
$this->usedMethods = array('decrement', 'increment');
|
$this->usedMethods = array('decrement', 'increment');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add one or more classes to analyze
|
* Add one or more classes to analyze
|
||||||
*
|
*
|
||||||
* @param array|string $classes
|
* @param array|string $classes
|
||||||
*/
|
*/
|
||||||
public function addClass($classes)
|
public function addClass($classes)
|
||||||
{
|
{
|
||||||
$classes = (array)$classes;
|
$classes = (array)$classes;
|
||||||
foreach ($classes as $class) {
|
foreach ($classes as $class) {
|
||||||
if (class_exists($class) || interface_exists($class)) {
|
if (class_exists($class) || interface_exists($class)) {
|
||||||
$this->classes[] = $class;
|
$this->classes[] = $class;
|
||||||
}else{
|
} else {
|
||||||
echo "Class not exists: $class\r\n";
|
echo "Class not exists: $class\r\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if this class is valid to process.
|
* Check if this class is valid to process.
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isValid()
|
public function isValid()
|
||||||
{
|
{
|
||||||
return $this->valid;
|
return $this->valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the classtype, 'interface' or 'class'
|
* Get the classtype, 'interface' or 'class'
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getClasstype()
|
public function getClasstype()
|
||||||
{
|
{
|
||||||
return $this->classType;
|
return $this->classType;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the class which this alias extends
|
* Get the class which this alias extends
|
||||||
*
|
*
|
||||||
* @return null|string
|
* @return null|string
|
||||||
*/
|
*/
|
||||||
public function getExtends()
|
public function getExtends()
|
||||||
{
|
{
|
||||||
return $this->extends;
|
return $this->extends;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the Alias by which this class is called
|
* Get the Alias by which this class is called
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getAlias()
|
public function getAlias()
|
||||||
{
|
{
|
||||||
return $this->alias;
|
return $this->alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the short name (without namespace)
|
* Return the short name (without namespace)
|
||||||
*/
|
*/
|
||||||
public function getShortName(){
|
public function getShortName()
|
||||||
return $this->short;
|
{
|
||||||
}
|
return $this->short;
|
||||||
/**
|
}
|
||||||
* Get the namespace from the alias
|
/**
|
||||||
*
|
* Get the namespace from the alias
|
||||||
* @return string
|
*
|
||||||
*/
|
* @return string
|
||||||
public function getNamespace()
|
*/
|
||||||
{
|
public function getNamespace()
|
||||||
return $this->namespace;
|
{
|
||||||
}
|
return $this->namespace;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Get the methods found by this Alias
|
/**
|
||||||
*
|
* Get the methods found by this Alias
|
||||||
* @return array
|
*
|
||||||
*/
|
* @return array
|
||||||
public function getMethods()
|
*/
|
||||||
{
|
public function getMethods()
|
||||||
$this->addMagicMethods();
|
{
|
||||||
$this->detectMethods();
|
$this->addMagicMethods();
|
||||||
return $this->methods;
|
$this->detectMethods();
|
||||||
}
|
return $this->methods;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Detect the namespace
|
/**
|
||||||
*/
|
* Detect the namespace
|
||||||
protected function detectNamespace()
|
*/
|
||||||
{
|
protected function detectNamespace()
|
||||||
if (strpos($this->alias, '\\')) {
|
{
|
||||||
$nsParts = explode('\\', $this->alias);
|
if (strpos($this->alias, '\\')) {
|
||||||
$this->short = array_pop($nsParts);
|
$nsParts = explode('\\', $this->alias);
|
||||||
$this->namespace = implode('\\', $nsParts);
|
$this->short = array_pop($nsParts);
|
||||||
}else{
|
$this->namespace = implode('\\', $nsParts);
|
||||||
$this->short = $this->alias;
|
} else {
|
||||||
}
|
$this->short = $this->alias;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Detect the class type
|
/**
|
||||||
*/
|
* Detect the class type
|
||||||
protected function detectClassType()
|
*/
|
||||||
{
|
protected function detectClassType()
|
||||||
//Some classes extend the facade
|
{
|
||||||
if (interface_exists($this->facade)) {
|
//Some classes extend the facade
|
||||||
$this->classType = 'interface';
|
if (interface_exists($this->facade)) {
|
||||||
$this->extends = $this->facade;
|
$this->classType = 'interface';
|
||||||
} else {
|
$this->extends = $this->facade;
|
||||||
$this->classType = 'class';
|
} else {
|
||||||
if (class_exists($this->facade)) {
|
$this->classType = 'class';
|
||||||
$this->extends = $this->facade;
|
if (class_exists($this->facade)) {
|
||||||
}
|
$this->extends = $this->facade;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Get the real root of a facade
|
/**
|
||||||
*
|
* Get the real root of a facade
|
||||||
* @return bool|string
|
*
|
||||||
*/
|
* @return bool|string
|
||||||
protected function detectRoot()
|
*/
|
||||||
{
|
protected function detectRoot()
|
||||||
$facade = $this->facade;
|
{
|
||||||
|
$facade = $this->facade;
|
||||||
try {
|
|
||||||
//If possible, get the facade root
|
try {
|
||||||
if (method_exists($facade, 'getFacadeRoot')) {
|
//If possible, get the facade root
|
||||||
$root = get_class($facade::getFacadeRoot());
|
if (method_exists($facade, 'getFacadeRoot')) {
|
||||||
} else {
|
$root = get_class($facade::getFacadeRoot());
|
||||||
$root = $facade;
|
} else {
|
||||||
}
|
$root = $facade;
|
||||||
|
}
|
||||||
//If it doesn't exist, skip it
|
|
||||||
if (!class_exists($root) && !interface_exists($root)) {
|
//If it doesn't exist, skip it
|
||||||
return;
|
if (!class_exists($root) && !interface_exists($root)) {
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
$this->root = $root;
|
|
||||||
|
$this->root = $root;
|
||||||
//When the database connection is not set, some classes will be skipped
|
|
||||||
} catch (\PDOException $e) {
|
//When the database connection is not set, some classes will be skipped
|
||||||
$this->error(
|
} catch (\PDOException $e) {
|
||||||
"PDOException: " . $e->getMessage() . "\nPlease configure your database connection correctly, or use the sqlite memory driver (-M). Skipping $facade."
|
$this->error(
|
||||||
);
|
"PDOException: " . $e->getMessage() .
|
||||||
} catch (\Exception $e) {
|
"\nPlease configure your database connection correctly, or use the sqlite memory driver (-M)." .
|
||||||
$this->error("Exception: " . $e->getMessage() . "\nSkipping $facade.");
|
" Skipping $facade."
|
||||||
}
|
);
|
||||||
}
|
} catch (\Exception $e) {
|
||||||
|
$this->error("Exception: " . $e->getMessage() . "\nSkipping $facade.");
|
||||||
/**
|
}
|
||||||
* Detect if this class is a trait or not.
|
}
|
||||||
*
|
|
||||||
* @return bool
|
/**
|
||||||
*/
|
* Detect if this class is a trait or not.
|
||||||
protected function isTrait()
|
*
|
||||||
{
|
* @return bool
|
||||||
// Check if the facade is not a Trait
|
*/
|
||||||
if (function_exists('trait_exists') && trait_exists($this->facade)) {
|
protected function isTrait()
|
||||||
return true;
|
{
|
||||||
}
|
// Check if the facade is not a Trait
|
||||||
return false;
|
if (function_exists('trait_exists') && trait_exists($this->facade)) {
|
||||||
}
|
return true;
|
||||||
|
}
|
||||||
/**
|
return false;
|
||||||
* Add magic methods, as defined in the configuration files
|
}
|
||||||
*/
|
|
||||||
protected function addMagicMethods()
|
/**
|
||||||
{
|
* Add magic methods, as defined in the configuration files
|
||||||
foreach($this->magicMethods as $magic => $real){
|
*/
|
||||||
list($className, $name) = explode('::', $real);
|
protected function addMagicMethods()
|
||||||
if(!class_exists($className) && !interface_exists($className)){
|
{
|
||||||
continue;
|
foreach ($this->magicMethods as $magic => $real) {
|
||||||
}
|
list($className, $name) = explode('::', $real);
|
||||||
$method = new \ReflectionMethod($className, $name);
|
if (!class_exists($className) && !interface_exists($className)) {
|
||||||
$class = new \ReflectionClass($className);
|
continue;
|
||||||
|
}
|
||||||
if(!in_array($method->name, $this->usedMethods)){
|
$method = new \ReflectionMethod($className, $name);
|
||||||
if($class !== $this->root){
|
$class = new \ReflectionClass($className);
|
||||||
$this->methods[] = new Method($method, $this->alias, $class, $magic, $this->interfaces);
|
|
||||||
}
|
if (!in_array($method->name, $this->usedMethods)) {
|
||||||
$this->usedMethods[] = $magic;
|
if ($class !== $this->root) {
|
||||||
}
|
$this->methods[] = new Method($method, $this->alias, $class, $magic, $this->interfaces);
|
||||||
}
|
}
|
||||||
}
|
$this->usedMethods[] = $magic;
|
||||||
|
}
|
||||||
/**
|
}
|
||||||
* Get the methods for one or multiple classes.
|
}
|
||||||
*
|
|
||||||
* @return string
|
/**
|
||||||
*/
|
* Get the methods for one or multiple classes.
|
||||||
protected function detectMethods()
|
*
|
||||||
{
|
* @return string
|
||||||
|
*/
|
||||||
foreach ($this->classes as $class) {
|
protected function detectMethods()
|
||||||
$reflection = new \ReflectionClass($class);
|
{
|
||||||
|
|
||||||
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
|
foreach ($this->classes as $class) {
|
||||||
if ($methods) {
|
$reflection = new \ReflectionClass($class);
|
||||||
foreach ($methods as $method) {
|
|
||||||
if (!in_array($method->name, $this->usedMethods)) {
|
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
|
||||||
// Only add the methods to the output when the root is not the same as the class.
|
if ($methods) {
|
||||||
// And don't add the __*() methods
|
foreach ($methods as $method) {
|
||||||
if ($this->extends !== $class && substr($method->name, 0, 2) !== '__') {
|
if (!in_array($method->name, $this->usedMethods)) {
|
||||||
$this->methods[] = new Method($method, $this->alias, $reflection, $method->name, $this->interfaces);
|
// Only add the methods to the output when the root is not the same as the class.
|
||||||
}
|
// And don't add the __*() methods
|
||||||
$this->usedMethods[] = $method->name;
|
if ($this->extends !== $class && substr($method->name, 0, 2) !== '__') {
|
||||||
}
|
$this->methods[] = new Method(
|
||||||
}
|
$method,
|
||||||
}
|
$this->alias,
|
||||||
}
|
$reflection,
|
||||||
}
|
$method->name,
|
||||||
|
$this->interfaces
|
||||||
/**
|
);
|
||||||
* Output an error.
|
}
|
||||||
*
|
$this->usedMethods[] = $method->name;
|
||||||
* @param string $string
|
}
|
||||||
* @return void
|
}
|
||||||
*/
|
}
|
||||||
protected function error($string)
|
}
|
||||||
{
|
}
|
||||||
echo $string . "\r\n";
|
|
||||||
}
|
/**
|
||||||
}
|
* Output an error.
|
||||||
|
*
|
||||||
|
* @param string $string
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function error($string)
|
||||||
|
{
|
||||||
|
echo $string . "\r\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ 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;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A command to generate autocomplete information for your IDE
|
* A command to generate autocomplete information for your IDE
|
||||||
*
|
*
|
||||||
@@ -60,7 +59,8 @@ class GeneratorCommand extends Command
|
|||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
/*ConfigRepository */ $config,
|
/*ConfigRepository */ $config,
|
||||||
Filesystem $files, /* Illuminate\View\Factory */
|
Filesystem $files,
|
||||||
|
/* Illuminate\View\Factory */
|
||||||
$view
|
$view
|
||||||
) {
|
) {
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
@@ -166,5 +166,4 @@ class GeneratorCommand extends Command
|
|||||||
array('sublime', "S", InputOption::VALUE_NONE, 'DEPRECATED: Use different style for SublimeText CodeIntel'),
|
array('sublime', "S", InputOption::VALUE_NONE, 'DEPRECATED: Use different style for SublimeText CodeIntel'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|||||||
*
|
*
|
||||||
* @author Barry vd. Heuvel <[email protected]>
|
* @author Barry vd. Heuvel <[email protected]>
|
||||||
*/
|
*/
|
||||||
class MetaCommand extends Command {
|
class MetaCommand extends Command
|
||||||
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The console command name.
|
* The console command name.
|
||||||
@@ -46,7 +47,7 @@ class MetaCommand extends Command {
|
|||||||
'new \Illuminate\Contracts\Container\Container',
|
'new \Illuminate\Contracts\Container\Container',
|
||||||
'\Illuminate\Contracts\Container\Container::make(\'\')',
|
'\Illuminate\Contracts\Container\Container::make(\'\')',
|
||||||
'\App::make(\'\')',
|
'\App::make(\'\')',
|
||||||
'app(\'\')',
|
'\app(\'\')',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -54,7 +55,8 @@ class MetaCommand extends Command {
|
|||||||
* @param \Illuminate\Contracts\Filesystem\Filesystem $files
|
* @param \Illuminate\Contracts\Filesystem\Filesystem $files
|
||||||
* @param \Illuminate\Contracts\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;
|
||||||
$this->view = $view;
|
$this->view = $view;
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
@@ -71,12 +73,17 @@ class MetaCommand extends Command {
|
|||||||
|
|
||||||
$bindings = array();
|
$bindings = array();
|
||||||
foreach ($this->getAbstracts() as $abstract) {
|
foreach ($this->getAbstracts() as $abstract) {
|
||||||
|
// Validator and seeder cause problems
|
||||||
|
if (in_array($abstract, ['validator', 'seeder'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
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) {
|
||||||
if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
|
if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
|
||||||
$this->comment("Cannot make '$abstract': ".$e->getMessage());
|
$this->comment("Cannot make '$abstract': ".$e->getMessage());
|
||||||
}
|
}
|
||||||
@@ -100,7 +107,7 @@ class MetaCommand extends Command {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a list of abstracts from the Laravel Application.
|
* Get a list of abstracts from the Laravel Application.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function getAbstracts()
|
protected function getAbstracts()
|
||||||
|
|||||||
+116
-65
@@ -11,15 +11,17 @@
|
|||||||
namespace Barryvdh\LaravelIdeHelper\Console;
|
namespace Barryvdh\LaravelIdeHelper\Console;
|
||||||
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||||
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;
|
||||||
use Symfony\Component\ClassLoader\ClassMapGenerator;
|
use Symfony\Component\ClassLoader\ClassMapGenerator;
|
||||||
use phpDocumentor\Reflection\DocBlock;
|
use Barryvdh\Reflection\DocBlock;
|
||||||
use phpDocumentor\Reflection\DocBlock\Context;
|
use Barryvdh\Reflection\DocBlock\Context;
|
||||||
use phpDocumentor\Reflection\DocBlock\Tag;
|
use Barryvdh\Reflection\DocBlock\Tag;
|
||||||
use phpDocumentor\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A command to generate autocomplete information for your IDE
|
* A command to generate autocomplete information for your IDE
|
||||||
@@ -28,6 +30,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 +56,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 +95,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 {
|
||||||
@@ -99,7 +113,7 @@ class ModelsCommand extends Command
|
|||||||
protected function getArguments()
|
protected function getArguments()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array('model', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Which models to include', array()),
|
array('model', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Which models to include', array()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,12 +125,12 @@ class ModelsCommand extends Command
|
|||||||
protected function getOptions()
|
protected function getOptions()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array('filename', 'F', InputOption::VALUE_OPTIONAL, 'The path to the helper file', $this->filename),
|
array('filename', 'F', InputOption::VALUE_OPTIONAL, 'The path to the helper file', $this->filename),
|
||||||
array('dir', 'D', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The model dir', array()),
|
array('dir', 'D', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The model dir', array()),
|
||||||
array('write', 'W', InputOption::VALUE_NONE, 'Write to Model file'),
|
array('write', 'W', InputOption::VALUE_NONE, 'Write to Model file'),
|
||||||
array('nowrite', 'N', InputOption::VALUE_NONE, 'Don\'t write to Model file'),
|
array('nowrite', 'N', InputOption::VALUE_NONE, 'Don\'t write to Model file'),
|
||||||
array('reset', 'R', InputOption::VALUE_NONE, 'Remove the original phpdocs instead of appending'),
|
array('reset', 'R', InputOption::VALUE_NONE, 'Remove the original phpdocs instead of appending'),
|
||||||
array('ignore', 'I', InputOption::VALUE_OPTIONAL, 'Which models to ignore', ''),
|
array('ignore', 'I', InputOption::VALUE_OPTIONAL, 'Which models to ignore', ''),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +140,7 @@ class ModelsCommand extends Command
|
|||||||
|
|
||||||
$output = "<?php
|
$output = "<?php
|
||||||
/**
|
/**
|
||||||
* An helper file for your Eloquent Models
|
* A helper file for your Eloquent Models
|
||||||
* Copy the phpDocs from this file to the correct Model,
|
* Copy the phpDocs from this file to the correct Model,
|
||||||
* And remove them from this file, to prevent double declarations.
|
* And remove them from this file, to prevent double declarations.
|
||||||
*
|
*
|
||||||
@@ -170,7 +184,8 @@ class ModelsCommand extends Command
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!$reflectionClass->IsInstantiable()) {
|
if (!$reflectionClass->IsInstantiable()) {
|
||||||
throw new \Exception($name . ' is not instantiable.');
|
// ignore abstract class or interface
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$model = $this->laravel->make($name);
|
$model = $this->laravel->make($name);
|
||||||
@@ -181,16 +196,17 @@ 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.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$hasDoctrine) {
|
if (!$hasDoctrine) {
|
||||||
$this->error(
|
$this->error(
|
||||||
'Warning: `"doctrine/dbal": "~2.3"` is required to load database information. Please require that in your composer.json and run `composer update`.'
|
'Warning: `"doctrine/dbal": "~2.3"` is required to load database information. '.
|
||||||
|
'Please require that in your composer.json and run `composer update`.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,9 +309,9 @@ class ModelsCommand extends Command
|
|||||||
if ($methods) {
|
if ($methods) {
|
||||||
foreach ($methods as $method) {
|
foreach ($methods as $method) {
|
||||||
if (Str::startsWith($method, 'get') && Str::endsWith(
|
if (Str::startsWith($method, 'get') && Str::endsWith(
|
||||||
$method,
|
$method,
|
||||||
'Attribute'
|
'Attribute'
|
||||||
) && $method !== 'getAttribute'
|
) && $method !== 'getAttribute'
|
||||||
) {
|
) {
|
||||||
//Magic get<name>Attribute
|
//Magic get<name>Attribute
|
||||||
$name = Str::snake(substr($method, 3, -9));
|
$name = Str::snake(substr($method, 3, -9));
|
||||||
@@ -303,9 +319,9 @@ class ModelsCommand extends Command
|
|||||||
$this->setProperty($name, null, true, null);
|
$this->setProperty($name, null, true, null);
|
||||||
}
|
}
|
||||||
} elseif (Str::startsWith($method, 'set') && Str::endsWith(
|
} elseif (Str::startsWith($method, 'set') && Str::endsWith(
|
||||||
$method,
|
$method,
|
||||||
'Attribute'
|
'Attribute'
|
||||||
) && $method !== 'setAttribute'
|
) && $method !== 'setAttribute'
|
||||||
) {
|
) {
|
||||||
//Magic set<name>Attribute
|
//Magic set<name>Attribute
|
||||||
$name = Str::snake(substr($method, 3, -9));
|
$name = Str::snake(substr($method, 3, -9));
|
||||||
@@ -322,8 +338,9 @@ class ModelsCommand extends Command
|
|||||||
array_shift($args);
|
array_shift($args);
|
||||||
$this->setMethod($name, '\Illuminate\Database\Query\Builder|\\' . $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('Illuminate\Database\Eloquent\Model', $method)
|
||||||
|
&& !Str::startsWith($method, 'get')
|
||||||
|
) {
|
||||||
//Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php
|
//Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php
|
||||||
$reflection = new \ReflectionMethod($model, $method);
|
$reflection = new \ReflectionMethod($model, $method);
|
||||||
|
|
||||||
@@ -335,39 +352,53 @@ 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);
|
||||||
|
|
||||||
foreach (array(
|
foreach (array(
|
||||||
'hasMany',
|
'hasMany',
|
||||||
'belongsToMany',
|
'hasManyThrough',
|
||||||
'hasOne',
|
'belongsToMany',
|
||||||
'belongsTo',
|
'hasOne',
|
||||||
'morphTo',
|
'belongsTo',
|
||||||
'morphMany',
|
'morphOne',
|
||||||
'morphToMany'
|
'morphTo',
|
||||||
|
'morphMany',
|
||||||
|
'morphToMany'
|
||||||
) as $relation) {
|
) as $relation) {
|
||||||
$search = '$this->' . $relation . '(';
|
$search = '$this->' . $relation . '(';
|
||||||
if ($pos = stripos($code, $search)) {
|
if ($pos = stripos($code, $search)) {
|
||||||
$code = substr($code, $pos + strlen($search));
|
//Resolve the relation's model to a Relation object.
|
||||||
$arguments = explode(',', substr($code, 0, strpos($code, ')')));
|
$relationObj = $model->$method();
|
||||||
//Remove quotes, ensure 1 \ in front of the model
|
|
||||||
$returnModel = $this->getClassName($arguments[0], $model);
|
if ($relationObj instanceof Relation) {
|
||||||
if ($relation === "belongsToMany" or $relation === 'hasMany' or $relation === 'morphMany' or $relation === 'morphToMany') {
|
$relatedModel = '\\' . get_class($relationObj->getRelated());
|
||||||
//Collection or array of models (because Collection is Arrayable)
|
|
||||||
$this->setProperty(
|
$relations = ['hasManyThrough', 'belongsToMany', 'hasMany', 'morphMany', 'morphToMany'];
|
||||||
$method,
|
if (in_array($relation, $relations)) {
|
||||||
'\Illuminate\Database\Eloquent\Collection|' . $returnModel . '[]',
|
//Collection or array of models (because Collection is Arrayable)
|
||||||
true,
|
$this->setProperty(
|
||||||
null
|
$method,
|
||||||
);
|
$this->getCollectionClass($relatedModel) . '|' . $relatedModel . '[]',
|
||||||
} else {
|
true,
|
||||||
//Single model is returned
|
null
|
||||||
$this->setProperty($method, $returnModel, true, null);
|
);
|
||||||
|
} elseif ($relation === "morphTo") {
|
||||||
|
// Model isn't specified because relation is polymorphic
|
||||||
|
$this->setProperty(
|
||||||
|
$method,
|
||||||
|
'\Illuminate\Database\Eloquent\Model|\Eloquent',
|
||||||
|
true,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
//Single model is returned
|
||||||
|
$this->setProperty($method, $relatedModel, true, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -380,7 +411,7 @@ class ModelsCommand extends Command
|
|||||||
* @param bool|null $write
|
* @param bool|null $write
|
||||||
* @param string|null $comment
|
* @param string|null $comment
|
||||||
*/
|
*/
|
||||||
protected function setProperty($name, $type = null, $read = null, $write = null, $comment='')
|
protected function setProperty($name, $type = null, $read = null, $write = null, $comment = '')
|
||||||
{
|
{
|
||||||
if (!isset($this->properties[$name])) {
|
if (!isset($this->properties[$name])) {
|
||||||
$this->properties[$name] = array();
|
$this->properties[$name] = array();
|
||||||
@@ -402,7 +433,9 @@ class ModelsCommand extends Command
|
|||||||
|
|
||||||
protected function setMethod($name, $type = '', $arguments = array())
|
protected function setMethod($name, $type = '', $arguments = array())
|
||||||
{
|
{
|
||||||
if (!isset($this->methods[$name])) {
|
$methods = array_change_key_case($this->methods, CASE_LOWER);
|
||||||
|
|
||||||
|
if (!isset($methods[strtolower($name)])) {
|
||||||
$this->methods[$name] = array();
|
$this->methods[$name] = array();
|
||||||
$this->methods[$name]['type'] = $type;
|
$this->methods[$name]['type'] = $type;
|
||||||
$this->methods[$name]['arguments'] = $arguments;
|
$this->methods[$name]['arguments'] = $arguments;
|
||||||
@@ -454,7 +487,13 @@ class ModelsCommand extends Command
|
|||||||
} else {
|
} else {
|
||||||
$attr = 'property-read';
|
$attr = 'property-read';
|
||||||
}
|
}
|
||||||
$tag = Tag::createInstance("@{$attr} {$property['type']} {$name} {$property['comment']}", $phpdoc);
|
|
||||||
|
if ($this->hasCamelCaseModelProperties()) {
|
||||||
|
$name = Str::camel($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
$tagLine = trim("@{$attr} {$property['type']} {$name} {$property['comment']}");
|
||||||
|
$tag = Tag::createInstance($tagLine, $phpdoc);
|
||||||
$phpdoc->appendTag($tag);
|
$phpdoc->appendTag($tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -467,6 +506,10 @@ class ModelsCommand extends Command
|
|||||||
$phpdoc->appendTag($tag);
|
$phpdoc->appendTag($tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($this->write && ! $phpdoc->getTagsByName('mixin')) {
|
||||||
|
$phpdoc->appendTag(Tag::createInstance("@mixin \\Eloquent", $phpdoc));
|
||||||
|
}
|
||||||
|
|
||||||
$serializer = new DocBlockSerializer();
|
$serializer = new DocBlockSerializer();
|
||||||
$serializer->getDocComment($phpdoc);
|
$serializer->getDocComment($phpdoc);
|
||||||
$docComment = $serializer->getDocComment($phpdoc);
|
$docComment = $serializer->getDocComment($phpdoc);
|
||||||
@@ -474,7 +517,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,12 +528,12 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$output = "namespace {$namespace}{\n{$docComment}\n\tclass {$classname} {}\n}\n\n";
|
$output = "namespace {$namespace}{\n{$docComment}\n\tclass {$classname} extends \Eloquent {}\n}\n\n";
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -505,10 +548,11 @@ class ModelsCommand extends Command
|
|||||||
//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();
|
||||||
|
/** @var \ReflectionParameter $param */
|
||||||
foreach ($method->getParameters() as $param) {
|
foreach ($method->getParameters() as $param) {
|
||||||
$paramStr = '$' . $param->getName();
|
$paramStr = '$' . $param->getName();
|
||||||
$params[] = $paramStr;
|
$params[] = $paramStr;
|
||||||
if ($param->isOptional()) {
|
if ($param->isOptional() && $param->isDefaultValueAvailable()) {
|
||||||
$default = $param->getDefaultValue();
|
$default = $param->getDefaultValue();
|
||||||
if (is_bool($default)) {
|
if (is_bool($default)) {
|
||||||
$default = $default ? 'true' : 'false';
|
$default = $default ? 'true' : 'false';
|
||||||
@@ -529,23 +573,30 @@ class ModelsCommand extends Command
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Determine a model classes' collection type.
|
||||||
|
*
|
||||||
|
* @see http://laravel.com/docs/eloquent-collections#custom-collections
|
||||||
* @param string $className
|
* @param string $className
|
||||||
* @param \Illuminate\Database\Eloquent\Model $model
|
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function getClassName($className, $model)
|
private function getCollectionClass($className)
|
||||||
{
|
{
|
||||||
// If the class name was resolved via get_class($this) or static::class
|
// Return something in the very very unlikely scenario the model doesn't
|
||||||
if (strpos($className, 'get_class($this)') !== false || strpos($className, 'static::class') !== false) {
|
// have a newCollection() method.
|
||||||
return get_class($model);
|
if (!method_exists($className, 'newCollection')) {
|
||||||
|
return '\Illuminate\Database\Eloquent\Collection';
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the class name was resolved via ::class (PHP 5.5+)
|
/** @var \Illuminate\Database\Eloquent\Model $model */
|
||||||
if (strpos($className, '::class') !== false) {
|
$model = new $className;
|
||||||
$end = -1 * strlen('::class');
|
return '\\' . get_class($model->newCollection());
|
||||||
return substr($className, 0, $end);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return "\\" . ltrim(trim($className, " \"'"), "\\") ;
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function hasCamelCaseModelProperties()
|
||||||
|
{
|
||||||
|
return $this->laravel['config']->get('ide-helper.model_camel_case_properties', false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+302
-241
@@ -1,241 +1,302 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Laravel IDE Helper Generator
|
* Laravel IDE Helper Generator
|
||||||
*
|
*
|
||||||
* @author Barry vd. Heuvel <[email protected]>
|
* @author Barry vd. Heuvel <[email protected]>
|
||||||
* @copyright 2014 Barry vd. Heuvel / Fruitcake Studio (http://www.fruitcakestudio.nl)
|
* @copyright 2014 Barry vd. Heuvel / Fruitcake Studio (http://www.fruitcakestudio.nl)
|
||||||
* @license http://www.opensource.org/licenses/mit-license.php MIT
|
* @license http://www.opensource.org/licenses/mit-license.php MIT
|
||||||
* @link https://github.com/barryvdh/laravel-ide-helper
|
* @link https://github.com/barryvdh/laravel-ide-helper
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Barryvdh\LaravelIdeHelper;
|
namespace Barryvdh\LaravelIdeHelper;
|
||||||
|
|
||||||
use Illuminate\Foundation\AliasLoader;
|
use Illuminate\Foundation\Application;
|
||||||
use Illuminate\Config\Repository as ConfigRepository;
|
use Illuminate\Foundation\AliasLoader;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Illuminate\Config\Repository as ConfigRepository;
|
||||||
|
use ReflectionClass;
|
||||||
class Generator
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
{
|
|
||||||
/** @var \Illuminate\Config\Repository */
|
class Generator
|
||||||
protected $config;
|
{
|
||||||
|
/** @var \Illuminate\Config\Repository */
|
||||||
/** @var \Illuminate\View\Factory */
|
protected $config;
|
||||||
protected $view;
|
|
||||||
|
/** @var \Illuminate\View\Factory */
|
||||||
/** @var \Symfony\Component\Console\Output\OutputInterface */
|
protected $view;
|
||||||
protected $output;
|
|
||||||
|
/** @var \Symfony\Component\Console\Output\OutputInterface */
|
||||||
protected $extra = array();
|
protected $output;
|
||||||
protected $magic = array();
|
|
||||||
protected $interfaces = array();
|
protected $extra = array();
|
||||||
protected $helpers;
|
protected $magic = array();
|
||||||
|
protected $interfaces = array();
|
||||||
/**
|
protected $helpers;
|
||||||
* @param \Illuminate\Config\Repository $config
|
|
||||||
* @param \Illuminate\View\Factory $view
|
/**
|
||||||
* @param \Symfony\Component\Console\Output\OutputInterface $output
|
* @param \Illuminate\Config\Repository $config
|
||||||
* @param string $helpers
|
* @param \Illuminate\View\Factory $view
|
||||||
*/
|
* @param \Symfony\Component\Console\Output\OutputInterface $output
|
||||||
public function __construct(/*ConfigRepository */ $config,
|
* @param string $helpers
|
||||||
/* Illuminate\View\Factory */ $view,
|
*/
|
||||||
OutputInterface $output = null,
|
public function __construct(
|
||||||
$helpers = ''
|
/*ConfigRepository */ $config,
|
||||||
) {
|
/* Illuminate\View\Factory */ $view,
|
||||||
$this->config = $config;
|
OutputInterface $output = null,
|
||||||
$this->view = $view;
|
$helpers = ''
|
||||||
|
) {
|
||||||
// Find the drivers to add to the extra/interfaces
|
$this->config = $config;
|
||||||
$this->detectDrivers();
|
$this->view = $view;
|
||||||
|
|
||||||
$this->extra = array_merge($this->extra, $this->config->get('ide-helper.extra'));
|
// Find the drivers to add to the extra/interfaces
|
||||||
$this->magic = array_merge($this->magic, $this->config->get('ide-helper.magic'));
|
$this->detectDrivers();
|
||||||
$this->interfaces = array_merge($this->interfaces, $this->config->get('ide-helper.interfaces'));
|
|
||||||
// Make all interface classes absolute
|
$this->extra = array_merge($this->extra, $this->config->get('ide-helper.extra'));
|
||||||
foreach ($this->interfaces as &$interface) {
|
$this->magic = array_merge($this->magic, $this->config->get('ide-helper.magic'));
|
||||||
$interface = '\\' . ltrim($interface, '\\');
|
$this->interfaces = array_merge($this->interfaces, $this->config->get('ide-helper.interfaces'));
|
||||||
}
|
// Make all interface classes absolute
|
||||||
$this->helpers = $helpers;
|
foreach ($this->interfaces as &$interface) {
|
||||||
}
|
$interface = '\\' . ltrim($interface, '\\');
|
||||||
|
}
|
||||||
/**
|
$this->helpers = $helpers;
|
||||||
* Generate the helper file contents;
|
}
|
||||||
*
|
|
||||||
* @param string $format The format to generate the helper in (php/json)
|
/**
|
||||||
* @return string;
|
* Generate the helper file contents;
|
||||||
*/
|
*
|
||||||
public function generate($format = 'php')
|
* @param string $format The format to generate the helper in (php/json)
|
||||||
{
|
* @return string;
|
||||||
// Check if the generator for this format exists
|
*/
|
||||||
$method = 'generate'.ucfirst($format).'Helper';
|
public function generate($format = 'php')
|
||||||
if (method_exists($this, $method)) {
|
{
|
||||||
return $this->$method();
|
// Check if the generator for this format exists
|
||||||
}
|
$method = 'generate'.ucfirst($format).'Helper';
|
||||||
|
if (method_exists($this, $method)) {
|
||||||
return $this->generatePhpHelper();
|
return $this->$method();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generatePhpHelper()
|
return $this->generatePhpHelper();
|
||||||
{
|
}
|
||||||
$app = app();
|
|
||||||
return $this->view->make('ide-helper::helper')
|
public function generatePhpHelper()
|
||||||
->with('namespaces', $this->getNamespaces())
|
{
|
||||||
->with('helpers', $this->helpers)
|
$app = app();
|
||||||
->with('version', $app::VERSION)
|
return $this->view->make('ide-helper::helper')
|
||||||
->render();
|
->with('namespaces', $this->getNamespaces())
|
||||||
}
|
->with('helpers', $this->helpers)
|
||||||
|
->with('version', $app->version())
|
||||||
public function generateJsonHelper()
|
->render();
|
||||||
{
|
}
|
||||||
$classes = array();
|
|
||||||
foreach ($this->getNamespaces() as $aliases) {
|
public function generateJsonHelper()
|
||||||
foreach($aliases as $alias) {
|
{
|
||||||
$functions = array();
|
$classes = array();
|
||||||
foreach ($alias->getMethods() as $method) {
|
foreach ($this->getNamespaces() as $aliases) {
|
||||||
$functions[$method->getName()] = '('. $method->getParamsWithDefault().')';
|
foreach ($aliases as $alias) {
|
||||||
}
|
$functions = array();
|
||||||
$classes[$alias->getAlias()] = array(
|
foreach ($alias->getMethods() as $method) {
|
||||||
'functions' => $functions,
|
$functions[$method->getName()] = '('. $method->getParamsWithDefault().')';
|
||||||
);
|
}
|
||||||
}
|
$classes[$alias->getAlias()] = array(
|
||||||
}
|
'functions' => $functions,
|
||||||
|
);
|
||||||
$flags = JSON_FORCE_OBJECT;
|
}
|
||||||
if (defined('JSON_PRETTY_PRINT')) {
|
}
|
||||||
$flags |= JSON_PRETTY_PRINT;
|
|
||||||
}
|
$flags = JSON_FORCE_OBJECT;
|
||||||
|
if (defined('JSON_PRETTY_PRINT')) {
|
||||||
return json_encode(array(
|
$flags |= JSON_PRETTY_PRINT;
|
||||||
'php' => array(
|
}
|
||||||
'classes' => $classes,
|
|
||||||
),
|
return json_encode(array(
|
||||||
), $flags);
|
'php' => array(
|
||||||
}
|
'classes' => $classes,
|
||||||
|
),
|
||||||
protected function detectDrivers()
|
), $flags);
|
||||||
{
|
}
|
||||||
try{
|
|
||||||
if (class_exists('Auth') && is_a('Auth', '\Illuminate\Support\Facades\Auth', true)) {
|
protected function detectDrivers()
|
||||||
$class = get_class(\Auth::driver());
|
{
|
||||||
$this->extra['Auth'] = array($class);
|
$defaultUserModel = config('auth.providers.users.model', config('auth.model', 'App\User'));
|
||||||
$this->interfaces['\Illuminate\Auth\UserProviderInterface'] = $class;
|
$this->interfaces['\Illuminate\Contracts\Auth\Authenticatable'] = $defaultUserModel;
|
||||||
}
|
|
||||||
}catch (\Exception $e) {}
|
try {
|
||||||
|
if (class_exists('Auth') && is_a('Auth', '\Illuminate\Support\Facades\Auth', true)) {
|
||||||
try{
|
if (class_exists('\Illuminate\Foundation\Application')) {
|
||||||
if (class_exists('DB') && is_a('DB', '\Illuminate\Support\Facades\DB', true)) {
|
$authMethod = version_compare(Application::VERSION, '5.2', '>=') ? 'guard' : 'driver';
|
||||||
$class = get_class(\DB::connection());
|
} else {
|
||||||
$this->extra['DB'] = array($class);
|
$refClass = new ReflectionClass('\Laravel\Lumen\Application');
|
||||||
$this->interfaces['\Illuminate\Database\ConnectionInterface'] = $class;
|
$versionStr = $refClass->newInstanceWithoutConstructor()->version();
|
||||||
}
|
$authMethod = strpos($versionStr, 'Lumen (5.0') === 0 ?
|
||||||
}catch (\Exception $e) {}
|
'driver' :
|
||||||
|
(strpos($versionStr, 'Lumen (5.1') === 0 ? 'driver' : 'guard');
|
||||||
try{
|
}
|
||||||
if (class_exists('Cache') && is_a('Cache', '\Illuminate\Support\Facades\Cache', true)) {
|
$class = get_class(\Auth::$authMethod());
|
||||||
$driver = get_class(\Cache::driver());
|
$this->extra['Auth'] = array($class);
|
||||||
$store = get_class(\Cache::getStore());
|
$this->interfaces['\Illuminate\Auth\UserProviderInterface'] = $class;
|
||||||
$this->extra['Cache'] = array($driver, $store);
|
}
|
||||||
$this->interfaces['\Illuminate\Cache\StoreInterface'] = $store;
|
} catch (\Exception $e) {
|
||||||
}
|
}
|
||||||
}catch (\Exception $e) {}
|
|
||||||
|
try {
|
||||||
try{
|
if (class_exists('DB') && is_a('DB', '\Illuminate\Support\Facades\DB', true)) {
|
||||||
if (class_exists('Queue') && is_a('Queue', '\Illuminate\Support\Facades\Queue', true)) {
|
$class = get_class(\DB::connection());
|
||||||
$class = get_class(\Queue::connection());
|
$this->extra['DB'] = array($class);
|
||||||
$this->extra['Queue'] = array($class);
|
$this->interfaces['\Illuminate\Database\ConnectionInterface'] = $class;
|
||||||
$this->interfaces['\Illuminate\Queue\QueueInterface'] = $class;
|
}
|
||||||
}
|
} catch (\Exception $e) {
|
||||||
}catch (\Exception $e) {}
|
}
|
||||||
|
|
||||||
try{
|
try {
|
||||||
if (class_exists('SSH') && is_a('SSH', '\Illuminate\Support\Facades\SSH', true)){
|
if (class_exists('Cache') && is_a('Cache', '\Illuminate\Support\Facades\Cache', true)) {
|
||||||
$class = get_class(\SSH::connection());
|
$driver = get_class(\Cache::driver());
|
||||||
$this->extra['SSH'] = array($class);
|
$store = get_class(\Cache::getStore());
|
||||||
$this->interfaces['\Illuminate\Remote\ConnectionInterface'] = $class;
|
$this->extra['Cache'] = array($driver, $store);
|
||||||
}
|
$this->interfaces['\Illuminate\Cache\StoreInterface'] = $store;
|
||||||
}catch (\Exception $e) {}
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
try {
|
||||||
* Find all namespaces/aliases that are valid for us to render
|
if (class_exists('Queue') && is_a('Queue', '\Illuminate\Support\Facades\Queue', true)) {
|
||||||
*
|
$class = get_class(\Queue::connection());
|
||||||
* @return array
|
$this->extra['Queue'] = array($class);
|
||||||
*/
|
$this->interfaces['\Illuminate\Queue\QueueInterface'] = $class;
|
||||||
protected function getNamespaces()
|
}
|
||||||
{
|
} catch (\Exception $e) {
|
||||||
$namespaces = array();
|
}
|
||||||
|
|
||||||
// Get all aliases
|
try {
|
||||||
foreach (AliasLoader::getInstance()->getAliases() as $name => $facade) {
|
if (class_exists('SSH') && is_a('SSH', '\Illuminate\Support\Facades\SSH', true)) {
|
||||||
|
$class = get_class(\SSH::connection());
|
||||||
// Skip the Redis facade, if not available (otherwise Fatal PHP Error)
|
$this->extra['SSH'] = array($class);
|
||||||
if ($facade == 'Illuminate\Support\Facades\Redis' && !class_exists('Predis\Client')) {
|
$this->interfaces['\Illuminate\Remote\ConnectionInterface'] = $class;
|
||||||
continue;
|
}
|
||||||
}
|
} catch (\Exception $e) {
|
||||||
|
}
|
||||||
$magicMethods = array_key_exists($name, $this->magic) ? $this->magic[$name] : array();
|
|
||||||
$alias = new Alias($name, $facade, $magicMethods, $this->interfaces);
|
try {
|
||||||
if ($alias->isValid()) {
|
if (class_exists('Storage') && is_a('Storage', '\Illuminate\Support\Facades\Storage', true)) {
|
||||||
|
$class = get_class(\Storage::disk());
|
||||||
//Add extra methods, from other classes (magic static calls)
|
$this->extra['Storage'] = array($class);
|
||||||
if (array_key_exists($name, $this->extra)) {
|
$this->interfaces['\Illuminate\Contracts\Filesystem\Filesystem'] = $class;
|
||||||
$alias->addClass($this->extra[$name]);
|
}
|
||||||
}
|
} catch (\Exception $e) {
|
||||||
|
}
|
||||||
$namespace = $alias->getNamespace();
|
|
||||||
if (!isset($namespaces[$namespace])) {
|
}
|
||||||
$namespaces[$namespace] = array();
|
|
||||||
}
|
/**
|
||||||
$namespaces[$namespace][] = $alias;
|
* Find all namespaces/aliases that are valid for us to render
|
||||||
}
|
*
|
||||||
|
* @return array
|
||||||
}
|
*/
|
||||||
|
protected function getNamespaces()
|
||||||
return $namespaces;
|
{
|
||||||
}
|
$namespaces = array();
|
||||||
|
|
||||||
/**
|
// Get all aliases
|
||||||
* Get the driver/connection/store from the managers
|
foreach ($this->getAliases() as $name => $facade) {
|
||||||
*
|
// Skip the Redis facade, if not available (otherwise Fatal PHP Error)
|
||||||
* @param $alias
|
if ($facade == 'Illuminate\Support\Facades\Redis' && !class_exists('Predis\Client')) {
|
||||||
* @return array|bool|string
|
continue;
|
||||||
*/
|
}
|
||||||
public function getDriver($alias)
|
|
||||||
{
|
$magicMethods = array_key_exists($name, $this->magic) ? $this->magic[$name] : array();
|
||||||
try {
|
$alias = new Alias($name, $facade, $magicMethods, $this->interfaces);
|
||||||
if ($alias == "Auth") {
|
if ($alias->isValid()) {
|
||||||
$driver = \Auth::driver();
|
//Add extra methods, from other classes (magic static calls)
|
||||||
} elseif ($alias == "DB") {
|
if (array_key_exists($name, $this->extra)) {
|
||||||
$driver = \DB::connection();
|
$alias->addClass($this->extra[$name]);
|
||||||
} elseif ($alias == "Cache") {
|
}
|
||||||
$driver = get_class(\Cache::driver());
|
|
||||||
$store = get_class(\Cache::getStore());
|
$namespace = $alias->getNamespace();
|
||||||
return array($driver, $store);
|
if (!isset($namespaces[$namespace])) {
|
||||||
} elseif ($alias == "Queue") {
|
$namespaces[$namespace] = array();
|
||||||
$driver = \Queue::connection();
|
}
|
||||||
} else {
|
$namespaces[$namespace][] = $alias;
|
||||||
return false;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return get_class($driver);
|
return $namespaces;
|
||||||
} catch (\Exception $e) {
|
}
|
||||||
$this->error("Could not determine driver/connection for $alias.");
|
|
||||||
return false;
|
protected function getAliases()
|
||||||
}
|
{
|
||||||
}
|
// For Laravel, use the AliasLoader
|
||||||
|
if (class_exists('Illuminate\Foundation\AliasLoader')) {
|
||||||
/**
|
return AliasLoader::getInstance()->getAliases();
|
||||||
* Write a string as error output.
|
}
|
||||||
*
|
|
||||||
* @param string $string
|
$facades = [
|
||||||
* @return void
|
'App' => 'Illuminate\Support\Facades\App',
|
||||||
*/
|
'Auth' => 'Illuminate\Support\Facades\Auth',
|
||||||
protected function error($string)
|
'Bus' => 'Illuminate\Support\Facades\Bus',
|
||||||
{
|
'DB' => 'Illuminate\Support\Facades\DB',
|
||||||
if($this->output){
|
'Cache' => 'Illuminate\Support\Facades\Cache',
|
||||||
$this->output->writeln("<error>$string</error>");
|
'Cookie' => 'Illuminate\Support\Facades\Cookie',
|
||||||
}else{
|
'Crypt' => 'Illuminate\Support\Facades\Crypt',
|
||||||
echo $string . "\r\n";
|
'Event' => 'Illuminate\Support\Facades\Event',
|
||||||
}
|
'Hash' => 'Illuminate\Support\Facades\Hash',
|
||||||
}
|
'Log' => 'Illuminate\Support\Facades\Log',
|
||||||
}
|
'Mail' => 'Illuminate\Support\Facades\Mail',
|
||||||
|
'Queue' => 'Illuminate\Support\Facades\Queue',
|
||||||
|
'Request' => 'Illuminate\Support\Facades\Request',
|
||||||
|
'Schema' => 'Illuminate\Support\Facades\Schema',
|
||||||
|
'Session' => 'Illuminate\Support\Facades\Session',
|
||||||
|
'Storage' => 'Illuminate\Support\Facades\Storage',
|
||||||
|
//'Validator' => 'Illuminate\Support\Facades\Validator',
|
||||||
|
];
|
||||||
|
|
||||||
|
$facades = array_merge($facades, $this->config->get('app.aliases', []));
|
||||||
|
|
||||||
|
// 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
|
||||||
|
*
|
||||||
|
* @param $alias
|
||||||
|
* @return array|bool|string
|
||||||
|
*/
|
||||||
|
public function getDriver($alias)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if ($alias == "Auth") {
|
||||||
|
$driver = \Auth::driver();
|
||||||
|
} elseif ($alias == "DB") {
|
||||||
|
$driver = \DB::connection();
|
||||||
|
} elseif ($alias == "Cache") {
|
||||||
|
$driver = get_class(\Cache::driver());
|
||||||
|
$store = get_class(\Cache::getStore());
|
||||||
|
return array($driver, $store);
|
||||||
|
} elseif ($alias == "Queue") {
|
||||||
|
$driver = \Queue::connection();
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return get_class($driver);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->error("Could not determine driver/connection for $alias.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a string as error output.
|
||||||
|
*
|
||||||
|
* @param string $string
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function error($string)
|
||||||
|
{
|
||||||
|
if ($this->output) {
|
||||||
|
$this->output->writeln("<error>$string</error>");
|
||||||
|
} else {
|
||||||
|
echo $string . "\r\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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,15 +61,15 @@ 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']);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->app['command.ide-helper.meta'] = $this->app->share(
|
$this->app['command.ide-helper.meta'] = $this->app->share(
|
||||||
function ($app) {
|
function ($app) {
|
||||||
return new MetaCommand($app['files'], $app['view']);
|
return new MetaCommand($app['files'], $app['view']);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->commands('command.ide-helper.generate', 'command.ide-helper.models', 'command.ide-helper.meta');
|
$this->commands('command.ide-helper.generate', 'command.ide-helper.models', 'command.ide-helper.meta');
|
||||||
@@ -79,5 +84,4 @@ class IdeHelperServiceProvider extends ServiceProvider
|
|||||||
{
|
{
|
||||||
return array('command.ide-helper.generate', 'command.ide-helper.models');
|
return array('command.ide-helper.generate', 'command.ide-helper.models');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+314
-311
@@ -1,311 +1,314 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Laravel IDE Helper Generator
|
* Laravel IDE Helper Generator
|
||||||
*
|
*
|
||||||
* @author Barry vd. Heuvel <[email protected]>
|
* @author Barry vd. Heuvel <[email protected]>
|
||||||
* @copyright 2014 Barry vd. Heuvel / Fruitcake Studio (http://www.fruitcakestudio.nl)
|
* @copyright 2014 Barry vd. Heuvel / Fruitcake Studio (http://www.fruitcakestudio.nl)
|
||||||
* @license http://www.opensource.org/licenses/mit-license.php MIT
|
* @license http://www.opensource.org/licenses/mit-license.php MIT
|
||||||
* @link https://github.com/barryvdh/laravel-ide-helper
|
* @link https://github.com/barryvdh/laravel-ide-helper
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Barryvdh\LaravelIdeHelper;
|
namespace Barryvdh\LaravelIdeHelper;
|
||||||
|
|
||||||
use phpDocumentor\Reflection\DocBlock;
|
use Barryvdh\Reflection\DocBlock;
|
||||||
use phpDocumentor\Reflection\DocBlock\Context;
|
use Barryvdh\Reflection\DocBlock\Context;
|
||||||
use phpDocumentor\Reflection\DocBlock\Tag;
|
use Barryvdh\Reflection\DocBlock\Tag;
|
||||||
use phpDocumentor\Reflection\DocBlock\Tag\ReturnTag;
|
use Barryvdh\Reflection\DocBlock\Tag\ReturnTag;
|
||||||
use phpDocumentor\Reflection\DocBlock\Tag\ParamTag;
|
use Barryvdh\Reflection\DocBlock\Tag\ParamTag;
|
||||||
use phpDocumentor\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
||||||
|
|
||||||
class Method
|
class Method
|
||||||
{
|
{
|
||||||
/** @var \phpDocumentor\Reflection\DocBlock */
|
/** @var \Barryvdh\Reflection\DocBlock */
|
||||||
protected $phpdoc;
|
protected $phpdoc;
|
||||||
|
|
||||||
/** @var \ReflectionMethod */
|
/** @var \ReflectionMethod */
|
||||||
protected $method;
|
protected $method;
|
||||||
|
|
||||||
protected $output = '';
|
protected $output = '';
|
||||||
protected $name;
|
protected $name;
|
||||||
protected $namespace;
|
protected $namespace;
|
||||||
protected $params = array();
|
protected $params = array();
|
||||||
protected $params_with_default = array();
|
protected $params_with_default = array();
|
||||||
protected $interfaces = array();
|
protected $interfaces = array();
|
||||||
protected $return = null;
|
protected $return = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \ReflectionMethod $method
|
* @param \ReflectionMethod $method
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
* @param string $class
|
* @param \ReflectionClass $class
|
||||||
* @param string|null $methodName
|
* @param string|null $methodName
|
||||||
* @param array $interfaces
|
* @param array $interfaces
|
||||||
*/
|
*/
|
||||||
public function __construct(\ReflectionMethod $method, $alias, $class, $methodName = null, $interfaces = array())
|
public function __construct(\ReflectionMethod $method, $alias, $class, $methodName = null, $interfaces = array())
|
||||||
{
|
{
|
||||||
$this->method = $method;
|
$this->method = $method;
|
||||||
$this->interfaces = $interfaces;
|
$this->interfaces = $interfaces;
|
||||||
$this->name = $methodName ?: $method->name;
|
$this->name = $methodName ?: $method->name;
|
||||||
$this->namespace = $method->getDeclaringClass()->getNamespaceName();
|
$this->namespace = $method->getDeclaringClass()->getNamespaceName();
|
||||||
|
|
||||||
//Create a DocBlock and serializer instance
|
//Create a DocBlock and serializer instance
|
||||||
$this->phpdoc = new DocBlock($method, new Context($this->namespace));
|
$this->phpdoc = new DocBlock($method, new Context($this->namespace));
|
||||||
|
|
||||||
//Normalize the description and inherit the docs from parents/interfaces
|
//Normalize the description and inherit the docs from parents/interfaces
|
||||||
try {
|
try {
|
||||||
$this->normalizeParams($this->phpdoc);
|
$this->normalizeParams($this->phpdoc);
|
||||||
$this->normalizeReturn($this->phpdoc);
|
$this->normalizeReturn($this->phpdoc);
|
||||||
$this->normalizeDescription($this->phpdoc);
|
$this->normalizeDescription($this->phpdoc);
|
||||||
} catch (\Exception $e) {}
|
} catch (\Exception $e) {
|
||||||
|
}
|
||||||
//Get the parameters, including formatted default values
|
|
||||||
$this->getParameters($method);
|
//Get the parameters, including formatted default values
|
||||||
|
$this->getParameters($method);
|
||||||
//Make the method static
|
|
||||||
$this->phpdoc->appendTag(Tag::createInstance('@static', $this->phpdoc));
|
//Make the method static
|
||||||
|
$this->phpdoc->appendTag(Tag::createInstance('@static', $this->phpdoc));
|
||||||
//Reference the 'real' function in the declaringclass
|
|
||||||
$declaringClass = $method->getDeclaringClass();
|
//Reference the 'real' function in the declaringclass
|
||||||
$this->declaringClassName = '\\' . ltrim($declaringClass->name, '\\');
|
$declaringClass = $method->getDeclaringClass();
|
||||||
$this->root = '\\' . ltrim($class->getName(), '\\');
|
$this->declaringClassName = '\\' . ltrim($declaringClass->name, '\\');
|
||||||
}
|
$this->root = '\\' . ltrim($class->getName(), '\\');
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Get the class wherein the function resides
|
/**
|
||||||
*
|
* Get the class wherein the function resides
|
||||||
* @return string
|
*
|
||||||
*/
|
* @return string
|
||||||
public function getDeclaringClass()
|
*/
|
||||||
{
|
public function getDeclaringClass()
|
||||||
return $this->declaringClassName;
|
{
|
||||||
}
|
return $this->declaringClassName;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Return the class from which this function would be called
|
/**
|
||||||
*
|
* Return the class from which this function would be called
|
||||||
* @return string
|
*
|
||||||
*/
|
* @return string
|
||||||
public function getRoot()
|
*/
|
||||||
{
|
public function getRoot()
|
||||||
return $this->root;
|
{
|
||||||
}
|
return $this->root;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Get the docblock for this method
|
/**
|
||||||
*
|
* Get the docblock for this method
|
||||||
* @param string $prefix
|
*
|
||||||
* @return mixed
|
* @param string $prefix
|
||||||
*/
|
* @return mixed
|
||||||
public function getDocComment($prefix = "\t\t")
|
*/
|
||||||
{
|
public function getDocComment($prefix = "\t\t")
|
||||||
$serializer = new DocBlockSerializer(1, $prefix);
|
{
|
||||||
return $serializer->getDocComment($this->phpdoc);
|
$serializer = new DocBlockSerializer(1, $prefix);
|
||||||
}
|
return $serializer->getDocComment($this->phpdoc);
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Get the method name
|
/**
|
||||||
*
|
* Get the method name
|
||||||
* @return string
|
*
|
||||||
*/
|
* @return string
|
||||||
public function getName()
|
*/
|
||||||
{
|
public function getName()
|
||||||
return $this->name;
|
{
|
||||||
}
|
return $this->name;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Get the parameters for this method
|
/**
|
||||||
*
|
* Get the parameters for this method
|
||||||
* @param bool $implode Wether to implode the array or not
|
*
|
||||||
* @return string
|
* @param bool $implode Wether to implode the array or not
|
||||||
*/
|
* @return string
|
||||||
public function getParams($implode = true)
|
*/
|
||||||
{
|
public function getParams($implode = true)
|
||||||
return $implode ? implode(', ', $this->params) : $this->params;
|
{
|
||||||
}
|
return $implode ? implode(', ', $this->params) : $this->params;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Get the parameters for this method including default values
|
/**
|
||||||
*
|
* Get the parameters for this method including default values
|
||||||
* @param bool $implode Wether to implode the array or not
|
*
|
||||||
* @return string
|
* @param bool $implode Wether to implode the array or not
|
||||||
*/
|
* @return string
|
||||||
public function getParamsWithDefault($implode = true)
|
*/
|
||||||
{
|
public function getParamsWithDefault($implode = true)
|
||||||
return $implode ? implode(', ', $this->params_with_default) : $this->params_with_default;
|
{
|
||||||
}
|
return $implode ? implode(', ', $this->params_with_default) : $this->params_with_default;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Get the description and get the inherited docs.
|
/**
|
||||||
*
|
* Get the description and get the inherited docs.
|
||||||
* @param DocBlock $phpdoc
|
*
|
||||||
*/
|
* @param DocBlock $phpdoc
|
||||||
protected function normalizeDescription(DocBlock $phpdoc)
|
*/
|
||||||
{
|
protected function normalizeDescription(DocBlock $phpdoc)
|
||||||
//Get the short + long description from the DocBlock
|
{
|
||||||
$description = $phpdoc->getText();
|
//Get the short + long description from the DocBlock
|
||||||
|
$description = $phpdoc->getText();
|
||||||
//Loop through parents/interfaces, to fill in {@inheritdoc}
|
|
||||||
if (strpos($description, '{@inheritdoc}') !== false) {
|
//Loop through parents/interfaces, to fill in {@inheritdoc}
|
||||||
$inheritdoc = $this->getInheritDoc($this->method);
|
if (strpos($description, '{@inheritdoc}') !== false) {
|
||||||
$inheritDescription = $inheritdoc->getText();
|
$inheritdoc = $this->getInheritDoc($this->method);
|
||||||
|
$inheritDescription = $inheritdoc->getText();
|
||||||
$description = str_replace('{@inheritdoc}', $inheritDescription, $description);
|
|
||||||
$phpdoc->setText($description);
|
$description = str_replace('{@inheritdoc}', $inheritDescription, $description);
|
||||||
|
$phpdoc->setText($description);
|
||||||
$this->normalizeParams($inheritdoc);
|
|
||||||
$this->normalizeReturn($inheritdoc);
|
$this->normalizeParams($inheritdoc);
|
||||||
|
$this->normalizeReturn($inheritdoc);
|
||||||
//Add the tags that are inherited
|
|
||||||
$inheritTags = $inheritdoc->getTags();
|
//Add the tags that are inherited
|
||||||
if ($inheritTags) {
|
$inheritTags = $inheritdoc->getTags();
|
||||||
/** @var Tag $tag */
|
if ($inheritTags) {
|
||||||
foreach ($inheritTags as $tag) {
|
/** @var Tag $tag */
|
||||||
$tag->setDocBlock();
|
foreach ($inheritTags as $tag) {
|
||||||
$phpdoc->appendTag($tag);
|
$tag->setDocBlock();
|
||||||
}
|
$phpdoc->appendTag($tag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Normalize the parameters
|
/**
|
||||||
*
|
* Normalize the parameters
|
||||||
* @param DocBlock $phpdoc
|
*
|
||||||
*/
|
* @param DocBlock $phpdoc
|
||||||
protected function normalizeParams(DocBlock $phpdoc)
|
*/
|
||||||
{
|
protected function normalizeParams(DocBlock $phpdoc)
|
||||||
//Get the return type and adjust them for beter autocomplete
|
{
|
||||||
$paramTags = $phpdoc->getTagsByName('param');
|
//Get the return type and adjust them for beter autocomplete
|
||||||
if ($paramTags) {
|
$paramTags = $phpdoc->getTagsByName('param');
|
||||||
/** @var ParamTag $tag */
|
if ($paramTags) {
|
||||||
foreach($paramTags as $tag){
|
/** @var ParamTag $tag */
|
||||||
// Convert the keywords
|
foreach ($paramTags as $tag) {
|
||||||
$content = $this->convertKeywords($tag->getContent());
|
// Convert the keywords
|
||||||
$tag->setContent($content);
|
$content = $this->convertKeywords($tag->getContent());
|
||||||
|
$tag->setContent($content);
|
||||||
// Get the expanded type and re-set the content
|
|
||||||
$content = $tag->getType() . ' ' . $tag->getVariableName() . ' ' . $tag->getDescription();
|
// Get the expanded type and re-set the content
|
||||||
$tag->setContent(trim($content));
|
$content = $tag->getType() . ' ' . $tag->getVariableName() . ' ' . $tag->getDescription();
|
||||||
}
|
$tag->setContent(trim($content));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Normalize the return tag (make full namespace, replace interfaces)
|
/**
|
||||||
*
|
* Normalize the return tag (make full namespace, replace interfaces)
|
||||||
* @param DocBlock $phpdoc
|
*
|
||||||
*/
|
* @param DocBlock $phpdoc
|
||||||
protected function normalizeReturn(DocBlock $phpdoc)
|
*/
|
||||||
{
|
protected function normalizeReturn(DocBlock $phpdoc)
|
||||||
//Get the return type and adjust them for beter autocomplete
|
{
|
||||||
$returnTags = $phpdoc->getTagsByName('return');
|
//Get the return type and adjust them for beter autocomplete
|
||||||
if ($returnTags) {
|
$returnTags = $phpdoc->getTagsByName('return');
|
||||||
/** @var ReturnTag $tag */
|
if ($returnTags) {
|
||||||
$tag = reset($returnTags);
|
/** @var ReturnTag $tag */
|
||||||
// Get the expanded type
|
$tag = reset($returnTags);
|
||||||
$returnValue = $tag->getType();
|
// Get the expanded type
|
||||||
|
$returnValue = $tag->getType();
|
||||||
// Replace the interfaces
|
|
||||||
foreach($this->interfaces as $interface => $real){
|
// Replace the interfaces
|
||||||
$returnValue = str_replace($interface, $real, $returnValue);
|
foreach ($this->interfaces as $interface => $real) {
|
||||||
}
|
$returnValue = str_replace($interface, $real, $returnValue);
|
||||||
|
}
|
||||||
// Set the changed content
|
|
||||||
$tag->setContent($returnValue . ' ' . $tag->getDescription());
|
// Set the changed content
|
||||||
$this->return = $returnValue;
|
$tag->setContent($returnValue . ' ' . $tag->getDescription());
|
||||||
}else{
|
$this->return = $returnValue;
|
||||||
$this->return = null;
|
} else {
|
||||||
}
|
$this->return = null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Convert keywwords that are incorrect.
|
/**
|
||||||
*
|
* Convert keywwords that are incorrect.
|
||||||
* @param string $string
|
*
|
||||||
* @return string
|
* @param string $string
|
||||||
*/
|
* @return string
|
||||||
protected function convertKeywords($string)
|
*/
|
||||||
{
|
protected function convertKeywords($string)
|
||||||
$string = str_replace('\Closure', 'Closure', $string);
|
{
|
||||||
$string = str_replace('Closure', '\Closure', $string);
|
$string = str_replace('\Closure', 'Closure', $string);
|
||||||
$string = str_replace('dynamic', 'mixed', $string);
|
$string = str_replace('Closure', '\Closure', $string);
|
||||||
|
$string = str_replace('dynamic', 'mixed', $string);
|
||||||
return $string;
|
|
||||||
}
|
return $string;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Should the function return a value?
|
/**
|
||||||
*
|
* Should the function return a value?
|
||||||
* @return bool
|
*
|
||||||
*/
|
* @return bool
|
||||||
public function shouldReturn()
|
*/
|
||||||
{
|
public function shouldReturn()
|
||||||
if($this->return !== "void" && $this->method->name !== "__construct"){
|
{
|
||||||
return true;
|
if ($this->return !== "void" && $this->method->name !== "__construct") {
|
||||||
}
|
return true;
|
||||||
|
}
|
||||||
return false;
|
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Get the parameters and format them correctly
|
/**
|
||||||
*
|
* Get the parameters and format them correctly
|
||||||
* @param $method
|
*
|
||||||
* @return array
|
* @param \ReflectionMethod $method
|
||||||
*/
|
* @return array
|
||||||
public function getParameters($method)
|
*/
|
||||||
{
|
public function getParameters($method)
|
||||||
//Loop through the default values for paremeters, and make the correct output string
|
{
|
||||||
$params = array();
|
//Loop through the default values for paremeters, and make the correct output string
|
||||||
$paramsWithDefault = array();
|
$params = array();
|
||||||
foreach ($method->getParameters() as $param) {
|
$paramsWithDefault = array();
|
||||||
$paramStr = '$' . $param->getName();
|
foreach ($method->getParameters() as $param) {
|
||||||
$params[] = $paramStr;
|
$paramStr = '$' . $param->getName();
|
||||||
if ($param->isOptional()) {
|
$params[] = $paramStr;
|
||||||
$default = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null;
|
if ($param->isOptional()) {
|
||||||
if (is_bool($default)) {
|
$default = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null;
|
||||||
$default = $default ? 'true' : 'false';
|
if (is_bool($default)) {
|
||||||
} elseif (is_array($default)) {
|
$default = $default ? 'true' : 'false';
|
||||||
$default = 'array()';
|
} elseif (is_array($default)) {
|
||||||
} elseif (is_null($default)) {
|
$default = 'array()';
|
||||||
$default = 'null';
|
} elseif (is_null($default)) {
|
||||||
} elseif (is_int($default)) {
|
$default = 'null';
|
||||||
//$default = $default;
|
} elseif (is_int($default)) {
|
||||||
} else {
|
//$default = $default;
|
||||||
$default = "'" . trim($default) . "'";
|
} elseif (is_resource($default)) {
|
||||||
}
|
//skip to not fail
|
||||||
$paramStr .= " = $default";
|
} else {
|
||||||
}
|
$default = "'" . trim($default) . "'";
|
||||||
$paramsWithDefault[] = $paramStr;
|
}
|
||||||
}
|
$paramStr .= " = $default";
|
||||||
|
}
|
||||||
$this->params = $params;
|
$paramsWithDefault[] = $paramStr;
|
||||||
$this->params_with_default = $paramsWithDefault;
|
}
|
||||||
}
|
|
||||||
|
$this->params = $params;
|
||||||
/**
|
$this->params_with_default = $paramsWithDefault;
|
||||||
* @param \ReflectionMethod $reflectionMethod
|
}
|
||||||
* @return DocBlock
|
|
||||||
*/
|
/**
|
||||||
protected function getInheritDoc($reflectionMethod)
|
* @param \ReflectionMethod $reflectionMethod
|
||||||
{
|
* @return DocBlock
|
||||||
$parentClass = $reflectionMethod->getDeclaringClass()->getParentClass();
|
*/
|
||||||
|
protected function getInheritDoc($reflectionMethod)
|
||||||
//Get either a parent or the interface
|
{
|
||||||
if ($parentClass) {
|
$parentClass = $reflectionMethod->getDeclaringClass()->getParentClass();
|
||||||
$method = $parentClass->getMethod($reflectionMethod->getName());
|
|
||||||
} else {
|
//Get either a parent or the interface
|
||||||
$method = $reflectionMethod->getPrototype();
|
if ($parentClass) {
|
||||||
}
|
$method = $parentClass->getMethod($reflectionMethod->getName());
|
||||||
if ($method) {
|
} else {
|
||||||
$namespace = $method->getDeclaringClass()->getNamespaceName();
|
$method = $reflectionMethod->getPrototype();
|
||||||
$phpdoc = new DocBlock($method, new Context($namespace));
|
}
|
||||||
|
if ($method) {
|
||||||
if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) {
|
$namespace = $method->getDeclaringClass()->getNamespaceName();
|
||||||
//Not at the end yet, try another parent/interface..
|
$phpdoc = new DocBlock($method, new Context($namespace));
|
||||||
return $this->getInheritDoc($method);
|
|
||||||
} else {
|
if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) {
|
||||||
return $phpdoc;
|
//Not at the end yet, try another parent/interface..
|
||||||
}
|
return $this->getInheritDoc($method);
|
||||||
}
|
} else {
|
||||||
}
|
return $phpdoc;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper;
|
||||||
|
|
||||||
|
class ExampleTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Test that we can actually instantiate the class
|
||||||
|
*/
|
||||||
|
public function testCanInstantiate()
|
||||||
|
{
|
||||||
|
$reflectionClass = new \ReflectionClass(ExampleClass::class);
|
||||||
|
$reflectionMethod = $reflectionClass->getMethod('setName');
|
||||||
|
|
||||||
|
$method = new Method($reflectionMethod, 'Example', $reflectionClass);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Method::class, $method);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the output of a class
|
||||||
|
*/
|
||||||
|
public function testOutput()
|
||||||
|
{
|
||||||
|
$reflectionClass = new \ReflectionClass(ExampleClass::class);
|
||||||
|
$reflectionMethod = $reflectionClass->getMethod('setName');
|
||||||
|
|
||||||
|
$method = new Method($reflectionMethod, 'Example', $reflectionClass);
|
||||||
|
|
||||||
|
$output = '/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param string $last
|
||||||
|
* @param string $first
|
||||||
|
* @static
|
||||||
|
*/';
|
||||||
|
$this->assertEquals($output, $method->getDocComment(''));
|
||||||
|
$this->assertEquals('setName', $method->getName());
|
||||||
|
$this->assertEquals('\\'.ExampleClass::class, $method->getDeclaringClass());
|
||||||
|
$this->assertEquals('$last, $first', $method->getParams(true));
|
||||||
|
$this->assertEquals(['$last', '$first'], $method->getParams(false));
|
||||||
|
$this->assertEquals('$last, $first = \'Barry\'', $method->getParamsWithDefault(true));
|
||||||
|
$this->assertEquals(['$last', '$first = \'Barry\''], $method->getParamsWithDefault(false));
|
||||||
|
$this->assertEquals(true, $method->shouldReturn());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ExampleClass
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string $last
|
||||||
|
* @param string $first
|
||||||
|
*/
|
||||||
|
public function setName($last, $first = 'Barry')
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user