mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57:13 +00:00
Add custom collection support
Add support for models that override Eloquent's newCollection() with a custom collection type. Do this by executing the relationship on the model then executing that relationship's newCollection() method to see what type of colletion class is returned. Remove the now-unused Barryvdh\LaravelIdeHelper\Console\ModelsCommand::getClassName() method.
This commit is contained in:
@@ -367,23 +367,20 @@ class ModelsCommand extends Command
|
|||||||
$code = substr($code, $pos + strlen($search));
|
$code = substr($code, $pos + strlen($search));
|
||||||
$end = strpos($code, ')->') ?:strpos($code, ');');
|
$end = strpos($code, ')->') ?:strpos($code, ');');
|
||||||
if (false !== $end) {
|
if (false !== $end) {
|
||||||
$arguments = substr($code, 0, $end);
|
//Resolve the relation's model to a fully-qualified class name.
|
||||||
$arguments = array_map(function($item) {
|
$relatedModel = '\\' . get_class($model->$method()->getRelated());
|
||||||
return trim($item, ' \'\"');
|
|
||||||
}, explode(',', $arguments));
|
|
||||||
//Remove quotes, ensure 1 \ in front of the model
|
|
||||||
$returnModel = $this->getClassName($arguments[0], $model);
|
|
||||||
if ($relation === "belongsToMany" or $relation === 'hasMany' or $relation === 'morphMany' or $relation === 'morphToMany') {
|
if ($relation === "belongsToMany" or $relation === 'hasMany' or $relation === 'morphMany' or $relation === 'morphToMany') {
|
||||||
//Collection or array of models (because Collection is Arrayable)
|
//Collection or array of models (because Collection is Arrayable)
|
||||||
$this->setProperty(
|
$this->setProperty(
|
||||||
$method,
|
$method,
|
||||||
'\Illuminate\Database\Eloquent\Collection|' . $returnModel . '[]',
|
$this->getCollectionClass($relatedModel) . '|' . $relatedModel . '[]',
|
||||||
true,
|
true,
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
//Single model is returned
|
//Single model is returned
|
||||||
$this->setProperty($method, $returnModel, true, null);
|
$this->setProperty($method, $relatedModel, true, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -550,23 +547,22 @@ 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, " \"'"), "\\") ;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user