mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-22 12:03:08 +00:00
Add dynamic whereField()
Fixes #61 Also added backslash to method return types and use full Str facade, not an alias.
This commit is contained in:
@@ -3,13 +3,15 @@
|
|||||||
* Laravel IDE Helper Generator
|
* Laravel IDE Helper Generator
|
||||||
*
|
*
|
||||||
* @author Barry vd. Heuvel <[email protected]>
|
* @author Barry vd. Heuvel <[email protected]>
|
||||||
* @copyright 2013 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\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
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\ClassLoader\ClassMapGenerator;
|
use Symfony\Component\ClassLoader\ClassMapGenerator;
|
||||||
@@ -17,6 +19,7 @@ use phpDocumentor\Reflection\DocBlock;
|
|||||||
use phpDocumentor\Reflection\DocBlock\Context;
|
use phpDocumentor\Reflection\DocBlock\Context;
|
||||||
use phpDocumentor\Reflection\DocBlock\Tag;
|
use phpDocumentor\Reflection\DocBlock\Tag;
|
||||||
use phpDocumentor\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
use phpDocumentor\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A command to generate autocomplete information for your IDE
|
* A command to generate autocomplete information for your IDE
|
||||||
*
|
*
|
||||||
@@ -216,7 +219,7 @@ class ModelsCommand extends Command {
|
|||||||
$type = 'integer';
|
$type = 'integer';
|
||||||
break;
|
break;
|
||||||
case 'decimal':
|
case 'decimal':
|
||||||
case 'float':
|
case 'float':
|
||||||
$type = 'float';
|
$type = 'float';
|
||||||
break;
|
break;
|
||||||
case 'boolean':
|
case 'boolean':
|
||||||
@@ -231,6 +234,7 @@ class ModelsCommand extends Command {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$this->setProperty($name, $type, true, true);
|
$this->setProperty($name, $type, true, true);
|
||||||
|
$this->setMethod(Str::camel("where_".$name), '\\'.get_class($model), array('$value'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -241,64 +245,64 @@ class ModelsCommand extends Command {
|
|||||||
protected function getPropertiesFromMethods($model){
|
protected function getPropertiesFromMethods($model){
|
||||||
$methods = get_class_methods($model);
|
$methods = get_class_methods($model);
|
||||||
if($methods){
|
if($methods){
|
||||||
foreach($methods as $method){
|
foreach($methods as $method){
|
||||||
if(\Str::startsWith($method, 'get') && \Str::endsWith($method, 'Attribute') && $method !== 'getAttribute'){
|
if(Str::startsWith($method, 'get') && Str::endsWith($method, 'Attribute') && $method !== 'getAttribute'){
|
||||||
//Magic get<name>Attribute
|
//Magic get<name>Attribute
|
||||||
$name = \Str::snake(substr($method, 3, -9));
|
$name = Str::snake(substr($method, 3, -9));
|
||||||
if(!empty($name)){
|
if(!empty($name)){
|
||||||
$this->setProperty($name, null, true, null);
|
$this->setProperty($name, null, true, null);
|
||||||
}
|
}
|
||||||
}elseif(\Str::startsWith($method, 'set') && \Str::endsWith($method, 'Attribute') && $method !== 'setAttribute'){
|
}elseif(Str::startsWith($method, 'set') && Str::endsWith($method, 'Attribute') && $method !== 'setAttribute'){
|
||||||
//Magic set<name>Attribute
|
//Magic set<name>Attribute
|
||||||
$name = \Str::snake(substr($method, 3, -9));
|
$name = Str::snake(substr($method, 3, -9));
|
||||||
if(!empty($name)){
|
if(!empty($name)){
|
||||||
$this->setProperty($name, null, null, true);
|
$this->setProperty($name, null, null, true);
|
||||||
}
|
}
|
||||||
}elseif(\Str::startsWith($method, 'scope') && $method !== 'scopeQuery'){
|
}elseif(Str::startsWith($method, 'scope') && $method !== 'scopeQuery'){
|
||||||
//Magic set<name>Attribute
|
//Magic set<name>Attribute
|
||||||
$name = \Str::camel(substr($method, 5));
|
$name = Str::camel(substr($method, 5));
|
||||||
if(!empty($name)){
|
if(!empty($name)){
|
||||||
$reflection = new \ReflectionMethod($model, $method);
|
$reflection = new \ReflectionMethod($model, $method);
|
||||||
$args = $this->getParameters($reflection);
|
$args = $this->getParameters($reflection);
|
||||||
//Remove the first ($query) argument
|
//Remove the first ($query) argument
|
||||||
array_shift($args);
|
array_shift($args);
|
||||||
$this->setMethod($name, $reflection->class, $args);
|
$this->setMethod($name, '\\'.$reflection->class, $args);
|
||||||
}
|
}
|
||||||
}elseif(!method_exists('Eloquent', $method) && !\Str::startsWith($method, 'get')){
|
}elseif(!method_exists('Eloquent', $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);
|
||||||
|
|
||||||
$file = new \SplFileObject($reflection->getFileName());
|
$file = new \SplFileObject($reflection->getFileName());
|
||||||
$file->seek($reflection->getStartLine() - 1);
|
$file->seek($reflection->getStartLine() - 1);
|
||||||
|
|
||||||
$code = '';
|
$code = '';
|
||||||
while ($file->key() < $reflection->getEndLine())
|
while ($file->key() < $reflection->getEndLine())
|
||||||
{
|
{
|
||||||
$code .= $file->current(); $file->next();
|
$code .= $file->current(); $file->next();
|
||||||
}
|
}
|
||||||
$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('hasMany', 'belongsToMany', 'hasOne', 'belongsTo') as $relation){
|
foreach(array('hasMany', 'belongsToMany', 'hasOne', 'belongsTo') as $relation){
|
||||||
$search = '$this->'.$relation.'(';
|
$search = '$this->'.$relation.'(';
|
||||||
if($pos = stripos($code, $search)){
|
if($pos = stripos($code, $search)){
|
||||||
$code = substr($code, $pos + strlen($search));
|
$code = substr($code, $pos + strlen($search));
|
||||||
$arguments = explode(',', substr($code, 0, stripos($code, ')')));
|
$arguments = explode(',', substr($code, 0, stripos($code, ')')));
|
||||||
//Remove quotes, ensure 1 \ in front of the model
|
//Remove quotes, ensure 1 \ in front of the model
|
||||||
$returnModel = "\\".ltrim(trim($arguments[0], " \"'"), "\\");
|
$returnModel = "\\".ltrim(trim($arguments[0], " \"'"), "\\");
|
||||||
if($relation === "belongsToMany" or $relation === 'hasMany'){
|
if($relation === "belongsToMany" or $relation === 'hasMany'){
|
||||||
//Collection or array of models (because Collection is Arrayable)
|
//Collection or array of models (because Collection is Arrayable)
|
||||||
$this->setProperty($method, '\Illuminate\Database\Eloquent\Collection|'.$returnModel.'[]', true, null);
|
$this->setProperty($method, '\Illuminate\Database\Eloquent\Collection|'.$returnModel.'[]', true, null);
|
||||||
}else{
|
}else{
|
||||||
//Single model is returned
|
//Single model is returned
|
||||||
$this->setProperty($method, $returnModel, true, null);
|
$this->setProperty($method, $returnModel, true, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user