Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Queue commands #38471

Closed
wants to merge 10 commits into from
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ public function firstOrFail($key = null, $operator = null, $value = null)
? $this->operatorForWhere(...func_get_args())
: $key;

$items = $this->when($filter)->filter($filter);
$items = $this->unless($filter == null)->filter($filter);

if ($items->isEmpty()) {
throw new ItemNotFoundException;
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ public function firstOrFail($key = null, $operator = null, $value = null)
: $key;

return $this
->when($filter)
->unless($filter == null)
->filter($filter)
->take(1)
->collect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,9 +731,7 @@ public function getMorphClass()
return array_search(static::class, $morphMap, true);
}

return Relation::$useTableNamesForMorphMap
? $this->getTable()
: static::class;
return static::class;
}

/**
Expand Down
17 changes: 0 additions & 17 deletions src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ abstract class Relation implements BuilderContract
*/
protected static $constraints = true;

/**
* Indicates that table names should be used when determining morph classes.
*
* @var bool
*/
public static $useTableNamesForMorphMap = false;

/**
* An array to map class names to their morph names in the database.
*
Expand Down Expand Up @@ -394,16 +387,6 @@ protected function whereInMethod(Model $model, $key)
: 'whereIn';
}

/**
* Indicate that the table names should be used when determining morphed class names.
*
* @return void
*/
public static function morphUsingTableNames()
{
static::$useTableNamesForMorphMap = true;
}

/**
* Set or get the morph map for polymorphic relations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ protected function registerQueueListenCommand()
*/
protected function registerQueueMonitorCommand()
{
$this->app->singleton('command.queue.monitor', function ($app) {
$this->app->singleton(QueueMonitorCommand::class, function ($app) {
return new QueueMonitorCommand($app['queue'], $app['events']);
});
}
Expand All @@ -705,7 +705,7 @@ protected function registerQueuePruneBatchesCommand()
*/
protected function registerQueuePruneFailedJobsCommand()
{
$this->app->singleton('command.queue.prune-failed-jobs', function () {
$this->app->singleton(QueuePruneFailedJobsCommand::class, function () {
return new QueuePruneFailedJobsCommand;
});
}
Expand Down
9 changes: 9 additions & 0 deletions src/Illuminate/Queue/Console/MonitorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ class MonitorCommand extends Command
{queues : The names of the queues to monitor}
{--max=1000 : The maximum number of jobs that can be on the queue before an event is dispatched}';

/**
* The name of the console command.
*
* This name is used to identify the command during lazy loading.
*
* @var string|null
*/
protected static $defaultName = 'queue:monitor';

/**
* The console command description.
*
Expand Down
9 changes: 9 additions & 0 deletions src/Illuminate/Queue/Console/PruneFailedJobsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ class PruneFailedJobsCommand extends Command
protected $signature = 'queue:prune-failed
{--hours=24 : The number of hours to retain failed jobs data}';

/**
* The name of the console command.
*
* This name is used to identify the command during lazy loading.
*
* @var string|null
*/
protected static $defaultName = 'queue:prune-failed';

/**
* The console command description.
*
Expand Down
21 changes: 20 additions & 1 deletion src/Illuminate/Validation/ConditionalRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,26 @@ class ConditionalRules
*/
protected $rules;

/**
* The rules to be added to the attribute if the condition fails.
*
* @var array|string
*/
protected $defaultRules;

/**
* Create a new conditional rules instance.
*
* @param callable|bool $condition
* @param array|string $rules
* @param array|string $defaultRules
* @return void
*/
public function __construct($condition, $rules)
public function __construct($condition, $rules, $defaultRules = [])
{
$this->condition = $condition;
$this->rules = $rules;
$this->defaultRules = $defaultRules;
}

/**
Expand All @@ -55,4 +64,14 @@ public function rules()
{
return is_string($this->rules) ? explode('|', $this->rules) : $this->rules;
}

/**
* Get the default rules.
*
* @return array
*/
public function defaultRules()
{
return is_string($this->defaultRules) ? explode('|', $this->defaultRules) : $this->defaultRules;
}
}
5 changes: 3 additions & 2 deletions src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ class Rule
*
* @param callable|bool $condition
* @param array|string $rules
* @param array|string $defaultRules
* @return \Illuminate\Validation\ConditionalRules
*/
public static function when($condition, $rules)
public static function when($condition, $rules, $defaultRules = [])
{
return new ConditionalRules($condition, $rules);
return new ConditionalRules($condition, $rules, $defaultRules);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Validation/ValidationRuleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,17 @@ public static function filterConditionalRules($rules, array $data = [])
}

if ($attributeRules instanceof ConditionalRules) {
return [$attribute => $attributeRules->passes($data) ? $attributeRules->rules() : null];
return [$attribute => $attributeRules->passes($data)
? $attributeRules->rules()
: $attributeRules->defaultRules(), ];
}

return [$attribute => collect($attributeRules)->map(function ($rule) use ($data) {
if (! $rule instanceof ConditionalRules) {
return [$rule];
}

return $rule->passes($data) ? $rule->rules() : null;
return $rule->passes($data) ? $rule->rules() : $rule->defaultRules();
})->filter()->flatten(1)->values()->all()];
})->filter()->all();
}
Expand Down
10 changes: 0 additions & 10 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1217,16 +1217,6 @@ public function testMorphOneCreatesProperRelation()
$this->assertEquals(EloquentModelStub::class, $relation->getMorphClass());
}

public function testMorphOneCreatesProperRelationWhenUsingTableNames()
{
Relation::morphUsingTableNames();
$model = new EloquentModelStub;
$this->addMockConnection($model);
$relation = $model->morphOne(EloquentModelSaveStub::class, 'morph');
$this->assertEquals('stub', $relation->getMorphClass());
Relation::$useTableNamesForMorphMap = false;
}

public function testCorrectMorphClassIsReturned()
{
Relation::morphMap(['alias' => 'AnotherModel']);
Expand Down
19 changes: 19 additions & 0 deletions tests/Validation/ValidationRuleParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,23 @@ public function test_conditional_rules_are_properly_expanded_and_filtered()
'city' => ['required', 'min:2'],
], $rules);
}

public function test_conditional_rules_with_default()
{
$rules = ValidationRuleParser::filterConditionalRules([
'name' => Rule::when(true, ['required', 'min:2'], ['string', 'max:10']),
'email' => Rule::when(false, ['required', 'min:2'], ['string', 'max:10']),
'password' => Rule::when(false, 'required|min:2', 'string|max:10'),
'username' => ['required', Rule::when(true, ['min:2'], ['string', 'max:10'])],
'address' => ['required', Rule::when(false, ['min:2'], ['string', 'max:10'])],
]);

$this->assertEquals([
'name' => ['required', 'min:2'],
'email' => ['string', 'max:10'],
'password' => ['string', 'max:10'],
'username' => ['required', 'min:2'],
'address' => ['required', 'string', 'max:10'],
], $rules);
}
}