Skip to content

Commit

Permalink
Merge branch '11.x' of github.com:laravel/framework into 11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 13, 2025
2 parents 60e183e + 85a02ca commit bdac8cf
Show file tree
Hide file tree
Showing 27 changed files with 411 additions and 134 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Cache/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\PostgresConnection;
use Illuminate\Database\QueryException;
use Illuminate\Database\SQLiteConnection;
use Illuminate\Database\SqlServerConnection;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -487,7 +488,7 @@ protected function serialize($value)
{
$result = serialize($value);

if ($this->connection instanceof PostgresConnection && str_contains($result, "\0")) {
if (($this->connection instanceof PostgresConnection || $this->connection instanceof SQLiteConnection) && str_contains($result, "\0")) {
$result = base64_encode($result);
}

Expand All @@ -502,7 +503,7 @@ protected function serialize($value)
*/
protected function unserialize($value)
{
if ($this->connection instanceof PostgresConnection && ! Str::contains($value, [':', ';'])) {
if (($this->connection instanceof PostgresConnection || $this->connection instanceof SQLiteConnection) && ! Str::contains($value, [':', ';'])) {
$value = base64_decode($value);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;

use function Illuminate\Support\artisan_binary;
use function Illuminate\Support\php_binary;

class Application extends SymfonyApplication implements ApplicationContract
Expand Down Expand Up @@ -95,7 +96,7 @@ public static function phpBinary()
*/
public static function artisanBinary()
{
return ProcessUtils::escapeArgument(defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan');
return ProcessUtils::escapeArgument(artisan_binary());
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Illuminate/Contracts/Pipeline/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
interface Pipeline
{
/**
* Set the traveler object being sent on the pipeline.
* Set the object being sent through the pipeline.
*
* @param mixed $traveler
* @param mixed $passable
* @return $this
*/
public function send($traveler);
public function send($passable);

/**
* Set the stops of the pipeline.
* Set the array of pipes.
*
* @param dynamic|array $stops
* @param array|mixed $pipes
* @return $this
*/
public function through($stops);
public function through($pipes);

/**
* Set the method to call on the stops.
* Set the method to call on the pipes.
*
* @param string $method
* @return $this
Expand Down
18 changes: 12 additions & 6 deletions src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ trait HasRelationships
/**
* Get the dynamic relation resolver if defined or inherited, or return null.
*
* @param string $class
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @param class-string<TRelatedModel> $class
* @param string $key
* @return mixed
* @return Closure|null
*/
public function relationResolver($class, $key)
{
Expand Down Expand Up @@ -851,8 +853,10 @@ public function getMorphClass()
/**
* Create a new model instance for a related model.
*
* @param string $class
* @return mixed
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @param class-string<TRelatedModel> $class
* @return TRelatedModel
*/
protected function newRelatedInstance($class)
{
Expand All @@ -866,8 +870,10 @@ protected function newRelatedInstance($class)
/**
* Create a new model instance for a related "through" model.
*
* @param string $class
* @return mixed
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @param class-string<TRelatedModel> $class
* @return TRelatedModel
*/
protected function newRelatedThroughInstance($class)
{
Expand Down
11 changes: 10 additions & 1 deletion src/Illuminate/Database/Events/MigrationsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@ abstract class MigrationsEvent implements MigrationEventContract
*/
public $method;

/**
* The options provided when the migration method was invoked.
*
* @var array<string, mixed>
*/
public $options;

/**
* Create a new event instance.
*
* @param string $method
* @param array<string, mixed> $options
* @return void
*/
public function __construct($method)
public function __construct($method, array $options = [])
{
$this->method = $method;
$this->options = $options;
}
}
8 changes: 4 additions & 4 deletions src/Illuminate/Database/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function runPending(array $migrations, array $options = [])

$step = $options['step'] ?? false;

$this->fireMigrationEvent(new MigrationsStarted('up'));
$this->fireMigrationEvent(new MigrationsStarted('up', $options));

$this->write(Info::class, 'Running migrations.');

Expand All @@ -184,7 +184,7 @@ public function runPending(array $migrations, array $options = [])
}
}

$this->fireMigrationEvent(new MigrationsEnded('up'));
$this->fireMigrationEvent(new MigrationsEnded('up', $options));

$this->output?->writeln('');
}
Expand Down Expand Up @@ -278,7 +278,7 @@ protected function rollbackMigrations(array $migrations, $paths, array $options)

$this->requireFiles($files = $this->getMigrationFiles($paths));

$this->fireMigrationEvent(new MigrationsStarted('down'));
$this->fireMigrationEvent(new MigrationsStarted('down', $options));

$this->write(Info::class, 'Rolling back migrations.');

Expand All @@ -302,7 +302,7 @@ protected function rollbackMigrations(array $migrations, $paths, array $options)
);
}

$this->fireMigrationEvent(new MigrationsEnded('down'));
$this->fireMigrationEvent(new MigrationsEnded('down', $options));

return $rolledBack;
}
Expand Down
70 changes: 70 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3561,6 +3561,17 @@ public function count($columns = '*')
return (int) $this->aggregate(__FUNCTION__, Arr::wrap($columns));
}

/**
* Retrieve the "count" of the distinct results of a given column for each group.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $columns
* @return \Illuminate\Support\Collection
*/
public function countByGroup($columns = '*')
{
return $this->aggregateByGroup('count', Arr::wrap($columns));
}

/**
* Retrieve the minimum value of a given column.
*
Expand All @@ -3572,6 +3583,17 @@ public function min($column)
return $this->aggregate(__FUNCTION__, [$column]);
}

/**
* Retrieve the minimum value of a given column by group.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @return \Illuminate\Support\Collection
*/
public function minByGroup($column)
{
return $this->aggregateByGroup('min', [$column]);
}

/**
* Retrieve the maximum value of a given column.
*
Expand All @@ -3583,6 +3605,17 @@ public function max($column)
return $this->aggregate(__FUNCTION__, [$column]);
}

/**
* Retrieve the maximum value of a given column by group.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @return \Illuminate\Support\Collection
*/
public function maxByGroup($column)
{
return $this->aggregateByGroup('max', [$column]);
}

/**
* Retrieve the sum of the values of a given column.
*
Expand All @@ -3596,6 +3629,17 @@ public function sum($column)
return $result ?: 0;
}

/**
* Retrieve the sum of the values of a given column by group.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @return \Illuminate\Support\Collection
*/
public function sumByGroup($column)
{
return $this->aggregateByGroup('sum', [$column]);
}

/**
* Retrieve the average of the values of a given column.
*
Expand All @@ -3607,6 +3651,17 @@ public function avg($column)
return $this->aggregate(__FUNCTION__, [$column]);
}

/**
* Retrieve the average of the values of a given column by group.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @return \Illuminate\Support\Collection
*/
public function avgByGroup($column)
{
return $this->aggregateByGroup('avg', [$column]);
}

/**
* Alias for the "avg" method.
*
Expand Down Expand Up @@ -3637,6 +3692,21 @@ public function aggregate($function, $columns = ['*'])
}
}

/**
* Execute an aggregate function for each group.
*
* @param string $function
* @param array $columns
* @return \Illuminate\Support\Collection
*/
public function aggregateByGroup(string $function, array $columns = ['*'])
{
return $this->cloneWithout($this->unions || $this->havings ? [] : ['columns'])
->cloneWithoutBindings($this->unions || $this->havings ? [] : ['select'])
->setAggregate($function, $columns)
->get($columns);
}

/**
* Execute a numeric aggregate function on the database.
*
Expand Down
21 changes: 14 additions & 7 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,15 @@ protected function compileAggregate(Builder $query, $aggregate)
$column = 'distinct '.$column;
}

return 'select '.$aggregate['function'].'('.$column.') as aggregate';
$sql = 'select ';

$sql .= $aggregate['function'].'('.$column.') as aggregate';

if ($query->groups) {
$sql .= ', '.$this->columnize($query->groups);
}

return $sql;
}

/**
Expand Down Expand Up @@ -1131,10 +1139,12 @@ protected function wrapUnion($sql)
protected function compileUnionAggregate(Builder $query)
{
$sql = $this->compileAggregate($query, $query->aggregate);
$groups = $query->groups ? ' '.$this->compileGroups($query, $query->groups) : '';

$query->aggregate = null;
$query->groups = null;

return $sql.' from ('.$this->compileSelect($query).') as '.$this->wrapTable('temp_table');
return $sql.' from ('.$this->compileSelect($query).') as '.$this->wrapTable('temp_table').$groups;
}

/**
Expand Down Expand Up @@ -1168,11 +1178,8 @@ public function compileInsert(Builder $query, array $values)
return "insert into {$table} default values";
}

if (! array_is_list($values)) {
$values = (new Collection(array_keys($values)))
->some(fn ($key) => ! is_numeric($key))
? [$values]
: array_values($values);
if (! is_array(reset($values))) {
$values = [$values];
}

$columns = $this->columnize(array_keys(reset($values)));
Expand Down
21 changes: 16 additions & 5 deletions src/Illuminate/Foundation/Bus/Dispatchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait Dispatchable
*/
public static function dispatch(...$arguments)
{
return new PendingDispatch(new static(...$arguments));
return static::newPendingDispatch(new static(...$arguments));
}

/**
Expand All @@ -32,12 +32,12 @@ public static function dispatchIf($boolean, ...$arguments)
$dispatchable = new static(...$arguments);

return value($boolean, $dispatchable)
? new PendingDispatch($dispatchable)
? static::newPendingDispatch($dispatchable)
: new Fluent;
}

return value($boolean)
? new PendingDispatch(new static(...$arguments))
? static::newPendingDispatch(new static(...$arguments))
: new Fluent;
}

Expand All @@ -54,12 +54,12 @@ public static function dispatchUnless($boolean, ...$arguments)
$dispatchable = new static(...$arguments);

return ! value($boolean, $dispatchable)
? new PendingDispatch($dispatchable)
? static::newPendingDispatch($dispatchable)
: new Fluent;
}

return ! value($boolean)
? new PendingDispatch(new static(...$arguments))
? static::newPendingDispatch(new static(...$arguments))
: new Fluent;
}

Expand Down Expand Up @@ -97,4 +97,15 @@ public static function withChain($chain)
{
return new PendingChain(static::class, $chain);
}

/**
* Create a new pending job dispatch instance.
*
* @param mixed $job
* @return \Illuminate\Foundation\Bus\PendingDispatch
*/
protected static function newPendingDispatch($job)
{
return new PendingDispatch($job);
}
}
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Bus/PendingDispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ protected function shouldDispatch()
/**
* Get the underlying job instance.
*
* @var mixed
* @return mixed
*/
public function getJob()
{
Expand Down
Loading

0 comments on commit bdac8cf

Please sign in to comment.