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:
Barry vd. Heuvel
2014-02-21 11:51:39 +01:00
parent 46558e9060
commit 512574de9a
@@ -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
* *
@@ -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'));
} }
} }
} }
@@ -242,29 +246,29 @@ class ModelsCommand extends Command {
$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);