Skip to content

Commit 0bc4977

Browse files
committed
Merge branch '8.x' into throttles_exceptions_with_redis
2 parents 35071ab + 5da5b44 commit 0bc4977

File tree

22 files changed

+1496
-25
lines changed

22 files changed

+1496
-25
lines changed

src/Illuminate/Collections/helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ function last($array)
179179
* @param mixed $value
180180
* @return mixed
181181
*/
182-
function value($value)
182+
function value($value, ...$args)
183183
{
184-
return $value instanceof Closure ? $value() : $value;
184+
return $value instanceof Closure ? $value(...$args) : $value;
185185
}
186186
}

src/Illuminate/Database/Console/Seeds/SeedCommand.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Console\ConfirmableTrait;
77
use Illuminate\Database\ConnectionResolverInterface as Resolver;
88
use Illuminate\Database\Eloquent\Model;
9+
use Symfony\Component\Console\Input\InputArgument;
910
use Symfony\Component\Console\Input\InputOption;
1011

1112
class SeedCommand extends Command
@@ -81,7 +82,7 @@ public function handle()
8182
*/
8283
protected function getSeeder()
8384
{
84-
$class = $this->input->getOption('class');
85+
$class = $this->input->getArgument('class') ?? $this->input->getOption('class');
8586

8687
if (strpos($class, '\\') === false) {
8788
$class = 'Database\\Seeders\\'.$class;
@@ -109,6 +110,18 @@ protected function getDatabase()
109110
return $database ?: $this->laravel['config']['database.default'];
110111
}
111112

113+
/**
114+
* Get the console command arguments.
115+
*
116+
* @return array
117+
*/
118+
protected function getArguments()
119+
{
120+
return [
121+
['class', InputArgument::OPTIONAL, 'The class name of the root seeder', null],
122+
];
123+
}
124+
112125
/**
113126
* Get the console command options.
114127
*

src/Illuminate/Foundation/Support/Providers/EventServiceProvider.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function discoverEvents()
119119
->reduce(function ($discovered, $directory) {
120120
return array_merge_recursive(
121121
$discovered,
122-
DiscoverEvents::within($directory, base_path())
122+
DiscoverEvents::within($directory, $this->eventDiscoveryBasePath())
123123
);
124124
}, []);
125125
}
@@ -135,4 +135,14 @@ protected function discoverEventsWithin()
135135
$this->app->path('Listeners'),
136136
];
137137
}
138+
139+
/**
140+
* Get the base path to be used during event discovery.
141+
*
142+
* @return string
143+
*/
144+
protected function eventDiscoveryBasePath()
145+
{
146+
return base_path();
147+
}
138148
}

src/Illuminate/Queue/Console/WorkCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class WorkCommand extends Command
3333
{--force : Force the worker to run even in maintenance mode}
3434
{--memory=128 : The memory limit in megabytes}
3535
{--sleep=3 : Number of seconds to sleep when no job is available}
36+
{--rest=0 : Number of seconds to rest between jobs}
3637
{--timeout=60 : The number of seconds a child process can run}
3738
{--tries=1 : Number of times to attempt a job before logging it failed}';
3839

@@ -134,7 +135,8 @@ protected function gatherWorkerOptions()
134135
$this->option('force'),
135136
$this->option('stop-when-empty'),
136137
$this->option('max-jobs'),
137-
$this->option('max-time')
138+
$this->option('max-time'),
139+
$this->option('rest')
138140
);
139141
}
140142

src/Illuminate/Queue/Middleware/ThrottlesExceptions.php

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
class ThrottlesExceptions
1010
{
11+
/**
12+
* The developer specified key that the rate limiter should use.
13+
*
14+
* @var string
15+
*/
16+
protected $key;
17+
1118
/**
1219
* The maximum number of attempts allowed before rate limiting applies.
1320
*
@@ -27,14 +34,7 @@ class ThrottlesExceptions
2734
*
2835
* @var int
2936
*/
30-
protected $retryAfterMinutes;
31-
32-
/**
33-
* The rate limiter key.
34-
*
35-
* @var string
36-
*/
37-
protected $key;
37+
protected $retryAfterMinutes = 0;
3838

3939
/**
4040
* The callback that determines if rate limiting should apply.
@@ -48,7 +48,7 @@ class ThrottlesExceptions
4848
*
4949
* @var string
5050
*/
51-
protected $prefix = 'circuit_breaker:';
51+
protected $prefix = 'laravel_throttles_exceptions:';
5252

5353
/**
5454
* The rate limiter instance.
@@ -62,15 +62,13 @@ class ThrottlesExceptions
6262
*
6363
* @param int $maxAttempts
6464
* @param int $decayMinutes
65-
* @param int $retryAfterMinutes
6665
* @param string $key
66+
* @return void
6767
*/
68-
public function __construct($maxAttempts = 10, $decayMinutes = 10, $retryAfterMinutes = 0, string $key = '')
68+
public function __construct($maxAttempts = 10, $decayMinutes = 10)
6969
{
7070
$this->maxAttempts = $maxAttempts;
7171
$this->decayMinutes = $decayMinutes;
72-
$this->retryAfterMinutes = $retryAfterMinutes;
73-
$this->key = $key;
7472
}
7573

7674
/**
@@ -129,6 +127,19 @@ public function withPrefix(string $prefix)
129127
return $this;
130128
}
131129

130+
/**
131+
* Specify the number of seconds a job should be delayed when it is released (before it has reached its max exceptions).
132+
*
133+
* @param int $backoff
134+
* @return $this
135+
*/
136+
public function backoff($backoff)
137+
{
138+
$this->retryAfterMinutes = $backoff;
139+
140+
return $this;
141+
}
142+
132143
/**
133144
* Get the cache key associated for the rate limiter.
134145
*
@@ -137,7 +148,20 @@ public function withPrefix(string $prefix)
137148
*/
138149
protected function getKey($job)
139150
{
140-
return $this->prefix.md5(empty($this->key) ? get_class($job) : $this->key);
151+
return $this->key ? $this->prefix.$this->key : $this->prefix.$job->job->uuid();
152+
}
153+
154+
/**
155+
* Set the value that the rate limiter should be keyed by.
156+
*
157+
* @param string $key
158+
* @return $this
159+
*/
160+
public function by($key)
161+
{
162+
$this->key = $key;
163+
164+
return $this;
141165
}
142166

143167
/**

src/Illuminate/Queue/Worker.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ public function daemon($connectionName, $queue, WorkerOptions $options)
156156
$jobsProcessed++;
157157

158158
$this->runJob($job, $connectionName, $options);
159+
160+
if ($options->rest > 0) {
161+
$this->sleep($options->rest);
162+
}
159163
} else {
160164
$this->sleep($options->sleep);
161165
}

src/Illuminate/Queue/WorkerOptions.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ class WorkerOptions
3939
*/
4040
public $sleep;
4141

42+
/**
43+
* The number of seconds to rest between jobs.
44+
*
45+
* @var int
46+
*/
47+
public $rest;
48+
4249
/**
4350
* The maximum amount of times a job may be attempted.
4451
*
@@ -87,14 +94,16 @@ class WorkerOptions
8794
* @param bool $stopWhenEmpty
8895
* @param int $maxJobs
8996
* @param int $maxTime
97+
* @param int $rest
9098
* @return void
9199
*/
92100
public function __construct($name = 'default', $backoff = 0, $memory = 128, $timeout = 60, $sleep = 3, $maxTries = 1,
93-
$force = false, $stopWhenEmpty = false, $maxJobs = 0, $maxTime = 0)
101+
$force = false, $stopWhenEmpty = false, $maxJobs = 0, $maxTime = 0, $rest = 0)
94102
{
95103
$this->name = $name;
96104
$this->backoff = $backoff;
97105
$this->sleep = $sleep;
106+
$this->rest = $rest;
98107
$this->force = $force;
99108
$this->memory = $memory;
100109
$this->timeout = $timeout;
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace Illuminate\Testing\Fluent;
4+
5+
use Closure;
6+
use Illuminate\Contracts\Support\Arrayable;
7+
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Traits\Macroable;
9+
use Illuminate\Support\Traits\Tappable;
10+
use Illuminate\Testing\AssertableJsonString;
11+
use PHPUnit\Framework\Assert as PHPUnit;
12+
13+
class AssertableJson implements Arrayable
14+
{
15+
use Concerns\Has,
16+
Concerns\Matching,
17+
Concerns\Debugging,
18+
Concerns\Interaction,
19+
Macroable,
20+
Tappable;
21+
22+
/**
23+
* The properties in the current scope.
24+
*
25+
* @var array
26+
*/
27+
private $props;
28+
29+
/**
30+
* The "dot" path to the current scope.
31+
*
32+
* @var string|null
33+
*/
34+
private $path;
35+
36+
/**
37+
* Create a new fluent, assertable JSON data instance.
38+
*
39+
* @param array $props
40+
* @param string|null $path
41+
* @return void
42+
*/
43+
protected function __construct(array $props, string $path = null)
44+
{
45+
$this->path = $path;
46+
$this->props = $props;
47+
}
48+
49+
/**
50+
* Compose the absolute "dot" path to the given key.
51+
*
52+
* @param string $key
53+
* @return string
54+
*/
55+
protected function dotPath(string $key): string
56+
{
57+
if (is_null($this->path)) {
58+
return $key;
59+
}
60+
61+
return implode('.', [$this->path, $key]);
62+
}
63+
64+
/**
65+
* Retrieve a prop within the current scope using "dot" notation.
66+
*
67+
* @param string|null $key
68+
* @return mixed
69+
*/
70+
protected function prop(string $key = null)
71+
{
72+
return Arr::get($this->props, $key);
73+
}
74+
75+
/**
76+
* Instantiate a new "scope" at the path of the given key.
77+
*
78+
* @param string $key
79+
* @param \Closure $callback
80+
* @return $this
81+
*/
82+
protected function scope(string $key, Closure $callback): self
83+
{
84+
$props = $this->prop($key);
85+
$path = $this->dotPath($key);
86+
87+
PHPUnit::assertIsArray($props, sprintf('Property [%s] is not scopeable.', $path));
88+
89+
$scope = new self($props, $path);
90+
$callback($scope);
91+
$scope->interacted();
92+
93+
return $this;
94+
}
95+
96+
/**
97+
* Create a new instance from an array.
98+
*
99+
* @param array $data
100+
* @return static
101+
*/
102+
public static function fromArray(array $data): self
103+
{
104+
return new self($data);
105+
}
106+
107+
/**
108+
* Create a new instance from a AssertableJsonString.
109+
*
110+
* @param \Illuminate\Testing\AssertableJsonString $json
111+
* @return static
112+
*/
113+
public static function fromAssertableJsonString(AssertableJsonString $json): self
114+
{
115+
return self::fromArray($json->json());
116+
}
117+
118+
/**
119+
* Get the instance as an array.
120+
*
121+
* @return array
122+
*/
123+
public function toArray()
124+
{
125+
return $this->props;
126+
}
127+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Illuminate\Testing\Fluent\Concerns;
4+
5+
trait Debugging
6+
{
7+
/**
8+
* Dumps the given props.
9+
*
10+
* @param string|null $prop
11+
* @return $this
12+
*/
13+
public function dump(string $prop = null): self
14+
{
15+
dump($this->prop($prop));
16+
17+
return $this;
18+
}
19+
20+
/**
21+
* Dumps the given props and exits.
22+
*
23+
* @param string|null $prop
24+
* @return void
25+
*/
26+
public function dd(string $prop = null): void
27+
{
28+
dd($this->prop($prop));
29+
}
30+
31+
/**
32+
* Retrieve a prop within the current scope using "dot" notation.
33+
*
34+
* @param string|null $key
35+
* @return mixed
36+
*/
37+
abstract protected function prop(string $key = null);
38+
}

0 commit comments

Comments
 (0)