Skip to content

Commit

Permalink
fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Sep 24, 2020
2 parents 0c3f039 + b1d96b2 commit ac6587c
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 48 deletions.
42 changes: 36 additions & 6 deletions Console/Factories/FactoryMakeCommand.php
Expand Up @@ -62,11 +62,18 @@ protected function buildClass($name)
{
$namespaceModel = $this->option('model')
? $this->qualifyModel($this->option('model'))
: $this->qualifyModel('Model');
: $this->qualifyModel($this->guessModelName($name));

$model = class_basename($namespaceModel);

if (Str::startsWith($namespaceModel, 'App\\Models')) {
$namespace = Str::beforeLast('Database\\Factories\\'.Str::after($namespaceModel, 'App\\Models\\'), '\\');
} else {
$namespace = 'Database\\Factories';
}

$replace = [
'{{ factoryNamespace }}' => $namespace,
'NamespacedDummyModel' => $namespaceModel,
'{{ namespacedModel }}' => $namespaceModel,
'{{namespacedModel}}' => $namespaceModel,
Expand All @@ -88,13 +95,36 @@ protected function buildClass($name)
*/
protected function getPath($name)
{
$name = str_replace(
['\\', '/'], '', $this->argument('name')
);
$name = Str::replaceFirst('App\\', '', $name);

$name = Str::finish($this->argument('name'), 'Factory');

return $this->laravel->databasePath().'/factories/'.str_replace('\\', '/', $name).'.php';
}

/**
* Guess the model name from the Factory name or return a default model name.
*
* @param string $name
* @return string
*/
protected function guessModelName($name)
{
if (Str::endsWith($name, 'Factory')) {
$name = substr($name, 0, -7);
}

$modelName = $this->qualifyModel(class_basename($name));

if (class_exists($modelName)) {
return $modelName;
}

$name = Str::finish($name, 'Factory');
if (is_dir(app_path('Models/'))) {
return 'App\Models\Model';
}

return $this->laravel->databasePath()."/factories/{$name}.php";
return 'App\Model';
}

/**
Expand Down
3 changes: 1 addition & 2 deletions Console/Factories/stubs/factory.stub
@@ -1,9 +1,8 @@
<?php

namespace Database\Factories;
namespace {{ factoryNamespace }};

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use {{ namespacedModel }};

class {{ model }}Factory extends Factory
Expand Down
2 changes: 2 additions & 0 deletions Console/Migrations/FreshCommand.php
Expand Up @@ -48,6 +48,7 @@ public function handle()
'--database' => $database,
'--path' => $this->input->getOption('path'),
'--realpath' => $this->input->getOption('realpath'),
'--schema-path' => $this->input->getOption('schema-path'),
'--force' => true,
'--step' => $this->option('step'),
]));
Expand Down Expand Up @@ -98,6 +99,7 @@ protected function getOptions()
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to be executed'],
['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
['schema-path', null, InputOption::VALUE_OPTIONAL, 'The path to a schema dump file'],
['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run'],
['seeder', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder'],
['step', null, InputOption::VALUE_NONE, 'Force the migrations to be run so they can be rolled back individually'],
Expand Down
4 changes: 1 addition & 3 deletions Console/Migrations/MigrateCommand.php
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Events\SchemaLoaded;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Database\SQLiteConnection;
use Illuminate\Database\SqlServerConnection;

class MigrateCommand extends BaseCommand
Expand Down Expand Up @@ -127,8 +126,7 @@ protected function loadSchemaState()
// First, we will make sure that the connection supports schema loading and that
// the schema file exists before we proceed any further. If not, we will just
// continue with the standard migration operation as normal without errors.
if ($connection instanceof SQLiteConnection ||
$connection instanceof SqlServerConnection ||
if ($connection instanceof SqlServerConnection ||
! is_file($path = $this->schemaPath($connection))) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions Eloquent/Builder.php
Expand Up @@ -790,7 +790,7 @@ public function forceCreate(array $attributes)
}

/**
* Update a record in the database.
* Update records in the database.
*
* @param array $values
* @return int
Expand Down Expand Up @@ -862,7 +862,7 @@ protected function addUpdatedAtColumn(array $values)
}

/**
* Delete a record from the database.
* Delete records from the database.
*
* @return mixed
*/
Expand Down
9 changes: 6 additions & 3 deletions Eloquent/Concerns/HasAttributes.php
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use InvalidArgumentException;
use LogicException;

trait HasAttributes
Expand Down Expand Up @@ -915,11 +916,13 @@ protected function asDateTime($value)
// Finally, we will just assume this date is in the format used by default on
// the database connection and use that format to create the Carbon object
// that is returned back out to the developers after we convert it here.
if (Date::hasFormat($value, $format)) {
return Date::createFromFormat($format, $value);
try {
$date = Date::createFromFormat($format, $value);
} catch (InvalidArgumentException $e) {
$date = false;
}

return Date::parse($value);
return $date ?: Date::parse($value);
}

/**
Expand Down
41 changes: 36 additions & 5 deletions Eloquent/Factories/Factory.php
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Faker\Generator;
use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -125,6 +126,7 @@ public function __construct($count = null,
$this->afterMaking = $afterMaking ?: new Collection;
$this->afterCreating = $afterCreating ?: new Collection;
$this->connection = $connection;
$this->faker = $this->withFaker();
}

/**
Expand Down Expand Up @@ -166,6 +168,18 @@ public function configure()
return $this;
}

/**
* Get the raw attributes generated by the factory.
*
* @param array $attributes
* @param \Illuminate\Database\Eloquent\Model|null $parent
* @return array
*/
public function raw($attributes = [], ?Model $parent = null)
{
return $this->state($attributes)->getExpandedAttributes($parent);
}

/**
* Create a single model and persist it to the database.
*
Expand All @@ -177,6 +191,21 @@ public function createOne($attributes = [])
return $this->count(null)->create($attributes);
}

/**
* Create a collection of models and persist them to the database.
*
* @param iterable $records
* @return \Illuminate\Database\Eloquent\Collection|mixed
*/
public function createMany(iterable $records)
{
return new EloquentCollection(
array_map(function ($record) {
return $this->state($record)->create();
}, $records)
);
}

/**
* Create a collection of models and persist them to the database.
*
Expand Down Expand Up @@ -318,8 +347,6 @@ protected function getExpandedAttributes(?Model $parent)
*/
protected function getRawAttributes(?Model $parent)
{
$this->faker = $this->withFaker();

return $this->states->pipe(function ($states) {
return $this->for->isEmpty() ? $states : new Collection(array_merge([function () {
return $this->parentResolvers();
Expand Down Expand Up @@ -678,9 +705,13 @@ public function __call($method, $parameters)

$relationship = Str::camel(Str::substr($method, 3));

$factory = static::factoryForModel(
get_class($this->newModel()->{$relationship}()->getRelated())
);
$relatedModel = get_class($this->newModel()->{$relationship}()->getRelated());

if (method_exists($relatedModel, 'newFactory')) {
$factory = $relatedModel::newFactory() ?: static::factoryForModel($relatedModel);
} else {
$factory = static::factoryForModel($relatedModel);
}

if (Str::startsWith($method, 'for')) {
return $this->for($factory->state($parameters[0] ?? []), $relationship);
Expand Down
14 changes: 13 additions & 1 deletion Eloquent/Factories/HasFactory.php
Expand Up @@ -13,8 +13,20 @@ trait HasFactory
*/
public static function factory($count = null, $state = [])
{
return Factory::factoryForModel(get_called_class())
$factory = static::newFactory() ?: Factory::factoryForModel(get_called_class());

return $factory
->count(is_numeric($count) ? $count : null)
->state(is_callable($count) || is_array($count) ? $count : $state);
}

/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
//
}
}
11 changes: 8 additions & 3 deletions Eloquent/Relations/MorphTo.php
Expand Up @@ -136,19 +136,24 @@ protected function getResultsByType($type)
$whereIn = $this->whereInMethod($instance, $ownerKey);

return $query->{$whereIn}(
$instance->getTable().'.'.$ownerKey, $this->gatherKeysByType($type)
$instance->getTable().'.'.$ownerKey, $this->gatherKeysByType($type, $instance->getKeyType())
)->get();
}

/**
* Gather all of the foreign keys for a given type.
*
* @param string $type
* @param string $keyType
* @return array
*/
protected function gatherKeysByType($type)
protected function gatherKeysByType($type, $keyType)
{
return array_keys($this->dictionary[$type]);
return $keyType !== 'string'
? array_keys($this->dictionary[$type])
: array_map(function ($modelId) {
return (string) $modelId;
}, array_keys($this->dictionary[$type]));
}

/**
Expand Down
30 changes: 25 additions & 5 deletions Query/Builder.php
Expand Up @@ -618,6 +618,26 @@ public function crossJoin($table, $first = null, $operator = null, $second = nul
return $this;
}

/**
* Add a subquery cross join to the query.
*
* @param \Closure|\Illuminate\Database\Query\Builder|string $query
* @param string $as
* @return $this
*/
public function crossJoinSub($query, $as)
{
[$query, $bindings] = $this->createSub($query);

$expression = '('.$query.') as '.$this->grammar->wrapTable($as);

$this->addBinding($bindings, 'join');

$this->joins[] = $this->newJoinClause($this, 'cross', new Expression($expression));

return $this;
}

/**
* Get a new join clause.
*
Expand Down Expand Up @@ -1088,7 +1108,7 @@ public function whereNotNull($columns, $boolean = 'and')
/**
* Add a where between statement to the query.
*
* @param string $column
* @param string|\Illuminate\Database\Query\Expression $column
* @param array $values
* @param string $boolean
* @param bool $not
Expand Down Expand Up @@ -2770,7 +2790,7 @@ protected function onceWithColumns($columns, $callback)
}

/**
* Insert a new record into the database.
* Insert new records into the database.
*
* @param array $values
* @return bool
Expand Down Expand Up @@ -2809,7 +2829,7 @@ public function insert(array $values)
}

/**
* Insert a new record into the database while ignoring errors.
* Insert new records into the database while ignoring errors.
*
* @param array $values
* @return int
Expand Down Expand Up @@ -2869,7 +2889,7 @@ public function insertUsing(array $columns, $query)
}

/**
* Update a record in the database.
* Update records in the database.
*
* @param array $values
* @return int
Expand Down Expand Up @@ -2950,7 +2970,7 @@ public function decrement($column, $amount = 1, array $extra = [])
}

/**
* Delete a record from the database.
* Delete records from the database.
*
* @param mixed $id
* @return int
Expand Down
4 changes: 2 additions & 2 deletions SQLiteConnection.php
Expand Up @@ -7,8 +7,8 @@
use Illuminate\Database\Query\Processors\SQLiteProcessor;
use Illuminate\Database\Schema\Grammars\SQLiteGrammar as SchemaGrammar;
use Illuminate\Database\Schema\SQLiteBuilder;
use Illuminate\Database\Schema\SqliteSchemaState;
use Illuminate\Filesystem\Filesystem;
use RuntimeException;

class SQLiteConnection extends Connection
{
Expand Down Expand Up @@ -80,7 +80,7 @@ protected function getDefaultSchemaGrammar()
*/
public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
{
throw new RuntimeException('Schema dumping is not supported when using SQLite.');
return new SqliteSchemaState($this, $files, $processFactory);
}

/**
Expand Down

0 comments on commit ac6587c

Please sign in to comment.