Inject filesystem

This commit is contained in:
Barry vd. Heuvel
2015-08-10 20:36:00 +02:00
parent 99953adb73
commit e7ee934bb1
+32 -27
View File
@@ -12,6 +12,7 @@ namespace Barryvdh\LaravelIdeHelper\Console;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@@ -28,6 +29,10 @@ use phpDocumentor\Reflection\DocBlock\Serializer as DocBlockSerializer;
*/ */
class ModelsCommand extends Command class ModelsCommand extends Command
{ {
/**
* @var Filesystem $files
*/
protected $files;
/** /**
* The console command name. * The console command name.
@@ -50,7 +55,14 @@ class ModelsCommand extends Command
protected $dirs = array(); protected $dirs = array();
protected $reset; protected $reset;
/**
* @param Filesystem $files
*/
public function __construct(Filesystem $files)
{
parent::__construct();
$this->files = $files;
}
/** /**
* Execute the console command. * Execute the console command.
* *
@@ -81,7 +93,7 @@ class ModelsCommand extends Command
$content = $this->generateDocs($model, $ignore); $content = $this->generateDocs($model, $ignore);
if (!$this->write) { if (!$this->write) {
$written = \File::put($filename, $content); $written = $this->files->put($filename, $content);
if ($written !== false) { if ($written !== false) {
$this->info("Model information was written to $filename"); $this->info("Model information was written to $filename");
} else { } else {
@@ -181,7 +193,6 @@ 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.");
} }
@@ -336,7 +347,6 @@ 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);
@@ -352,29 +362,24 @@ class ModelsCommand extends Command
$search = '$this->' . $relation . '('; $search = '$this->' . $relation . '(';
if ($pos = stripos($code, $search)) { if ($pos = stripos($code, $search)) {
$code = substr($code, $pos + strlen($search)); $code = substr($code, $pos + strlen($search));
$end = strpos($code, ')->') ?:strpos($code, ');'); $arguments = explode(',', substr($code, 0, strpos($code, ')')));
if (false !== $end) { //Remove quotes, ensure 1 \ in front of the model
$arguments = substr($code, 0, $end); $returnModel = $this->getClassName($arguments[0], $model);
$arguments = array_map(function($item) { if ($relation === "belongsToMany" or $relation === 'hasMany' or $relation === 'morphMany' or $relation === 'morphToMany') {
return trim($item, ' \'\"'); //Collection or array of models (because Collection is Arrayable)
}, explode(',', $arguments)); $this->setProperty(
//Remove quotes, ensure 1 \ in front of the model $method,
$returnModel = $this->getClassName($arguments[0], $model); '\Illuminate\Database\Eloquent\Collection|' . $returnModel . '[]',
if ($relation === "belongsToMany" or $relation === 'hasMany' or $relation === 'morphMany' or $relation === 'morphToMany') { true,
//Collection or array of models (because Collection is Arrayable) null
$this->setProperty( );
$method, } else {
'\Illuminate\Database\Eloquent\Collection|' . $returnModel . '[]', //Single model is returned
true, $this->setProperty($method, $returnModel, true, null);
null
);
} else {
//Single model is returned
$this->setProperty($method, $returnModel, true, null);
}
} }
} }
} }
} }
} }
} }
@@ -481,7 +486,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 {
@@ -492,7 +497,7 @@ class ModelsCommand extends Command
$contents = substr_replace($contents, $replace, $pos, strlen($needle)); $contents = substr_replace($contents, $replace, $pos, strlen($needle));
} }
} }
if (\File::put($filename, $contents)) { if ($this->files->put($filename, $contents)) {
$this->info('Written new phpDocBlock to ' . $filename); $this->info('Written new phpDocBlock to ' . $filename);
} }
} }
@@ -544,7 +549,7 @@ class ModelsCommand extends Command
{ {
// If the class name was resolved via get_class($this) or static::class // If the class name was resolved via get_class($this) or static::class
if (strpos($className, 'get_class($this)') !== false || strpos($className, 'static::class') !== false) { if (strpos($className, 'get_class($this)') !== false || strpos($className, 'static::class') !== false) {
return "\\" . get_class($model); return get_class($model);
} }
// If the class name was resolved via ::class (PHP 5.5+) // If the class name was resolved via ::class (PHP 5.5+)