One Hat Cyber Team
Your IP:
216.73.216.102
Server IP:
198.54.114.155
Server:
Linux server71.web-hosting.com 4.18.0-513.18.1.lve.el8.x86_64 #1 SMP Thu Feb 22 12:55:50 UTC 2024 x86_64
Server Software:
LiteSpeed
PHP Version:
5.6.40
Create File
|
Create Folder
Execute
Dir :
~
/
home
/
fluxyjvi
/
www
/
assets
/
images
/
View File Name :
framework.tar
src/Illuminate/Queue/QueueServiceProvider.php 0000644 00000023300 15107327036 0015355 0 ustar 00 <?php namespace Illuminate\Queue; use Aws\DynamoDb\DynamoDbClient; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Queue\Connectors\BeanstalkdConnector; use Illuminate\Queue\Connectors\DatabaseConnector; use Illuminate\Queue\Connectors\NullConnector; use Illuminate\Queue\Connectors\RedisConnector; use Illuminate\Queue\Connectors\SqsConnector; use Illuminate\Queue\Connectors\SyncConnector; use Illuminate\Queue\Failed\DatabaseFailedJobProvider; use Illuminate\Queue\Failed\DatabaseUuidFailedJobProvider; use Illuminate\Queue\Failed\DynamoDbFailedJobProvider; use Illuminate\Queue\Failed\FileFailedJobProvider; use Illuminate\Queue\Failed\NullFailedJobProvider; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Facade; use Illuminate\Support\ServiceProvider; use Laravel\SerializableClosure\SerializableClosure; class QueueServiceProvider extends ServiceProvider implements DeferrableProvider { use SerializesAndRestoresModelIdentifiers; /** * Register the service provider. * * @return void */ public function register() { $this->configureSerializableClosureUses(); $this->registerManager(); $this->registerConnection(); $this->registerWorker(); $this->registerListener(); $this->registerFailedJobServices(); } /** * Configure serializable closures uses. * * @return void */ protected function configureSerializableClosureUses() { SerializableClosure::transformUseVariablesUsing(function ($data) { foreach ($data as $key => $value) { $data[$key] = $this->getSerializedPropertyValue($value); } return $data; }); SerializableClosure::resolveUseVariablesUsing(function ($data) { foreach ($data as $key => $value) { $data[$key] = $this->getRestoredPropertyValue($value); } return $data; }); } /** * Register the queue manager. * * @return void */ protected function registerManager() { $this->app->singleton('queue', function ($app) { // Once we have an instance of the queue manager, we will register the various // resolvers for the queue connectors. These connectors are responsible for // creating the classes that accept queue configs and instantiate queues. return tap(new QueueManager($app), function ($manager) { $this->registerConnectors($manager); }); }); } /** * Register the default queue connection binding. * * @return void */ protected function registerConnection() { $this->app->singleton('queue.connection', function ($app) { return $app['queue']->connection(); }); } /** * Register the connectors on the queue manager. * * @param \Illuminate\Queue\QueueManager $manager * @return void */ public function registerConnectors($manager) { foreach (['Null', 'Sync', 'Database', 'Redis', 'Beanstalkd', 'Sqs'] as $connector) { $this->{"register{$connector}Connector"}($manager); } } /** * Register the Null queue connector. * * @param \Illuminate\Queue\QueueManager $manager * @return void */ protected function registerNullConnector($manager) { $manager->addConnector('null', function () { return new NullConnector; }); } /** * Register the Sync queue connector. * * @param \Illuminate\Queue\QueueManager $manager * @return void */ protected function registerSyncConnector($manager) { $manager->addConnector('sync', function () { return new SyncConnector; }); } /** * Register the database queue connector. * * @param \Illuminate\Queue\QueueManager $manager * @return void */ protected function registerDatabaseConnector($manager) { $manager->addConnector('database', function () { return new DatabaseConnector($this->app['db']); }); } /** * Register the Redis queue connector. * * @param \Illuminate\Queue\QueueManager $manager * @return void */ protected function registerRedisConnector($manager) { $manager->addConnector('redis', function () { return new RedisConnector($this->app['redis']); }); } /** * Register the Beanstalkd queue connector. * * @param \Illuminate\Queue\QueueManager $manager * @return void */ protected function registerBeanstalkdConnector($manager) { $manager->addConnector('beanstalkd', function () { return new BeanstalkdConnector; }); } /** * Register the Amazon SQS queue connector. * * @param \Illuminate\Queue\QueueManager $manager * @return void */ protected function registerSqsConnector($manager) { $manager->addConnector('sqs', function () { return new SqsConnector; }); } /** * Register the queue worker. * * @return void */ protected function registerWorker() { $this->app->singleton('queue.worker', function ($app) { $isDownForMaintenance = function () { return $this->app->isDownForMaintenance(); }; $resetScope = function () use ($app) { if (method_exists($app['log']->driver(), 'withoutContext')) { $app['log']->withoutContext(); } if (method_exists($app['db'], 'getConnections')) { foreach ($app['db']->getConnections() as $connection) { $connection->resetTotalQueryDuration(); $connection->allowQueryDurationHandlersToRunAgain(); } } $app->forgetScopedInstances(); Facade::clearResolvedInstances(); }; return new Worker( $app['queue'], $app['events'], $app[ExceptionHandler::class], $isDownForMaintenance, $resetScope ); }); } /** * Register the queue listener. * * @return void */ protected function registerListener() { $this->app->singleton('queue.listener', function ($app) { return new Listener($app->basePath()); }); } /** * Register the failed job services. * * @return void */ protected function registerFailedJobServices() { $this->app->singleton('queue.failer', function ($app) { $config = $app['config']['queue.failed']; if (array_key_exists('driver', $config) && (is_null($config['driver']) || $config['driver'] === 'null')) { return new NullFailedJobProvider; } if (isset($config['driver']) && $config['driver'] === 'file') { return new FileFailedJobProvider( $config['path'] ?? $this->app->storagePath('framework/cache/failed-jobs.json'), $config['limit'] ?? 100, fn () => $app['cache']->store('file'), ); } elseif (isset($config['driver']) && $config['driver'] === 'dynamodb') { return $this->dynamoFailedJobProvider($config); } elseif (isset($config['driver']) && $config['driver'] === 'database-uuids') { return $this->databaseUuidFailedJobProvider($config); } elseif (isset($config['table'])) { return $this->databaseFailedJobProvider($config); } else { return new NullFailedJobProvider; } }); } /** * Create a new database failed job provider. * * @param array $config * @return \Illuminate\Queue\Failed\DatabaseFailedJobProvider */ protected function databaseFailedJobProvider($config) { return new DatabaseFailedJobProvider( $this->app['db'], $config['database'], $config['table'] ); } /** * Create a new database failed job provider that uses UUIDs as IDs. * * @param array $config * @return \Illuminate\Queue\Failed\DatabaseUuidFailedJobProvider */ protected function databaseUuidFailedJobProvider($config) { return new DatabaseUuidFailedJobProvider( $this->app['db'], $config['database'], $config['table'] ); } /** * Create a new DynamoDb failed job provider. * * @param array $config * @return \Illuminate\Queue\Failed\DynamoDbFailedJobProvider */ protected function dynamoFailedJobProvider($config) { $dynamoConfig = [ 'region' => $config['region'], 'version' => 'latest', 'endpoint' => $config['endpoint'] ?? null, ]; if (! empty($config['key']) && ! empty($config['secret'])) { $dynamoConfig['credentials'] = Arr::only( $config, ['key', 'secret', 'token'] ); } return new DynamoDbFailedJobProvider( new DynamoDbClient($dynamoConfig), $this->app['config']['app.name'], $config['table'] ); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return [ 'queue', 'queue.connection', 'queue.failer', 'queue.listener', 'queue.worker', ]; } } src/Illuminate/Queue/Queue.php 0000644 00000025430 15107327036 0012327 0 ustar 00 <?php namespace Illuminate\Queue; use Closure; use DateTimeInterface; use Illuminate\Container\Container; use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Queue\ShouldBeEncrypted; use Illuminate\Contracts\Queue\ShouldQueueAfterCommit; use Illuminate\Queue\Events\JobQueued; use Illuminate\Support\Arr; use Illuminate\Support\InteractsWithTime; use Illuminate\Support\Str; abstract class Queue { use InteractsWithTime; /** * The IoC container instance. * * @var \Illuminate\Container\Container */ protected $container; /** * The connection name for the queue. * * @var string */ protected $connectionName; /** * Indicates that jobs should be dispatched after all database transactions have committed. * * @var bool */ protected $dispatchAfterCommit; /** * The create payload callbacks. * * @var callable[] */ protected static $createPayloadCallbacks = []; /** * Push a new job onto the queue. * * @param string $queue * @param string $job * @param mixed $data * @return mixed */ public function pushOn($queue, $job, $data = '') { return $this->push($job, $data, $queue); } /** * Push a new job onto a specific queue after (n) seconds. * * @param string $queue * @param \DateTimeInterface|\DateInterval|int $delay * @param string $job * @param mixed $data * @return mixed */ public function laterOn($queue, $delay, $job, $data = '') { return $this->later($delay, $job, $data, $queue); } /** * Push an array of jobs onto the queue. * * @param array $jobs * @param mixed $data * @param string|null $queue * @return void */ public function bulk($jobs, $data = '', $queue = null) { foreach ((array) $jobs as $job) { $this->push($job, $data, $queue); } } /** * Create a payload string from the given job and data. * * @param \Closure|string|object $job * @param string $queue * @param mixed $data * @return string * * @throws \Illuminate\Queue\InvalidPayloadException */ protected function createPayload($job, $queue, $data = '') { if ($job instanceof Closure) { $job = CallQueuedClosure::create($job); } $payload = json_encode($value = $this->createPayloadArray($job, $queue, $data), \JSON_UNESCAPED_UNICODE); if (json_last_error() !== JSON_ERROR_NONE) { throw new InvalidPayloadException( 'Unable to JSON encode payload. Error code: '.json_last_error(), $value ); } return $payload; } /** * Create a payload array from the given job and data. * * @param string|object $job * @param string $queue * @param mixed $data * @return array */ protected function createPayloadArray($job, $queue, $data = '') { return is_object($job) ? $this->createObjectPayload($job, $queue) : $this->createStringPayload($job, $queue, $data); } /** * Create a payload for an object-based queue handler. * * @param object $job * @param string $queue * @return array */ protected function createObjectPayload($job, $queue) { $payload = $this->withCreatePayloadHooks($queue, [ 'uuid' => (string) Str::uuid(), 'displayName' => $this->getDisplayName($job), 'job' => 'Illuminate\Queue\CallQueuedHandler@call', 'maxTries' => $job->tries ?? null, 'maxExceptions' => $job->maxExceptions ?? null, 'failOnTimeout' => $job->failOnTimeout ?? false, 'backoff' => $this->getJobBackoff($job), 'timeout' => $job->timeout ?? null, 'retryUntil' => $this->getJobExpiration($job), 'data' => [ 'commandName' => $job, 'command' => $job, ], ]); $command = $this->jobShouldBeEncrypted($job) && $this->container->bound(Encrypter::class) ? $this->container[Encrypter::class]->encrypt(serialize(clone $job)) : serialize(clone $job); return array_merge($payload, [ 'data' => array_merge($payload['data'], [ 'commandName' => get_class($job), 'command' => $command, ]), ]); } /** * Get the display name for the given job. * * @param object $job * @return string */ protected function getDisplayName($job) { return method_exists($job, 'displayName') ? $job->displayName() : get_class($job); } /** * Get the backoff for an object-based queue handler. * * @param mixed $job * @return mixed */ public function getJobBackoff($job) { if (! method_exists($job, 'backoff') && ! isset($job->backoff)) { return; } if (is_null($backoff = $job->backoff ?? $job->backoff())) { return; } return collect(Arr::wrap($backoff)) ->map(function ($backoff) { return $backoff instanceof DateTimeInterface ? $this->secondsUntil($backoff) : $backoff; })->implode(','); } /** * Get the expiration timestamp for an object-based queue handler. * * @param mixed $job * @return mixed */ public function getJobExpiration($job) { if (! method_exists($job, 'retryUntil') && ! isset($job->retryUntil)) { return; } $expiration = $job->retryUntil ?? $job->retryUntil(); return $expiration instanceof DateTimeInterface ? $expiration->getTimestamp() : $expiration; } /** * Determine if the job should be encrypted. * * @param object $job * @return bool */ protected function jobShouldBeEncrypted($job) { if ($job instanceof ShouldBeEncrypted) { return true; } return isset($job->shouldBeEncrypted) && $job->shouldBeEncrypted; } /** * Create a typical, string based queue payload array. * * @param string $job * @param string $queue * @param mixed $data * @return array */ protected function createStringPayload($job, $queue, $data) { return $this->withCreatePayloadHooks($queue, [ 'uuid' => (string) Str::uuid(), 'displayName' => is_string($job) ? explode('@', $job)[0] : null, 'job' => $job, 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => $data, ]); } /** * Register a callback to be executed when creating job payloads. * * @param callable|null $callback * @return void */ public static function createPayloadUsing($callback) { if (is_null($callback)) { static::$createPayloadCallbacks = []; } else { static::$createPayloadCallbacks[] = $callback; } } /** * Create the given payload using any registered payload hooks. * * @param string $queue * @param array $payload * @return array */ protected function withCreatePayloadHooks($queue, array $payload) { if (! empty(static::$createPayloadCallbacks)) { foreach (static::$createPayloadCallbacks as $callback) { $payload = array_merge($payload, $callback($this->getConnectionName(), $queue, $payload)); } } return $payload; } /** * Enqueue a job using the given callback. * * @param \Closure|string|object $job * @param string $payload * @param string $queue * @param \DateTimeInterface|\DateInterval|int|null $delay * @param callable $callback * @return mixed */ protected function enqueueUsing($job, $payload, $queue, $delay, $callback) { if ($this->shouldDispatchAfterCommit($job) && $this->container->bound('db.transactions')) { return $this->container->make('db.transactions')->addCallback( function () use ($payload, $queue, $delay, $callback, $job) { return tap($callback($payload, $queue, $delay), function ($jobId) use ($job, $payload) { $this->raiseJobQueuedEvent($jobId, $job, $payload); }); } ); } return tap($callback($payload, $queue, $delay), function ($jobId) use ($job, $payload) { $this->raiseJobQueuedEvent($jobId, $job, $payload); }); } /** * Determine if the job should be dispatched after all database transactions have committed. * * @param \Closure|string|object $job * @return bool */ protected function shouldDispatchAfterCommit($job) { if (is_object($job) && $job instanceof ShouldQueueAfterCommit) { return true; } if (! $job instanceof Closure && is_object($job) && isset($job->afterCommit)) { return $job->afterCommit; } if (isset($this->dispatchAfterCommit)) { return $this->dispatchAfterCommit; } return false; } /** * Raise the job queued event. * * @param string|int|null $jobId * @param \Closure|string|object $job * @param string $payload * @return void */ protected function raiseJobQueuedEvent($jobId, $job, $payload) { if ($this->container->bound('events')) { $this->container['events']->dispatch(new JobQueued($this->connectionName, $jobId, $job, $payload)); } } /** * Get the connection name for the queue. * * @return string */ public function getConnectionName() { return $this->connectionName; } /** * Set the connection name for the queue. * * @param string $name * @return $this */ public function setConnectionName($name) { $this->connectionName = $name; return $this; } /** * Get the container instance being used by the connection. * * @return \Illuminate\Container\Container */ public function getContainer() { return $this->container; } /** * Set the IoC container instance. * * @param \Illuminate\Container\Container $container * @return void */ public function setContainer(Container $container) { $this->container = $container; } } src/Illuminate/Queue/InvalidPayloadException.php 0000644 00000001050 15107327036 0016012 0 ustar 00 <?php namespace Illuminate\Queue; use InvalidArgumentException; class InvalidPayloadException extends InvalidArgumentException { /** * The value that failed to decode. * * @var mixed */ public $value; /** * Create a new exception instance. * * @param string|null $message * @param mixed $value * @return void */ public function __construct($message = null, $value = null) { parent::__construct($message ?: json_last_error()); $this->value = $value; } } src/Illuminate/Queue/ListenerOptions.php 0000644 00000001606 15107327036 0014403 0 ustar 00 <?php namespace Illuminate\Queue; class ListenerOptions extends WorkerOptions { /** * The environment the worker should run in. * * @var string */ public $environment; /** * Create a new listener options instance. * * @param string $name * @param string|null $environment * @param int|int[] $backoff * @param int $memory * @param int $timeout * @param int $sleep * @param int $maxTries * @param bool $force * @param int $rest * @return void */ public function __construct($name = 'default', $environment = null, $backoff = 0, $memory = 128, $timeout = 60, $sleep = 3, $maxTries = 1, $force = false, $rest = 0) { $this->environment = $environment; parent::__construct($name, $backoff, $memory, $timeout, $sleep, $maxTries, $force, false, 0, 0, $rest); } } src/Illuminate/Queue/Console/FailedTableCommand.php 0000644 00000001661 15107327036 0016300 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Console\MigrationGeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'queue:failed-table')] class FailedTableCommand extends MigrationGeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'queue:failed-table'; /** * The console command description. * * @var string */ protected $description = 'Create a migration for the failed queue jobs database table'; /** * Get the migration table name. * * @return string */ protected function migrationTableName() { return $this->laravel['config']['queue.failed.table']; } /** * Get the path to the migration stub file. * * @return string */ protected function migrationStubFile() { return __DIR__.'/stubs/failed_jobs.stub'; } } src/Illuminate/Queue/Console/ListenCommand.php 0000644 00000010042 15107327036 0015373 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Console\Command; use Illuminate\Queue\Listener; use Illuminate\Queue\ListenerOptions; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'queue:listen')] class ListenCommand extends Command { /** * The console command name. * * @var string */ protected $signature = 'queue:listen {connection? : The name of connection} {--name=default : The name of the worker} {--delay=0 : The number of seconds to delay failed jobs (Deprecated)} {--backoff=0 : The number of seconds to wait before retrying a job that encountered an uncaught exception} {--force : Force the worker to run even in maintenance mode} {--memory=128 : The memory limit in megabytes} {--queue= : The queue to listen on} {--sleep=3 : Number of seconds to sleep when no job is available} {--rest=0 : Number of seconds to rest between jobs} {--timeout=60 : The number of seconds a child process can run} {--tries=1 : Number of times to attempt a job before logging it failed}'; /** * The console command description. * * @var string */ protected $description = 'Listen to a given queue'; /** * The queue listener instance. * * @var \Illuminate\Queue\Listener */ protected $listener; /** * Create a new queue listen command. * * @param \Illuminate\Queue\Listener $listener * @return void */ public function __construct(Listener $listener) { parent::__construct(); $this->setOutputHandler($this->listener = $listener); } /** * Execute the console command. * * @return void */ public function handle() { // We need to get the right queue for the connection which is set in the queue // configuration file for the application. We will pull it based on the set // connection being run for the queue operation currently being executed. $queue = $this->getQueue( $connection = $this->input->getArgument('connection') ); $this->components->info(sprintf('Processing jobs from the [%s] %s.', $queue, str('queue')->plural(explode(',', $queue)))); $this->listener->listen( $connection, $queue, $this->gatherOptions() ); } /** * Get the name of the queue connection to listen on. * * @param string $connection * @return string */ protected function getQueue($connection) { $connection = $connection ?: $this->laravel['config']['queue.default']; return $this->input->getOption('queue') ?: $this->laravel['config']->get( "queue.connections.{$connection}.queue", 'default' ); } /** * Get the listener options for the command. * * @return \Illuminate\Queue\ListenerOptions */ protected function gatherOptions() { $backoff = $this->hasOption('backoff') ? $this->option('backoff') : $this->option('delay'); return new ListenerOptions( name: $this->option('name'), environment: $this->option('env'), backoff: $backoff, memory: $this->option('memory'), timeout: $this->option('timeout'), sleep: $this->option('sleep'), rest: $this->option('rest'), maxTries: $this->option('tries'), force: $this->option('force') ); } /** * Set the options on the queue listener. * * @param \Illuminate\Queue\Listener $listener * @return void */ protected function setOutputHandler(Listener $listener) { $listener->setOutputHandler(function ($type, $line) { $this->output->write($line); }); } } src/Illuminate/Queue/Console/RestartCommand.php 0000644 00000002411 15107327036 0015562 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Console\Command; use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Support\InteractsWithTime; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'queue:restart')] class RestartCommand extends Command { use InteractsWithTime; /** * The console command name. * * @var string */ protected $name = 'queue:restart'; /** * The console command description. * * @var string */ protected $description = 'Restart queue worker daemons after their current job'; /** * The cache store implementation. * * @var \Illuminate\Contracts\Cache\Repository */ protected $cache; /** * Create a new queue restart command. * * @param \Illuminate\Contracts\Cache\Repository $cache * @return void */ public function __construct(Cache $cache) { parent::__construct(); $this->cache = $cache; } /** * Execute the console command. * * @return void */ public function handle() { $this->cache->forever('illuminate:queue:restart', $this->currentTime()); $this->components->info('Broadcasting queue restart signal.'); } } src/Illuminate/Queue/Console/ClearCommand.php 0000644 00000005324 15107327036 0015172 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Console\Command; use Illuminate\Console\ConfirmableTrait; use Illuminate\Contracts\Queue\ClearableQueue; use Illuminate\Support\Str; use ReflectionClass; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'queue:clear')] class ClearCommand extends Command { use ConfirmableTrait; /** * The console command name. * * @var string */ protected $name = 'queue:clear'; /** * The console command description. * * @var string */ protected $description = 'Delete all of the jobs from the specified queue'; /** * Execute the console command. * * @return int|null */ public function handle() { if (! $this->confirmToProceed()) { return 1; } $connection = $this->argument('connection') ?: $this->laravel['config']['queue.default']; // We need to get the right queue for the connection which is set in the queue // configuration file for the application. We will pull it based on the set // connection being run for the queue operation currently being executed. $queueName = $this->getQueue($connection); $queue = $this->laravel['queue']->connection($connection); if ($queue instanceof ClearableQueue) { $count = $queue->clear($queueName); $this->components->info('Cleared '.$count.' '.Str::plural('job', $count).' from the ['.$queueName.'] queue'); } else { $this->components->error('Clearing queues is not supported on ['.(new ReflectionClass($queue))->getShortName().']'); } return 0; } /** * Get the queue name to clear. * * @param string $connection * @return string */ protected function getQueue($connection) { return $this->option('queue') ?: $this->laravel['config']->get( "queue.connections.{$connection}.queue", 'default' ); } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return [ ['connection', InputArgument::OPTIONAL, 'The name of the queue connection to clear'], ]; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['queue', null, InputOption::VALUE_OPTIONAL, 'The name of the queue to clear'], ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'], ]; } } src/Illuminate/Queue/Console/ListFailedCommand.php 0000644 00000005624 15107327036 0016167 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Console\Command; use Illuminate\Support\Arr; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'queue:failed')] class ListFailedCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'queue:failed'; /** * The console command description. * * @var string */ protected $description = 'List all of the failed queue jobs'; /** * The table headers for the command. * * @var string[] */ protected $headers = ['ID', 'Connection', 'Queue', 'Class', 'Failed At']; /** * Execute the console command. * * @return void */ public function handle() { if (count($jobs = $this->getFailedJobs()) === 0) { return $this->components->info('No failed jobs found.'); } $this->newLine(); $this->displayFailedJobs($jobs); $this->newLine(); } /** * Compile the failed jobs into a displayable format. * * @return array */ protected function getFailedJobs() { $failed = $this->laravel['queue.failer']->all(); return collect($failed)->map(function ($failed) { return $this->parseFailedJob((array) $failed); })->filter()->all(); } /** * Parse the failed job row. * * @param array $failed * @return array */ protected function parseFailedJob(array $failed) { $row = array_values(Arr::except($failed, ['payload', 'exception'])); array_splice($row, 3, 0, $this->extractJobName($failed['payload']) ?: ''); return $row; } /** * Extract the failed job name from payload. * * @param string $payload * @return string|null */ private function extractJobName($payload) { $payload = json_decode($payload, true); if ($payload && (! isset($payload['data']['command']))) { return $payload['job'] ?? null; } elseif ($payload && isset($payload['data']['command'])) { return $this->matchJobName($payload); } } /** * Match the job name from the payload. * * @param array $payload * @return string|null */ protected function matchJobName($payload) { preg_match('/"([^"]+)"/', $payload['data']['command'], $matches); return $matches[1] ?? $payload['job'] ?? null; } /** * Display the failed jobs in the console. * * @param array $jobs * @return void */ protected function displayFailedJobs(array $jobs) { collect($jobs)->each( fn ($job) => $this->components->twoColumnDetail( sprintf('<fg=gray>%s</> %s</>', $job[4], $job[0]), sprintf('<fg=gray>%s@%s</> %s', $job[1], $job[2], $job[3]) ), ); } } src/Illuminate/Queue/Console/ForgetFailedCommand.php 0000644 00000001560 15107327036 0016475 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'queue:forget')] class ForgetFailedCommand extends Command { /** * The console command signature. * * @var string */ protected $signature = 'queue:forget {id : The ID of the failed job}'; /** * The console command description. * * @var string */ protected $description = 'Delete a failed queue job'; /** * Execute the console command. * * @return void */ public function handle() { if ($this->laravel['queue.failer']->forget($this->argument('id'))) { $this->components->info('Failed job deleted successfully.'); } else { $this->components->error('No failed job matches the given ID.'); } } } src/Illuminate/Queue/Console/TableCommand.php 0000644 00000001635 15107327036 0015174 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Console\MigrationGeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'queue:table')] class TableCommand extends MigrationGeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'queue:table'; /** * The console command description. * * @var string */ protected $description = 'Create a migration for the queue jobs database table'; /** * Get the migration table name. * * @return string */ protected function migrationTableName() { return $this->laravel['config']['queue.connections.database.table']; } /** * Get the path to the migration stub file. * * @return string */ protected function migrationStubFile() { return __DIR__.'/stubs/jobs.stub'; } } src/Illuminate/Queue/Console/BatchesTableCommand.php 0000644 00000001671 15107327036 0016466 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Console\MigrationGeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'queue:batches-table')] class BatchesTableCommand extends MigrationGeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'queue:batches-table'; /** * The console command description. * * @var string */ protected $description = 'Create a migration for the batches database table'; /** * Get the migration table name. * * @return string */ protected function migrationTableName() { return $this->laravel['config']['queue.batching.table'] ?? 'job_batches'; } /** * Get the path to the migration stub file. * * @return string */ protected function migrationStubFile() { return __DIR__.'/stubs/batches.stub'; } } src/Illuminate/Queue/Console/PruneBatchesCommand.php 0000644 00000003772 15107327036 0016534 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Bus\BatchRepository; use Illuminate\Bus\DatabaseBatchRepository; use Illuminate\Bus\PrunableBatchRepository; use Illuminate\Console\Command; use Illuminate\Support\Carbon; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'queue:prune-batches')] class PruneBatchesCommand extends Command { /** * The console command signature. * * @var string */ protected $signature = 'queue:prune-batches {--hours=24 : The number of hours to retain batch data} {--unfinished= : The number of hours to retain unfinished batch data } {--cancelled= : The number of hours to retain cancelled batch data }'; /** * The console command description. * * @var string */ protected $description = 'Prune stale entries from the batches database'; /** * Execute the console command. * * @return void */ public function handle() { $repository = $this->laravel[BatchRepository::class]; $count = 0; if ($repository instanceof PrunableBatchRepository) { $count = $repository->prune(Carbon::now()->subHours($this->option('hours'))); } $this->components->info("{$count} entries deleted."); if ($this->option('unfinished') !== null) { $count = 0; if ($repository instanceof DatabaseBatchRepository) { $count = $repository->pruneUnfinished(Carbon::now()->subHours($this->option('unfinished'))); } $this->components->info("{$count} unfinished entries deleted."); } if ($this->option('cancelled') !== null) { $count = 0; if ($repository instanceof DatabaseBatchRepository) { $count = $repository->pruneCancelled(Carbon::now()->subHours($this->option('cancelled'))); } $this->components->info("{$count} cancelled entries deleted."); } } } src/Illuminate/Queue/Console/WorkCommand.php 0000644 00000023232 15107327036 0015064 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Carbon\CarbonInterval; use Illuminate\Console\Command; use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Contracts\Queue\Job; use Illuminate\Queue\Events\JobFailed; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; use Illuminate\Queue\Events\JobReleasedAfterException; use Illuminate\Queue\Worker; use Illuminate\Queue\WorkerOptions; use Illuminate\Support\Carbon; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Terminal; use function Termwind\terminal; #[AsCommand(name: 'queue:work')] class WorkCommand extends Command { /** * The console command name. * * @var string */ protected $signature = 'queue:work {connection? : The name of the queue connection to work} {--name=default : The name of the worker} {--queue= : The names of the queues to work} {--daemon : Run the worker in daemon mode (Deprecated)} {--once : Only process the next job on the queue} {--stop-when-empty : Stop when the queue is empty} {--delay=0 : The number of seconds to delay failed jobs (Deprecated)} {--backoff=0 : The number of seconds to wait before retrying a job that encountered an uncaught exception} {--max-jobs=0 : The number of jobs to process before stopping} {--max-time=0 : The maximum number of seconds the worker should run} {--force : Force the worker to run even in maintenance mode} {--memory=128 : The memory limit in megabytes} {--sleep=3 : Number of seconds to sleep when no job is available} {--rest=0 : Number of seconds to rest between jobs} {--timeout=60 : The number of seconds a child process can run} {--tries=1 : Number of times to attempt a job before logging it failed}'; /** * The console command description. * * @var string */ protected $description = 'Start processing jobs on the queue as a daemon'; /** * The queue worker instance. * * @var \Illuminate\Queue\Worker */ protected $worker; /** * The cache store implementation. * * @var \Illuminate\Contracts\Cache\Repository */ protected $cache; /** * Holds the start time of the last processed job, if any. * * @var float|null */ protected $latestStartedAt; /** * Create a new queue work command. * * @param \Illuminate\Queue\Worker $worker * @param \Illuminate\Contracts\Cache\Repository $cache * @return void */ public function __construct(Worker $worker, Cache $cache) { parent::__construct(); $this->cache = $cache; $this->worker = $worker; } /** * Execute the console command. * * @return int|null */ public function handle() { if ($this->downForMaintenance() && $this->option('once')) { return $this->worker->sleep($this->option('sleep')); } // We'll listen to the processed and failed events so we can write information // to the console as jobs are processed, which will let the developer watch // which jobs are coming through a queue and be informed on its progress. $this->listenForEvents(); $connection = $this->argument('connection') ?: $this->laravel['config']['queue.default']; // We need to get the right queue for the connection which is set in the queue // configuration file for the application. We will pull it based on the set // connection being run for the queue operation currently being executed. $queue = $this->getQueue($connection); if (Terminal::hasSttyAvailable()) { $this->components->info( sprintf('Processing jobs from the [%s] %s.', $queue, str('queue')->plural(explode(',', $queue))) ); } return $this->runWorker( $connection, $queue ); } /** * Run the worker instance. * * @param string $connection * @param string $queue * @return int|null */ protected function runWorker($connection, $queue) { return $this->worker ->setName($this->option('name')) ->setCache($this->cache) ->{$this->option('once') ? 'runNextJob' : 'daemon'}( $connection, $queue, $this->gatherWorkerOptions() ); } /** * Gather all of the queue worker options as a single object. * * @return \Illuminate\Queue\WorkerOptions */ protected function gatherWorkerOptions() { return new WorkerOptions( $this->option('name'), max($this->option('backoff'), $this->option('delay')), $this->option('memory'), $this->option('timeout'), $this->option('sleep'), $this->option('tries'), $this->option('force'), $this->option('stop-when-empty'), $this->option('max-jobs'), $this->option('max-time'), $this->option('rest') ); } /** * Listen for the queue events in order to update the console output. * * @return void */ protected function listenForEvents() { $this->laravel['events']->listen(JobProcessing::class, function ($event) { $this->writeOutput($event->job, 'starting'); }); $this->laravel['events']->listen(JobProcessed::class, function ($event) { $this->writeOutput($event->job, 'success'); }); $this->laravel['events']->listen(JobReleasedAfterException::class, function ($event) { $this->writeOutput($event->job, 'released_after_exception'); }); $this->laravel['events']->listen(JobFailed::class, function ($event) { $this->writeOutput($event->job, 'failed'); $this->logFailedJob($event); }); } /** * Write the status output for the queue worker. * * @param \Illuminate\Contracts\Queue\Job $job * @param string $status * @return void */ protected function writeOutput(Job $job, $status) { $this->output->write(sprintf( ' <fg=gray>%s</> %s%s', $this->now()->format('Y-m-d H:i:s'), $job->resolveName(), $this->output->isVerbose() ? sprintf(' <fg=gray>%s</>', $job->getJobId()) : '' )); if ($status == 'starting') { $this->latestStartedAt = microtime(true); $dots = max(terminal()->width() - mb_strlen($job->resolveName()) - ( $this->output->isVerbose() ? (mb_strlen($job->getJobId()) + 1) : 0 ) - 33, 0); $this->output->write(' '.str_repeat('<fg=gray>.</>', $dots)); return $this->output->writeln(' <fg=yellow;options=bold>RUNNING</>'); } $runTime = $this->formatRunTime($this->latestStartedAt); $dots = max(terminal()->width() - mb_strlen($job->resolveName()) - ( $this->output->isVerbose() ? (mb_strlen($job->getJobId()) + 1) : 0 ) - mb_strlen($runTime) - 31, 0); $this->output->write(' '.str_repeat('<fg=gray>.</>', $dots)); $this->output->write(" <fg=gray>$runTime</>"); $this->output->writeln(match ($status) { 'success' => ' <fg=green;options=bold>DONE</>', 'released_after_exception' => ' <fg=yellow;options=bold>FAIL</>', default => ' <fg=red;options=bold>FAIL</>', }); } /** * Get the current date / time. * * @return \Illuminate\Support\Carbon */ protected function now() { $queueTimezone = $this->laravel['config']->get('queue.output_timezone'); if ($queueTimezone && $queueTimezone !== $this->laravel['config']->get('app.timezone')) { return Carbon::now()->setTimezone($queueTimezone); } return Carbon::now(); } /** * Given a start time, format the total run time for human readability. * * @param float $startTime * @return string */ protected function formatRunTime($startTime) { $runTime = (microtime(true) - $startTime) * 1000; return $runTime > 1000 ? CarbonInterval::milliseconds($runTime)->cascade()->forHumans(short: true) : number_format($runTime, 2).'ms'; } /** * Store a failed job event. * * @param \Illuminate\Queue\Events\JobFailed $event * @return void */ protected function logFailedJob(JobFailed $event) { $this->laravel['queue.failer']->log( $event->connectionName, $event->job->getQueue(), $event->job->getRawBody(), $event->exception ); } /** * Get the queue name for the worker. * * @param string $connection * @return string */ protected function getQueue($connection) { return $this->option('queue') ?: $this->laravel['config']->get( "queue.connections.{$connection}.queue", 'default' ); } /** * Determine if the worker should run in maintenance mode. * * @return bool */ protected function downForMaintenance() { return $this->option('force') ? false : $this->laravel->isDownForMaintenance(); } } src/Illuminate/Queue/Console/FlushFailedCommand.php 0000644 00000001760 15107327036 0016332 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'queue:flush')] class FlushFailedCommand extends Command { /** * The console command name. * * @var string */ protected $signature = 'queue:flush {--hours= : The number of hours to retain failed job data}'; /** * The console command description. * * @var string */ protected $description = 'Flush all of the failed queue jobs'; /** * Execute the console command. * * @return void */ public function handle() { $this->laravel['queue.failer']->flush($this->option('hours')); if ($this->option('hours')) { $this->components->info("All jobs that failed more than {$this->option('hours')} hours ago have been deleted successfully."); return; } $this->components->info('All failed jobs deleted successfully.'); } } src/Illuminate/Queue/Console/RetryCommand.php 0000644 00000012423 15107327036 0015247 0 ustar 00 <?php namespace Illuminate\Queue\Console; use DateTimeInterface; use Illuminate\Console\Command; use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Queue\Events\JobRetryRequested; use Illuminate\Support\Arr; use RuntimeException; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'queue:retry')] class RetryCommand extends Command { /** * The console command signature. * * @var string */ protected $signature = 'queue:retry {id?* : The ID of the failed job or "all" to retry all jobs} {--queue= : Retry all of the failed jobs for the specified queue} {--range=* : Range of job IDs (numeric) to be retried (e.g. 1-5)}'; /** * The console command description. * * @var string */ protected $description = 'Retry a failed queue job'; /** * Execute the console command. * * @return void */ public function handle() { $jobsFound = count($ids = $this->getJobIds()) > 0; if ($jobsFound) { $this->components->info('Pushing failed queue jobs back onto the queue.'); } foreach ($ids as $id) { $job = $this->laravel['queue.failer']->find($id); if (is_null($job)) { $this->components->error("Unable to find failed job with ID [{$id}]."); } else { $this->laravel['events']->dispatch(new JobRetryRequested($job)); $this->components->task($id, fn () => $this->retryJob($job)); $this->laravel['queue.failer']->forget($id); } } $jobsFound ? $this->newLine() : $this->components->info('No retryable jobs found.'); } /** * Get the job IDs to be retried. * * @return array */ protected function getJobIds() { $ids = (array) $this->argument('id'); if (count($ids) === 1 && $ids[0] === 'all') { return Arr::pluck($this->laravel['queue.failer']->all(), 'id'); } if ($queue = $this->option('queue')) { return $this->getJobIdsByQueue($queue); } if ($ranges = (array) $this->option('range')) { $ids = array_merge($ids, $this->getJobIdsByRanges($ranges)); } return array_values(array_filter(array_unique($ids))); } /** * Get the job IDs by queue, if applicable. * * @param string $queue * @return array */ protected function getJobIdsByQueue($queue) { $ids = collect($this->laravel['queue.failer']->all()) ->where('queue', $queue) ->pluck('id') ->toArray(); if (count($ids) === 0) { $this->components->error("Unable to find failed jobs for queue [{$queue}]."); } return $ids; } /** * Get the job IDs ranges, if applicable. * * @param array $ranges * @return array */ protected function getJobIdsByRanges(array $ranges) { $ids = []; foreach ($ranges as $range) { if (preg_match('/^[0-9]+\-[0-9]+$/', $range)) { $ids = array_merge($ids, range(...explode('-', $range))); } } return $ids; } /** * Retry the queue job. * * @param \stdClass $job * @return void */ protected function retryJob($job) { $this->laravel['queue']->connection($job->connection)->pushRaw( $this->refreshRetryUntil($this->resetAttempts($job->payload)), $job->queue ); } /** * Reset the payload attempts. * * Applicable to Redis and other jobs which store attempts in their payload. * * @param string $payload * @return string */ protected function resetAttempts($payload) { $payload = json_decode($payload, true); if (isset($payload['attempts'])) { $payload['attempts'] = 0; } return json_encode($payload); } /** * Refresh the "retry until" timestamp for the job. * * @param string $payload * @return string * * @throws \RuntimeException */ protected function refreshRetryUntil($payload) { $payload = json_decode($payload, true); if (! isset($payload['data']['command'])) { return json_encode($payload); } if (str_starts_with($payload['data']['command'], 'O:')) { $instance = unserialize($payload['data']['command']); } elseif ($this->laravel->bound(Encrypter::class)) { $instance = unserialize($this->laravel->make(Encrypter::class)->decrypt($payload['data']['command'])); } if (! isset($instance)) { throw new RuntimeException('Unable to extract job payload.'); } if (is_object($instance) && ! $instance instanceof \__PHP_Incomplete_Class && method_exists($instance, 'retryUntil')) { $retryUntil = $instance->retryUntil(); $payload['retryUntil'] = $retryUntil instanceof DateTimeInterface ? $retryUntil->getTimestamp() : $retryUntil; } return json_encode($payload); } } src/Illuminate/Queue/Console/MonitorCommand.php 0000644 00000007377 15107327036 0015605 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Console\Command; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Queue\Factory; use Illuminate\Queue\Events\QueueBusy; use Illuminate\Support\Collection; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'queue:monitor')] class MonitorCommand extends Command { /** * The console command name. * * @var string */ protected $signature = 'queue:monitor {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 console command description. * * @var string */ protected $description = 'Monitor the size of the specified queues'; /** * The queue manager instance. * * @var \Illuminate\Contracts\Queue\Factory */ protected $manager; /** * The events dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * Create a new queue monitor command. * * @param \Illuminate\Contracts\Queue\Factory $manager * @param \Illuminate\Contracts\Events\Dispatcher $events * @return void */ public function __construct(Factory $manager, Dispatcher $events) { parent::__construct(); $this->manager = $manager; $this->events = $events; } /** * Execute the console command. * * @return void */ public function handle() { $queues = $this->parseQueues($this->argument('queues')); $this->displaySizes($queues); $this->dispatchEvents($queues); } /** * Parse the queues into an array of the connections and queues. * * @param string $queues * @return \Illuminate\Support\Collection */ protected function parseQueues($queues) { return collect(explode(',', $queues))->map(function ($queue) { [$connection, $queue] = array_pad(explode(':', $queue, 2), 2, null); if (! isset($queue)) { $queue = $connection; $connection = $this->laravel['config']['queue.default']; } return [ 'connection' => $connection, 'queue' => $queue, 'size' => $size = $this->manager->connection($connection)->size($queue), 'status' => $size >= $this->option('max') ? '<fg=yellow;options=bold>ALERT</>' : '<fg=green;options=bold>OK</>', ]; }); } /** * Display the queue sizes in the console. * * @param \Illuminate\Support\Collection $queues * @return void */ protected function displaySizes(Collection $queues) { $this->newLine(); $this->components->twoColumnDetail('<fg=gray>Queue name</>', '<fg=gray>Size / Status</>'); $queues->each(function ($queue) { $name = '['.$queue['connection'].'] '.$queue['queue']; $status = '['.$queue['size'].'] '.$queue['status']; $this->components->twoColumnDetail($name, $status); }); $this->newLine(); } /** * Fire the monitoring events. * * @param \Illuminate\Support\Collection $queues * @return void */ protected function dispatchEvents(Collection $queues) { foreach ($queues as $queue) { if ($queue['status'] == '<fg=green;options=bold>OK</>') { continue; } $this->events->dispatch( new QueueBusy( $queue['connection'], $queue['queue'], $queue['size'], ) ); } } } src/Illuminate/Queue/Console/PruneFailedJobsCommand.php 0000644 00000002323 15107327036 0017154 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Console\Command; use Illuminate\Queue\Failed\PrunableFailedJobProvider; use Illuminate\Support\Carbon; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'queue:prune-failed')] class PruneFailedJobsCommand extends Command { /** * The console command signature. * * @var string */ protected $signature = 'queue:prune-failed {--hours=24 : The number of hours to retain failed jobs data}'; /** * The console command description. * * @var string */ protected $description = 'Prune stale entries from the failed jobs table'; /** * Execute the console command. * * @return int|null */ public function handle() { $failer = $this->laravel['queue.failer']; if ($failer instanceof PrunableFailedJobProvider) { $count = $failer->prune(Carbon::now()->subHours($this->option('hours'))); } else { $this->components->error('The ['.class_basename($failer).'] failed job storage driver does not support pruning.'); return 1; } $this->components->info("{$count} entries deleted."); } } src/Illuminate/Queue/Console/RetryBatchCommand.php 0000644 00000003224 15107327036 0016210 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Bus\BatchRepository; use Illuminate\Console\Command; use Illuminate\Contracts\Console\Isolatable; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'queue:retry-batch')] class RetryBatchCommand extends Command implements Isolatable { /** * The console command signature. * * @var string */ protected $signature = 'queue:retry-batch {id : The ID of the batch whose failed jobs should be retried}'; /** * The console command description. * * @var string */ protected $description = 'Retry the failed jobs for a batch'; /** * Execute the console command. * * @return int|null */ public function handle() { $batch = $this->laravel[BatchRepository::class]->find($id = $this->argument('id')); if (! $batch) { $this->components->error("Unable to find a batch with ID [{$id}]."); return 1; } elseif (empty($batch->failedJobIds)) { $this->components->error('The given batch does not contain any failed jobs.'); return 1; } $this->components->info("Pushing failed queue jobs of the batch [$id] back onto the queue."); foreach ($batch->failedJobIds as $failedJobId) { $this->components->task($failedJobId, fn () => $this->callSilent('queue:retry', ['id' => $failedJobId]) == 0); } $this->newLine(); } /** * Get the custom mutex name for an isolated command. * * @return string */ public function isolatableId() { return $this->argument('id'); } } src/Illuminate/Queue/Console/stubs/batches.stub 0000644 00000001663 15107327036 0015606 0 ustar 00 <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('{{table}}', function (Blueprint $table) { $table->string('id')->primary(); $table->string('name'); $table->integer('total_jobs'); $table->integer('pending_jobs'); $table->integer('failed_jobs'); $table->longText('failed_job_ids'); $table->mediumText('options')->nullable(); $table->integer('cancelled_at')->nullable(); $table->integer('created_at'); $table->integer('finished_at')->nullable(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('{{table}}'); } }; src/Illuminate/Queue/Console/stubs/jobs.stub 0000644 00000001470 15107327036 0015126 0 ustar 00 <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('{{table}}', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('queue')->index(); $table->longText('payload'); $table->unsignedTinyInteger('attempts'); $table->unsignedInteger('reserved_at')->nullable(); $table->unsignedInteger('available_at'); $table->unsignedInteger('created_at'); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('{{table}}'); } }; src/Illuminate/Queue/Console/stubs/failed_jobs.stub 0000644 00000001374 15107327036 0016435 0 ustar 00 <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('{{table}}', function (Blueprint $table) { $table->id(); $table->string('uuid')->unique(); $table->text('connection'); $table->text('queue'); $table->longText('payload'); $table->longText('exception'); $table->timestamp('failed_at')->useCurrent(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('{{table}}'); } }; src/Illuminate/Queue/Worker.php 0000644 00000063132 15107327036 0012515 0 ustar 00 <?php namespace Illuminate\Queue; use Illuminate\Contracts\Cache\Repository as CacheContract; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Queue\Factory as QueueManager; use Illuminate\Database\DetectsLostConnections; use Illuminate\Queue\Events\JobExceptionOccurred; use Illuminate\Queue\Events\JobPopped; use Illuminate\Queue\Events\JobPopping; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; use Illuminate\Queue\Events\JobReleasedAfterException; use Illuminate\Queue\Events\JobTimedOut; use Illuminate\Queue\Events\Looping; use Illuminate\Queue\Events\WorkerStopping; use Illuminate\Support\Carbon; use Throwable; class Worker { use DetectsLostConnections; const EXIT_SUCCESS = 0; const EXIT_ERROR = 1; const EXIT_MEMORY_LIMIT = 12; /** * The name of the worker. * * @var string */ protected $name; /** * The queue manager instance. * * @var \Illuminate\Contracts\Queue\Factory */ protected $manager; /** * The event dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * The cache repository implementation. * * @var \Illuminate\Contracts\Cache\Repository */ protected $cache; /** * The exception handler instance. * * @var \Illuminate\Contracts\Debug\ExceptionHandler */ protected $exceptions; /** * The callback used to determine if the application is in maintenance mode. * * @var callable */ protected $isDownForMaintenance; /** * The callback used to reset the application's scope. * * @var callable */ protected $resetScope; /** * Indicates if the worker should exit. * * @var bool */ public $shouldQuit = false; /** * Indicates if the worker is paused. * * @var bool */ public $paused = false; /** * The callbacks used to pop jobs from queues. * * @var callable[] */ protected static $popCallbacks = []; /** * Create a new queue worker. * * @param \Illuminate\Contracts\Queue\Factory $manager * @param \Illuminate\Contracts\Events\Dispatcher $events * @param \Illuminate\Contracts\Debug\ExceptionHandler $exceptions * @param callable $isDownForMaintenance * @param callable|null $resetScope * @return void */ public function __construct(QueueManager $manager, Dispatcher $events, ExceptionHandler $exceptions, callable $isDownForMaintenance, callable $resetScope = null) { $this->events = $events; $this->manager = $manager; $this->exceptions = $exceptions; $this->isDownForMaintenance = $isDownForMaintenance; $this->resetScope = $resetScope; } /** * Listen to the given queue in a loop. * * @param string $connectionName * @param string $queue * @param \Illuminate\Queue\WorkerOptions $options * @return int */ public function daemon($connectionName, $queue, WorkerOptions $options) { if ($supportsAsyncSignals = $this->supportsAsyncSignals()) { $this->listenForSignals(); } $lastRestart = $this->getTimestampOfLastQueueRestart(); [$startTime, $jobsProcessed] = [hrtime(true) / 1e9, 0]; while (true) { // Before reserving any jobs, we will make sure this queue is not paused and // if it is we will just pause this worker for a given amount of time and // make sure we do not need to kill this worker process off completely. if (! $this->daemonShouldRun($options, $connectionName, $queue)) { $status = $this->pauseWorker($options, $lastRestart); if (! is_null($status)) { return $this->stop($status, $options); } continue; } if (isset($this->resetScope)) { ($this->resetScope)(); } // First, we will attempt to get the next job off of the queue. We will also // register the timeout handler and reset the alarm for this job so it is // not stuck in a frozen state forever. Then, we can fire off this job. $job = $this->getNextJob( $this->manager->connection($connectionName), $queue ); if ($supportsAsyncSignals) { $this->registerTimeoutHandler($job, $options); } // If the daemon should run (not in maintenance mode, etc.), then we can run // fire off this job for processing. Otherwise, we will need to sleep the // worker so no more jobs are processed until they should be processed. if ($job) { $jobsProcessed++; $this->runJob($job, $connectionName, $options); if ($options->rest > 0) { $this->sleep($options->rest); } } else { $this->sleep($options->sleep); } if ($supportsAsyncSignals) { $this->resetTimeoutHandler(); } // Finally, we will check to see if we have exceeded our memory limits or if // the queue should restart based on other indications. If so, we'll stop // this worker and let whatever is "monitoring" it restart the process. $status = $this->stopIfNecessary( $options, $lastRestart, $startTime, $jobsProcessed, $job ); if (! is_null($status)) { return $this->stop($status, $options); } } } /** * Register the worker timeout handler. * * @param \Illuminate\Contracts\Queue\Job|null $job * @param \Illuminate\Queue\WorkerOptions $options * @return void */ protected function registerTimeoutHandler($job, WorkerOptions $options) { // We will register a signal handler for the alarm signal so that we can kill this // process if it is running too long because it has frozen. This uses the async // signals supported in recent versions of PHP to accomplish it conveniently. pcntl_signal(SIGALRM, function () use ($job, $options) { if ($job) { $this->markJobAsFailedIfWillExceedMaxAttempts( $job->getConnectionName(), $job, (int) $options->maxTries, $e = $this->timeoutExceededException($job) ); $this->markJobAsFailedIfWillExceedMaxExceptions( $job->getConnectionName(), $job, $e ); $this->markJobAsFailedIfItShouldFailOnTimeout( $job->getConnectionName(), $job, $e ); $this->events->dispatch(new JobTimedOut( $job->getConnectionName(), $job )); } $this->kill(static::EXIT_ERROR, $options); }, true); pcntl_alarm( max($this->timeoutForJob($job, $options), 0) ); } /** * Reset the worker timeout handler. * * @return void */ protected function resetTimeoutHandler() { pcntl_alarm(0); } /** * Get the appropriate timeout for the given job. * * @param \Illuminate\Contracts\Queue\Job|null $job * @param \Illuminate\Queue\WorkerOptions $options * @return int */ protected function timeoutForJob($job, WorkerOptions $options) { return $job && ! is_null($job->timeout()) ? $job->timeout() : $options->timeout; } /** * Determine if the daemon should process on this iteration. * * @param \Illuminate\Queue\WorkerOptions $options * @param string $connectionName * @param string $queue * @return bool */ protected function daemonShouldRun(WorkerOptions $options, $connectionName, $queue) { return ! ((($this->isDownForMaintenance)() && ! $options->force) || $this->paused || $this->events->until(new Looping($connectionName, $queue)) === false); } /** * Pause the worker for the current loop. * * @param \Illuminate\Queue\WorkerOptions $options * @param int $lastRestart * @return int|null */ protected function pauseWorker(WorkerOptions $options, $lastRestart) { $this->sleep($options->sleep > 0 ? $options->sleep : 1); return $this->stopIfNecessary($options, $lastRestart); } /** * Determine the exit code to stop the process if necessary. * * @param \Illuminate\Queue\WorkerOptions $options * @param int $lastRestart * @param int $startTime * @param int $jobsProcessed * @param mixed $job * @return int|null */ protected function stopIfNecessary(WorkerOptions $options, $lastRestart, $startTime = 0, $jobsProcessed = 0, $job = null) { return match (true) { $this->shouldQuit => static::EXIT_SUCCESS, $this->memoryExceeded($options->memory) => static::EXIT_MEMORY_LIMIT, $this->queueShouldRestart($lastRestart) => static::EXIT_SUCCESS, $options->stopWhenEmpty && is_null($job) => static::EXIT_SUCCESS, $options->maxTime && hrtime(true) / 1e9 - $startTime >= $options->maxTime => static::EXIT_SUCCESS, $options->maxJobs && $jobsProcessed >= $options->maxJobs => static::EXIT_SUCCESS, default => null }; } /** * Process the next job on the queue. * * @param string $connectionName * @param string $queue * @param \Illuminate\Queue\WorkerOptions $options * @return void */ public function runNextJob($connectionName, $queue, WorkerOptions $options) { $job = $this->getNextJob( $this->manager->connection($connectionName), $queue ); // If we're able to pull a job off of the stack, we will process it and then return // from this method. If there is no job on the queue, we will "sleep" the worker // for the specified number of seconds, then keep processing jobs after sleep. if ($job) { return $this->runJob($job, $connectionName, $options); } $this->sleep($options->sleep); } /** * Get the next job from the queue connection. * * @param \Illuminate\Contracts\Queue\Queue $connection * @param string $queue * @return \Illuminate\Contracts\Queue\Job|null */ protected function getNextJob($connection, $queue) { $popJobCallback = function ($queue) use ($connection) { return $connection->pop($queue); }; $this->raiseBeforeJobPopEvent($connection->getConnectionName()); try { if (isset(static::$popCallbacks[$this->name])) { return tap( (static::$popCallbacks[$this->name])($popJobCallback, $queue), fn ($job) => $this->raiseAfterJobPopEvent($connection->getConnectionName(), $job) ); } foreach (explode(',', $queue) as $queue) { if (! is_null($job = $popJobCallback($queue))) { $this->raiseAfterJobPopEvent($connection->getConnectionName(), $job); return $job; } } } catch (Throwable $e) { $this->exceptions->report($e); $this->stopWorkerIfLostConnection($e); $this->sleep(1); } } /** * Process the given job. * * @param \Illuminate\Contracts\Queue\Job $job * @param string $connectionName * @param \Illuminate\Queue\WorkerOptions $options * @return void */ protected function runJob($job, $connectionName, WorkerOptions $options) { try { return $this->process($connectionName, $job, $options); } catch (Throwable $e) { $this->exceptions->report($e); $this->stopWorkerIfLostConnection($e); } } /** * Stop the worker if we have lost connection to a database. * * @param \Throwable $e * @return void */ protected function stopWorkerIfLostConnection($e) { if ($this->causedByLostConnection($e)) { $this->shouldQuit = true; } } /** * Process the given job from the queue. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @param \Illuminate\Queue\WorkerOptions $options * @return void * * @throws \Throwable */ public function process($connectionName, $job, WorkerOptions $options) { try { // First we will raise the before job event and determine if the job has already run // over its maximum attempt limits, which could primarily happen when this job is // continually timing out and not actually throwing any exceptions from itself. $this->raiseBeforeJobEvent($connectionName, $job); $this->markJobAsFailedIfAlreadyExceedsMaxAttempts( $connectionName, $job, (int) $options->maxTries ); if ($job->isDeleted()) { return $this->raiseAfterJobEvent($connectionName, $job); } // Here we will fire off the job and let it process. We will catch any exceptions, so // they can be reported to the developer's logs, etc. Once the job is finished the // proper events will be fired to let any listeners know this job has completed. $job->fire(); $this->raiseAfterJobEvent($connectionName, $job); } catch (Throwable $e) { $this->handleJobException($connectionName, $job, $options, $e); } } /** * Handle an exception that occurred while the job was running. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @param \Illuminate\Queue\WorkerOptions $options * @param \Throwable $e * @return void * * @throws \Throwable */ protected function handleJobException($connectionName, $job, WorkerOptions $options, Throwable $e) { try { // First, we will go ahead and mark the job as failed if it will exceed the maximum // attempts it is allowed to run the next time we process it. If so we will just // go ahead and mark it as failed now so we do not have to release this again. if (! $job->hasFailed()) { $this->markJobAsFailedIfWillExceedMaxAttempts( $connectionName, $job, (int) $options->maxTries, $e ); $this->markJobAsFailedIfWillExceedMaxExceptions( $connectionName, $job, $e ); } $this->raiseExceptionOccurredJobEvent( $connectionName, $job, $e ); } finally { // If we catch an exception, we will attempt to release the job back onto the queue // so it is not lost entirely. This'll let the job be retried at a later time by // another listener (or this same one). We will re-throw this exception after. if (! $job->isDeleted() && ! $job->isReleased() && ! $job->hasFailed()) { $job->release($this->calculateBackoff($job, $options)); $this->events->dispatch(new JobReleasedAfterException( $connectionName, $job )); } } throw $e; } /** * Mark the given job as failed if it has exceeded the maximum allowed attempts. * * This will likely be because the job previously exceeded a timeout. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @param int $maxTries * @return void * * @throws \Throwable */ protected function markJobAsFailedIfAlreadyExceedsMaxAttempts($connectionName, $job, $maxTries) { $maxTries = ! is_null($job->maxTries()) ? $job->maxTries() : $maxTries; $retryUntil = $job->retryUntil(); if ($retryUntil && Carbon::now()->getTimestamp() <= $retryUntil) { return; } if (! $retryUntil && ($maxTries === 0 || $job->attempts() <= $maxTries)) { return; } $this->failJob($job, $e = $this->maxAttemptsExceededException($job)); throw $e; } /** * Mark the given job as failed if it has exceeded the maximum allowed attempts. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @param int $maxTries * @param \Throwable $e * @return void */ protected function markJobAsFailedIfWillExceedMaxAttempts($connectionName, $job, $maxTries, Throwable $e) { $maxTries = ! is_null($job->maxTries()) ? $job->maxTries() : $maxTries; if ($job->retryUntil() && $job->retryUntil() <= Carbon::now()->getTimestamp()) { $this->failJob($job, $e); } if (! $job->retryUntil() && $maxTries > 0 && $job->attempts() >= $maxTries) { $this->failJob($job, $e); } } /** * Mark the given job as failed if it has exceeded the maximum allowed attempts. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @param \Throwable $e * @return void */ protected function markJobAsFailedIfWillExceedMaxExceptions($connectionName, $job, Throwable $e) { if (! $this->cache || is_null($uuid = $job->uuid()) || is_null($maxExceptions = $job->maxExceptions())) { return; } if (! $this->cache->get('job-exceptions:'.$uuid)) { $this->cache->put('job-exceptions:'.$uuid, 0, Carbon::now()->addDay()); } if ($maxExceptions <= $this->cache->increment('job-exceptions:'.$uuid)) { $this->cache->forget('job-exceptions:'.$uuid); $this->failJob($job, $e); } } /** * Mark the given job as failed if it should fail on timeouts. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @param \Throwable $e * @return void */ protected function markJobAsFailedIfItShouldFailOnTimeout($connectionName, $job, Throwable $e) { if (method_exists($job, 'shouldFailOnTimeout') ? $job->shouldFailOnTimeout() : false) { $this->failJob($job, $e); } } /** * Mark the given job as failed and raise the relevant event. * * @param \Illuminate\Contracts\Queue\Job $job * @param \Throwable $e * @return void */ protected function failJob($job, Throwable $e) { $job->fail($e); } /** * Calculate the backoff for the given job. * * @param \Illuminate\Contracts\Queue\Job $job * @param \Illuminate\Queue\WorkerOptions $options * @return int */ protected function calculateBackoff($job, WorkerOptions $options) { $backoff = explode( ',', method_exists($job, 'backoff') && ! is_null($job->backoff()) ? $job->backoff() : $options->backoff ); return (int) ($backoff[$job->attempts() - 1] ?? last($backoff)); } /** * Raise the before job has been popped. * * @param string $connectionName * @return void */ protected function raiseBeforeJobPopEvent($connectionName) { $this->events->dispatch(new JobPopping($connectionName)); } /** * Raise the after job has been popped. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job|null $job * @return void */ protected function raiseAfterJobPopEvent($connectionName, $job) { $this->events->dispatch(new JobPopped( $connectionName, $job )); } /** * Raise the before queue job event. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @return void */ protected function raiseBeforeJobEvent($connectionName, $job) { $this->events->dispatch(new JobProcessing( $connectionName, $job )); } /** * Raise the after queue job event. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @return void */ protected function raiseAfterJobEvent($connectionName, $job) { $this->events->dispatch(new JobProcessed( $connectionName, $job )); } /** * Raise the exception occurred queue job event. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @param \Throwable $e * @return void */ protected function raiseExceptionOccurredJobEvent($connectionName, $job, Throwable $e) { $this->events->dispatch(new JobExceptionOccurred( $connectionName, $job, $e )); } /** * Determine if the queue worker should restart. * * @param int|null $lastRestart * @return bool */ protected function queueShouldRestart($lastRestart) { return $this->getTimestampOfLastQueueRestart() != $lastRestart; } /** * Get the last queue restart timestamp, or null. * * @return int|null */ protected function getTimestampOfLastQueueRestart() { if ($this->cache) { return $this->cache->get('illuminate:queue:restart'); } } /** * Enable async signals for the process. * * @return void */ protected function listenForSignals() { pcntl_async_signals(true); pcntl_signal(SIGQUIT, fn () => $this->shouldQuit = true); pcntl_signal(SIGTERM, fn () => $this->shouldQuit = true); pcntl_signal(SIGUSR2, fn () => $this->paused = true); pcntl_signal(SIGCONT, fn () => $this->paused = false); } /** * Determine if "async" signals are supported. * * @return bool */ protected function supportsAsyncSignals() { return extension_loaded('pcntl'); } /** * Determine if the memory limit has been exceeded. * * @param int $memoryLimit * @return bool */ public function memoryExceeded($memoryLimit) { return (memory_get_usage(true) / 1024 / 1024) >= $memoryLimit; } /** * Stop listening and bail out of the script. * * @param int $status * @param WorkerOptions|null $options * @return int */ public function stop($status = 0, $options = null) { $this->events->dispatch(new WorkerStopping($status, $options)); return $status; } /** * Kill the process. * * @param int $status * @param \Illuminate\Queue\WorkerOptions|null $options * @return never */ public function kill($status = 0, $options = null) { $this->events->dispatch(new WorkerStopping($status, $options)); if (extension_loaded('posix')) { posix_kill(getmypid(), SIGKILL); } exit($status); } /** * Create an instance of MaxAttemptsExceededException. * * @param \Illuminate\Contracts\Queue\Job $job * @return \Illuminate\Queue\MaxAttemptsExceededException */ protected function maxAttemptsExceededException($job) { return MaxAttemptsExceededException::forJob($job); } /** * Create an instance of TimeoutExceededException. * * @param \Illuminate\Contracts\Queue\Job $job * @return \Illuminate\Queue\TimeoutExceededException */ protected function timeoutExceededException($job) { return TimeoutExceededException::forJob($job); } /** * Sleep the script for a given number of seconds. * * @param int|float $seconds * @return void */ public function sleep($seconds) { if ($seconds < 1) { usleep($seconds * 1000000); } else { sleep($seconds); } } /** * Set the cache repository implementation. * * @param \Illuminate\Contracts\Cache\Repository $cache * @return $this */ public function setCache(CacheContract $cache) { $this->cache = $cache; return $this; } /** * Set the name of the worker. * * @param string $name * @return $this */ public function setName($name) { $this->name = $name; return $this; } /** * Register a callback to be executed to pick jobs. * * @param string $workerName * @param callable $callback * @return void */ public static function popUsing($workerName, $callback) { if (is_null($callback)) { unset(static::$popCallbacks[$workerName]); } else { static::$popCallbacks[$workerName] = $callback; } } /** * Get the queue manager instance. * * @return \Illuminate\Contracts\Queue\Factory */ public function getManager() { return $this->manager; } /** * Set the queue manager instance. * * @param \Illuminate\Contracts\Queue\Factory $manager * @return void */ public function setManager(QueueManager $manager) { $this->manager = $manager; } } src/Illuminate/Queue/WorkerOptions.php 0000644 00000004574 15107327036 0014076 0 ustar 00 <?php namespace Illuminate\Queue; class WorkerOptions { /** * The name of the worker. * * @var string */ public $name; /** * The number of seconds to wait before retrying a job that encountered an uncaught exception. * * @var int|int[] */ public $backoff; /** * The maximum amount of RAM the worker may consume. * * @var int */ public $memory; /** * The maximum number of seconds a child worker may run. * * @var int */ public $timeout; /** * The number of seconds to wait in between polling the queue. * * @var int */ public $sleep; /** * The number of seconds to rest between jobs. * * @var int */ public $rest; /** * The maximum number of times a job may be attempted. * * @var int */ public $maxTries; /** * Indicates if the worker should run in maintenance mode. * * @var bool */ public $force; /** * Indicates if the worker should stop when the queue is empty. * * @var bool */ public $stopWhenEmpty; /** * The maximum number of jobs to run. * * @var int */ public $maxJobs; /** * The maximum number of seconds a worker may live. * * @var int */ public $maxTime; /** * Create a new worker options instance. * * @param string $name * @param int|int[] $backoff * @param int $memory * @param int $timeout * @param int $sleep * @param int $maxTries * @param bool $force * @param bool $stopWhenEmpty * @param int $maxJobs * @param int $maxTime * @param int $rest * @return void */ public function __construct($name = 'default', $backoff = 0, $memory = 128, $timeout = 60, $sleep = 3, $maxTries = 1, $force = false, $stopWhenEmpty = false, $maxJobs = 0, $maxTime = 0, $rest = 0) { $this->name = $name; $this->backoff = $backoff; $this->sleep = $sleep; $this->rest = $rest; $this->force = $force; $this->memory = $memory; $this->timeout = $timeout; $this->maxTries = $maxTries; $this->stopWhenEmpty = $stopWhenEmpty; $this->maxJobs = $maxJobs; $this->maxTime = $maxTime; } } src/Illuminate/Queue/CallQueuedClosure.php 0000644 00000005000 15107327036 0014613 0 ustar 00 <?php namespace Illuminate\Queue; use Closure; use Illuminate\Bus\Batchable; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Laravel\SerializableClosure\SerializableClosure; use ReflectionFunction; class CallQueuedClosure implements ShouldQueue { use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * The serializable Closure instance. * * @var \Laravel\SerializableClosure\SerializableClosure */ public $closure; /** * The callbacks that should be executed on failure. * * @var array */ public $failureCallbacks = []; /** * Indicate if the job should be deleted when models are missing. * * @var bool */ public $deleteWhenMissingModels = true; /** * Create a new job instance. * * @param \Laravel\SerializableClosure\SerializableClosure $closure * @return void */ public function __construct($closure) { $this->closure = $closure; } /** * Create a new job instance. * * @param \Closure $job * @return self */ public static function create(Closure $job) { return new self(new SerializableClosure($job)); } /** * Execute the job. * * @param \Illuminate\Contracts\Container\Container $container * @return void */ public function handle(Container $container) { $container->call($this->closure->getClosure(), ['job' => $this]); } /** * Add a callback to be executed if the job fails. * * @param callable $callback * @return $this */ public function onFailure($callback) { $this->failureCallbacks[] = $callback instanceof Closure ? new SerializableClosure($callback) : $callback; return $this; } /** * Handle a job failure. * * @param \Throwable $e * @return void */ public function failed($e) { foreach ($this->failureCallbacks as $callback) { $callback($e); } } /** * Get the display name for the queued job. * * @return string */ public function displayName() { $reflection = new ReflectionFunction($this->closure->getClosure()); return 'Closure ('.basename($reflection->getFileName()).':'.$reflection->getStartLine().')'; } } src/Illuminate/Queue/NullQueue.php 0000644 00000002622 15107327036 0013160 0 ustar 00 <?php namespace Illuminate\Queue; use Illuminate\Contracts\Queue\Queue as QueueContract; class NullQueue extends Queue implements QueueContract { /** * Get the size of the queue. * * @param string|null $queue * @return int */ public function size($queue = null) { return 0; } /** * Push a new job onto the queue. * * @param string $job * @param mixed $data * @param string|null $queue * @return mixed */ public function push($job, $data = '', $queue = null) { // } /** * Push a raw payload onto the queue. * * @param string $payload * @param string|null $queue * @param array $options * @return mixed */ public function pushRaw($payload, $queue = null, array $options = []) { // } /** * Push a new job onto the queue after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param string $job * @param mixed $data * @param string|null $queue * @return mixed */ public function later($delay, $job, $data = '', $queue = null) { // } /** * Pop the next job off of the queue. * * @param string|null $queue * @return \Illuminate\Contracts\Queue\Job|null */ public function pop($queue = null) { // } } src/Illuminate/Queue/README.md 0000644 00000002301 15107327036 0012001 0 ustar 00 ## Illuminate Queue The Laravel Queue component provides a unified API across a variety of different queue services. Queues allow you to defer the processing of a time consuming task, such as sending an e-mail, until a later time, thus drastically speeding up the web requests to your application. ### Usage Instructions First, create a new Queue `Capsule` manager instance. Similar to the "Capsule" provided for the Eloquent ORM, the queue Capsule aims to make configuring the library for usage outside of the Laravel framework as easy as possible. ```PHP use Illuminate\Queue\Capsule\Manager as Queue; $queue = new Queue; $queue->addConnection([ 'driver' => 'beanstalkd', 'host' => 'localhost', 'queue' => 'default', ]); // Make this Capsule instance available globally via static methods... (optional) $queue->setAsGlobal(); ``` Once the Capsule instance has been registered. You may use it like so: ```PHP // As an instance... $queue->push('SendEmail', ['message' => $message]); // If setAsGlobal has been called... Queue::push('SendEmail', ['message' => $message]); ``` For further documentation on using the queue, consult the [Laravel framework documentation](https://laravel.com/docs). src/Illuminate/Queue/BeanstalkdQueue.php 0000644 00000012337 15107327036 0014322 0 ustar 00 <?php namespace Illuminate\Queue; use Illuminate\Contracts\Queue\Queue as QueueContract; use Illuminate\Queue\Jobs\BeanstalkdJob; use Pheanstalk\Job as PheanstalkJob; use Pheanstalk\Pheanstalk; class BeanstalkdQueue extends Queue implements QueueContract { /** * The Pheanstalk instance. * * @var \Pheanstalk\Pheanstalk */ protected $pheanstalk; /** * The name of the default tube. * * @var string */ protected $default; /** * The "time to run" for all pushed jobs. * * @var int */ protected $timeToRun; /** * The maximum number of seconds to block for a job. * * @var int */ protected $blockFor; /** * Create a new Beanstalkd queue instance. * * @param \Pheanstalk\Pheanstalk $pheanstalk * @param string $default * @param int $timeToRun * @param int $blockFor * @param bool $dispatchAfterCommit * @return void */ public function __construct(Pheanstalk $pheanstalk, $default, $timeToRun, $blockFor = 0, $dispatchAfterCommit = false) { $this->default = $default; $this->blockFor = $blockFor; $this->timeToRun = $timeToRun; $this->pheanstalk = $pheanstalk; $this->dispatchAfterCommit = $dispatchAfterCommit; } /** * Get the size of the queue. * * @param string|null $queue * @return int */ public function size($queue = null) { $queue = $this->getQueue($queue); return (int) $this->pheanstalk->statsTube($queue)->current_jobs_ready; } /** * Push a new job onto the queue. * * @param string $job * @param mixed $data * @param string|null $queue * @return mixed */ public function push($job, $data = '', $queue = null) { return $this->enqueueUsing( $job, $this->createPayload($job, $this->getQueue($queue), $data), $queue, null, function ($payload, $queue) { return $this->pushRaw($payload, $queue); } ); } /** * Push a raw payload onto the queue. * * @param string $payload * @param string|null $queue * @param array $options * @return mixed */ public function pushRaw($payload, $queue = null, array $options = []) { return $this->pheanstalk->useTube($this->getQueue($queue))->put( $payload, Pheanstalk::DEFAULT_PRIORITY, Pheanstalk::DEFAULT_DELAY, $this->timeToRun ); } /** * Push a new job onto the queue after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param string $job * @param mixed $data * @param string|null $queue * @return mixed */ public function later($delay, $job, $data = '', $queue = null) { return $this->enqueueUsing( $job, $this->createPayload($job, $this->getQueue($queue), $data), $queue, $delay, function ($payload, $queue, $delay) { return $this->pheanstalk->useTube($this->getQueue($queue))->put( $payload, Pheanstalk::DEFAULT_PRIORITY, $this->secondsUntil($delay), $this->timeToRun ); } ); } /** * Push an array of jobs onto the queue. * * @param array $jobs * @param mixed $data * @param string|null $queue * @return void */ public function bulk($jobs, $data = '', $queue = null) { foreach ((array) $jobs as $job) { if (isset($job->delay)) { $this->later($job->delay, $job, $data, $queue); } else { $this->push($job, $data, $queue); } } } /** * Pop the next job off of the queue. * * @param string|null $queue * @return \Illuminate\Contracts\Queue\Job|null */ public function pop($queue = null) { $queue = $this->getQueue($queue); $job = $this->pheanstalk->watchOnly($queue)->reserveWithTimeout($this->blockFor); if ($job instanceof PheanstalkJob) { return new BeanstalkdJob( $this->container, $this->pheanstalk, $job, $this->connectionName, $queue ); } } /** * Delete a message from the Beanstalk queue. * * @param string $queue * @param string|int $id * @return void */ public function deleteMessage($queue, $id) { $queue = $this->getQueue($queue); $this->pheanstalk->useTube($queue)->delete(new PheanstalkJob($id, '')); } /** * Get the queue or return the default. * * @param string|null $queue * @return string */ public function getQueue($queue) { return $queue ?: $this->default; } /** * Get the underlying Pheanstalk instance. * * @return \Pheanstalk\Pheanstalk */ public function getPheanstalk() { return $this->pheanstalk; } } src/Illuminate/Queue/LuaScripts.php 0000644 00000010530 15107327036 0013327 0 ustar 00 <?php namespace Illuminate\Queue; class LuaScripts { /** * Get the Lua script for computing the size of queue. * * KEYS[1] - The name of the primary queue * KEYS[2] - The name of the "delayed" queue * KEYS[3] - The name of the "reserved" queue * * @return string */ public static function size() { return <<<'LUA' return redis.call('llen', KEYS[1]) + redis.call('zcard', KEYS[2]) + redis.call('zcard', KEYS[3]) LUA; } /** * Get the Lua script for pushing jobs onto the queue. * * KEYS[1] - The queue to push the job onto, for example: queues:foo * KEYS[2] - The notification list for the queue we are pushing jobs onto, for example: queues:foo:notify * ARGV[1] - The job payload * * @return string */ public static function push() { return <<<'LUA' -- Push the job onto the queue... redis.call('rpush', KEYS[1], ARGV[1]) -- Push a notification onto the "notify" queue... redis.call('rpush', KEYS[2], 1) LUA; } /** * Get the Lua script for popping the next job off of the queue. * * KEYS[1] - The queue to pop jobs from, for example: queues:foo * KEYS[2] - The queue to place reserved jobs on, for example: queues:foo:reserved * KEYS[3] - The notify queue * ARGV[1] - The time at which the reserved job will expire * * @return string */ public static function pop() { return <<<'LUA' -- Pop the first job off of the queue... local job = redis.call('lpop', KEYS[1]) local reserved = false if(job ~= false) then -- Increment the attempt count and place job on the reserved queue... reserved = cjson.decode(job) reserved['attempts'] = reserved['attempts'] + 1 reserved = cjson.encode(reserved) redis.call('zadd', KEYS[2], ARGV[1], reserved) redis.call('lpop', KEYS[3]) end return {job, reserved} LUA; } /** * Get the Lua script for releasing reserved jobs. * * KEYS[1] - The "delayed" queue we release jobs onto, for example: queues:foo:delayed * KEYS[2] - The queue the jobs are currently on, for example: queues:foo:reserved * ARGV[1] - The raw payload of the job to add to the "delayed" queue * ARGV[2] - The UNIX timestamp at which the job should become available * * @return string */ public static function release() { return <<<'LUA' -- Remove the job from the current queue... redis.call('zrem', KEYS[2], ARGV[1]) -- Add the job onto the "delayed" queue... redis.call('zadd', KEYS[1], ARGV[2], ARGV[1]) return true LUA; } /** * Get the Lua script to migrate expired jobs back onto the queue. * * KEYS[1] - The queue we are removing jobs from, for example: queues:foo:reserved * KEYS[2] - The queue we are moving jobs to, for example: queues:foo * KEYS[3] - The notification list for the queue we are moving jobs to, for example queues:foo:notify * ARGV[1] - The current UNIX timestamp * * @return string */ public static function migrateExpiredJobs() { return <<<'LUA' -- Get all of the jobs with an expired "score"... local val = redis.call('zrangebyscore', KEYS[1], '-inf', ARGV[1], 'limit', 0, ARGV[2]) -- If we have values in the array, we will remove them from the first queue -- and add them onto the destination queue in chunks of 100, which moves -- all of the appropriate jobs onto the destination queue very safely. if(next(val) ~= nil) then redis.call('zremrangebyrank', KEYS[1], 0, #val - 1) for i = 1, #val, 100 do redis.call('rpush', KEYS[2], unpack(val, i, math.min(i+99, #val))) -- Push a notification for every job that was migrated... for j = i, math.min(i+99, #val) do redis.call('rpush', KEYS[3], 1) end end end return val LUA; } /** * Get the Lua script for removing all jobs from the queue. * * KEYS[1] - The name of the primary queue * KEYS[2] - The name of the "delayed" queue * KEYS[3] - The name of the "reserved" queue * KEYS[4] - The name of the "notify" queue * * @return string */ public static function clear() { return <<<'LUA' local size = redis.call('llen', KEYS[1]) + redis.call('zcard', KEYS[2]) + redis.call('zcard', KEYS[3]) redis.call('del', KEYS[1], KEYS[2], KEYS[3], KEYS[4]) return size LUA; } } src/Illuminate/Queue/SerializesModels.php 0000644 00000005236 15107327036 0014523 0 ustar 00 <?php namespace Illuminate\Queue; use Illuminate\Queue\Attributes\WithoutRelations; use ReflectionClass; use ReflectionProperty; trait SerializesModels { use SerializesAndRestoresModelIdentifiers; /** * Prepare the instance values for serialization. * * @return array */ public function __serialize() { $values = []; $reflectionClass = new ReflectionClass($this); [$class, $properties, $classLevelWithoutRelations] = [ get_class($this), $reflectionClass->getProperties(), ! empty($reflectionClass->getAttributes(WithoutRelations::class)), ]; foreach ($properties as $property) { if ($property->isStatic()) { continue; } if (! $property->isInitialized($this)) { continue; } $value = $this->getPropertyValue($property); if ($property->hasDefaultValue() && $value === $property->getDefaultValue()) { continue; } $name = $property->getName(); if ($property->isPrivate()) { $name = "\0{$class}\0{$name}"; } elseif ($property->isProtected()) { $name = "\0*\0{$name}"; } $values[$name] = $this->getSerializedPropertyValue( $value, ! $classLevelWithoutRelations && empty($property->getAttributes(WithoutRelations::class)) ); } return $values; } /** * Restore the model after serialization. * * @param array $values * @return void */ public function __unserialize(array $values) { $properties = (new ReflectionClass($this))->getProperties(); $class = get_class($this); foreach ($properties as $property) { if ($property->isStatic()) { continue; } $name = $property->getName(); if ($property->isPrivate()) { $name = "\0{$class}\0{$name}"; } elseif ($property->isProtected()) { $name = "\0*\0{$name}"; } if (! array_key_exists($name, $values)) { continue; } $property->setValue( $this, $this->getRestoredPropertyValue($values[$name]) ); } } /** * Get the property value for the given property. * * @param \ReflectionProperty $property * @return mixed */ protected function getPropertyValue(ReflectionProperty $property) { return $property->getValue($this); } } src/Illuminate/Queue/SqsQueue.php 0000644 00000014050 15107327036 0013012 0 ustar 00 <?php namespace Illuminate\Queue; use Aws\Sqs\SqsClient; use Illuminate\Contracts\Queue\ClearableQueue; use Illuminate\Contracts\Queue\Queue as QueueContract; use Illuminate\Queue\Jobs\SqsJob; use Illuminate\Support\Str; class SqsQueue extends Queue implements QueueContract, ClearableQueue { /** * The Amazon SQS instance. * * @var \Aws\Sqs\SqsClient */ protected $sqs; /** * The name of the default queue. * * @var string */ protected $default; /** * The queue URL prefix. * * @var string */ protected $prefix; /** * The queue name suffix. * * @var string */ protected $suffix; /** * Create a new Amazon SQS queue instance. * * @param \Aws\Sqs\SqsClient $sqs * @param string $default * @param string $prefix * @param string $suffix * @param bool $dispatchAfterCommit * @return void */ public function __construct(SqsClient $sqs, $default, $prefix = '', $suffix = '', $dispatchAfterCommit = false) { $this->sqs = $sqs; $this->prefix = $prefix; $this->default = $default; $this->suffix = $suffix; $this->dispatchAfterCommit = $dispatchAfterCommit; } /** * Get the size of the queue. * * @param string|null $queue * @return int */ public function size($queue = null) { $response = $this->sqs->getQueueAttributes([ 'QueueUrl' => $this->getQueue($queue), 'AttributeNames' => ['ApproximateNumberOfMessages'], ]); $attributes = $response->get('Attributes'); return (int) $attributes['ApproximateNumberOfMessages']; } /** * Push a new job onto the queue. * * @param string $job * @param mixed $data * @param string|null $queue * @return mixed */ public function push($job, $data = '', $queue = null) { return $this->enqueueUsing( $job, $this->createPayload($job, $queue ?: $this->default, $data), $queue, null, function ($payload, $queue) { return $this->pushRaw($payload, $queue); } ); } /** * Push a raw payload onto the queue. * * @param string $payload * @param string|null $queue * @param array $options * @return mixed */ public function pushRaw($payload, $queue = null, array $options = []) { return $this->sqs->sendMessage([ 'QueueUrl' => $this->getQueue($queue), 'MessageBody' => $payload, ])->get('MessageId'); } /** * Push a new job onto the queue after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param string $job * @param mixed $data * @param string|null $queue * @return mixed */ public function later($delay, $job, $data = '', $queue = null) { return $this->enqueueUsing( $job, $this->createPayload($job, $queue ?: $this->default, $data), $queue, $delay, function ($payload, $queue, $delay) { return $this->sqs->sendMessage([ 'QueueUrl' => $this->getQueue($queue), 'MessageBody' => $payload, 'DelaySeconds' => $this->secondsUntil($delay), ])->get('MessageId'); } ); } /** * Push an array of jobs onto the queue. * * @param array $jobs * @param mixed $data * @param string|null $queue * @return void */ public function bulk($jobs, $data = '', $queue = null) { foreach ((array) $jobs as $job) { if (isset($job->delay)) { $this->later($job->delay, $job, $data, $queue); } else { $this->push($job, $data, $queue); } } } /** * Pop the next job off of the queue. * * @param string|null $queue * @return \Illuminate\Contracts\Queue\Job|null */ public function pop($queue = null) { $response = $this->sqs->receiveMessage([ 'QueueUrl' => $queue = $this->getQueue($queue), 'AttributeNames' => ['ApproximateReceiveCount'], ]); if (! is_null($response['Messages']) && count($response['Messages']) > 0) { return new SqsJob( $this->container, $this->sqs, $response['Messages'][0], $this->connectionName, $queue ); } } /** * Delete all of the jobs from the queue. * * @param string $queue * @return int */ public function clear($queue) { return tap($this->size($queue), function () use ($queue) { $this->sqs->purgeQueue([ 'QueueUrl' => $this->getQueue($queue), ]); }); } /** * Get the queue or return the default. * * @param string|null $queue * @return string */ public function getQueue($queue) { $queue = $queue ?: $this->default; return filter_var($queue, FILTER_VALIDATE_URL) === false ? $this->suffixQueue($queue, $this->suffix) : $queue; } /** * Add the given suffix to the given queue name. * * @param string $queue * @param string $suffix * @return string */ protected function suffixQueue($queue, $suffix = '') { if (str_ends_with($queue, '.fifo')) { $queue = Str::beforeLast($queue, '.fifo'); return rtrim($this->prefix, '/').'/'.Str::finish($queue, $suffix).'.fifo'; } return rtrim($this->prefix, '/').'/'.Str::finish($queue, $this->suffix); } /** * Get the underlying SQS instance. * * @return \Aws\Sqs\SqsClient */ public function getSqs() { return $this->sqs; } } src/Illuminate/Queue/RedisQueue.php 0000644 00000023463 15107327036 0013322 0 ustar 00 <?php namespace Illuminate\Queue; use Illuminate\Contracts\Queue\ClearableQueue; use Illuminate\Contracts\Queue\Queue as QueueContract; use Illuminate\Contracts\Redis\Factory as Redis; use Illuminate\Queue\Jobs\RedisJob; use Illuminate\Support\Str; class RedisQueue extends Queue implements QueueContract, ClearableQueue { /** * The Redis factory implementation. * * @var \Illuminate\Contracts\Redis\Factory */ protected $redis; /** * The connection name. * * @var string */ protected $connection; /** * The name of the default queue. * * @var string */ protected $default; /** * The expiration time of a job. * * @var int|null */ protected $retryAfter = 60; /** * The maximum number of seconds to block for a job. * * @var int|null */ protected $blockFor = null; /** * The batch size to use when migrating delayed / expired jobs onto the primary queue. * * Negative values are infinite. * * @var int */ protected $migrationBatchSize = -1; /** * Create a new Redis queue instance. * * @param \Illuminate\Contracts\Redis\Factory $redis * @param string $default * @param string|null $connection * @param int $retryAfter * @param int|null $blockFor * @param bool $dispatchAfterCommit * @param int $migrationBatchSize * @return void */ public function __construct(Redis $redis, $default = 'default', $connection = null, $retryAfter = 60, $blockFor = null, $dispatchAfterCommit = false, $migrationBatchSize = -1) { $this->redis = $redis; $this->default = $default; $this->blockFor = $blockFor; $this->connection = $connection; $this->retryAfter = $retryAfter; $this->dispatchAfterCommit = $dispatchAfterCommit; $this->migrationBatchSize = $migrationBatchSize; } /** * Get the size of the queue. * * @param string|null $queue * @return int */ public function size($queue = null) { $queue = $this->getQueue($queue); return $this->getConnection()->eval( LuaScripts::size(), 3, $queue, $queue.':delayed', $queue.':reserved' ); } /** * Push an array of jobs onto the queue. * * @param array $jobs * @param mixed $data * @param string|null $queue * @return void */ public function bulk($jobs, $data = '', $queue = null) { $this->getConnection()->pipeline(function () use ($jobs, $data, $queue) { $this->getConnection()->transaction(function () use ($jobs, $data, $queue) { foreach ((array) $jobs as $job) { if (isset($job->delay)) { $this->later($job->delay, $job, $data, $queue); } else { $this->push($job, $data, $queue); } } }); }); } /** * Push a new job onto the queue. * * @param object|string $job * @param mixed $data * @param string|null $queue * @return mixed */ public function push($job, $data = '', $queue = null) { return $this->enqueueUsing( $job, $this->createPayload($job, $this->getQueue($queue), $data), $queue, null, function ($payload, $queue) { return $this->pushRaw($payload, $queue); } ); } /** * Push a raw payload onto the queue. * * @param string $payload * @param string|null $queue * @param array $options * @return mixed */ public function pushRaw($payload, $queue = null, array $options = []) { $this->getConnection()->eval( LuaScripts::push(), 2, $this->getQueue($queue), $this->getQueue($queue).':notify', $payload ); return json_decode($payload, true)['id'] ?? null; } /** * Push a new job onto the queue after a delay. * * @param \DateTimeInterface|\DateInterval|int $delay * @param object|string $job * @param mixed $data * @param string|null $queue * @return mixed */ public function later($delay, $job, $data = '', $queue = null) { return $this->enqueueUsing( $job, $this->createPayload($job, $this->getQueue($queue), $data), $queue, $delay, function ($payload, $queue, $delay) { return $this->laterRaw($delay, $payload, $queue); } ); } /** * Push a raw job onto the queue after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param string $payload * @param string|null $queue * @return mixed */ protected function laterRaw($delay, $payload, $queue = null) { $this->getConnection()->zadd( $this->getQueue($queue).':delayed', $this->availableAt($delay), $payload ); return json_decode($payload, true)['id'] ?? null; } /** * Create a payload string from the given job and data. * * @param string $job * @param string $queue * @param mixed $data * @return array */ protected function createPayloadArray($job, $queue, $data = '') { return array_merge(parent::createPayloadArray($job, $queue, $data), [ 'id' => $this->getRandomId(), 'attempts' => 0, ]); } /** * Pop the next job off of the queue. * * @param string|null $queue * @return \Illuminate\Contracts\Queue\Job|null */ public function pop($queue = null) { $this->migrate($prefixed = $this->getQueue($queue)); [$job, $reserved] = $this->retrieveNextJob($prefixed); if ($reserved) { return new RedisJob( $this->container, $this, $job, $reserved, $this->connectionName, $queue ?: $this->default ); } } /** * Migrate any delayed or expired jobs onto the primary queue. * * @param string $queue * @return void */ protected function migrate($queue) { $this->migrateExpiredJobs($queue.':delayed', $queue); if (! is_null($this->retryAfter)) { $this->migrateExpiredJobs($queue.':reserved', $queue); } } /** * Migrate the delayed jobs that are ready to the regular queue. * * @param string $from * @param string $to * @param int $limit * @return array */ public function migrateExpiredJobs($from, $to) { return $this->getConnection()->eval( LuaScripts::migrateExpiredJobs(), 3, $from, $to, $to.':notify', $this->currentTime(), $this->migrationBatchSize ); } /** * Retrieve the next job from the queue. * * @param string $queue * @param bool $block * @return array */ protected function retrieveNextJob($queue, $block = true) { $nextJob = $this->getConnection()->eval( LuaScripts::pop(), 3, $queue, $queue.':reserved', $queue.':notify', $this->availableAt($this->retryAfter) ); if (empty($nextJob)) { return [null, null]; } [$job, $reserved] = $nextJob; if (! $job && ! is_null($this->blockFor) && $block && $this->getConnection()->blpop([$queue.':notify'], $this->blockFor)) { return $this->retrieveNextJob($queue, false); } return [$job, $reserved]; } /** * Delete a reserved job from the queue. * * @param string $queue * @param \Illuminate\Queue\Jobs\RedisJob $job * @return void */ public function deleteReserved($queue, $job) { $this->getConnection()->zrem($this->getQueue($queue).':reserved', $job->getReservedJob()); } /** * Delete a reserved job from the reserved queue and release it. * * @param string $queue * @param \Illuminate\Queue\Jobs\RedisJob $job * @param int $delay * @return void */ public function deleteAndRelease($queue, $job, $delay) { $queue = $this->getQueue($queue); $this->getConnection()->eval( LuaScripts::release(), 2, $queue.':delayed', $queue.':reserved', $job->getReservedJob(), $this->availableAt($delay) ); } /** * Delete all of the jobs from the queue. * * @param string $queue * @return int */ public function clear($queue) { $queue = $this->getQueue($queue); return $this->getConnection()->eval( LuaScripts::clear(), 4, $queue, $queue.':delayed', $queue.':reserved', $queue.':notify' ); } /** * Get a random ID string. * * @return string */ protected function getRandomId() { return Str::random(32); } /** * Get the queue or return the default. * * @param string|null $queue * @return string */ public function getQueue($queue) { return 'queues:'.($queue ?: $this->default); } /** * Get the connection for the queue. * * @return \Illuminate\Redis\Connections\Connection */ public function getConnection() { return $this->redis->connection($this->connection); } /** * Get the underlying Redis instance. * * @return \Illuminate\Contracts\Redis\Factory */ public function getRedis() { return $this->redis; } } src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php 0000644 00000007432 15107327036 0020700 0 ustar 00 <?php namespace Illuminate\Queue; use Illuminate\Contracts\Database\ModelIdentifier; use Illuminate\Contracts\Queue\QueueableCollection; use Illuminate\Contracts\Queue\QueueableEntity; use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot; use Illuminate\Database\Eloquent\Relations\Pivot; trait SerializesAndRestoresModelIdentifiers { /** * Get the property value prepared for serialization. * * @param mixed $value * @param bool $withRelations * @return mixed */ protected function getSerializedPropertyValue($value, $withRelations = true) { if ($value instanceof QueueableCollection) { return (new ModelIdentifier( $value->getQueueableClass(), $value->getQueueableIds(), $withRelations ? $value->getQueueableRelations() : [], $value->getQueueableConnection() ))->useCollectionClass( ($collectionClass = get_class($value)) !== EloquentCollection::class ? $collectionClass : null ); } if ($value instanceof QueueableEntity) { return new ModelIdentifier( get_class($value), $value->getQueueableId(), $withRelations ? $value->getQueueableRelations() : [], $value->getQueueableConnection() ); } return $value; } /** * Get the restored property value after deserialization. * * @param mixed $value * @return mixed */ protected function getRestoredPropertyValue($value) { if (! $value instanceof ModelIdentifier) { return $value; } return is_array($value->id) ? $this->restoreCollection($value) : $this->restoreModel($value); } /** * Restore a queueable collection instance. * * @param \Illuminate\Contracts\Database\ModelIdentifier $value * @return \Illuminate\Database\Eloquent\Collection */ protected function restoreCollection($value) { if (! $value->class || count($value->id) === 0) { return ! is_null($value->collectionClass ?? null) ? new $value->collectionClass : new EloquentCollection; } $collection = $this->getQueryForModelRestoration( (new $value->class)->setConnection($value->connection), $value->id )->useWritePdo()->get(); if (is_a($value->class, Pivot::class, true) || in_array(AsPivot::class, class_uses($value->class))) { return $collection; } $collection = $collection->keyBy->getKey(); $collectionClass = get_class($collection); return new $collectionClass( collect($value->id)->map(function ($id) use ($collection) { return $collection[$id] ?? null; })->filter() ); } /** * Restore the model from the model identifier instance. * * @param \Illuminate\Contracts\Database\ModelIdentifier $value * @return \Illuminate\Database\Eloquent\Model */ public function restoreModel($value) { return $this->getQueryForModelRestoration( (new $value->class)->setConnection($value->connection), $value->id )->useWritePdo()->firstOrFail()->load($value->relations ?? []); } /** * Get the query for model restoration. * * @param \Illuminate\Database\Eloquent\Model $model * @param array|int $ids * @return \Illuminate\Database\Eloquent\Builder */ protected function getQueryForModelRestoration($model, $ids) { return $model->newQueryForRestoration($ids); } } src/Illuminate/Queue/composer.json 0000644 00000003437 15107327036 0013257 0 ustar 00 { "name": "illuminate/queue", "description": "The Illuminate Queue package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/collections": "^10.0", "illuminate/console": "^10.0", "illuminate/container": "^10.0", "illuminate/contracts": "^10.0", "illuminate/database": "^10.0", "illuminate/filesystem": "^10.0", "illuminate/pipeline": "^10.0", "illuminate/support": "^10.0", "laravel/serializable-closure": "^1.2.2", "ramsey/uuid": "^4.7", "symfony/process": "^6.2" }, "autoload": { "psr-4": { "Illuminate\\Queue\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "ext-pdo": "Required to use the database queue worker.", "ext-filter": "Required to use the SQS queue worker.", "ext-mbstring": "Required to use the database failed job providers.", "ext-pcntl": "Required to use all features of the queue worker.", "ext-posix": "Required to use all features of the queue worker.", "aws/aws-sdk-php": "Required to use the SQS queue driver and DynamoDb failed job storage (^3.235.5).", "illuminate/redis": "Required to use the Redis queue driver (^10.0).", "pda/pheanstalk": "Required to use the Beanstalk queue driver (^4.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Queue/Events/JobFailed.php 0000644 00000001433 15107327036 0014323 0 ustar 00 <?php namespace Illuminate\Queue\Events; class JobFailed { /** * The connection name. * * @var string */ public $connectionName; /** * The job instance. * * @var \Illuminate\Contracts\Queue\Job */ public $job; /** * The exception that caused the job to fail. * * @var \Throwable */ public $exception; /** * Create a new event instance. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @param \Throwable $exception * @return void */ public function __construct($connectionName, $job, $exception) { $this->job = $job; $this->exception = $exception; $this->connectionName = $connectionName; } } src/Illuminate/Queue/Events/JobProcessing.php 0000644 00000001116 15107327036 0015251 0 ustar 00 <?php namespace Illuminate\Queue\Events; class JobProcessing { /** * The connection name. * * @var string */ public $connectionName; /** * The job instance. * * @var \Illuminate\Contracts\Queue\Job */ public $job; /** * Create a new event instance. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @return void */ public function __construct($connectionName, $job) { $this->job = $job; $this->connectionName = $connectionName; } } src/Illuminate/Queue/Events/JobQueued.php 0000644 00000002404 15107327036 0014366 0 ustar 00 <?php namespace Illuminate\Queue\Events; use RuntimeException; class JobQueued { /** * The connection name. * * @var string */ public $connectionName; /** * The job ID. * * @var string|int|null */ public $id; /** * The job instance. * * @var \Closure|string|object */ public $job; /** * The job payload. * * @var string|null */ public $payload; /** * Create a new event instance. * * @param string $connectionName * @param string|int|null $id * @param \Closure|string|object $job * @param string|null $payload * @return void */ public function __construct($connectionName, $id, $job, $payload = null) { $this->connectionName = $connectionName; $this->id = $id; $this->job = $job; $this->payload = $payload; } /** * Get the decoded job payload. * * @return array */ public function payload() { if ($this->payload === null) { throw new RuntimeException('The job payload was not provided when the event was dispatched.'); } return json_decode($this->payload, true, flags: JSON_THROW_ON_ERROR); } } src/Illuminate/Queue/Events/JobProcessed.php 0000644 00000001115 15107327036 0015063 0 ustar 00 <?php namespace Illuminate\Queue\Events; class JobProcessed { /** * The connection name. * * @var string */ public $connectionName; /** * The job instance. * * @var \Illuminate\Contracts\Queue\Job */ public $job; /** * Create a new event instance. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @return void */ public function __construct($connectionName, $job) { $this->job = $job; $this->connectionName = $connectionName; } } src/Illuminate/Queue/Events/JobRetryRequested.php 0000644 00000001315 15107327036 0016125 0 ustar 00 <?php namespace Illuminate\Queue\Events; class JobRetryRequested { /** * The job instance. * * @var \stdClass */ public $job; /** * The decoded job payload. * * @var array|null */ protected $payload = null; /** * Create a new event instance. * * @param \stdClass $job * @return void */ public function __construct($job) { $this->job = $job; } /** * The job payload. * * @return array */ public function payload() { if (is_null($this->payload)) { $this->payload = json_decode($this->job->payload, true); } return $this->payload; } } src/Illuminate/Queue/Events/WorkerStopping.php 0000644 00000001155 15107327036 0015502 0 ustar 00 <?php namespace Illuminate\Queue\Events; class WorkerStopping { /** * The worker exit status. * * @var int */ public $status; /** * The worker options. * * @var \Illuminate\Queue\WorkerOptions|null */ public $workerOptions; /** * Create a new event instance. * * @param int $status * @param \Illuminate\Queue\WorkerOptions|null $workerOptions * @return void */ public function __construct($status = 0, $workerOptions = null) { $this->status = $status; $this->workerOptions = $workerOptions; } } src/Illuminate/Queue/Events/JobPopping.php 0000644 00000000607 15107327036 0014555 0 ustar 00 <?php namespace Illuminate\Queue\Events; class JobPopping { /** * The connection name. * * @var string */ public $connectionName; /** * Create a new event instance. * * @param string $connectionName * @return void */ public function __construct($connectionName) { $this->connectionName = $connectionName; } } src/Illuminate/Queue/Events/JobExceptionOccurred.php 0000644 00000001423 15107327036 0016563 0 ustar 00 <?php namespace Illuminate\Queue\Events; class JobExceptionOccurred { /** * The connection name. * * @var string */ public $connectionName; /** * The job instance. * * @var \Illuminate\Contracts\Queue\Job */ public $job; /** * The exception instance. * * @var \Throwable */ public $exception; /** * Create a new event instance. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @param \Throwable $exception * @return void */ public function __construct($connectionName, $job, $exception) { $this->job = $job; $this->exception = $exception; $this->connectionName = $connectionName; } } src/Illuminate/Queue/Events/JobReleasedAfterException.php 0000644 00000001132 15107327036 0017520 0 ustar 00 <?php namespace Illuminate\Queue\Events; class JobReleasedAfterException { /** * The connection name. * * @var string */ public $connectionName; /** * The job instance. * * @var \Illuminate\Contracts\Queue\Job */ public $job; /** * Create a new event instance. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @return void */ public function __construct($connectionName, $job) { $this->job = $job; $this->connectionName = $connectionName; } } src/Illuminate/Queue/Events/Looping.php 0000644 00000001036 15107327036 0014112 0 ustar 00 <?php namespace Illuminate\Queue\Events; class Looping { /** * The connection name. * * @var string */ public $connectionName; /** * The queue name. * * @var string */ public $queue; /** * Create a new event instance. * * @param string $connectionName * @param string $queue * @return void */ public function __construct($connectionName, $queue) { $this->queue = $queue; $this->connectionName = $connectionName; } } src/Illuminate/Queue/Events/JobPopped.php 0000644 00000001124 15107327036 0014363 0 ustar 00 <?php namespace Illuminate\Queue\Events; class JobPopped { /** * The connection name. * * @var string */ public $connectionName; /** * The job instance. * * @var \Illuminate\Contracts\Queue\Job|null */ public $job; /** * Create a new event instance. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job|null $job * @return void */ public function __construct($connectionName, $job) { $this->connectionName = $connectionName; $this->job = $job; } } src/Illuminate/Queue/Events/QueueBusy.php 0000644 00000001242 15107327036 0014431 0 ustar 00 <?php namespace Illuminate\Queue\Events; class QueueBusy { /** * The connection name. * * @var string */ public $connection; /** * The queue name. * * @var string */ public $queue; /** * The size of the queue. * * @var int */ public $size; /** * Create a new event instance. * * @param string $connection * @param string $queue * @param int $size * @return void */ public function __construct($connection, $queue, $size) { $this->connection = $connection; $this->queue = $queue; $this->size = $size; } } src/Illuminate/Queue/Events/JobTimedOut.php 0000644 00000001114 15107327036 0014665 0 ustar 00 <?php namespace Illuminate\Queue\Events; class JobTimedOut { /** * The connection name. * * @var string */ public $connectionName; /** * The job instance. * * @var \Illuminate\Contracts\Queue\Job */ public $job; /** * Create a new event instance. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job * @return void */ public function __construct($connectionName, $job) { $this->job = $job; $this->connectionName = $connectionName; } } src/Illuminate/Queue/SyncQueue.php 0000644 00000007562 15107327036 0013172 0 ustar 00 <?php namespace Illuminate\Queue; use Illuminate\Contracts\Queue\Job; use Illuminate\Contracts\Queue\Queue as QueueContract; use Illuminate\Queue\Events\JobExceptionOccurred; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; use Illuminate\Queue\Jobs\SyncJob; use Throwable; class SyncQueue extends Queue implements QueueContract { /** * Get the size of the queue. * * @param string|null $queue * @return int */ public function size($queue = null) { return 0; } /** * Push a new job onto the queue. * * @param string $job * @param mixed $data * @param string|null $queue * @return mixed * * @throws \Throwable */ public function push($job, $data = '', $queue = null) { $queueJob = $this->resolveJob($this->createPayload($job, $queue, $data), $queue); try { $this->raiseBeforeJobEvent($queueJob); $queueJob->fire(); $this->raiseAfterJobEvent($queueJob); } catch (Throwable $e) { $this->handleException($queueJob, $e); } return 0; } /** * Resolve a Sync job instance. * * @param string $payload * @param string $queue * @return \Illuminate\Queue\Jobs\SyncJob */ protected function resolveJob($payload, $queue) { return new SyncJob($this->container, $payload, $this->connectionName, $queue); } /** * Raise the before queue job event. * * @param \Illuminate\Contracts\Queue\Job $job * @return void */ protected function raiseBeforeJobEvent(Job $job) { if ($this->container->bound('events')) { $this->container['events']->dispatch(new JobProcessing($this->connectionName, $job)); } } /** * Raise the after queue job event. * * @param \Illuminate\Contracts\Queue\Job $job * @return void */ protected function raiseAfterJobEvent(Job $job) { if ($this->container->bound('events')) { $this->container['events']->dispatch(new JobProcessed($this->connectionName, $job)); } } /** * Raise the exception occurred queue job event. * * @param \Illuminate\Contracts\Queue\Job $job * @param \Throwable $e * @return void */ protected function raiseExceptionOccurredJobEvent(Job $job, Throwable $e) { if ($this->container->bound('events')) { $this->container['events']->dispatch(new JobExceptionOccurred($this->connectionName, $job, $e)); } } /** * Handle an exception that occurred while processing a job. * * @param \Illuminate\Contracts\Queue\Job $queueJob * @param \Throwable $e * @return void * * @throws \Throwable */ protected function handleException(Job $queueJob, Throwable $e) { $this->raiseExceptionOccurredJobEvent($queueJob, $e); $queueJob->fail($e); throw $e; } /** * Push a raw payload onto the queue. * * @param string $payload * @param string|null $queue * @param array $options * @return mixed */ public function pushRaw($payload, $queue = null, array $options = []) { // } /** * Push a new job onto the queue after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param string $job * @param mixed $data * @param string|null $queue * @return mixed */ public function later($delay, $job, $data = '', $queue = null) { return $this->push($job, $data, $queue); } /** * Pop the next job off of the queue. * * @param string|null $queue * @return \Illuminate\Contracts\Queue\Job|null */ public function pop($queue = null) { // } } src/Illuminate/Queue/Failed/FileFailedJobProvider.php 0000644 00000012473 15107327036 0016564 0 ustar 00 <?php namespace Illuminate\Queue\Failed; use Closure; use DateTimeInterface; use Illuminate\Support\Facades\Date; class FileFailedJobProvider implements CountableFailedJobProvider, FailedJobProviderInterface, PrunableFailedJobProvider { /** * The file path where the failed job file should be stored. * * @var string */ protected $path; /** * The maximum number of failed jobs to retain. * * @var int */ protected $limit; /** * The lock provider resolver. * * @var \Closure */ protected $lockProviderResolver; /** * Create a new database failed job provider. * * @param string $path * @param int $limit * @param \Closure|null $lockProviderResolver * @return void */ public function __construct($path, $limit = 100, ?Closure $lockProviderResolver = null) { $this->path = $path; $this->limit = $limit; $this->lockProviderResolver = $lockProviderResolver; } /** * Log a failed job into storage. * * @param string $connection * @param string $queue * @param string $payload * @param \Throwable $exception * @return int|null */ public function log($connection, $queue, $payload, $exception) { return $this->lock(function () use ($connection, $queue, $payload, $exception) { $id = json_decode($payload, true)['uuid']; $jobs = $this->read(); $failedAt = Date::now(); array_unshift($jobs, [ 'id' => $id, 'connection' => $connection, 'queue' => $queue, 'payload' => $payload, 'exception' => (string) mb_convert_encoding($exception, 'UTF-8'), 'failed_at' => $failedAt->format('Y-m-d H:i:s'), 'failed_at_timestamp' => $failedAt->getTimestamp(), ]); $this->write(array_slice($jobs, 0, $this->limit)); return $id; }); } /** * Get a list of all of the failed jobs. * * @return array */ public function all() { return $this->read(); } /** * Get a single failed job. * * @param mixed $id * @return object|null */ public function find($id) { return collect($this->read()) ->first(fn ($job) => $job->id === $id); } /** * Delete a single failed job from storage. * * @param mixed $id * @return bool */ public function forget($id) { return $this->lock(function () use ($id) { $this->write($pruned = collect($jobs = $this->read()) ->reject(fn ($job) => $job->id === $id) ->values() ->all()); return count($jobs) !== count($pruned); }); } /** * Flush all of the failed jobs from storage. * * @param int|null $hours * @return void */ public function flush($hours = null) { $this->prune(Date::now()->subHours($hours ?: 0)); } /** * Prune all of the entries older than the given date. * * @param \DateTimeInterface $before * @return int */ public function prune(DateTimeInterface $before) { return $this->lock(function () use ($before) { $jobs = $this->read(); $this->write($prunedJobs = collect($jobs)->reject(function ($job) use ($before) { return $job->failed_at_timestamp <= $before->getTimestamp(); })->values()->all()); return count($jobs) - count($prunedJobs); }); } /** * Execute the given callback while holding a lock. * * @param \Closure $callback * @return mixed */ protected function lock(Closure $callback) { if (! $this->lockProviderResolver) { return $callback(); } return ($this->lockProviderResolver)() ->lock('laravel-failed-jobs', 5) ->block(10, function () use ($callback) { return $callback(); }); } /** * Read the failed jobs file. * * @return array */ protected function read() { if (! file_exists($this->path)) { return []; } $content = file_get_contents($this->path); if (empty(trim($content))) { return []; } $content = json_decode($content); return is_array($content) ? $content : []; } /** * Write the given array of jobs to the failed jobs file. * * @param array $jobs * @return void */ protected function write(array $jobs) { file_put_contents( $this->path, json_encode($jobs, JSON_PRETTY_PRINT) ); } /** * Count the failed jobs. * * @param string|null $connection * @param string|null $queue * @return int */ public function count($connection = null, $queue = null) { if (($connection ?? $queue) === null) { return count($this->read()); } return collect($this->read()) ->filter(fn ($job) => $job->connection === ($connection ?? $job->connection) && $job->queue === ($queue ?? $job->queue)) ->count(); } } src/Illuminate/Queue/Failed/PrunableFailedJobProvider.php 0000644 00000000461 15107327036 0017447 0 ustar 00 <?php namespace Illuminate\Queue\Failed; use DateTimeInterface; interface PrunableFailedJobProvider { /** * Prune all of the entries older than the given date. * * @param \DateTimeInterface $before * @return int */ public function prune(DateTimeInterface $before); } src/Illuminate/Queue/Failed/DynamoDbFailedJobProvider.php 0000644 00000011454 15107327036 0017400 0 ustar 00 <?php namespace Illuminate\Queue\Failed; use Aws\DynamoDb\DynamoDbClient; use DateTimeInterface; use Exception; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Date; class DynamoDbFailedJobProvider implements FailedJobProviderInterface { /** * The DynamoDB client instance. * * @var \Aws\DynamoDb\DynamoDbClient */ protected $dynamo; /** * The application name. * * @var string */ protected $applicationName; /** * The table name. * * @var string */ protected $table; /** * Create a new DynamoDb failed job provider. * * @param \Aws\DynamoDb\DynamoDbClient $dynamo * @param string $applicationName * @param string $table * @return void */ public function __construct(DynamoDbClient $dynamo, $applicationName, $table) { $this->table = $table; $this->dynamo = $dynamo; $this->applicationName = $applicationName; } /** * Log a failed job into storage. * * @param string $connection * @param string $queue * @param string $payload * @param \Throwable $exception * @return string|int|null */ public function log($connection, $queue, $payload, $exception) { $id = json_decode($payload, true)['uuid']; $failedAt = Date::now(); $this->dynamo->putItem([ 'TableName' => $this->table, 'Item' => [ 'application' => ['S' => $this->applicationName], 'uuid' => ['S' => $id], 'connection' => ['S' => $connection], 'queue' => ['S' => $queue], 'payload' => ['S' => $payload], 'exception' => ['S' => (string) $exception], 'failed_at' => ['N' => (string) $failedAt->getTimestamp()], 'expires_at' => ['N' => (string) $failedAt->addDays(3)->getTimestamp()], ], ]); return $id; } /** * Get a list of all of the failed jobs. * * @return array */ public function all() { $results = $this->dynamo->query([ 'TableName' => $this->table, 'Select' => 'ALL_ATTRIBUTES', 'KeyConditionExpression' => 'application = :application', 'ExpressionAttributeValues' => [ ':application' => ['S' => $this->applicationName], ], 'ScanIndexForward' => false, ]); return collect($results['Items'])->sortByDesc(function ($result) { return (int) $result['failed_at']['N']; })->map(function ($result) { return (object) [ 'id' => $result['uuid']['S'], 'connection' => $result['connection']['S'], 'queue' => $result['queue']['S'], 'payload' => $result['payload']['S'], 'exception' => $result['exception']['S'], 'failed_at' => Carbon::createFromTimestamp( (int) $result['failed_at']['N'] )->format(DateTimeInterface::ISO8601), ]; })->all(); } /** * Get a single failed job. * * @param mixed $id * @return object|null */ public function find($id) { $result = $this->dynamo->getItem([ 'TableName' => $this->table, 'Key' => [ 'application' => ['S' => $this->applicationName], 'uuid' => ['S' => $id], ], ]); if (! isset($result['Item'])) { return; } return (object) [ 'id' => $result['Item']['uuid']['S'], 'connection' => $result['Item']['connection']['S'], 'queue' => $result['Item']['queue']['S'], 'payload' => $result['Item']['payload']['S'], 'exception' => $result['Item']['exception']['S'], 'failed_at' => Carbon::createFromTimestamp( (int) $result['Item']['failed_at']['N'] )->format(DateTimeInterface::ISO8601), ]; } /** * Delete a single failed job from storage. * * @param mixed $id * @return bool */ public function forget($id) { $this->dynamo->deleteItem([ 'TableName' => $this->table, 'Key' => [ 'application' => ['S' => $this->applicationName], 'uuid' => ['S' => $id], ], ]); return true; } /** * Flush all of the failed jobs from storage. * * @param int|null $hours * @return void * * @throws \Exception */ public function flush($hours = null) { throw new Exception("DynamoDb failed job storage may not be flushed. Please use DynamoDb's TTL features on your expires_at attribute."); } } src/Illuminate/Queue/Failed/DatabaseFailedJobProvider.php 0000644 00000007272 15107327036 0017412 0 ustar 00 <?php namespace Illuminate\Queue\Failed; use DateTimeInterface; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Support\Facades\Date; class DatabaseFailedJobProvider implements CountableFailedJobProvider, FailedJobProviderInterface, PrunableFailedJobProvider { /** * The connection resolver implementation. * * @var \Illuminate\Database\ConnectionResolverInterface */ protected $resolver; /** * The database connection name. * * @var string */ protected $database; /** * The database table. * * @var string */ protected $table; /** * Create a new database failed job provider. * * @param \Illuminate\Database\ConnectionResolverInterface $resolver * @param string $database * @param string $table * @return void */ public function __construct(ConnectionResolverInterface $resolver, $database, $table) { $this->table = $table; $this->resolver = $resolver; $this->database = $database; } /** * Log a failed job into storage. * * @param string $connection * @param string $queue * @param string $payload * @param \Throwable $exception * @return int|null */ public function log($connection, $queue, $payload, $exception) { $failed_at = Date::now(); $exception = (string) mb_convert_encoding($exception, 'UTF-8'); return $this->getTable()->insertGetId(compact( 'connection', 'queue', 'payload', 'exception', 'failed_at' )); } /** * Get a list of all of the failed jobs. * * @return array */ public function all() { return $this->getTable()->orderBy('id', 'desc')->get()->all(); } /** * Get a single failed job. * * @param mixed $id * @return object|null */ public function find($id) { return $this->getTable()->find($id); } /** * Delete a single failed job from storage. * * @param mixed $id * @return bool */ public function forget($id) { return $this->getTable()->where('id', $id)->delete() > 0; } /** * Flush all of the failed jobs from storage. * * @param int|null $hours * @return void */ public function flush($hours = null) { $this->getTable()->when($hours, function ($query, $hours) { $query->where('failed_at', '<=', Date::now()->subHours($hours)); })->delete(); } /** * Prune all of the entries older than the given date. * * @param \DateTimeInterface $before * @return int */ public function prune(DateTimeInterface $before) { $query = $this->getTable()->where('failed_at', '<', $before); $totalDeleted = 0; do { $deleted = $query->take(1000)->delete(); $totalDeleted += $deleted; } while ($deleted !== 0); return $totalDeleted; } /** * Count the failed jobs. * * @param string|null $connection * @param string|null $queue * @return int */ public function count($connection = null, $queue = null) { return $this->getTable() ->when($connection, fn ($builder) => $builder->whereConnection($connection)) ->when($queue, fn ($builder) => $builder->whereQueue($queue)) ->count(); } /** * Get a new query builder instance for the table. * * @return \Illuminate\Database\Query\Builder */ protected function getTable() { return $this->resolver->connection($this->database)->table($this->table); } } src/Illuminate/Queue/Failed/FailedJobProviderInterface.php 0000644 00000001673 15107327036 0017605 0 ustar 00 <?php namespace Illuminate\Queue\Failed; interface FailedJobProviderInterface { /** * Log a failed job into storage. * * @param string $connection * @param string $queue * @param string $payload * @param \Throwable $exception * @return string|int|null */ public function log($connection, $queue, $payload, $exception); /** * Get a list of all of the failed jobs. * * @return array */ public function all(); /** * Get a single failed job. * * @param mixed $id * @return object|null */ public function find($id); /** * Delete a single failed job from storage. * * @param mixed $id * @return bool */ public function forget($id); /** * Flush all of the failed jobs from storage. * * @param int|null $hours * @return void */ public function flush($hours = null); } src/Illuminate/Queue/Failed/DatabaseUuidFailedJobProvider.php 0000644 00000010116 15107327036 0020230 0 ustar 00 <?php namespace Illuminate\Queue\Failed; use DateTimeInterface; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Support\Facades\Date; class DatabaseUuidFailedJobProvider implements CountableFailedJobProvider, FailedJobProviderInterface, PrunableFailedJobProvider { /** * The connection resolver implementation. * * @var \Illuminate\Database\ConnectionResolverInterface */ protected $resolver; /** * The database connection name. * * @var string */ protected $database; /** * The database table. * * @var string */ protected $table; /** * Create a new database failed job provider. * * @param \Illuminate\Database\ConnectionResolverInterface $resolver * @param string $database * @param string $table * @return void */ public function __construct(ConnectionResolverInterface $resolver, $database, $table) { $this->table = $table; $this->resolver = $resolver; $this->database = $database; } /** * Log a failed job into storage. * * @param string $connection * @param string $queue * @param string $payload * @param \Throwable $exception * @return string|null */ public function log($connection, $queue, $payload, $exception) { $this->getTable()->insert([ 'uuid' => $uuid = json_decode($payload, true)['uuid'], 'connection' => $connection, 'queue' => $queue, 'payload' => $payload, 'exception' => (string) mb_convert_encoding($exception, 'UTF-8'), 'failed_at' => Date::now(), ]); return $uuid; } /** * Get a list of all of the failed jobs. * * @return array */ public function all() { return $this->getTable()->orderBy('id', 'desc')->get()->map(function ($record) { $record->id = $record->uuid; unset($record->uuid); return $record; })->all(); } /** * Get a single failed job. * * @param mixed $id * @return object|null */ public function find($id) { if ($record = $this->getTable()->where('uuid', $id)->first()) { $record->id = $record->uuid; unset($record->uuid); } return $record; } /** * Delete a single failed job from storage. * * @param mixed $id * @return bool */ public function forget($id) { return $this->getTable()->where('uuid', $id)->delete() > 0; } /** * Flush all of the failed jobs from storage. * * @param int|null $hours * @return void */ public function flush($hours = null) { $this->getTable()->when($hours, function ($query, $hours) { $query->where('failed_at', '<=', Date::now()->subHours($hours)); })->delete(); } /** * Prune all of the entries older than the given date. * * @param \DateTimeInterface $before * @return int */ public function prune(DateTimeInterface $before) { $query = $this->getTable()->where('failed_at', '<', $before); $totalDeleted = 0; do { $deleted = $query->take(1000)->delete(); $totalDeleted += $deleted; } while ($deleted !== 0); return $totalDeleted; } /** * Count the failed jobs. * * @param string|null $connection * @param string|null $queue * @return int */ public function count($connection = null, $queue = null) { return $this->getTable() ->when($connection, fn ($builder) => $builder->whereConnection($connection)) ->when($queue, fn ($builder) => $builder->whereQueue($queue)) ->count(); } /** * Get a new query builder instance for the table. * * @return \Illuminate\Database\Query\Builder */ protected function getTable() { return $this->resolver->connection($this->database)->table($this->table); } } src/Illuminate/Queue/Failed/CountableFailedJobProvider.php 0000644 00000000445 15107327036 0017615 0 ustar 00 <?php namespace Illuminate\Queue\Failed; interface CountableFailedJobProvider { /** * Count the failed jobs. * * @param string|null $connection * @param string|null $queue * @return int */ public function count($connection = null, $queue = null); } src/Illuminate/Queue/Failed/NullFailedJobProvider.php 0000644 00000002534 15107327036 0016614 0 ustar 00 <?php namespace Illuminate\Queue\Failed; class NullFailedJobProvider implements CountableFailedJobProvider, FailedJobProviderInterface { /** * Log a failed job into storage. * * @param string $connection * @param string $queue * @param string $payload * @param \Throwable $exception * @return int|null */ public function log($connection, $queue, $payload, $exception) { // } /** * Get a list of all of the failed jobs. * * @return array */ public function all() { return []; } /** * Get a single failed job. * * @param mixed $id * @return object|null */ public function find($id) { // } /** * Delete a single failed job from storage. * * @param mixed $id * @return bool */ public function forget($id) { return true; } /** * Flush all of the failed jobs from storage. * * @param int|null $hours * @return void */ public function flush($hours = null) { // } /** * Count the failed jobs. * * @param string|null $connection * @param string|null $queue * @return int */ public function count($connection = null, $queue = null) { return 0; } } src/Illuminate/Queue/ManuallyFailedException.php 0000644 00000000175 15107327036 0016010 0 ustar 00 <?php namespace Illuminate\Queue; use RuntimeException; class ManuallyFailedException extends RuntimeException { // } src/Illuminate/Queue/CallQueuedHandler.php 0000644 00000020112 15107327036 0014555 0 ustar 00 <?php namespace Illuminate\Queue; use Exception; use Illuminate\Bus\Batchable; use Illuminate\Bus\UniqueLock; use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Queue\Job; use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Pipeline\Pipeline; use ReflectionClass; use RuntimeException; class CallQueuedHandler { /** * The bus dispatcher implementation. * * @var \Illuminate\Contracts\Bus\Dispatcher */ protected $dispatcher; /** * The container instance. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * Create a new handler instance. * * @param \Illuminate\Contracts\Bus\Dispatcher $dispatcher * @param \Illuminate\Contracts\Container\Container $container * @return void */ public function __construct(Dispatcher $dispatcher, Container $container) { $this->container = $container; $this->dispatcher = $dispatcher; } /** * Handle the queued job. * * @param \Illuminate\Contracts\Queue\Job $job * @param array $data * @return void */ public function call(Job $job, array $data) { try { $command = $this->setJobInstanceIfNecessary( $job, $this->getCommand($data) ); } catch (ModelNotFoundException $e) { return $this->handleModelNotFound($job, $e); } if ($command instanceof ShouldBeUniqueUntilProcessing) { $this->ensureUniqueJobLockIsReleased($command); } $this->dispatchThroughMiddleware($job, $command); if (! $job->isReleased() && ! $command instanceof ShouldBeUniqueUntilProcessing) { $this->ensureUniqueJobLockIsReleased($command); } if (! $job->hasFailed() && ! $job->isReleased()) { $this->ensureNextJobInChainIsDispatched($command); $this->ensureSuccessfulBatchJobIsRecorded($command); } if (! $job->isDeletedOrReleased()) { $job->delete(); } } /** * Get the command from the given payload. * * @param array $data * @return mixed * * @throws \RuntimeException */ protected function getCommand(array $data) { if (str_starts_with($data['command'], 'O:')) { return unserialize($data['command']); } if ($this->container->bound(Encrypter::class)) { return unserialize($this->container[Encrypter::class]->decrypt($data['command'])); } throw new RuntimeException('Unable to extract job payload.'); } /** * Dispatch the given job / command through its specified middleware. * * @param \Illuminate\Contracts\Queue\Job $job * @param mixed $command * @return mixed */ protected function dispatchThroughMiddleware(Job $job, $command) { if ($command instanceof \__PHP_Incomplete_Class) { throw new Exception('Job is incomplete class: '.json_encode($command)); } return (new Pipeline($this->container))->send($command) ->through(array_merge(method_exists($command, 'middleware') ? $command->middleware() : [], $command->middleware ?? [])) ->then(function ($command) use ($job) { return $this->dispatcher->dispatchNow( $command, $this->resolveHandler($job, $command) ); }); } /** * Resolve the handler for the given command. * * @param \Illuminate\Contracts\Queue\Job $job * @param mixed $command * @return mixed */ protected function resolveHandler($job, $command) { $handler = $this->dispatcher->getCommandHandler($command) ?: null; if ($handler) { $this->setJobInstanceIfNecessary($job, $handler); } return $handler; } /** * Set the job instance of the given class if necessary. * * @param \Illuminate\Contracts\Queue\Job $job * @param mixed $instance * @return mixed */ protected function setJobInstanceIfNecessary(Job $job, $instance) { if (in_array(InteractsWithQueue::class, class_uses_recursive($instance))) { $instance->setJob($job); } return $instance; } /** * Ensure the next job in the chain is dispatched if applicable. * * @param mixed $command * @return void */ protected function ensureNextJobInChainIsDispatched($command) { if (method_exists($command, 'dispatchNextJobInChain')) { $command->dispatchNextJobInChain(); } } /** * Ensure the batch is notified of the successful job completion. * * @param mixed $command * @return void */ protected function ensureSuccessfulBatchJobIsRecorded($command) { $uses = class_uses_recursive($command); if (! in_array(Batchable::class, $uses) || ! in_array(InteractsWithQueue::class, $uses)) { return; } if ($batch = $command->batch()) { $batch->recordSuccessfulJob($command->job->uuid()); } } /** * Ensure the lock for a unique job is released. * * @param mixed $command * @return void */ protected function ensureUniqueJobLockIsReleased($command) { if ($command instanceof ShouldBeUnique) { (new UniqueLock($this->container->make(Cache::class)))->release($command); } } /** * Handle a model not found exception. * * @param \Illuminate\Contracts\Queue\Job $job * @param \Throwable $e * @return void */ protected function handleModelNotFound(Job $job, $e) { $class = $job->resolveName(); try { $shouldDelete = (new ReflectionClass($class)) ->getDefaultProperties()['deleteWhenMissingModels'] ?? false; } catch (Exception) { $shouldDelete = false; } if ($shouldDelete) { return $job->delete(); } return $job->fail($e); } /** * Call the failed method on the job instance. * * The exception that caused the failure will be passed. * * @param array $data * @param \Throwable|null $e * @param string $uuid * @return void */ public function failed(array $data, $e, string $uuid) { $command = $this->getCommand($data); if (! $command instanceof ShouldBeUniqueUntilProcessing) { $this->ensureUniqueJobLockIsReleased($command); } if ($command instanceof \__PHP_Incomplete_Class) { return; } $this->ensureFailedBatchJobIsRecorded($uuid, $command, $e); $this->ensureChainCatchCallbacksAreInvoked($uuid, $command, $e); if (method_exists($command, 'failed')) { $command->failed($e); } } /** * Ensure the batch is notified of the failed job. * * @param string $uuid * @param mixed $command * @param \Throwable $e * @return void */ protected function ensureFailedBatchJobIsRecorded(string $uuid, $command, $e) { if (! in_array(Batchable::class, class_uses_recursive($command))) { return; } if ($batch = $command->batch()) { $batch->recordFailedJob($uuid, $e); } } /** * Ensure the chained job catch callbacks are invoked. * * @param string $uuid * @param mixed $command * @param \Throwable $e * @return void */ protected function ensureChainCatchCallbacksAreInvoked(string $uuid, $command, $e) { if (method_exists($command, 'invokeChainCatchCallbacks')) { $command->invokeChainCatchCallbacks($e); } } } src/Illuminate/Queue/Attributes/WithoutRelations.php 0000644 00000000244 15107327036 0016711 0 ustar 00 <?php namespace Illuminate\Queue\Attributes; use Attribute; #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)] class WithoutRelations { // } src/Illuminate/Queue/TimeoutExceededException.php 0000644 00000000665 15107327036 0016202 0 ustar 00 <?php namespace Illuminate\Queue; class TimeoutExceededException extends MaxAttemptsExceededException { /** * Create a new instance for the job. * * @param \Illuminate\Contracts\Queue\Job $job * @return static */ public static function forJob($job) { return tap(new static($job->resolveName().' has timed out.'), function ($e) use ($job) { $e->job = $job; }); } } src/Illuminate/Queue/LICENSE.md 0000644 00000002063 15107327036 0012133 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Queue/Middleware/ThrottlesExceptions.php 0000644 00000010447 15107327036 0017354 0 ustar 00 <?php namespace Illuminate\Queue\Middleware; use Illuminate\Cache\RateLimiter; use Illuminate\Container\Container; use Throwable; class ThrottlesExceptions { /** * The developer specified key that the rate limiter should use. * * @var string */ protected $key; /** * Indicates whether the throttle key should use the job's UUID. * * @var bool */ protected $byJob = false; /** * The maximum number of attempts allowed before rate limiting applies. * * @var int */ protected $maxAttempts; /** * The number of minutes until the maximum attempts are reset. * * @var int */ protected $decayMinutes; /** * The number of minutes to wait before retrying the job after an exception. * * @var int */ protected $retryAfterMinutes = 0; /** * The callback that determines if rate limiting should apply. * * @var callable */ protected $whenCallback; /** * The prefix of the rate limiter key. * * @var string */ protected $prefix = 'laravel_throttles_exceptions:'; /** * The rate limiter instance. * * @var \Illuminate\Cache\RateLimiter */ protected $limiter; /** * Create a new middleware instance. * * @param int $maxAttempts * @param int $decayMinutes * @return void */ public function __construct($maxAttempts = 10, $decayMinutes = 10) { $this->maxAttempts = $maxAttempts; $this->decayMinutes = $decayMinutes; } /** * Process the job. * * @param mixed $job * @param callable $next * @return mixed */ public function handle($job, $next) { $this->limiter = Container::getInstance()->make(RateLimiter::class); if ($this->limiter->tooManyAttempts($jobKey = $this->getKey($job), $this->maxAttempts)) { return $job->release($this->getTimeUntilNextRetry($jobKey)); } try { $next($job); $this->limiter->clear($jobKey); } catch (Throwable $throwable) { if ($this->whenCallback && ! call_user_func($this->whenCallback, $throwable)) { throw $throwable; } $this->limiter->hit($jobKey, $this->decayMinutes * 60); return $job->release($this->retryAfterMinutes * 60); } } /** * Specify a callback that should determine if rate limiting behavior should apply. * * @param callable $callback * @return $this */ public function when(callable $callback) { $this->whenCallback = $callback; return $this; } /** * Set the prefix of the rate limiter key. * * @param string $prefix * @return $this */ public function withPrefix(string $prefix) { $this->prefix = $prefix; return $this; } /** * Specify the number of minutes a job should be delayed when it is released (before it has reached its max exceptions). * * @param int $backoff * @return $this */ public function backoff($backoff) { $this->retryAfterMinutes = $backoff; return $this; } /** * Get the cache key associated for the rate limiter. * * @param mixed $job * @return string */ protected function getKey($job) { if ($this->key) { return $this->prefix.$this->key; } elseif ($this->byJob) { return $this->prefix.$job->job->uuid(); } return $this->prefix.md5(get_class($job)); } /** * Set the value that the rate limiter should be keyed by. * * @param string $key * @return $this */ public function by($key) { $this->key = $key; return $this; } /** * Indicate that the throttle key should use the job's UUID. * * @return $this */ public function byJob() { $this->byJob = true; return $this; } /** * Get the number of seconds that should elapse before the job is retried. * * @param string $key * @return int */ protected function getTimeUntilNextRetry($key) { return $this->limiter->availableIn($key) + 3; } } src/Illuminate/Queue/Middleware/RateLimitedWithRedis.php 0000644 00000004720 15107327036 0017345 0 ustar 00 <?php namespace Illuminate\Queue\Middleware; use Illuminate\Container\Container; use Illuminate\Contracts\Redis\Factory as Redis; use Illuminate\Redis\Limiters\DurationLimiter; use Illuminate\Support\InteractsWithTime; class RateLimitedWithRedis extends RateLimited { use InteractsWithTime; /** * The Redis factory implementation. * * @var \Illuminate\Contracts\Redis\Factory */ protected $redis; /** * The timestamp of the end of the current duration by key. * * @var array */ public $decaysAt = []; /** * Create a new middleware instance. * * @param string $limiterName * @return void */ public function __construct($limiterName) { parent::__construct($limiterName); $this->redis = Container::getInstance()->make(Redis::class); } /** * Handle a rate limited job. * * @param mixed $job * @param callable $next * @param array $limits * @return mixed */ protected function handleJob($job, $next, array $limits) { foreach ($limits as $limit) { if ($this->tooManyAttempts($limit->key, $limit->maxAttempts, $limit->decayMinutes)) { return $this->shouldRelease ? $job->release($this->getTimeUntilNextRetry($limit->key)) : false; } } return $next($job); } /** * Determine if the given key has been "accessed" too many times. * * @param string $key * @param int $maxAttempts * @param int $decayMinutes * @return bool */ protected function tooManyAttempts($key, $maxAttempts, $decayMinutes) { $limiter = new DurationLimiter( $this->redis, $key, $maxAttempts, $decayMinutes * 60 ); return tap(! $limiter->acquire(), function () use ($key, $limiter) { $this->decaysAt[$key] = $limiter->decaysAt; }); } /** * Get the number of seconds that should elapse before the job is retried. * * @param string $key * @return int */ protected function getTimeUntilNextRetry($key) { return ($this->decaysAt[$key] - $this->currentTime()) + 3; } /** * Prepare the object after unserialization. * * @return void */ public function __wakeup() { parent::__wakeup(); $this->redis = Container::getInstance()->make(Redis::class); } } src/Illuminate/Queue/Middleware/WithoutOverlapping.php 0000644 00000006720 15107327036 0017173 0 ustar 00 <?php namespace Illuminate\Queue\Middleware; use Illuminate\Container\Container; use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Support\InteractsWithTime; class WithoutOverlapping { use InteractsWithTime; /** * The job's unique key used for preventing overlaps. * * @var string */ public $key; /** * The number of seconds before a job should be available again if no lock was acquired. * * @var \DateTimeInterface|int|null */ public $releaseAfter; /** * The number of seconds before the lock should expire. * * @var int */ public $expiresAfter; /** * The prefix of the lock key. * * @var string */ public $prefix = 'laravel-queue-overlap:'; /** * Share the key across different jobs. * * @var bool */ public $shareKey = false; /** * Create a new middleware instance. * * @param string $key * @param \DateTimeInterface|int|null $releaseAfter * @param \DateTimeInterface|int $expiresAfter * @return void */ public function __construct($key = '', $releaseAfter = 0, $expiresAfter = 0) { $this->key = $key; $this->releaseAfter = $releaseAfter; $this->expiresAfter = $this->secondsUntil($expiresAfter); } /** * Process the job. * * @param mixed $job * @param callable $next * @return mixed */ public function handle($job, $next) { $lock = Container::getInstance()->make(Cache::class)->lock( $this->getLockKey($job), $this->expiresAfter ); if ($lock->get()) { try { $next($job); } finally { $lock->release(); } } elseif (! is_null($this->releaseAfter)) { $job->release($this->releaseAfter); } } /** * Set the delay (in seconds) to release the job back to the queue. * * @param \DateTimeInterface|int $releaseAfter * @return $this */ public function releaseAfter($releaseAfter) { $this->releaseAfter = $releaseAfter; return $this; } /** * Do not release the job back to the queue if no lock can be acquired. * * @return $this */ public function dontRelease() { $this->releaseAfter = null; return $this; } /** * Set the maximum number of seconds that can elapse before the lock is released. * * @param \DateTimeInterface|\DateInterval|int $expiresAfter * @return $this */ public function expireAfter($expiresAfter) { $this->expiresAfter = $this->secondsUntil($expiresAfter); return $this; } /** * Set the prefix of the lock key. * * @param string $prefix * @return $this */ public function withPrefix(string $prefix) { $this->prefix = $prefix; return $this; } /** * Indicate that the lock key should be shared across job classes. * * @return $this */ public function shared() { $this->shareKey = true; return $this; } /** * Get the lock key for the given job. * * @param mixed $job * @return string */ public function getLockKey($job) { return $this->shareKey ? $this->prefix.$this->key : $this->prefix.get_class($job).':'.$this->key; } } src/Illuminate/Queue/Middleware/ThrottlesExceptionsWithRedis.php 0000644 00000002757 15107327036 0021204 0 ustar 00 <?php namespace Illuminate\Queue\Middleware; use Illuminate\Container\Container; use Illuminate\Contracts\Redis\Factory as Redis; use Illuminate\Redis\Limiters\DurationLimiter; use Illuminate\Support\InteractsWithTime; use Throwable; class ThrottlesExceptionsWithRedis extends ThrottlesExceptions { use InteractsWithTime; /** * The Redis factory implementation. * * @var \Illuminate\Contracts\Redis\Factory */ protected $redis; /** * The rate limiter instance. * * @var \Illuminate\Redis\Limiters\DurationLimiter */ protected $limiter; /** * Process the job. * * @param mixed $job * @param callable $next * @return mixed */ public function handle($job, $next) { $this->redis = Container::getInstance()->make(Redis::class); $this->limiter = new DurationLimiter( $this->redis, $this->getKey($job), $this->maxAttempts, $this->decayMinutes * 60 ); if ($this->limiter->tooManyAttempts()) { return $job->release($this->limiter->decaysAt - $this->currentTime()); } try { $next($job); $this->limiter->clear(); } catch (Throwable $throwable) { if ($this->whenCallback && ! call_user_func($this->whenCallback, $throwable)) { throw $throwable; } $this->limiter->acquire(); return $job->release($this->retryAfterMinutes * 60); } } } src/Illuminate/Queue/Middleware/SkipIfBatchCancelled.php 0000644 00000000577 15107327036 0017247 0 ustar 00 <?php namespace Illuminate\Queue\Middleware; class SkipIfBatchCancelled { /** * Process the job. * * @param mixed $job * @param callable $next * @return mixed */ public function handle($job, $next) { if (method_exists($job, 'batch') && $job->batch()?->cancelled()) { return; } $next($job); } } src/Illuminate/Queue/Middleware/RateLimited.php 0000644 00000006367 15107327036 0015533 0 ustar 00 <?php namespace Illuminate\Queue\Middleware; use Illuminate\Cache\RateLimiter; use Illuminate\Cache\RateLimiting\Unlimited; use Illuminate\Container\Container; use Illuminate\Support\Arr; class RateLimited { /** * The rate limiter instance. * * @var \Illuminate\Cache\RateLimiter */ protected $limiter; /** * The name of the rate limiter. * * @var string */ protected $limiterName; /** * Indicates if the job should be released if the limit is exceeded. * * @var bool */ public $shouldRelease = true; /** * Create a new middleware instance. * * @param string $limiterName * @return void */ public function __construct($limiterName) { $this->limiter = Container::getInstance()->make(RateLimiter::class); $this->limiterName = $limiterName; } /** * Process the job. * * @param mixed $job * @param callable $next * @return mixed */ public function handle($job, $next) { if (is_null($limiter = $this->limiter->limiter($this->limiterName))) { return $next($job); } $limiterResponse = $limiter($job); if ($limiterResponse instanceof Unlimited) { return $next($job); } return $this->handleJob( $job, $next, collect(Arr::wrap($limiterResponse))->map(function ($limit) { return (object) [ 'key' => md5($this->limiterName.$limit->key), 'maxAttempts' => $limit->maxAttempts, 'decayMinutes' => $limit->decayMinutes, ]; })->all() ); } /** * Handle a rate limited job. * * @param mixed $job * @param callable $next * @param array $limits * @return mixed */ protected function handleJob($job, $next, array $limits) { foreach ($limits as $limit) { if ($this->limiter->tooManyAttempts($limit->key, $limit->maxAttempts)) { return $this->shouldRelease ? $job->release($this->getTimeUntilNextRetry($limit->key)) : false; } $this->limiter->hit($limit->key, $limit->decayMinutes * 60); } return $next($job); } /** * Do not release the job back to the queue if the limit is exceeded. * * @return $this */ public function dontRelease() { $this->shouldRelease = false; return $this; } /** * Get the number of seconds that should elapse before the job is retried. * * @param string $key * @return int */ protected function getTimeUntilNextRetry($key) { return $this->limiter->availableIn($key) + 3; } /** * Prepare the object for serialization. * * @return array */ public function __sleep() { return [ 'limiterName', 'shouldRelease', ]; } /** * Prepare the object after unserialization. * * @return void */ public function __wakeup() { $this->limiter = Container::getInstance()->make(RateLimiter::class); } } src/Illuminate/Queue/Connectors/SyncConnector.php 0000644 00000000546 15107327036 0016150 0 ustar 00 <?php namespace Illuminate\Queue\Connectors; use Illuminate\Queue\SyncQueue; class SyncConnector implements ConnectorInterface { /** * Establish a queue connection. * * @param array $config * @return \Illuminate\Contracts\Queue\Queue */ public function connect(array $config) { return new SyncQueue; } } src/Illuminate/Queue/Connectors/RedisConnector.php 0000644 00000002371 15107327036 0016300 0 ustar 00 <?php namespace Illuminate\Queue\Connectors; use Illuminate\Contracts\Redis\Factory as Redis; use Illuminate\Queue\RedisQueue; class RedisConnector implements ConnectorInterface { /** * The Redis database instance. * * @var \Illuminate\Contracts\Redis\Factory */ protected $redis; /** * The connection name. * * @var string */ protected $connection; /** * Create a new Redis queue connector instance. * * @param \Illuminate\Contracts\Redis\Factory $redis * @param string|null $connection * @return void */ public function __construct(Redis $redis, $connection = null) { $this->redis = $redis; $this->connection = $connection; } /** * Establish a queue connection. * * @param array $config * @return \Illuminate\Contracts\Queue\Queue */ public function connect(array $config) { return new RedisQueue( $this->redis, $config['queue'], $config['connection'] ?? $this->connection, $config['retry_after'] ?? 60, $config['block_for'] ?? null, $config['after_commit'] ?? null, $config['migration_batch_size'] ?? -1 ); } } src/Illuminate/Queue/Connectors/DatabaseConnector.php 0000644 00000002100 15107327036 0016724 0 ustar 00 <?php namespace Illuminate\Queue\Connectors; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Queue\DatabaseQueue; class DatabaseConnector implements ConnectorInterface { /** * Database connections. * * @var \Illuminate\Database\ConnectionResolverInterface */ protected $connections; /** * Create a new connector instance. * * @param \Illuminate\Database\ConnectionResolverInterface $connections * @return void */ public function __construct(ConnectionResolverInterface $connections) { $this->connections = $connections; } /** * Establish a queue connection. * * @param array $config * @return \Illuminate\Contracts\Queue\Queue */ public function connect(array $config) { return new DatabaseQueue( $this->connections->connection($config['connection'] ?? null), $config['table'], $config['queue'], $config['retry_after'] ?? 60, $config['after_commit'] ?? null ); } } src/Illuminate/Queue/Connectors/ConnectorInterface.php 0000644 00000000407 15107327036 0017130 0 ustar 00 <?php namespace Illuminate\Queue\Connectors; interface ConnectorInterface { /** * Establish a queue connection. * * @param array $config * @return \Illuminate\Contracts\Queue\Queue */ public function connect(array $config); } src/Illuminate/Queue/Connectors/SqsConnector.php 0000644 00000002371 15107327036 0016000 0 ustar 00 <?php namespace Illuminate\Queue\Connectors; use Aws\Sqs\SqsClient; use Illuminate\Queue\SqsQueue; use Illuminate\Support\Arr; class SqsConnector implements ConnectorInterface { /** * Establish a queue connection. * * @param array $config * @return \Illuminate\Contracts\Queue\Queue */ public function connect(array $config) { $config = $this->getDefaultConfiguration($config); if (! empty($config['key']) && ! empty($config['secret'])) { $config['credentials'] = Arr::only($config, ['key', 'secret', 'token']); } return new SqsQueue( new SqsClient( Arr::except($config, ['token']) ), $config['queue'], $config['prefix'] ?? '', $config['suffix'] ?? '', $config['after_commit'] ?? null ); } /** * Get the default configuration for SQS. * * @param array $config * @return array */ protected function getDefaultConfiguration(array $config) { return array_merge([ 'version' => 'latest', 'http' => [ 'timeout' => 60, 'connect_timeout' => 60, ], ], $config); } } src/Illuminate/Queue/Connectors/BeanstalkdConnector.php 0000644 00000002031 15107327036 0017273 0 ustar 00 <?php namespace Illuminate\Queue\Connectors; use Illuminate\Queue\BeanstalkdQueue; use Pheanstalk\Connection; use Pheanstalk\Pheanstalk; class BeanstalkdConnector implements ConnectorInterface { /** * Establish a queue connection. * * @param array $config * @return \Illuminate\Contracts\Queue\Queue */ public function connect(array $config) { return new BeanstalkdQueue( $this->pheanstalk($config), $config['queue'], $config['retry_after'] ?? Pheanstalk::DEFAULT_TTR, $config['block_for'] ?? 0, $config['after_commit'] ?? null ); } /** * Create a Pheanstalk instance. * * @param array $config * @return \Pheanstalk\Pheanstalk */ protected function pheanstalk(array $config) { return Pheanstalk::create( $config['host'], $config['port'] ?? Pheanstalk::DEFAULT_PORT, $config['timeout'] ?? Connection::DEFAULT_CONNECT_TIMEOUT ); } } src/Illuminate/Queue/Connectors/NullConnector.php 0000644 00000000546 15107327036 0016146 0 ustar 00 <?php namespace Illuminate\Queue\Connectors; use Illuminate\Queue\NullQueue; class NullConnector implements ConnectorInterface { /** * Establish a queue connection. * * @param array $config * @return \Illuminate\Contracts\Queue\Queue */ public function connect(array $config) { return new NullQueue; } } src/Illuminate/Queue/InteractsWithQueue.php 0000644 00000004014 15107327036 0015033 0 ustar 00 <?php namespace Illuminate\Queue; use DateTimeInterface; use Illuminate\Contracts\Queue\Job as JobContract; use Illuminate\Support\InteractsWithTime; use InvalidArgumentException; use Throwable; trait InteractsWithQueue { use InteractsWithTime; /** * The underlying queue job instance. * * @var \Illuminate\Contracts\Queue\Job|null */ public $job; /** * Get the number of times the job has been attempted. * * @return int */ public function attempts() { return $this->job ? $this->job->attempts() : 1; } /** * Delete the job from the queue. * * @return void */ public function delete() { if ($this->job) { return $this->job->delete(); } } /** * Fail the job from the queue. * * @param \Throwable|string|null $exception * @return void */ public function fail($exception = null) { if (is_string($exception)) { $exception = new ManuallyFailedException($exception); } if ($exception instanceof Throwable || is_null($exception)) { if ($this->job) { return $this->job->fail($exception); } } else { throw new InvalidArgumentException('The fail method requires a string or an instance of Throwable.'); } } /** * Release the job back into the queue after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @return void */ public function release($delay = 0) { $delay = $delay instanceof DateTimeInterface ? $this->secondsUntil($delay) : $delay; if ($this->job) { return $this->job->release($delay); } } /** * Set the base queue job instance. * * @param \Illuminate\Contracts\Queue\Job $job * @return $this */ public function setJob(JobContract $job) { $this->job = $job; return $this; } } src/Illuminate/Queue/Jobs/SyncJob.php 0000644 00000003270 15107327036 0013505 0 ustar 00 <?php namespace Illuminate\Queue\Jobs; use Illuminate\Container\Container; use Illuminate\Contracts\Queue\Job as JobContract; class SyncJob extends Job implements JobContract { /** * The class name of the job. * * @var string */ protected $job; /** * The queue message data. * * @var string */ protected $payload; /** * Create a new job instance. * * @param \Illuminate\Container\Container $container * @param string $payload * @param string $connectionName * @param string $queue * @return void */ public function __construct(Container $container, $payload, $connectionName, $queue) { $this->queue = $queue; $this->payload = $payload; $this->container = $container; $this->connectionName = $connectionName; } /** * Release the job back into the queue after (n) seconds. * * @param int $delay * @return void */ public function release($delay = 0) { parent::release($delay); } /** * Get the number of times the job has been attempted. * * @return int */ public function attempts() { return 1; } /** * Get the job identifier. * * @return string */ public function getJobId() { return ''; } /** * Get the raw body string for the job. * * @return string */ public function getRawBody() { return $this->payload; } /** * Get the name of the queue the job belongs to. * * @return string */ public function getQueue() { return 'sync'; } } src/Illuminate/Queue/Jobs/DatabaseJobRecord.php 0000644 00000002150 15107327036 0015430 0 ustar 00 <?php namespace Illuminate\Queue\Jobs; use Illuminate\Support\InteractsWithTime; class DatabaseJobRecord { use InteractsWithTime; /** * The underlying job record. * * @var \stdClass */ protected $record; /** * Create a new job record instance. * * @param \stdClass $record * @return void */ public function __construct($record) { $this->record = $record; } /** * Increment the number of times the job has been attempted. * * @return int */ public function increment() { $this->record->attempts++; return $this->record->attempts; } /** * Update the "reserved at" timestamp of the job. * * @return int */ public function touch() { $this->record->reserved_at = $this->currentTime(); return $this->record->reserved_at; } /** * Dynamically access the underlying job information. * * @param string $key * @return mixed */ public function __get($key) { return $this->record->{$key}; } } src/Illuminate/Queue/Jobs/DatabaseJob.php 0000644 00000004325 15107327036 0014277 0 ustar 00 <?php namespace Illuminate\Queue\Jobs; use Illuminate\Container\Container; use Illuminate\Contracts\Queue\Job as JobContract; use Illuminate\Queue\DatabaseQueue; class DatabaseJob extends Job implements JobContract { /** * The database queue instance. * * @var \Illuminate\Queue\DatabaseQueue */ protected $database; /** * The database job payload. * * @var \stdClass */ protected $job; /** * Create a new job instance. * * @param \Illuminate\Container\Container $container * @param \Illuminate\Queue\DatabaseQueue $database * @param \stdClass $job * @param string $connectionName * @param string $queue * @return void */ public function __construct(Container $container, DatabaseQueue $database, $job, $connectionName, $queue) { $this->job = $job; $this->queue = $queue; $this->database = $database; $this->container = $container; $this->connectionName = $connectionName; } /** * Release the job back into the queue after (n) seconds. * * @param int $delay * @return void */ public function release($delay = 0) { parent::release($delay); $this->database->deleteAndRelease($this->queue, $this, $delay); } /** * Delete the job from the queue. * * @return void */ public function delete() { parent::delete(); $this->database->deleteReserved($this->queue, $this->job->id); } /** * Get the number of times the job has been attempted. * * @return int */ public function attempts() { return (int) $this->job->attempts; } /** * Get the job identifier. * * @return string */ public function getJobId() { return $this->job->id; } /** * Get the raw body string for the job. * * @return string */ public function getRawBody() { return $this->job->payload; } /** * Get the database job record. * * @return \Illuminate\Queue\Jobs\DatabaseJobRecord */ public function getJobRecord() { return $this->job; } } src/Illuminate/Queue/Jobs/RedisJob.php 0000644 00000005745 15107327036 0013650 0 ustar 00 <?php namespace Illuminate\Queue\Jobs; use Illuminate\Container\Container; use Illuminate\Contracts\Queue\Job as JobContract; use Illuminate\Queue\RedisQueue; class RedisJob extends Job implements JobContract { /** * The Redis queue instance. * * @var \Illuminate\Queue\RedisQueue */ protected $redis; /** * The Redis raw job payload. * * @var string */ protected $job; /** * The JSON decoded version of "$job". * * @var array */ protected $decoded; /** * The Redis job payload inside the reserved queue. * * @var string */ protected $reserved; /** * Create a new job instance. * * @param \Illuminate\Container\Container $container * @param \Illuminate\Queue\RedisQueue $redis * @param string $job * @param string $reserved * @param string $connectionName * @param string $queue * @return void */ public function __construct(Container $container, RedisQueue $redis, $job, $reserved, $connectionName, $queue) { // The $job variable is the original job JSON as it existed in the ready queue while // the $reserved variable is the raw JSON in the reserved queue. The exact format // of the reserved job is required in order for us to properly delete its data. $this->job = $job; $this->redis = $redis; $this->queue = $queue; $this->reserved = $reserved; $this->container = $container; $this->connectionName = $connectionName; $this->decoded = $this->payload(); } /** * Get the raw body string for the job. * * @return string */ public function getRawBody() { return $this->job; } /** * Delete the job from the queue. * * @return void */ public function delete() { parent::delete(); $this->redis->deleteReserved($this->queue, $this); } /** * Release the job back into the queue after (n) seconds. * * @param int $delay * @return void */ public function release($delay = 0) { parent::release($delay); $this->redis->deleteAndRelease($this->queue, $this, $delay); } /** * Get the number of times the job has been attempted. * * @return int */ public function attempts() { return ($this->decoded['attempts'] ?? null) + 1; } /** * Get the job identifier. * * @return string|null */ public function getJobId() { return $this->decoded['id'] ?? null; } /** * Get the underlying Redis factory implementation. * * @return \Illuminate\Queue\RedisQueue */ public function getRedisQueue() { return $this->redis; } /** * Get the underlying reserved Redis job. * * @return string */ public function getReservedJob() { return $this->reserved; } } src/Illuminate/Queue/Jobs/JobName.php 0000644 00000001236 15107327036 0013451 0 ustar 00 <?php namespace Illuminate\Queue\Jobs; use Illuminate\Support\Str; class JobName { /** * Parse the given job name into a class / method array. * * @param string $job * @return array */ public static function parse($job) { return Str::parseCallback($job, 'fire'); } /** * Get the resolved name of the queued job class. * * @param string $name * @param array $payload * @return string */ public static function resolve($name, $payload) { if (! empty($payload['displayName'])) { return $payload['displayName']; } return $name; } } src/Illuminate/Queue/Jobs/BeanstalkdJob.php 0000644 00000005300 15107327036 0014635 0 ustar 00 <?php namespace Illuminate\Queue\Jobs; use Illuminate\Container\Container; use Illuminate\Contracts\Queue\Job as JobContract; use Pheanstalk\Job as PheanstalkJob; use Pheanstalk\Pheanstalk; class BeanstalkdJob extends Job implements JobContract { /** * The Pheanstalk instance. * * @var \Pheanstalk\Pheanstalk */ protected $pheanstalk; /** * The Pheanstalk job instance. * * @var \Pheanstalk\Job */ protected $job; /** * Create a new job instance. * * @param \Illuminate\Container\Container $container * @param \Pheanstalk\Pheanstalk $pheanstalk * @param \Pheanstalk\Job $job * @param string $connectionName * @param string $queue * @return void */ public function __construct(Container $container, Pheanstalk $pheanstalk, PheanstalkJob $job, $connectionName, $queue) { $this->job = $job; $this->queue = $queue; $this->container = $container; $this->pheanstalk = $pheanstalk; $this->connectionName = $connectionName; } /** * Release the job back into the queue after (n) seconds. * * @param int $delay * @return void */ public function release($delay = 0) { parent::release($delay); $priority = Pheanstalk::DEFAULT_PRIORITY; $this->pheanstalk->release($this->job, $priority, $delay); } /** * Bury the job in the queue. * * @return void */ public function bury() { parent::release(); $this->pheanstalk->bury($this->job); } /** * Delete the job from the queue. * * @return void */ public function delete() { parent::delete(); $this->pheanstalk->delete($this->job); } /** * Get the number of times the job has been attempted. * * @return int */ public function attempts() { $stats = $this->pheanstalk->statsJob($this->job); return (int) $stats->reserves; } /** * Get the job identifier. * * @return int */ public function getJobId() { return $this->job->getId(); } /** * Get the raw body string for the job. * * @return string */ public function getRawBody() { return $this->job->getData(); } /** * Get the underlying Pheanstalk instance. * * @return \Pheanstalk\Pheanstalk */ public function getPheanstalk() { return $this->pheanstalk; } /** * Get the underlying Pheanstalk job. * * @return \Pheanstalk\Job */ public function getPheanstalkJob() { return $this->job; } } src/Illuminate/Queue/Jobs/SqsJob.php 0000644 00000005003 15107327036 0013333 0 ustar 00 <?php namespace Illuminate\Queue\Jobs; use Aws\Sqs\SqsClient; use Illuminate\Container\Container; use Illuminate\Contracts\Queue\Job as JobContract; class SqsJob extends Job implements JobContract { /** * The Amazon SQS client instance. * * @var \Aws\Sqs\SqsClient */ protected $sqs; /** * The Amazon SQS job instance. * * @var array */ protected $job; /** * Create a new job instance. * * @param \Illuminate\Container\Container $container * @param \Aws\Sqs\SqsClient $sqs * @param array $job * @param string $connectionName * @param string $queue * @return void */ public function __construct(Container $container, SqsClient $sqs, array $job, $connectionName, $queue) { $this->sqs = $sqs; $this->job = $job; $this->queue = $queue; $this->container = $container; $this->connectionName = $connectionName; } /** * Release the job back into the queue after (n) seconds. * * @param int $delay * @return void */ public function release($delay = 0) { parent::release($delay); $this->sqs->changeMessageVisibility([ 'QueueUrl' => $this->queue, 'ReceiptHandle' => $this->job['ReceiptHandle'], 'VisibilityTimeout' => $delay, ]); } /** * Delete the job from the queue. * * @return void */ public function delete() { parent::delete(); $this->sqs->deleteMessage([ 'QueueUrl' => $this->queue, 'ReceiptHandle' => $this->job['ReceiptHandle'], ]); } /** * Get the number of times the job has been attempted. * * @return int */ public function attempts() { return (int) $this->job['Attributes']['ApproximateReceiveCount']; } /** * Get the job identifier. * * @return string */ public function getJobId() { return $this->job['MessageId']; } /** * Get the raw body string for the job. * * @return string */ public function getRawBody() { return $this->job['Body']; } /** * Get the underlying SQS client instance. * * @return \Aws\Sqs\SqsClient */ public function getSqs() { return $this->sqs; } /** * Get the underlying raw SQS job. * * @return array */ public function getSqsJob() { return $this->job; } } src/Illuminate/Queue/Jobs/Job.php 0000644 00000016141 15107327036 0012651 0 ustar 00 <?php namespace Illuminate\Queue\Jobs; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Queue\Events\JobFailed; use Illuminate\Queue\ManuallyFailedException; use Illuminate\Support\InteractsWithTime; abstract class Job { use InteractsWithTime; /** * The job handler instance. * * @var mixed */ protected $instance; /** * The IoC container instance. * * @var \Illuminate\Container\Container */ protected $container; /** * Indicates if the job has been deleted. * * @var bool */ protected $deleted = false; /** * Indicates if the job has been released. * * @var bool */ protected $released = false; /** * Indicates if the job has failed. * * @var bool */ protected $failed = false; /** * The name of the connection the job belongs to. * * @var string */ protected $connectionName; /** * The name of the queue the job belongs to. * * @var string */ protected $queue; /** * Get the job identifier. * * @return string */ abstract public function getJobId(); /** * Get the raw body of the job. * * @return string */ abstract public function getRawBody(); /** * Get the UUID of the job. * * @return string|null */ public function uuid() { return $this->payload()['uuid'] ?? null; } /** * Fire the job. * * @return void */ public function fire() { $payload = $this->payload(); [$class, $method] = JobName::parse($payload['job']); ($this->instance = $this->resolve($class))->{$method}($this, $payload['data']); } /** * Delete the job from the queue. * * @return void */ public function delete() { $this->deleted = true; } /** * Determine if the job has been deleted. * * @return bool */ public function isDeleted() { return $this->deleted; } /** * Release the job back into the queue after (n) seconds. * * @param int $delay * @return void */ public function release($delay = 0) { $this->released = true; } /** * Determine if the job was released back into the queue. * * @return bool */ public function isReleased() { return $this->released; } /** * Determine if the job has been deleted or released. * * @return bool */ public function isDeletedOrReleased() { return $this->isDeleted() || $this->isReleased(); } /** * Determine if the job has been marked as a failure. * * @return bool */ public function hasFailed() { return $this->failed; } /** * Mark the job as "failed". * * @return void */ public function markAsFailed() { $this->failed = true; } /** * Delete the job, call the "failed" method, and raise the failed job event. * * @param \Throwable|null $e * @return void */ public function fail($e = null) { $this->markAsFailed(); if ($this->isDeleted()) { return; } try { // If the job has failed, we will delete it, call the "failed" method and then call // an event indicating the job has failed so it can be logged if needed. This is // to allow every developer to better keep monitor of their failed queue jobs. $this->delete(); $this->failed($e); } finally { $this->resolve(Dispatcher::class)->dispatch(new JobFailed( $this->connectionName, $this, $e ?: new ManuallyFailedException )); } } /** * Process an exception that caused the job to fail. * * @param \Throwable|null $e * @return void */ protected function failed($e) { $payload = $this->payload(); [$class, $method] = JobName::parse($payload['job']); if (method_exists($this->instance = $this->resolve($class), 'failed')) { $this->instance->failed($payload['data'], $e, $payload['uuid'] ?? ''); } } /** * Resolve the given class. * * @param string $class * @return mixed */ protected function resolve($class) { return $this->container->make($class); } /** * Get the resolved job handler instance. * * @return mixed */ public function getResolvedJob() { return $this->instance; } /** * Get the decoded body of the job. * * @return array */ public function payload() { return json_decode($this->getRawBody(), true); } /** * Get the number of times to attempt a job. * * @return int|null */ public function maxTries() { return $this->payload()['maxTries'] ?? null; } /** * Get the number of times to attempt a job after an exception. * * @return int|null */ public function maxExceptions() { return $this->payload()['maxExceptions'] ?? null; } /** * Determine if the job should fail when it timeouts. * * @return bool */ public function shouldFailOnTimeout() { return $this->payload()['failOnTimeout'] ?? false; } /** * The number of seconds to wait before retrying a job that encountered an uncaught exception. * * @return int|int[]|null */ public function backoff() { return $this->payload()['backoff'] ?? $this->payload()['delay'] ?? null; } /** * Get the number of seconds the job can run. * * @return int|null */ public function timeout() { return $this->payload()['timeout'] ?? null; } /** * Get the timestamp indicating when the job should timeout. * * @return int|null */ public function retryUntil() { return $this->payload()['retryUntil'] ?? null; } /** * Get the name of the queued job class. * * @return string */ public function getName() { return $this->payload()['job']; } /** * Get the resolved name of the queued job class. * * Resolves the name of "wrapped" jobs such as class-based handlers. * * @return string */ public function resolveName() { return JobName::resolve($this->getName(), $this->payload()); } /** * Get the name of the connection the job belongs to. * * @return string */ public function getConnectionName() { return $this->connectionName; } /** * Get the name of the queue the job belongs to. * * @return string */ public function getQueue() { return $this->queue; } /** * Get the service container instance. * * @return \Illuminate\Container\Container */ public function getContainer() { return $this->container; } } src/Illuminate/Queue/MaxAttemptsExceededException.php 0000644 00000001113 15107327036 0017010 0 ustar 00 <?php namespace Illuminate\Queue; use RuntimeException; class MaxAttemptsExceededException extends RuntimeException { /** * The job instance. * * @var \Illuminate\Contracts\Queue\Job|null */ public $job; /** * Create a new instance for the job. * * @param \Illuminate\Contracts\Queue\Job $job * @return static */ public static function forJob($job) { return tap(new static($job->resolveName().' has been attempted too many times.'), function ($e) use ($job) { $e->job = $job; }); } } src/Illuminate/Queue/QueueManager.php 0000644 00000015616 15107327036 0013627 0 ustar 00 <?php namespace Illuminate\Queue; use Closure; use Illuminate\Contracts\Queue\Factory as FactoryContract; use Illuminate\Contracts\Queue\Monitor as MonitorContract; use InvalidArgumentException; /** * @mixin \Illuminate\Contracts\Queue\Queue */ class QueueManager implements FactoryContract, MonitorContract { /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The array of resolved queue connections. * * @var array */ protected $connections = []; /** * The array of resolved queue connectors. * * @var array */ protected $connectors = []; /** * Create a new queue manager instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function __construct($app) { $this->app = $app; } /** * Register an event listener for the before job event. * * @param mixed $callback * @return void */ public function before($callback) { $this->app['events']->listen(Events\JobProcessing::class, $callback); } /** * Register an event listener for the after job event. * * @param mixed $callback * @return void */ public function after($callback) { $this->app['events']->listen(Events\JobProcessed::class, $callback); } /** * Register an event listener for the exception occurred job event. * * @param mixed $callback * @return void */ public function exceptionOccurred($callback) { $this->app['events']->listen(Events\JobExceptionOccurred::class, $callback); } /** * Register an event listener for the daemon queue loop. * * @param mixed $callback * @return void */ public function looping($callback) { $this->app['events']->listen(Events\Looping::class, $callback); } /** * Register an event listener for the failed job event. * * @param mixed $callback * @return void */ public function failing($callback) { $this->app['events']->listen(Events\JobFailed::class, $callback); } /** * Register an event listener for the daemon queue stopping. * * @param mixed $callback * @return void */ public function stopping($callback) { $this->app['events']->listen(Events\WorkerStopping::class, $callback); } /** * Determine if the driver is connected. * * @param string|null $name * @return bool */ public function connected($name = null) { return isset($this->connections[$name ?: $this->getDefaultDriver()]); } /** * Resolve a queue connection instance. * * @param string|null $name * @return \Illuminate\Contracts\Queue\Queue */ public function connection($name = null) { $name = $name ?: $this->getDefaultDriver(); // If the connection has not been resolved yet we will resolve it now as all // of the connections are resolved when they are actually needed so we do // not make any unnecessary connection to the various queue end-points. if (! isset($this->connections[$name])) { $this->connections[$name] = $this->resolve($name); $this->connections[$name]->setContainer($this->app); } return $this->connections[$name]; } /** * Resolve a queue connection. * * @param string $name * @return \Illuminate\Contracts\Queue\Queue * * @throws \InvalidArgumentException */ protected function resolve($name) { $config = $this->getConfig($name); if (is_null($config)) { throw new InvalidArgumentException("The [{$name}] queue connection has not been configured."); } return $this->getConnector($config['driver']) ->connect($config) ->setConnectionName($name); } /** * Get the connector for a given driver. * * @param string $driver * @return \Illuminate\Queue\Connectors\ConnectorInterface * * @throws \InvalidArgumentException */ protected function getConnector($driver) { if (! isset($this->connectors[$driver])) { throw new InvalidArgumentException("No connector for [$driver]."); } return call_user_func($this->connectors[$driver]); } /** * Add a queue connection resolver. * * @param string $driver * @param \Closure $resolver * @return void */ public function extend($driver, Closure $resolver) { return $this->addConnector($driver, $resolver); } /** * Add a queue connection resolver. * * @param string $driver * @param \Closure $resolver * @return void */ public function addConnector($driver, Closure $resolver) { $this->connectors[$driver] = $resolver; } /** * Get the queue connection configuration. * * @param string $name * @return array|null */ protected function getConfig($name) { if (! is_null($name) && $name !== 'null') { return $this->app['config']["queue.connections.{$name}"]; } return ['driver' => 'null']; } /** * Get the name of the default queue connection. * * @return string */ public function getDefaultDriver() { return $this->app['config']['queue.default']; } /** * Set the name of the default queue connection. * * @param string $name * @return void */ public function setDefaultDriver($name) { $this->app['config']['queue.default'] = $name; } /** * Get the full name for the given connection. * * @param string|null $connection * @return string */ public function getName($connection = null) { return $connection ?: $this->getDefaultDriver(); } /** * Get the application instance used by the manager. * * @return \Illuminate\Contracts\Foundation\Application */ public function getApplication() { return $this->app; } /** * Set the application instance used by the manager. * * @param \Illuminate\Contracts\Foundation\Application $app * @return $this */ public function setApplication($app) { $this->app = $app; foreach ($this->connections as $connection) { $connection->setContainer($app); } return $this; } /** * Dynamically pass calls to the default connection. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->connection()->$method(...$parameters); } } src/Illuminate/Queue/Capsule/Manager.php 0000644 00000011260 15107327036 0014205 0 ustar 00 <?php namespace Illuminate\Queue\Capsule; use Illuminate\Container\Container; use Illuminate\Queue\QueueManager; use Illuminate\Queue\QueueServiceProvider; use Illuminate\Support\Traits\CapsuleManagerTrait; /** * @mixin \Illuminate\Queue\QueueManager * @mixin \Illuminate\Contracts\Queue\Queue */ class Manager { use CapsuleManagerTrait; /** * The queue manager instance. * * @var \Illuminate\Queue\QueueManager */ protected $manager; /** * Create a new queue capsule manager. * * @param \Illuminate\Container\Container|null $container * @return void */ public function __construct(Container $container = null) { $this->setupContainer($container ?: new Container); // Once we have the container setup, we will set up the default configuration // options in the container "config" bindings. This'll just make the queue // manager behave correctly since all the correct bindings are in place. $this->setupDefaultConfiguration(); $this->setupManager(); $this->registerConnectors(); } /** * Setup the default queue configuration options. * * @return void */ protected function setupDefaultConfiguration() { $this->container['config']['queue.default'] = 'default'; } /** * Build the queue manager instance. * * @return void */ protected function setupManager() { $this->manager = new QueueManager($this->container); } /** * Register the default connectors that the component ships with. * * @return void */ protected function registerConnectors() { $provider = new QueueServiceProvider($this->container); $provider->registerConnectors($this->manager); } /** * Get a connection instance from the global manager. * * @param string|null $connection * @return \Illuminate\Contracts\Queue\Queue */ public static function connection($connection = null) { return static::$instance->getConnection($connection); } /** * Push a new job onto the queue. * * @param string $job * @param mixed $data * @param string|null $queue * @param string|null $connection * @return mixed */ public static function push($job, $data = '', $queue = null, $connection = null) { return static::$instance->connection($connection)->push($job, $data, $queue); } /** * Push a new an array of jobs onto the queue. * * @param array $jobs * @param mixed $data * @param string|null $queue * @param string|null $connection * @return mixed */ public static function bulk($jobs, $data = '', $queue = null, $connection = null) { return static::$instance->connection($connection)->bulk($jobs, $data, $queue); } /** * Push a new job onto the queue after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param string $job * @param mixed $data * @param string|null $queue * @param string|null $connection * @return mixed */ public static function later($delay, $job, $data = '', $queue = null, $connection = null) { return static::$instance->connection($connection)->later($delay, $job, $data, $queue); } /** * Get a registered connection instance. * * @param string|null $name * @return \Illuminate\Contracts\Queue\Queue */ public function getConnection($name = null) { return $this->manager->connection($name); } /** * Register a connection with the manager. * * @param array $config * @param string $name * @return void */ public function addConnection(array $config, $name = 'default') { $this->container['config']["queue.connections.{$name}"] = $config; } /** * Get the queue manager instance. * * @return \Illuminate\Queue\QueueManager */ public function getQueueManager() { return $this->manager; } /** * Pass dynamic instance methods to the manager. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->manager->$method(...$parameters); } /** * Dynamically pass methods to the default connection. * * @param string $method * @param array $parameters * @return mixed */ public static function __callStatic($method, $parameters) { return static::connection()->$method(...$parameters); } } src/Illuminate/Queue/DatabaseQueue.php 0000644 00000027100 15107327036 0013750 0 ustar 00 <?php namespace Illuminate\Queue; use Illuminate\Contracts\Queue\ClearableQueue; use Illuminate\Contracts\Queue\Queue as QueueContract; use Illuminate\Database\Connection; use Illuminate\Queue\Jobs\DatabaseJob; use Illuminate\Queue\Jobs\DatabaseJobRecord; use Illuminate\Support\Carbon; use Illuminate\Support\Str; use PDO; class DatabaseQueue extends Queue implements QueueContract, ClearableQueue { /** * The database connection instance. * * @var \Illuminate\Database\Connection */ protected $database; /** * The database table that holds the jobs. * * @var string */ protected $table; /** * The name of the default queue. * * @var string */ protected $default; /** * The expiration time of a job. * * @var int|null */ protected $retryAfter = 60; /** * Create a new database queue instance. * * @param \Illuminate\Database\Connection $database * @param string $table * @param string $default * @param int $retryAfter * @param bool $dispatchAfterCommit * @return void */ public function __construct(Connection $database, $table, $default = 'default', $retryAfter = 60, $dispatchAfterCommit = false) { $this->table = $table; $this->default = $default; $this->database = $database; $this->retryAfter = $retryAfter; $this->dispatchAfterCommit = $dispatchAfterCommit; } /** * Get the size of the queue. * * @param string|null $queue * @return int */ public function size($queue = null) { return $this->database->table($this->table) ->where('queue', $this->getQueue($queue)) ->count(); } /** * Push a new job onto the queue. * * @param string $job * @param mixed $data * @param string|null $queue * @return mixed */ public function push($job, $data = '', $queue = null) { return $this->enqueueUsing( $job, $this->createPayload($job, $this->getQueue($queue), $data), $queue, null, function ($payload, $queue) { return $this->pushToDatabase($queue, $payload); } ); } /** * Push a raw payload onto the queue. * * @param string $payload * @param string|null $queue * @param array $options * @return mixed */ public function pushRaw($payload, $queue = null, array $options = []) { return $this->pushToDatabase($queue, $payload); } /** * Push a new job onto the queue after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param string $job * @param mixed $data * @param string|null $queue * @return mixed */ public function later($delay, $job, $data = '', $queue = null) { return $this->enqueueUsing( $job, $this->createPayload($job, $this->getQueue($queue), $data), $queue, $delay, function ($payload, $queue, $delay) { return $this->pushToDatabase($queue, $payload, $delay); } ); } /** * Push an array of jobs onto the queue. * * @param array $jobs * @param mixed $data * @param string|null $queue * @return mixed */ public function bulk($jobs, $data = '', $queue = null) { $queue = $this->getQueue($queue); $now = $this->availableAt(); return $this->database->table($this->table)->insert(collect((array) $jobs)->map( function ($job) use ($queue, $data, $now) { return $this->buildDatabaseRecord( $queue, $this->createPayload($job, $this->getQueue($queue), $data), isset($job->delay) ? $this->availableAt($job->delay) : $now, ); } )->all()); } /** * Release a reserved job back onto the queue after (n) seconds. * * @param string $queue * @param \Illuminate\Queue\Jobs\DatabaseJobRecord $job * @param int $delay * @return mixed */ public function release($queue, $job, $delay) { return $this->pushToDatabase($queue, $job->payload, $delay, $job->attempts); } /** * Push a raw payload to the database with a given delay of (n) seconds. * * @param string|null $queue * @param string $payload * @param \DateTimeInterface|\DateInterval|int $delay * @param int $attempts * @return mixed */ protected function pushToDatabase($queue, $payload, $delay = 0, $attempts = 0) { return $this->database->table($this->table)->insertGetId($this->buildDatabaseRecord( $this->getQueue($queue), $payload, $this->availableAt($delay), $attempts )); } /** * Create an array to insert for the given job. * * @param string|null $queue * @param string $payload * @param int $availableAt * @param int $attempts * @return array */ protected function buildDatabaseRecord($queue, $payload, $availableAt, $attempts = 0) { return [ 'queue' => $queue, 'attempts' => $attempts, 'reserved_at' => null, 'available_at' => $availableAt, 'created_at' => $this->currentTime(), 'payload' => $payload, ]; } /** * Pop the next job off of the queue. * * @param string|null $queue * @return \Illuminate\Contracts\Queue\Job|null * * @throws \Throwable */ public function pop($queue = null) { $queue = $this->getQueue($queue); return $this->database->transaction(function () use ($queue) { if ($job = $this->getNextAvailableJob($queue)) { return $this->marshalJob($queue, $job); } }); } /** * Get the next available job for the queue. * * @param string|null $queue * @return \Illuminate\Queue\Jobs\DatabaseJobRecord|null */ protected function getNextAvailableJob($queue) { $job = $this->database->table($this->table) ->lock($this->getLockForPopping()) ->where('queue', $this->getQueue($queue)) ->where(function ($query) { $this->isAvailable($query); $this->isReservedButExpired($query); }) ->orderBy('id', 'asc') ->first(); return $job ? new DatabaseJobRecord((object) $job) : null; } /** * Get the lock required for popping the next job. * * @return string|bool */ protected function getLockForPopping() { $databaseEngine = $this->database->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME); $databaseVersion = $this->database->getConfig('version') ?? $this->database->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION); if (Str::of($databaseVersion)->contains('MariaDB')) { $databaseEngine = 'mariadb'; $databaseVersion = Str::before(Str::after($databaseVersion, '5.5.5-'), '-'); } elseif (Str::of($databaseVersion)->contains(['vitess', 'PlanetScale'])) { $databaseEngine = 'vitess'; $databaseVersion = Str::before($databaseVersion, '-'); } if (($databaseEngine === 'mysql' && version_compare($databaseVersion, '8.0.1', '>=')) || ($databaseEngine === 'mariadb' && version_compare($databaseVersion, '10.6.0', '>=')) || ($databaseEngine === 'pgsql' && version_compare($databaseVersion, '9.5', '>='))) { return 'FOR UPDATE SKIP LOCKED'; } if ($databaseEngine === 'sqlsrv') { return 'with(rowlock,updlock,readpast)'; } return true; } /** * Modify the query to check for available jobs. * * @param \Illuminate\Database\Query\Builder $query * @return void */ protected function isAvailable($query) { $query->where(function ($query) { $query->whereNull('reserved_at') ->where('available_at', '<=', $this->currentTime()); }); } /** * Modify the query to check for jobs that are reserved but have expired. * * @param \Illuminate\Database\Query\Builder $query * @return void */ protected function isReservedButExpired($query) { $expiration = Carbon::now()->subSeconds($this->retryAfter)->getTimestamp(); $query->orWhere(function ($query) use ($expiration) { $query->where('reserved_at', '<=', $expiration); }); } /** * Marshal the reserved job into a DatabaseJob instance. * * @param string $queue * @param \Illuminate\Queue\Jobs\DatabaseJobRecord $job * @return \Illuminate\Queue\Jobs\DatabaseJob */ protected function marshalJob($queue, $job) { $job = $this->markJobAsReserved($job); return new DatabaseJob( $this->container, $this, $job, $this->connectionName, $queue ); } /** * Mark the given job ID as reserved. * * @param \Illuminate\Queue\Jobs\DatabaseJobRecord $job * @return \Illuminate\Queue\Jobs\DatabaseJobRecord */ protected function markJobAsReserved($job) { $this->database->table($this->table)->where('id', $job->id)->update([ 'reserved_at' => $job->touch(), 'attempts' => $job->increment(), ]); return $job; } /** * Delete a reserved job from the queue. * * @param string $queue * @param string $id * @return void * * @throws \Throwable */ public function deleteReserved($queue, $id) { $this->database->transaction(function () use ($id) { if ($this->database->table($this->table)->lockForUpdate()->find($id)) { $this->database->table($this->table)->where('id', $id)->delete(); } }); } /** * Delete a reserved job from the reserved queue and release it. * * @param string $queue * @param \Illuminate\Queue\Jobs\DatabaseJob $job * @param int $delay * @return void */ public function deleteAndRelease($queue, $job, $delay) { $this->database->transaction(function () use ($queue, $job, $delay) { if ($this->database->table($this->table)->lockForUpdate()->find($job->getJobId())) { $this->database->table($this->table)->where('id', $job->getJobId())->delete(); } $this->release($queue, $job->getJobRecord(), $delay); }); } /** * Delete all of the jobs from the queue. * * @param string $queue * @return int */ public function clear($queue) { return $this->database->table($this->table) ->where('queue', $this->getQueue($queue)) ->delete(); } /** * Get the queue or return the default. * * @param string|null $queue * @return string */ public function getQueue($queue) { return $queue ?: $this->default; } /** * Get the underlying database instance. * * @return \Illuminate\Database\Connection */ public function getDatabase() { return $this->database; } } src/Illuminate/Queue/Listener.php 0000644 00000013223 15107327036 0013025 0 ustar 00 <?php namespace Illuminate\Queue; use Closure; use Symfony\Component\Process\PhpExecutableFinder; use Symfony\Component\Process\Process; class Listener { /** * The command working path. * * @var string */ protected $commandPath; /** * The environment the workers should run under. * * @var string */ protected $environment; /** * The amount of seconds to wait before polling the queue. * * @var int */ protected $sleep = 3; /** * The number of times to try a job before logging it failed. * * @var int */ protected $maxTries = 0; /** * The output handler callback. * * @var \Closure|null */ protected $outputHandler; /** * Create a new queue listener. * * @param string $commandPath * @return void */ public function __construct($commandPath) { $this->commandPath = $commandPath; } /** * Get the PHP binary. * * @return string */ protected function phpBinary() { return (new PhpExecutableFinder)->find(false); } /** * Get the Artisan binary. * * @return string */ protected function artisanBinary() { return defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan'; } /** * Listen to the given queue connection. * * @param string $connection * @param string $queue * @param \Illuminate\Queue\ListenerOptions $options * @return void */ public function listen($connection, $queue, ListenerOptions $options) { $process = $this->makeProcess($connection, $queue, $options); while (true) { $this->runProcess($process, $options->memory); if ($options->rest) { sleep($options->rest); } } } /** * Create a new Symfony process for the worker. * * @param string $connection * @param string $queue * @param \Illuminate\Queue\ListenerOptions $options * @return \Symfony\Component\Process\Process */ public function makeProcess($connection, $queue, ListenerOptions $options) { $command = $this->createCommand( $connection, $queue, $options ); // If the environment is set, we will append it to the command array so the // workers will run under the specified environment. Otherwise, they will // just run under the production environment which is not always right. if (isset($options->environment)) { $command = $this->addEnvironment($command, $options); } return new Process( $command, $this->commandPath, null, null, $options->timeout ); } /** * Add the environment option to the given command. * * @param array $command * @param \Illuminate\Queue\ListenerOptions $options * @return array */ protected function addEnvironment($command, ListenerOptions $options) { return array_merge($command, ["--env={$options->environment}"]); } /** * Create the command with the listener options. * * @param string $connection * @param string $queue * @param \Illuminate\Queue\ListenerOptions $options * @return array */ protected function createCommand($connection, $queue, ListenerOptions $options) { return array_filter([ $this->phpBinary(), $this->artisanBinary(), 'queue:work', $connection, '--once', "--name={$options->name}", "--queue={$queue}", "--backoff={$options->backoff}", "--memory={$options->memory}", "--sleep={$options->sleep}", "--tries={$options->maxTries}", $options->force ? '--force' : null, ], function ($value) { return ! is_null($value); }); } /** * Run the given process. * * @param \Symfony\Component\Process\Process $process * @param int $memory * @return void */ public function runProcess(Process $process, $memory) { $process->run(function ($type, $line) { $this->handleWorkerOutput($type, $line); }); // Once we have run the job we'll go check if the memory limit has been exceeded // for the script. If it has, we will kill this script so the process manager // will restart this with a clean slate of memory automatically on exiting. if ($this->memoryExceeded($memory)) { $this->stop(); } } /** * Handle output from the worker process. * * @param int $type * @param string $line * @return void */ protected function handleWorkerOutput($type, $line) { if (isset($this->outputHandler)) { call_user_func($this->outputHandler, $type, $line); } } /** * Determine if the memory limit has been exceeded. * * @param int $memoryLimit * @return bool */ public function memoryExceeded($memoryLimit) { return (memory_get_usage(true) / 1024 / 1024) >= $memoryLimit; } /** * Stop listening and bail out of the script. * * @return void */ public function stop() { exit; } /** * Set the output handler callback. * * @param \Closure $outputHandler * @return void */ public function setOutputHandler(Closure $outputHandler) { $this->outputHandler = $outputHandler; } } src/Illuminate/Contracts/Queue/ShouldBeEncrypted.php 0000644 00000000125 15107327036 0016560 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface ShouldBeEncrypted { // } src/Illuminate/Contracts/Queue/Queue.php 0000644 00000004434 15107327036 0014270 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface Queue { /** * Get the size of the queue. * * @param string|null $queue * @return int */ public function size($queue = null); /** * Push a new job onto the queue. * * @param string|object $job * @param mixed $data * @param string|null $queue * @return mixed */ public function push($job, $data = '', $queue = null); /** * Push a new job onto the queue. * * @param string $queue * @param string|object $job * @param mixed $data * @return mixed */ public function pushOn($queue, $job, $data = ''); /** * Push a raw payload onto the queue. * * @param string $payload * @param string|null $queue * @param array $options * @return mixed */ public function pushRaw($payload, $queue = null, array $options = []); /** * Push a new job onto the queue after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param string|object $job * @param mixed $data * @param string|null $queue * @return mixed */ public function later($delay, $job, $data = '', $queue = null); /** * Push a new job onto a specific queue after (n) seconds. * * @param string $queue * @param \DateTimeInterface|\DateInterval|int $delay * @param string|object $job * @param mixed $data * @return mixed */ public function laterOn($queue, $delay, $job, $data = ''); /** * Push an array of jobs onto the queue. * * @param array $jobs * @param mixed $data * @param string|null $queue * @return mixed */ public function bulk($jobs, $data = '', $queue = null); /** * Pop the next job off of the queue. * * @param string|null $queue * @return \Illuminate\Contracts\Queue\Job|null */ public function pop($queue = null); /** * Get the connection name for the queue. * * @return string */ public function getConnectionName(); /** * Set the connection name for the queue. * * @param string $name * @return $this */ public function setConnectionName($name); } src/Illuminate/Contracts/Queue/ShouldBeUniqueUntilProcessing.php 0000644 00000000170 15107327036 0021142 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface ShouldBeUniqueUntilProcessing extends ShouldBeUnique { // } src/Illuminate/Contracts/Queue/EntityResolver.php 0000644 00000000403 15107327036 0016172 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface EntityResolver { /** * Resolve the entity for the given ID. * * @param string $type * @param mixed $id * @return mixed */ public function resolve($type, $id); } src/Illuminate/Contracts/Queue/ClearableQueue.php 0000644 00000000344 15107327036 0016057 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface ClearableQueue { /** * Delete all of the jobs from the queue. * * @param string $queue * @return int */ public function clear($queue); } src/Illuminate/Contracts/Queue/QueueableEntity.php 0000644 00000000741 15107327036 0016306 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface QueueableEntity { /** * Get the queueable identity for the entity. * * @return mixed */ public function getQueueableId(); /** * Get the relationships for the entity. * * @return array */ public function getQueueableRelations(); /** * Get the connection of the entity. * * @return string|null */ public function getQueueableConnection(); } src/Illuminate/Contracts/Queue/ShouldQueue.php 0000644 00000000117 15107327036 0015441 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface ShouldQueue { // } src/Illuminate/Contracts/Queue/ShouldQueueAfterCommit.php 0000644 00000000156 15107327036 0017577 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface ShouldQueueAfterCommit extends ShouldQueue { // } src/Illuminate/Contracts/Queue/ShouldBeUnique.php 0000644 00000000122 15107327036 0016066 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface ShouldBeUnique { // } src/Illuminate/Contracts/Queue/Factory.php 0000644 00000000410 15107327036 0014601 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface Factory { /** * Resolve a queue connection instance. * * @param string|null $name * @return \Illuminate\Contracts\Queue\Queue */ public function connection($name = null); } src/Illuminate/Contracts/Queue/Job.php 0000644 00000005775 15107327036 0013727 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface Job { /** * Get the UUID of the job. * * @return string|null */ public function uuid(); /** * Get the job identifier. * * @return string */ public function getJobId(); /** * Get the decoded body of the job. * * @return array */ public function payload(); /** * Fire the job. * * @return void */ public function fire(); /** * Release the job back into the queue after (n) seconds. * * @param int $delay * @return void */ public function release($delay = 0); /** * Determine if the job was released back into the queue. * * @return bool */ public function isReleased(); /** * Delete the job from the queue. * * @return void */ public function delete(); /** * Determine if the job has been deleted. * * @return bool */ public function isDeleted(); /** * Determine if the job has been deleted or released. * * @return bool */ public function isDeletedOrReleased(); /** * Get the number of times the job has been attempted. * * @return int */ public function attempts(); /** * Determine if the job has been marked as a failure. * * @return bool */ public function hasFailed(); /** * Mark the job as "failed". * * @return void */ public function markAsFailed(); /** * Delete the job, call the "failed" method, and raise the failed job event. * * @param \Throwable|null $e * @return void */ public function fail($e = null); /** * Get the number of times to attempt a job. * * @return int|null */ public function maxTries(); /** * Get the maximum number of exceptions allowed, regardless of attempts. * * @return int|null */ public function maxExceptions(); /** * Get the number of seconds the job can run. * * @return int|null */ public function timeout(); /** * Get the timestamp indicating when the job should timeout. * * @return int|null */ public function retryUntil(); /** * Get the name of the queued job class. * * @return string */ public function getName(); /** * Get the resolved name of the queued job class. * * Resolves the name of "wrapped" jobs such as class-based handlers. * * @return string */ public function resolveName(); /** * Get the name of the connection the job belongs to. * * @return string */ public function getConnectionName(); /** * Get the name of the queue the job belongs to. * * @return string */ public function getQueue(); /** * Get the raw body string for the job. * * @return string */ public function getRawBody(); } src/Illuminate/Contracts/Queue/QueueableCollection.php 0000644 00000001254 15107327036 0017125 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface QueueableCollection { /** * Get the type of the entities being queued. * * @return string|null */ public function getQueueableClass(); /** * Get the identifiers for all of the entities. * * @return array<int, mixed> */ public function getQueueableIds(); /** * Get the relationships of the entities being queued. * * @return array<int, string> */ public function getQueueableRelations(); /** * Get the connection of the entities being queued. * * @return string|null */ public function getQueueableConnection(); } src/Illuminate/Contracts/Queue/Monitor.php 0000644 00000001240 15107327036 0014623 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface Monitor { /** * Register a callback to be executed on every iteration through the queue loop. * * @param mixed $callback * @return void */ public function looping($callback); /** * Register a callback to be executed when a job fails after the maximum number of retries. * * @param mixed $callback * @return void */ public function failing($callback); /** * Register a callback to be executed when a daemon queue is stopping. * * @param mixed $callback * @return void */ public function stopping($callback); } src/Illuminate/Contracts/Queue/EntityNotFoundException.php 0000644 00000000704 15107327036 0020010 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; use InvalidArgumentException; class EntityNotFoundException extends InvalidArgumentException { /** * Create a new exception instance. * * @param string $type * @param mixed $id * @return void */ public function __construct($type, $id) { $id = (string) $id; parent::__construct("Queueable entity [{$type}] not found for ID [{$id}]."); } } src/Illuminate/Contracts/Container/Container.php 0000644 00000012664 15107327036 0015770 0 ustar 00 <?php namespace Illuminate\Contracts\Container; use Closure; use Psr\Container\ContainerInterface; interface Container extends ContainerInterface { /** * Determine if the given abstract type has been bound. * * @param string $abstract * @return bool */ public function bound($abstract); /** * Alias a type to a different name. * * @param string $abstract * @param string $alias * @return void * * @throws \LogicException */ public function alias($abstract, $alias); /** * Assign a set of tags to a given binding. * * @param array|string $abstracts * @param array|mixed ...$tags * @return void */ public function tag($abstracts, $tags); /** * Resolve all of the bindings for a given tag. * * @param string $tag * @return iterable */ public function tagged($tag); /** * Register a binding with the container. * * @param string $abstract * @param \Closure|string|null $concrete * @param bool $shared * @return void */ public function bind($abstract, $concrete = null, $shared = false); /** * Bind a callback to resolve with Container::call. * * @param array|string $method * @param \Closure $callback * @return void */ public function bindMethod($method, $callback); /** * Register a binding if it hasn't already been registered. * * @param string $abstract * @param \Closure|string|null $concrete * @param bool $shared * @return void */ public function bindIf($abstract, $concrete = null, $shared = false); /** * Register a shared binding in the container. * * @param string $abstract * @param \Closure|string|null $concrete * @return void */ public function singleton($abstract, $concrete = null); /** * Register a shared binding if it hasn't already been registered. * * @param string $abstract * @param \Closure|string|null $concrete * @return void */ public function singletonIf($abstract, $concrete = null); /** * Register a scoped binding in the container. * * @param string $abstract * @param \Closure|string|null $concrete * @return void */ public function scoped($abstract, $concrete = null); /** * Register a scoped binding if it hasn't already been registered. * * @param string $abstract * @param \Closure|string|null $concrete * @return void */ public function scopedIf($abstract, $concrete = null); /** * "Extend" an abstract type in the container. * * @param string $abstract * @param \Closure $closure * @return void * * @throws \InvalidArgumentException */ public function extend($abstract, Closure $closure); /** * Register an existing instance as shared in the container. * * @param string $abstract * @param mixed $instance * @return mixed */ public function instance($abstract, $instance); /** * Add a contextual binding to the container. * * @param string $concrete * @param string $abstract * @param \Closure|string $implementation * @return void */ public function addContextualBinding($concrete, $abstract, $implementation); /** * Define a contextual binding. * * @param string|array $concrete * @return \Illuminate\Contracts\Container\ContextualBindingBuilder */ public function when($concrete); /** * Get a closure to resolve the given type from the container. * * @param string $abstract * @return \Closure */ public function factory($abstract); /** * Flush the container of all bindings and resolved instances. * * @return void */ public function flush(); /** * Resolve the given type from the container. * * @param string $abstract * @param array $parameters * @return mixed * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function make($abstract, array $parameters = []); /** * Call the given Closure / class@method and inject its dependencies. * * @param callable|string $callback * @param array $parameters * @param string|null $defaultMethod * @return mixed */ public function call($callback, array $parameters = [], $defaultMethod = null); /** * Determine if the given abstract type has been resolved. * * @param string $abstract * @return bool */ public function resolved($abstract); /** * Register a new before resolving callback. * * @param \Closure|string $abstract * @param \Closure|null $callback * @return void */ public function beforeResolving($abstract, Closure $callback = null); /** * Register a new resolving callback. * * @param \Closure|string $abstract * @param \Closure|null $callback * @return void */ public function resolving($abstract, Closure $callback = null); /** * Register a new after resolving callback. * * @param \Closure|string $abstract * @param \Closure|null $callback * @return void */ public function afterResolving($abstract, Closure $callback = null); } src/Illuminate/Contracts/Container/ContextualBindingBuilder.php 0000644 00000001572 15107327036 0020772 0 ustar 00 <?php namespace Illuminate\Contracts\Container; interface ContextualBindingBuilder { /** * Define the abstract target that depends on the context. * * @param string $abstract * @return $this */ public function needs($abstract); /** * Define the implementation for the contextual binding. * * @param \Closure|string|array $implementation * @return void */ public function give($implementation); /** * Define tagged services to be used as the implementation for the contextual binding. * * @param string $tag * @return void */ public function giveTagged($tag); /** * Specify the configuration item to bind as a primitive. * * @param string $key * @param mixed $default * @return void */ public function giveConfig($key, $default = null); } src/Illuminate/Contracts/Container/CircularDependencyException.php 0000644 00000000327 15107327036 0021461 0 ustar 00 <?php namespace Illuminate\Contracts\Container; use Exception; use Psr\Container\ContainerExceptionInterface; class CircularDependencyException extends Exception implements ContainerExceptionInterface { // } src/Illuminate/Contracts/Container/BindingResolutionException.php 0000644 00000000326 15107327036 0021353 0 ustar 00 <?php namespace Illuminate\Contracts\Container; use Exception; use Psr\Container\ContainerExceptionInterface; class BindingResolutionException extends Exception implements ContainerExceptionInterface { // } src/Illuminate/Contracts/Foundation/CachesConfiguration.php 0000644 00000001010 15107327036 0020127 0 ustar 00 <?php namespace Illuminate\Contracts\Foundation; interface CachesConfiguration { /** * Determine if the application configuration is cached. * * @return bool */ public function configurationIsCached(); /** * Get the path to the configuration cache file. * * @return string */ public function getCachedConfigPath(); /** * Get the path to the cached services.php file. * * @return string */ public function getCachedServicesPath(); } src/Illuminate/Contracts/Foundation/ExceptionRenderer.php 0000644 00000000372 15107327036 0017650 0 ustar 00 <?php namespace Illuminate\Contracts\Foundation; interface ExceptionRenderer { /** * Renders the given exception as HTML. * * @param \Throwable $throwable * @return string */ public function render($throwable); } src/Illuminate/Contracts/Foundation/MaintenanceMode.php 0000644 00000001313 15107327036 0017246 0 ustar 00 <?php namespace Illuminate\Contracts\Foundation; interface MaintenanceMode { /** * Take the application down for maintenance. * * @param array $payload * @return void */ public function activate(array $payload): void; /** * Take the application out of maintenance. * * @return void */ public function deactivate(): void; /** * Determine if the application is currently down for maintenance. * * @return bool */ public function active(): bool; /** * Get the data array which was provided when the application was placed into maintenance. * * @return array */ public function data(): array; } src/Illuminate/Contracts/Foundation/Application.php 0000644 00000012746 15107327036 0016476 0 ustar 00 <?php namespace Illuminate\Contracts\Foundation; use Illuminate\Contracts\Container\Container; interface Application extends Container { /** * Get the version number of the application. * * @return string */ public function version(); /** * Get the base path of the Laravel installation. * * @param string $path * @return string */ public function basePath($path = ''); /** * Get the path to the bootstrap directory. * * @param string $path * @return string */ public function bootstrapPath($path = ''); /** * Get the path to the application configuration files. * * @param string $path * @return string */ public function configPath($path = ''); /** * Get the path to the database directory. * * @param string $path * @return string */ public function databasePath($path = ''); /** * Get the path to the language files. * * @param string $path * @return string */ public function langPath($path = ''); /** * Get the path to the public directory. * * @param string $path * @return string */ public function publicPath($path = ''); /** * Get the path to the resources directory. * * @param string $path * @return string */ public function resourcePath($path = ''); /** * Get the path to the storage directory. * * @param string $path * @return string */ public function storagePath($path = ''); /** * Get or check the current application environment. * * @param string|array ...$environments * @return string|bool */ public function environment(...$environments); /** * Determine if the application is running in the console. * * @return bool */ public function runningInConsole(); /** * Determine if the application is running unit tests. * * @return bool */ public function runningUnitTests(); /** * Determine if the application is running with debug mode enabled. * * @return bool */ public function hasDebugModeEnabled(); /** * Get an instance of the maintenance mode manager implementation. * * @return \Illuminate\Contracts\Foundation\MaintenanceMode */ public function maintenanceMode(); /** * Determine if the application is currently down for maintenance. * * @return bool */ public function isDownForMaintenance(); /** * Register all of the configured providers. * * @return void */ public function registerConfiguredProviders(); /** * Register a service provider with the application. * * @param \Illuminate\Support\ServiceProvider|string $provider * @param bool $force * @return \Illuminate\Support\ServiceProvider */ public function register($provider, $force = false); /** * Register a deferred provider and service. * * @param string $provider * @param string|null $service * @return void */ public function registerDeferredProvider($provider, $service = null); /** * Resolve a service provider instance from the class name. * * @param string $provider * @return \Illuminate\Support\ServiceProvider */ public function resolveProvider($provider); /** * Boot the application's service providers. * * @return void */ public function boot(); /** * Register a new boot listener. * * @param callable $callback * @return void */ public function booting($callback); /** * Register a new "booted" listener. * * @param callable $callback * @return void */ public function booted($callback); /** * Run the given array of bootstrap classes. * * @param array $bootstrappers * @return void */ public function bootstrapWith(array $bootstrappers); /** * Get the current application locale. * * @return string */ public function getLocale(); /** * Get the application namespace. * * @return string * * @throws \RuntimeException */ public function getNamespace(); /** * Get the registered service provider instances if any exist. * * @param \Illuminate\Support\ServiceProvider|string $provider * @return array */ public function getProviders($provider); /** * Determine if the application has been bootstrapped before. * * @return bool */ public function hasBeenBootstrapped(); /** * Load and boot all of the remaining deferred providers. * * @return void */ public function loadDeferredProviders(); /** * Set the current application locale. * * @param string $locale * @return void */ public function setLocale($locale); /** * Determine if middleware has been disabled for the application. * * @return bool */ public function shouldSkipMiddleware(); /** * Register a terminating callback with the application. * * @param callable|string $callback * @return \Illuminate\Contracts\Foundation\Application */ public function terminating($callback); /** * Terminate the application. * * @return void */ public function terminate(); } src/Illuminate/Contracts/Foundation/CachesRoutes.php 0000644 00000000536 15107327036 0016615 0 ustar 00 <?php namespace Illuminate\Contracts\Foundation; interface CachesRoutes { /** * Determine if the application routes are cached. * * @return bool */ public function routesAreCached(); /** * Get the path to the routes cache file. * * @return string */ public function getCachedRoutesPath(); } src/Illuminate/Contracts/Console/PromptsForMissingInput.php 0000644 00000000134 15107327036 0020200 0 ustar 00 <?php namespace Illuminate\Contracts\Console; interface PromptsForMissingInput { // } src/Illuminate/Contracts/Console/Application.php 0000644 00000001005 15107327036 0015754 0 ustar 00 <?php namespace Illuminate\Contracts\Console; interface Application { /** * Run an Artisan console command by name. * * @param string $command * @param array $parameters * @param \Symfony\Component\Console\Output\OutputInterface|null $outputBuffer * @return int */ public function call($command, array $parameters = [], $outputBuffer = null); /** * Get the output from the last command. * * @return string */ public function output(); } src/Illuminate/Contracts/Console/Kernel.php 0000644 00000003016 15107327036 0014735 0 ustar 00 <?php namespace Illuminate\Contracts\Console; interface Kernel { /** * Bootstrap the application for artisan commands. * * @return void */ public function bootstrap(); /** * Handle an incoming console command. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface|null $output * @return int */ public function handle($input, $output = null); /** * Run an Artisan console command by name. * * @param string $command * @param array $parameters * @param \Symfony\Component\Console\Output\OutputInterface|null $outputBuffer * @return int */ public function call($command, array $parameters = [], $outputBuffer = null); /** * Queue an Artisan console command by name. * * @param string $command * @param array $parameters * @return \Illuminate\Foundation\Bus\PendingDispatch */ public function queue($command, array $parameters = []); /** * Get all of the commands registered with the console. * * @return array */ public function all(); /** * Get the output for the last run command. * * @return string */ public function output(); /** * Terminate the application. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param int $status * @return void */ public function terminate($input, $status); } src/Illuminate/Contracts/Console/Isolatable.php 0000644 00000000120 15107327036 0015565 0 ustar 00 <?php namespace Illuminate\Contracts\Console; interface Isolatable { // } src/Illuminate/Contracts/Cache/LockProvider.php 0000644 00000001065 15107327036 0015523 0 ustar 00 <?php namespace Illuminate\Contracts\Cache; interface LockProvider { /** * Get a lock instance. * * @param string $name * @param int $seconds * @param string|null $owner * @return \Illuminate\Contracts\Cache\Lock */ public function lock($name, $seconds = 0, $owner = null); /** * Restore a lock instance using the owner identifier. * * @param string $name * @param string $owner * @return \Illuminate\Contracts\Cache\Lock */ public function restoreLock($name, $owner); } src/Illuminate/Contracts/Cache/Store.php 0000644 00000003555 15107327036 0014222 0 ustar 00 <?php namespace Illuminate\Contracts\Cache; interface Store { /** * Retrieve an item from the cache by key. * * @param string|array $key * @return mixed */ public function get($key); /** * Retrieve multiple items from the cache by key. * * Items not found in the cache will have a null value. * * @param array $keys * @return array */ public function many(array $keys); /** * Store an item in the cache for a given number of seconds. * * @param string $key * @param mixed $value * @param int $seconds * @return bool */ public function put($key, $value, $seconds); /** * Store multiple items in the cache for a given number of seconds. * * @param array $values * @param int $seconds * @return bool */ public function putMany(array $values, $seconds); /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function increment($key, $value = 1); /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function decrement($key, $value = 1); /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return bool */ public function forever($key, $value); /** * Remove an item from the cache. * * @param string $key * @return bool */ public function forget($key); /** * Remove all items from the cache. * * @return bool */ public function flush(); /** * Get the cache key prefix. * * @return string */ public function getPrefix(); } src/Illuminate/Contracts/Cache/LockTimeoutException.php 0000644 00000000166 15107327036 0017237 0 ustar 00 <?php namespace Illuminate\Contracts\Cache; use Exception; class LockTimeoutException extends Exception { // } src/Illuminate/Contracts/Cache/Factory.php 0000644 00000000407 15107327036 0014526 0 ustar 00 <?php namespace Illuminate\Contracts\Cache; interface Factory { /** * Get a cache store instance by name. * * @param string|null $name * @return \Illuminate\Contracts\Cache\Repository */ public function store($name = null); } src/Illuminate/Contracts/Cache/Repository.php 0000644 00000005525 15107327036 0015304 0 ustar 00 <?php namespace Illuminate\Contracts\Cache; use Closure; use Psr\SimpleCache\CacheInterface; interface Repository extends CacheInterface { /** * Retrieve an item from the cache and delete it. * * @template TCacheValue * * @param array|string $key * @param TCacheValue|(\Closure(): TCacheValue) $default * @return (TCacheValue is null ? mixed : TCacheValue) */ public function pull($key, $default = null); /** * Store an item in the cache. * * @param string $key * @param mixed $value * @param \DateTimeInterface|\DateInterval|int|null $ttl * @return bool */ public function put($key, $value, $ttl = null); /** * Store an item in the cache if the key does not exist. * * @param string $key * @param mixed $value * @param \DateTimeInterface|\DateInterval|int|null $ttl * @return bool */ public function add($key, $value, $ttl = null); /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function increment($key, $value = 1); /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function decrement($key, $value = 1); /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return bool */ public function forever($key, $value); /** * Get an item from the cache, or execute the given Closure and store the result. * * @template TCacheValue * * @param string $key * @param \DateTimeInterface|\DateInterval|\Closure|int|null $ttl * @param \Closure(): TCacheValue $callback * @return TCacheValue */ public function remember($key, $ttl, Closure $callback); /** * Get an item from the cache, or execute the given Closure and store the result forever. * * @template TCacheValue * * @param string $key * @param \Closure(): TCacheValue $callback * @return TCacheValue */ public function sear($key, Closure $callback); /** * Get an item from the cache, or execute the given Closure and store the result forever. * * @template TCacheValue * * @param string $key * @param \Closure(): TCacheValue $callback * @return TCacheValue */ public function rememberForever($key, Closure $callback); /** * Remove an item from the cache. * * @param string $key * @return bool */ public function forget($key); /** * Get the cache store implementation. * * @return \Illuminate\Contracts\Cache\Store */ public function getStore(); } src/Illuminate/Contracts/Cache/Lock.php 0000644 00000001463 15107327036 0014012 0 ustar 00 <?php namespace Illuminate\Contracts\Cache; interface Lock { /** * Attempt to acquire the lock. * * @param callable|null $callback * @return mixed */ public function get($callback = null); /** * Attempt to acquire the lock for the given number of seconds. * * @param int $seconds * @param callable|null $callback * @return mixed */ public function block($seconds, $callback = null); /** * Release the lock. * * @return bool */ public function release(); /** * Returns the current owner of the lock. * * @return string */ public function owner(); /** * Releases this lock in disregard of ownership. * * @return void */ public function forceRelease(); } src/Illuminate/Contracts/Hashing/Hasher.php 0000644 00000001662 15107327036 0014713 0 ustar 00 <?php namespace Illuminate\Contracts\Hashing; interface Hasher { /** * Get information about the given hashed value. * * @param string $hashedValue * @return array */ public function info($hashedValue); /** * Hash the given value. * * @param string $value * @param array $options * @return string */ public function make($value, array $options = []); /** * Check the given plain value against a hash. * * @param string $value * @param string $hashedValue * @param array $options * @return bool */ public function check($value, $hashedValue, array $options = []); /** * Check if the given hash has been hashed using the given options. * * @param string $hashedValue * @param array $options * @return bool */ public function needsRehash($hashedValue, array $options = []); } src/Illuminate/Contracts/Routing/Registrar.php 0000644 00000005203 15107327036 0015504 0 ustar 00 <?php namespace Illuminate\Contracts\Routing; interface Registrar { /** * Register a new GET route with the router. * * @param string $uri * @param array|string|callable $action * @return \Illuminate\Routing\Route */ public function get($uri, $action); /** * Register a new POST route with the router. * * @param string $uri * @param array|string|callable $action * @return \Illuminate\Routing\Route */ public function post($uri, $action); /** * Register a new PUT route with the router. * * @param string $uri * @param array|string|callable $action * @return \Illuminate\Routing\Route */ public function put($uri, $action); /** * Register a new DELETE route with the router. * * @param string $uri * @param array|string|callable $action * @return \Illuminate\Routing\Route */ public function delete($uri, $action); /** * Register a new PATCH route with the router. * * @param string $uri * @param array|string|callable $action * @return \Illuminate\Routing\Route */ public function patch($uri, $action); /** * Register a new OPTIONS route with the router. * * @param string $uri * @param array|string|callable $action * @return \Illuminate\Routing\Route */ public function options($uri, $action); /** * Register a new route with the given verbs. * * @param array|string $methods * @param string $uri * @param array|string|callable $action * @return \Illuminate\Routing\Route */ public function match($methods, $uri, $action); /** * Route a resource to a controller. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\PendingResourceRegistration */ public function resource($name, $controller, array $options = []); /** * Create a route group with shared attributes. * * @param array $attributes * @param \Closure|string $routes * @return void */ public function group(array $attributes, $routes); /** * Substitute the route bindings onto the route. * * @param \Illuminate\Routing\Route $route * @return \Illuminate\Routing\Route */ public function substituteBindings($route); /** * Substitute the implicit Eloquent model bindings for the route. * * @param \Illuminate\Routing\Route $route * @return void */ public function substituteImplicitBindings($route); } src/Illuminate/Contracts/Routing/BindingRegistrar.php 0000644 00000000677 15107327036 0017011 0 ustar 00 <?php namespace Illuminate\Contracts\Routing; interface BindingRegistrar { /** * Add a new route parameter binder. * * @param string $key * @param string|callable $binder * @return void */ public function bind($key, $binder); /** * Get the binding callback for a given binding. * * @param string $key * @return \Closure */ public function getBindingCallback($key); } src/Illuminate/Contracts/Routing/UrlRoutable.php 0000644 00000001561 15107327036 0016005 0 ustar 00 <?php namespace Illuminate\Contracts\Routing; interface UrlRoutable { /** * Get the value of the model's route key. * * @return mixed */ public function getRouteKey(); /** * Get the route key for the model. * * @return string */ public function getRouteKeyName(); /** * Retrieve the model for a bound value. * * @param mixed $value * @param string|null $field * @return \Illuminate\Database\Eloquent\Model|null */ public function resolveRouteBinding($value, $field = null); /** * Retrieve the child model for a bound value. * * @param string $childType * @param mixed $value * @param string|null $field * @return \Illuminate\Database\Eloquent\Model|null */ public function resolveChildRouteBinding($childType, $value, $field); } src/Illuminate/Contracts/Routing/ResponseFactory.php 0000644 00000011202 15107327036 0016664 0 ustar 00 <?php namespace Illuminate\Contracts\Routing; interface ResponseFactory { /** * Create a new response instance. * * @param array|string $content * @param int $status * @param array $headers * @return \Illuminate\Http\Response */ public function make($content = '', $status = 200, array $headers = []); /** * Create a new "no content" response. * * @param int $status * @param array $headers * @return \Illuminate\Http\Response */ public function noContent($status = 204, array $headers = []); /** * Create a new response for a given view. * * @param string|array $view * @param array $data * @param int $status * @param array $headers * @return \Illuminate\Http\Response */ public function view($view, $data = [], $status = 200, array $headers = []); /** * Create a new JSON response instance. * * @param mixed $data * @param int $status * @param array $headers * @param int $options * @return \Illuminate\Http\JsonResponse */ public function json($data = [], $status = 200, array $headers = [], $options = 0); /** * Create a new JSONP response instance. * * @param string $callback * @param mixed $data * @param int $status * @param array $headers * @param int $options * @return \Illuminate\Http\JsonResponse */ public function jsonp($callback, $data = [], $status = 200, array $headers = [], $options = 0); /** * Create a new streamed response instance. * * @param callable $callback * @param int $status * @param array $headers * @return \Symfony\Component\HttpFoundation\StreamedResponse */ public function stream($callback, $status = 200, array $headers = []); /** * Create a new streamed response instance as a file download. * * @param callable $callback * @param string|null $name * @param array $headers * @param string|null $disposition * @return \Symfony\Component\HttpFoundation\StreamedResponse */ public function streamDownload($callback, $name = null, array $headers = [], $disposition = 'attachment'); /** * Create a new file download response. * * @param \SplFileInfo|string $file * @param string|null $name * @param array $headers * @param string|null $disposition * @return \Symfony\Component\HttpFoundation\BinaryFileResponse */ public function download($file, $name = null, array $headers = [], $disposition = 'attachment'); /** * Return the raw contents of a binary file. * * @param \SplFileInfo|string $file * @param array $headers * @return \Symfony\Component\HttpFoundation\BinaryFileResponse */ public function file($file, array $headers = []); /** * Create a new redirect response to the given path. * * @param string $path * @param int $status * @param array $headers * @param bool|null $secure * @return \Illuminate\Http\RedirectResponse */ public function redirectTo($path, $status = 302, $headers = [], $secure = null); /** * Create a new redirect response to a named route. * * @param string $route * @param mixed $parameters * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function redirectToRoute($route, $parameters = [], $status = 302, $headers = []); /** * Create a new redirect response to a controller action. * * @param array|string $action * @param mixed $parameters * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function redirectToAction($action, $parameters = [], $status = 302, $headers = []); /** * Create a new redirect response, while putting the current URL in the session. * * @param string $path * @param int $status * @param array $headers * @param bool|null $secure * @return \Illuminate\Http\RedirectResponse */ public function redirectGuest($path, $status = 302, $headers = [], $secure = null); /** * Create a new redirect response to the previously intended location. * * @param string $default * @param int $status * @param array $headers * @param bool|null $secure * @return \Illuminate\Http\RedirectResponse */ public function redirectToIntended($default = '/', $status = 302, $headers = [], $secure = null); } src/Illuminate/Contracts/Routing/UrlGenerator.php 0000644 00000003612 15107327036 0016155 0 ustar 00 <?php namespace Illuminate\Contracts\Routing; interface UrlGenerator { /** * Get the current URL for the request. * * @return string */ public function current(); /** * Get the URL for the previous request. * * @param mixed $fallback * @return string */ public function previous($fallback = false); /** * Generate an absolute URL to the given path. * * @param string $path * @param mixed $extra * @param bool|null $secure * @return string */ public function to($path, $extra = [], $secure = null); /** * Generate a secure, absolute URL to the given path. * * @param string $path * @param array $parameters * @return string */ public function secure($path, $parameters = []); /** * Generate the URL to an application asset. * * @param string $path * @param bool|null $secure * @return string */ public function asset($path, $secure = null); /** * Get the URL to a named route. * * @param string $name * @param mixed $parameters * @param bool $absolute * @return string * * @throws \InvalidArgumentException */ public function route($name, $parameters = [], $absolute = true); /** * Get the URL to a controller action. * * @param string|array $action * @param mixed $parameters * @param bool $absolute * @return string */ public function action($action, $parameters = [], $absolute = true); /** * Get the root controller namespace. * * @return string */ public function getRootControllerNamespace(); /** * Set the root controller namespace. * * @param string $rootNamespace * @return $this */ public function setRootControllerNamespace($rootNamespace); } src/Illuminate/Contracts/Translation/HasLocalePreference.php 0000644 00000000336 15107327036 0020245 0 ustar 00 <?php namespace Illuminate\Contracts\Translation; interface HasLocalePreference { /** * Get the preferred locale of the entity. * * @return string|null */ public function preferredLocale(); } src/Illuminate/Contracts/Translation/Loader.php 0000644 00000001460 15107327036 0015620 0 ustar 00 <?php namespace Illuminate\Contracts\Translation; interface Loader { /** * Load the messages for the given locale. * * @param string $locale * @param string $group * @param string|null $namespace * @return array */ public function load($locale, $group, $namespace = null); /** * Add a new namespace to the loader. * * @param string $namespace * @param string $hint * @return void */ public function addNamespace($namespace, $hint); /** * Add a new JSON path to the loader. * * @param string $path * @return void */ public function addJsonPath($path); /** * Get an array of all the registered namespaces. * * @return array */ public function namespaces(); } src/Illuminate/Contracts/Translation/Translator.php 0000644 00000001625 15107327036 0016546 0 ustar 00 <?php namespace Illuminate\Contracts\Translation; interface Translator { /** * Get the translation for a given key. * * @param string $key * @param array $replace * @param string|null $locale * @return mixed */ public function get($key, array $replace = [], $locale = null); /** * Get a translation according to an integer value. * * @param string $key * @param \Countable|int|array $number * @param array $replace * @param string|null $locale * @return string */ public function choice($key, $number, array $replace = [], $locale = null); /** * Get the default locale being used. * * @return string */ public function getLocale(); /** * Set the default locale. * * @param string $locale * @return void */ public function setLocale($locale); } src/Illuminate/Contracts/Debug/ExceptionHandler.php 0000644 00000002113 15107327036 0016372 0 ustar 00 <?php namespace Illuminate\Contracts\Debug; use Throwable; interface ExceptionHandler { /** * Report or log an exception. * * @param \Throwable $e * @return void * * @throws \Throwable */ public function report(Throwable $e); /** * Determine if the exception should be reported. * * @param \Throwable $e * @return bool */ public function shouldReport(Throwable $e); /** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Throwable $e * @return \Symfony\Component\HttpFoundation\Response * * @throws \Throwable */ public function render($request, Throwable $e); /** * Render an exception to the console. * * @param \Symfony\Component\Console\Output\OutputInterface $output * @param \Throwable $e * @return void * * @internal This method is not meant to be used or overwritten outside the framework. */ public function renderForConsole($output, Throwable $e); } src/Illuminate/Contracts/Database/ModelIdentifier.php 0000644 00000002656 15107327036 0016673 0 ustar 00 <?php namespace Illuminate\Contracts\Database; class ModelIdentifier { /** * The class name of the model. * * @var string */ public $class; /** * The unique identifier of the model. * * This may be either a single ID or an array of IDs. * * @var mixed */ public $id; /** * The relationships loaded on the model. * * @var array */ public $relations; /** * The connection name of the model. * * @var string|null */ public $connection; /** * The class name of the model collection. * * @var string|null */ public $collectionClass; /** * Create a new model identifier. * * @param string $class * @param mixed $id * @param array $relations * @param mixed $connection * @return void */ public function __construct($class, $id, array $relations, $connection) { $this->id = $id; $this->class = $class; $this->relations = $relations; $this->connection = $connection; } /** * Specify the collection class that should be used when serializing / restoring collections. * * @param string|null $collectionClass * @return $this */ public function useCollectionClass(?string $collectionClass) { $this->collectionClass = $collectionClass; return $this; } } src/Illuminate/Contracts/Database/Events/MigrationEvent.php 0000644 00000000134 15107327036 0020014 0 ustar 00 <?php namespace Illuminate\Contracts\Database\Events; interface MigrationEvent { // } src/Illuminate/Contracts/Database/Eloquent/CastsAttributes.php 0000644 00000001602 15107327036 0020536 0 ustar 00 <?php namespace Illuminate\Contracts\Database\Eloquent; use Illuminate\Database\Eloquent\Model; /** * @template TGet * @template TSet */ interface CastsAttributes { /** * Transform the attribute from the underlying model values. * * @param \Illuminate\Database\Eloquent\Model $model * @param string $key * @param mixed $value * @param array<string, mixed> $attributes * @return TGet|null */ public function get(Model $model, string $key, mixed $value, array $attributes); /** * Transform the attribute to its underlying model values. * * @param \Illuminate\Database\Eloquent\Model $model * @param string $key * @param TSet|null $value * @param array<string, mixed> $attributes * @return mixed */ public function set(Model $model, string $key, mixed $value, array $attributes); } src/Illuminate/Contracts/Database/Eloquent/Castable.php 0000644 00000000604 15107327036 0017131 0 ustar 00 <?php namespace Illuminate\Contracts\Database\Eloquent; interface Castable { /** * Get the name of the caster class to use when casting from / to this cast target. * * @param array $arguments * @return class-string<CastsAttributes|CastsInboundAttributes>|CastsAttributes|CastsInboundAttributes */ public static function castUsing(array $arguments); } src/Illuminate/Contracts/Database/Eloquent/SerializesCastableAttributes.php 0000644 00000000761 15107327036 0023237 0 ustar 00 <?php namespace Illuminate\Contracts\Database\Eloquent; use Illuminate\Database\Eloquent\Model; interface SerializesCastableAttributes { /** * Serialize the attribute when converting the model to an array. * * @param \Illuminate\Database\Eloquent\Model $model * @param string $key * @param mixed $value * @param array $attributes * @return mixed */ public function serialize(Model $model, string $key, mixed $value, array $attributes); } src/Illuminate/Contracts/Database/Eloquent/Builder.php 0000644 00000000456 15107327036 0017006 0 ustar 00 <?php namespace Illuminate\Contracts\Database\Eloquent; use Illuminate\Contracts\Database\Query\Builder as BaseContract; /** * This interface is intentionally empty and exists to improve IDE support. * * @mixin \Illuminate\Database\Eloquent\Builder */ interface Builder extends BaseContract { } src/Illuminate/Contracts/Database/Eloquent/DeviatesCastableAttributes.php 0000644 00000001306 15107327036 0022665 0 ustar 00 <?php namespace Illuminate\Contracts\Database\Eloquent; interface DeviatesCastableAttributes { /** * Increment the attribute. * * @param \Illuminate\Database\Eloquent\Model $model * @param string $key * @param mixed $value * @param array $attributes * @return mixed */ public function increment($model, string $key, $value, array $attributes); /** * Decrement the attribute. * * @param \Illuminate\Database\Eloquent\Model $model * @param string $key * @param mixed $value * @param array $attributes * @return mixed */ public function decrement($model, string $key, $value, array $attributes); } src/Illuminate/Contracts/Database/Eloquent/SupportsPartialRelations.php 0000644 00000001415 15107327036 0022451 0 ustar 00 <?php namespace Illuminate\Contracts\Database\Eloquent; interface SupportsPartialRelations { /** * Indicate that the relation is a single result of a larger one-to-many relationship. * * @param string|null $column * @param string|\Closure|null $aggregate * @param string $relation * @return $this */ public function ofMany($column = 'id', $aggregate = 'MAX', $relation = null); /** * Determine whether the relationship is a one-of-many relationship. * * @return bool */ public function isOneOfMany(); /** * Get the one of many inner join subselect query builder instance. * * @return \Illuminate\Database\Eloquent\Builder|void */ public function getOneOfManySubQuery(); } src/Illuminate/Contracts/Database/Eloquent/CastsInboundAttributes.php 0000644 00000000736 15107327036 0022064 0 ustar 00 <?php namespace Illuminate\Contracts\Database\Eloquent; use Illuminate\Database\Eloquent\Model; interface CastsInboundAttributes { /** * Transform the attribute to its underlying model values. * * @param \Illuminate\Database\Eloquent\Model $model * @param string $key * @param mixed $value * @param array $attributes * @return mixed */ public function set(Model $model, string $key, mixed $value, array $attributes); } src/Illuminate/Contracts/Database/Query/ConditionExpression.php 0000644 00000000154 15107327036 0020732 0 ustar 00 <?php namespace Illuminate\Contracts\Database\Query; interface ConditionExpression extends Expression { } src/Illuminate/Contracts/Database/Query/Builder.php 0000644 00000000321 15107327036 0016306 0 ustar 00 <?php namespace Illuminate\Contracts\Database\Query; /** * This interface is intentionally empty and exists to improve IDE support. * * @mixin \Illuminate\Database\Query\Builder */ interface Builder { } src/Illuminate/Contracts/Database/Query/Expression.php 0000644 00000000467 15107327036 0017072 0 ustar 00 <?php namespace Illuminate\Contracts\Database\Query; use Illuminate\Database\Grammar; interface Expression { /** * Get the value of the expression. * * @param \Illuminate\Database\Grammar $grammar * @return string|int|float */ public function getValue(Grammar $grammar); } src/Illuminate/Contracts/Broadcasting/Broadcaster.php 0000644 00000001473 15107327036 0016751 0 ustar 00 <?php namespace Illuminate\Contracts\Broadcasting; interface Broadcaster { /** * Authenticate the incoming request for a given channel. * * @param \Illuminate\Http\Request $request * @return mixed */ public function auth($request); /** * Return the valid authentication response. * * @param \Illuminate\Http\Request $request * @param mixed $result * @return mixed */ public function validAuthenticationResponse($request, $result); /** * Broadcast the given event. * * @param array $channels * @param string $event * @param array $payload * @return void * * @throws \Illuminate\Broadcasting\BroadcastException */ public function broadcast(array $channels, $event, array $payload = []); } src/Illuminate/Contracts/Broadcasting/ShouldBroadcast.php 0000644 00000000447 15107327036 0017601 0 ustar 00 <?php namespace Illuminate\Contracts\Broadcasting; interface ShouldBroadcast { /** * Get the channels the event should broadcast on. * * @return \Illuminate\Broadcasting\Channel|\Illuminate\Broadcasting\Channel[]|string[]|string */ public function broadcastOn(); } src/Illuminate/Contracts/Broadcasting/HasBroadcastChannel.php 0000644 00000000663 15107327036 0020347 0 ustar 00 <?php namespace Illuminate\Contracts\Broadcasting; interface HasBroadcastChannel { /** * Get the broadcast channel route definition that is associated with the given entity. * * @return string */ public function broadcastChannelRoute(); /** * Get the broadcast channel name that is associated with the given entity. * * @return string */ public function broadcastChannel(); } src/Illuminate/Contracts/Broadcasting/ShouldBroadcastNow.php 0000644 00000000165 15107327036 0020262 0 ustar 00 <?php namespace Illuminate\Contracts\Broadcasting; interface ShouldBroadcastNow extends ShouldBroadcast { // } src/Illuminate/Contracts/Broadcasting/ShouldBeUnique.php 0000644 00000000131 15107327036 0017402 0 ustar 00 <?php namespace Illuminate\Contracts\Broadcasting; interface ShouldBeUnique { // } src/Illuminate/Contracts/Broadcasting/Factory.php 0000644 00000000441 15107327036 0016121 0 ustar 00 <?php namespace Illuminate\Contracts\Broadcasting; interface Factory { /** * Get a broadcaster implementation by name. * * @param string|null $name * @return \Illuminate\Contracts\Broadcasting\Broadcaster */ public function connection($name = null); } src/Illuminate/Contracts/Filesystem/LockTimeoutException.php 0000644 00000000173 15107327036 0020356 0 ustar 00 <?php namespace Illuminate\Contracts\Filesystem; use Exception; class LockTimeoutException extends Exception { // } src/Illuminate/Contracts/Filesystem/Filesystem.php 0000644 00000010016 15107327036 0016361 0 ustar 00 <?php namespace Illuminate\Contracts\Filesystem; interface Filesystem { /** * The public visibility setting. * * @var string */ const VISIBILITY_PUBLIC = 'public'; /** * The private visibility setting. * * @var string */ const VISIBILITY_PRIVATE = 'private'; /** * Determine if a file exists. * * @param string $path * @return bool */ public function exists($path); /** * Get the contents of a file. * * @param string $path * @return string|null */ public function get($path); /** * Get a resource to read the file. * * @param string $path * @return resource|null The path resource or null on failure. */ public function readStream($path); /** * Write the contents of a file. * * @param string $path * @param \Psr\Http\Message\StreamInterface|\Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|resource $contents * @param mixed $options * @return bool */ public function put($path, $contents, $options = []); /** * Write a new file using a stream. * * @param string $path * @param resource $resource * @param array $options * @return bool */ public function writeStream($path, $resource, array $options = []); /** * Get the visibility for the given path. * * @param string $path * @return string */ public function getVisibility($path); /** * Set the visibility for the given path. * * @param string $path * @param string $visibility * @return bool */ public function setVisibility($path, $visibility); /** * Prepend to a file. * * @param string $path * @param string $data * @return bool */ public function prepend($path, $data); /** * Append to a file. * * @param string $path * @param string $data * @return bool */ public function append($path, $data); /** * Delete the file at a given path. * * @param string|array $paths * @return bool */ public function delete($paths); /** * Copy a file to a new location. * * @param string $from * @param string $to * @return bool */ public function copy($from, $to); /** * Move a file to a new location. * * @param string $from * @param string $to * @return bool */ public function move($from, $to); /** * Get the file size of a given file. * * @param string $path * @return int */ public function size($path); /** * Get the file's last modification time. * * @param string $path * @return int */ public function lastModified($path); /** * Get an array of all files in a directory. * * @param string|null $directory * @param bool $recursive * @return array */ public function files($directory = null, $recursive = false); /** * Get all of the files from the given directory (recursive). * * @param string|null $directory * @return array */ public function allFiles($directory = null); /** * Get all of the directories within a given directory. * * @param string|null $directory * @param bool $recursive * @return array */ public function directories($directory = null, $recursive = false); /** * Get all (recursive) of the directories within a given directory. * * @param string|null $directory * @return array */ public function allDirectories($directory = null); /** * Create a directory. * * @param string $path * @return bool */ public function makeDirectory($path); /** * Recursively delete a directory. * * @param string $directory * @return bool */ public function deleteDirectory($directory); } src/Illuminate/Contracts/Filesystem/Factory.php 0000644 00000000415 15107327036 0015646 0 ustar 00 <?php namespace Illuminate\Contracts\Filesystem; interface Factory { /** * Get a filesystem implementation. * * @param string|null $name * @return \Illuminate\Contracts\Filesystem\Filesystem */ public function disk($name = null); } src/Illuminate/Contracts/Filesystem/Cloud.php 0000644 00000000367 15107327036 0015313 0 ustar 00 <?php namespace Illuminate\Contracts\Filesystem; interface Cloud extends Filesystem { /** * Get the URL for the file at the given path. * * @param string $path * @return string */ public function url($path); } src/Illuminate/Contracts/Filesystem/FileNotFoundException.php 0000644 00000000174 15107327036 0020454 0 ustar 00 <?php namespace Illuminate\Contracts\Filesystem; use Exception; class FileNotFoundException extends Exception { // } src/Illuminate/Contracts/composer.json 0000644 00000001503 15107327036 0014123 0 ustar 00 { "name": "illuminate/contracts", "description": "The Illuminate Contracts package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "psr/container": "^1.1.1|^2.0.1", "psr/simple-cache": "^1.0|^2.0|^3.0" }, "autoload": { "psr-4": { "Illuminate\\Contracts\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Contracts/Events/Dispatcher.php 0000644 00000003440 15107327036 0015446 0 ustar 00 <?php namespace Illuminate\Contracts\Events; interface Dispatcher { /** * Register an event listener with the dispatcher. * * @param \Closure|string|array $events * @param \Closure|string|array|null $listener * @return void */ public function listen($events, $listener = null); /** * Determine if a given event has listeners. * * @param string $eventName * @return bool */ public function hasListeners($eventName); /** * Register an event subscriber with the dispatcher. * * @param object|string $subscriber * @return void */ public function subscribe($subscriber); /** * Dispatch an event until the first non-null response is returned. * * @param string|object $event * @param mixed $payload * @return mixed */ public function until($event, $payload = []); /** * Dispatch an event and call the listeners. * * @param string|object $event * @param mixed $payload * @param bool $halt * @return array|null */ public function dispatch($event, $payload = [], $halt = false); /** * Register an event and payload to be fired later. * * @param string $event * @param array $payload * @return void */ public function push($event, $payload = []); /** * Flush a set of pushed events. * * @param string $event * @return void */ public function flush($event); /** * Remove a set of listeners from the dispatcher. * * @param string $event * @return void */ public function forget($event); /** * Forget all of the queued listeners. * * @return void */ public function forgetPushed(); } src/Illuminate/Contracts/Events/ShouldHandleEventsAfterCommit.php 0000644 00000000142 15107327036 0021246 0 ustar 00 <?php namespace Illuminate\Contracts\Events; interface ShouldHandleEventsAfterCommit { // } src/Illuminate/Contracts/Events/ShouldDispatchAfterCommit.php 0000644 00000000136 15107327036 0020430 0 ustar 00 <?php namespace Illuminate\Contracts\Events; interface ShouldDispatchAfterCommit { // } src/Illuminate/Contracts/Mail/MailQueue.php 0000644 00000001210 15107327036 0014656 0 ustar 00 <?php namespace Illuminate\Contracts\Mail; interface MailQueue { /** * Queue a new e-mail message for sending. * * @param \Illuminate\Contracts\Mail\Mailable|string|array $view * @param string|null $queue * @return mixed */ public function queue($view, $queue = null); /** * Queue a new e-mail message for sending after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param \Illuminate\Contracts\Mail\Mailable|string|array $view * @param string|null $queue * @return mixed */ public function later($delay, $view, $queue = null); } src/Illuminate/Contracts/Mail/Mailable.php 0000644 00000003376 15107327036 0014514 0 ustar 00 <?php namespace Illuminate\Contracts\Mail; use Illuminate\Contracts\Queue\Factory as Queue; interface Mailable { /** * Send the message using the given mailer. * * @param \Illuminate\Contracts\Mail\Factory|\Illuminate\Contracts\Mail\Mailer $mailer * @return \Illuminate\Mail\SentMessage|null */ public function send($mailer); /** * Queue the given message. * * @param \Illuminate\Contracts\Queue\Factory $queue * @return mixed */ public function queue(Queue $queue); /** * Deliver the queued message after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param \Illuminate\Contracts\Queue\Factory $queue * @return mixed */ public function later($delay, Queue $queue); /** * Set the recipients of the message. * * @param object|array|string $address * @param string|null $name * @return self */ public function cc($address, $name = null); /** * Set the recipients of the message. * * @param object|array|string $address * @param string|null $name * @return $this */ public function bcc($address, $name = null); /** * Set the recipients of the message. * * @param object|array|string $address * @param string|null $name * @return $this */ public function to($address, $name = null); /** * Set the locale of the message. * * @param string $locale * @return $this */ public function locale($locale); /** * Set the name of the mailer that should be used to send the message. * * @param string $mailer * @return $this */ public function mailer($mailer); } src/Illuminate/Contracts/Mail/Mailer.php 0000644 00000001760 15107327036 0014212 0 ustar 00 <?php namespace Illuminate\Contracts\Mail; interface Mailer { /** * Begin the process of mailing a mailable class instance. * * @param mixed $users * @return \Illuminate\Mail\PendingMail */ public function to($users); /** * Begin the process of mailing a mailable class instance. * * @param mixed $users * @return \Illuminate\Mail\PendingMail */ public function bcc($users); /** * Send a new message with only a raw text part. * * @param string $text * @param mixed $callback * @return \Illuminate\Mail\SentMessage|null */ public function raw($text, $callback); /** * Send a new message using a view. * * @param \Illuminate\Contracts\Mail\Mailable|string|array $view * @param array $data * @param \Closure|string|null $callback * @return \Illuminate\Mail\SentMessage|null */ public function send($view, array $data = [], $callback = null); } src/Illuminate/Contracts/Mail/Factory.php 0000644 00000000375 15107327036 0014411 0 ustar 00 <?php namespace Illuminate\Contracts\Mail; interface Factory { /** * Get a mailer instance by name. * * @param string|null $name * @return \Illuminate\Contracts\Mail\Mailer */ public function mailer($name = null); } src/Illuminate/Contracts/Mail/Attachable.php 0000644 00000000343 15107327036 0015025 0 ustar 00 <?php namespace Illuminate\Contracts\Mail; interface Attachable { /** * Get an attachment instance for this entity. * * @return \Illuminate\Mail\Attachment */ public function toMailAttachment(); } src/Illuminate/Contracts/Auth/UserProvider.php 0000644 00000002532 15107327036 0015447 0 ustar 00 <?php namespace Illuminate\Contracts\Auth; interface UserProvider { /** * Retrieve a user by their unique identifier. * * @param mixed $identifier * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveById($identifier); /** * Retrieve a user by their unique identifier and "remember me" token. * * @param mixed $identifier * @param string $token * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByToken($identifier, $token); /** * Update the "remember me" token for the given user in storage. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param string $token * @return void */ public function updateRememberToken(Authenticatable $user, $token); /** * Retrieve a user by the given credentials. * * @param array $credentials * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByCredentials(array $credentials); /** * Validate a user against the given credentials. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param array $credentials * @return bool */ public function validateCredentials(Authenticatable $user, array $credentials); } src/Illuminate/Contracts/Auth/MustVerifyEmail.php 0000644 00000001232 15107327036 0016077 0 ustar 00 <?php namespace Illuminate\Contracts\Auth; interface MustVerifyEmail { /** * Determine if the user has verified their email address. * * @return bool */ public function hasVerifiedEmail(); /** * Mark the given user's email as verified. * * @return bool */ public function markEmailAsVerified(); /** * Send the email verification notification. * * @return void */ public function sendEmailVerificationNotification(); /** * Get the email address that should be used for verification. * * @return string */ public function getEmailForVerification(); } src/Illuminate/Contracts/Auth/SupportsBasicAuth.php 0000644 00000001173 15107327036 0016441 0 ustar 00 <?php namespace Illuminate\Contracts\Auth; interface SupportsBasicAuth { /** * Attempt to authenticate using HTTP Basic Auth. * * @param string $field * @param array $extraConditions * @return \Symfony\Component\HttpFoundation\Response|null */ public function basic($field = 'email', $extraConditions = []); /** * Perform a stateless HTTP Basic login attempt. * * @param string $field * @param array $extraConditions * @return \Symfony\Component\HttpFoundation\Response|null */ public function onceBasic($field = 'email', $extraConditions = []); } src/Illuminate/Contracts/Auth/StatefulGuard.php 0000644 00000003000 15107327036 0015557 0 ustar 00 <?php namespace Illuminate\Contracts\Auth; interface StatefulGuard extends Guard { /** * Attempt to authenticate a user using the given credentials. * * @param array $credentials * @param bool $remember * @return bool */ public function attempt(array $credentials = [], $remember = false); /** * Log a user into the application without sessions or cookies. * * @param array $credentials * @return bool */ public function once(array $credentials = []); /** * Log a user into the application. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param bool $remember * @return void */ public function login(Authenticatable $user, $remember = false); /** * Log the given user ID into the application. * * @param mixed $id * @param bool $remember * @return \Illuminate\Contracts\Auth\Authenticatable|bool */ public function loginUsingId($id, $remember = false); /** * Log the given user ID into the application without sessions or cookies. * * @param mixed $id * @return \Illuminate\Contracts\Auth\Authenticatable|bool */ public function onceUsingId($id); /** * Determine if the user was authenticated via "remember me" cookie. * * @return bool */ public function viaRemember(); /** * Log the user out of the application. * * @return void */ public function logout(); } src/Illuminate/Contracts/Auth/PasswordBrokerFactory.php 0000644 00000000434 15107327036 0017314 0 ustar 00 <?php namespace Illuminate\Contracts\Auth; interface PasswordBrokerFactory { /** * Get a password broker instance by name. * * @param string|null $name * @return \Illuminate\Contracts\Auth\PasswordBroker */ public function broker($name = null); } src/Illuminate/Contracts/Auth/Authenticatable.php 0000644 00000001664 15107327036 0016120 0 ustar 00 <?php namespace Illuminate\Contracts\Auth; interface Authenticatable { /** * Get the name of the unique identifier for the user. * * @return string */ public function getAuthIdentifierName(); /** * Get the unique identifier for the user. * * @return mixed */ public function getAuthIdentifier(); /** * Get the password for the user. * * @return string */ public function getAuthPassword(); /** * Get the token value for the "remember me" session. * * @return string */ public function getRememberToken(); /** * Set the token value for the "remember me" session. * * @param string $value * @return void */ public function setRememberToken($value); /** * Get the column name for the "remember me" token. * * @return string */ public function getRememberTokenName(); } src/Illuminate/Contracts/Auth/Factory.php 0000644 00000000711 15107327036 0014422 0 ustar 00 <?php namespace Illuminate\Contracts\Auth; interface Factory { /** * Get a guard instance by name. * * @param string|null $name * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard */ public function guard($name = null); /** * Set the default guard the factory should serve. * * @param string $name * @return void */ public function shouldUse($name); } src/Illuminate/Contracts/Auth/PasswordBroker.php 0000644 00000002410 15107327036 0015760 0 ustar 00 <?php namespace Illuminate\Contracts\Auth; use Closure; interface PasswordBroker { /** * Constant representing a successfully sent reminder. * * @var string */ const RESET_LINK_SENT = 'passwords.sent'; /** * Constant representing a successfully reset password. * * @var string */ const PASSWORD_RESET = 'passwords.reset'; /** * Constant representing the user not found response. * * @var string */ const INVALID_USER = 'passwords.user'; /** * Constant representing an invalid token. * * @var string */ const INVALID_TOKEN = 'passwords.token'; /** * Constant representing a throttled reset attempt. * * @var string */ const RESET_THROTTLED = 'passwords.throttled'; /** * Send a password reset link to a user. * * @param array $credentials * @param \Closure|null $callback * @return string */ public function sendResetLink(array $credentials, Closure $callback = null); /** * Reset the password for the given token. * * @param array $credentials * @param \Closure $callback * @return mixed */ public function reset(array $credentials, Closure $callback); } src/Illuminate/Contracts/Auth/Guard.php 0000644 00000002113 15107327036 0014053 0 ustar 00 <?php namespace Illuminate\Contracts\Auth; interface Guard { /** * Determine if the current user is authenticated. * * @return bool */ public function check(); /** * Determine if the current user is a guest. * * @return bool */ public function guest(); /** * Get the currently authenticated user. * * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function user(); /** * Get the ID for the currently authenticated user. * * @return int|string|null */ public function id(); /** * Validate a user's credentials. * * @param array $credentials * @return bool */ public function validate(array $credentials = []); /** * Determine if the guard has a user instance. * * @return bool */ public function hasUser(); /** * Set the current user. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ public function setUser(Authenticatable $user); } src/Illuminate/Contracts/Auth/Middleware/AuthenticatesRequests.php 0000644 00000000143 15107327036 0021424 0 ustar 00 <?php namespace Illuminate\Contracts\Auth\Middleware; interface AuthenticatesRequests { // } src/Illuminate/Contracts/Auth/Access/Authorizable.php 0000644 00000000466 15107327036 0016654 0 ustar 00 <?php namespace Illuminate\Contracts\Auth\Access; interface Authorizable { /** * Determine if the entity has a given ability. * * @param iterable|string $abilities * @param array|mixed $arguments * @return bool */ public function can($abilities, $arguments = []); } src/Illuminate/Contracts/Auth/Access/Gate.php 0000644 00000007213 15107327036 0015100 0 ustar 00 <?php namespace Illuminate\Contracts\Auth\Access; interface Gate { /** * Determine if a given ability has been defined. * * @param string $ability * @return bool */ public function has($ability); /** * Define a new ability. * * @param string $ability * @param callable|string $callback * @return $this */ public function define($ability, $callback); /** * Define abilities for a resource. * * @param string $name * @param string $class * @param array|null $abilities * @return $this */ public function resource($name, $class, array $abilities = null); /** * Define a policy class for a given class type. * * @param string $class * @param string $policy * @return $this */ public function policy($class, $policy); /** * Register a callback to run before all Gate checks. * * @param callable $callback * @return $this */ public function before(callable $callback); /** * Register a callback to run after all Gate checks. * * @param callable $callback * @return $this */ public function after(callable $callback); /** * Determine if the given ability should be granted for the current user. * * @param string $ability * @param array|mixed $arguments * @return bool */ public function allows($ability, $arguments = []); /** * Determine if the given ability should be denied for the current user. * * @param string $ability * @param array|mixed $arguments * @return bool */ public function denies($ability, $arguments = []); /** * Determine if all of the given abilities should be granted for the current user. * * @param iterable|string $abilities * @param array|mixed $arguments * @return bool */ public function check($abilities, $arguments = []); /** * Determine if any one of the given abilities should be granted for the current user. * * @param iterable|string $abilities * @param array|mixed $arguments * @return bool */ public function any($abilities, $arguments = []); /** * Determine if the given ability should be granted for the current user. * * @param string $ability * @param array|mixed $arguments * @return \Illuminate\Auth\Access\Response * * @throws \Illuminate\Auth\Access\AuthorizationException */ public function authorize($ability, $arguments = []); /** * Inspect the user for the given ability. * * @param string $ability * @param array|mixed $arguments * @return \Illuminate\Auth\Access\Response */ public function inspect($ability, $arguments = []); /** * Get the raw result from the authorization callback. * * @param string $ability * @param array|mixed $arguments * @return mixed * * @throws \Illuminate\Auth\Access\AuthorizationException */ public function raw($ability, $arguments = []); /** * Get a policy instance for a given class. * * @param object|string $class * @return mixed * * @throws \InvalidArgumentException */ public function getPolicyFor($class); /** * Get a guard instance for the given user. * * @param \Illuminate\Contracts\Auth\Authenticatable|mixed $user * @return static */ public function forUser($user); /** * Get all of the defined abilities. * * @return array */ public function abilities(); } src/Illuminate/Contracts/Auth/CanResetPassword.php 0000644 00000000636 15107327036 0016250 0 ustar 00 <?php namespace Illuminate\Contracts\Auth; interface CanResetPassword { /** * Get the e-mail address where password reset links are sent. * * @return string */ public function getEmailForPasswordReset(); /** * Send the password reset notification. * * @param string $token * @return void */ public function sendPasswordResetNotification($token); } src/Illuminate/Contracts/Session/Session.php 0000644 00000007467 15107327036 0015177 0 ustar 00 <?php namespace Illuminate\Contracts\Session; interface Session { /** * Get the name of the session. * * @return string */ public function getName(); /** * Set the name of the session. * * @param string $name * @return void */ public function setName($name); /** * Get the current session ID. * * @return string */ public function getId(); /** * Set the session ID. * * @param string $id * @return void */ public function setId($id); /** * Start the session, reading the data from a handler. * * @return bool */ public function start(); /** * Save the session data to storage. * * @return void */ public function save(); /** * Get all of the session data. * * @return array */ public function all(); /** * Checks if a key exists. * * @param string|array $key * @return bool */ public function exists($key); /** * Checks if a key is present and not null. * * @param string|array $key * @return bool */ public function has($key); /** * Get an item from the session. * * @param string $key * @param mixed $default * @return mixed */ public function get($key, $default = null); /** * Get the value of a given key and then forget it. * * @param string $key * @param mixed $default * @return mixed */ public function pull($key, $default = null); /** * Put a key / value pair or array of key / value pairs in the session. * * @param string|array $key * @param mixed $value * @return void */ public function put($key, $value = null); /** * Get the CSRF token value. * * @return string */ public function token(); /** * Regenerate the CSRF token value. * * @return void */ public function regenerateToken(); /** * Remove an item from the session, returning its value. * * @param string $key * @return mixed */ public function remove($key); /** * Remove one or many items from the session. * * @param string|array $keys * @return void */ public function forget($keys); /** * Remove all of the items from the session. * * @return void */ public function flush(); /** * Flush the session data and regenerate the ID. * * @return bool */ public function invalidate(); /** * Generate a new session identifier. * * @param bool $destroy * @return bool */ public function regenerate($destroy = false); /** * Generate a new session ID for the session. * * @param bool $destroy * @return bool */ public function migrate($destroy = false); /** * Determine if the session has been started. * * @return bool */ public function isStarted(); /** * Get the previous URL from the session. * * @return string|null */ public function previousUrl(); /** * Set the "previous" URL in the session. * * @param string $url * @return void */ public function setPreviousUrl($url); /** * Get the session handler instance. * * @return \SessionHandlerInterface */ public function getHandler(); /** * Determine if the session handler needs a request. * * @return bool */ public function handlerNeedsRequest(); /** * Set the request on the handler instance. * * @param \Illuminate\Http\Request $request * @return void */ public function setRequestOnHandler($request); } src/Illuminate/Contracts/Session/Middleware/AuthenticatesSessions.php 0000644 00000000146 15107327036 0022144 0 ustar 00 <?php namespace Illuminate\Contracts\Session\Middleware; interface AuthenticatesSessions { // } src/Illuminate/Contracts/Process/ProcessResult.php 0000644 00000002375 15107327036 0016355 0 ustar 00 <?php namespace Illuminate\Contracts\Process; interface ProcessResult { /** * Get the original command executed by the process. * * @return string */ public function command(); /** * Determine if the process was successful. * * @return bool */ public function successful(); /** * Determine if the process failed. * * @return bool */ public function failed(); /** * Get the exit code of the process. * * @return int|null */ public function exitCode(); /** * Get the standard output of the process. * * @return string */ public function output(); /** * Get the error output of the process. * * @return string */ public function errorOutput(); /** * Throw an exception if the process failed. * * @param callable|null $callback * @return $this */ public function throw(callable $callback = null); /** * Throw an exception if the process failed and the given condition is true. * * @param bool $condition * @param callable|null $callback * @return $this */ public function throwIf(bool $condition, callable $callback = null); } src/Illuminate/Contracts/Process/InvokedProcess.php 0000644 00000002304 15107327036 0016466 0 ustar 00 <?php namespace Illuminate\Contracts\Process; interface InvokedProcess { /** * Get the process ID if the process is still running. * * @return int|null */ public function id(); /** * Send a signal to the process. * * @param int $signal * @return $this */ public function signal(int $signal); /** * Determine if the process is still running. * * @return bool */ public function running(); /** * Get the standard output for the process. * * @return string */ public function output(); /** * Get the error output for the process. * * @return string */ public function errorOutput(); /** * Get the latest standard output for the process. * * @return string */ public function latestOutput(); /** * Get the latest error output for the process. * * @return string */ public function latestErrorOutput(); /** * Wait for the process to finish. * * @param callable|null $output * @return \Illuminate\Console\Process\ProcessResult */ public function wait(callable $output = null); } src/Illuminate/Contracts/Support/Renderable.php 0000644 00000000305 15107327036 0015630 0 ustar 00 <?php namespace Illuminate\Contracts\Support; interface Renderable { /** * Get the evaluated contents of the object. * * @return string */ public function render(); } src/Illuminate/Contracts/Support/CanBeEscapedWhenCastToString.php 0000644 00000000511 15107327036 0021150 0 ustar 00 <?php namespace Illuminate\Contracts\Support; interface CanBeEscapedWhenCastToString { /** * Indicate that the object's string representation should be escaped when __toString is invoked. * * @param bool $escape * @return $this */ public function escapeWhenCastingToString($escape = true); } src/Illuminate/Contracts/Support/Arrayable.php 0000644 00000000401 15107327036 0015464 0 ustar 00 <?php namespace Illuminate\Contracts\Support; /** * @template TKey of array-key * @template TValue */ interface Arrayable { /** * Get the instance as an array. * * @return array<TKey, TValue> */ public function toArray(); } src/Illuminate/Contracts/Support/DeferrableProvider.php 0000644 00000000317 15107327036 0017336 0 ustar 00 <?php namespace Illuminate\Contracts\Support; interface DeferrableProvider { /** * Get the services provided by the provider. * * @return array */ public function provides(); } src/Illuminate/Contracts/Support/Responsable.php 0000644 00000000462 15107327036 0016046 0 ustar 00 <?php namespace Illuminate\Contracts\Support; interface Responsable { /** * Create an HTTP response that represents the object. * * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function toResponse($request); } src/Illuminate/Contracts/Support/Htmlable.php 0000644 00000000272 15107327036 0015320 0 ustar 00 <?php namespace Illuminate\Contracts\Support; interface Htmlable { /** * Get content as a string of HTML. * * @return string */ public function toHtml(); } src/Illuminate/Contracts/Support/MessageProvider.php 0000644 00000000354 15107327036 0016670 0 ustar 00 <?php namespace Illuminate\Contracts\Support; interface MessageProvider { /** * Get the messages for the instance. * * @return \Illuminate\Contracts\Support\MessageBag */ public function getMessageBag(); } src/Illuminate/Contracts/Support/DeferringDisplayableValue.php 0000644 00000000435 15107327036 0020645 0 ustar 00 <?php namespace Illuminate\Contracts\Support; interface DeferringDisplayableValue { /** * Resolve the displayable value that the class is deferring. * * @return \Illuminate\Contracts\Support\Htmlable|string */ public function resolveDisplayableValue(); } src/Illuminate/Contracts/Support/Jsonable.php 0000644 00000000361 15107327036 0015324 0 ustar 00 <?php namespace Illuminate\Contracts\Support; interface Jsonable { /** * Convert the object to its JSON representation. * * @param int $options * @return string */ public function toJson($options = 0); } src/Illuminate/Contracts/Support/MessageBag.php 0000644 00000004272 15107327036 0015572 0 ustar 00 <?php namespace Illuminate\Contracts\Support; use Countable; interface MessageBag extends Arrayable, Countable { /** * Get the keys present in the message bag. * * @return array */ public function keys(); /** * Add a message to the bag. * * @param string $key * @param string $message * @return $this */ public function add($key, $message); /** * Merge a new array of messages into the bag. * * @param \Illuminate\Contracts\Support\MessageProvider|array $messages * @return $this */ public function merge($messages); /** * Determine if messages exist for a given key. * * @param string|array $key * @return bool */ public function has($key); /** * Get the first message from the bag for a given key. * * @param string|null $key * @param string|null $format * @return string */ public function first($key = null, $format = null); /** * Get all of the messages from the bag for a given key. * * @param string $key * @param string|null $format * @return array */ public function get($key, $format = null); /** * Get all of the messages for every key in the bag. * * @param string|null $format * @return array */ public function all($format = null); /** * Remove a message from the bag. * * @param string $key * @return $this */ public function forget($key); /** * Get the raw messages in the container. * * @return array */ public function getMessages(); /** * Get the default message format. * * @return string */ public function getFormat(); /** * Set the default message format. * * @param string $format * @return $this */ public function setFormat($format = ':message'); /** * Determine if the message bag has any messages. * * @return bool */ public function isEmpty(); /** * Determine if the message bag has any messages. * * @return bool */ public function isNotEmpty(); } src/Illuminate/Contracts/Support/ValidatedData.php 0000644 00000000256 15107327036 0016261 0 ustar 00 <?php namespace Illuminate\Contracts\Support; use ArrayAccess; use IteratorAggregate; interface ValidatedData extends Arrayable, ArrayAccess, IteratorAggregate { // } src/Illuminate/Contracts/Pagination/LengthAwarePaginator.php 0000644 00000001051 15107327036 0020247 0 ustar 00 <?php namespace Illuminate\Contracts\Pagination; interface LengthAwarePaginator extends Paginator { /** * Create a range of pagination URLs. * * @param int $start * @param int $end * @return array */ public function getUrlRange($start, $end); /** * Determine the total number of items in the data store. * * @return int */ public function total(); /** * Get the page number of the last available page. * * @return int */ public function lastPage(); } src/Illuminate/Contracts/Pagination/CursorPaginator.php 0000644 00000005024 15107327036 0017327 0 ustar 00 <?php namespace Illuminate\Contracts\Pagination; interface CursorPaginator { /** * Get the URL for a given cursor. * * @param \Illuminate\Pagination\Cursor|null $cursor * @return string */ public function url($cursor); /** * Add a set of query string values to the paginator. * * @param array|string|null $key * @param string|null $value * @return $this */ public function appends($key, $value = null); /** * Get / set the URL fragment to be appended to URLs. * * @param string|null $fragment * @return $this|string|null */ public function fragment($fragment = null); /** * Add all current query string values to the paginator. * * @return $this */ public function withQueryString(); /** * Get the URL for the previous page, or null. * * @return string|null */ public function previousPageUrl(); /** * The URL for the next page, or null. * * @return string|null */ public function nextPageUrl(); /** * Get all of the items being paginated. * * @return array */ public function items(); /** * Get the "cursor" of the previous set of items. * * @return \Illuminate\Pagination\Cursor|null */ public function previousCursor(); /** * Get the "cursor" of the next set of items. * * @return \Illuminate\Pagination\Cursor|null */ public function nextCursor(); /** * Determine how many items are being shown per page. * * @return int */ public function perPage(); /** * Get the current cursor being paginated. * * @return \Illuminate\Pagination\Cursor|null */ public function cursor(); /** * Determine if there are enough items to split into multiple pages. * * @return bool */ public function hasPages(); /** * Get the base path for paginator generated URLs. * * @return string|null */ public function path(); /** * Determine if the list of items is empty or not. * * @return bool */ public function isEmpty(); /** * Determine if the list of items is not empty. * * @return bool */ public function isNotEmpty(); /** * Render the paginator using a given view. * * @param string|null $view * @param array $data * @return string */ public function render($view = null, $data = []); } src/Illuminate/Contracts/Pagination/Paginator.php 0000644 00000004636 15107327036 0016141 0 ustar 00 <?php namespace Illuminate\Contracts\Pagination; interface Paginator { /** * Get the URL for a given page. * * @param int $page * @return string */ public function url($page); /** * Add a set of query string values to the paginator. * * @param array|string|null $key * @param string|null $value * @return $this */ public function appends($key, $value = null); /** * Get / set the URL fragment to be appended to URLs. * * @param string|null $fragment * @return $this|string|null */ public function fragment($fragment = null); /** * The URL for the next page, or null. * * @return string|null */ public function nextPageUrl(); /** * Get the URL for the previous page, or null. * * @return string|null */ public function previousPageUrl(); /** * Get all of the items being paginated. * * @return array */ public function items(); /** * Get the "index" of the first item being paginated. * * @return int|null */ public function firstItem(); /** * Get the "index" of the last item being paginated. * * @return int|null */ public function lastItem(); /** * Determine how many items are being shown per page. * * @return int */ public function perPage(); /** * Determine the current page being paginated. * * @return int */ public function currentPage(); /** * Determine if there are enough items to split into multiple pages. * * @return bool */ public function hasPages(); /** * Determine if there are more items in the data store. * * @return bool */ public function hasMorePages(); /** * Get the base path for paginator generated URLs. * * @return string|null */ public function path(); /** * Determine if the list of items is empty or not. * * @return bool */ public function isEmpty(); /** * Determine if the list of items is not empty. * * @return bool */ public function isNotEmpty(); /** * Render the paginator using a given view. * * @param string|null $view * @param array $data * @return string */ public function render($view = null, $data = []); } src/Illuminate/Contracts/Notifications/Dispatcher.php 0000644 00000001144 15107327036 0017012 0 ustar 00 <?php namespace Illuminate\Contracts\Notifications; interface Dispatcher { /** * Send the given notification to the given notifiable entities. * * @param \Illuminate\Support\Collection|array|mixed $notifiables * @param mixed $notification * @return void */ public function send($notifiables, $notification); /** * Send the given notification immediately. * * @param \Illuminate\Support\Collection|array|mixed $notifiables * @param mixed $notification * @return void */ public function sendNow($notifiables, $notification); } src/Illuminate/Contracts/Notifications/Factory.php 0000644 00000001402 15107327036 0016330 0 ustar 00 <?php namespace Illuminate\Contracts\Notifications; interface Factory { /** * Get a channel instance by name. * * @param string|null $name * @return mixed */ public function channel($name = null); /** * Send the given notification to the given notifiable entities. * * @param \Illuminate\Support\Collection|array|mixed $notifiables * @param mixed $notification * @return void */ public function send($notifiables, $notification); /** * Send the given notification immediately. * * @param \Illuminate\Support\Collection|array|mixed $notifiables * @param mixed $notification * @return void */ public function sendNow($notifiables, $notification); } src/Illuminate/Contracts/LICENSE.md 0000644 00000002063 15107327036 0013007 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Contracts/Redis/Factory.php 0000644 00000000412 15107327036 0014565 0 ustar 00 <?php namespace Illuminate\Contracts\Redis; interface Factory { /** * Get a Redis connection by name. * * @param string|null $name * @return \Illuminate\Redis\Connections\Connection */ public function connection($name = null); } src/Illuminate/Contracts/Redis/LimiterTimeoutException.php 0000644 00000000171 15107327036 0020013 0 ustar 00 <?php namespace Illuminate\Contracts\Redis; use Exception; class LimiterTimeoutException extends Exception { // } src/Illuminate/Contracts/Redis/Connector.php 0000644 00000001174 15107327036 0015116 0 ustar 00 <?php namespace Illuminate\Contracts\Redis; interface Connector { /** * Create a connection to a Redis cluster. * * @param array $config * @param array $options * @return \Illuminate\Redis\Connections\Connection */ public function connect(array $config, array $options); /** * Create a connection to a Redis instance. * * @param array $config * @param array $clusterOptions * @param array $options * @return \Illuminate\Redis\Connections\Connection */ public function connectToCluster(array $config, array $clusterOptions, array $options); } src/Illuminate/Contracts/Redis/Connection.php 0000644 00000001416 15107327036 0015262 0 ustar 00 <?php namespace Illuminate\Contracts\Redis; use Closure; interface Connection { /** * Subscribe to a set of given channels for messages. * * @param array|string $channels * @param \Closure $callback * @return void */ public function subscribe($channels, Closure $callback); /** * Subscribe to a set of given channels with wildcards. * * @param array|string $channels * @param \Closure $callback * @return void */ public function psubscribe($channels, Closure $callback); /** * Run a command against the Redis database. * * @param string $method * @param array $parameters * @return mixed */ public function command($method, array $parameters = []); } src/Illuminate/Contracts/Bus/QueueingDispatcher.php 0000644 00000001263 15107327036 0016437 0 ustar 00 <?php namespace Illuminate\Contracts\Bus; interface QueueingDispatcher extends Dispatcher { /** * Attempt to find the batch with the given ID. * * @param string $batchId * @return \Illuminate\Bus\Batch|null */ public function findBatch(string $batchId); /** * Create a new batch of queueable jobs. * * @param \Illuminate\Support\Collection|array $jobs * @return \Illuminate\Bus\PendingBatch */ public function batch($jobs); /** * Dispatch a command to its appropriate handler behind a queue. * * @param mixed $command * @return mixed */ public function dispatchToQueue($command); } src/Illuminate/Contracts/Bus/Dispatcher.php 0000644 00000002715 15107327036 0014737 0 ustar 00 <?php namespace Illuminate\Contracts\Bus; interface Dispatcher { /** * Dispatch a command to its appropriate handler. * * @param mixed $command * @return mixed */ public function dispatch($command); /** * Dispatch a command to its appropriate handler in the current process. * * Queueable jobs will be dispatched to the "sync" queue. * * @param mixed $command * @param mixed $handler * @return mixed */ public function dispatchSync($command, $handler = null); /** * Dispatch a command to its appropriate handler in the current process. * * @param mixed $command * @param mixed $handler * @return mixed */ public function dispatchNow($command, $handler = null); /** * Determine if the given command has a handler. * * @param mixed $command * @return bool */ public function hasCommandHandler($command); /** * Retrieve the handler for a command. * * @param mixed $command * @return bool|mixed */ public function getCommandHandler($command); /** * Set the pipes commands should be piped through before dispatching. * * @param array $pipes * @return $this */ public function pipeThrough(array $pipes); /** * Map a command to a handler. * * @param array $map * @return $this */ public function map(array $map); } src/Illuminate/Contracts/Encryption/StringEncrypter.php 0000644 00000001072 15107327036 0017407 0 ustar 00 <?php namespace Illuminate\Contracts\Encryption; interface StringEncrypter { /** * Encrypt a string without serialization. * * @param string $value * @return string * * @throws \Illuminate\Contracts\Encryption\EncryptException */ public function encryptString($value); /** * Decrypt the given string without unserialization. * * @param string $payload * @return string * * @throws \Illuminate\Contracts\Encryption\DecryptException */ public function decryptString($payload); } src/Illuminate/Contracts/Encryption/DecryptException.php 0000644 00000000205 15107327036 0017533 0 ustar 00 <?php namespace Illuminate\Contracts\Encryption; use RuntimeException; class DecryptException extends RuntimeException { // } src/Illuminate/Contracts/Encryption/Encrypter.php 0000644 00000001371 15107327036 0016222 0 ustar 00 <?php namespace Illuminate\Contracts\Encryption; interface Encrypter { /** * Encrypt the given value. * * @param mixed $value * @param bool $serialize * @return string * * @throws \Illuminate\Contracts\Encryption\EncryptException */ public function encrypt($value, $serialize = true); /** * Decrypt the given value. * * @param string $payload * @param bool $unserialize * @return mixed * * @throws \Illuminate\Contracts\Encryption\DecryptException */ public function decrypt($payload, $unserialize = true); /** * Get the encryption key that the encrypter is currently using. * * @return string */ public function getKey(); } src/Illuminate/Contracts/Encryption/EncryptException.php 0000644 00000000205 15107327036 0017545 0 ustar 00 <?php namespace Illuminate\Contracts\Encryption; use RuntimeException; class EncryptException extends RuntimeException { // } src/Illuminate/Contracts/View/Engine.php 0000644 00000000411 15107327036 0014226 0 ustar 00 <?php namespace Illuminate\Contracts\View; interface Engine { /** * Get the evaluated contents of the view. * * @param string $path * @param array $data * @return string */ public function get($path, array $data = []); } src/Illuminate/Contracts/View/View.php 0000644 00000001035 15107327036 0013736 0 ustar 00 <?php namespace Illuminate\Contracts\View; use Illuminate\Contracts\Support\Renderable; interface View extends Renderable { /** * Get the name of the view. * * @return string */ public function name(); /** * Add a piece of data to the view. * * @param string|array $key * @param mixed $value * @return $this */ public function with($key, $value = null); /** * Get the array of view data. * * @return array */ public function getData(); } src/Illuminate/Contracts/View/Factory.php 0000644 00000003615 15107327036 0014441 0 ustar 00 <?php namespace Illuminate\Contracts\View; interface Factory { /** * Determine if a given view exists. * * @param string $view * @return bool */ public function exists($view); /** * Get the evaluated view contents for the given path. * * @param string $path * @param \Illuminate\Contracts\Support\Arrayable|array $data * @param array $mergeData * @return \Illuminate\Contracts\View\View */ public function file($path, $data = [], $mergeData = []); /** * Get the evaluated view contents for the given view. * * @param string $view * @param \Illuminate\Contracts\Support\Arrayable|array $data * @param array $mergeData * @return \Illuminate\Contracts\View\View */ public function make($view, $data = [], $mergeData = []); /** * Add a piece of shared data to the environment. * * @param array|string $key * @param mixed $value * @return mixed */ public function share($key, $value = null); /** * Register a view composer event. * * @param array|string $views * @param \Closure|string $callback * @return array */ public function composer($views, $callback); /** * Register a view creator event. * * @param array|string $views * @param \Closure|string $callback * @return array */ public function creator($views, $callback); /** * Add a new namespace to the loader. * * @param string $namespace * @param string|array $hints * @return $this */ public function addNamespace($namespace, $hints); /** * Replace the namespace hints for the given namespace. * * @param string $namespace * @param string|array $hints * @return $this */ public function replaceNamespace($namespace, $hints); } src/Illuminate/Contracts/View/ViewCompilationException.php 0000644 00000000171 15107327036 0020014 0 ustar 00 <?php namespace Illuminate\Contracts\View; use Exception; class ViewCompilationException extends Exception { // } src/Illuminate/Contracts/Config/Repository.php 0000644 00000002212 15107327036 0015474 0 ustar 00 <?php namespace Illuminate\Contracts\Config; interface Repository { /** * Determine if the given configuration value exists. * * @param string $key * @return bool */ public function has($key); /** * Get the specified configuration value. * * @param array|string $key * @param mixed $default * @return mixed */ public function get($key, $default = null); /** * Get all of the configuration items for the application. * * @return array */ public function all(); /** * Set a given configuration value. * * @param array|string $key * @param mixed $value * @return void */ public function set($key, $value = null); /** * Prepend a value onto an array configuration value. * * @param string $key * @param mixed $value * @return void */ public function prepend($key, $value); /** * Push a value onto an array configuration value. * * @param string $key * @param mixed $value * @return void */ public function push($key, $value); } src/Illuminate/Contracts/Validation/ValidationRule.php 0000644 00000000630 15107327036 0017126 0 ustar 00 <?php namespace Illuminate\Contracts\Validation; use Closure; interface ValidationRule { /** * Run the validation rule. * * @param string $attribute * @param mixed $value * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * @return void */ public function validate(string $attribute, mixed $value, Closure $fail): void; } src/Illuminate/Contracts/Validation/ValidatorAwareRule.php 0000644 00000000474 15107327036 0017747 0 ustar 00 <?php namespace Illuminate\Contracts\Validation; use Illuminate\Validation\Validator; interface ValidatorAwareRule { /** * Set the current validator. * * @param \Illuminate\Validation\Validator $validator * @return $this */ public function setValidator(Validator $validator); } src/Illuminate/Contracts/Validation/ValidatesWhenResolved.php 0000644 00000000324 15107327036 0020446 0 ustar 00 <?php namespace Illuminate\Contracts\Validation; interface ValidatesWhenResolved { /** * Validate the given class instance. * * @return void */ public function validateResolved(); } src/Illuminate/Contracts/Validation/DataAwareRule.php 0000644 00000000347 15107327036 0016672 0 ustar 00 <?php namespace Illuminate\Contracts\Validation; interface DataAwareRule { /** * Set the data under validation. * * @param array $data * @return $this */ public function setData(array $data); } src/Illuminate/Contracts/Validation/Rule.php 0000644 00000000666 15107327036 0015124 0 ustar 00 <?php namespace Illuminate\Contracts\Validation; /** * @deprecated see ValidationRule */ interface Rule { /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value); /** * Get the validation error message. * * @return string|array */ public function message(); } src/Illuminate/Contracts/Validation/Factory.php 0000644 00000002240 15107327036 0015612 0 ustar 00 <?php namespace Illuminate\Contracts\Validation; interface Factory { /** * Create a new Validator instance. * * @param array $data * @param array $rules * @param array $messages * @param array $attributes * @return \Illuminate\Contracts\Validation\Validator */ public function make(array $data, array $rules, array $messages = [], array $attributes = []); /** * Register a custom validator extension. * * @param string $rule * @param \Closure|string $extension * @param string|null $message * @return void */ public function extend($rule, $extension, $message = null); /** * Register a custom implicit validator extension. * * @param string $rule * @param \Closure|string $extension * @param string|null $message * @return void */ public function extendImplicit($rule, $extension, $message = null); /** * Register a custom implicit validator message replacer. * * @param string $rule * @param \Closure|string $replacer * @return void */ public function replacer($rule, $replacer); } src/Illuminate/Contracts/Validation/Validator.php 0000644 00000002555 15107327036 0016141 0 ustar 00 <?php namespace Illuminate\Contracts\Validation; use Illuminate\Contracts\Support\MessageProvider; interface Validator extends MessageProvider { /** * Run the validator's rules against its data. * * @return array * * @throws \Illuminate\Validation\ValidationException */ public function validate(); /** * Get the attributes and values that were validated. * * @return array * * @throws \Illuminate\Validation\ValidationException */ public function validated(); /** * Determine if the data fails the validation rules. * * @return bool */ public function fails(); /** * Get the failed validation rules. * * @return array */ public function failed(); /** * Add conditions to a given field based on a Closure. * * @param string|array $attribute * @param string|array $rules * @param callable $callback * @return $this */ public function sometimes($attribute, $rules, callable $callback); /** * Add an after validation callback. * * @param callable|string $callback * @return $this */ public function after($callback); /** * Get all of the validation error messages. * * @return \Illuminate\Support\MessageBag */ public function errors(); } src/Illuminate/Contracts/Validation/ImplicitRule.php 0000644 00000000214 15107327036 0016604 0 ustar 00 <?php namespace Illuminate\Contracts\Validation; /** * @deprecated see ValidationRule */ interface ImplicitRule extends Rule { // } src/Illuminate/Contracts/Validation/InvokableRule.php 0000644 00000000673 15107327036 0016755 0 ustar 00 <?php namespace Illuminate\Contracts\Validation; use Closure; /** * @deprecated see ValidationRule */ interface InvokableRule { /** * Run the validation rule. * * @param string $attribute * @param mixed $value * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * @return void */ public function __invoke(string $attribute, mixed $value, Closure $fail); } src/Illuminate/Contracts/Validation/UncompromisedVerifier.php 0000644 00000000413 15107327036 0020523 0 ustar 00 <?php namespace Illuminate\Contracts\Validation; interface UncompromisedVerifier { /** * Verify that the given data has not been compromised in data leaks. * * @param array $data * @return bool */ public function verify($data); } src/Illuminate/Contracts/Cookie/QueueingFactory.php 0000644 00000001153 15107327036 0016436 0 ustar 00 <?php namespace Illuminate\Contracts\Cookie; interface QueueingFactory extends Factory { /** * Queue a cookie to send with the next response. * * @param mixed ...$parameters * @return void */ public function queue(...$parameters); /** * Remove a cookie from the queue. * * @param string $name * @param string|null $path * @return void */ public function unqueue($name, $path = null); /** * Get the cookies which have been queued for the next request. * * @return array */ public function getQueuedCookies(); } src/Illuminate/Contracts/Cookie/Factory.php 0000644 00000002633 15107327036 0014737 0 ustar 00 <?php namespace Illuminate\Contracts\Cookie; interface Factory { /** * Create a new cookie instance. * * @param string $name * @param string $value * @param int $minutes * @param string|null $path * @param string|null $domain * @param bool|null $secure * @param bool $httpOnly * @param bool $raw * @param string|null $sameSite * @return \Symfony\Component\HttpFoundation\Cookie */ public function make($name, $value, $minutes = 0, $path = null, $domain = null, $secure = null, $httpOnly = true, $raw = false, $sameSite = null); /** * Create a cookie that lasts "forever" (five years). * * @param string $name * @param string $value * @param string|null $path * @param string|null $domain * @param bool|null $secure * @param bool $httpOnly * @param bool $raw * @param string|null $sameSite * @return \Symfony\Component\HttpFoundation\Cookie */ public function forever($name, $value, $path = null, $domain = null, $secure = null, $httpOnly = true, $raw = false, $sameSite = null); /** * Expire the given cookie. * * @param string $name * @param string|null $path * @param string|null $domain * @return \Symfony\Component\HttpFoundation\Cookie */ public function forget($name, $path = null, $domain = null); } src/Illuminate/Contracts/Http/Kernel.php 0000644 00000001560 15107327036 0014254 0 ustar 00 <?php namespace Illuminate\Contracts\Http; interface Kernel { /** * Bootstrap the application for HTTP requests. * * @return void */ public function bootstrap(); /** * Handle an incoming HTTP request. * * @param \Symfony\Component\HttpFoundation\Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function handle($request); /** * Perform any final actions for the request lifecycle. * * @param \Symfony\Component\HttpFoundation\Request $request * @param \Symfony\Component\HttpFoundation\Response $response * @return void */ public function terminate($request, $response); /** * Get the Laravel application instance. * * @return \Illuminate\Contracts\Foundation\Application */ public function getApplication(); } src/Illuminate/Contracts/Pipeline/Pipeline.php 0000644 00000001367 15107327036 0015434 0 ustar 00 <?php namespace Illuminate\Contracts\Pipeline; use Closure; interface Pipeline { /** * Set the traveler object being sent on the pipeline. * * @param mixed $traveler * @return $this */ public function send($traveler); /** * Set the stops of the pipeline. * * @param dynamic|array $stops * @return $this */ public function through($stops); /** * Set the method to call on the stops. * * @param string $method * @return $this */ public function via($method); /** * Run the pipeline with a final destination callback. * * @param \Closure $destination * @return mixed */ public function then(Closure $destination); } src/Illuminate/Contracts/Pipeline/Hub.php 0000644 00000000446 15107327036 0014402 0 ustar 00 <?php namespace Illuminate\Contracts\Pipeline; interface Hub { /** * Send an object through one of the available pipelines. * * @param mixed $object * @param string|null $pipeline * @return mixed */ public function pipe($object, $pipeline = null); } src/Illuminate/Container/EntryNotFoundException.php 0000644 00000000306 15107327036 0016531 0 ustar 00 <?php namespace Illuminate\Container; use Exception; use Psr\Container\NotFoundExceptionInterface; class EntryNotFoundException extends Exception implements NotFoundExceptionInterface { // } src/Illuminate/Container/BoundMethod.php 0000644 00000015707 15107327036 0014317 0 ustar 00 <?php namespace Illuminate\Container; use Closure; use Illuminate\Contracts\Container\BindingResolutionException; use InvalidArgumentException; use ReflectionFunction; use ReflectionMethod; class BoundMethod { /** * Call the given Closure / class@method and inject its dependencies. * * @param \Illuminate\Container\Container $container * @param callable|string $callback * @param array $parameters * @param string|null $defaultMethod * @return mixed * * @throws \ReflectionException * @throws \InvalidArgumentException */ public static function call($container, $callback, array $parameters = [], $defaultMethod = null) { if (is_string($callback) && ! $defaultMethod && method_exists($callback, '__invoke')) { $defaultMethod = '__invoke'; } if (static::isCallableWithAtSign($callback) || $defaultMethod) { return static::callClass($container, $callback, $parameters, $defaultMethod); } return static::callBoundMethod($container, $callback, function () use ($container, $callback, $parameters) { return $callback(...array_values(static::getMethodDependencies($container, $callback, $parameters))); }); } /** * Call a string reference to a class using Class@method syntax. * * @param \Illuminate\Container\Container $container * @param string $target * @param array $parameters * @param string|null $defaultMethod * @return mixed * * @throws \InvalidArgumentException */ protected static function callClass($container, $target, array $parameters = [], $defaultMethod = null) { $segments = explode('@', $target); // We will assume an @ sign is used to delimit the class name from the method // name. We will split on this @ sign and then build a callable array that // we can pass right back into the "call" method for dependency binding. $method = count($segments) === 2 ? $segments[1] : $defaultMethod; if (is_null($method)) { throw new InvalidArgumentException('Method not provided.'); } return static::call( $container, [$container->make($segments[0]), $method], $parameters ); } /** * Call a method that has been bound to the container. * * @param \Illuminate\Container\Container $container * @param callable $callback * @param mixed $default * @return mixed */ protected static function callBoundMethod($container, $callback, $default) { if (! is_array($callback)) { return Util::unwrapIfClosure($default); } // Here we need to turn the array callable into a Class@method string we can use to // examine the container and see if there are any method bindings for this given // method. If there are, we can call this method binding callback immediately. $method = static::normalizeMethod($callback); if ($container->hasMethodBinding($method)) { return $container->callMethodBinding($method, $callback[0]); } return Util::unwrapIfClosure($default); } /** * Normalize the given callback into a Class@method string. * * @param callable $callback * @return string */ protected static function normalizeMethod($callback) { $class = is_string($callback[0]) ? $callback[0] : get_class($callback[0]); return "{$class}@{$callback[1]}"; } /** * Get all dependencies for a given method. * * @param \Illuminate\Container\Container $container * @param callable|string $callback * @param array $parameters * @return array * * @throws \ReflectionException */ protected static function getMethodDependencies($container, $callback, array $parameters = []) { $dependencies = []; foreach (static::getCallReflector($callback)->getParameters() as $parameter) { static::addDependencyForCallParameter($container, $parameter, $parameters, $dependencies); } return array_merge($dependencies, array_values($parameters)); } /** * Get the proper reflection instance for the given callback. * * @param callable|string $callback * @return \ReflectionFunctionAbstract * * @throws \ReflectionException */ protected static function getCallReflector($callback) { if (is_string($callback) && str_contains($callback, '::')) { $callback = explode('::', $callback); } elseif (is_object($callback) && ! $callback instanceof Closure) { $callback = [$callback, '__invoke']; } return is_array($callback) ? new ReflectionMethod($callback[0], $callback[1]) : new ReflectionFunction($callback); } /** * Get the dependency for the given call parameter. * * @param \Illuminate\Container\Container $container * @param \ReflectionParameter $parameter * @param array $parameters * @param array $dependencies * @return void * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ protected static function addDependencyForCallParameter($container, $parameter, array &$parameters, &$dependencies) { if (array_key_exists($paramName = $parameter->getName(), $parameters)) { $dependencies[] = $parameters[$paramName]; unset($parameters[$paramName]); } elseif (! is_null($className = Util::getParameterClassName($parameter))) { if (array_key_exists($className, $parameters)) { $dependencies[] = $parameters[$className]; unset($parameters[$className]); } elseif ($parameter->isVariadic()) { $variadicDependencies = $container->make($className); $dependencies = array_merge($dependencies, is_array($variadicDependencies) ? $variadicDependencies : [$variadicDependencies]); } else { $dependencies[] = $container->make($className); } } elseif ($parameter->isDefaultValueAvailable()) { $dependencies[] = $parameter->getDefaultValue(); } elseif (! $parameter->isOptional() && ! array_key_exists($paramName, $parameters)) { $message = "Unable to resolve dependency [{$parameter}] in class {$parameter->getDeclaringClass()->getName()}"; throw new BindingResolutionException($message); } } /** * Determine if the given string is in Class@method syntax. * * @param mixed $callback * @return bool */ protected static function isCallableWithAtSign($callback) { return is_string($callback) && str_contains($callback, '@'); } } src/Illuminate/Container/Container.php 0000644 00000120456 15107327036 0014027 0 ustar 00 <?php namespace Illuminate\Container; use ArrayAccess; use Closure; use Exception; use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Contracts\Container\CircularDependencyException; use Illuminate\Contracts\Container\Container as ContainerContract; use LogicException; use ReflectionClass; use ReflectionException; use ReflectionFunction; use ReflectionParameter; use TypeError; class Container implements ArrayAccess, ContainerContract { /** * The current globally available container (if any). * * @var static */ protected static $instance; /** * An array of the types that have been resolved. * * @var bool[] */ protected $resolved = []; /** * The container's bindings. * * @var array[] */ protected $bindings = []; /** * The container's method bindings. * * @var \Closure[] */ protected $methodBindings = []; /** * The container's shared instances. * * @var object[] */ protected $instances = []; /** * The container's scoped instances. * * @var array */ protected $scopedInstances = []; /** * The registered type aliases. * * @var string[] */ protected $aliases = []; /** * The registered aliases keyed by the abstract name. * * @var array[] */ protected $abstractAliases = []; /** * The extension closures for services. * * @var array[] */ protected $extenders = []; /** * All of the registered tags. * * @var array[] */ protected $tags = []; /** * The stack of concretions currently being built. * * @var array[] */ protected $buildStack = []; /** * The parameter override stack. * * @var array[] */ protected $with = []; /** * The contextual binding map. * * @var array[] */ public $contextual = []; /** * All of the registered rebound callbacks. * * @var array[] */ protected $reboundCallbacks = []; /** * All of the global before resolving callbacks. * * @var \Closure[] */ protected $globalBeforeResolvingCallbacks = []; /** * All of the global resolving callbacks. * * @var \Closure[] */ protected $globalResolvingCallbacks = []; /** * All of the global after resolving callbacks. * * @var \Closure[] */ protected $globalAfterResolvingCallbacks = []; /** * All of the before resolving callbacks by class type. * * @var array[] */ protected $beforeResolvingCallbacks = []; /** * All of the resolving callbacks by class type. * * @var array[] */ protected $resolvingCallbacks = []; /** * All of the after resolving callbacks by class type. * * @var array[] */ protected $afterResolvingCallbacks = []; /** * Define a contextual binding. * * @param array|string $concrete * @return \Illuminate\Contracts\Container\ContextualBindingBuilder */ public function when($concrete) { $aliases = []; foreach (Util::arrayWrap($concrete) as $c) { $aliases[] = $this->getAlias($c); } return new ContextualBindingBuilder($this, $aliases); } /** * Determine if the given abstract type has been bound. * * @param string $abstract * @return bool */ public function bound($abstract) { return isset($this->bindings[$abstract]) || isset($this->instances[$abstract]) || $this->isAlias($abstract); } /** * {@inheritdoc} * * @return bool */ public function has(string $id): bool { return $this->bound($id); } /** * Determine if the given abstract type has been resolved. * * @param string $abstract * @return bool */ public function resolved($abstract) { if ($this->isAlias($abstract)) { $abstract = $this->getAlias($abstract); } return isset($this->resolved[$abstract]) || isset($this->instances[$abstract]); } /** * Determine if a given type is shared. * * @param string $abstract * @return bool */ public function isShared($abstract) { return isset($this->instances[$abstract]) || (isset($this->bindings[$abstract]['shared']) && $this->bindings[$abstract]['shared'] === true); } /** * Determine if a given string is an alias. * * @param string $name * @return bool */ public function isAlias($name) { return isset($this->aliases[$name]); } /** * Register a binding with the container. * * @param string $abstract * @param \Closure|string|null $concrete * @param bool $shared * @return void * * @throws \TypeError */ public function bind($abstract, $concrete = null, $shared = false) { $this->dropStaleInstances($abstract); // If no concrete type was given, we will simply set the concrete type to the // abstract type. After that, the concrete type to be registered as shared // without being forced to state their classes in both of the parameters. if (is_null($concrete)) { $concrete = $abstract; } // If the factory is not a Closure, it means it is just a class name which is // bound into this container to the abstract type and we will just wrap it // up inside its own Closure to give us more convenience when extending. if (! $concrete instanceof Closure) { if (! is_string($concrete)) { throw new TypeError(self::class.'::bind(): Argument #2 ($concrete) must be of type Closure|string|null'); } $concrete = $this->getClosure($abstract, $concrete); } $this->bindings[$abstract] = compact('concrete', 'shared'); // If the abstract type was already resolved in this container we'll fire the // rebound listener so that any objects which have already gotten resolved // can have their copy of the object updated via the listener callbacks. if ($this->resolved($abstract)) { $this->rebound($abstract); } } /** * Get the Closure to be used when building a type. * * @param string $abstract * @param string $concrete * @return \Closure */ protected function getClosure($abstract, $concrete) { return function ($container, $parameters = []) use ($abstract, $concrete) { if ($abstract == $concrete) { return $container->build($concrete); } return $container->resolve( $concrete, $parameters, $raiseEvents = false ); }; } /** * Determine if the container has a method binding. * * @param string $method * @return bool */ public function hasMethodBinding($method) { return isset($this->methodBindings[$method]); } /** * Bind a callback to resolve with Container::call. * * @param array|string $method * @param \Closure $callback * @return void */ public function bindMethod($method, $callback) { $this->methodBindings[$this->parseBindMethod($method)] = $callback; } /** * Get the method to be bound in class@method format. * * @param array|string $method * @return string */ protected function parseBindMethod($method) { if (is_array($method)) { return $method[0].'@'.$method[1]; } return $method; } /** * Get the method binding for the given method. * * @param string $method * @param mixed $instance * @return mixed */ public function callMethodBinding($method, $instance) { return call_user_func($this->methodBindings[$method], $instance, $this); } /** * Add a contextual binding to the container. * * @param string $concrete * @param string $abstract * @param \Closure|string $implementation * @return void */ public function addContextualBinding($concrete, $abstract, $implementation) { $this->contextual[$concrete][$this->getAlias($abstract)] = $implementation; } /** * Register a binding if it hasn't already been registered. * * @param string $abstract * @param \Closure|string|null $concrete * @param bool $shared * @return void */ public function bindIf($abstract, $concrete = null, $shared = false) { if (! $this->bound($abstract)) { $this->bind($abstract, $concrete, $shared); } } /** * Register a shared binding in the container. * * @param string $abstract * @param \Closure|string|null $concrete * @return void */ public function singleton($abstract, $concrete = null) { $this->bind($abstract, $concrete, true); } /** * Register a shared binding if it hasn't already been registered. * * @param string $abstract * @param \Closure|string|null $concrete * @return void */ public function singletonIf($abstract, $concrete = null) { if (! $this->bound($abstract)) { $this->singleton($abstract, $concrete); } } /** * Register a scoped binding in the container. * * @param string $abstract * @param \Closure|string|null $concrete * @return void */ public function scoped($abstract, $concrete = null) { $this->scopedInstances[] = $abstract; $this->singleton($abstract, $concrete); } /** * Register a scoped binding if it hasn't already been registered. * * @param string $abstract * @param \Closure|string|null $concrete * @return void */ public function scopedIf($abstract, $concrete = null) { if (! $this->bound($abstract)) { $this->scoped($abstract, $concrete); } } /** * "Extend" an abstract type in the container. * * @param string $abstract * @param \Closure $closure * @return void * * @throws \InvalidArgumentException */ public function extend($abstract, Closure $closure) { $abstract = $this->getAlias($abstract); if (isset($this->instances[$abstract])) { $this->instances[$abstract] = $closure($this->instances[$abstract], $this); $this->rebound($abstract); } else { $this->extenders[$abstract][] = $closure; if ($this->resolved($abstract)) { $this->rebound($abstract); } } } /** * Register an existing instance as shared in the container. * * @param string $abstract * @param mixed $instance * @return mixed */ public function instance($abstract, $instance) { $this->removeAbstractAlias($abstract); $isBound = $this->bound($abstract); unset($this->aliases[$abstract]); // We'll check to determine if this type has been bound before, and if it has // we will fire the rebound callbacks registered with the container and it // can be updated with consuming classes that have gotten resolved here. $this->instances[$abstract] = $instance; if ($isBound) { $this->rebound($abstract); } return $instance; } /** * Remove an alias from the contextual binding alias cache. * * @param string $searched * @return void */ protected function removeAbstractAlias($searched) { if (! isset($this->aliases[$searched])) { return; } foreach ($this->abstractAliases as $abstract => $aliases) { foreach ($aliases as $index => $alias) { if ($alias == $searched) { unset($this->abstractAliases[$abstract][$index]); } } } } /** * Assign a set of tags to a given binding. * * @param array|string $abstracts * @param array|mixed ...$tags * @return void */ public function tag($abstracts, $tags) { $tags = is_array($tags) ? $tags : array_slice(func_get_args(), 1); foreach ($tags as $tag) { if (! isset($this->tags[$tag])) { $this->tags[$tag] = []; } foreach ((array) $abstracts as $abstract) { $this->tags[$tag][] = $abstract; } } } /** * Resolve all of the bindings for a given tag. * * @param string $tag * @return iterable */ public function tagged($tag) { if (! isset($this->tags[$tag])) { return []; } return new RewindableGenerator(function () use ($tag) { foreach ($this->tags[$tag] as $abstract) { yield $this->make($abstract); } }, count($this->tags[$tag])); } /** * Alias a type to a different name. * * @param string $abstract * @param string $alias * @return void * * @throws \LogicException */ public function alias($abstract, $alias) { if ($alias === $abstract) { throw new LogicException("[{$abstract}] is aliased to itself."); } $this->aliases[$alias] = $abstract; $this->abstractAliases[$abstract][] = $alias; } /** * Bind a new callback to an abstract's rebind event. * * @param string $abstract * @param \Closure $callback * @return mixed */ public function rebinding($abstract, Closure $callback) { $this->reboundCallbacks[$abstract = $this->getAlias($abstract)][] = $callback; if ($this->bound($abstract)) { return $this->make($abstract); } } /** * Refresh an instance on the given target and method. * * @param string $abstract * @param mixed $target * @param string $method * @return mixed */ public function refresh($abstract, $target, $method) { return $this->rebinding($abstract, function ($app, $instance) use ($target, $method) { $target->{$method}($instance); }); } /** * Fire the "rebound" callbacks for the given abstract type. * * @param string $abstract * @return void */ protected function rebound($abstract) { $instance = $this->make($abstract); foreach ($this->getReboundCallbacks($abstract) as $callback) { $callback($this, $instance); } } /** * Get the rebound callbacks for a given type. * * @param string $abstract * @return array */ protected function getReboundCallbacks($abstract) { return $this->reboundCallbacks[$abstract] ?? []; } /** * Wrap the given closure such that its dependencies will be injected when executed. * * @param \Closure $callback * @param array $parameters * @return \Closure */ public function wrap(Closure $callback, array $parameters = []) { return fn () => $this->call($callback, $parameters); } /** * Call the given Closure / class@method and inject its dependencies. * * @param callable|string $callback * @param array<string, mixed> $parameters * @param string|null $defaultMethod * @return mixed * * @throws \InvalidArgumentException */ public function call($callback, array $parameters = [], $defaultMethod = null) { $pushedToBuildStack = false; if (($className = $this->getClassForCallable($callback)) && ! in_array( $className, $this->buildStack, true )) { $this->buildStack[] = $className; $pushedToBuildStack = true; } $result = BoundMethod::call($this, $callback, $parameters, $defaultMethod); if ($pushedToBuildStack) { array_pop($this->buildStack); } return $result; } /** * Get the class name for the given callback, if one can be determined. * * @param callable|string $callback * @return string|false */ protected function getClassForCallable($callback) { if (PHP_VERSION_ID >= 80200) { if (is_callable($callback) && ! ($reflector = new ReflectionFunction($callback(...)))->isAnonymous()) { return $reflector->getClosureScopeClass()->name ?? false; } return false; } if (! is_array($callback)) { return false; } return is_string($callback[0]) ? $callback[0] : get_class($callback[0]); } /** * Get a closure to resolve the given type from the container. * * @param string $abstract * @return \Closure */ public function factory($abstract) { return fn () => $this->make($abstract); } /** * An alias function name for make(). * * @param string|callable $abstract * @param array $parameters * @return mixed * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function makeWith($abstract, array $parameters = []) { return $this->make($abstract, $parameters); } /** * Resolve the given type from the container. * * @param string|callable $abstract * @param array $parameters * @return mixed * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function make($abstract, array $parameters = []) { return $this->resolve($abstract, $parameters); } /** * {@inheritdoc} * * @return mixed */ public function get(string $id) { try { return $this->resolve($id); } catch (Exception $e) { if ($this->has($id) || $e instanceof CircularDependencyException) { throw $e; } throw new EntryNotFoundException($id, is_int($e->getCode()) ? $e->getCode() : 0, $e); } } /** * Resolve the given type from the container. * * @param string|callable $abstract * @param array $parameters * @param bool $raiseEvents * @return mixed * * @throws \Illuminate\Contracts\Container\BindingResolutionException * @throws \Illuminate\Contracts\Container\CircularDependencyException */ protected function resolve($abstract, $parameters = [], $raiseEvents = true) { $abstract = $this->getAlias($abstract); // First we'll fire any event handlers which handle the "before" resolving of // specific types. This gives some hooks the chance to add various extends // calls to change the resolution of objects that they're interested in. if ($raiseEvents) { $this->fireBeforeResolvingCallbacks($abstract, $parameters); } $concrete = $this->getContextualConcrete($abstract); $needsContextualBuild = ! empty($parameters) || ! is_null($concrete); // If an instance of the type is currently being managed as a singleton we'll // just return an existing instance instead of instantiating new instances // so the developer can keep using the same objects instance every time. if (isset($this->instances[$abstract]) && ! $needsContextualBuild) { return $this->instances[$abstract]; } $this->with[] = $parameters; if (is_null($concrete)) { $concrete = $this->getConcrete($abstract); } // We're ready to instantiate an instance of the concrete type registered for // the binding. This will instantiate the types, as well as resolve any of // its "nested" dependencies recursively until all have gotten resolved. $object = $this->isBuildable($concrete, $abstract) ? $this->build($concrete) : $this->make($concrete); // If we defined any extenders for this type, we'll need to spin through them // and apply them to the object being built. This allows for the extension // of services, such as changing configuration or decorating the object. foreach ($this->getExtenders($abstract) as $extender) { $object = $extender($object, $this); } // If the requested type is registered as a singleton we'll want to cache off // the instances in "memory" so we can return it later without creating an // entirely new instance of an object on each subsequent request for it. if ($this->isShared($abstract) && ! $needsContextualBuild) { $this->instances[$abstract] = $object; } if ($raiseEvents) { $this->fireResolvingCallbacks($abstract, $object); } // Before returning, we will also set the resolved flag to "true" and pop off // the parameter overrides for this build. After those two things are done // we will be ready to return back the fully constructed class instance. $this->resolved[$abstract] = true; array_pop($this->with); return $object; } /** * Get the concrete type for a given abstract. * * @param string|callable $abstract * @return mixed */ protected function getConcrete($abstract) { // If we don't have a registered resolver or concrete for the type, we'll just // assume each type is a concrete name and will attempt to resolve it as is // since the container should be able to resolve concretes automatically. if (isset($this->bindings[$abstract])) { return $this->bindings[$abstract]['concrete']; } return $abstract; } /** * Get the contextual concrete binding for the given abstract. * * @param string|callable $abstract * @return \Closure|string|array|null */ protected function getContextualConcrete($abstract) { if (! is_null($binding = $this->findInContextualBindings($abstract))) { return $binding; } // Next we need to see if a contextual binding might be bound under an alias of the // given abstract type. So, we will need to check if any aliases exist with this // type and then spin through them and check for contextual bindings on these. if (empty($this->abstractAliases[$abstract])) { return; } foreach ($this->abstractAliases[$abstract] as $alias) { if (! is_null($binding = $this->findInContextualBindings($alias))) { return $binding; } } } /** * Find the concrete binding for the given abstract in the contextual binding array. * * @param string|callable $abstract * @return \Closure|string|null */ protected function findInContextualBindings($abstract) { return $this->contextual[end($this->buildStack)][$abstract] ?? null; } /** * Determine if the given concrete is buildable. * * @param mixed $concrete * @param string $abstract * @return bool */ protected function isBuildable($concrete, $abstract) { return $concrete === $abstract || $concrete instanceof Closure; } /** * Instantiate a concrete instance of the given type. * * @param \Closure|string $concrete * @return mixed * * @throws \Illuminate\Contracts\Container\BindingResolutionException * @throws \Illuminate\Contracts\Container\CircularDependencyException */ public function build($concrete) { // If the concrete type is actually a Closure, we will just execute it and // hand back the results of the functions, which allows functions to be // used as resolvers for more fine-tuned resolution of these objects. if ($concrete instanceof Closure) { return $concrete($this, $this->getLastParameterOverride()); } try { $reflector = new ReflectionClass($concrete); } catch (ReflectionException $e) { throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e); } // If the type is not instantiable, the developer is attempting to resolve // an abstract type such as an Interface or Abstract Class and there is // no binding registered for the abstractions so we need to bail out. if (! $reflector->isInstantiable()) { return $this->notInstantiable($concrete); } $this->buildStack[] = $concrete; $constructor = $reflector->getConstructor(); // If there are no constructors, that means there are no dependencies then // we can just resolve the instances of the objects right away, without // resolving any other types or dependencies out of these containers. if (is_null($constructor)) { array_pop($this->buildStack); return new $concrete; } $dependencies = $constructor->getParameters(); // Once we have all the constructor's parameters we can create each of the // dependency instances and then use the reflection instances to make a // new instance of this class, injecting the created dependencies in. try { $instances = $this->resolveDependencies($dependencies); } catch (BindingResolutionException $e) { array_pop($this->buildStack); throw $e; } array_pop($this->buildStack); return $reflector->newInstanceArgs($instances); } /** * Resolve all of the dependencies from the ReflectionParameters. * * @param \ReflectionParameter[] $dependencies * @return array * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ protected function resolveDependencies(array $dependencies) { $results = []; foreach ($dependencies as $dependency) { // If the dependency has an override for this particular build we will use // that instead as the value. Otherwise, we will continue with this run // of resolutions and let reflection attempt to determine the result. if ($this->hasParameterOverride($dependency)) { $results[] = $this->getParameterOverride($dependency); continue; } // If the class is null, it means the dependency is a string or some other // primitive type which we can not resolve since it is not a class and // we will just bomb out with an error since we have no-where to go. $result = is_null(Util::getParameterClassName($dependency)) ? $this->resolvePrimitive($dependency) : $this->resolveClass($dependency); if ($dependency->isVariadic()) { $results = array_merge($results, $result); } else { $results[] = $result; } } return $results; } /** * Determine if the given dependency has a parameter override. * * @param \ReflectionParameter $dependency * @return bool */ protected function hasParameterOverride($dependency) { return array_key_exists( $dependency->name, $this->getLastParameterOverride() ); } /** * Get a parameter override for a dependency. * * @param \ReflectionParameter $dependency * @return mixed */ protected function getParameterOverride($dependency) { return $this->getLastParameterOverride()[$dependency->name]; } /** * Get the last parameter override. * * @return array */ protected function getLastParameterOverride() { return count($this->with) ? end($this->with) : []; } /** * Resolve a non-class hinted primitive dependency. * * @param \ReflectionParameter $parameter * @return mixed * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ protected function resolvePrimitive(ReflectionParameter $parameter) { if (! is_null($concrete = $this->getContextualConcrete('$'.$parameter->getName()))) { return Util::unwrapIfClosure($concrete, $this); } if ($parameter->isDefaultValueAvailable()) { return $parameter->getDefaultValue(); } if ($parameter->isVariadic()) { return []; } $this->unresolvablePrimitive($parameter); } /** * Resolve a class based dependency from the container. * * @param \ReflectionParameter $parameter * @return mixed * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ protected function resolveClass(ReflectionParameter $parameter) { try { return $parameter->isVariadic() ? $this->resolveVariadicClass($parameter) : $this->make(Util::getParameterClassName($parameter)); } // If we can not resolve the class instance, we will check to see if the value // is optional, and if it is we will return the optional parameter value as // the value of the dependency, similarly to how we do this with scalars. catch (BindingResolutionException $e) { if ($parameter->isDefaultValueAvailable()) { array_pop($this->with); return $parameter->getDefaultValue(); } if ($parameter->isVariadic()) { array_pop($this->with); return []; } throw $e; } } /** * Resolve a class based variadic dependency from the container. * * @param \ReflectionParameter $parameter * @return mixed */ protected function resolveVariadicClass(ReflectionParameter $parameter) { $className = Util::getParameterClassName($parameter); $abstract = $this->getAlias($className); if (! is_array($concrete = $this->getContextualConcrete($abstract))) { return $this->make($className); } return array_map(fn ($abstract) => $this->resolve($abstract), $concrete); } /** * Throw an exception that the concrete is not instantiable. * * @param string $concrete * @return void * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ protected function notInstantiable($concrete) { if (! empty($this->buildStack)) { $previous = implode(', ', $this->buildStack); $message = "Target [$concrete] is not instantiable while building [$previous]."; } else { $message = "Target [$concrete] is not instantiable."; } throw new BindingResolutionException($message); } /** * Throw an exception for an unresolvable primitive. * * @param \ReflectionParameter $parameter * @return void * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ protected function unresolvablePrimitive(ReflectionParameter $parameter) { $message = "Unresolvable dependency resolving [$parameter] in class {$parameter->getDeclaringClass()->getName()}"; throw new BindingResolutionException($message); } /** * Register a new before resolving callback for all types. * * @param \Closure|string $abstract * @param \Closure|null $callback * @return void */ public function beforeResolving($abstract, Closure $callback = null) { if (is_string($abstract)) { $abstract = $this->getAlias($abstract); } if ($abstract instanceof Closure && is_null($callback)) { $this->globalBeforeResolvingCallbacks[] = $abstract; } else { $this->beforeResolvingCallbacks[$abstract][] = $callback; } } /** * Register a new resolving callback. * * @param \Closure|string $abstract * @param \Closure|null $callback * @return void */ public function resolving($abstract, Closure $callback = null) { if (is_string($abstract)) { $abstract = $this->getAlias($abstract); } if (is_null($callback) && $abstract instanceof Closure) { $this->globalResolvingCallbacks[] = $abstract; } else { $this->resolvingCallbacks[$abstract][] = $callback; } } /** * Register a new after resolving callback for all types. * * @param \Closure|string $abstract * @param \Closure|null $callback * @return void */ public function afterResolving($abstract, Closure $callback = null) { if (is_string($abstract)) { $abstract = $this->getAlias($abstract); } if ($abstract instanceof Closure && is_null($callback)) { $this->globalAfterResolvingCallbacks[] = $abstract; } else { $this->afterResolvingCallbacks[$abstract][] = $callback; } } /** * Fire all of the before resolving callbacks. * * @param string $abstract * @param array $parameters * @return void */ protected function fireBeforeResolvingCallbacks($abstract, $parameters = []) { $this->fireBeforeCallbackArray($abstract, $parameters, $this->globalBeforeResolvingCallbacks); foreach ($this->beforeResolvingCallbacks as $type => $callbacks) { if ($type === $abstract || is_subclass_of($abstract, $type)) { $this->fireBeforeCallbackArray($abstract, $parameters, $callbacks); } } } /** * Fire an array of callbacks with an object. * * @param string $abstract * @param array $parameters * @param array $callbacks * @return void */ protected function fireBeforeCallbackArray($abstract, $parameters, array $callbacks) { foreach ($callbacks as $callback) { $callback($abstract, $parameters, $this); } } /** * Fire all of the resolving callbacks. * * @param string $abstract * @param mixed $object * @return void */ protected function fireResolvingCallbacks($abstract, $object) { $this->fireCallbackArray($object, $this->globalResolvingCallbacks); $this->fireCallbackArray( $object, $this->getCallbacksForType($abstract, $object, $this->resolvingCallbacks) ); $this->fireAfterResolvingCallbacks($abstract, $object); } /** * Fire all of the after resolving callbacks. * * @param string $abstract * @param mixed $object * @return void */ protected function fireAfterResolvingCallbacks($abstract, $object) { $this->fireCallbackArray($object, $this->globalAfterResolvingCallbacks); $this->fireCallbackArray( $object, $this->getCallbacksForType($abstract, $object, $this->afterResolvingCallbacks) ); } /** * Get all callbacks for a given type. * * @param string $abstract * @param object $object * @param array $callbacksPerType * @return array */ protected function getCallbacksForType($abstract, $object, array $callbacksPerType) { $results = []; foreach ($callbacksPerType as $type => $callbacks) { if ($type === $abstract || $object instanceof $type) { $results = array_merge($results, $callbacks); } } return $results; } /** * Fire an array of callbacks with an object. * * @param mixed $object * @param array $callbacks * @return void */ protected function fireCallbackArray($object, array $callbacks) { foreach ($callbacks as $callback) { $callback($object, $this); } } /** * Get the container's bindings. * * @return array */ public function getBindings() { return $this->bindings; } /** * Get the alias for an abstract if available. * * @param string $abstract * @return string */ public function getAlias($abstract) { return isset($this->aliases[$abstract]) ? $this->getAlias($this->aliases[$abstract]) : $abstract; } /** * Get the extender callbacks for a given type. * * @param string $abstract * @return array */ protected function getExtenders($abstract) { return $this->extenders[$this->getAlias($abstract)] ?? []; } /** * Remove all of the extender callbacks for a given type. * * @param string $abstract * @return void */ public function forgetExtenders($abstract) { unset($this->extenders[$this->getAlias($abstract)]); } /** * Drop all of the stale instances and aliases. * * @param string $abstract * @return void */ protected function dropStaleInstances($abstract) { unset($this->instances[$abstract], $this->aliases[$abstract]); } /** * Remove a resolved instance from the instance cache. * * @param string $abstract * @return void */ public function forgetInstance($abstract) { unset($this->instances[$abstract]); } /** * Clear all of the instances from the container. * * @return void */ public function forgetInstances() { $this->instances = []; } /** * Clear all of the scoped instances from the container. * * @return void */ public function forgetScopedInstances() { foreach ($this->scopedInstances as $scoped) { unset($this->instances[$scoped]); } } /** * Flush the container of all bindings and resolved instances. * * @return void */ public function flush() { $this->aliases = []; $this->resolved = []; $this->bindings = []; $this->instances = []; $this->abstractAliases = []; $this->scopedInstances = []; } /** * Get the globally available instance of the container. * * @return static */ public static function getInstance() { if (is_null(static::$instance)) { static::$instance = new static; } return static::$instance; } /** * Set the shared instance of the container. * * @param \Illuminate\Contracts\Container\Container|null $container * @return \Illuminate\Contracts\Container\Container|static */ public static function setInstance(ContainerContract $container = null) { return static::$instance = $container; } /** * Determine if a given offset exists. * * @param string $key * @return bool */ public function offsetExists($key): bool { return $this->bound($key); } /** * Get the value at a given offset. * * @param string $key * @return mixed */ public function offsetGet($key): mixed { return $this->make($key); } /** * Set the value at a given offset. * * @param string $key * @param mixed $value * @return void */ public function offsetSet($key, $value): void { $this->bind($key, $value instanceof Closure ? $value : fn () => $value); } /** * Unset the value at a given offset. * * @param string $key * @return void */ public function offsetUnset($key): void { unset($this->bindings[$key], $this->instances[$key], $this->resolved[$key]); } /** * Dynamically access container services. * * @param string $key * @return mixed */ public function __get($key) { return $this[$key]; } /** * Dynamically set container services. * * @param string $key * @param mixed $value * @return void */ public function __set($key, $value) { $this[$key] = $value; } } src/Illuminate/Container/composer.json 0000644 00000001610 15107327036 0014104 0 ustar 00 { "name": "illuminate/container", "description": "The Illuminate Container package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/contracts": "^10.0", "psr/container": "^1.1.1|^2.0.1" }, "provide": { "psr/container-implementation": "1.1|2.0" }, "autoload": { "psr-4": { "Illuminate\\Container\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Container/RewindableGenerator.php 0000644 00000002151 15107327036 0016017 0 ustar 00 <?php namespace Illuminate\Container; use Countable; use IteratorAggregate; use Traversable; class RewindableGenerator implements Countable, IteratorAggregate { /** * The generator callback. * * @var callable */ protected $generator; /** * The number of tagged services. * * @var callable|int */ protected $count; /** * Create a new generator instance. * * @param callable $generator * @param callable|int $count * @return void */ public function __construct(callable $generator, $count) { $this->count = $count; $this->generator = $generator; } /** * Get an iterator from the generator. * * @return \Traversable */ public function getIterator(): Traversable { return ($this->generator)(); } /** * Get the total number of tagged services. * * @return int */ public function count(): int { if (is_callable($count = $this->count)) { $this->count = $count(); } return $this->count; } } src/Illuminate/Container/ContextualBindingBuilder.php 0000644 00000004450 15107327036 0017030 0 ustar 00 <?php namespace Illuminate\Container; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\ContextualBindingBuilder as ContextualBindingBuilderContract; class ContextualBindingBuilder implements ContextualBindingBuilderContract { /** * The underlying container instance. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * The concrete instance. * * @var string|array */ protected $concrete; /** * The abstract target. * * @var string */ protected $needs; /** * Create a new contextual binding builder. * * @param \Illuminate\Contracts\Container\Container $container * @param string|array $concrete * @return void */ public function __construct(Container $container, $concrete) { $this->concrete = $concrete; $this->container = $container; } /** * Define the abstract target that depends on the context. * * @param string $abstract * @return $this */ public function needs($abstract) { $this->needs = $abstract; return $this; } /** * Define the implementation for the contextual binding. * * @param \Closure|string|array $implementation * @return void */ public function give($implementation) { foreach (Util::arrayWrap($this->concrete) as $concrete) { $this->container->addContextualBinding($concrete, $this->needs, $implementation); } } /** * Define tagged services to be used as the implementation for the contextual binding. * * @param string $tag * @return void */ public function giveTagged($tag) { $this->give(function ($container) use ($tag) { $taggedServices = $container->tagged($tag); return is_array($taggedServices) ? $taggedServices : iterator_to_array($taggedServices); }); } /** * Specify the configuration item to bind as a primitive. * * @param string $key * @param mixed $default * @return void */ public function giveConfig($key, $default = null) { $this->give(fn ($container) => $container->get('config')->get($key, $default)); } } src/Illuminate/Container/LICENSE.md 0000644 00000002063 15107327036 0012771 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Container/Util.php 0000644 00000003225 15107327036 0013014 0 ustar 00 <?php namespace Illuminate\Container; use Closure; use ReflectionNamedType; /** * @internal */ class Util { /** * If the given value is not an array and not null, wrap it in one. * * From Arr::wrap() in Illuminate\Support. * * @param mixed $value * @return array */ public static function arrayWrap($value) { if (is_null($value)) { return []; } return is_array($value) ? $value : [$value]; } /** * Return the default value of the given value. * * From global value() helper in Illuminate\Support. * * @param mixed $value * @param mixed ...$args * @return mixed */ public static function unwrapIfClosure($value, ...$args) { return $value instanceof Closure ? $value(...$args) : $value; } /** * Get the class name of the given parameter's type, if possible. * * From Reflector::getParameterClassName() in Illuminate\Support. * * @param \ReflectionParameter $parameter * @return string|null */ public static function getParameterClassName($parameter) { $type = $parameter->getType(); if (! $type instanceof ReflectionNamedType || $type->isBuiltin()) { return null; } $name = $type->getName(); if (! is_null($class = $parameter->getDeclaringClass())) { if ($name === 'self') { return $class->getName(); } if ($name === 'parent' && $parent = $class->getParentClass()) { return $parent->getName(); } } return $name; } } src/Illuminate/Foundation/helpers.php 0000644 00000062263 15107327036 0013734 0 ustar 00 <?php use Illuminate\Container\Container; use Illuminate\Contracts\Auth\Access\Gate; use Illuminate\Contracts\Auth\Factory as AuthFactory; use Illuminate\Contracts\Broadcasting\Factory as BroadcastFactory; use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Contracts\Cookie\Factory as CookieFactory; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Routing\ResponseFactory; use Illuminate\Contracts\Routing\UrlGenerator; use Illuminate\Contracts\Support\Responsable; use Illuminate\Contracts\Validation\Factory as ValidationFactory; use Illuminate\Contracts\View\Factory as ViewFactory; use Illuminate\Foundation\Bus\PendingClosureDispatch; use Illuminate\Foundation\Bus\PendingDispatch; use Illuminate\Foundation\Mix; use Illuminate\Http\Exceptions\HttpResponseException; use Illuminate\Queue\CallQueuedClosure; use Illuminate\Routing\Router; use Illuminate\Support\Facades\Date; use Illuminate\Support\HtmlString; use Symfony\Component\HttpFoundation\Response; if (! function_exists('abort')) { /** * Throw an HttpException with the given data. * * @param \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Support\Responsable|int $code * @param string $message * @param array $headers * @return never * * @throws \Symfony\Component\HttpKernel\Exception\HttpException * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ function abort($code, $message = '', array $headers = []) { if ($code instanceof Response) { throw new HttpResponseException($code); } elseif ($code instanceof Responsable) { throw new HttpResponseException($code->toResponse(request())); } app()->abort($code, $message, $headers); } } if (! function_exists('abort_if')) { /** * Throw an HttpException with the given data if the given condition is true. * * @param bool $boolean * @param \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Support\Responsable|int $code * @param string $message * @param array $headers * @return void * * @throws \Symfony\Component\HttpKernel\Exception\HttpException * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ function abort_if($boolean, $code, $message = '', array $headers = []) { if ($boolean) { abort($code, $message, $headers); } } } if (! function_exists('abort_unless')) { /** * Throw an HttpException with the given data unless the given condition is true. * * @param bool $boolean * @param \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Support\Responsable|int $code * @param string $message * @param array $headers * @return void * * @throws \Symfony\Component\HttpKernel\Exception\HttpException * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ function abort_unless($boolean, $code, $message = '', array $headers = []) { if (! $boolean) { abort($code, $message, $headers); } } } if (! function_exists('action')) { /** * Generate the URL to a controller action. * * @param string|array $name * @param mixed $parameters * @param bool $absolute * @return string */ function action($name, $parameters = [], $absolute = true) { return app('url')->action($name, $parameters, $absolute); } } if (! function_exists('app')) { /** * Get the available container instance. * * @param string|null $abstract * @param array $parameters * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Foundation\Application|mixed */ function app($abstract = null, array $parameters = []) { if (is_null($abstract)) { return Container::getInstance(); } return Container::getInstance()->make($abstract, $parameters); } } if (! function_exists('app_path')) { /** * Get the path to the application folder. * * @param string $path * @return string */ function app_path($path = '') { return app()->path($path); } } if (! function_exists('asset')) { /** * Generate an asset path for the application. * * @param string $path * @param bool|null $secure * @return string */ function asset($path, $secure = null) { return app('url')->asset($path, $secure); } } if (! function_exists('auth')) { /** * Get the available auth instance. * * @param string|null $guard * @return \Illuminate\Contracts\Auth\Factory|\Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard */ function auth($guard = null) { if (is_null($guard)) { return app(AuthFactory::class); } return app(AuthFactory::class)->guard($guard); } } if (! function_exists('back')) { /** * Create a new redirect response to the previous location. * * @param int $status * @param array $headers * @param mixed $fallback * @return \Illuminate\Http\RedirectResponse */ function back($status = 302, $headers = [], $fallback = false) { return app('redirect')->back($status, $headers, $fallback); } } if (! function_exists('base_path')) { /** * Get the path to the base of the install. * * @param string $path * @return string */ function base_path($path = '') { return app()->basePath($path); } } if (! function_exists('bcrypt')) { /** * Hash the given value against the bcrypt algorithm. * * @param string $value * @param array $options * @return string */ function bcrypt($value, $options = []) { return app('hash')->driver('bcrypt')->make($value, $options); } } if (! function_exists('broadcast')) { /** * Begin broadcasting an event. * * @param mixed|null $event * @return \Illuminate\Broadcasting\PendingBroadcast */ function broadcast($event = null) { return app(BroadcastFactory::class)->event($event); } } if (! function_exists('cache')) { /** * Get / set the specified cache value. * * If an array is passed, we'll assume you want to put to the cache. * * @param mixed ...$arguments key|key,default|data,expiration|null * @return mixed|\Illuminate\Cache\CacheManager * * @throws \InvalidArgumentException */ function cache(...$arguments) { if (empty($arguments)) { return app('cache'); } if (is_string($arguments[0])) { return app('cache')->get(...$arguments); } if (! is_array($arguments[0])) { throw new InvalidArgumentException( 'When setting a value in the cache, you must pass an array of key / value pairs.' ); } return app('cache')->put(key($arguments[0]), reset($arguments[0]), $arguments[1] ?? null); } } if (! function_exists('config')) { /** * Get / set the specified configuration value. * * If an array is passed as the key, we will assume you want to set an array of values. * * @param array|string|null $key * @param mixed $default * @return mixed|\Illuminate\Config\Repository */ function config($key = null, $default = null) { if (is_null($key)) { return app('config'); } if (is_array($key)) { return app('config')->set($key); } return app('config')->get($key, $default); } } if (! function_exists('config_path')) { /** * Get the configuration path. * * @param string $path * @return string */ function config_path($path = '') { return app()->configPath($path); } } if (! function_exists('cookie')) { /** * Create a new cookie instance. * * @param string|null $name * @param string|null $value * @param int $minutes * @param string|null $path * @param string|null $domain * @param bool|null $secure * @param bool $httpOnly * @param bool $raw * @param string|null $sameSite * @return \Illuminate\Cookie\CookieJar|\Symfony\Component\HttpFoundation\Cookie */ function cookie($name = null, $value = null, $minutes = 0, $path = null, $domain = null, $secure = null, $httpOnly = true, $raw = false, $sameSite = null) { $cookie = app(CookieFactory::class); if (is_null($name)) { return $cookie; } return $cookie->make($name, $value, $minutes, $path, $domain, $secure, $httpOnly, $raw, $sameSite); } } if (! function_exists('csrf_field')) { /** * Generate a CSRF token form field. * * @return \Illuminate\Support\HtmlString */ function csrf_field() { return new HtmlString('<input type="hidden" name="_token" value="'.csrf_token().'" autocomplete="off">'); } } if (! function_exists('csrf_token')) { /** * Get the CSRF token value. * * @return string * * @throws \RuntimeException */ function csrf_token() { $session = app('session'); if (isset($session)) { return $session->token(); } throw new RuntimeException('Application session store not set.'); } } if (! function_exists('database_path')) { /** * Get the database path. * * @param string $path * @return string */ function database_path($path = '') { return app()->databasePath($path); } } if (! function_exists('decrypt')) { /** * Decrypt the given value. * * @param string $value * @param bool $unserialize * @return mixed */ function decrypt($value, $unserialize = true) { return app('encrypter')->decrypt($value, $unserialize); } } if (! function_exists('dispatch')) { /** * Dispatch a job to its appropriate handler. * * @param mixed $job * @return \Illuminate\Foundation\Bus\PendingDispatch */ function dispatch($job) { return $job instanceof Closure ? new PendingClosureDispatch(CallQueuedClosure::create($job)) : new PendingDispatch($job); } } if (! function_exists('dispatch_sync')) { /** * Dispatch a command to its appropriate handler in the current process. * * Queueable jobs will be dispatched to the "sync" queue. * * @param mixed $job * @param mixed $handler * @return mixed */ function dispatch_sync($job, $handler = null) { return app(Dispatcher::class)->dispatchSync($job, $handler); } } if (! function_exists('encrypt')) { /** * Encrypt the given value. * * @param mixed $value * @param bool $serialize * @return string */ function encrypt($value, $serialize = true) { return app('encrypter')->encrypt($value, $serialize); } } if (! function_exists('event')) { /** * Dispatch an event and call the listeners. * * @param string|object $event * @param mixed $payload * @param bool $halt * @return array|null */ function event(...$args) { return app('events')->dispatch(...$args); } } if (! function_exists('fake') && class_exists(\Faker\Factory::class)) { /** * Get a faker instance. * * @param string|null $locale * @return \Faker\Generator */ function fake($locale = null) { if (app()->bound('config')) { $locale ??= app('config')->get('app.faker_locale'); } $locale ??= 'en_US'; $abstract = \Faker\Generator::class.':'.$locale; if (! app()->bound($abstract)) { app()->singleton($abstract, fn () => \Faker\Factory::create($locale)); } return app()->make($abstract); } } if (! function_exists('info')) { /** * Write some information to the log. * * @param string $message * @param array $context * @return void */ function info($message, $context = []) { app('log')->info($message, $context); } } if (! function_exists('logger')) { /** * Log a debug message to the logs. * * @param string|null $message * @param array $context * @return \Illuminate\Log\LogManager|null */ function logger($message = null, array $context = []) { if (is_null($message)) { return app('log'); } return app('log')->debug($message, $context); } } if (! function_exists('lang_path')) { /** * Get the path to the language folder. * * @param string $path * @return string */ function lang_path($path = '') { return app()->langPath($path); } } if (! function_exists('logs')) { /** * Get a log driver instance. * * @param string|null $driver * @return \Illuminate\Log\LogManager|\Psr\Log\LoggerInterface */ function logs($driver = null) { return $driver ? app('log')->driver($driver) : app('log'); } } if (! function_exists('method_field')) { /** * Generate a form field to spoof the HTTP verb used by forms. * * @param string $method * @return \Illuminate\Support\HtmlString */ function method_field($method) { return new HtmlString('<input type="hidden" name="_method" value="'.$method.'">'); } } if (! function_exists('mix')) { /** * Get the path to a versioned Mix file. * * @param string $path * @param string $manifestDirectory * @return \Illuminate\Support\HtmlString|string * * @throws \Exception */ function mix($path, $manifestDirectory = '') { return app(Mix::class)(...func_get_args()); } } if (! function_exists('now')) { /** * Create a new Carbon instance for the current time. * * @param \DateTimeZone|string|null $tz * @return \Illuminate\Support\Carbon */ function now($tz = null) { return Date::now($tz); } } if (! function_exists('old')) { /** * Retrieve an old input item. * * @param string|null $key * @param mixed $default * @return mixed */ function old($key = null, $default = null) { return app('request')->old($key, $default); } } if (! function_exists('policy')) { /** * Get a policy instance for a given class. * * @param object|string $class * @return mixed * * @throws \InvalidArgumentException */ function policy($class) { return app(Gate::class)->getPolicyFor($class); } } if (! function_exists('precognitive')) { /** * Handle a Precognition controller hook. * * @param null|callable $callable * @return mixed */ function precognitive($callable = null) { $callable ??= function () { // }; $payload = $callable(function ($default, $precognition = null) { $response = request()->isPrecognitive() ? ($precognition ?? $default) : $default; abort(Router::toResponse(request(), value($response))); }); if (request()->isPrecognitive()) { abort(204, headers: ['Precognition-Success' => 'true']); } return $payload; } } if (! function_exists('public_path')) { /** * Get the path to the public folder. * * @param string $path * @return string */ function public_path($path = '') { return app()->publicPath($path); } } if (! function_exists('redirect')) { /** * Get an instance of the redirector. * * @param string|null $to * @param int $status * @param array $headers * @param bool|null $secure * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse */ function redirect($to = null, $status = 302, $headers = [], $secure = null) { if (is_null($to)) { return app('redirect'); } return app('redirect')->to($to, $status, $headers, $secure); } } if (! function_exists('report')) { /** * Report an exception. * * @param \Throwable|string $exception * @return void */ function report($exception) { if (is_string($exception)) { $exception = new Exception($exception); } app(ExceptionHandler::class)->report($exception); } } if (! function_exists('report_if')) { /** * Report an exception if the given condition is true. * * @param bool $boolean * @param \Throwable|string $exception * @return void */ function report_if($boolean, $exception) { if ($boolean) { report($exception); } } } if (! function_exists('report_unless')) { /** * Report an exception unless the given condition is true. * * @param bool $boolean * @param \Throwable|string $exception * @return void */ function report_unless($boolean, $exception) { if (! $boolean) { report($exception); } } } if (! function_exists('request')) { /** * Get an instance of the current request or an input item from the request. * * @param array|string|null $key * @param mixed $default * @return mixed|\Illuminate\Http\Request|string|array|null */ function request($key = null, $default = null) { if (is_null($key)) { return app('request'); } if (is_array($key)) { return app('request')->only($key); } $value = app('request')->__get($key); return is_null($value) ? value($default) : $value; } } if (! function_exists('rescue')) { /** * Catch a potential exception and return a default value. * * @template TRescueValue * @template TRescueFallback * * @param callable(): TRescueValue $callback * @param (callable(\Throwable): TRescueFallback)|TRescueFallback $rescue * @param bool|callable $report * @return TRescueValue|TRescueFallback */ function rescue(callable $callback, $rescue = null, $report = true) { try { return $callback(); } catch (Throwable $e) { if (value($report, $e)) { report($e); } return value($rescue, $e); } } } if (! function_exists('resolve')) { /** * Resolve a service from the container. * * @param string $name * @param array $parameters * @return mixed */ function resolve($name, array $parameters = []) { return app($name, $parameters); } } if (! function_exists('resource_path')) { /** * Get the path to the resources folder. * * @param string $path * @return string */ function resource_path($path = '') { return app()->resourcePath($path); } } if (! function_exists('response')) { /** * Return a new response from the application. * * @param \Illuminate\Contracts\View\View|string|array|null $content * @param int $status * @param array $headers * @return \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory */ function response($content = '', $status = 200, array $headers = []) { $factory = app(ResponseFactory::class); if (func_num_args() === 0) { return $factory; } return $factory->make($content, $status, $headers); } } if (! function_exists('route')) { /** * Generate the URL to a named route. * * @param string $name * @param mixed $parameters * @param bool $absolute * @return string */ function route($name, $parameters = [], $absolute = true) { return app('url')->route($name, $parameters, $absolute); } } if (! function_exists('secure_asset')) { /** * Generate an asset path for the application. * * @param string $path * @return string */ function secure_asset($path) { return asset($path, true); } } if (! function_exists('secure_url')) { /** * Generate a HTTPS url for the application. * * @param string $path * @param mixed $parameters * @return string */ function secure_url($path, $parameters = []) { return url($path, $parameters, true); } } if (! function_exists('session')) { /** * Get / set the specified session value. * * If an array is passed as the key, we will assume you want to set an array of values. * * @param array|string|null $key * @param mixed $default * @return mixed|\Illuminate\Session\Store|\Illuminate\Session\SessionManager */ function session($key = null, $default = null) { if (is_null($key)) { return app('session'); } if (is_array($key)) { return app('session')->put($key); } return app('session')->get($key, $default); } } if (! function_exists('storage_path')) { /** * Get the path to the storage folder. * * @param string $path * @return string */ function storage_path($path = '') { return app()->storagePath($path); } } if (! function_exists('to_route')) { /** * Create a new redirect response to a named route. * * @param string $route * @param mixed $parameters * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ function to_route($route, $parameters = [], $status = 302, $headers = []) { return redirect()->route($route, $parameters, $status, $headers); } } if (! function_exists('today')) { /** * Create a new Carbon instance for the current date. * * @param \DateTimeZone|string|null $tz * @return \Illuminate\Support\Carbon */ function today($tz = null) { return Date::today($tz); } } if (! function_exists('trans')) { /** * Translate the given message. * * @param string|null $key * @param array $replace * @param string|null $locale * @return \Illuminate\Contracts\Translation\Translator|string|array|null */ function trans($key = null, $replace = [], $locale = null) { if (is_null($key)) { return app('translator'); } return app('translator')->get($key, $replace, $locale); } } if (! function_exists('trans_choice')) { /** * Translates the given message based on a count. * * @param string $key * @param \Countable|int|array $number * @param array $replace * @param string|null $locale * @return string */ function trans_choice($key, $number, array $replace = [], $locale = null) { return app('translator')->choice($key, $number, $replace, $locale); } } if (! function_exists('__')) { /** * Translate the given message. * * @param string|null $key * @param array $replace * @param string|null $locale * @return string|array|null */ function __($key = null, $replace = [], $locale = null) { if (is_null($key)) { return $key; } return trans($key, $replace, $locale); } } if (! function_exists('url')) { /** * Generate a url for the application. * * @param string|null $path * @param mixed $parameters * @param bool|null $secure * @return \Illuminate\Contracts\Routing\UrlGenerator|string */ function url($path = null, $parameters = [], $secure = null) { if (is_null($path)) { return app(UrlGenerator::class); } return app(UrlGenerator::class)->to($path, $parameters, $secure); } } if (! function_exists('validator')) { /** * Create a new Validator instance. * * @param array $data * @param array $rules * @param array $messages * @param array $attributes * @return \Illuminate\Contracts\Validation\Validator|\Illuminate\Contracts\Validation\Factory */ function validator(array $data = [], array $rules = [], array $messages = [], array $attributes = []) { $factory = app(ValidationFactory::class); if (func_num_args() === 0) { return $factory; } return $factory->make($data, $rules, $messages, $attributes); } } if (! function_exists('view')) { /** * Get the evaluated view contents for the given view. * * @param string|null $view * @param \Illuminate\Contracts\Support\Arrayable|array $data * @param array $mergeData * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory */ function view($view = null, $data = [], $mergeData = []) { $factory = app(ViewFactory::class); if (func_num_args() === 0) { return $factory; } return $factory->make($view, $data, $mergeData); } } src/Illuminate/Foundation/FileBasedMaintenanceMode.php 0000644 00000002616 15107327036 0017054 0 ustar 00 <?php namespace Illuminate\Foundation; use Illuminate\Contracts\Foundation\MaintenanceMode as MaintenanceModeContract; class FileBasedMaintenanceMode implements MaintenanceModeContract { /** * Take the application down for maintenance. * * @param array $payload * @return void */ public function activate(array $payload): void { file_put_contents( $this->path(), json_encode($payload, JSON_PRETTY_PRINT) ); } /** * Take the application out of maintenance. * * @return void */ public function deactivate(): void { if ($this->active()) { unlink($this->path()); } } /** * Determine if the application is currently down for maintenance. * * @return bool */ public function active(): bool { return file_exists($this->path()); } /** * Get the data array which was provided when the application was placed into maintenance. * * @return array */ public function data(): array { return json_decode(file_get_contents($this->path()), true); } /** * Get the path where the file is stored that signals that the application is down for maintenance. * * @return string */ protected function path(): string { return storage_path('framework/down'); } } src/Illuminate/Foundation/MaintenanceModeManager.php 0000644 00000002215 15107327036 0016603 0 ustar 00 <?php namespace Illuminate\Foundation; use Illuminate\Support\Manager; class MaintenanceModeManager extends Manager { /** * Create an instance of the file based maintenance driver. * * @return \Illuminate\Foundation\FileBasedMaintenanceMode */ protected function createFileDriver(): FileBasedMaintenanceMode { return new FileBasedMaintenanceMode(); } /** * Create an instance of the cache based maintenance driver. * * @return \Illuminate\Foundation\CacheBasedMaintenanceMode * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ protected function createCacheDriver(): CacheBasedMaintenanceMode { return new CacheBasedMaintenanceMode( $this->container->make('cache'), $this->config->get('app.maintenance.store') ?: $this->config->get('cache.default'), 'illuminate:foundation:down' ); } /** * Get the default driver name. * * @return string */ public function getDefaultDriver(): string { return $this->config->get('app.maintenance.driver', 'file'); } } src/Illuminate/Foundation/ComposerScripts.php 0000644 00000003155 15107327036 0015424 0 ustar 00 <?php namespace Illuminate\Foundation; use Composer\Script\Event; class ComposerScripts { /** * Handle the post-install Composer event. * * @param \Composer\Script\Event $event * @return void */ public static function postInstall(Event $event) { require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php'; static::clearCompiled(); } /** * Handle the post-update Composer event. * * @param \Composer\Script\Event $event * @return void */ public static function postUpdate(Event $event) { require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php'; static::clearCompiled(); } /** * Handle the post-autoload-dump Composer event. * * @param \Composer\Script\Event $event * @return void */ public static function postAutoloadDump(Event $event) { require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php'; static::clearCompiled(); } /** * Clear the cached Laravel bootstrapping files. * * @return void */ protected static function clearCompiled() { $laravel = new Application(getcwd()); if (is_file($configPath = $laravel->getCachedConfigPath())) { @unlink($configPath); } if (is_file($servicesPath = $laravel->getCachedServicesPath())) { @unlink($servicesPath); } if (is_file($packagesPath = $laravel->getCachedPackagesPath())) { @unlink($packagesPath); } } } src/Illuminate/Foundation/Console/ComponentMakeCommand.php 0000644 00000010554 15107327036 0017727 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Concerns\CreatesMatchingTest; use Illuminate\Console\GeneratorCommand; use Illuminate\Foundation\Inspiring; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:component')] class ComponentMakeCommand extends GeneratorCommand { use CreatesMatchingTest; /** * The console command name. * * @var string */ protected $name = 'make:component'; /** * The console command description. * * @var string */ protected $description = 'Create a new view component class'; /** * The type of class being generated. * * @var string */ protected $type = 'Component'; /** * Execute the console command. * * @return void */ public function handle() { if ($this->option('view')) { $this->writeView(function () { $this->components->info($this->type.' created successfully.'); }); return; } if (parent::handle() === false && ! $this->option('force')) { return false; } if (! $this->option('inline')) { $this->writeView(); } } /** * Write the view for the component. * * @param callable|null $onSuccess * @return void */ protected function writeView($onSuccess = null) { $path = $this->viewPath( str_replace('.', '/', 'components.'.$this->getView()).'.blade.php' ); if (! $this->files->isDirectory(dirname($path))) { $this->files->makeDirectory(dirname($path), 0777, true, true); } if ($this->files->exists($path) && ! $this->option('force')) { $this->components->error('View already exists.'); return; } file_put_contents( $path, '<div> <!-- '.Inspiring::quotes()->random().' --> </div>' ); if ($onSuccess) { $onSuccess(); } } /** * Build the class with the given name. * * @param string $name * @return string */ protected function buildClass($name) { if ($this->option('inline')) { return str_replace( ['DummyView', '{{ view }}'], "<<<'blade'\n<div>\n <!-- ".Inspiring::quotes()->random()." -->\n</div>\nblade", parent::buildClass($name) ); } return str_replace( ['DummyView', '{{ view }}'], 'view(\'components.'.$this->getView().'\')', parent::buildClass($name) ); } /** * Get the view name relative to the components directory. * * @return string view */ protected function getView() { $name = str_replace('\\', '/', $this->argument('name')); return collect(explode('/', $name)) ->map(function ($part) { return Str::kebab($part); }) ->implode('.'); } /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->resolveStubPath('/stubs/view-component.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\View\Components'; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the component already exists'], ['inline', null, InputOption::VALUE_NONE, 'Create a component that renders an inline view'], ['view', null, InputOption::VALUE_NONE, 'Create an anonymous component with only a view'], ]; } } src/Illuminate/Foundation/Console/QueuedCommand.php 0000644 00000002011 15107327037 0016405 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Console\Kernel as KernelContract; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; class QueuedCommand implements ShouldQueue { use Dispatchable, Queueable; /** * The data to pass to the Artisan command. * * @var array */ protected $data; /** * Create a new job instance. * * @param array $data * @return void */ public function __construct($data) { $this->data = $data; } /** * Handle the job. * * @param \Illuminate\Contracts\Console\Kernel $kernel * @return void */ public function handle(KernelContract $kernel) { $kernel->call(...array_values($this->data)); } /** * Get the display name for the queued job. * * @return string */ public function displayName() { return array_values($this->data)[0]; } } src/Illuminate/Foundation/Console/ConsoleMakeCommand.php 0000644 00000004720 15107327037 0017366 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Concerns\CreatesMatchingTest; use Illuminate\Console\GeneratorCommand; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:command')] class ConsoleMakeCommand extends GeneratorCommand { use CreatesMatchingTest; /** * The console command name. * * @var string */ protected $name = 'make:command'; /** * The console command description. * * @var string */ protected $description = 'Create a new Artisan command'; /** * The type of class being generated. * * @var string */ protected $type = 'Console command'; /** * Replace the class name for the given stub. * * @param string $stub * @param string $name * @return string */ protected function replaceClass($stub, $name) { $stub = parent::replaceClass($stub, $name); $command = $this->option('command') ?: 'app:'.Str::of($name)->classBasename()->kebab()->value(); return str_replace(['dummy:command', '{{ command }}'], $command, $stub); } /** * Get the stub file for the generator. * * @return string */ protected function getStub() { $relativePath = '/stubs/console.stub'; return file_exists($customPath = $this->laravel->basePath(trim($relativePath, '/'))) ? $customPath : __DIR__.$relativePath; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Console\Commands'; } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return [ ['name', InputArgument::REQUIRED, 'The name of the command'], ]; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the console command already exists'], ['command', null, InputOption::VALUE_OPTIONAL, 'The terminal command that will be used to invoke the class'], ]; } } src/Illuminate/Foundation/Console/RouteCacheCommand.php 0000644 00000005350 15107327037 0017210 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract; use Illuminate\Filesystem\Filesystem; use Illuminate\Routing\RouteCollection; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'route:cache')] class RouteCacheCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'route:cache'; /** * The console command description. * * @var string */ protected $description = 'Create a route cache file for faster route registration'; /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * Create a new route command instance. * * @param \Illuminate\Filesystem\Filesystem $files * @return void */ public function __construct(Filesystem $files) { parent::__construct(); $this->files = $files; } /** * Execute the console command. * * @return void */ public function handle() { $this->callSilent('route:clear'); $routes = $this->getFreshApplicationRoutes(); if (count($routes) === 0) { return $this->components->error("Your application doesn't have any routes."); } foreach ($routes as $route) { $route->prepareForSerialization(); } $this->files->put( $this->laravel->getCachedRoutesPath(), $this->buildRouteCacheFile($routes) ); $this->components->info('Routes cached successfully.'); } /** * Boot a fresh copy of the application and get the routes. * * @return \Illuminate\Routing\RouteCollection */ protected function getFreshApplicationRoutes() { return tap($this->getFreshApplication()['router']->getRoutes(), function ($routes) { $routes->refreshNameLookups(); $routes->refreshActionLookups(); }); } /** * Get a fresh application instance. * * @return \Illuminate\Contracts\Foundation\Application */ protected function getFreshApplication() { return tap(require $this->laravel->bootstrapPath('app.php'), function ($app) { $app->make(ConsoleKernelContract::class)->bootstrap(); }); } /** * Build the route cache file. * * @param \Illuminate\Routing\RouteCollection $routes * @return string */ protected function buildRouteCacheFile(RouteCollection $routes) { $stub = $this->files->get(__DIR__.'/stubs/routes.stub'); return str_replace('{{routes}}', var_export($routes->compile(), true), $stub); } } src/Illuminate/Foundation/Console/EventMakeCommand.php 0000644 00000003725 15107327037 0017051 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:event')] class EventMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:event'; /** * The console command description. * * @var string */ protected $description = 'Create a new event class'; /** * The type of class being generated. * * @var string */ protected $type = 'Event'; /** * Determine if the class already exists. * * @param string $rawName * @return bool */ protected function alreadyExists($rawName) { return class_exists($rawName) || $this->files->exists($this->getPath($this->qualifyClass($rawName))); } /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->resolveStubPath('/stubs/event.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Events'; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the event already exists'], ]; } } src/Illuminate/Foundation/Console/ClosureCommand.php 0000644 00000003660 15107327037 0016604 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Closure; use Illuminate\Console\Command; use ReflectionFunction; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class ClosureCommand extends Command { /** * The command callback. * * @var \Closure */ protected $callback; /** * Create a new command instance. * * @param string $signature * @param \Closure $callback * @return void */ public function __construct($signature, Closure $callback) { $this->callback = $callback; $this->signature = $signature; parent::__construct(); } /** * Execute the console command. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { $inputs = array_merge($input->getArguments(), $input->getOptions()); $parameters = []; foreach ((new ReflectionFunction($this->callback))->getParameters() as $parameter) { if (isset($inputs[$parameter->getName()])) { $parameters[$parameter->getName()] = $inputs[$parameter->getName()]; } } return (int) $this->laravel->call( $this->callback->bindTo($this, $this), $parameters ); } /** * Set the description for the command. * * @param string $description * @return $this */ public function purpose($description) { return $this->describe($description); } /** * Set the description for the command. * * @param string $description * @return $this */ public function describe($description) { $this->setDescription($description); return $this; } } src/Illuminate/Foundation/Console/ProviderMakeCommand.php 0000644 00000003255 15107327037 0017560 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:provider')] class ProviderMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:provider'; /** * The console command description. * * @var string */ protected $description = 'Create a new service provider class'; /** * The type of class being generated. * * @var string */ protected $type = 'Provider'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->resolveStubPath('/stubs/provider.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Providers'; } /** * Get the console command arguments. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the provider already exists'], ]; } } src/Illuminate/Foundation/Console/RouteListCommand.php 0000644 00000035455 15107327037 0017131 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Closure; use Illuminate\Console\Command; use Illuminate\Contracts\Routing\UrlGenerator; use Illuminate\Routing\Route; use Illuminate\Routing\Router; use Illuminate\Routing\ViewController; use Illuminate\Support\Arr; use Illuminate\Support\Str; use ReflectionClass; use ReflectionFunction; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Terminal; #[AsCommand(name: 'route:list')] class RouteListCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'route:list'; /** * The console command description. * * @var string */ protected $description = 'List all registered routes'; /** * The router instance. * * @var \Illuminate\Routing\Router */ protected $router; /** * The table headers for the command. * * @var string[] */ protected $headers = ['Domain', 'Method', 'URI', 'Name', 'Action', 'Middleware']; /** * The terminal width resolver callback. * * @var \Closure|null */ protected static $terminalWidthResolver; /** * The verb colors for the command. * * @var array */ protected $verbColors = [ 'ANY' => 'red', 'GET' => 'blue', 'HEAD' => '#6C7280', 'OPTIONS' => '#6C7280', 'POST' => 'yellow', 'PUT' => 'yellow', 'PATCH' => 'yellow', 'DELETE' => 'red', ]; /** * Create a new route command instance. * * @param \Illuminate\Routing\Router $router * @return void */ public function __construct(Router $router) { parent::__construct(); $this->router = $router; } /** * Execute the console command. * * @return void */ public function handle() { if (! $this->output->isVeryVerbose()) { $this->router->flushMiddlewareGroups(); } if (! $this->router->getRoutes()->count()) { return $this->components->error("Your application doesn't have any routes."); } if (empty($routes = $this->getRoutes())) { return $this->components->error("Your application doesn't have any routes matching the given criteria."); } $this->displayRoutes($routes); } /** * Compile the routes into a displayable format. * * @return array */ protected function getRoutes() { $routes = collect($this->router->getRoutes())->map(function ($route) { return $this->getRouteInformation($route); })->filter()->all(); if (($sort = $this->option('sort')) !== null) { $routes = $this->sortRoutes($sort, $routes); } else { $routes = $this->sortRoutes('uri', $routes); } if ($this->option('reverse')) { $routes = array_reverse($routes); } return $this->pluckColumns($routes); } /** * Get the route information for a given route. * * @param \Illuminate\Routing\Route $route * @return array */ protected function getRouteInformation(Route $route) { return $this->filterRoute([ 'domain' => $route->domain(), 'method' => implode('|', $route->methods()), 'uri' => $route->uri(), 'name' => $route->getName(), 'action' => ltrim($route->getActionName(), '\\'), 'middleware' => $this->getMiddleware($route), 'vendor' => $this->isVendorRoute($route), ]); } /** * Sort the routes by a given element. * * @param string $sort * @param array $routes * @return array */ protected function sortRoutes($sort, array $routes) { return Arr::sort($routes, function ($route) use ($sort) { return $route[$sort]; }); } /** * Remove unnecessary columns from the routes. * * @param array $routes * @return array */ protected function pluckColumns(array $routes) { return array_map(function ($route) { return Arr::only($route, $this->getColumns()); }, $routes); } /** * Display the route information on the console. * * @param array $routes * @return void */ protected function displayRoutes(array $routes) { $routes = collect($routes); $this->output->writeln( $this->option('json') ? $this->asJson($routes) : $this->forCli($routes) ); } /** * Get the middleware for the route. * * @param \Illuminate\Routing\Route $route * @return string */ protected function getMiddleware($route) { return collect($this->router->gatherRouteMiddleware($route))->map(function ($middleware) { return $middleware instanceof Closure ? 'Closure' : $middleware; })->implode("\n"); } /** * Determine if the route has been defined outside of the application. * * @param \Illuminate\Routing\Route $route * @return bool */ protected function isVendorRoute(Route $route) { if ($route->action['uses'] instanceof Closure) { $path = (new ReflectionFunction($route->action['uses'])) ->getFileName(); } elseif (is_string($route->action['uses']) && str_contains($route->action['uses'], 'SerializableClosure')) { return false; } elseif (is_string($route->action['uses'])) { if ($this->isFrameworkController($route)) { return false; } $path = (new ReflectionClass($route->getControllerClass())) ->getFileName(); } else { return false; } return str_starts_with($path, base_path('vendor')); } /** * Determine if the route uses a framework controller. * * @param \Illuminate\Routing\Route $route * @return bool */ protected function isFrameworkController(Route $route) { return in_array($route->getControllerClass(), [ '\Illuminate\Routing\RedirectController', '\Illuminate\Routing\ViewController', ], true); } /** * Filter the route by URI and / or name. * * @param array $route * @return array|null */ protected function filterRoute(array $route) { if (($this->option('name') && ! Str::contains((string) $route['name'], $this->option('name'))) || ($this->option('path') && ! Str::contains($route['uri'], $this->option('path'))) || ($this->option('method') && ! Str::contains($route['method'], strtoupper($this->option('method')))) || ($this->option('domain') && ! Str::contains((string) $route['domain'], $this->option('domain'))) || ($this->option('except-vendor') && $route['vendor']) || ($this->option('only-vendor') && ! $route['vendor'])) { return; } if ($this->option('except-path')) { foreach (explode(',', $this->option('except-path')) as $path) { if (str_contains($route['uri'], $path)) { return; } } } return $route; } /** * Get the table headers for the visible columns. * * @return array */ protected function getHeaders() { return Arr::only($this->headers, array_keys($this->getColumns())); } /** * Get the column names to show (lowercase table headers). * * @return array */ protected function getColumns() { return array_map('strtolower', $this->headers); } /** * Parse the column list. * * @param array $columns * @return array */ protected function parseColumns(array $columns) { $results = []; foreach ($columns as $column) { if (str_contains($column, ',')) { $results = array_merge($results, explode(',', $column)); } else { $results[] = $column; } } return array_map('strtolower', $results); } /** * Convert the given routes to JSON. * * @param \Illuminate\Support\Collection $routes * @return string */ protected function asJson($routes) { return $routes ->map(function ($route) { $route['middleware'] = empty($route['middleware']) ? [] : explode("\n", $route['middleware']); return $route; }) ->values() ->toJson(); } /** * Convert the given routes to regular CLI output. * * @param \Illuminate\Support\Collection $routes * @return array */ protected function forCli($routes) { $routes = $routes->map( fn ($route) => array_merge($route, [ 'action' => $this->formatActionForCli($route), 'method' => $route['method'] == 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS' ? 'ANY' : $route['method'], 'uri' => $route['domain'] ? ($route['domain'].'/'.ltrim($route['uri'], '/')) : $route['uri'], ]), ); $maxMethod = mb_strlen($routes->max('method')); $terminalWidth = $this->getTerminalWidth(); $routeCount = $this->determineRouteCountOutput($routes, $terminalWidth); return $routes->map(function ($route) use ($maxMethod, $terminalWidth) { [ 'action' => $action, 'domain' => $domain, 'method' => $method, 'middleware' => $middleware, 'uri' => $uri, ] = $route; $middleware = Str::of($middleware)->explode("\n")->filter()->whenNotEmpty( fn ($collection) => $collection->map( fn ($middleware) => sprintf(' %s⇂ %s', str_repeat(' ', $maxMethod), $middleware) ) )->implode("\n"); $spaces = str_repeat(' ', max($maxMethod + 6 - mb_strlen($method), 0)); $dots = str_repeat('.', max( $terminalWidth - mb_strlen($method.$spaces.$uri.$action) - 6 - ($action ? 1 : 0), 0 )); $dots = empty($dots) ? $dots : " $dots"; if ($action && ! $this->output->isVerbose() && mb_strlen($method.$spaces.$uri.$action.$dots) > ($terminalWidth - 6)) { $action = substr($action, 0, $terminalWidth - 7 - mb_strlen($method.$spaces.$uri.$dots)).'…'; } $method = Str::of($method)->explode('|')->map( fn ($method) => sprintf('<fg=%s>%s</>', $this->verbColors[$method] ?? 'default', $method), )->implode('<fg=#6C7280>|</>'); return [sprintf( ' <fg=white;options=bold>%s</> %s<fg=white>%s</><fg=#6C7280>%s %s</>', $method, $spaces, preg_replace('#({[^}]+})#', '<fg=yellow>$1</>', $uri), $dots, str_replace(' ', ' › ', $action ?? ''), ), $this->output->isVerbose() && ! empty($middleware) ? "<fg=#6C7280>$middleware</>" : null]; }) ->flatten() ->filter() ->prepend('') ->push('')->push($routeCount)->push('') ->toArray(); } /** * Get the formatted action for display on the CLI. * * @param array $route * @return string */ protected function formatActionForCli($route) { ['action' => $action, 'name' => $name] = $route; if ($action === 'Closure' || $action === ViewController::class) { return $name; } $name = $name ? "$name " : null; $rootControllerNamespace = $this->laravel[UrlGenerator::class]->getRootControllerNamespace() ?? ($this->laravel->getNamespace().'Http\\Controllers'); if (str_starts_with($action, $rootControllerNamespace)) { return $name.substr($action, mb_strlen($rootControllerNamespace) + 1); } $actionClass = explode('@', $action)[0]; if (class_exists($actionClass) && str_starts_with((new ReflectionClass($actionClass))->getFilename(), base_path('vendor'))) { $actionCollection = collect(explode('\\', $action)); return $name.$actionCollection->take(2)->implode('\\').' '.$actionCollection->last(); } return $name.$action; } /** * Determine and return the output for displaying the number of routes in the CLI output. * * @param \Illuminate\Support\Collection $routes * @param int $terminalWidth * @return string */ protected function determineRouteCountOutput($routes, $terminalWidth) { $routeCountText = 'Showing ['.$routes->count().'] routes'; $offset = $terminalWidth - mb_strlen($routeCountText) - 2; $spaces = str_repeat(' ', $offset); return $spaces.'<fg=blue;options=bold>Showing ['.$routes->count().'] routes</>'; } /** * Get the terminal width. * * @return int */ public static function getTerminalWidth() { return is_null(static::$terminalWidthResolver) ? (new Terminal)->getWidth() : call_user_func(static::$terminalWidthResolver); } /** * Set a callback that should be used when resolving the terminal width. * * @param \Closure|null $resolver * @return void */ public static function resolveTerminalWidthUsing($resolver) { static::$terminalWidthResolver = $resolver; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['json', null, InputOption::VALUE_NONE, 'Output the route list as JSON'], ['method', null, InputOption::VALUE_OPTIONAL, 'Filter the routes by method'], ['name', null, InputOption::VALUE_OPTIONAL, 'Filter the routes by name'], ['domain', null, InputOption::VALUE_OPTIONAL, 'Filter the routes by domain'], ['path', null, InputOption::VALUE_OPTIONAL, 'Only show routes matching the given path pattern'], ['except-path', null, InputOption::VALUE_OPTIONAL, 'Do not display the routes matching the given path pattern'], ['reverse', 'r', InputOption::VALUE_NONE, 'Reverse the ordering of the routes'], ['sort', null, InputOption::VALUE_OPTIONAL, 'The column (domain, method, uri, name, action, middleware) to sort by', 'uri'], ['except-vendor', null, InputOption::VALUE_NONE, 'Do not display routes defined by vendor packages'], ['only-vendor', null, InputOption::VALUE_NONE, 'Only display routes defined by vendor packages'], ]; } } src/Illuminate/Foundation/Console/ClearCompiledCommand.php 0000644 00000001664 15107327037 0017675 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'clear-compiled')] class ClearCompiledCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'clear-compiled'; /** * The console command description. * * @var string */ protected $description = 'Remove the compiled class file'; /** * Execute the console command. * * @return void */ public function handle() { if (is_file($servicesPath = $this->laravel->getCachedServicesPath())) { @unlink($servicesPath); } if (is_file($packagesPath = $this->laravel->getCachedPackagesPath())) { @unlink($packagesPath); } $this->components->info('Compiled services and packages files removed successfully.'); } } src/Illuminate/Foundation/Console/ExceptionMakeCommand.php 0000644 00000004146 15107327037 0017724 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:exception')] class ExceptionMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:exception'; /** * The console command description. * * @var string */ protected $description = 'Create a new custom exception class'; /** * The type of class being generated. * * @var string */ protected $type = 'Exception'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { if ($this->option('render')) { return $this->option('report') ? __DIR__.'/stubs/exception-render-report.stub' : __DIR__.'/stubs/exception-render.stub'; } return $this->option('report') ? __DIR__.'/stubs/exception-report.stub' : __DIR__.'/stubs/exception.stub'; } /** * Determine if the class already exists. * * @param string $rawName * @return bool */ protected function alreadyExists($rawName) { return class_exists($this->rootNamespace().'Exceptions\\'.$rawName); } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Exceptions'; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the exception already exists'], ['render', null, InputOption::VALUE_NONE, 'Create the exception with an empty render method'], ['report', null, InputOption::VALUE_NONE, 'Create the exception with an empty report method'], ]; } } src/Illuminate/Foundation/Console/ViewMakeCommand.php 0000644 00000013131 15107327037 0016672 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Concerns\CreatesMatchingTest; use Illuminate\Console\GeneratorCommand; use Illuminate\Foundation\Inspiring; use Illuminate\Support\Facades\File; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:view')] class ViewMakeCommand extends GeneratorCommand { use CreatesMatchingTest; /** * The console command description. * * @var string */ protected $description = 'Create a new view'; /** * The name and signature of the console command. * * @var string */ protected $name = 'make:view'; /** * The type of file being generated. * * @var string */ protected $type = 'View'; /** * Build the class with the given name. * * @param string $name * @return string * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ protected function buildClass($name) { $contents = parent::buildClass($name); return str_replace( '{{ quote }}', Inspiring::quotes()->random(), $contents, ); } /** * Get the destination view path. * * @param string $name * @return string */ protected function getPath($name) { return $this->viewPath( $this->getNameInput().'.'.$this->option('extension'), ); } /** * Get the desired view name from the input. * * @return string */ protected function getNameInput() { $name = trim($this->argument('name')); $name = str_replace(['\\', '.'], '/', $this->argument('name')); return $name; } /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->resolveStubPath( '/stubs/view.stub', ); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the destination test case path. * * @return string */ protected function getTestPath() { return base_path( Str::of($this->testClassFullyQualifiedName()) ->replace('\\', '/') ->replaceFirst('Tests/Feature', 'tests/Feature') ->append('Test.php') ->value() ); } /** * Create the matching test case if requested. * * @param string $path */ protected function handleTestCreation($path): bool { if (! $this->option('test') && ! $this->option('pest')) { return false; } $contents = preg_replace( ['/\{{ namespace \}}/', '/\{{ class \}}/', '/\{{ name \}}/'], [$this->testNamespace(), $this->testClassName(), $this->testViewName()], File::get($this->getTestStub()), ); File::ensureDirectoryExists(dirname($this->getTestPath()), 0755, true); return File::put($this->getTestPath(), $contents); } /** * Get the namespace for the test. * * @return string */ protected function testNamespace() { return Str::of($this->testClassFullyQualifiedName()) ->beforeLast('\\') ->value(); } /** * Get the class name for the test. * * @return string */ protected function testClassName() { return Str::of($this->testClassFullyQualifiedName()) ->afterLast('\\') ->append('Test') ->value(); } /** * Get the class fully qualified name for the test. * * @return string */ protected function testClassFullyQualifiedName() { $name = Str::of(Str::lower($this->getNameInput()))->replace('.'.$this->option('extension'), ''); $namespacedName = Str::of( Str::of($name) ->replace('/', ' ') ->explode(' ') ->map(fn ($part) => Str::of($part)->ucfirst()) ->implode('\\') ) ->replace(['-', '_'], ' ') ->explode(' ') ->map(fn ($part) => Str::of($part)->ucfirst()) ->implode(''); return 'Tests\\Feature\\View\\'.$namespacedName; } /** * Get the test stub file for the generator. * * @return string */ protected function getTestStub() { $stubName = 'view.'.($this->option('pest') ? 'pest' : 'test').'.stub'; return file_exists($customPath = $this->laravel->basePath("stubs/$stubName")) ? $customPath : __DIR__.'/stubs/'.$stubName; } /** * Get the view name for the test. * * @return string */ protected function testViewName() { return Str::of($this->getNameInput()) ->replace('/', '.') ->lower() ->value(); } /** * Get the console command arguments. * * @return array */ protected function getOptions() { return [ ['extension', null, InputOption::VALUE_OPTIONAL, 'The extension of the generated view', 'blade.php'], ['force', 'f', InputOption::VALUE_NONE, 'Create the view even if the view already exists'], ]; } } src/Illuminate/Foundation/Console/CliDumper.php 0000644 00000006354 15107327037 0015560 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Foundation\Concerns\ResolvesDumpSource; use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\VarDumper\Caster\ReflectionCaster; use Symfony\Component\VarDumper\Cloner\Data; use Symfony\Component\VarDumper\Cloner\VarCloner; use Symfony\Component\VarDumper\Dumper\CliDumper as BaseCliDumper; use Symfony\Component\VarDumper\VarDumper; class CliDumper extends BaseCliDumper { use ResolvesDumpSource; /** * The base path of the application. * * @var string */ protected $basePath; /** * The output instance. * * @var \Symfony\Component\Console\Output\OutputInterface */ protected $output; /** * The compiled view path for the application. * * @var string */ protected $compiledViewPath; /** * If the dumper is currently dumping. * * @var bool */ protected $dumping = false; /** * Create a new CLI dumper instance. * * @param \Symfony\Component\Console\Output\OutputInterface $output * @param string $basePath * @param string $compiledViewPath * @return void */ public function __construct($output, $basePath, $compiledViewPath) { parent::__construct(); $this->basePath = $basePath; $this->output = $output; $this->compiledViewPath = $compiledViewPath; } /** * Create a new CLI dumper instance and register it as the default dumper. * * @param string $basePath * @param string $compiledViewPath * @return void */ public static function register($basePath, $compiledViewPath) { $cloner = tap(new VarCloner())->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO); $dumper = new static(new ConsoleOutput(), $basePath, $compiledViewPath); VarDumper::setHandler(fn ($value) => $dumper->dumpWithSource($cloner->cloneVar($value))); } /** * Dump a variable with its source file / line. * * @param \Symfony\Component\VarDumper\Cloner\Data $data * @return void */ public function dumpWithSource(Data $data) { if ($this->dumping) { $this->dump($data); return; } $this->dumping = true; $output = (string) $this->dump($data, true); $lines = explode("\n", $output); $lines[array_key_last($lines) - 1] .= $this->getDumpSourceContent(); $this->output->write(implode("\n", $lines)); $this->dumping = false; } /** * Get the dump's source console content. * * @return string */ protected function getDumpSourceContent() { if (is_null($dumpSource = $this->resolveDumpSource())) { return ''; } [$file, $relativeFile, $line] = $dumpSource; $href = $this->resolveSourceHref($file, $line); return sprintf( ' <fg=gray>// <fg=gray%s>%s%s</></>', is_null($href) ? '' : ";href=$href", $relativeFile, is_null($line) ? '' : ":$line" ); } /** * {@inheritDoc} */ protected function supportsColors(): bool { return $this->output->isDecorated(); } } src/Illuminate/Foundation/Console/CastMakeCommand.php 0000644 00000003547 15107327037 0016664 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:cast')] class CastMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:cast'; /** * The console command description. * * @var string */ protected $description = 'Create a new custom Eloquent cast class'; /** * The type of class being generated. * * @var string */ protected $type = 'Cast'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->option('inbound') ? $this->resolveStubPath('/stubs/cast.inbound.stub') : $this->resolveStubPath('/stubs/cast.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Casts'; } /** * Get the console command arguments. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the cast already exists'], ['inbound', null, InputOption::VALUE_NONE, 'Generate an inbound cast class'], ]; } } src/Illuminate/Foundation/Console/MailMakeCommand.php 0000644 00000007334 15107327037 0016652 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Concerns\CreatesMatchingTest; use Illuminate\Console\GeneratorCommand; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:mail')] class MailMakeCommand extends GeneratorCommand { use CreatesMatchingTest; /** * The console command name. * * @var string */ protected $name = 'make:mail'; /** * The console command description. * * @var string */ protected $description = 'Create a new email class'; /** * The type of class being generated. * * @var string */ protected $type = 'Mailable'; /** * Execute the console command. * * @return void */ public function handle() { if (parent::handle() === false && ! $this->option('force')) { return; } if ($this->option('markdown') !== false) { $this->writeMarkdownTemplate(); } } /** * Write the Markdown template for the mailable. * * @return void */ protected function writeMarkdownTemplate() { $path = $this->viewPath( str_replace('.', '/', $this->getView()).'.blade.php' ); if (! $this->files->isDirectory(dirname($path))) { $this->files->makeDirectory(dirname($path), 0755, true); } $this->files->put($path, file_get_contents(__DIR__.'/stubs/markdown.stub')); } /** * Build the class with the given name. * * @param string $name * @return string */ protected function buildClass($name) { $class = str_replace( '{{ subject }}', Str::headline(str_replace($this->getNamespace($name).'\\', '', $name)), parent::buildClass($name) ); if ($this->option('markdown') !== false) { $class = str_replace(['DummyView', '{{ view }}'], $this->getView(), $class); } return $class; } /** * Get the view name. * * @return string */ protected function getView() { $view = $this->option('markdown'); if (! $view) { $name = str_replace('\\', '/', $this->argument('name')); $view = 'mail.'.collect(explode('/', $name)) ->map(fn ($part) => Str::kebab($part)) ->implode('.'); } return $view; } /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->resolveStubPath( $this->option('markdown') !== false ? '/stubs/markdown-mail.stub' : '/stubs/mail.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Mail'; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the mailable already exists'], ['markdown', 'm', InputOption::VALUE_OPTIONAL, 'Create a new Markdown template for the mailable', false], ]; } } src/Illuminate/Foundation/Console/ListenerMakeCommand.php 0000644 00000007356 15107327037 0017561 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Concerns\CreatesMatchingTest; use Illuminate\Console\GeneratorCommand; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use function Laravel\Prompts\suggest; #[AsCommand(name: 'make:listener')] class ListenerMakeCommand extends GeneratorCommand { use CreatesMatchingTest; /** * The console command name. * * @var string */ protected $name = 'make:listener'; /** * The console command description. * * @var string */ protected $description = 'Create a new event listener class'; /** * The type of class being generated. * * @var string */ protected $type = 'Listener'; /** * Build the class with the given name. * * @param string $name * @return string */ protected function buildClass($name) { $event = $this->option('event') ?? ''; if (! Str::startsWith($event, [ $this->laravel->getNamespace(), 'Illuminate', '\\', ])) { $event = $this->laravel->getNamespace().'Events\\'.str_replace('/', '\\', $event); } $stub = str_replace( ['DummyEvent', '{{ event }}'], class_basename($event), parent::buildClass($name) ); return str_replace( ['DummyFullEvent', '{{ eventNamespace }}'], trim($event, '\\'), $stub ); } /** * Get the stub file for the generator. * * @return string */ protected function getStub() { if ($this->option('queued')) { return $this->option('event') ? __DIR__.'/stubs/listener-queued.stub' : __DIR__.'/stubs/listener-queued-duck.stub'; } return $this->option('event') ? __DIR__.'/stubs/listener.stub' : __DIR__.'/stubs/listener-duck.stub'; } /** * Determine if the class already exists. * * @param string $rawName * @return bool */ protected function alreadyExists($rawName) { return class_exists($rawName); } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Listeners'; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['event', 'e', InputOption::VALUE_OPTIONAL, 'The event class being listened for'], ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the listener already exists'], ['queued', null, InputOption::VALUE_NONE, 'Indicates the event listener should be queued'], ]; } /** * Interact further with the user if they were prompted for missing arguments. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) { if ($this->isReservedName($this->getNameInput()) || $this->didReceiveOptions($input)) { return; } $event = suggest( 'What event should be listened for? (Optional)', $this->possibleEvents(), ); if ($event) { $input->setOption('event', $event); } } } src/Illuminate/Foundation/Console/UpCommand.php 0000644 00000002647 15107327037 0015560 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Exception; use Illuminate\Console\Command; use Illuminate\Foundation\Events\MaintenanceModeDisabled; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'up')] class UpCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'up'; /** * The console command description. * * @var string */ protected $description = 'Bring the application out of maintenance mode'; /** * Execute the console command. * * @return int */ public function handle() { try { if (! $this->laravel->maintenanceMode()->active()) { $this->components->info('Application is already up.'); return 0; } $this->laravel->maintenanceMode()->deactivate(); if (is_file(storage_path('framework/maintenance.php'))) { unlink(storage_path('framework/maintenance.php')); } $this->laravel->get('events')->dispatch(new MaintenanceModeDisabled()); $this->components->info('Application is now live.'); } catch (Exception $e) { $this->components->error(sprintf( 'Failed to disable maintenance mode: %s.', $e->getMessage(), )); return 1; } return 0; } } src/Illuminate/Foundation/Console/EnvironmentEncryptCommand.php 0000644 00000006313 15107327037 0021037 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Exception; use Illuminate\Console\Command; use Illuminate\Encryption\Encrypter; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'env:encrypt')] class EnvironmentEncryptCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'env:encrypt {--key= : The encryption key} {--cipher= : The encryption cipher} {--env= : The environment to be encrypted} {--force : Overwrite the existing encrypted environment file}'; /** * The console command description. * * @var string */ protected $description = 'Encrypt an environment file'; /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * Create a new command instance. * * @param \Illuminate\Filesystem\Filesystem $files * @return void */ public function __construct(Filesystem $files) { parent::__construct(); $this->files = $files; } /** * Execute the console command. * * @return void */ public function handle() { $cipher = $this->option('cipher') ?: 'AES-256-CBC'; $key = $this->option('key'); $keyPassed = $key !== null; $environmentFile = $this->option('env') ? base_path('.env').'.'.$this->option('env') : $this->laravel->environmentFilePath(); $encryptedFile = $environmentFile.'.encrypted'; if (! $keyPassed) { $key = Encrypter::generateKey($cipher); } if (! $this->files->exists($environmentFile)) { $this->components->error('Environment file not found.'); return Command::FAILURE; } if ($this->files->exists($encryptedFile) && ! $this->option('force')) { $this->components->error('Encrypted environment file already exists.'); return Command::FAILURE; } try { $encrypter = new Encrypter($this->parseKey($key), $cipher); $this->files->put( $encryptedFile, $encrypter->encrypt($this->files->get($environmentFile)) ); } catch (Exception $e) { $this->components->error($e->getMessage()); return Command::FAILURE; } $this->components->info('Environment successfully encrypted.'); $this->components->twoColumnDetail('Key', $keyPassed ? $key : 'base64:'.base64_encode($key)); $this->components->twoColumnDetail('Cipher', $cipher); $this->components->twoColumnDetail('Encrypted file', $encryptedFile); $this->newLine(); } /** * Parse the encryption key. * * @param string $key * @return string */ protected function parseKey(string $key) { if (Str::startsWith($key, $prefix = 'base64:')) { $key = base64_decode(Str::after($key, $prefix)); } return $key; } } src/Illuminate/Foundation/Console/EnvironmentCommand.php 0000644 00000001326 15107327037 0017471 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'env')] class EnvironmentCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'env'; /** * The console command description. * * @var string */ protected $description = 'Display the current framework environment'; /** * Execute the console command. * * @return void */ public function handle() { $this->components->info(sprintf( 'The application environment is [%s].', $this->laravel['env'], )); } } src/Illuminate/Foundation/Console/RuleMakeCommand.php 0000644 00000004004 15107327037 0016666 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:rule')] class RuleMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:rule'; /** * The console command description. * * @var string */ protected $description = 'Create a new validation rule'; /** * The type of class being generated. * * @var string */ protected $type = 'Rule'; /** * Build the class with the given name. * * @param string $name * @return string * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ protected function buildClass($name) { return str_replace( '{{ ruleType }}', $this->option('implicit') ? 'ImplicitRule' : 'Rule', parent::buildClass($name) ); } /** * Get the stub file for the generator. * * @return string */ protected function getStub() { $stub = $this->option('implicit') ? '/stubs/rule.implicit.stub' : '/stubs/rule.stub'; return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Rules'; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the rule already exists'], ['implicit', 'i', InputOption::VALUE_NONE, 'Generate an implicit rule'], ]; } } src/Illuminate/Foundation/Console/EventClearCommand.php 0000644 00000002303 15107327037 0017211 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'event:clear')] class EventClearCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'event:clear'; /** * The console command description. * * @var string */ protected $description = 'Clear all cached events and listeners'; /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * Create a new config clear command instance. * * @param \Illuminate\Filesystem\Filesystem $files * @return void */ public function __construct(Filesystem $files) { parent::__construct(); $this->files = $files; } /** * Execute the console command. * * @return void * * @throws \RuntimeException */ public function handle() { $this->files->delete($this->laravel->getCachedEventsPath()); $this->components->info('Cached events cleared successfully.'); } } src/Illuminate/Foundation/Console/ScopeMakeCommand.php 0000644 00000003316 15107327037 0017035 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:scope')] class ScopeMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:scope'; /** * The console command description. * * @var string */ protected $description = 'Create a new scope class'; /** * The type of class being generated. * * @var string */ protected $type = 'Scope'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->resolveStubPath('/stubs/scope.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return is_dir(app_path('Models')) ? $rootNamespace.'\\Models\\Scopes' : $rootNamespace.'\Scopes'; } /** * Get the console command arguments. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the scope already exists'], ]; } } src/Illuminate/Foundation/Console/EventGenerateCommand.php 0000644 00000003661 15107327037 0017725 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Foundation\Support\Providers\EventServiceProvider; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'event:generate')] class EventGenerateCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'event:generate'; /** * The console command description. * * @var string */ protected $description = 'Generate the missing events and listeners based on registration'; /** * Execute the console command. * * @return void */ public function handle() { $providers = $this->laravel->getProviders(EventServiceProvider::class); foreach ($providers as $provider) { foreach ($provider->listens() as $event => $listeners) { $this->makeEventAndListeners($event, $listeners); } } $this->components->info('Events and listeners generated successfully.'); } /** * Make the event and listeners for the given event. * * @param string $event * @param array $listeners * @return void */ protected function makeEventAndListeners($event, $listeners) { if (! str_contains($event, '\\')) { return; } $this->callSilent('make:event', ['name' => $event]); $this->makeListeners($event, $listeners); } /** * Make the listeners for the given event. * * @param string $event * @param array $listeners * @return void */ protected function makeListeners($event, $listeners) { foreach ($listeners as $listener) { $listener = preg_replace('/@.+$/', '', $listener); $this->callSilent('make:listener', array_filter( ['name' => $listener, '--event' => $event] )); } } } src/Illuminate/Foundation/Console/ConfigShowCommand.php 0000644 00000005635 15107327037 0017242 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Support\Arr; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'config:show')] class ConfigShowCommand extends Command { /** * The console command signature. * * @var string */ protected $signature = 'config:show {config : The configuration file to show}'; /** * The console command description. * * @var string */ protected $description = 'Display all of the values for a given configuration file'; /** * Execute the console command. * * @return int */ public function handle() { $config = $this->argument('config'); if (! config()->has($config)) { $this->components->error("Configuration file `{$config}` does not exist."); return Command::FAILURE; } $this->newLine(); $this->render($config); $this->newLine(); return Command::SUCCESS; } /** * Render the configuration values. * * @param string $name * @return void */ public function render($name) { $data = config($name); if (! is_array($data)) { $this->title($name, $this->formatValue($data)); return; } $this->title($name); foreach (Arr::dot($data) as $key => $value) { $this->components->twoColumnDetail( $this->formatKey($key), $this->formatValue($value) ); } } /** * Render the title. * * @param string $title * @param string|null $subtitle * @return void */ public function title($title, $subtitle = null) { $this->components->twoColumnDetail( "<fg=green;options=bold>{$title}</>", $subtitle, ); } /** * Format the given configuration key. * * @param string $key * @return string */ protected function formatKey($key) { return preg_replace_callback( '/(.*)\.(.*)$/', fn ($matches) => sprintf( '<fg=gray>%s ⇁</> %s', str_replace('.', ' ⇁ ', $matches[1]), $matches[2] ), $key ); } /** * Format the given configuration value. * * @param mixed $value * @return string */ protected function formatValue($value) { return match (true) { is_bool($value) => sprintf('<fg=#ef8414;options=bold>%s</>', $value ? 'true' : 'false'), is_null($value) => '<fg=#ef8414;options=bold>null</>', is_numeric($value) => "<fg=#ef8414;options=bold>{$value}</>", is_array($value) => '[]', is_object($value) => get_class($value), is_string($value) => $value, default => print_r($value, true), }; } } src/Illuminate/Foundation/Console/TestMakeCommand.php 0000644 00000007335 15107327037 0016710 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use function Laravel\Prompts\select; #[AsCommand(name: 'make:test')] class TestMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:test'; /** * The console command description. * * @var string */ protected $description = 'Create a new test class'; /** * The type of class being generated. * * @var string */ protected $type = 'Test'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { $suffix = $this->option('unit') ? '.unit.stub' : '.stub'; return $this->option('pest') ? $this->resolveStubPath('/stubs/pest'.$suffix) : $this->resolveStubPath('/stubs/test'.$suffix); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the destination class path. * * @param string $name * @return string */ protected function getPath($name) { $name = Str::replaceFirst($this->rootNamespace(), '', $name); return base_path('tests').str_replace('\\', '/', $name).'.php'; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { if ($this->option('unit')) { return $rootNamespace.'\Unit'; } else { return $rootNamespace.'\Feature'; } } /** * Get the root namespace for the class. * * @return string */ protected function rootNamespace() { return 'Tests'; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the test already exists'], ['unit', 'u', InputOption::VALUE_NONE, 'Create a unit test'], ['pest', 'p', InputOption::VALUE_NONE, 'Create a Pest test'], ]; } /** * Interact further with the user if they were prompted for missing arguments. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) { if ($this->isReservedName($this->getNameInput()) || $this->didReceiveOptions($input)) { return; } $type = select('Which type of test would you like?', [ 'feature' => 'Feature (PHPUnit)', 'unit' => 'Unit (PHPUnit)', 'pest-feature' => 'Feature (Pest)', 'pest-unit' => 'Unit (Pest)', ]); match ($type) { 'feature' => null, 'unit' => $input->setOption('unit', true), 'pest-feature' => $input->setOption('pest', true), 'pest-unit' => tap($input)->setOption('pest', true)->setOption('unit', true), }; } } src/Illuminate/Foundation/Console/EnvironmentDecryptCommand.php 0000644 00000007553 15107327037 0021034 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Exception; use Illuminate\Console\Command; use Illuminate\Encryption\Encrypter; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Env; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'env:decrypt')] class EnvironmentDecryptCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'env:decrypt {--key= : The encryption key} {--cipher= : The encryption cipher} {--env= : The environment to be decrypted} {--force : Overwrite the existing environment file} {--path= : Path to write the decrypted file} {--filename= : Filename of the decrypted file}'; /** * The console command description. * * @var string */ protected $description = 'Decrypt an environment file'; /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * Create a new command instance. * * @param \Illuminate\Filesystem\Filesystem $files * @return void */ public function __construct(Filesystem $files) { parent::__construct(); $this->files = $files; } /** * Execute the console command. * * @return void */ public function handle() { $key = $this->option('key') ?: Env::get('LARAVEL_ENV_ENCRYPTION_KEY'); if (! $key) { $this->components->error('A decryption key is required.'); return Command::FAILURE; } $cipher = $this->option('cipher') ?: 'AES-256-CBC'; $key = $this->parseKey($key); $encryptedFile = ($this->option('env') ? base_path('.env').'.'.$this->option('env') : $this->laravel->environmentFilePath()).'.encrypted'; $outputFile = $this->outputFilePath(); if (Str::endsWith($outputFile, '.encrypted')) { $this->components->error('Invalid filename.'); return Command::FAILURE; } if (! $this->files->exists($encryptedFile)) { $this->components->error('Encrypted environment file not found.'); return Command::FAILURE; } if ($this->files->exists($outputFile) && ! $this->option('force')) { $this->components->error('Environment file already exists.'); return Command::FAILURE; } try { $encrypter = new Encrypter($key, $cipher); $this->files->put( $outputFile, $encrypter->decrypt($this->files->get($encryptedFile)) ); } catch (Exception $e) { $this->components->error($e->getMessage()); return Command::FAILURE; } $this->components->info('Environment successfully decrypted.'); $this->components->twoColumnDetail('Decrypted file', $outputFile); $this->newLine(); } /** * Parse the encryption key. * * @param string $key * @return string */ protected function parseKey(string $key) { if (Str::startsWith($key, $prefix = 'base64:')) { $key = base64_decode(Str::after($key, $prefix)); } return $key; } /** * Get the output file path that should be used for the command. * * @return string */ protected function outputFilePath() { $path = Str::finish($this->option('path') ?: base_path(), DIRECTORY_SEPARATOR); $outputFile = $this->option('filename') ?: ('.env'.($this->option('env') ? '.'.$this->option('env') : '')); $outputFile = ltrim($outputFile, DIRECTORY_SEPARATOR); return $path.$outputFile; } } src/Illuminate/Foundation/Console/VendorPublishCommand.php 0000644 00000022246 15107327037 0017755 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Illuminate\Foundation\Events\VendorTagPublished; use Illuminate\Support\Arr; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; use League\Flysystem\Filesystem as Flysystem; use League\Flysystem\Local\LocalFilesystemAdapter as LocalAdapter; use League\Flysystem\MountManager; use League\Flysystem\UnixVisibility\PortableVisibilityConverter; use League\Flysystem\Visibility; use Symfony\Component\Console\Attribute\AsCommand; use function Laravel\Prompts\search; use function Laravel\Prompts\select; #[AsCommand(name: 'vendor:publish')] class VendorPublishCommand extends Command { /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * The provider to publish. * * @var string */ protected $provider = null; /** * The tags to publish. * * @var array */ protected $tags = []; /** * The console command signature. * * @var string */ protected $signature = 'vendor:publish {--existing : Publish and overwrite only the files that have already been published} {--force : Overwrite any existing files} {--all : Publish assets for all service providers without prompt} {--provider= : The service provider that has assets you want to publish} {--tag=* : One or many tags that have assets you want to publish}'; /** * The console command description. * * @var string */ protected $description = 'Publish any publishable assets from vendor packages'; /** * Create a new command instance. * * @param \Illuminate\Filesystem\Filesystem $files * @return void */ public function __construct(Filesystem $files) { parent::__construct(); $this->files = $files; } /** * Execute the console command. * * @return void */ public function handle() { $this->determineWhatShouldBePublished(); foreach ($this->tags ?: [null] as $tag) { $this->publishTag($tag); } } /** * Determine the provider or tag(s) to publish. * * @return void */ protected function determineWhatShouldBePublished() { if ($this->option('all')) { return; } [$this->provider, $this->tags] = [ $this->option('provider'), (array) $this->option('tag'), ]; if (! $this->provider && ! $this->tags) { $this->promptForProviderOrTag(); } } /** * Prompt for which provider or tag to publish. * * @return void */ protected function promptForProviderOrTag() { $choices = $this->publishableChoices(); $choice = windows_os() ? select( "Which provider or tag's files would you like to publish?", $choices, scroll: 15, ) : search( label: "Which provider or tag's files would you like to publish?", placeholder: 'Search...', options: fn ($search) => array_values(array_filter( $choices, fn ($choice) => str_contains(strtolower($choice), strtolower($search)) )), scroll: 15, ); if ($choice == $choices[0] || is_null($choice)) { return; } $this->parseChoice($choice); } /** * The choices available via the prompt. * * @return array */ protected function publishableChoices() { return array_merge( ['All providers and tags'], preg_filter('/^/', '<fg=gray>Provider:</> ', Arr::sort(ServiceProvider::publishableProviders())), preg_filter('/^/', '<fg=gray>Tag:</> ', Arr::sort(ServiceProvider::publishableGroups())) ); } /** * Parse the answer that was given via the prompt. * * @param string $choice * @return void */ protected function parseChoice($choice) { [$type, $value] = explode(': ', strip_tags($choice)); if ($type === 'Provider') { $this->provider = $value; } elseif ($type === 'Tag') { $this->tags = [$value]; } } /** * Publishes the assets for a tag. * * @param string $tag * @return mixed */ protected function publishTag($tag) { $pathsToPublish = $this->pathsToPublish($tag); if ($publishing = count($pathsToPublish) > 0) { $this->components->info(sprintf( 'Publishing %sassets', $tag ? "[$tag] " : '', )); } foreach ($pathsToPublish as $from => $to) { $this->publishItem($from, $to); } if ($publishing === false) { $this->components->info('No publishable resources for tag ['.$tag.'].'); } else { $this->laravel['events']->dispatch(new VendorTagPublished($tag, $pathsToPublish)); $this->newLine(); } } /** * Get all of the paths to publish. * * @param string $tag * @return array */ protected function pathsToPublish($tag) { return ServiceProvider::pathsToPublish( $this->provider, $tag ); } /** * Publish the given item from and to the given location. * * @param string $from * @param string $to * @return void */ protected function publishItem($from, $to) { if ($this->files->isFile($from)) { return $this->publishFile($from, $to); } elseif ($this->files->isDirectory($from)) { return $this->publishDirectory($from, $to); } $this->components->error("Can't locate path: <{$from}>"); } /** * Publish the file to the given path. * * @param string $from * @param string $to * @return void */ protected function publishFile($from, $to) { if ((! $this->option('existing') && (! $this->files->exists($to) || $this->option('force'))) || ($this->option('existing') && $this->files->exists($to))) { $this->createParentDirectory(dirname($to)); $this->files->copy($from, $to); $this->status($from, $to, 'file'); } else { if ($this->option('existing')) { $this->components->twoColumnDetail(sprintf( 'File [%s] does not exist', str_replace(base_path().'/', '', $to), ), '<fg=yellow;options=bold>SKIPPED</>'); } else { $this->components->twoColumnDetail(sprintf( 'File [%s] already exists', str_replace(base_path().'/', '', realpath($to)), ), '<fg=yellow;options=bold>SKIPPED</>'); } } } /** * Publish the directory to the given directory. * * @param string $from * @param string $to * @return void */ protected function publishDirectory($from, $to) { $visibility = PortableVisibilityConverter::fromArray([], Visibility::PUBLIC); $this->moveManagedFiles(new MountManager([ 'from' => new Flysystem(new LocalAdapter($from)), 'to' => new Flysystem(new LocalAdapter($to, $visibility)), ])); $this->status($from, $to, 'directory'); } /** * Move all the files in the given MountManager. * * @param \League\Flysystem\MountManager $manager * @return void */ protected function moveManagedFiles($manager) { foreach ($manager->listContents('from://', true) as $file) { $path = Str::after($file['path'], 'from://'); if ( $file['type'] === 'file' && ( (! $this->option('existing') && (! $manager->fileExists('to://'.$path) || $this->option('force'))) || ($this->option('existing') && $manager->fileExists('to://'.$path)) ) ) { $manager->write('to://'.$path, $manager->read($file['path'])); } } } /** * Create the directory to house the published files if needed. * * @param string $directory * @return void */ protected function createParentDirectory($directory) { if (! $this->files->isDirectory($directory)) { $this->files->makeDirectory($directory, 0755, true); } } /** * Write a status message to the console. * * @param string $from * @param string $to * @param string $type * @return void */ protected function status($from, $to, $type) { $from = str_replace(base_path().'/', '', realpath($from)); $to = str_replace(base_path().'/', '', realpath($to)); $this->components->task(sprintf( 'Copying %s [%s] to [%s]', $type, $from, $to, )); } } src/Illuminate/Foundation/Console/ObserverMakeCommand.php 0000644 00000010654 15107327037 0017556 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; use InvalidArgumentException; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use function Laravel\Prompts\suggest; #[AsCommand(name: 'make:observer')] class ObserverMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:observer'; /** * The console command description. * * @var string */ protected $description = 'Create a new observer class'; /** * The type of class being generated. * * @var string */ protected $type = 'Observer'; /** * Build the class with the given name. * * @param string $name * @return string */ protected function buildClass($name) { $stub = parent::buildClass($name); $model = $this->option('model'); return $model ? $this->replaceModel($stub, $model) : $stub; } /** * Replace the model for the given stub. * * @param string $stub * @param string $model * @return string */ protected function replaceModel($stub, $model) { $modelClass = $this->parseModel($model); $replace = [ 'DummyFullModelClass' => $modelClass, '{{ namespacedModel }}' => $modelClass, '{{namespacedModel}}' => $modelClass, 'DummyModelClass' => class_basename($modelClass), '{{ model }}' => class_basename($modelClass), '{{model}}' => class_basename($modelClass), 'DummyModelVariable' => lcfirst(class_basename($modelClass)), '{{ modelVariable }}' => lcfirst(class_basename($modelClass)), '{{modelVariable}}' => lcfirst(class_basename($modelClass)), ]; return str_replace( array_keys($replace), array_values($replace), $stub ); } /** * Get the fully-qualified model class name. * * @param string $model * @return string * * @throws \InvalidArgumentException */ protected function parseModel($model) { if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) { throw new InvalidArgumentException('Model name contains invalid characters.'); } return $this->qualifyModel($model); } /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->option('model') ? $this->resolveStubPath('/stubs/observer.stub') : $this->resolveStubPath('/stubs/observer.plain.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Observers'; } /** * Get the console command arguments. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the observer already exists'], ['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the observer applies to'], ]; } /** * Interact further with the user if they were prompted for missing arguments. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) { if ($this->isReservedName($this->getNameInput()) || $this->didReceiveOptions($input)) { return; } $model = suggest( 'What model should this observer apply to? (Optional)', $this->possibleModels(), ); if ($model) { $input->setOption('model', $model); } } } src/Illuminate/Foundation/Console/EventCacheCommand.php 0000644 00000002733 15107327037 0017175 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Foundation\Support\Providers\EventServiceProvider; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'event:cache')] class EventCacheCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'event:cache'; /** * The console command description. * * @var string */ protected $description = "Discover and cache the application's events and listeners"; /** * Execute the console command. * * @return mixed */ public function handle() { $this->callSilent('event:clear'); file_put_contents( $this->laravel->getCachedEventsPath(), '<?php return '.var_export($this->getEvents(), true).';' ); $this->components->info('Events cached successfully.'); } /** * Get all of the events and listeners configured for the application. * * @return array */ protected function getEvents() { $events = []; foreach ($this->laravel->getProviders(EventServiceProvider::class) as $provider) { $providerEvents = array_merge_recursive($provider->shouldDiscoverEvents() ? $provider->discoverEvents() : [], $provider->listens()); $events[get_class($provider)] = $providerEvents; } return $events; } } src/Illuminate/Foundation/Console/OptimizeClearCommand.php 0000644 00000002333 15107327037 0017733 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'optimize:clear')] class OptimizeClearCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'optimize:clear'; /** * The console command description. * * @var string */ protected $description = 'Remove the cached bootstrap files'; /** * Execute the console command. * * @return void */ public function handle() { $this->components->info('Clearing cached bootstrap files.'); collect([ 'events' => fn () => $this->callSilent('event:clear') == 0, 'views' => fn () => $this->callSilent('view:clear') == 0, 'cache' => fn () => $this->callSilent('cache:clear') == 0, 'route' => fn () => $this->callSilent('route:clear') == 0, 'config' => fn () => $this->callSilent('config:clear') == 0, 'compiled' => fn () => $this->callSilent('clear-compiled') == 0, ])->each(fn ($task, $description) => $this->components->task($description, $task)); $this->newLine(); } } src/Illuminate/Foundation/Console/StorageLinkCommand.php 0000644 00000004072 15107327037 0017410 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'storage:link')] class StorageLinkCommand extends Command { /** * The console command signature. * * @var string */ protected $signature = 'storage:link {--relative : Create the symbolic link using relative paths} {--force : Recreate existing symbolic links}'; /** * The console command description. * * @var string */ protected $description = 'Create the symbolic links configured for the application'; /** * Execute the console command. * * @return void */ public function handle() { $relative = $this->option('relative'); foreach ($this->links() as $link => $target) { if (file_exists($link) && ! $this->isRemovableSymlink($link, $this->option('force'))) { $this->components->error("The [$link] link already exists."); continue; } if (is_link($link)) { $this->laravel->make('files')->delete($link); } if ($relative) { $this->laravel->make('files')->relativeLink($target, $link); } else { $this->laravel->make('files')->link($target, $link); } $this->components->info("The [$link] link has been connected to [$target]."); } } /** * Get the symbolic links that are configured for the application. * * @return array */ protected function links() { return $this->laravel['config']['filesystems.links'] ?? [public_path('storage') => storage_path('app/public')]; } /** * Determine if the provided path is a symlink that can be removed. * * @param string $link * @param bool $force * @return bool */ protected function isRemovableSymlink(string $link, bool $force): bool { return is_link($link) && $force; } } src/Illuminate/Foundation/Console/ModelMakeCommand.php 0000644 00000017172 15107327037 0017031 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Concerns\CreatesMatchingTest; use Illuminate\Console\GeneratorCommand; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use function Laravel\Prompts\multiselect; #[AsCommand(name: 'make:model')] class ModelMakeCommand extends GeneratorCommand { use CreatesMatchingTest; /** * The console command name. * * @var string */ protected $name = 'make:model'; /** * The console command description. * * @var string */ protected $description = 'Create a new Eloquent model class'; /** * The type of class being generated. * * @var string */ protected $type = 'Model'; /** * Execute the console command. * * @return void */ public function handle() { if (parent::handle() === false && ! $this->option('force')) { return false; } if ($this->option('all')) { $this->input->setOption('factory', true); $this->input->setOption('seed', true); $this->input->setOption('migration', true); $this->input->setOption('controller', true); $this->input->setOption('policy', true); $this->input->setOption('resource', true); } if ($this->option('factory')) { $this->createFactory(); } if ($this->option('migration')) { $this->createMigration(); } if ($this->option('seed')) { $this->createSeeder(); } if ($this->option('controller') || $this->option('resource') || $this->option('api')) { $this->createController(); } if ($this->option('policy')) { $this->createPolicy(); } } /** * Create a model factory for the model. * * @return void */ protected function createFactory() { $factory = Str::studly($this->argument('name')); $this->call('make:factory', [ 'name' => "{$factory}Factory", '--model' => $this->qualifyClass($this->getNameInput()), ]); } /** * Create a migration file for the model. * * @return void */ protected function createMigration() { $table = Str::snake(Str::pluralStudly(class_basename($this->argument('name')))); if ($this->option('pivot')) { $table = Str::singular($table); } $this->call('make:migration', [ 'name' => "create_{$table}_table", '--create' => $table, '--fullpath' => true, ]); } /** * Create a seeder file for the model. * * @return void */ protected function createSeeder() { $seeder = Str::studly(class_basename($this->argument('name'))); $this->call('make:seeder', [ 'name' => "{$seeder}Seeder", ]); } /** * Create a controller for the model. * * @return void */ protected function createController() { $controller = Str::studly(class_basename($this->argument('name'))); $modelName = $this->qualifyClass($this->getNameInput()); $this->call('make:controller', array_filter([ 'name' => "{$controller}Controller", '--model' => $this->option('resource') || $this->option('api') ? $modelName : null, '--api' => $this->option('api'), '--requests' => $this->option('requests') || $this->option('all'), '--test' => $this->option('test'), '--pest' => $this->option('pest'), ])); } /** * Create a policy file for the model. * * @return void */ protected function createPolicy() { $policy = Str::studly(class_basename($this->argument('name'))); $this->call('make:policy', [ 'name' => "{$policy}Policy", '--model' => $this->qualifyClass($this->getNameInput()), ]); } /** * Get the stub file for the generator. * * @return string */ protected function getStub() { if ($this->option('pivot')) { return $this->resolveStubPath('/stubs/model.pivot.stub'); } if ($this->option('morph-pivot')) { return $this->resolveStubPath('/stubs/model.morph-pivot.stub'); } return $this->resolveStubPath('/stubs/model.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return is_dir(app_path('Models')) ? $rootNamespace.'\\Models' : $rootNamespace; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, seeder, factory, policy, resource controller, and form request classes for the model'], ['controller', 'c', InputOption::VALUE_NONE, 'Create a new controller for the model'], ['factory', 'f', InputOption::VALUE_NONE, 'Create a new factory for the model'], ['force', null, InputOption::VALUE_NONE, 'Create the class even if the model already exists'], ['migration', 'm', InputOption::VALUE_NONE, 'Create a new migration file for the model'], ['morph-pivot', null, InputOption::VALUE_NONE, 'Indicates if the generated model should be a custom polymorphic intermediate table model'], ['policy', null, InputOption::VALUE_NONE, 'Create a new policy for the model'], ['seed', 's', InputOption::VALUE_NONE, 'Create a new seeder for the model'], ['pivot', 'p', InputOption::VALUE_NONE, 'Indicates if the generated model should be a custom intermediate table model'], ['resource', 'r', InputOption::VALUE_NONE, 'Indicates if the generated controller should be a resource controller'], ['api', null, InputOption::VALUE_NONE, 'Indicates if the generated controller should be an API resource controller'], ['requests', 'R', InputOption::VALUE_NONE, 'Create new form request classes and use them in the resource controller'], ]; } /** * Interact further with the user if they were prompted for missing arguments. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) { if ($this->isReservedName($this->getNameInput()) || $this->didReceiveOptions($input)) { return; } collect(multiselect('Would you like any of the following?', [ 'seed' => 'Database Seeder', 'factory' => 'Factory', 'requests' => 'Form Requests', 'migration' => 'Migration', 'policy' => 'Policy', 'resource' => 'Resource Controller', ]))->each(fn ($option) => $input->setOption($option, true)); } } src/Illuminate/Foundation/Console/RequestMakeCommand.php 0000644 00000003277 15107327037 0017422 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:request')] class RequestMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:request'; /** * The console command description. * * @var string */ protected $description = 'Create a new form request class'; /** * The type of class being generated. * * @var string */ protected $type = 'Request'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->resolveStubPath('/stubs/request.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Http\Requests'; } /** * Get the console command arguments. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the request already exists'], ]; } } src/Illuminate/Foundation/Console/KeyGenerateCommand.php 0000644 00000006243 15107327037 0017373 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Console\ConfirmableTrait; use Illuminate\Encryption\Encrypter; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'key:generate')] class KeyGenerateCommand extends Command { use ConfirmableTrait; /** * The name and signature of the console command. * * @var string */ protected $signature = 'key:generate {--show : Display the key instead of modifying files} {--force : Force the operation to run when in production}'; /** * The console command description. * * @var string */ protected $description = 'Set the application key'; /** * Execute the console command. * * @return void */ public function handle() { $key = $this->generateRandomKey(); if ($this->option('show')) { return $this->line('<comment>'.$key.'</comment>'); } // Next, we will replace the application key in the environment file so it is // automatically setup for this developer. This key gets generated using a // secure random byte generator and is later base64 encoded for storage. if (! $this->setKeyInEnvironmentFile($key)) { return; } $this->laravel['config']['app.key'] = $key; $this->components->info('Application key set successfully.'); } /** * Generate a random key for the application. * * @return string */ protected function generateRandomKey() { return 'base64:'.base64_encode( Encrypter::generateKey($this->laravel['config']['app.cipher']) ); } /** * Set the application key in the environment file. * * @param string $key * @return bool */ protected function setKeyInEnvironmentFile($key) { $currentKey = $this->laravel['config']['app.key']; if (strlen($currentKey) !== 0 && (! $this->confirmToProceed())) { return false; } if (! $this->writeNewEnvironmentFileWith($key)) { return false; } return true; } /** * Write a new environment file with the given key. * * @param string $key * @return bool */ protected function writeNewEnvironmentFileWith($key) { $replaced = preg_replace( $this->keyReplacementPattern(), 'APP_KEY='.$key, $input = file_get_contents($this->laravel->environmentFilePath()) ); if ($replaced === $input || $replaced === null) { $this->error('Unable to set application key. No APP_KEY variable was found in the .env file.'); return false; } file_put_contents($this->laravel->environmentFilePath(), $replaced); return true; } /** * Get a regex pattern that will match env APP_KEY with any random key. * * @return string */ protected function keyReplacementPattern() { $escaped = preg_quote('='.$this->laravel['config']['app.key'], '/'); return "/^APP_KEY{$escaped}/m"; } } src/Illuminate/Foundation/Console/PolicyMakeCommand.php 0000644 00000014006 15107327037 0017221 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; use Illuminate\Support\Str; use LogicException; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use function Laravel\Prompts\suggest; #[AsCommand(name: 'make:policy')] class PolicyMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:policy'; /** * The console command description. * * @var string */ protected $description = 'Create a new policy class'; /** * The type of class being generated. * * @var string */ protected $type = 'Policy'; /** * Build the class with the given name. * * @param string $name * @return string */ protected function buildClass($name) { $stub = $this->replaceUserNamespace( parent::buildClass($name) ); $model = $this->option('model'); return $model ? $this->replaceModel($stub, $model) : $stub; } /** * Replace the User model namespace. * * @param string $stub * @return string */ protected function replaceUserNamespace($stub) { $model = $this->userProviderModel(); if (! $model) { return $stub; } return str_replace( $this->rootNamespace().'User', $model, $stub ); } /** * Get the model for the guard's user provider. * * @return string|null * * @throws \LogicException */ protected function userProviderModel() { $config = $this->laravel['config']; $guard = $this->option('guard') ?: $config->get('auth.defaults.guard'); if (is_null($guardProvider = $config->get('auth.guards.'.$guard.'.provider'))) { throw new LogicException('The ['.$guard.'] guard is not defined in your "auth" configuration file.'); } if (! $config->get('auth.providers.'.$guardProvider.'.model')) { return 'App\\Models\\User'; } return $config->get( 'auth.providers.'.$guardProvider.'.model' ); } /** * Replace the model for the given stub. * * @param string $stub * @param string $model * @return string */ protected function replaceModel($stub, $model) { $model = str_replace('/', '\\', $model); if (str_starts_with($model, '\\')) { $namespacedModel = trim($model, '\\'); } else { $namespacedModel = $this->qualifyModel($model); } $model = class_basename(trim($model, '\\')); $dummyUser = class_basename($this->userProviderModel()); $dummyModel = Str::camel($model) === 'user' ? 'model' : $model; $replace = [ 'NamespacedDummyModel' => $namespacedModel, '{{ namespacedModel }}' => $namespacedModel, '{{namespacedModel}}' => $namespacedModel, 'DummyModel' => $model, '{{ model }}' => $model, '{{model}}' => $model, 'dummyModel' => Str::camel($dummyModel), '{{ modelVariable }}' => Str::camel($dummyModel), '{{modelVariable}}' => Str::camel($dummyModel), 'DummyUser' => $dummyUser, '{{ user }}' => $dummyUser, '{{user}}' => $dummyUser, '$user' => '$'.Str::camel($dummyUser), ]; $stub = str_replace( array_keys($replace), array_values($replace), $stub ); return preg_replace( vsprintf('/use %s;[\r\n]+use %s;/', [ preg_quote($namespacedModel, '/'), preg_quote($namespacedModel, '/'), ]), "use {$namespacedModel};", $stub ); } /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->option('model') ? $this->resolveStubPath('/stubs/policy.stub') : $this->resolveStubPath('/stubs/policy.plain.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Policies'; } /** * Get the console command arguments. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the policy already exists'], ['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the policy applies to'], ['guard', 'g', InputOption::VALUE_OPTIONAL, 'The guard that the policy relies on'], ]; } /** * Interact further with the user if they were prompted for missing arguments. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) { if ($this->isReservedName($this->getNameInput()) || $this->didReceiveOptions($input)) { return; } $model = suggest( 'What model should this policy apply to? (Optional)', $this->possibleModels(), ); if ($model) { $input->setOption('model', $model); } } } src/Illuminate/Foundation/Console/ServeCommand.php 0000644 00000023045 15107327037 0016253 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Support\Carbon; use Illuminate\Support\Env; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Process\PhpExecutableFinder; use Symfony\Component\Process\Process; use function Termwind\terminal; #[AsCommand(name: 'serve')] class ServeCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'serve'; /** * The console command description. * * @var string */ protected $description = 'Serve the application on the PHP development server'; /** * The current port offset. * * @var int */ protected $portOffset = 0; /** * The list of requests being handled and their start time. * * @var array<int, \Illuminate\Support\Carbon> */ protected $requestsPool; /** * Indicates if the "Server running on..." output message has been displayed. * * @var bool */ protected $serverRunningHasBeenDisplayed = false; /** * The environment variables that should be passed from host machine to the PHP server process. * * @var string[] */ public static $passthroughVariables = [ 'APP_ENV', 'IGNITION_LOCAL_SITES_PATH', 'LARAVEL_SAIL', 'PATH', 'PHP_CLI_SERVER_WORKERS', 'PHP_IDE_CONFIG', 'SYSTEMROOT', 'XDEBUG_CONFIG', 'XDEBUG_MODE', 'XDEBUG_SESSION', ]; /** * Execute the console command. * * @return int * * @throws \Exception */ public function handle() { $environmentFile = $this->option('env') ? base_path('.env').'.'.$this->option('env') : base_path('.env'); $hasEnvironment = file_exists($environmentFile); $environmentLastModified = $hasEnvironment ? filemtime($environmentFile) : now()->addDays(30)->getTimestamp(); $process = $this->startProcess($hasEnvironment); while ($process->isRunning()) { if ($hasEnvironment) { clearstatcache(false, $environmentFile); } if (! $this->option('no-reload') && $hasEnvironment && filemtime($environmentFile) > $environmentLastModified) { $environmentLastModified = filemtime($environmentFile); $this->newLine(); $this->components->info('Environment modified. Restarting server...'); $process->stop(5); $this->serverRunningHasBeenDisplayed = false; $process = $this->startProcess($hasEnvironment); } usleep(500 * 1000); } $status = $process->getExitCode(); if ($status && $this->canTryAnotherPort()) { $this->portOffset += 1; return $this->handle(); } return $status; } /** * Start a new server process. * * @param bool $hasEnvironment * @return \Symfony\Component\Process\Process */ protected function startProcess($hasEnvironment) { $process = new Process($this->serverCommand(), public_path(), collect($_ENV)->mapWithKeys(function ($value, $key) use ($hasEnvironment) { if ($this->option('no-reload') || ! $hasEnvironment) { return [$key => $value]; } return in_array($key, static::$passthroughVariables) ? [$key => $value] : [$key => false]; })->all()); $process->start($this->handleProcessOutput()); return $process; } /** * Get the full server command. * * @return array */ protected function serverCommand() { $server = file_exists(base_path('server.php')) ? base_path('server.php') : __DIR__.'/../resources/server.php'; return [ (new PhpExecutableFinder)->find(false), '-S', $this->host().':'.$this->port(), $server, ]; } /** * Get the host for the command. * * @return string */ protected function host() { [$host] = $this->getHostAndPort(); return $host; } /** * Get the port for the command. * * @return string */ protected function port() { $port = $this->input->getOption('port'); if (is_null($port)) { [, $port] = $this->getHostAndPort(); } $port = $port ?: 8000; return $port + $this->portOffset; } /** * Get the host and port from the host option string. * * @return array */ protected function getHostAndPort() { if (preg_match('/(\[.*\]):?([0-9]+)?/', $this->input->getOption('host'), $matches) !== false) { return [ $matches[1] ?? $this->input->getOption('host'), $matches[2] ?? null, ]; } $hostParts = explode(':', $this->input->getOption('host')); return [ $hostParts[0], $hostParts[1] ?? null, ]; } /** * Check if the command has reached its maximum number of port tries. * * @return bool */ protected function canTryAnotherPort() { return is_null($this->input->getOption('port')) && ($this->input->getOption('tries') > $this->portOffset); } /** * Returns a "callable" to handle the process output. * * @return callable(string, string): void */ protected function handleProcessOutput() { return fn ($type, $buffer) => str($buffer)->explode("\n")->each(function ($line) { if (str($line)->contains('Development Server (http')) { if ($this->serverRunningHasBeenDisplayed) { return; } $this->components->info("Server running on [http://{$this->host()}:{$this->port()}]."); $this->comment(' <fg=yellow;options=bold>Press Ctrl+C to stop the server</>'); $this->newLine(); $this->serverRunningHasBeenDisplayed = true; } elseif (str($line)->contains(' Accepted')) { $requestPort = $this->getRequestPortFromLine($line); $this->requestsPool[$requestPort] = [ $this->getDateFromLine($line), false, ]; } elseif (str($line)->contains([' [200]: GET '])) { $requestPort = $this->getRequestPortFromLine($line); $this->requestsPool[$requestPort][1] = trim(explode('[200]: GET', $line)[1]); } elseif (str($line)->contains(' Closing')) { $requestPort = $this->getRequestPortFromLine($line); if (empty($this->requestsPool[$requestPort])) { return; } [$startDate, $file] = $this->requestsPool[$requestPort]; $formattedStartedAt = $startDate->format('Y-m-d H:i:s'); unset($this->requestsPool[$requestPort]); [$date, $time] = explode(' ', $formattedStartedAt); $this->output->write(" <fg=gray>$date</> $time"); $runTime = $this->getDateFromLine($line)->diffInSeconds($startDate); if ($file) { $this->output->write($file = " $file"); } $dots = max(terminal()->width() - mb_strlen($formattedStartedAt) - mb_strlen($file) - mb_strlen($runTime) - 9, 0); $this->output->write(' '.str_repeat('<fg=gray>.</>', $dots)); $this->output->writeln(" <fg=gray>~ {$runTime}s</>"); } elseif (str($line)->contains(['Closed without sending a request'])) { // ... } elseif (! empty($line)) { $warning = explode('] ', $line); $this->components->warn(count($warning) > 1 ? $warning[1] : $warning[0]); } }); } /** * Get the date from the given PHP server output. * * @param string $line * @return \Illuminate\Support\Carbon */ protected function getDateFromLine($line) { $regex = env('PHP_CLI_SERVER_WORKERS', 1) > 1 ? '/^\[\d+]\s\[([a-zA-Z0-9: ]+)\]/' : '/^\[([^\]]+)\]/'; $line = str_replace(' ', ' ', $line); preg_match($regex, $line, $matches); return Carbon::createFromFormat('D M d H:i:s Y', $matches[1]); } /** * Get the request port from the given PHP server output. * * @param string $line * @return int */ protected function getRequestPortFromLine($line) { preg_match('/:(\d+)\s(?:(?:\w+$)|(?:\[.*))/', $line, $matches); return (int) $matches[1]; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['host', null, InputOption::VALUE_OPTIONAL, 'The host address to serve the application on', Env::get('SERVER_HOST', '127.0.0.1')], ['port', null, InputOption::VALUE_OPTIONAL, 'The port to serve the application on', Env::get('SERVER_PORT')], ['tries', null, InputOption::VALUE_OPTIONAL, 'The max number of ports to attempt to serve from', 10], ['no-reload', null, InputOption::VALUE_NONE, 'Do not reload the development server on .env file changes'], ]; } } src/Illuminate/Foundation/Console/ConfigCacheCommand.php 0000644 00000004167 15107327037 0017324 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract; use Illuminate\Filesystem\Filesystem; use LogicException; use Symfony\Component\Console\Attribute\AsCommand; use Throwable; #[AsCommand(name: 'config:cache')] class ConfigCacheCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'config:cache'; /** * The console command description. * * @var string */ protected $description = 'Create a cache file for faster configuration loading'; /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * Create a new config cache command instance. * * @param \Illuminate\Filesystem\Filesystem $files * @return void */ public function __construct(Filesystem $files) { parent::__construct(); $this->files = $files; } /** * Execute the console command. * * @return void * * @throws \LogicException */ public function handle() { $this->callSilent('config:clear'); $config = $this->getFreshConfiguration(); $configPath = $this->laravel->getCachedConfigPath(); $this->files->put( $configPath, '<?php return '.var_export($config, true).';'.PHP_EOL ); try { require $configPath; } catch (Throwable $e) { $this->files->delete($configPath); throw new LogicException('Your configuration files are not serializable.', 0, $e); } $this->components->info('Configuration cached successfully.'); } /** * Boot a fresh copy of the application configuration. * * @return array */ protected function getFreshConfiguration() { $app = require $this->laravel->bootstrapPath('app.php'); $app->useStoragePath($this->laravel->storagePath()); $app->make(ConsoleKernelContract::class)->bootstrap(); return $app['config']->all(); } } src/Illuminate/Foundation/Console/ConfigClearCommand.php 0000644 00000002242 15107327037 0017337 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'config:clear')] class ConfigClearCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'config:clear'; /** * The console command description. * * @var string */ protected $description = 'Remove the configuration cache file'; /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * Create a new config clear command instance. * * @param \Illuminate\Filesystem\Filesystem $files * @return void */ public function __construct(Filesystem $files) { parent::__construct(); $this->files = $files; } /** * Execute the console command. * * @return void */ public function handle() { $this->files->delete($this->laravel->getCachedConfigPath()); $this->components->info('Configuration cache cleared successfully.'); } } src/Illuminate/Foundation/Console/LangPublishCommand.php 0000644 00000003513 15107327037 0017375 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'lang:publish')] class LangPublishCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'lang:publish {--existing : Publish and overwrite only the files that have already been published} {--force : Overwrite any existing files}'; /** * The console command description. * * @var string */ protected $description = 'Publish all language files that are available for customization'; /** * Execute the console command. * * @return void */ public function handle() { if (! is_dir($langPath = $this->laravel->basePath('lang/en'))) { (new Filesystem)->makeDirectory($langPath, recursive: true); } $stubs = [ realpath(__DIR__.'/../../Translation/lang/en/auth.php') => 'auth.php', realpath(__DIR__.'/../../Translation/lang/en/pagination.php') => 'pagination.php', realpath(__DIR__.'/../../Translation/lang/en/passwords.php') => 'passwords.php', realpath(__DIR__.'/../../Translation/lang/en/validation.php') => 'validation.php', ]; foreach ($stubs as $from => $to) { $to = $langPath.DIRECTORY_SEPARATOR.ltrim($to, DIRECTORY_SEPARATOR); if ((! $this->option('existing') && (! file_exists($to) || $this->option('force'))) || ($this->option('existing') && file_exists($to))) { file_put_contents($to, file_get_contents($from)); } } $this->components->info('Language files published successfully.'); } } src/Illuminate/Foundation/Console/ResourceMakeCommand.php 0000644 00000004624 15107327037 0017556 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:resource')] class ResourceMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:resource'; /** * The console command description. * * @var string */ protected $description = 'Create a new resource'; /** * The type of class being generated. * * @var string */ protected $type = 'Resource'; /** * Execute the console command. * * @return void */ public function handle() { if ($this->collection()) { $this->type = 'Resource collection'; } parent::handle(); } /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->collection() ? $this->resolveStubPath('/stubs/resource-collection.stub') : $this->resolveStubPath('/stubs/resource.stub'); } /** * Determine if the command is generating a resource collection. * * @return bool */ protected function collection() { return $this->option('collection') || str_ends_with($this->argument('name'), 'Collection'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Http\Resources'; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the resource already exists'], ['collection', 'c', InputOption::VALUE_NONE, 'Create a resource collection'], ]; } } src/Illuminate/Foundation/Console/JobMakeCommand.php 0000644 00000003702 15107327037 0016475 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Concerns\CreatesMatchingTest; use Illuminate\Console\GeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:job')] class JobMakeCommand extends GeneratorCommand { use CreatesMatchingTest; /** * The console command name. * * @var string */ protected $name = 'make:job'; /** * The console command description. * * @var string */ protected $description = 'Create a new job class'; /** * The type of class being generated. * * @var string */ protected $type = 'Job'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->option('sync') ? $this->resolveStubPath('/stubs/job.stub') : $this->resolveStubPath('/stubs/job.queued.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Jobs'; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the job already exists'], ['sync', null, InputOption::VALUE_NONE, 'Indicates that job should be synchronous'], ]; } } src/Illuminate/Foundation/Console/ChannelMakeCommand.php 0000644 00000003252 15107327037 0017333 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:channel')] class ChannelMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:channel'; /** * The console command description. * * @var string */ protected $description = 'Create a new channel class'; /** * The type of class being generated. * * @var string */ protected $type = 'Channel'; /** * Build the class with the given name. * * @param string $name * @return string */ protected function buildClass($name) { return str_replace( ['DummyUser', '{{ userModel }}'], class_basename($this->userProviderModel()), parent::buildClass($name) ); } /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return __DIR__.'/stubs/channel.stub'; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Broadcasting'; } /** * Get the console command arguments. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the channel already exists'], ]; } } src/Illuminate/Foundation/Console/AboutCommand.php 0000644 00000020356 15107327037 0016243 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Support\Composer; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'about')] class AboutCommand extends Command { /** * The console command signature. * * @var string */ protected $signature = 'about {--only= : The section to display} {--json : Output the information as JSON}'; /** * The console command description. * * @var string */ protected $description = 'Display basic information about your application'; /** * The Composer instance. * * @var \Illuminate\Support\Composer */ protected $composer; /** * The data to display. * * @var array */ protected static $data = []; /** * The registered callables that add custom data to the command output. * * @var array */ protected static $customDataResolvers = []; /** * Create a new command instance. * * @param \Illuminate\Support\Composer $composer * @return void */ public function __construct(Composer $composer) { parent::__construct(); $this->composer = $composer; } /** * Execute the console command. * * @return int */ public function handle() { $this->gatherApplicationInformation(); collect(static::$data) ->map(fn ($items) => collect($items) ->map(function ($value) { if (is_array($value)) { return [$value]; } if (is_string($value)) { $value = $this->laravel->make($value); } return collect($this->laravel->call($value)) ->map(fn ($value, $key) => [$key, $value]) ->values() ->all(); })->flatten(1) ) ->sortBy(function ($data, $key) { $index = array_search($key, ['Environment', 'Cache', 'Drivers']); return $index === false ? 99 : $index; }) ->filter(function ($data, $key) { return $this->option('only') ? in_array($this->toSearchKeyword($key), $this->sections()) : true; }) ->pipe(fn ($data) => $this->display($data)); $this->newLine(); return 0; } /** * Display the application information. * * @param \Illuminate\Support\Collection $data * @return void */ protected function display($data) { $this->option('json') ? $this->displayJson($data) : $this->displayDetail($data); } /** * Display the application information as a detail view. * * @param \Illuminate\Support\Collection $data * @return void */ protected function displayDetail($data) { $data->each(function ($data, $section) { $this->newLine(); $this->components->twoColumnDetail(' <fg=green;options=bold>'.$section.'</>'); $data->pipe(fn ($data) => $section !== 'Environment' ? $data->sort() : $data)->each(function ($detail) { [$label, $value] = $detail; $this->components->twoColumnDetail($label, value($value)); }); }); } /** * Display the application information as JSON. * * @param \Illuminate\Support\Collection $data * @return void */ protected function displayJson($data) { $output = $data->flatMap(function ($data, $section) { return [ (string) Str::of($section)->snake() => $data->mapWithKeys(fn ($item, $key) => [ $this->toSearchKeyword($item[0]) => value($item[1]), ]), ]; }); $this->output->writeln(strip_tags(json_encode($output))); } /** * Gather information about the application. * * @return void */ protected function gatherApplicationInformation() { static::addToSection('Environment', fn () => [ 'Application Name' => config('app.name'), 'Laravel Version' => $this->laravel->version(), 'PHP Version' => phpversion(), 'Composer Version' => $this->composer->getVersion() ?? '<fg=yellow;options=bold>-</>', 'Environment' => $this->laravel->environment(), 'Debug Mode' => config('app.debug') ? '<fg=yellow;options=bold>ENABLED</>' : 'OFF', 'URL' => Str::of(config('app.url'))->replace(['http://', 'https://'], ''), 'Maintenance Mode' => $this->laravel->isDownForMaintenance() ? '<fg=yellow;options=bold>ENABLED</>' : 'OFF', ]); static::addToSection('Cache', fn () => [ 'Config' => $this->laravel->configurationIsCached() ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>', 'Events' => $this->laravel->eventsAreCached() ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>', 'Routes' => $this->laravel->routesAreCached() ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>', 'Views' => $this->hasPhpFiles($this->laravel->storagePath('framework/views')) ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>', ]); $logChannel = config('logging.default'); if (config('logging.channels.'.$logChannel.'.driver') === 'stack') { $secondary = collect(config('logging.channels.'.$logChannel.'.channels')) ->implode(', '); $logs = '<fg=yellow;options=bold>'.$logChannel.'</> <fg=gray;options=bold>/</> '.$secondary; } else { $logs = $logChannel; } static::addToSection('Drivers', fn () => array_filter([ 'Broadcasting' => config('broadcasting.default'), 'Cache' => config('cache.default'), 'Database' => config('database.default'), 'Logs' => $logs, 'Mail' => config('mail.default'), 'Octane' => config('octane.server'), 'Queue' => config('queue.default'), 'Scout' => config('scout.driver'), 'Session' => config('session.driver'), ])); collect(static::$customDataResolvers)->each->__invoke(); } /** * Determine whether the given directory has PHP files. * * @param string $path * @return bool */ protected function hasPhpFiles(string $path): bool { return count(glob($path.'/*.php')) > 0; } /** * Add additional data to the output of the "about" command. * * @param string $section * @param callable|string|array $data * @param string|null $value * @return void */ public static function add(string $section, $data, string $value = null) { static::$customDataResolvers[] = fn () => static::addToSection($section, $data, $value); } /** * Add additional data to the output of the "about" command. * * @param string $section * @param callable|string|array $data * @param string|null $value * @return void */ protected static function addToSection(string $section, $data, string $value = null) { if (is_array($data)) { foreach ($data as $key => $value) { self::$data[$section][] = [$key, $value]; } } elseif (is_callable($data) || ($value === null && class_exists($data))) { self::$data[$section][] = $data; } else { self::$data[$section][] = [$data, $value]; } } /** * Get the sections provided to the command. * * @return array */ protected function sections() { return collect(explode(',', $this->option('only') ?? '')) ->filter() ->map(fn ($only) => $this->toSearchKeyword($only)) ->all(); } /** * Format the given string for searching. * * @param string $value * @return string */ protected function toSearchKeyword(string $value) { return (string) Str::of($value)->lower()->snake(); } } src/Illuminate/Foundation/Console/NotificationMakeCommand.php 0000644 00000006261 15107327037 0020414 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Concerns\CreatesMatchingTest; use Illuminate\Console\GeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:notification')] class NotificationMakeCommand extends GeneratorCommand { use CreatesMatchingTest; /** * The console command name. * * @var string */ protected $name = 'make:notification'; /** * The console command description. * * @var string */ protected $description = 'Create a new notification class'; /** * The type of class being generated. * * @var string */ protected $type = 'Notification'; /** * Execute the console command. * * @return void */ public function handle() { if (parent::handle() === false && ! $this->option('force')) { return; } if ($this->option('markdown')) { $this->writeMarkdownTemplate(); } } /** * Write the Markdown template for the mailable. * * @return void */ protected function writeMarkdownTemplate() { $path = $this->viewPath( str_replace('.', '/', $this->option('markdown')).'.blade.php' ); if (! $this->files->isDirectory(dirname($path))) { $this->files->makeDirectory(dirname($path), 0755, true); } $this->files->put($path, file_get_contents(__DIR__.'/stubs/markdown.stub')); } /** * Build the class with the given name. * * @param string $name * @return string */ protected function buildClass($name) { $class = parent::buildClass($name); if ($this->option('markdown')) { $class = str_replace(['DummyView', '{{ view }}'], $this->option('markdown'), $class); } return $class; } /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->option('markdown') ? $this->resolveStubPath('/stubs/markdown-notification.stub') : $this->resolveStubPath('/stubs/notification.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Notifications'; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the notification already exists'], ['markdown', 'm', InputOption::VALUE_OPTIONAL, 'Create a new Markdown template for the notification'], ]; } } src/Illuminate/Foundation/Console/ViewClearCommand.php 0000644 00000003023 15107327037 0017042 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use RuntimeException; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'view:clear')] class ViewClearCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'view:clear'; /** * The console command description. * * @var string */ protected $description = 'Clear all compiled view files'; /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * Create a new config clear command instance. * * @param \Illuminate\Filesystem\Filesystem $files * @return void */ public function __construct(Filesystem $files) { parent::__construct(); $this->files = $files; } /** * Execute the console command. * * @return void * * @throws \RuntimeException */ public function handle() { $path = $this->laravel['config']['view.compiled']; if (! $path) { throw new RuntimeException('View path not found.'); } $this->laravel['view.engine.resolver'] ->resolve('blade') ->forgetCompiledOrNotExpired(); foreach ($this->files->glob("{$path}/*") as $view) { $this->files->delete($view); } $this->components->info('Compiled views cleared successfully.'); } } src/Illuminate/Foundation/Console/EventListCommand.php 0000644 00000013166 15107327037 0017107 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Closure; use Illuminate\Console\Command; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Contracts\Queue\ShouldQueue; use ReflectionFunction; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'event:list')] class EventListCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'event:list {--event= : Filter the events by name}'; /** * The console command description. * * @var string */ protected $description = "List the application's events and listeners"; /** * The events dispatcher resolver callback. * * @var \Closure|null */ protected static $eventsResolver; /** * Execute the console command. * * @return void */ public function handle() { $events = $this->getEvents()->sortKeys(); if ($events->isEmpty()) { $this->components->info("Your application doesn't have any events matching the given criteria."); return; } $this->newLine(); $events->each(function ($listeners, $event) { $this->components->twoColumnDetail($this->appendEventInterfaces($event)); $this->components->bulletList($listeners); }); $this->newLine(); } /** * Get all of the events and listeners configured for the application. * * @return \Illuminate\Support\Collection */ protected function getEvents() { $events = collect($this->getListenersOnDispatcher()); if ($this->filteringByEvent()) { $events = $this->filterEvents($events); } return $events; } /** * Get the event / listeners from the dispatcher object. * * @return array */ protected function getListenersOnDispatcher() { $events = []; foreach ($this->getRawListeners() as $event => $rawListeners) { foreach ($rawListeners as $rawListener) { if (is_string($rawListener)) { $events[$event][] = $this->appendListenerInterfaces($rawListener); } elseif ($rawListener instanceof Closure) { $events[$event][] = $this->stringifyClosure($rawListener); } elseif (is_array($rawListener) && count($rawListener) === 2) { if (is_object($rawListener[0])) { $rawListener[0] = get_class($rawListener[0]); } $events[$event][] = $this->appendListenerInterfaces(implode('@', $rawListener)); } } } return $events; } /** * Add the event implemented interfaces to the output. * * @param string $event * @return string */ protected function appendEventInterfaces($event) { if (! class_exists($event)) { return $event; } $interfaces = class_implements($event); if (in_array(ShouldBroadcast::class, $interfaces)) { $event .= ' <fg=bright-blue>(ShouldBroadcast)</>'; } return $event; } /** * Add the listener implemented interfaces to the output. * * @param string $listener * @return string */ protected function appendListenerInterfaces($listener) { $listener = explode('@', $listener); $interfaces = class_implements($listener[0]); $listener = implode('@', $listener); if (in_array(ShouldQueue::class, $interfaces)) { $listener .= ' <fg=bright-blue>(ShouldQueue)</>'; } return $listener; } /** * Get a displayable string representation of a Closure listener. * * @param \Closure $rawListener * @return string */ protected function stringifyClosure(Closure $rawListener) { $reflection = new ReflectionFunction($rawListener); $path = str_replace([base_path(), DIRECTORY_SEPARATOR], ['', '/'], $reflection->getFileName() ?: ''); return 'Closure at: '.$path.':'.$reflection->getStartLine(); } /** * Filter the given events using the provided event name filter. * * @param \Illuminate\Support\Collection $events * @return \Illuminate\Support\Collection */ protected function filterEvents($events) { if (! $eventName = $this->option('event')) { return $events; } return $events->filter( fn ($listeners, $event) => str_contains($event, $eventName) ); } /** * Determine whether the user is filtering by an event name. * * @return bool */ protected function filteringByEvent() { return ! empty($this->option('event')); } /** * Gets the raw version of event listeners from the event dispatcher. * * @return array */ protected function getRawListeners() { return $this->getEventsDispatcher()->getRawListeners(); } /** * Get the event dispatcher. * * @return \Illuminate\Events\Dispatcher */ public function getEventsDispatcher() { return is_null(self::$eventsResolver) ? $this->getLaravel()->make('events') : call_user_func(self::$eventsResolver); } /** * Set a callback that should be used when resolving the events dispatcher. * * @param \Closure|null $resolver * @return void */ public static function resolveEventsUsing($resolver) { static::$eventsResolver = $resolver; } } src/Illuminate/Foundation/Console/PackageDiscoverCommand.php 0000644 00000002010 15107327037 0020206 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Foundation\PackageManifest; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'package:discover')] class PackageDiscoverCommand extends Command { /** * The console command signature. * * @var string */ protected $signature = 'package:discover'; /** * The console command description. * * @var string */ protected $description = 'Rebuild the cached package manifest'; /** * Execute the console command. * * @param \Illuminate\Foundation\PackageManifest $manifest * @return void */ public function handle(PackageManifest $manifest) { $this->components->info('Discovering packages'); $manifest->build(); collect($manifest->manifest) ->keys() ->each(fn ($description) => $this->components->task($description)) ->whenNotEmpty(fn () => $this->newLine()); } } src/Illuminate/Foundation/Console/StubPublishCommand.php 0000644 00000012244 15107327037 0017432 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Illuminate\Foundation\Events\PublishingStubs; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'stub:publish')] class StubPublishCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'stub:publish {--existing : Publish and overwrite only the files that have already been published} {--force : Overwrite any existing files}'; /** * The console command description. * * @var string */ protected $description = 'Publish all stubs that are available for customization'; /** * Execute the console command. * * @return void */ public function handle() { if (! is_dir($stubsPath = $this->laravel->basePath('stubs'))) { (new Filesystem)->makeDirectory($stubsPath); } $stubs = [ __DIR__.'/stubs/cast.inbound.stub' => 'cast.inbound.stub', __DIR__.'/stubs/cast.stub' => 'cast.stub', __DIR__.'/stubs/console.stub' => 'console.stub', __DIR__.'/stubs/event.stub' => 'event.stub', __DIR__.'/stubs/job.queued.stub' => 'job.queued.stub', __DIR__.'/stubs/job.stub' => 'job.stub', __DIR__.'/stubs/mail.stub' => 'mail.stub', __DIR__.'/stubs/markdown-mail.stub' => 'markdown-mail.stub', __DIR__.'/stubs/markdown-notification.stub' => 'markdown-notification.stub', __DIR__.'/stubs/model.pivot.stub' => 'model.pivot.stub', __DIR__.'/stubs/model.stub' => 'model.stub', __DIR__.'/stubs/notification.stub' => 'notification.stub', __DIR__.'/stubs/observer.plain.stub' => 'observer.plain.stub', __DIR__.'/stubs/observer.stub' => 'observer.stub', __DIR__.'/stubs/policy.plain.stub' => 'policy.plain.stub', __DIR__.'/stubs/policy.stub' => 'policy.stub', __DIR__.'/stubs/provider.stub' => 'provider.stub', __DIR__.'/stubs/request.stub' => 'request.stub', __DIR__.'/stubs/resource.stub' => 'resource.stub', __DIR__.'/stubs/resource-collection.stub' => 'resource-collection.stub', __DIR__.'/stubs/rule.stub' => 'rule.stub', __DIR__.'/stubs/scope.stub' => 'scope.stub', __DIR__.'/stubs/test.stub' => 'test.stub', __DIR__.'/stubs/test.unit.stub' => 'test.unit.stub', __DIR__.'/stubs/view-component.stub' => 'view-component.stub', realpath(__DIR__.'/../../Database/Console/Factories/stubs/factory.stub') => 'factory.stub', realpath(__DIR__.'/../../Database/Console/Seeds/stubs/seeder.stub') => 'seeder.stub', realpath(__DIR__.'/../../Database/Migrations/stubs/migration.create.stub') => 'migration.create.stub', realpath(__DIR__.'/../../Database/Migrations/stubs/migration.stub') => 'migration.stub', realpath(__DIR__.'/../../Database/Migrations/stubs/migration.update.stub') => 'migration.update.stub', realpath(__DIR__.'/../../Routing/Console/stubs/controller.api.stub') => 'controller.api.stub', realpath(__DIR__.'/../../Routing/Console/stubs/controller.invokable.stub') => 'controller.invokable.stub', realpath(__DIR__.'/../../Routing/Console/stubs/controller.model.api.stub') => 'controller.model.api.stub', realpath(__DIR__.'/../../Routing/Console/stubs/controller.model.stub') => 'controller.model.stub', realpath(__DIR__.'/../../Routing/Console/stubs/controller.nested.api.stub') => 'controller.nested.api.stub', realpath(__DIR__.'/../../Routing/Console/stubs/controller.nested.singleton.api.stub') => 'controller.nested.singleton.api.stub', realpath(__DIR__.'/../../Routing/Console/stubs/controller.nested.singleton.stub') => 'controller.nested.singleton.stub', realpath(__DIR__.'/../../Routing/Console/stubs/controller.nested.stub') => 'controller.nested.stub', realpath(__DIR__.'/../../Routing/Console/stubs/controller.plain.stub') => 'controller.plain.stub', realpath(__DIR__.'/../../Routing/Console/stubs/controller.singleton.api.stub') => 'controller.singleton.api.stub', realpath(__DIR__.'/../../Routing/Console/stubs/controller.singleton.stub') => 'controller.singleton.stub', realpath(__DIR__.'/../../Routing/Console/stubs/controller.stub') => 'controller.stub', realpath(__DIR__.'/../../Routing/Console/stubs/middleware.stub') => 'middleware.stub', ]; $this->laravel['events']->dispatch($event = new PublishingStubs($stubs)); foreach ($event->stubs as $from => $to) { $to = $stubsPath.DIRECTORY_SEPARATOR.ltrim($to, DIRECTORY_SEPARATOR); if ((! $this->option('existing') && (! file_exists($to) || $this->option('force'))) || ($this->option('existing') && file_exists($to))) { file_put_contents($to, file_get_contents($from)); } } $this->components->info('Stubs published successfully.'); } } src/Illuminate/Foundation/Console/Kernel.php 0000644 00000034335 15107327037 0015114 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Carbon\CarbonInterval; use Closure; use DateTimeInterface; use Illuminate\Console\Application as Artisan; use Illuminate\Console\Command; use Illuminate\Console\Events\CommandFinished; use Illuminate\Console\Events\CommandStarting; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Contracts\Console\Kernel as KernelContract; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Foundation\Application; use Illuminate\Support\Arr; use Illuminate\Support\Carbon; use Illuminate\Support\Env; use Illuminate\Support\InteractsWithTime; use Illuminate\Support\Str; use ReflectionClass; use SplFileInfo; use Symfony\Component\Console\ConsoleEvents; use Symfony\Component\Console\Event\ConsoleCommandEvent; use Symfony\Component\Console\Event\ConsoleTerminateEvent; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Finder\Finder; use Throwable; class Kernel implements KernelContract { use InteractsWithTime; /** * The application implementation. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The event dispatcher implementation. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * The Symfony event dispatcher implementation. * * @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface|null */ protected $symfonyDispatcher; /** * The Artisan application instance. * * @var \Illuminate\Console\Application|null */ protected $artisan; /** * The Artisan commands provided by the application. * * @var array */ protected $commands = []; /** * Indicates if the Closure commands have been loaded. * * @var bool */ protected $commandsLoaded = false; /** * All of the registered command duration handlers. * * @var array */ protected $commandLifecycleDurationHandlers = []; /** * When the currently handled command started. * * @var \Illuminate\Support\Carbon|null */ protected $commandStartedAt; /** * The bootstrap classes for the application. * * @var string[] */ protected $bootstrappers = [ \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class, \Illuminate\Foundation\Bootstrap\LoadConfiguration::class, \Illuminate\Foundation\Bootstrap\HandleExceptions::class, \Illuminate\Foundation\Bootstrap\RegisterFacades::class, \Illuminate\Foundation\Bootstrap\SetRequestForConsole::class, \Illuminate\Foundation\Bootstrap\RegisterProviders::class, \Illuminate\Foundation\Bootstrap\BootProviders::class, ]; /** * Create a new console kernel instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @param \Illuminate\Contracts\Events\Dispatcher $events * @return void */ public function __construct(Application $app, Dispatcher $events) { if (! defined('ARTISAN_BINARY')) { define('ARTISAN_BINARY', 'artisan'); } $this->app = $app; $this->events = $events; $this->app->booted(function () { if (! $this->app->runningUnitTests()) { $this->rerouteSymfonyCommandEvents(); } $this->defineConsoleSchedule(); }); } /** * Re-route the Symfony command events to their Laravel counterparts. * * @internal * * @return $this */ public function rerouteSymfonyCommandEvents() { if (is_null($this->symfonyDispatcher)) { $this->symfonyDispatcher = new EventDispatcher; $this->symfonyDispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) { $this->events->dispatch( new CommandStarting($event->getCommand()->getName(), $event->getInput(), $event->getOutput()) ); }); $this->symfonyDispatcher->addListener(ConsoleEvents::TERMINATE, function (ConsoleTerminateEvent $event) { $this->events->dispatch( new CommandFinished($event->getCommand()->getName(), $event->getInput(), $event->getOutput(), $event->getExitCode()) ); }); } return $this; } /** * Define the application's command schedule. * * @return void */ protected function defineConsoleSchedule() { $this->app->singleton(Schedule::class, function ($app) { return tap(new Schedule($this->scheduleTimezone()), function ($schedule) { $this->schedule($schedule->useCache($this->scheduleCache())); }); }); } /** * Get the name of the cache store that should manage scheduling mutexes. * * @return string */ protected function scheduleCache() { return $this->app['config']->get('cache.schedule_store', Env::get('SCHEDULE_CACHE_DRIVER')); } /** * Run the console application. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface|null $output * @return int */ public function handle($input, $output = null) { $this->commandStartedAt = Carbon::now(); try { if (in_array($input->getFirstArgument(), ['env:encrypt', 'env:decrypt'], true)) { $this->bootstrapWithoutBootingProviders(); } $this->bootstrap(); return $this->getArtisan()->run($input, $output); } catch (Throwable $e) { $this->reportException($e); $this->renderException($output, $e); return 1; } } /** * Terminate the application. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param int $status * @return void */ public function terminate($input, $status) { $this->app->terminate(); if ($this->commandStartedAt === null) { return; } $this->commandStartedAt->setTimezone($this->app['config']->get('app.timezone') ?? 'UTC'); foreach ($this->commandLifecycleDurationHandlers as ['threshold' => $threshold, 'handler' => $handler]) { $end ??= Carbon::now(); if ($this->commandStartedAt->diffInMilliseconds($end) > $threshold) { $handler($this->commandStartedAt, $input, $status); } } $this->commandStartedAt = null; } /** * Register a callback to be invoked when the command lifecycle duration exceeds a given amount of time. * * @param \DateTimeInterface|\Carbon\CarbonInterval|float|int $threshold * @param callable $handler * @return void */ public function whenCommandLifecycleIsLongerThan($threshold, $handler) { $threshold = $threshold instanceof DateTimeInterface ? $this->secondsUntil($threshold) * 1000 : $threshold; $threshold = $threshold instanceof CarbonInterval ? $threshold->totalMilliseconds : $threshold; $this->commandLifecycleDurationHandlers[] = [ 'threshold' => $threshold, 'handler' => $handler, ]; } /** * When the command being handled started. * * @return \Illuminate\Support\Carbon|null */ public function commandStartedAt() { return $this->commandStartedAt; } /** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { // } /** * Get the timezone that should be used by default for scheduled events. * * @return \DateTimeZone|string|null */ protected function scheduleTimezone() { $config = $this->app['config']; return $config->get('app.schedule_timezone', $config->get('app.timezone')); } /** * Register the commands for the application. * * @return void */ protected function commands() { // } /** * Register a Closure based command with the application. * * @param string $signature * @param \Closure $callback * @return \Illuminate\Foundation\Console\ClosureCommand */ public function command($signature, Closure $callback) { $command = new ClosureCommand($signature, $callback); Artisan::starting(function ($artisan) use ($command) { $artisan->add($command); }); return $command; } /** * Register all of the commands in the given directory. * * @param array|string $paths * @return void */ protected function load($paths) { $paths = array_unique(Arr::wrap($paths)); $paths = array_filter($paths, function ($path) { return is_dir($path); }); if (empty($paths)) { return; } $namespace = $this->app->getNamespace(); foreach ((new Finder)->in($paths)->files() as $file) { $command = $this->commandClassFromFile($file, $namespace); if (is_subclass_of($command, Command::class) && ! (new ReflectionClass($command))->isAbstract()) { Artisan::starting(function ($artisan) use ($command) { $artisan->resolve($command); }); } } } /** * Extract the command class name from the given file path. * * @param \SplFileInfo $file * @param string $namespace * @return string */ protected function commandClassFromFile(SplFileInfo $file, string $namespace): string { return $namespace.str_replace( ['/', '.php'], ['\\', ''], Str::after($file->getRealPath(), realpath(app_path()).DIRECTORY_SEPARATOR) ); } /** * Register the given command with the console application. * * @param \Symfony\Component\Console\Command\Command $command * @return void */ public function registerCommand($command) { $this->getArtisan()->add($command); } /** * Run an Artisan console command by name. * * @param string $command * @param array $parameters * @param \Symfony\Component\Console\Output\OutputInterface|null $outputBuffer * @return int * * @throws \Symfony\Component\Console\Exception\CommandNotFoundException */ public function call($command, array $parameters = [], $outputBuffer = null) { if (in_array($command, ['env:encrypt', 'env:decrypt'], true)) { $this->bootstrapWithoutBootingProviders(); } $this->bootstrap(); return $this->getArtisan()->call($command, $parameters, $outputBuffer); } /** * Queue the given console command. * * @param string $command * @param array $parameters * @return \Illuminate\Foundation\Bus\PendingDispatch */ public function queue($command, array $parameters = []) { return QueuedCommand::dispatch(func_get_args()); } /** * Get all of the commands registered with the console. * * @return array */ public function all() { $this->bootstrap(); return $this->getArtisan()->all(); } /** * Get the output for the last run command. * * @return string */ public function output() { $this->bootstrap(); return $this->getArtisan()->output(); } /** * Bootstrap the application for artisan commands. * * @return void */ public function bootstrap() { if (! $this->app->hasBeenBootstrapped()) { $this->app->bootstrapWith($this->bootstrappers()); } $this->app->loadDeferredProviders(); if (! $this->commandsLoaded) { $this->commands(); $this->commandsLoaded = true; } } /** * Bootstrap the application without booting service providers. * * @return void */ public function bootstrapWithoutBootingProviders() { $this->app->bootstrapWith( collect($this->bootstrappers())->reject(function ($bootstrapper) { return $bootstrapper === \Illuminate\Foundation\Bootstrap\BootProviders::class; })->all() ); } /** * Get the Artisan application instance. * * @return \Illuminate\Console\Application */ protected function getArtisan() { if (is_null($this->artisan)) { $this->artisan = (new Artisan($this->app, $this->events, $this->app->version())) ->resolveCommands($this->commands) ->setContainerCommandLoader(); if ($this->symfonyDispatcher instanceof EventDispatcher) { $this->artisan->setDispatcher($this->symfonyDispatcher); $this->artisan->setSignalsToDispatchEvent(); } } return $this->artisan; } /** * Set the Artisan application instance. * * @param \Illuminate\Console\Application|null $artisan * @return void */ public function setArtisan($artisan) { $this->artisan = $artisan; } /** * Get the bootstrap classes for the application. * * @return array */ protected function bootstrappers() { return $this->bootstrappers; } /** * Report the exception to the exception handler. * * @param \Throwable $e * @return void */ protected function reportException(Throwable $e) { $this->app[ExceptionHandler::class]->report($e); } /** * Render the given exception. * * @param \Symfony\Component\Console\Output\OutputInterface $output * @param \Throwable $e * @return void */ protected function renderException($output, Throwable $e) { $this->app[ExceptionHandler::class]->renderForConsole($output, $e); } } src/Illuminate/Foundation/Console/OptimizeCommand.php 0000644 00000001660 15107327037 0016766 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'optimize')] class OptimizeCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'optimize'; /** * The console command description. * * @var string */ protected $description = 'Cache the framework bootstrap files'; /** * Execute the console command. * * @return void */ public function handle() { $this->components->info('Caching the framework bootstrap files'); collect([ 'config' => fn () => $this->callSilent('config:cache') == 0, 'routes' => fn () => $this->callSilent('route:cache') == 0, ])->each(fn ($task, $description) => $this->components->task($description, $task)); $this->newLine(); } } src/Illuminate/Foundation/Console/ChannelListCommand.php 0000644 00000010437 15107327037 0017374 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Closure; use Illuminate\Console\Command; use Illuminate\Contracts\Broadcasting\Broadcaster; use Illuminate\Support\Collection; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Terminal; #[AsCommand(name: 'channel:list')] class ChannelListCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'channel:list'; /** * The console command description. * * @var string */ protected $description = 'List all registered private broadcast channels'; /** * The terminal width resolver callback. * * @var \Closure|null */ protected static $terminalWidthResolver; /** * Execute the console command. * * @param \Illuminate\Contracts\Broadcasting\Broadcaster * @return void */ public function handle(Broadcaster $broadcaster) { $channels = $broadcaster->getChannels(); if (! $this->laravel->providerIsLoaded('App\Providers\BroadcastServiceProvider') && file_exists($this->laravel->path('Providers/BroadcastServiceProvider.php'))) { $this->components->warn('The [App\Providers\BroadcastServiceProvider] has not been loaded. Your private channels may not be loaded.'); } if (! $channels->count()) { return $this->components->error("Your application doesn't have any private broadcasting channels."); } $this->displayChannels($channels); } /** * Display the channel information on the console. * * @param Collection $channels * @return void */ protected function displayChannels($channels) { $this->output->writeln($this->forCli($channels)); } /** * Convert the given channels to regular CLI output. * * @param \Illuminate\Support\Collection $channels * @return array */ protected function forCli($channels) { $maxChannelName = $channels->keys()->max(function ($channelName) { return mb_strlen($channelName); }); $terminalWidth = $this->getTerminalWidth(); $channelCount = $this->determineChannelCountOutput($channels, $terminalWidth); return $channels->map(function ($channel, $channelName) use ($maxChannelName, $terminalWidth) { $resolver = $channel instanceof Closure ? 'Closure' : $channel; $spaces = str_repeat(' ', max($maxChannelName + 6 - mb_strlen($channelName), 0)); $dots = str_repeat('.', max( $terminalWidth - mb_strlen($channelName.$spaces.$resolver) - 6, 0 )); $dots = empty($dots) ? $dots : " $dots"; return sprintf( ' <fg=blue;options=bold>%s</> %s<fg=white>%s</><fg=#6C7280>%s</>', $channelName, $spaces, $resolver, $dots, ); }) ->filter() ->sort() ->prepend('') ->push('')->push($channelCount)->push('') ->toArray(); } /** * Determine and return the output for displaying the number of registered channels in the CLI output. * * @param \Illuminate\Support\Collection $channels * @param int $terminalWidth * @return string */ protected function determineChannelCountOutput($channels, $terminalWidth) { $channelCountText = 'Showing ['.$channels->count().'] private channels'; $offset = $terminalWidth - mb_strlen($channelCountText) - 2; $spaces = str_repeat(' ', $offset); return $spaces.'<fg=blue;options=bold>Showing ['.$channels->count().'] private channels</>'; } /** * Get the terminal width. * * @return int */ public static function getTerminalWidth() { return is_null(static::$terminalWidthResolver) ? (new Terminal)->getWidth() : call_user_func(static::$terminalWidthResolver); } /** * Set a callback that should be used when resolving the terminal width. * * @param \Closure|null $resolver * @return void */ public static function resolveTerminalWidthUsing($resolver) { static::$terminalWidthResolver = $resolver; } } src/Illuminate/Foundation/Console/ViewCacheCommand.php 0000644 00000005442 15107327037 0017026 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Support\Collection; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; #[AsCommand(name: 'view:cache')] class ViewCacheCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'view:cache'; /** * The console command description. * * @var string */ protected $description = "Compile all of the application's Blade templates"; /** * Execute the console command. * * @return mixed */ public function handle() { $this->callSilent('view:clear'); $this->paths()->each(function ($path) { $prefix = $this->output->isVeryVerbose() ? '<fg=yellow;options=bold>DIR</> ' : ''; $this->components->task($prefix.$path, null, OutputInterface::VERBOSITY_VERBOSE); $this->compileViews($this->bladeFilesIn([$path])); }); $this->newLine(); $this->components->info('Blade templates cached successfully.'); } /** * Compile the given view files. * * @param \Illuminate\Support\Collection $views * @return void */ protected function compileViews(Collection $views) { $compiler = $this->laravel['view']->getEngineResolver()->resolve('blade')->getCompiler(); $views->map(function (SplFileInfo $file) use ($compiler) { $this->components->task(' '.$file->getRelativePathname(), null, OutputInterface::VERBOSITY_VERY_VERBOSE); $compiler->compile($file->getRealPath()); }); if ($this->output->isVeryVerbose()) { $this->newLine(); } } /** * Get the Blade files in the given path. * * @param array $paths * @return \Illuminate\Support\Collection */ protected function bladeFilesIn(array $paths) { $extensions = collect($this->laravel['view']->getExtensions()) ->filter(fn ($value) => $value === 'blade') ->keys() ->map(fn ($extension) => "*.{$extension}") ->all(); return collect( Finder::create() ->in($paths) ->exclude('vendor') ->name($extensions) ->files() ); } /** * Get all of the possible view paths. * * @return \Illuminate\Support\Collection */ protected function paths() { $finder = $this->laravel['view']->getFinder(); return collect($finder->getPaths())->merge( collect($finder->getHints())->flatten() ); } } src/Illuminate/Foundation/Console/RouteClearCommand.php 0000644 00000002216 15107327037 0017231 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'route:clear')] class RouteClearCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'route:clear'; /** * The console command description. * * @var string */ protected $description = 'Remove the route cache file'; /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * Create a new route clear command instance. * * @param \Illuminate\Filesystem\Filesystem $files * @return void */ public function __construct(Filesystem $files) { parent::__construct(); $this->files = $files; } /** * Execute the console command. * * @return void */ public function handle() { $this->files->delete($this->laravel->getCachedRoutesPath()); $this->components->info('Route cache cleared successfully.'); } } src/Illuminate/Foundation/Console/DocsCommand.php 0000644 00000032626 15107327037 0016064 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Carbon\CarbonInterval; use Illuminate\Console\Command; use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Http\Client\Factory as Http; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Env; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\ExecutableFinder; use Symfony\Component\Process\Process; use Throwable; use function Laravel\Prompts\suggest; #[AsCommand(name: 'docs')] class DocsCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'docs {page? : The documentation page to open} {section? : The section of the page to open}'; /** * The console command description. * * @var string */ protected $description = 'Access the Laravel documentation'; /** * The console command help text. * * @var string */ protected $help = 'If you would like to perform a content search against the documentation, you may call: <fg=green>php artisan docs -- </><fg=green;options=bold;>search query here</>'; /** * The HTTP client instance. * * @var \Illuminate\Http\Client\Factory */ protected $http; /** * The cache repository implementation. * * @var \Illuminate\Contracts\Cache\Repository */ protected $cache; /** * The custom URL opener. * * @var callable|null */ protected $urlOpener; /** * The custom documentation version to open. * * @var string|null */ protected $version; /** * The operating system family. * * @var string */ protected $systemOsFamily = PHP_OS_FAMILY; /** * Configure the current command. * * @return void */ protected function configure() { parent::configure(); if ($this->isSearching()) { $this->ignoreValidationErrors(); } } /** * Execute the console command. * * @param \Illuminate\Http\Client\Factory $http * @param \Illuminate\Contracts\Cache\Repository $cache * @return int */ public function handle(Http $http, Cache $cache) { $this->http = $http; $this->cache = $cache; try { $this->openUrl(); } catch (ProcessFailedException $e) { if ($e->getProcess()->getExitCodeText() === 'Interrupt') { return $e->getProcess()->getExitCode(); } throw $e; } $this->refreshDocs(); return Command::SUCCESS; } /** * Open the documentation URL. * * @return void */ protected function openUrl() { with($this->url(), function ($url) { $this->components->info("Opening the docs to: <fg=yellow>{$url}</>"); $this->open($url); }); } /** * The URL to the documentation page. * * @return string */ protected function url() { if ($this->isSearching()) { return "https://laravel.com/docs/{$this->version()}?".Arr::query([ 'q' => $this->searchQuery(), ]); } return with($this->page(), function ($page) { return trim("https://laravel.com/docs/{$this->version()}/{$page}#{$this->section($page)}", '#/'); }); } /** * The page the user is opening. * * @return string */ protected function page() { return with($this->resolvePage(), function ($page) { if ($page === null) { $this->components->warn('Unable to determine the page you are trying to visit.'); return '/'; } return $page; }); } /** * Determine the page to open. * * @return string|null */ protected function resolvePage() { if ($this->option('no-interaction') && $this->didNotRequestPage()) { return '/'; } return $this->didNotRequestPage() ? $this->askForPage() : $this->guessPage($this->argument('page')); } /** * Determine if the user requested a specific page when calling the command. * * @return bool */ protected function didNotRequestPage() { return $this->argument('page') === null; } /** * Ask the user which page they would like to open. * * @return string|null */ protected function askForPage() { return $this->askForPageViaCustomStrategy() ?? $this->askForPageViaAutocomplete(); } /** * Ask the user which page they would like to open via a custom strategy. * * @return string|null */ protected function askForPageViaCustomStrategy() { try { $strategy = require Env::get('ARTISAN_DOCS_ASK_STRATEGY'); } catch (Throwable) { return null; } if (! is_callable($strategy)) { return null; } return $strategy($this) ?? '/'; } /** * Ask the user which page they would like to open using autocomplete. * * @return string|null */ protected function askForPageViaAutocomplete() { $choice = suggest( label: 'Which page would you like to open?', options: fn ($value) => $this->pages() ->mapWithKeys(fn ($option) => [ Str::lower($option['title']) => $option['title'], ]) ->filter(fn ($title) => str_contains(Str::lower($title), Str::lower($value))) ->all(), placeholder: 'E.g. Collections' ); return $this->pages()->filter( fn ($page) => $page['title'] === $choice || Str::lower($page['title']) === $choice )->keys()->first() ?: $this->guessPage($choice); } /** * Guess the page the user is attempting to open. * * @return string|null */ protected function guessPage($search) { return $this->pages() ->filter(fn ($page) => str_starts_with( Str::slug($page['title'], ' '), Str::slug($search, ' ') ))->keys()->first() ?? $this->pages()->map(fn ($page) => similar_text( Str::slug($page['title'], ' '), Str::slug($search, ' '), )) ->filter(fn ($score) => $score >= min(3, Str::length($search))) ->sortDesc() ->keys() ->sortByDesc(fn ($slug) => Str::contains( Str::slug($this->pages()[$slug]['title'], ' '), Str::slug($search, ' ') ) ? 1 : 0) ->first(); } /** * The section the user specifically asked to open. * * @param string $page * @return string|null */ protected function section($page) { return $this->didNotRequestSection() ? null : $this->guessSection($page); } /** * Determine if the user requested a specific section when calling the command. * * @return bool */ protected function didNotRequestSection() { return $this->argument('section') === null; } /** * Guess the section the user is attempting to open. * * @param string $page * @return string|null */ protected function guessSection($page) { return $this->sectionsFor($page) ->filter(fn ($section) => str_starts_with( Str::slug($section['title'], ' '), Str::slug($this->argument('section'), ' ') ))->keys()->first() ?? $this->sectionsFor($page)->map(fn ($section) => similar_text( Str::slug($section['title'], ' '), Str::slug($this->argument('section'), ' '), )) ->filter(fn ($score) => $score >= min(3, Str::length($this->argument('section')))) ->sortDesc() ->keys() ->sortByDesc(fn ($slug) => Str::contains( Str::slug($this->sectionsFor($page)[$slug]['title'], ' '), Str::slug($this->argument('section'), ' ') ) ? 1 : 0) ->first(); } /** * Open the URL in the user's browser. * * @param string $url * @return void */ protected function open($url) { ($this->urlOpener ?? function ($url) { if (Env::get('ARTISAN_DOCS_OPEN_STRATEGY')) { $this->openViaCustomStrategy($url); } elseif (in_array($this->systemOsFamily, ['Darwin', 'Windows', 'Linux'])) { $this->openViaBuiltInStrategy($url); } else { $this->components->warn('Unable to open the URL on your system. You will need to open it yourself or create a custom opener for your system.'); } })($url); } /** * Open the URL via a custom strategy. * * @param string $url * @return void */ protected function openViaCustomStrategy($url) { try { $command = require Env::get('ARTISAN_DOCS_OPEN_STRATEGY'); } catch (Throwable) { $command = null; } if (! is_callable($command)) { $this->components->warn('Unable to open the URL with your custom strategy. You will need to open it yourself.'); return; } $command($url); } /** * Open the URL via the built in strategy. * * @param string $url * @return void */ protected function openViaBuiltInStrategy($url) { if ($this->systemOsFamily === 'Windows') { $process = tap(Process::fromShellCommandline(escapeshellcmd("start {$url}")))->run(); if (! $process->isSuccessful()) { throw new ProcessFailedException($process); } return; } $binary = Collection::make(match ($this->systemOsFamily) { 'Darwin' => ['open'], 'Linux' => ['xdg-open', 'wslview'], })->first(fn ($binary) => (new ExecutableFinder)->find($binary) !== null); if ($binary === null) { $this->components->warn('Unable to open the URL on your system. You will need to open it yourself or create a custom opener for your system.'); return; } $process = tap(Process::fromShellCommandline(escapeshellcmd("{$binary} {$url}")))->run(); if (! $process->isSuccessful()) { throw new ProcessFailedException($process); } } /** * The available sections for the page. * * @param string $page * @return \Illuminate\Support\Collection */ public function sectionsFor($page) { return new Collection($this->pages()[$page]['sections']); } /** * The pages available to open. * * @return \Illuminate\Support\Collection */ public function pages() { return new Collection($this->docs()['pages']); } /** * Get the documentation index as a collection. * * @return \Illuminate\Support\Collection */ public function docs() { return $this->cache->remember( "artisan.docs.{{$this->version()}}.index", CarbonInterval::months(2), fn () => $this->fetchDocs()->throw()->collect() ); } /** * Refresh the cached copy of the documentation index. * * @return void */ protected function refreshDocs() { with($this->fetchDocs(), function ($response) { if ($response->successful()) { $this->cache->put("artisan.docs.{{$this->version()}}.index", $response->collect(), CarbonInterval::months(2)); } }); } /** * Fetch the documentation index from the Laravel website. * * @return \Illuminate\Http\Client\Response */ protected function fetchDocs() { return $this->http->get("https://laravel.com/docs/{$this->version()}/index.json"); } /** * Determine the version of the docs to open. * * @return string */ protected function version() { return Str::before($this->version ?? $this->laravel->version(), '.').'.x'; } /** * The search query the user provided. * * @return string */ protected function searchQuery() { return Collection::make($_SERVER['argv'])->skip(3)->implode(' '); } /** * Determine if the command is intended to perform a search. * * @return bool */ protected function isSearching() { return ($_SERVER['argv'][2] ?? null) === '--'; } /** * Set the documentation version. * * @param string $version * @return $this */ public function setVersion($version) { $this->version = $version; return $this; } /** * Set a custom URL opener. * * @param callable|null $opener * @return $this */ public function setUrlOpener($opener) { $this->urlOpener = $opener; return $this; } /** * Set the system operating system family. * * @param string $family * @return $this */ public function setSystemOsFamily($family) { $this->systemOsFamily = $family; return $this; } } src/Illuminate/Foundation/Console/stubs/job.stub 0000644 00000000513 15107327037 0015763 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Foundation\Bus\Dispatchable; class {{ class }} { use Dispatchable; /** * Create a new job instance. */ public function __construct() { // } /** * Execute the job. */ public function handle(): void { // } } src/Illuminate/Foundation/Console/stubs/maintenance-mode.stub 0000644 00000004041 15107327037 0020415 0 ustar 00 <?php // Check if the application is in maintenance mode... if (! file_exists($down = __DIR__.'/down')) { return; } // Decode the "down" file's JSON... $data = json_decode(file_get_contents($down), true); // Allow framework to handle request if no prerendered template... if (! isset($data['template'])) { return; } // Allow framework to handle request if request URI is in the exclude list... if (isset($data['except'])) { $uri = parse_url($_SERVER['REQUEST_URI'])['path']; $uri = rawurldecode($uri !== '/' ? trim($uri, '/') : $uri); foreach ((array) $data['except'] as $except) { $except = $except !== '/' ? trim($except, '/') : $except; if ($except == $uri) { return; } $except = preg_quote($except, '#'); $except = str_replace('\*', '.*', $except); if (preg_match('#^'.$except.'\z#u', $uri) === 1) { return; } } } // Allow framework to handle maintenance mode bypass route... if (isset($data['secret']) && $_SERVER['REQUEST_URI'] === '/'.$data['secret']) { return; } // Determine if maintenance mode bypass cookie is valid... if (isset($_COOKIE['laravel_maintenance']) && isset($data['secret'])) { $payload = json_decode(base64_decode($_COOKIE['laravel_maintenance']), true); if (is_array($payload) && is_numeric($payload['expires_at'] ?? null) && isset($payload['mac']) && hash_equals(hash_hmac('sha256', $payload['expires_at'], $data['secret']), $payload['mac']) && (int) $payload['expires_at'] >= time()) { return; } } // Redirect to the proper path if necessary... if (isset($data['redirect']) && $_SERVER['REQUEST_URI'] !== $data['redirect']) { http_response_code(302); header('Location: '.$data['redirect']); exit; } // Output the prerendered template... http_response_code($data['status'] ?? 503); if (isset($data['retry'])) { header('Retry-After: '.$data['retry']); } if (isset($data['refresh'])) { header('Refresh: '.$data['refresh']); } echo $data['template']; exit; src/Illuminate/Foundation/Console/stubs/console.stub 0000644 00000000746 15107327037 0016663 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Console\Command; class {{ class }} extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = '{{ command }}'; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Execute the console command. */ public function handle() { // } } src/Illuminate/Foundation/Console/stubs/resource.stub 0000644 00000000560 15107327037 0017042 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; class {{ class }} extends JsonResource { /** * Transform the resource into an array. * * @return array<string, mixed> */ public function toArray(Request $request): array { return parent::toArray($request); } } src/Illuminate/Foundation/Console/stubs/notification.stub 0000644 00000002235 15107327037 0017702 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; class {{ class }} extends Notification { use Queueable; /** * Create a new notification instance. */ public function __construct() { // } /** * Get the notification's delivery channels. * * @return array<int, string> */ public function via(object $notifiable): array { return ['mail']; } /** * Get the mail representation of the notification. */ public function toMail(object $notifiable): MailMessage { return (new MailMessage) ->line('The introduction to the notification.') ->action('Notification Action', url('/')) ->line('Thank you for using our application!'); } /** * Get the array representation of the notification. * * @return array<string, mixed> */ public function toArray(object $notifiable): array { return [ // ]; } } src/Illuminate/Foundation/Console/stubs/markdown.stub 0000644 00000000257 15107327037 0017040 0 ustar 00 <x-mail::message> # Introduction The body of your message. <x-mail::button :url="''"> Button Text </x-mail::button> Thanks,<br> {{ config('app.name') }} </x-mail::message> src/Illuminate/Foundation/Console/stubs/channel.stub 0000644 00000000542 15107327037 0016623 0 ustar 00 <?php namespace {{ namespace }}; use {{ namespacedUserModel }}; class {{ class }} { /** * Create a new channel instance. */ public function __construct() { // } /** * Authenticate the user's access to the channel. */ public function join({{ userModel }} $user): array|bool { // } } src/Illuminate/Foundation/Console/stubs/listener-queued.stub 0000644 00000000676 15107327037 0020336 0 ustar 00 <?php namespace {{ namespace }}; use {{ eventNamespace }}; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; class {{ class }} implements ShouldQueue { use InteractsWithQueue; /** * Create the event listener. */ public function __construct() { // } /** * Handle the event. */ public function handle({{ event }} $event): void { // } } src/Illuminate/Foundation/Console/stubs/rule.implicit.stub 0000644 00000001016 15107327037 0017770 0 ustar 00 <?php namespace {{ namespace }}; use Closure; use Illuminate\Contracts\Validation\ValidationRule; class {{ class }} implements ValidationRule { /** * Indicates whether the rule should be implicit. * * @var bool */ public $implicit = true; /** * Run the validation rule. * * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail */ public function validate(string $attribute, mixed $value, Closure $fail): void { // } } src/Illuminate/Foundation/Console/stubs/listener-queued-duck.stub 0000644 00000000637 15107327037 0021257 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; class {{ class }} implements ShouldQueue { use InteractsWithQueue; /** * Create the event listener. */ public function __construct() { // } /** * Handle the event. */ public function handle(object $event): void { // } } src/Illuminate/Foundation/Console/stubs/exception-render.stub 0000644 00000000445 15107327037 0020470 0 ustar 00 <?php namespace {{ namespace }}; use Exception; use Illuminate\Http\Request; use Illuminate\Http\Response; class {{ class }} extends Exception { /** * Render the exception as an HTTP response. */ public function render(Request $request): Response { // } } src/Illuminate/Foundation/Console/stubs/routes.stub 0000644 00000000707 15107327037 0016537 0 ustar 00 <?php /* |-------------------------------------------------------------------------- | Load The Cached Routes |-------------------------------------------------------------------------- | | Here we will decode and unserialize the RouteCollection instance that | holds all of the route information for an application. This allows | us to instantaneously load the entire route map into the router. | */ app('router')->setCompiledRoutes( {{routes}} ); src/Illuminate/Foundation/Console/stubs/test.unit.stub 0000644 00000000363 15107327037 0017151 0 ustar 00 <?php namespace {{ namespace }}; use PHPUnit\Framework\TestCase; class {{ class }} extends TestCase { /** * A basic unit test example. */ public function test_example(): void { $this->assertTrue(true); } } src/Illuminate/Foundation/Console/stubs/model.stub 0000644 00000000273 15107327037 0016314 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class {{ class }} extends Model { use HasFactory; } src/Illuminate/Foundation/Console/stubs/mail.stub 0000644 00000001765 15107327037 0016145 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Queue\SerializesModels; class {{ class }} extends Mailable { use Queueable, SerializesModels; /** * Create a new message instance. */ public function __construct() { // } /** * Get the message envelope. */ public function envelope(): Envelope { return new Envelope( subject: '{{ subject }}', ); } /** * Get the message content definition. */ public function content(): Content { return new Content( view: 'view.name', ); } /** * Get the attachments for the message. * * @return array<int, \Illuminate\Mail\Mailables\Attachment> */ public function attachments(): array { return []; } } src/Illuminate/Foundation/Console/stubs/test.stub 0000644 00000000565 15107327037 0016177 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; class {{ class }} extends TestCase { /** * A basic feature test example. */ public function test_example(): void { $response = $this->get('/'); $response->assertStatus(200); } } src/Illuminate/Foundation/Console/stubs/request.stub 0000644 00000001042 15107327037 0016677 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Foundation\Http\FormRequest; class {{ class }} extends FormRequest { /** * Determine if the user is authorized to make this request. */ public function authorize(): bool { return false; } /** * Get the validation rules that apply to the request. * * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> */ public function rules(): array { return [ // ]; } } src/Illuminate/Foundation/Console/stubs/scope.stub 0000644 00000000546 15107327037 0016330 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Scope; class {{ class }} implements Scope { /** * Apply the scope to a given Eloquent query builder. */ public function apply(Builder $builder, Model $model): void { // } } src/Illuminate/Foundation/Console/stubs/exception-report.stub 0000644 00000000302 15107327037 0020514 0 ustar 00 <?php namespace {{ namespace }}; use Exception; class {{ class }} extends Exception { /** * Report the exception. */ public function report(): void { // } } src/Illuminate/Foundation/Console/stubs/model.morph-pivot.stub 0000644 00000000213 15107327037 0020571 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Database\Eloquent\Relations\MorphPivot; class {{ class }} extends MorphPivot { // } src/Illuminate/Foundation/Console/stubs/listener.stub 0000644 00000000612 15107327037 0017036 0 ustar 00 <?php namespace {{ namespace }}; use {{ eventNamespace }}; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; class {{ class }} { /** * Create the event listener. */ public function __construct() { // } /** * Handle the event. */ public function handle({{ event }} $event): void { // } } src/Illuminate/Foundation/Console/stubs/model.pivot.stub 0000644 00000000201 15107327037 0017443 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Database\Eloquent\Relations\Pivot; class {{ class }} extends Pivot { // } src/Illuminate/Foundation/Console/stubs/cast.stub 0000644 00000001217 15107327037 0016145 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Database\Eloquent\Model; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; class {{ class }} implements CastsAttributes { /** * Cast the given value. * * @param array<string, mixed> $attributes */ public function get(Model $model, string $key, mixed $value, array $attributes): mixed { return $value; } /** * Prepare the given value for storage. * * @param array<string, mixed> $attributes */ public function set(Model $model, string $key, mixed $value, array $attributes): mixed { return $value; } } src/Illuminate/Foundation/Console/stubs/markdown-mail.stub 0000644 00000001772 15107327037 0017763 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Queue\SerializesModels; class {{ class }} extends Mailable { use Queueable, SerializesModels; /** * Create a new message instance. */ public function __construct() { // } /** * Get the message envelope. */ public function envelope(): Envelope { return new Envelope( subject: '{{ subject }}', ); } /** * Get the message content definition. */ public function content(): Content { return new Content( markdown: '{{ view }}', ); } /** * Get the attachments for the message. * * @return array<int, \Illuminate\Mail\Mailables\Attachment> */ public function attachments(): array { return []; } } src/Illuminate/Foundation/Console/stubs/event.stub 0000644 00000001457 15107327037 0016342 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Broadcasting\Channel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\PresenceChannel; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\SerializesModels; class {{ class }} { use Dispatchable, InteractsWithSockets, SerializesModels; /** * Create a new event instance. */ public function __construct() { // } /** * Get the channels the event should broadcast on. * * @return array<int, \Illuminate\Broadcasting\Channel> */ public function broadcastOn(): array { return [ new PrivateChannel('channel-name'), ]; } } src/Illuminate/Foundation/Console/stubs/observer.stub 0000644 00000001550 15107327037 0017042 0 ustar 00 <?php namespace {{ namespace }}; use {{ namespacedModel }}; class {{ class }} { /** * Handle the {{ model }} "created" event. */ public function created({{ model }} ${{ modelVariable }}): void { // } /** * Handle the {{ model }} "updated" event. */ public function updated({{ model }} ${{ modelVariable }}): void { // } /** * Handle the {{ model }} "deleted" event. */ public function deleted({{ model }} ${{ modelVariable }}): void { // } /** * Handle the {{ model }} "restored" event. */ public function restored({{ model }} ${{ modelVariable }}): void { // } /** * Handle the {{ model }} "force deleted" event. */ public function forceDeleted({{ model }} ${{ modelVariable }}): void { // } } src/Illuminate/Foundation/Console/stubs/provider.stub 0000644 00000000504 15107327037 0017043 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Support\ServiceProvider; class {{ class }} extends ServiceProvider { /** * Register services. */ public function register(): void { // } /** * Bootstrap services. */ public function boot(): void { // } } src/Illuminate/Foundation/Console/stubs/resource-collection.stub 0000644 00000000613 15107327037 0021172 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\ResourceCollection; class {{ class }} extends ResourceCollection { /** * Transform the resource collection into an array. * * @return array<int|string, mixed> */ public function toArray(Request $request): array { return parent::toArray($request); } } src/Illuminate/Foundation/Console/stubs/exception.stub 0000644 00000000142 15107327037 0017205 0 ustar 00 <?php namespace {{ namespace }}; use Exception; class {{ class }} extends Exception { // } src/Illuminate/Foundation/Console/stubs/cast.inbound.stub 0000644 00000000671 15107327037 0017605 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Database\Eloquent\Model; use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes; class {{ class }} implements CastsInboundAttributes { /** * Prepare the given value for storage. * * @param array<string, mixed> $attributes */ public function set(Model $model, string $key, mixed $value, array $attributes): mixed { return $value; } } src/Illuminate/Foundation/Console/stubs/markdown-notification.stub 0000644 00000001760 15107327037 0021524 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; class {{ class }} extends Notification { use Queueable; /** * Create a new notification instance. */ public function __construct() { // } /** * Get the notification's delivery channels. * * @return array<int, string> */ public function via(object $notifiable): array { return ['mail']; } /** * Get the mail representation of the notification. */ public function toMail(object $notifiable): MailMessage { return (new MailMessage)->markdown('{{ view }}'); } /** * Get the array representation of the notification. * * @return array<string, mixed> */ public function toArray(object $notifiable): array { return [ // ]; } } src/Illuminate/Foundation/Console/stubs/policy.plain.stub 0000644 00000000307 15107327037 0017613 0 ustar 00 <?php namespace {{ namespace }}; use {{ namespacedUserModel }}; class {{ class }} { /** * Create a new policy instance. */ public function __construct() { // } } src/Illuminate/Foundation/Console/stubs/view.stub 0000644 00000000046 15107327037 0016164 0 ustar 00 <div> <!-- {{ quote }} --> </div> src/Illuminate/Foundation/Console/stubs/pest.stub 0000644 00000000155 15107327037 0016166 0 ustar 00 <?php test('example', function () { $response = $this->get('/'); $response->assertStatus(200); }); src/Illuminate/Foundation/Console/stubs/rule.stub 0000644 00000000622 15107327037 0016161 0 ustar 00 <?php namespace {{ namespace }}; use Closure; use Illuminate\Contracts\Validation\ValidationRule; class {{ class }} implements ValidationRule { /** * Run the validation rule. * * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail */ public function validate(string $attribute, mixed $value, Closure $fail): void { // } } src/Illuminate/Foundation/Console/stubs/exception-render-report.stub 0000644 00000000615 15107327037 0022000 0 ustar 00 <?php namespace {{ namespace }}; use Exception; use Illuminate\Http\Request; use Illuminate\Http\Response; class {{ class }} extends Exception { /** * Report the exception. */ public function report(): void { // } /** * Render the exception as an HTTP response. */ public function render(Request $request): Response { // } } src/Illuminate/Foundation/Console/stubs/listener-duck.stub 0000644 00000000553 15107327037 0017766 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; class {{ class }} { /** * Create the event listener. */ public function __construct() { // } /** * Handle the event. */ public function handle(object $event): void { // } } src/Illuminate/Foundation/Console/stubs/job.queued.stub 0000644 00000001134 15107327037 0017252 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; class {{ class }} implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * Create a new job instance. */ public function __construct() { // } /** * Execute the job. */ public function handle(): void { // } } src/Illuminate/Foundation/Console/stubs/view.pest.stub 0000644 00000000210 15107327037 0017127 0 ustar 00 <?php it('can render', function () { $contents = $this->view('{{ name }}', [ // ]); $contents->assertSee(''); }); src/Illuminate/Foundation/Console/stubs/view.test.stub 0000644 00000000472 15107327037 0017145 0 ustar 00 <?php namespace {{ namespace }}; use Tests\TestCase; class {{ class }} extends TestCase { /** * A basic view test example. */ public function test_it_can_render(): void { $contents = $this->view('{{ name }}', [ // ]); $contents->assertSee(''); } } src/Illuminate/Foundation/Console/stubs/pest.unit.stub 0000644 00000000107 15107327037 0017141 0 ustar 00 <?php test('example', function () { expect(true)->toBeTrue(); }); src/Illuminate/Foundation/Console/stubs/observer.plain.stub 0000644 00000000100 15107327037 0020132 0 ustar 00 <?php namespace {{ namespace }}; class {{ class }} { // } src/Illuminate/Foundation/Console/stubs/view-component.stub 0000644 00000000664 15107327037 0020172 0 ustar 00 <?php namespace {{ namespace }}; use Closure; use Illuminate\View\Component; use Illuminate\Contracts\View\View; class {{ class }} extends Component { /** * Create a new component instance. */ public function __construct() { // } /** * Get the view / contents that represent the component. */ public function render(): View|Closure|string { return {{ view }}; } } src/Illuminate/Foundation/Console/stubs/policy.stub 0000644 00000002523 15107327037 0016513 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Auth\Access\Response; use {{ namespacedModel }}; use {{ namespacedUserModel }}; class {{ class }} { /** * Determine whether the user can view any models. */ public function viewAny({{ user }} $user): bool { // } /** * Determine whether the user can view the model. */ public function view({{ user }} $user, {{ model }} ${{ modelVariable }}): bool { // } /** * Determine whether the user can create models. */ public function create({{ user }} $user): bool { // } /** * Determine whether the user can update the model. */ public function update({{ user }} $user, {{ model }} ${{ modelVariable }}): bool { // } /** * Determine whether the user can delete the model. */ public function delete({{ user }} $user, {{ model }} ${{ modelVariable }}): bool { // } /** * Determine whether the user can restore the model. */ public function restore({{ user }} $user, {{ model }} ${{ modelVariable }}): bool { // } /** * Determine whether the user can permanently delete the model. */ public function forceDelete({{ user }} $user, {{ model }} ${{ modelVariable }}): bool { // } } src/Illuminate/Foundation/Console/DownCommand.php 0000644 00000010131 15107327037 0016066 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use App\Http\Middleware\PreventRequestsDuringMaintenance; use Exception; use Illuminate\Console\Command; use Illuminate\Foundation\Events\MaintenanceModeEnabled; use Illuminate\Foundation\Exceptions\RegisterErrorViewPaths; use Symfony\Component\Console\Attribute\AsCommand; use Throwable; #[AsCommand(name: 'down')] class DownCommand extends Command { /** * The console command signature. * * @var string */ protected $signature = 'down {--redirect= : The path that users should be redirected to} {--render= : The view that should be prerendered for display during maintenance mode} {--retry= : The number of seconds after which the request may be retried} {--refresh= : The number of seconds after which the browser may refresh} {--secret= : The secret phrase that may be used to bypass maintenance mode} {--status=503 : The status code that should be used when returning the maintenance mode response}'; /** * The console command description. * * @var string */ protected $description = 'Put the application into maintenance / demo mode'; /** * Execute the console command. * * @return int */ public function handle() { try { if ($this->laravel->maintenanceMode()->active()) { $this->components->info('Application is already down.'); return 0; } $this->laravel->maintenanceMode()->activate($this->getDownFilePayload()); file_put_contents( storage_path('framework/maintenance.php'), file_get_contents(__DIR__.'/stubs/maintenance-mode.stub') ); $this->laravel->get('events')->dispatch(new MaintenanceModeEnabled()); $this->components->info('Application is now in maintenance mode.'); } catch (Exception $e) { $this->components->error(sprintf( 'Failed to enter maintenance mode: %s.', $e->getMessage(), )); return 1; } } /** * Get the payload to be placed in the "down" file. * * @return array */ protected function getDownFilePayload() { return [ 'except' => $this->excludedPaths(), 'redirect' => $this->redirectPath(), 'retry' => $this->getRetryTime(), 'refresh' => $this->option('refresh'), 'secret' => $this->option('secret'), 'status' => (int) $this->option('status', 503), 'template' => $this->option('render') ? $this->prerenderView() : null, ]; } /** * Get the paths that should be excluded from maintenance mode. * * @return array */ protected function excludedPaths() { try { return $this->laravel->make(PreventRequestsDuringMaintenance::class)->getExcludedPaths(); } catch (Throwable) { return []; } } /** * Get the path that users should be redirected to. * * @return string */ protected function redirectPath() { if ($this->option('redirect') && $this->option('redirect') !== '/') { return '/'.trim($this->option('redirect'), '/'); } return $this->option('redirect'); } /** * Prerender the specified view so that it can be rendered even before loading Composer. * * @return string */ protected function prerenderView() { (new RegisterErrorViewPaths)(); return view($this->option('render'), [ 'retryAfter' => $this->option('retry'), ])->render(); } /** * Get the number of seconds the client should wait before retrying their request. * * @return int|null */ protected function getRetryTime() { $retry = $this->option('retry'); return is_numeric($retry) && $retry > 0 ? (int) $retry : null; } } src/Illuminate/Foundation/Exceptions/Handler.php 0000644 00000063166 15107327037 0015774 0 ustar 00 <?php namespace Illuminate\Foundation\Exceptions; use Closure; use Exception; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\AuthenticationException; use Illuminate\Cache\RateLimiter; use Illuminate\Cache\RateLimiting\Limit; use Illuminate\Cache\RateLimiting\Unlimited; use Illuminate\Console\View\Components\BulletList; use Illuminate\Console\View\Components\Error; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract; use Illuminate\Contracts\Foundation\ExceptionRenderer; use Illuminate\Contracts\Support\Responsable; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\MultipleRecordsFoundException; use Illuminate\Database\RecordsNotFoundException; use Illuminate\Http\Exceptions\HttpResponseException; use Illuminate\Http\JsonResponse; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Response; use Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException; use Illuminate\Routing\Router; use Illuminate\Session\TokenMismatchException; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Lottery; use Illuminate\Support\Reflector; use Illuminate\Support\Traits\ReflectsClosures; use Illuminate\Support\ViewErrorBag; use Illuminate\Validation\ValidationException; use InvalidArgumentException; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; use Symfony\Component\Console\Application as ConsoleApplication; use Symfony\Component\Console\Exception\CommandNotFoundException; use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer; use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException; use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirectResponse; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Throwable; use WeakMap; class Handler implements ExceptionHandlerContract { use ReflectsClosures; /** * The container implementation. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * A list of the exception types that are not reported. * * @var array<int, class-string<\Throwable>> */ protected $dontReport = []; /** * The callbacks that should be used during reporting. * * @var \Illuminate\Foundation\Exceptions\ReportableHandler[] */ protected $reportCallbacks = []; /** * A map of exceptions with their corresponding custom log levels. * * @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*> */ protected $levels = []; /** * The callbacks that should be used during rendering. * * @var \Closure[] */ protected $renderCallbacks = []; /** * The registered exception mappings. * * @var array<string, \Closure> */ protected $exceptionMap = []; /** * Indicates that throttled keys should be hashed. * * @var bool */ protected $hashThrottleKeys = true; /** * A list of the internal exception types that should not be reported. * * @var array<int, class-string<\Throwable>> */ protected $internalDontReport = [ AuthenticationException::class, AuthorizationException::class, BackedEnumCaseNotFoundException::class, HttpException::class, HttpResponseException::class, ModelNotFoundException::class, MultipleRecordsFoundException::class, RecordsNotFoundException::class, SuspiciousOperationException::class, TokenMismatchException::class, ValidationException::class, ]; /** * A list of the inputs that are never flashed for validation exceptions. * * @var array<int, string> */ protected $dontFlash = [ 'current_password', 'password', 'password_confirmation', ]; /** * Indicates that an exception instance should only be reported once. * * @var bool */ protected $withoutDuplicates = false; /** * The already reported exception map. * * @var \WeakMap */ protected $reportedExceptionMap; /** * Create a new exception handler instance. * * @param \Illuminate\Contracts\Container\Container $container * @return void */ public function __construct(Container $container) { $this->container = $container; $this->reportedExceptionMap = new WeakMap; $this->register(); } /** * Register the exception handling callbacks for the application. * * @return void */ public function register() { // } /** * Register a reportable callback. * * @param callable $reportUsing * @return \Illuminate\Foundation\Exceptions\ReportableHandler */ public function reportable(callable $reportUsing) { if (! $reportUsing instanceof Closure) { $reportUsing = Closure::fromCallable($reportUsing); } return tap(new ReportableHandler($reportUsing), function ($callback) { $this->reportCallbacks[] = $callback; }); } /** * Register a renderable callback. * * @param callable $renderUsing * @return $this */ public function renderable(callable $renderUsing) { if (! $renderUsing instanceof Closure) { $renderUsing = Closure::fromCallable($renderUsing); } $this->renderCallbacks[] = $renderUsing; return $this; } /** * Register a new exception mapping. * * @param \Closure|string $from * @param \Closure|string|null $to * @return $this * * @throws \InvalidArgumentException */ public function map($from, $to = null) { if (is_string($to)) { $to = fn ($exception) => new $to('', 0, $exception); } if (is_callable($from) && is_null($to)) { $from = $this->firstClosureParameterType($to = $from); } if (! is_string($from) || ! $to instanceof Closure) { throw new InvalidArgumentException('Invalid exception mapping.'); } $this->exceptionMap[$from] = $to; return $this; } /** * Indicate that the given exception type should not be reported. * * @param string $class * @return $this */ public function ignore(string $class) { $this->dontReport[] = $class; return $this; } /** * Set the log level for the given exception type. * * @param class-string<\Throwable> $type * @param \Psr\Log\LogLevel::* $level * @return $this */ public function level($type, $level) { $this->levels[$type] = $level; return $this; } /** * Report or log an exception. * * @param \Throwable $e * @return void * * @throws \Throwable */ public function report(Throwable $e) { $e = $this->mapException($e); if ($this->shouldntReport($e)) { return; } $this->reportThrowable($e); } /** * Reports error based on report method on exception or to logger. * * @param \Throwable $e * @return void * * @throws \Throwable */ protected function reportThrowable(Throwable $e): void { $this->reportedExceptionMap[$e] = true; if (Reflector::isCallable($reportCallable = [$e, 'report']) && $this->container->call($reportCallable) !== false) { return; } foreach ($this->reportCallbacks as $reportCallback) { if ($reportCallback->handles($e) && $reportCallback($e) === false) { return; } } try { $logger = $this->newLogger(); } catch (Exception) { throw $e; } $level = Arr::first( $this->levels, fn ($level, $type) => $e instanceof $type, LogLevel::ERROR ); $context = $this->buildExceptionContext($e); method_exists($logger, $level) ? $logger->{$level}($e->getMessage(), $context) : $logger->log($level, $e->getMessage(), $context); } /** * Determine if the exception should be reported. * * @param \Throwable $e * @return bool */ public function shouldReport(Throwable $e) { return ! $this->shouldntReport($e); } /** * Determine if the exception is in the "do not report" list. * * @param \Throwable $e * @return bool */ protected function shouldntReport(Throwable $e) { if ($this->withoutDuplicates && ($this->reportedExceptionMap[$e] ?? false)) { return true; } $dontReport = array_merge($this->dontReport, $this->internalDontReport); if (! is_null(Arr::first($dontReport, fn ($type) => $e instanceof $type))) { return true; } return rescue(fn () => with($this->throttle($e), function ($throttle) use ($e) { if ($throttle instanceof Unlimited || $throttle === null) { return false; } if ($throttle instanceof Lottery) { return ! $throttle($e); } return ! $this->container->make(RateLimiter::class)->attempt( with($throttle->key ?: 'illuminate:foundation:exceptions:'.$e::class, fn ($key) => $this->hashThrottleKeys ? md5($key) : $key), $throttle->maxAttempts, fn () => true, 60 * $throttle->decayMinutes ); }), rescue: false, report: false); } /** * Throttle the given exception. * * @param \Throwable $e * @return \Illuminate\Support\Lottery|\Illuminate\Cache\RateLimiting\Limit|null */ protected function throttle(Throwable $e) { return Limit::none(); } /** * Remove the given exception class from the list of exceptions that should be ignored. * * @param string $exception * @return $this */ public function stopIgnoring(string $exception) { $this->dontReport = collect($this->dontReport) ->reject(fn ($ignored) => $ignored === $exception)->values()->all(); $this->internalDontReport = collect($this->internalDontReport) ->reject(fn ($ignored) => $ignored === $exception)->values()->all(); return $this; } /** * Create the context array for logging the given exception. * * @param \Throwable $e * @return array */ protected function buildExceptionContext(Throwable $e) { return array_merge( $this->exceptionContext($e), $this->context(), ['exception' => $e] ); } /** * Get the default exception context variables for logging. * * @param \Throwable $e * @return array */ protected function exceptionContext(Throwable $e) { if (method_exists($e, 'context')) { return $e->context(); } return []; } /** * Get the default context variables for logging. * * @return array */ protected function context() { try { return array_filter([ 'userId' => Auth::id(), ]); } catch (Throwable) { return []; } } /** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Throwable $e * @return \Symfony\Component\HttpFoundation\Response * * @throws \Throwable */ public function render($request, Throwable $e) { $e = $this->mapException($e); if (method_exists($e, 'render') && $response = $e->render($request)) { return Router::toResponse($request, $response); } if ($e instanceof Responsable) { return $e->toResponse($request); } $e = $this->prepareException($e); if ($response = $this->renderViaCallbacks($request, $e)) { return $response; } return match (true) { $e instanceof HttpResponseException => $e->getResponse(), $e instanceof AuthenticationException => $this->unauthenticated($request, $e), $e instanceof ValidationException => $this->convertValidationExceptionToResponse($e, $request), default => $this->renderExceptionResponse($request, $e), }; } /** * Prepare exception for rendering. * * @param \Throwable $e * @return \Throwable */ protected function prepareException(Throwable $e) { return match (true) { $e instanceof BackedEnumCaseNotFoundException => new NotFoundHttpException($e->getMessage(), $e), $e instanceof ModelNotFoundException => new NotFoundHttpException($e->getMessage(), $e), $e instanceof AuthorizationException && $e->hasStatus() => new HttpException( $e->status(), $e->response()?->message() ?: (Response::$statusTexts[$e->status()] ?? 'Whoops, looks like something went wrong.'), $e ), $e instanceof AuthorizationException && ! $e->hasStatus() => new AccessDeniedHttpException($e->getMessage(), $e), $e instanceof TokenMismatchException => new HttpException(419, $e->getMessage(), $e), $e instanceof SuspiciousOperationException => new NotFoundHttpException('Bad hostname provided.', $e), $e instanceof RecordsNotFoundException => new NotFoundHttpException('Not found.', $e), default => $e, }; } /** * Map the exception using a registered mapper if possible. * * @param \Throwable $e * @return \Throwable */ protected function mapException(Throwable $e) { if (method_exists($e, 'getInnerException') && ($inner = $e->getInnerException()) instanceof Throwable) { return $inner; } foreach ($this->exceptionMap as $class => $mapper) { if (is_a($e, $class)) { return $mapper($e); } } return $e; } /** * Try to render a response from request and exception via render callbacks. * * @param \Illuminate\Http\Request $request * @param \Throwable $e * @return mixed * * @throws \ReflectionException */ protected function renderViaCallbacks($request, Throwable $e) { foreach ($this->renderCallbacks as $renderCallback) { foreach ($this->firstClosureParameterTypes($renderCallback) as $type) { if (is_a($e, $type)) { $response = $renderCallback($e, $request); if (! is_null($response)) { return $response; } } } } } /** * Render a default exception response if any. * * @param \Illuminate\Http\Request $request * @param \Throwable $e * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse */ protected function renderExceptionResponse($request, Throwable $e) { return $this->shouldReturnJson($request, $e) ? $this->prepareJsonResponse($request, $e) : $this->prepareResponse($request, $e); } /** * Convert an authentication exception into a response. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Auth\AuthenticationException $exception * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse */ protected function unauthenticated($request, AuthenticationException $exception) { return $this->shouldReturnJson($request, $exception) ? response()->json(['message' => $exception->getMessage()], 401) : redirect()->guest($exception->redirectTo() ?? route('login')); } /** * Create a response object from the given validation exception. * * @param \Illuminate\Validation\ValidationException $e * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response */ protected function convertValidationExceptionToResponse(ValidationException $e, $request) { if ($e->response) { return $e->response; } return $this->shouldReturnJson($request, $e) ? $this->invalidJson($request, $e) : $this->invalid($request, $e); } /** * Convert a validation exception into a response. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Validation\ValidationException $exception * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse */ protected function invalid($request, ValidationException $exception) { return redirect($exception->redirectTo ?? url()->previous()) ->withInput(Arr::except($request->input(), $this->dontFlash)) ->withErrors($exception->errors(), $request->input('_error_bag', $exception->errorBag)); } /** * Convert a validation exception into a JSON response. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Validation\ValidationException $exception * @return \Illuminate\Http\JsonResponse */ protected function invalidJson($request, ValidationException $exception) { return response()->json([ 'message' => $exception->getMessage(), 'errors' => $exception->errors(), ], $exception->status); } /** * Determine if the exception handler response should be JSON. * * @param \Illuminate\Http\Request $request * @param \Throwable $e * @return bool */ protected function shouldReturnJson($request, Throwable $e) { return $request->expectsJson(); } /** * Prepare a response for the given exception. * * @param \Illuminate\Http\Request $request * @param \Throwable $e * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse */ protected function prepareResponse($request, Throwable $e) { if (! $this->isHttpException($e) && config('app.debug')) { return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e)->prepare($request); } if (! $this->isHttpException($e)) { $e = new HttpException(500, $e->getMessage(), $e); } return $this->toIlluminateResponse( $this->renderHttpException($e), $e )->prepare($request); } /** * Create a Symfony response for the given exception. * * @param \Throwable $e * @return \Symfony\Component\HttpFoundation\Response */ protected function convertExceptionToResponse(Throwable $e) { return new SymfonyResponse( $this->renderExceptionContent($e), $this->isHttpException($e) ? $e->getStatusCode() : 500, $this->isHttpException($e) ? $e->getHeaders() : [] ); } /** * Get the response content for the given exception. * * @param \Throwable $e * @return string */ protected function renderExceptionContent(Throwable $e) { try { return config('app.debug') && app()->has(ExceptionRenderer::class) ? $this->renderExceptionWithCustomRenderer($e) : $this->renderExceptionWithSymfony($e, config('app.debug')); } catch (Throwable $e) { return $this->renderExceptionWithSymfony($e, config('app.debug')); } } /** * Render an exception to a string using the registered `ExceptionRenderer`. * * @param \Throwable $e * @return string */ protected function renderExceptionWithCustomRenderer(Throwable $e) { return app(ExceptionRenderer::class)->render($e); } /** * Render an exception to a string using Symfony. * * @param \Throwable $e * @param bool $debug * @return string */ protected function renderExceptionWithSymfony(Throwable $e, $debug) { $renderer = new HtmlErrorRenderer($debug); return $renderer->render($e)->getAsString(); } /** * Render the given HttpException. * * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e * @return \Symfony\Component\HttpFoundation\Response */ protected function renderHttpException(HttpExceptionInterface $e) { $this->registerErrorViewPaths(); if ($view = $this->getHttpExceptionView($e)) { try { return response()->view($view, [ 'errors' => new ViewErrorBag, 'exception' => $e, ], $e->getStatusCode(), $e->getHeaders()); } catch (Throwable $t) { config('app.debug') && throw $t; $this->report($t); } } return $this->convertExceptionToResponse($e); } /** * Register the error template hint paths. * * @return void */ protected function registerErrorViewPaths() { (new RegisterErrorViewPaths)(); } /** * Get the view used to render HTTP exceptions. * * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e * @return string|null */ protected function getHttpExceptionView(HttpExceptionInterface $e) { $view = 'errors::'.$e->getStatusCode(); if (view()->exists($view)) { return $view; } $view = substr($view, 0, -2).'xx'; if (view()->exists($view)) { return $view; } return null; } /** * Map the given exception into an Illuminate response. * * @param \Symfony\Component\HttpFoundation\Response $response * @param \Throwable $e * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse */ protected function toIlluminateResponse($response, Throwable $e) { if ($response instanceof SymfonyRedirectResponse) { $response = new RedirectResponse( $response->getTargetUrl(), $response->getStatusCode(), $response->headers->all() ); } else { $response = new Response( $response->getContent(), $response->getStatusCode(), $response->headers->all() ); } return $response->withException($e); } /** * Prepare a JSON response for the given exception. * * @param \Illuminate\Http\Request $request * @param \Throwable $e * @return \Illuminate\Http\JsonResponse */ protected function prepareJsonResponse($request, Throwable $e) { return new JsonResponse( $this->convertExceptionToArray($e), $this->isHttpException($e) ? $e->getStatusCode() : 500, $this->isHttpException($e) ? $e->getHeaders() : [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ); } /** * Convert the given exception to an array. * * @param \Throwable $e * @return array */ protected function convertExceptionToArray(Throwable $e) { return config('app.debug') ? [ 'message' => $e->getMessage(), 'exception' => get_class($e), 'file' => $e->getFile(), 'line' => $e->getLine(), 'trace' => collect($e->getTrace())->map(fn ($trace) => Arr::except($trace, ['args']))->all(), ] : [ 'message' => $this->isHttpException($e) ? $e->getMessage() : 'Server Error', ]; } /** * Render an exception to the console. * * @param \Symfony\Component\Console\Output\OutputInterface $output * @param \Throwable $e * @return void * * @internal This method is not meant to be used or overwritten outside the framework. */ public function renderForConsole($output, Throwable $e) { if ($e instanceof CommandNotFoundException) { $message = str($e->getMessage())->explode('.')->first(); if (! empty($alternatives = $e->getAlternatives())) { $message .= '. Did you mean one of these?'; with(new Error($output))->render($message); with(new BulletList($output))->render($e->getAlternatives()); $output->writeln(''); } else { with(new Error($output))->render($message); } return; } (new ConsoleApplication)->renderThrowable($e, $output); } /** * Do not report duplicate exceptions. * * @return $this */ public function dontReportDuplicates() { $this->withoutDuplicates = true; return $this; } /** * Determine if the given exception is an HTTP exception. * * @param \Throwable $e * @return bool */ protected function isHttpException(Throwable $e) { return $e instanceof HttpExceptionInterface; } /** * Create a new logger instance. * * @return \Psr\Log\LoggerInterface */ protected function newLogger() { return $this->container->make(LoggerInterface::class); } } src/Illuminate/Foundation/Exceptions/ReportableHandler.php 0000644 00000003037 15107327037 0020003 0 ustar 00 <?php namespace Illuminate\Foundation\Exceptions; use Illuminate\Support\Traits\ReflectsClosures; use Throwable; class ReportableHandler { use ReflectsClosures; /** * The underlying callback. * * @var callable */ protected $callback; /** * Indicates if reporting should stop after invoking this handler. * * @var bool */ protected $shouldStop = false; /** * Create a new reportable handler instance. * * @param callable $callback * @return void */ public function __construct(callable $callback) { $this->callback = $callback; } /** * Invoke the handler. * * @param \Throwable $e * @return bool */ public function __invoke(Throwable $e) { $result = call_user_func($this->callback, $e); if ($result === false) { return false; } return ! $this->shouldStop; } /** * Determine if the callback handles the given exception. * * @param \Throwable $e * @return bool */ public function handles(Throwable $e) { foreach ($this->firstClosureParameterTypes($this->callback) as $type) { if (is_a($e, $type)) { return true; } } return false; } /** * Indicate that report handling should stop after invoking this callback. * * @return $this */ public function stop() { $this->shouldStop = true; return $this; } } src/Illuminate/Foundation/Exceptions/RegisterErrorViewPaths.php 0000644 00000000647 15107327037 0021043 0 ustar 00 <?php namespace Illuminate\Foundation\Exceptions; use Illuminate\Support\Facades\View; class RegisterErrorViewPaths { /** * Register the error view paths. * * @return void */ public function __invoke() { View::replaceNamespace('errors', collect(config('view.paths'))->map(function ($path) { return "{$path}/errors"; })->push(__DIR__.'/views')->all()); } } src/Illuminate/Foundation/Exceptions/Whoops/WhoopsExceptionRenderer.php 0000644 00000001523 15107327037 0022510 0 ustar 00 <?php namespace Illuminate\Foundation\Exceptions\Whoops; use Illuminate\Contracts\Foundation\ExceptionRenderer; use Whoops\Run as Whoops; use function tap; class WhoopsExceptionRenderer implements ExceptionRenderer { /** * Renders the given exception as HTML. * * @param \Throwable $throwable * @return string */ public function render($throwable) { return tap(new Whoops, function ($whoops) { $whoops->appendHandler($this->whoopsHandler()); $whoops->writeToOutput(false); $whoops->allowQuit(false); })->handleException($throwable); } /** * Get the Whoops handler for the application. * * @return \Whoops\Handler\Handler */ protected function whoopsHandler() { return (new WhoopsHandler)->forDebug(); } } src/Illuminate/Foundation/Exceptions/Whoops/WhoopsHandler.php 0000644 00000004063 15107327037 0020442 0 ustar 00 <?php namespace Illuminate\Foundation\Exceptions\Whoops; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Arr; use Whoops\Handler\PrettyPageHandler; class WhoopsHandler { /** * Create a new Whoops handler for debug mode. * * @return \Whoops\Handler\PrettyPageHandler */ public function forDebug() { return tap(new PrettyPageHandler, function ($handler) { $handler->handleUnconditionally(true); $this->registerApplicationPaths($handler) ->registerBlacklist($handler) ->registerEditor($handler); }); } /** * Register the application paths with the handler. * * @param \Whoops\Handler\PrettyPageHandler $handler * @return $this */ protected function registerApplicationPaths($handler) { $handler->setApplicationPaths( array_flip($this->directoriesExceptVendor()) ); return $this; } /** * Get the application paths except for the "vendor" directory. * * @return array */ protected function directoriesExceptVendor() { return Arr::except( array_flip((new Filesystem)->directories(base_path())), [base_path('vendor')] ); } /** * Register the blacklist with the handler. * * @param \Whoops\Handler\PrettyPageHandler $handler * @return $this */ protected function registerBlacklist($handler) { foreach (config('app.debug_blacklist', config('app.debug_hide', [])) as $key => $secrets) { foreach ($secrets as $secret) { $handler->blacklist($key, $secret); } } return $this; } /** * Register the editor with the handler. * * @param \Whoops\Handler\PrettyPageHandler $handler * @return $this */ protected function registerEditor($handler) { if (config('app.editor', false)) { $handler->setEditor(config('app.editor')); } return $this; } } src/Illuminate/Foundation/Exceptions/views/500.blade.php 0000644 00000000203 15107327037 0017105 0 ustar 00 @extends('errors::minimal') @section('title', __('Server Error')) @section('code', '500') @section('message', __('Server Error')) src/Illuminate/Foundation/Exceptions/views/429.blade.php 0000644 00000000215 15107327037 0017122 0 ustar 00 @extends('errors::minimal') @section('title', __('Too Many Requests')) @section('code', '429') @section('message', __('Too Many Requests')) src/Illuminate/Foundation/Exceptions/views/layout.blade.php 0000644 00000002633 15107327037 0020127 0 ustar 00 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>@yield('title')</title> <!-- Styles --> <style> html, body { background-color: #fff; color: #636b6f; font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-weight: 100; height: 100vh; margin: 0; } .full-height { height: 100vh; } .flex-center { align-items: center; display: flex; justify-content: center; } .position-ref { position: relative; } .content { text-align: center; } .title { font-size: 36px; padding: 20px; } </style> </head> <body> <div class="flex-center position-ref full-height"> <div class="content"> <div class="title"> @yield('message') </div> </div> </div> </body> </html> src/Illuminate/Foundation/Exceptions/views/401.blade.php 0000644 00000000203 15107327037 0017105 0 ustar 00 @extends('errors::minimal') @section('title', __('Unauthorized')) @section('code', '401') @section('message', __('Unauthorized')) src/Illuminate/Foundation/Exceptions/views/minimal.blade.php 0000644 00000014746 15107327037 0020250 0 ustar 00 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>@yield('title')</title> <style> /*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */html{line-height:1.15;-webkit-text-size-adjust:100%}body{margin:0}a{background-color:transparent}code{font-family:monospace,monospace;font-size:1em}[hidden]{display:none}html{font-family:system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;line-height:1.5}*,:after,:before{box-sizing:border-box;border:0 solid #e2e8f0}a{color:inherit;text-decoration:inherit}code{font-family:Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace}svg,video{display:block;vertical-align:middle}video{max-width:100%;height:auto}.bg-white{--bg-opacity:1;background-color:#fff;background-color:rgba(255,255,255,var(--bg-opacity))}.bg-gray-100{--bg-opacity:1;background-color:#f7fafc;background-color:rgba(247,250,252,var(--bg-opacity))}.border-gray-200{--border-opacity:1;border-color:#edf2f7;border-color:rgba(237,242,247,var(--border-opacity))}.border-gray-400{--border-opacity:1;border-color:#cbd5e0;border-color:rgba(203,213,224,var(--border-opacity))}.border-t{border-top-width:1px}.border-r{border-right-width:1px}.flex{display:flex}.grid{display:grid}.hidden{display:none}.items-center{align-items:center}.justify-center{justify-content:center}.font-semibold{font-weight:600}.h-5{height:1.25rem}.h-8{height:2rem}.h-16{height:4rem}.text-sm{font-size:.875rem}.text-lg{font-size:1.125rem}.leading-7{line-height:1.75rem}.mx-auto{margin-left:auto;margin-right:auto}.ml-1{margin-left:.25rem}.mt-2{margin-top:.5rem}.mr-2{margin-right:.5rem}.ml-2{margin-left:.5rem}.mt-4{margin-top:1rem}.ml-4{margin-left:1rem}.mt-8{margin-top:2rem}.ml-12{margin-left:3rem}.-mt-px{margin-top:-1px}.max-w-xl{max-width:36rem}.max-w-6xl{max-width:72rem}.min-h-screen{min-height:100vh}.overflow-hidden{overflow:hidden}.p-6{padding:1.5rem}.py-4{padding-top:1rem;padding-bottom:1rem}.px-4{padding-left:1rem;padding-right:1rem}.px-6{padding-left:1.5rem;padding-right:1.5rem}.pt-8{padding-top:2rem}.fixed{position:fixed}.relative{position:relative}.top-0{top:0}.right-0{right:0}.shadow{box-shadow:0 1px 3px 0 rgba(0,0,0,.1),0 1px 2px 0 rgba(0,0,0,.06)}.text-center{text-align:center}.text-gray-200{--text-opacity:1;color:#edf2f7;color:rgba(237,242,247,var(--text-opacity))}.text-gray-300{--text-opacity:1;color:#e2e8f0;color:rgba(226,232,240,var(--text-opacity))}.text-gray-400{--text-opacity:1;color:#cbd5e0;color:rgba(203,213,224,var(--text-opacity))}.text-gray-500{--text-opacity:1;color:#a0aec0;color:rgba(160,174,192,var(--text-opacity))}.text-gray-600{--text-opacity:1;color:#718096;color:rgba(113,128,150,var(--text-opacity))}.text-gray-700{--text-opacity:1;color:#4a5568;color:rgba(74,85,104,var(--text-opacity))}.text-gray-900{--text-opacity:1;color:#1a202c;color:rgba(26,32,44,var(--text-opacity))}.uppercase{text-transform:uppercase}.underline{text-decoration:underline}.antialiased{-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.tracking-wider{letter-spacing:.05em}.w-5{width:1.25rem}.w-8{width:2rem}.w-auto{width:auto}.grid-cols-1{grid-template-columns:repeat(1,minmax(0,1fr))}@-webkit-keyframes spin{0%{transform:rotate(0deg)}to{transform:rotate(1turn)}}@keyframes spin{0%{transform:rotate(0deg)}to{transform:rotate(1turn)}}@-webkit-keyframes ping{0%{transform:scale(1);opacity:1}75%,to{transform:scale(2);opacity:0}}@keyframes ping{0%{transform:scale(1);opacity:1}75%,to{transform:scale(2);opacity:0}}@-webkit-keyframes pulse{0%,to{opacity:1}50%{opacity:.5}}@keyframes pulse{0%,to{opacity:1}50%{opacity:.5}}@-webkit-keyframes bounce{0%,to{transform:translateY(-25%);-webkit-animation-timing-function:cubic-bezier(.8,0,1,1);animation-timing-function:cubic-bezier(.8,0,1,1)}50%{transform:translateY(0);-webkit-animation-timing-function:cubic-bezier(0,0,.2,1);animation-timing-function:cubic-bezier(0,0,.2,1)}}@keyframes bounce{0%,to{transform:translateY(-25%);-webkit-animation-timing-function:cubic-bezier(.8,0,1,1);animation-timing-function:cubic-bezier(.8,0,1,1)}50%{transform:translateY(0);-webkit-animation-timing-function:cubic-bezier(0,0,.2,1);animation-timing-function:cubic-bezier(0,0,.2,1)}}@media (min-width:640px){.sm\:rounded-lg{border-radius:.5rem}.sm\:block{display:block}.sm\:items-center{align-items:center}.sm\:justify-start{justify-content:flex-start}.sm\:justify-between{justify-content:space-between}.sm\:h-20{height:5rem}.sm\:ml-0{margin-left:0}.sm\:px-6{padding-left:1.5rem;padding-right:1.5rem}.sm\:pt-0{padding-top:0}.sm\:text-left{text-align:left}.sm\:text-right{text-align:right}}@media (min-width:768px){.md\:border-t-0{border-top-width:0}.md\:border-l{border-left-width:1px}.md\:grid-cols-2{grid-template-columns:repeat(2,minmax(0,1fr))}}@media (min-width:1024px){.lg\:px-8{padding-left:2rem;padding-right:2rem}}@media (prefers-color-scheme:dark){.dark\:bg-gray-800{--bg-opacity:1;background-color:#2d3748;background-color:rgba(45,55,72,var(--bg-opacity))}.dark\:bg-gray-900{--bg-opacity:1;background-color:#1a202c;background-color:rgba(26,32,44,var(--bg-opacity))}.dark\:border-gray-700{--border-opacity:1;border-color:#4a5568;border-color:rgba(74,85,104,var(--border-opacity))}.dark\:text-white{--text-opacity:1;color:#fff;color:rgba(255,255,255,var(--text-opacity))}.dark\:text-gray-400{--text-opacity:1;color:#cbd5e0;color:rgba(203,213,224,var(--text-opacity))}} </style> <style> body { font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; } </style> </head> <body class="antialiased"> <div class="relative flex items-top justify-center min-h-screen bg-gray-100 dark:bg-gray-900 sm:items-center sm:pt-0"> <div class="max-w-xl mx-auto sm:px-6 lg:px-8"> <div class="flex items-center pt-8 sm:justify-start sm:pt-0"> <div class="px-4 text-lg text-gray-500 border-r border-gray-400 tracking-wider"> @yield('code') </div> <div class="ml-4 text-lg text-gray-500 uppercase tracking-wider"> @yield('message') </div> </div> </div> </div> </body> </html> src/Illuminate/Foundation/Exceptions/views/403.blade.php 0000644 00000000231 15107327037 0017110 0 ustar 00 @extends('errors::minimal') @section('title', __('Forbidden')) @section('code', '403') @section('message', __($exception->getMessage() ?: 'Forbidden')) src/Illuminate/Foundation/Exceptions/views/404.blade.php 0000644 00000000175 15107327037 0017120 0 ustar 00 @extends('errors::minimal') @section('title', __('Not Found')) @section('code', '404') @section('message', __('Not Found')) src/Illuminate/Foundation/Exceptions/views/503.blade.php 0000644 00000000221 15107327037 0017110 0 ustar 00 @extends('errors::minimal') @section('title', __('Service Unavailable')) @section('code', '503') @section('message', __('Service Unavailable')) src/Illuminate/Foundation/Exceptions/views/402.blade.php 0000644 00000000213 15107327037 0017107 0 ustar 00 @extends('errors::minimal') @section('title', __('Payment Required')) @section('code', '402') @section('message', __('Payment Required')) src/Illuminate/Foundation/Exceptions/views/419.blade.php 0000644 00000000203 15107327037 0017116 0 ustar 00 @extends('errors::minimal') @section('title', __('Page Expired')) @section('code', '419') @section('message', __('Page Expired')) src/Illuminate/Foundation/Vite.php 0000644 00000050526 15107327037 0013201 0 ustar 00 <?php namespace Illuminate\Foundation; use Exception; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\Collection; use Illuminate\Support\HtmlString; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; class Vite implements Htmlable { use Macroable; /** * The Content Security Policy nonce to apply to all generated tags. * * @var string|null */ protected $nonce; /** * The key to check for integrity hashes within the manifest. * * @var string|false */ protected $integrityKey = 'integrity'; /** * The configured entry points. * * @var array */ protected $entryPoints = []; /** * The path to the "hot" file. * * @var string|null */ protected $hotFile; /** * The path to the build directory. * * @var string */ protected $buildDirectory = 'build'; /** * The name of the manifest file. * * @var string */ protected $manifestFilename = 'manifest.json'; /** * The script tag attributes resolvers. * * @var array */ protected $scriptTagAttributesResolvers = []; /** * The style tag attributes resolvers. * * @var array */ protected $styleTagAttributesResolvers = []; /** * The preload tag attributes resolvers. * * @var array */ protected $preloadTagAttributesResolvers = []; /** * The preloaded assets. * * @var array */ protected $preloadedAssets = []; /** * The cached manifest files. * * @var array */ protected static $manifests = []; /** * Get the preloaded assets. * * @return array */ public function preloadedAssets() { return $this->preloadedAssets; } /** * Get the Content Security Policy nonce applied to all generated tags. * * @return string|null */ public function cspNonce() { return $this->nonce; } /** * Generate or set a Content Security Policy nonce to apply to all generated tags. * * @param string|null $nonce * @return string */ public function useCspNonce($nonce = null) { return $this->nonce = $nonce ?? Str::random(40); } /** * Use the given key to detect integrity hashes in the manifest. * * @param string|false $key * @return $this */ public function useIntegrityKey($key) { $this->integrityKey = $key; return $this; } /** * Set the Vite entry points. * * @param array $entryPoints * @return $this */ public function withEntryPoints($entryPoints) { $this->entryPoints = $entryPoints; return $this; } /** * Set the filename for the manifest file. * * @param string $filename * @return $this */ public function useManifestFilename($filename) { $this->manifestFilename = $filename; return $this; } /** * Get the Vite "hot" file path. * * @return string */ public function hotFile() { return $this->hotFile ?? public_path('/hot'); } /** * Set the Vite "hot" file path. * * @param string $path * @return $this */ public function useHotFile($path) { $this->hotFile = $path; return $this; } /** * Set the Vite build directory. * * @param string $path * @return $this */ public function useBuildDirectory($path) { $this->buildDirectory = $path; return $this; } /** * Use the given callback to resolve attributes for script tags. * * @param (callable(string, string, ?array, ?array): array)|array $attributes * @return $this */ public function useScriptTagAttributes($attributes) { if (! is_callable($attributes)) { $attributes = fn () => $attributes; } $this->scriptTagAttributesResolvers[] = $attributes; return $this; } /** * Use the given callback to resolve attributes for style tags. * * @param (callable(string, string, ?array, ?array): array)|array $attributes * @return $this */ public function useStyleTagAttributes($attributes) { if (! is_callable($attributes)) { $attributes = fn () => $attributes; } $this->styleTagAttributesResolvers[] = $attributes; return $this; } /** * Use the given callback to resolve attributes for preload tags. * * @param (callable(string, string, ?array, ?array): (array|false))|array|false $attributes * @return $this */ public function usePreloadTagAttributes($attributes) { if (! is_callable($attributes)) { $attributes = fn () => $attributes; } $this->preloadTagAttributesResolvers[] = $attributes; return $this; } /** * Generate Vite tags for an entrypoint. * * @param string|string[] $entrypoints * @param string|null $buildDirectory * @return \Illuminate\Support\HtmlString * * @throws \Exception */ public function __invoke($entrypoints, $buildDirectory = null) { $entrypoints = collect($entrypoints); $buildDirectory ??= $this->buildDirectory; if ($this->isRunningHot()) { return new HtmlString( $entrypoints ->prepend('@vite/client') ->map(fn ($entrypoint) => $this->makeTagForChunk($entrypoint, $this->hotAsset($entrypoint), null, null)) ->join('') ); } $manifest = $this->manifest($buildDirectory); $tags = collect(); $preloads = collect(); foreach ($entrypoints as $entrypoint) { $chunk = $this->chunk($manifest, $entrypoint); $preloads->push([ $chunk['src'], $this->assetPath("{$buildDirectory}/{$chunk['file']}"), $chunk, $manifest, ]); foreach ($chunk['imports'] ?? [] as $import) { $preloads->push([ $import, $this->assetPath("{$buildDirectory}/{$manifest[$import]['file']}"), $manifest[$import], $manifest, ]); foreach ($manifest[$import]['css'] ?? [] as $css) { $partialManifest = Collection::make($manifest)->where('file', $css); $preloads->push([ $partialManifest->keys()->first(), $this->assetPath("{$buildDirectory}/{$css}"), $partialManifest->first(), $manifest, ]); $tags->push($this->makeTagForChunk( $partialManifest->keys()->first(), $this->assetPath("{$buildDirectory}/{$css}"), $partialManifest->first(), $manifest )); } } $tags->push($this->makeTagForChunk( $entrypoint, $this->assetPath("{$buildDirectory}/{$chunk['file']}"), $chunk, $manifest )); foreach ($chunk['css'] ?? [] as $css) { $partialManifest = Collection::make($manifest)->where('file', $css); $preloads->push([ $partialManifest->keys()->first(), $this->assetPath("{$buildDirectory}/{$css}"), $partialManifest->first(), $manifest, ]); $tags->push($this->makeTagForChunk( $partialManifest->keys()->first(), $this->assetPath("{$buildDirectory}/{$css}"), $partialManifest->first(), $manifest )); } } [$stylesheets, $scripts] = $tags->unique()->partition(fn ($tag) => str_starts_with($tag, '<link')); $preloads = $preloads->unique() ->sortByDesc(fn ($args) => $this->isCssPath($args[1])) ->map(fn ($args) => $this->makePreloadTagForChunk(...$args)); return new HtmlString($preloads->join('').$stylesheets->join('').$scripts->join('')); } /** * Make tag for the given chunk. * * @param string $src * @param string $url * @param array|null $chunk * @param array|null $manifest * @return string */ protected function makeTagForChunk($src, $url, $chunk, $manifest) { if ( $this->nonce === null && $this->integrityKey !== false && ! array_key_exists($this->integrityKey, $chunk ?? []) && $this->scriptTagAttributesResolvers === [] && $this->styleTagAttributesResolvers === []) { return $this->makeTag($url); } if ($this->isCssPath($url)) { return $this->makeStylesheetTagWithAttributes( $url, $this->resolveStylesheetTagAttributes($src, $url, $chunk, $manifest) ); } return $this->makeScriptTagWithAttributes( $url, $this->resolveScriptTagAttributes($src, $url, $chunk, $manifest) ); } /** * Make a preload tag for the given chunk. * * @param string $src * @param string $url * @param array $chunk * @param array $manifest * @return string */ protected function makePreloadTagForChunk($src, $url, $chunk, $manifest) { $attributes = $this->resolvePreloadTagAttributes($src, $url, $chunk, $manifest); if ($attributes === false) { return ''; } $this->preloadedAssets[$url] = $this->parseAttributes( Collection::make($attributes)->forget('href')->all() ); return '<link '.implode(' ', $this->parseAttributes($attributes)).' />'; } /** * Resolve the attributes for the chunks generated script tag. * * @param string $src * @param string $url * @param array|null $chunk * @param array|null $manifest * @return array */ protected function resolveScriptTagAttributes($src, $url, $chunk, $manifest) { $attributes = $this->integrityKey !== false ? ['integrity' => $chunk[$this->integrityKey] ?? false] : []; foreach ($this->scriptTagAttributesResolvers as $resolver) { $attributes = array_merge($attributes, $resolver($src, $url, $chunk, $manifest)); } return $attributes; } /** * Resolve the attributes for the chunks generated stylesheet tag. * * @param string $src * @param string $url * @param array|null $chunk * @param array|null $manifest * @return array */ protected function resolveStylesheetTagAttributes($src, $url, $chunk, $manifest) { $attributes = $this->integrityKey !== false ? ['integrity' => $chunk[$this->integrityKey] ?? false] : []; foreach ($this->styleTagAttributesResolvers as $resolver) { $attributes = array_merge($attributes, $resolver($src, $url, $chunk, $manifest)); } return $attributes; } /** * Resolve the attributes for the chunks generated preload tag. * * @param string $src * @param string $url * @param array $chunk * @param array $manifest * @return array|false */ protected function resolvePreloadTagAttributes($src, $url, $chunk, $manifest) { $attributes = $this->isCssPath($url) ? [ 'rel' => 'preload', 'as' => 'style', 'href' => $url, 'nonce' => $this->nonce ?? false, 'crossorigin' => $this->resolveStylesheetTagAttributes($src, $url, $chunk, $manifest)['crossorigin'] ?? false, ] : [ 'rel' => 'modulepreload', 'href' => $url, 'nonce' => $this->nonce ?? false, 'crossorigin' => $this->resolveScriptTagAttributes($src, $url, $chunk, $manifest)['crossorigin'] ?? false, ]; $attributes = $this->integrityKey !== false ? array_merge($attributes, ['integrity' => $chunk[$this->integrityKey] ?? false]) : $attributes; foreach ($this->preloadTagAttributesResolvers as $resolver) { if (false === ($resolvedAttributes = $resolver($src, $url, $chunk, $manifest))) { return false; } $attributes = array_merge($attributes, $resolvedAttributes); } return $attributes; } /** * Generate an appropriate tag for the given URL in HMR mode. * * @deprecated Will be removed in a future Laravel version. * * @param string $url * @return string */ protected function makeTag($url) { if ($this->isCssPath($url)) { return $this->makeStylesheetTag($url); } return $this->makeScriptTag($url); } /** * Generate a script tag for the given URL. * * @deprecated Will be removed in a future Laravel version. * * @param string $url * @return string */ protected function makeScriptTag($url) { return $this->makeScriptTagWithAttributes($url, []); } /** * Generate a stylesheet tag for the given URL in HMR mode. * * @deprecated Will be removed in a future Laravel version. * * @param string $url * @return string */ protected function makeStylesheetTag($url) { return $this->makeStylesheetTagWithAttributes($url, []); } /** * Generate a script tag with attributes for the given URL. * * @param string $url * @param array $attributes * @return string */ protected function makeScriptTagWithAttributes($url, $attributes) { $attributes = $this->parseAttributes(array_merge([ 'type' => 'module', 'src' => $url, 'nonce' => $this->nonce ?? false, ], $attributes)); return '<script '.implode(' ', $attributes).'></script>'; } /** * Generate a link tag with attributes for the given URL. * * @param string $url * @param array $attributes * @return string */ protected function makeStylesheetTagWithAttributes($url, $attributes) { $attributes = $this->parseAttributes(array_merge([ 'rel' => 'stylesheet', 'href' => $url, 'nonce' => $this->nonce ?? false, ], $attributes)); return '<link '.implode(' ', $attributes).' />'; } /** * Determine whether the given path is a CSS file. * * @param string $path * @return bool */ protected function isCssPath($path) { return preg_match('/\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/', $path) === 1; } /** * Parse the attributes into key="value" strings. * * @param array $attributes * @return array */ protected function parseAttributes($attributes) { return Collection::make($attributes) ->reject(fn ($value, $key) => in_array($value, [false, null], true)) ->flatMap(fn ($value, $key) => $value === true ? [$key] : [$key => $value]) ->map(fn ($value, $key) => is_int($key) ? $value : $key.'="'.$value.'"') ->values() ->all(); } /** * Generate React refresh runtime script. * * @return \Illuminate\Support\HtmlString|void */ public function reactRefresh() { if (! $this->isRunningHot()) { return; } $attributes = $this->parseAttributes([ 'nonce' => $this->cspNonce(), ]); return new HtmlString( sprintf( <<<'HTML' <script type="module" %s> import RefreshRuntime from '%s' RefreshRuntime.injectIntoGlobalHook(window) window.$RefreshReg$ = () => {} window.$RefreshSig$ = () => (type) => type window.__vite_plugin_react_preamble_installed__ = true </script> HTML, implode(' ', $attributes), $this->hotAsset('@react-refresh') ) ); } /** * Get the path to a given asset when running in HMR mode. * * @return string */ protected function hotAsset($asset) { return rtrim(file_get_contents($this->hotFile())).'/'.$asset; } /** * Get the URL for an asset. * * @param string $asset * @param string|null $buildDirectory * @return string */ public function asset($asset, $buildDirectory = null) { $buildDirectory ??= $this->buildDirectory; if ($this->isRunningHot()) { return $this->hotAsset($asset); } $chunk = $this->chunk($this->manifest($buildDirectory), $asset); return $this->assetPath($buildDirectory.'/'.$chunk['file']); } /** * Get the content of a given asset. * * @param string $asset * @param string|null $buildDirectory * @return string * * @throws \Exception */ public function content($asset, $buildDirectory = null) { $buildDirectory ??= $this->buildDirectory; $chunk = $this->chunk($this->manifest($buildDirectory), $asset); $path = public_path($buildDirectory.'/'.$chunk['file']); if (! is_file($path) || ! file_exists($path)) { throw new Exception("Unable to locate file from Vite manifest: {$path}."); } return file_get_contents($path); } /** * Generate an asset path for the application. * * @param string $path * @param bool|null $secure * @return string */ protected function assetPath($path, $secure = null) { return asset($path, $secure); } /** * Get the the manifest file for the given build directory. * * @param string $buildDirectory * @return array * * @throws \Illuminate\Foundation\ViteManifestNotFoundException */ protected function manifest($buildDirectory) { $path = $this->manifestPath($buildDirectory); if (! isset(static::$manifests[$path])) { if (! is_file($path)) { throw new ViteManifestNotFoundException("Vite manifest not found at: $path"); } static::$manifests[$path] = json_decode(file_get_contents($path), true); } return static::$manifests[$path]; } /** * Get the path to the manifest file for the given build directory. * * @param string $buildDirectory * @return string */ protected function manifestPath($buildDirectory) { return public_path($buildDirectory.'/'.$this->manifestFilename); } /** * Get a unique hash representing the current manifest, or null if there is no manifest. * * @param string|null $buildDirectory * @return string|null */ public function manifestHash($buildDirectory = null) { $buildDirectory ??= $this->buildDirectory; if ($this->isRunningHot()) { return null; } if (! is_file($path = $this->manifestPath($buildDirectory))) { return null; } return md5_file($path) ?: null; } /** * Get the chunk for the given entry point / asset. * * @param array $manifest * @param string $file * @return array * * @throws \Exception */ protected function chunk($manifest, $file) { if (! isset($manifest[$file])) { throw new Exception("Unable to locate file in Vite manifest: {$file}."); } return $manifest[$file]; } /** * Determine if the HMR server is running. * * @return bool */ public function isRunningHot() { return is_file($this->hotFile()); } /** * Get the Vite tag content as a string of HTML. * * @return string */ public function toHtml() { return $this->__invoke($this->entryPoints)->toHtml(); } } src/Illuminate/Foundation/Providers/ComposerServiceProvider.php 0000644 00000001267 15107327037 0021070 0 ustar 00 <?php namespace Illuminate\Foundation\Providers; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\Composer; use Illuminate\Support\ServiceProvider; class ComposerServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton('composer', function ($app) { return new Composer($app['files'], $app->basePath()); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return ['composer']; } } src/Illuminate/Foundation/Providers/FormRequestServiceProvider.php 0000644 00000001666 15107327037 0021560 0 ustar 00 <?php namespace Illuminate\Foundation\Providers; use Illuminate\Contracts\Validation\ValidatesWhenResolved; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Routing\Redirector; use Illuminate\Support\ServiceProvider; class FormRequestServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { // } /** * Bootstrap the application services. * * @return void */ public function boot() { $this->app->afterResolving(ValidatesWhenResolved::class, function ($resolved) { $resolved->validateResolved(); }); $this->app->resolving(FormRequest::class, function ($request, $app) { $request = FormRequest::createFrom($app['request'], $request); $request->setContainer($app)->setRedirector($app->make(Redirector::class)); }); } } src/Illuminate/Foundation/Providers/ConsoleSupportServiceProvider.php 0000644 00000001034 15107327037 0022270 0 ustar 00 <?php namespace Illuminate\Foundation\Providers; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Database\MigrationServiceProvider; use Illuminate\Support\AggregateServiceProvider; class ConsoleSupportServiceProvider extends AggregateServiceProvider implements DeferrableProvider { /** * The provider class names. * * @var string[] */ protected $providers = [ ArtisanServiceProvider::class, MigrationServiceProvider::class, ComposerServiceProvider::class, ]; } src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php 0000644 00000061204 15107327037 0020677 0 ustar 00 <?php namespace Illuminate\Foundation\Providers; use Illuminate\Auth\Console\ClearResetsCommand; use Illuminate\Cache\Console\CacheTableCommand; use Illuminate\Cache\Console\ClearCommand as CacheClearCommand; use Illuminate\Cache\Console\ForgetCommand as CacheForgetCommand; use Illuminate\Cache\Console\PruneStaleTagsCommand; use Illuminate\Console\Scheduling\ScheduleClearCacheCommand; use Illuminate\Console\Scheduling\ScheduleFinishCommand; use Illuminate\Console\Scheduling\ScheduleInterruptCommand; use Illuminate\Console\Scheduling\ScheduleListCommand; use Illuminate\Console\Scheduling\ScheduleRunCommand; use Illuminate\Console\Scheduling\ScheduleTestCommand; use Illuminate\Console\Scheduling\ScheduleWorkCommand; use Illuminate\Console\Signals; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Database\Console\DbCommand; use Illuminate\Database\Console\DumpCommand; use Illuminate\Database\Console\Factories\FactoryMakeCommand; use Illuminate\Database\Console\MonitorCommand as DatabaseMonitorCommand; use Illuminate\Database\Console\PruneCommand; use Illuminate\Database\Console\Seeds\SeedCommand; use Illuminate\Database\Console\Seeds\SeederMakeCommand; use Illuminate\Database\Console\ShowCommand; use Illuminate\Database\Console\ShowModelCommand; use Illuminate\Database\Console\TableCommand as DatabaseTableCommand; use Illuminate\Database\Console\WipeCommand; use Illuminate\Foundation\Console\AboutCommand; use Illuminate\Foundation\Console\CastMakeCommand; use Illuminate\Foundation\Console\ChannelListCommand; use Illuminate\Foundation\Console\ChannelMakeCommand; use Illuminate\Foundation\Console\ClearCompiledCommand; use Illuminate\Foundation\Console\ComponentMakeCommand; use Illuminate\Foundation\Console\ConfigCacheCommand; use Illuminate\Foundation\Console\ConfigClearCommand; use Illuminate\Foundation\Console\ConfigShowCommand; use Illuminate\Foundation\Console\ConsoleMakeCommand; use Illuminate\Foundation\Console\DocsCommand; use Illuminate\Foundation\Console\DownCommand; use Illuminate\Foundation\Console\EnvironmentCommand; use Illuminate\Foundation\Console\EnvironmentDecryptCommand; use Illuminate\Foundation\Console\EnvironmentEncryptCommand; use Illuminate\Foundation\Console\EventCacheCommand; use Illuminate\Foundation\Console\EventClearCommand; use Illuminate\Foundation\Console\EventGenerateCommand; use Illuminate\Foundation\Console\EventListCommand; use Illuminate\Foundation\Console\EventMakeCommand; use Illuminate\Foundation\Console\ExceptionMakeCommand; use Illuminate\Foundation\Console\JobMakeCommand; use Illuminate\Foundation\Console\KeyGenerateCommand; use Illuminate\Foundation\Console\LangPublishCommand; use Illuminate\Foundation\Console\ListenerMakeCommand; use Illuminate\Foundation\Console\MailMakeCommand; use Illuminate\Foundation\Console\ModelMakeCommand; use Illuminate\Foundation\Console\NotificationMakeCommand; use Illuminate\Foundation\Console\ObserverMakeCommand; use Illuminate\Foundation\Console\OptimizeClearCommand; use Illuminate\Foundation\Console\OptimizeCommand; use Illuminate\Foundation\Console\PackageDiscoverCommand; use Illuminate\Foundation\Console\PolicyMakeCommand; use Illuminate\Foundation\Console\ProviderMakeCommand; use Illuminate\Foundation\Console\RequestMakeCommand; use Illuminate\Foundation\Console\ResourceMakeCommand; use Illuminate\Foundation\Console\RouteCacheCommand; use Illuminate\Foundation\Console\RouteClearCommand; use Illuminate\Foundation\Console\RouteListCommand; use Illuminate\Foundation\Console\RuleMakeCommand; use Illuminate\Foundation\Console\ScopeMakeCommand; use Illuminate\Foundation\Console\ServeCommand; use Illuminate\Foundation\Console\StorageLinkCommand; use Illuminate\Foundation\Console\StubPublishCommand; use Illuminate\Foundation\Console\TestMakeCommand; use Illuminate\Foundation\Console\UpCommand; use Illuminate\Foundation\Console\VendorPublishCommand; use Illuminate\Foundation\Console\ViewCacheCommand; use Illuminate\Foundation\Console\ViewClearCommand; use Illuminate\Foundation\Console\ViewMakeCommand; use Illuminate\Notifications\Console\NotificationTableCommand; use Illuminate\Queue\Console\BatchesTableCommand; use Illuminate\Queue\Console\ClearCommand as QueueClearCommand; use Illuminate\Queue\Console\FailedTableCommand; use Illuminate\Queue\Console\FlushFailedCommand as FlushFailedQueueCommand; use Illuminate\Queue\Console\ForgetFailedCommand as ForgetFailedQueueCommand; use Illuminate\Queue\Console\ListenCommand as QueueListenCommand; use Illuminate\Queue\Console\ListFailedCommand as ListFailedQueueCommand; use Illuminate\Queue\Console\MonitorCommand as QueueMonitorCommand; use Illuminate\Queue\Console\PruneBatchesCommand as QueuePruneBatchesCommand; use Illuminate\Queue\Console\PruneFailedJobsCommand as QueuePruneFailedJobsCommand; use Illuminate\Queue\Console\RestartCommand as QueueRestartCommand; use Illuminate\Queue\Console\RetryBatchCommand as QueueRetryBatchCommand; use Illuminate\Queue\Console\RetryCommand as QueueRetryCommand; use Illuminate\Queue\Console\TableCommand; use Illuminate\Queue\Console\WorkCommand as QueueWorkCommand; use Illuminate\Routing\Console\ControllerMakeCommand; use Illuminate\Routing\Console\MiddlewareMakeCommand; use Illuminate\Session\Console\SessionTableCommand; use Illuminate\Support\ServiceProvider; class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvider { /** * The commands to be registered. * * @var array */ protected $commands = [ 'About' => AboutCommand::class, 'CacheClear' => CacheClearCommand::class, 'CacheForget' => CacheForgetCommand::class, 'ClearCompiled' => ClearCompiledCommand::class, 'ClearResets' => ClearResetsCommand::class, 'ConfigCache' => ConfigCacheCommand::class, 'ConfigClear' => ConfigClearCommand::class, 'ConfigShow' => ConfigShowCommand::class, 'Db' => DbCommand::class, 'DbMonitor' => DatabaseMonitorCommand::class, 'DbPrune' => PruneCommand::class, 'DbShow' => ShowCommand::class, 'DbTable' => DatabaseTableCommand::class, 'DbWipe' => WipeCommand::class, 'Down' => DownCommand::class, 'Environment' => EnvironmentCommand::class, 'EnvironmentDecrypt' => EnvironmentDecryptCommand::class, 'EnvironmentEncrypt' => EnvironmentEncryptCommand::class, 'EventCache' => EventCacheCommand::class, 'EventClear' => EventClearCommand::class, 'EventList' => EventListCommand::class, 'KeyGenerate' => KeyGenerateCommand::class, 'Optimize' => OptimizeCommand::class, 'OptimizeClear' => OptimizeClearCommand::class, 'PackageDiscover' => PackageDiscoverCommand::class, 'PruneStaleTagsCommand' => PruneStaleTagsCommand::class, 'QueueClear' => QueueClearCommand::class, 'QueueFailed' => ListFailedQueueCommand::class, 'QueueFlush' => FlushFailedQueueCommand::class, 'QueueForget' => ForgetFailedQueueCommand::class, 'QueueListen' => QueueListenCommand::class, 'QueueMonitor' => QueueMonitorCommand::class, 'QueuePruneBatches' => QueuePruneBatchesCommand::class, 'QueuePruneFailedJobs' => QueuePruneFailedJobsCommand::class, 'QueueRestart' => QueueRestartCommand::class, 'QueueRetry' => QueueRetryCommand::class, 'QueueRetryBatch' => QueueRetryBatchCommand::class, 'QueueWork' => QueueWorkCommand::class, 'RouteCache' => RouteCacheCommand::class, 'RouteClear' => RouteClearCommand::class, 'RouteList' => RouteListCommand::class, 'SchemaDump' => DumpCommand::class, 'Seed' => SeedCommand::class, 'ScheduleFinish' => ScheduleFinishCommand::class, 'ScheduleList' => ScheduleListCommand::class, 'ScheduleRun' => ScheduleRunCommand::class, 'ScheduleClearCache' => ScheduleClearCacheCommand::class, 'ScheduleTest' => ScheduleTestCommand::class, 'ScheduleWork' => ScheduleWorkCommand::class, 'ScheduleInterrupt' => ScheduleInterruptCommand::class, 'ShowModel' => ShowModelCommand::class, 'StorageLink' => StorageLinkCommand::class, 'Up' => UpCommand::class, 'ViewCache' => ViewCacheCommand::class, 'ViewClear' => ViewClearCommand::class, ]; /** * The commands to be registered. * * @var array */ protected $devCommands = [ 'CacheTable' => CacheTableCommand::class, 'CastMake' => CastMakeCommand::class, 'ChannelList' => ChannelListCommand::class, 'ChannelMake' => ChannelMakeCommand::class, 'ComponentMake' => ComponentMakeCommand::class, 'ConsoleMake' => ConsoleMakeCommand::class, 'ControllerMake' => ControllerMakeCommand::class, 'Docs' => DocsCommand::class, 'EventGenerate' => EventGenerateCommand::class, 'EventMake' => EventMakeCommand::class, 'ExceptionMake' => ExceptionMakeCommand::class, 'FactoryMake' => FactoryMakeCommand::class, 'JobMake' => JobMakeCommand::class, 'LangPublish' => LangPublishCommand::class, 'ListenerMake' => ListenerMakeCommand::class, 'MailMake' => MailMakeCommand::class, 'MiddlewareMake' => MiddlewareMakeCommand::class, 'ModelMake' => ModelMakeCommand::class, 'NotificationMake' => NotificationMakeCommand::class, 'NotificationTable' => NotificationTableCommand::class, 'ObserverMake' => ObserverMakeCommand::class, 'PolicyMake' => PolicyMakeCommand::class, 'ProviderMake' => ProviderMakeCommand::class, 'QueueFailedTable' => FailedTableCommand::class, 'QueueTable' => TableCommand::class, 'QueueBatchesTable' => BatchesTableCommand::class, 'RequestMake' => RequestMakeCommand::class, 'ResourceMake' => ResourceMakeCommand::class, 'RuleMake' => RuleMakeCommand::class, 'ScopeMake' => ScopeMakeCommand::class, 'SeederMake' => SeederMakeCommand::class, 'SessionTable' => SessionTableCommand::class, 'Serve' => ServeCommand::class, 'StubPublish' => StubPublishCommand::class, 'TestMake' => TestMakeCommand::class, 'VendorPublish' => VendorPublishCommand::class, 'ViewMake' => ViewMakeCommand::class, ]; /** * Register the service provider. * * @return void */ public function register() { $this->registerCommands(array_merge( $this->commands, $this->devCommands )); Signals::resolveAvailabilityUsing(function () { return $this->app->runningInConsole() && ! $this->app->runningUnitTests() && extension_loaded('pcntl'); }); } /** * Register the given commands. * * @param array $commands * @return void */ protected function registerCommands(array $commands) { foreach ($commands as $commandName => $command) { $method = "register{$commandName}Command"; if (method_exists($this, $method)) { $this->{$method}(); } else { $this->app->singleton($command); } } $this->commands(array_values($commands)); } /** * Register the command. * * @return void */ protected function registerAboutCommand() { $this->app->singleton(AboutCommand::class, function ($app) { return new AboutCommand($app['composer']); }); } /** * Register the command. * * @return void */ protected function registerCacheClearCommand() { $this->app->singleton(CacheClearCommand::class, function ($app) { return new CacheClearCommand($app['cache'], $app['files']); }); } /** * Register the command. * * @return void */ protected function registerCacheForgetCommand() { $this->app->singleton(CacheForgetCommand::class, function ($app) { return new CacheForgetCommand($app['cache']); }); } /** * Register the command. * * @return void */ protected function registerCacheTableCommand() { $this->app->singleton(CacheTableCommand::class, function ($app) { return new CacheTableCommand($app['files'], $app['composer']); }); } /** * Register the command. * * @return void */ protected function registerCastMakeCommand() { $this->app->singleton(CastMakeCommand::class, function ($app) { return new CastMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerChannelMakeCommand() { $this->app->singleton(ChannelMakeCommand::class, function ($app) { return new ChannelMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerComponentMakeCommand() { $this->app->singleton(ComponentMakeCommand::class, function ($app) { return new ComponentMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerConfigCacheCommand() { $this->app->singleton(ConfigCacheCommand::class, function ($app) { return new ConfigCacheCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerConfigClearCommand() { $this->app->singleton(ConfigClearCommand::class, function ($app) { return new ConfigClearCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerConsoleMakeCommand() { $this->app->singleton(ConsoleMakeCommand::class, function ($app) { return new ConsoleMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerControllerMakeCommand() { $this->app->singleton(ControllerMakeCommand::class, function ($app) { return new ControllerMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerEventMakeCommand() { $this->app->singleton(EventMakeCommand::class, function ($app) { return new EventMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerExceptionMakeCommand() { $this->app->singleton(ExceptionMakeCommand::class, function ($app) { return new ExceptionMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerFactoryMakeCommand() { $this->app->singleton(FactoryMakeCommand::class, function ($app) { return new FactoryMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerEventClearCommand() { $this->app->singleton(EventClearCommand::class, function ($app) { return new EventClearCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerJobMakeCommand() { $this->app->singleton(JobMakeCommand::class, function ($app) { return new JobMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerListenerMakeCommand() { $this->app->singleton(ListenerMakeCommand::class, function ($app) { return new ListenerMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerMailMakeCommand() { $this->app->singleton(MailMakeCommand::class, function ($app) { return new MailMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerMiddlewareMakeCommand() { $this->app->singleton(MiddlewareMakeCommand::class, function ($app) { return new MiddlewareMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerModelMakeCommand() { $this->app->singleton(ModelMakeCommand::class, function ($app) { return new ModelMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerNotificationMakeCommand() { $this->app->singleton(NotificationMakeCommand::class, function ($app) { return new NotificationMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerNotificationTableCommand() { $this->app->singleton(NotificationTableCommand::class, function ($app) { return new NotificationTableCommand($app['files'], $app['composer']); }); } /** * Register the command. * * @return void */ protected function registerObserverMakeCommand() { $this->app->singleton(ObserverMakeCommand::class, function ($app) { return new ObserverMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerPolicyMakeCommand() { $this->app->singleton(PolicyMakeCommand::class, function ($app) { return new PolicyMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerProviderMakeCommand() { $this->app->singleton(ProviderMakeCommand::class, function ($app) { return new ProviderMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerQueueForgetCommand() { $this->app->singleton(ForgetFailedQueueCommand::class); } /** * Register the command. * * @return void */ protected function registerQueueListenCommand() { $this->app->singleton(QueueListenCommand::class, function ($app) { return new QueueListenCommand($app['queue.listener']); }); } /** * Register the command. * * @return void */ protected function registerQueueMonitorCommand() { $this->app->singleton(QueueMonitorCommand::class, function ($app) { return new QueueMonitorCommand($app['queue'], $app['events']); }); } /** * Register the command. * * @return void */ protected function registerQueuePruneBatchesCommand() { $this->app->singleton(QueuePruneBatchesCommand::class, function () { return new QueuePruneBatchesCommand; }); } /** * Register the command. * * @return void */ protected function registerQueuePruneFailedJobsCommand() { $this->app->singleton(QueuePruneFailedJobsCommand::class, function () { return new QueuePruneFailedJobsCommand; }); } /** * Register the command. * * @return void */ protected function registerQueueRestartCommand() { $this->app->singleton(QueueRestartCommand::class, function ($app) { return new QueueRestartCommand($app['cache.store']); }); } /** * Register the command. * * @return void */ protected function registerQueueWorkCommand() { $this->app->singleton(QueueWorkCommand::class, function ($app) { return new QueueWorkCommand($app['queue.worker'], $app['cache.store']); }); } /** * Register the command. * * @return void */ protected function registerQueueFailedTableCommand() { $this->app->singleton(FailedTableCommand::class, function ($app) { return new FailedTableCommand($app['files'], $app['composer']); }); } /** * Register the command. * * @return void */ protected function registerQueueTableCommand() { $this->app->singleton(TableCommand::class, function ($app) { return new TableCommand($app['files'], $app['composer']); }); } /** * Register the command. * * @return void */ protected function registerQueueBatchesTableCommand() { $this->app->singleton(BatchesTableCommand::class, function ($app) { return new BatchesTableCommand($app['files'], $app['composer']); }); } /** * Register the command. * * @return void */ protected function registerRequestMakeCommand() { $this->app->singleton(RequestMakeCommand::class, function ($app) { return new RequestMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerResourceMakeCommand() { $this->app->singleton(ResourceMakeCommand::class, function ($app) { return new ResourceMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerRuleMakeCommand() { $this->app->singleton(RuleMakeCommand::class, function ($app) { return new RuleMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerScopeMakeCommand() { $this->app->singleton(ScopeMakeCommand::class, function ($app) { return new ScopeMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerSeederMakeCommand() { $this->app->singleton(SeederMakeCommand::class, function ($app) { return new SeederMakeCommand($app['files'], $app['composer']); }); } /** * Register the command. * * @return void */ protected function registerSessionTableCommand() { $this->app->singleton(SessionTableCommand::class, function ($app) { return new SessionTableCommand($app['files'], $app['composer']); }); } /** * Register the command. * * @return void */ protected function registerRouteCacheCommand() { $this->app->singleton(RouteCacheCommand::class, function ($app) { return new RouteCacheCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerRouteClearCommand() { $this->app->singleton(RouteClearCommand::class, function ($app) { return new RouteClearCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerRouteListCommand() { $this->app->singleton(RouteListCommand::class, function ($app) { return new RouteListCommand($app['router']); }); } /** * Register the command. * * @return void */ protected function registerSeedCommand() { $this->app->singleton(SeedCommand::class, function ($app) { return new SeedCommand($app['db']); }); } /** * Register the command. * * @return void */ protected function registerTestMakeCommand() { $this->app->singleton(TestMakeCommand::class, function ($app) { return new TestMakeCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerVendorPublishCommand() { $this->app->singleton(VendorPublishCommand::class, function ($app) { return new VendorPublishCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerViewClearCommand() { $this->app->singleton(ViewClearCommand::class, function ($app) { return new ViewClearCommand($app['files']); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return array_merge(array_values($this->commands), array_values($this->devCommands)); } } src/Illuminate/Foundation/Providers/FoundationServiceProvider.php 0000644 00000014233 15107327037 0021404 0 ustar 00 <?php namespace Illuminate\Foundation\Providers; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Foundation\MaintenanceMode as MaintenanceModeContract; use Illuminate\Contracts\View\Factory; use Illuminate\Database\ConnectionInterface; use Illuminate\Database\Grammar; use Illuminate\Foundation\Console\CliDumper; use Illuminate\Foundation\Http\HtmlDumper; use Illuminate\Foundation\MaintenanceModeManager; use Illuminate\Foundation\Precognition; use Illuminate\Foundation\Vite; use Illuminate\Http\Client\Factory as HttpFactory; use Illuminate\Http\Request; use Illuminate\Log\Events\MessageLogged; use Illuminate\Support\AggregateServiceProvider; use Illuminate\Support\Facades\URL; use Illuminate\Testing\LoggedExceptionCollection; use Illuminate\Testing\ParallelTestingServiceProvider; use Illuminate\Validation\ValidationException; use Symfony\Component\VarDumper\Caster\StubCaster; use Symfony\Component\VarDumper\Cloner\AbstractCloner; class FoundationServiceProvider extends AggregateServiceProvider { /** * The provider class names. * * @var string[] */ protected $providers = [ FormRequestServiceProvider::class, ParallelTestingServiceProvider::class, ]; /** * The singletons to register into the container. * * @var array */ public $singletons = [ HttpFactory::class => HttpFactory::class, Vite::class => Vite::class, ]; /** * Boot the service provider. * * @return void */ public function boot() { if ($this->app->runningInConsole()) { $this->publishes([ __DIR__.'/../Exceptions/views' => $this->app->resourcePath('views/errors/'), ], 'laravel-errors'); } } /** * Register the service provider. * * @return void */ public function register() { parent::register(); $this->registerDumper(); $this->registerRequestValidation(); $this->registerRequestSignatureValidation(); $this->registerExceptionTracking(); $this->registerMaintenanceModeManager(); } /** * Register a var dumper (with source) to debug variables. * * @return void */ public function registerDumper() { AbstractCloner::$defaultCasters[ConnectionInterface::class] = [StubCaster::class, 'cutInternals']; AbstractCloner::$defaultCasters[Container::class] = [StubCaster::class, 'cutInternals']; AbstractCloner::$defaultCasters[Dispatcher::class] = [StubCaster::class, 'cutInternals']; AbstractCloner::$defaultCasters[Factory::class] = [StubCaster::class, 'cutInternals']; AbstractCloner::$defaultCasters[Grammar::class] = [StubCaster::class, 'cutInternals']; $basePath = $this->app->basePath(); $compiledViewPath = $this->app['config']->get('view.compiled'); $format = $_SERVER['VAR_DUMPER_FORMAT'] ?? null; match (true) { 'html' == $format => HtmlDumper::register($basePath, $compiledViewPath), 'cli' == $format => CliDumper::register($basePath, $compiledViewPath), 'server' == $format => null, $format && 'tcp' == parse_url($format, PHP_URL_SCHEME) => null, default => in_array(PHP_SAPI, ['cli', 'phpdbg']) ? CliDumper::register($basePath, $compiledViewPath) : HtmlDumper::register($basePath, $compiledViewPath), }; } /** * Register the "validate" macro on the request. * * @return void * * @throws \Illuminate\Validation\ValidationException */ public function registerRequestValidation() { Request::macro('validate', function (array $rules, ...$params) { return tap(validator($this->all(), $rules, ...$params), function ($validator) { if ($this->isPrecognitive()) { $validator->after(Precognition::afterValidationHook($this)) ->setRules( $this->filterPrecognitiveRules($validator->getRulesWithoutPlaceholders()) ); } })->validate(); }); Request::macro('validateWithBag', function (string $errorBag, array $rules, ...$params) { try { return $this->validate($rules, ...$params); } catch (ValidationException $e) { $e->errorBag = $errorBag; throw $e; } }); } /** * Register the "hasValidSignature" macro on the request. * * @return void */ public function registerRequestSignatureValidation() { Request::macro('hasValidSignature', function ($absolute = true) { return URL::hasValidSignature($this, $absolute); }); Request::macro('hasValidRelativeSignature', function () { return URL::hasValidSignature($this, $absolute = false); }); Request::macro('hasValidSignatureWhileIgnoring', function ($ignoreQuery = [], $absolute = true) { return URL::hasValidSignature($this, $absolute, $ignoreQuery); }); } /** * Register an event listener to track logged exceptions. * * @return void */ protected function registerExceptionTracking() { if (! $this->app->runningUnitTests()) { return; } $this->app->instance( LoggedExceptionCollection::class, new LoggedExceptionCollection ); $this->app->make('events')->listen(MessageLogged::class, function ($event) { if (isset($event->context['exception'])) { $this->app->make(LoggedExceptionCollection::class) ->push($event->context['exception']); } }); } /** * Register the maintenance mode manager service. * * @return void */ public function registerMaintenanceModeManager() { $this->app->singleton(MaintenanceModeManager::class); $this->app->bind( MaintenanceModeContract::class, fn () => $this->app->make(MaintenanceModeManager::class)->driver() ); } } src/Illuminate/Foundation/EnvironmentDetector.php 0000644 00000003621 15107327037 0016262 0 ustar 00 <?php namespace Illuminate\Foundation; use Closure; class EnvironmentDetector { /** * Detect the application's current environment. * * @param \Closure $callback * @param array|null $consoleArgs * @return string */ public function detect(Closure $callback, $consoleArgs = null) { if ($consoleArgs) { return $this->detectConsoleEnvironment($callback, $consoleArgs); } return $this->detectWebEnvironment($callback); } /** * Set the application environment for a web request. * * @param \Closure $callback * @return string */ protected function detectWebEnvironment(Closure $callback) { return $callback(); } /** * Set the application environment from command-line arguments. * * @param \Closure $callback * @param array $args * @return string */ protected function detectConsoleEnvironment(Closure $callback, array $args) { // First we will check if an environment argument was passed via console arguments // and if it was that automatically overrides as the environment. Otherwise, we // will check the environment as a "web" request like a typical HTTP request. if (! is_null($value = $this->getEnvironmentArgument($args))) { return $value; } return $this->detectWebEnvironment($callback); } /** * Get the environment argument from the console. * * @param array $args * @return string|null */ protected function getEnvironmentArgument(array $args) { foreach ($args as $i => $value) { if ($value === '--env') { return $args[$i + 1] ?? null; } if (str_starts_with($value, '--env')) { return head(array_slice(explode('=', $value), 1)); } } } } src/Illuminate/Foundation/Routing/PrecognitionControllerDispatcher.php 0000644 00000002302 15107327037 0022421 0 ustar 00 <?php namespace Illuminate\Foundation\Routing; use Illuminate\Routing\ControllerDispatcher; use Illuminate\Routing\Route; use RuntimeException; class PrecognitionControllerDispatcher extends ControllerDispatcher { /** * Dispatch a request to a given controller and method. * * @param \Illuminate\Routing\Route $route * @param mixed $controller * @param string $method * @return void */ public function dispatch(Route $route, $controller, $method) { $this->ensureMethodExists($controller, $method); $this->resolveParameters($route, $controller, $method); abort(204, headers: ['Precognition-Success' => 'true']); } /** * Ensure that the given method exists on the controller. * * @param object $controller * @param string $method * @return $this */ protected function ensureMethodExists($controller, $method) { if (method_exists($controller, $method)) { return $this; } $class = $controller::class; throw new RuntimeException("Attempting to predict the outcome of the [{$class}::{$method}()] method but the method is not defined."); } } src/Illuminate/Foundation/Routing/PrecognitionCallableDispatcher.php 0000644 00000001047 15107327037 0022002 0 ustar 00 <?php namespace Illuminate\Foundation\Routing; use Illuminate\Routing\CallableDispatcher; use Illuminate\Routing\Route; class PrecognitionCallableDispatcher extends CallableDispatcher { /** * Dispatch a request to a given callable. * * @param \Illuminate\Routing\Route $route * @param callable $callable * @return mixed */ public function dispatch(Route $route, $callable) { $this->resolveParameters($route, $callable); abort(204, headers: ['Precognition-Success' => 'true']); } } src/Illuminate/Foundation/CacheBasedMaintenanceMode.php 0000644 00000004101 15107327037 0017170 0 ustar 00 <?php namespace Illuminate\Foundation; use Illuminate\Contracts\Cache\Factory; use Illuminate\Contracts\Cache\Repository; use Illuminate\Contracts\Foundation\MaintenanceMode; class CacheBasedMaintenanceMode implements MaintenanceMode { /** * The cache factory. * * @var \Illuminate\Contracts\Cache\Factory */ protected $cache; /** * The cache store that should be utilized. * * @var string */ protected $store; /** * The cache key to use when storing maintenance mode information. * * @var string */ protected $key; /** * Create a new cache based maintenance mode implementation. * * @param \Illuminate\Contracts\Cache\Factory $cache * @param string $store * @param string $key * @return void */ public function __construct(Factory $cache, string $store, string $key) { $this->cache = $cache; $this->store = $store; $this->key = $key; } /** * Take the application down for maintenance. * * @param array $payload * @return void */ public function activate(array $payload): void { $this->getStore()->put($this->key, $payload); } /** * Take the application out of maintenance. * * @return void */ public function deactivate(): void { $this->getStore()->forget($this->key); } /** * Determine if the application is currently down for maintenance. * * @return bool */ public function active(): bool { return $this->getStore()->has($this->key); } /** * Get the data array which was provided when the application was placed into maintenance. * * @return array */ public function data(): array { return $this->getStore()->get($this->key); } /** * Get the cache store to use. * * @return \Illuminate\Contracts\Cache\Repository */ protected function getStore(): Repository { return $this->cache->store($this->store); } } src/Illuminate/Foundation/Concerns/ResolvesDumpSource.php 0000644 00000012613 15107327037 0017650 0 ustar 00 <?php namespace Illuminate\Foundation\Concerns; use Throwable; trait ResolvesDumpSource { /** * All of the href formats for common editors. * * @var array<string, string> */ protected $editorHrefs = [ 'atom' => 'atom://core/open/file?filename={file}&line={line}', 'emacs' => 'emacs://open?url=file://{file}&line={line}', 'idea' => 'idea://open?file={file}&line={line}', 'macvim' => 'mvim://open/?url=file://{file}&line={line}', 'netbeans' => 'netbeans://open/?f={file}:{line}', 'nova' => 'nova://core/open/file?filename={file}&line={line}', 'phpstorm' => 'phpstorm://open?file={file}&line={line}', 'sublime' => 'subl://open?url=file://{file}&line={line}', 'textmate' => 'txmt://open?url=file://{file}&line={line}', 'vscode' => 'vscode://file/{file}:{line}', 'vscode-insiders' => 'vscode-insiders://file/{file}:{line}', 'vscode-insiders-remote' => 'vscode-insiders://vscode-remote/{file}:{line}', 'vscode-remote' => 'vscode://vscode-remote/{file}:{line}', 'vscodium' => 'vscodium://file/{file}:{line}', 'xdebug' => 'xdebug://{file}@{line}', ]; /** * Files that require special trace handling and their levels. * * @var array<string, int> */ protected static $adjustableTraces = [ 'symfony/var-dumper/Resources/functions/dump.php' => 1, 'Illuminate/Collections/Traits/EnumeratesValues.php' => 4, ]; /** * The source resolver. * * @var (callable(): (array{0: string, 1: string, 2: int|null}|null))|null|false */ protected static $dumpSourceResolver; /** * Resolve the source of the dump call. * * @return array{0: string, 1: string, 2: int|null}|null */ public function resolveDumpSource() { if (static::$dumpSourceResolver === false) { return null; } if (static::$dumpSourceResolver) { return call_user_func(static::$dumpSourceResolver); } $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 20); $sourceKey = null; foreach ($trace as $traceKey => $traceFile) { if (! isset($traceFile['file'])) { continue; } foreach (self::$adjustableTraces as $name => $key) { if (str_ends_with( $traceFile['file'], str_replace('/', DIRECTORY_SEPARATOR, $name) )) { $sourceKey = $traceKey + $key; break; } } if (! is_null($sourceKey)) { break; } } if (is_null($sourceKey)) { return; } $file = $trace[$sourceKey]['file'] ?? null; $line = $trace[$sourceKey]['line'] ?? null; if (is_null($file) || is_null($line)) { return; } $relativeFile = $file; if ($this->isCompiledViewFile($file)) { $file = $this->getOriginalFileForCompiledView($file); $line = null; } if (str_starts_with($file, $this->basePath)) { $relativeFile = substr($file, strlen($this->basePath) + 1); } return [$file, $relativeFile, $line]; } /** * Determine if the given file is a view compiled. * * @param string $file * @return bool */ protected function isCompiledViewFile($file) { return str_starts_with($file, $this->compiledViewPath) && str_ends_with($file, '.php'); } /** * Get the original view compiled file by the given compiled file. * * @param string $file * @return string */ protected function getOriginalFileForCompiledView($file) { preg_match('/\/\*\*PATH\s(.*)\sENDPATH/', file_get_contents($file), $matches); if (isset($matches[1])) { $file = $matches[1]; } return $file; } /** * Resolve the source href, if possible. * * @param string $file * @param int|null $line * @return string|null */ protected function resolveSourceHref($file, $line) { try { $editor = config('app.editor'); } catch (Throwable) { // .. } if (! isset($editor)) { return; } $href = is_array($editor) && isset($editor['href']) ? $editor['href'] : ($this->editorHrefs[$editor['name'] ?? $editor] ?? sprintf('%s://open?file={file}&line={line}', $editor['name'] ?? $editor)); if ($basePath = $editor['base_path'] ?? false) { $file = str_replace($this->basePath, $basePath, $file); } $href = str_replace( ['{file}', '{line}'], [$file, is_null($line) ? 1 : $line], $href, ); return $href; } /** * Set the resolver that resolves the source of the dump call. * * @param (callable(): (array{0: string, 1: string, 2: int|null}|null))|null $callable * @return void */ public static function resolveDumpSourceUsing($callable) { static::$dumpSourceResolver = $callable; } /** * Don't include the location / file of the dump in dumps. * * @return void */ public static function dontIncludeSource() { static::$dumpSourceResolver = false; } } src/Illuminate/Foundation/Precognition.php 0000644 00000001076 15107327037 0014726 0 ustar 00 <?php namespace Illuminate\Foundation; class Precognition { /** * Get the "after" validation hook that can be used for precognition requests. * * @param \Illuminate\Http\Request $request * @return \Closure */ public static function afterValidationHook($request) { return function ($validator) use ($request) { if ($validator->messages()->isEmpty() && $request->headers->has('Precognition-Validate-Only')) { abort(204, headers: ['Precognition-Success' => 'true']); } }; } } src/Illuminate/Foundation/ProviderRepository.php 0000644 00000014362 15107327037 0016162 0 ustar 00 <?php namespace Illuminate\Foundation; use Exception; use Illuminate\Contracts\Foundation\Application as ApplicationContract; use Illuminate\Filesystem\Filesystem; class ProviderRepository { /** * The application implementation. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * The path to the manifest file. * * @var string */ protected $manifestPath; /** * Create a new service repository instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @param \Illuminate\Filesystem\Filesystem $files * @param string $manifestPath * @return void */ public function __construct(ApplicationContract $app, Filesystem $files, $manifestPath) { $this->app = $app; $this->files = $files; $this->manifestPath = $manifestPath; } /** * Register the application service providers. * * @param array $providers * @return void */ public function load(array $providers) { $manifest = $this->loadManifest(); // First we will load the service manifest, which contains information on all // service providers registered with the application and which services it // provides. This is used to know which services are "deferred" loaders. if ($this->shouldRecompile($manifest, $providers)) { $manifest = $this->compileManifest($providers); } // Next, we will register events to load the providers for each of the events // that it has requested. This allows the service provider to defer itself // while still getting automatically loaded when a certain event occurs. foreach ($manifest['when'] as $provider => $events) { $this->registerLoadEvents($provider, $events); } // We will go ahead and register all of the eagerly loaded providers with the // application so their services can be registered with the application as // a provided service. Then we will set the deferred service list on it. foreach ($manifest['eager'] as $provider) { $this->app->register($provider); } $this->app->addDeferredServices($manifest['deferred']); } /** * Load the service provider manifest JSON file. * * @return array|null */ public function loadManifest() { // The service manifest is a file containing a JSON representation of every // service provided by the application and whether its provider is using // deferred loading or should be eagerly loaded on each request to us. if ($this->files->exists($this->manifestPath)) { $manifest = $this->files->getRequire($this->manifestPath); if ($manifest) { return array_merge(['when' => []], $manifest); } } } /** * Determine if the manifest should be compiled. * * @param array $manifest * @param array $providers * @return bool */ public function shouldRecompile($manifest, $providers) { return is_null($manifest) || $manifest['providers'] != $providers; } /** * Register the load events for the given provider. * * @param string $provider * @param array $events * @return void */ protected function registerLoadEvents($provider, array $events) { if (count($events) < 1) { return; } $this->app->make('events')->listen($events, fn () => $this->app->register($provider)); } /** * Compile the application service manifest file. * * @param array $providers * @return array */ protected function compileManifest($providers) { // The service manifest should contain a list of all of the providers for // the application so we can compare it on each request to the service // and determine if the manifest should be recompiled or is current. $manifest = $this->freshManifest($providers); foreach ($providers as $provider) { $instance = $this->createProvider($provider); // When recompiling the service manifest, we will spin through each of the // providers and check if it's a deferred provider or not. If so we'll // add it's provided services to the manifest and note the provider. if ($instance->isDeferred()) { foreach ($instance->provides() as $service) { $manifest['deferred'][$service] = $provider; } $manifest['when'][$provider] = $instance->when(); } // If the service providers are not deferred, we will simply add it to an // array of eagerly loaded providers that will get registered on every // request to this application instead of "lazy" loading every time. else { $manifest['eager'][] = $provider; } } return $this->writeManifest($manifest); } /** * Create a fresh service manifest data structure. * * @param array $providers * @return array */ protected function freshManifest(array $providers) { return ['providers' => $providers, 'eager' => [], 'deferred' => []]; } /** * Write the service manifest file to disk. * * @param array $manifest * @return array * * @throws \Exception */ public function writeManifest($manifest) { if (! is_writable($dirname = dirname($this->manifestPath))) { throw new Exception("The {$dirname} directory must be present and writable."); } $this->files->replace( $this->manifestPath, '<?php return '.var_export($manifest, true).';' ); return array_merge(['when' => []], $manifest); } /** * Create a new provider instance. * * @param string $provider * @return \Illuminate\Support\ServiceProvider */ public function createProvider($provider) { return new $provider($this->app); } } src/Illuminate/Foundation/ViteManifestNotFoundException.php 0000644 00000000172 15107327037 0020214 0 ustar 00 <?php namespace Illuminate\Foundation; use Exception; class ViteManifestNotFoundException extends Exception { // } src/Illuminate/Foundation/resources/server.php 0000644 00000000712 15107327037 0015602 0 ustar 00 <?php $publicPath = getcwd(); $uri = urldecode( parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?? '' ); // This file allows us to emulate Apache's "mod_rewrite" functionality from the // built-in PHP web server. This provides a convenient way to test a Laravel // application without having installed a "real" web server software here. if ($uri !== '/' && file_exists($publicPath.$uri)) { return false; } require_once $publicPath.'/index.php'; src/Illuminate/Foundation/Events/Dispatchable.php 0000644 00000002324 15107327037 0016112 0 ustar 00 <?php namespace Illuminate\Foundation\Events; trait Dispatchable { /** * Dispatch the event with the given arguments. * * @return mixed */ public static function dispatch() { return event(new static(...func_get_args())); } /** * Dispatch the event with the given arguments if the given truth test passes. * * @param bool $boolean * @param mixed ...$arguments * @return mixed */ public static function dispatchIf($boolean, ...$arguments) { if ($boolean) { return event(new static(...$arguments)); } } /** * Dispatch the event with the given arguments unless the given truth test passes. * * @param bool $boolean * @param mixed ...$arguments * @return mixed */ public static function dispatchUnless($boolean, ...$arguments) { if (! $boolean) { return event(new static(...$arguments)); } } /** * Broadcast the event with the given arguments. * * @return \Illuminate\Broadcasting\PendingBroadcast */ public static function broadcast() { return broadcast(new static(...func_get_args())); } } src/Illuminate/Foundation/Events/VendorTagPublished.php 0000644 00000001040 15107327037 0017252 0 ustar 00 <?php namespace Illuminate\Foundation\Events; class VendorTagPublished { /** * The vendor tag that was published. * * @var string */ public $tag; /** * The publishable paths registered by the tag. * * @var array */ public $paths; /** * Create a new event instance. * * @param string $tag * @param array $paths * @return void */ public function __construct($tag, $paths) { $this->tag = $tag; $this->paths = $paths; } } src/Illuminate/Foundation/Events/MaintenanceModeDisabled.php 0000644 00000000131 15107327037 0020200 0 ustar 00 <?php namespace Illuminate\Foundation\Events; class MaintenanceModeDisabled { // } src/Illuminate/Foundation/Events/MaintenanceModeEnabled.php 0000644 00000000130 15107327037 0020022 0 ustar 00 <?php namespace Illuminate\Foundation\Events; class MaintenanceModeEnabled { // } src/Illuminate/Foundation/Events/PublishingStubs.php 0000644 00000001224 15107327037 0016652 0 ustar 00 <?php namespace Illuminate\Foundation\Events; class PublishingStubs { use Dispatchable; /** * The stubs being published. * * @var array */ public $stubs = []; /** * Create a new event instance. * * @param array $stubs * @return void */ public function __construct(array $stubs) { $this->stubs = $stubs; } /** * Add a new stub to be published. * * @param string $path * @param string $name * @return $this */ public function add(string $path, string $name) { $this->stubs[$path] = $name; return $this; } } src/Illuminate/Foundation/Events/DiscoverEvents.php 0000644 00000006565 15107327037 0016505 0 ustar 00 <?php namespace Illuminate\Foundation\Events; use Illuminate\Support\Reflector; use Illuminate\Support\Str; use ReflectionClass; use ReflectionException; use ReflectionMethod; use SplFileInfo; use Symfony\Component\Finder\Finder; class DiscoverEvents { /** * The callback to be used to guess class names. * * @var callable(SplFileInfo, string): string|null */ public static $guessClassNamesUsingCallback; /** * Get all of the events and listeners by searching the given listener directory. * * @param string $listenerPath * @param string $basePath * @return array */ public static function within($listenerPath, $basePath) { $listeners = collect(static::getListenerEvents( (new Finder)->files()->in($listenerPath), $basePath )); $discoveredEvents = []; foreach ($listeners as $listener => $events) { foreach ($events as $event) { if (! isset($discoveredEvents[$event])) { $discoveredEvents[$event] = []; } $discoveredEvents[$event][] = $listener; } } return $discoveredEvents; } /** * Get all of the listeners and their corresponding events. * * @param iterable $listeners * @param string $basePath * @return array */ protected static function getListenerEvents($listeners, $basePath) { $listenerEvents = []; foreach ($listeners as $listener) { try { $listener = new ReflectionClass( static::classFromFile($listener, $basePath) ); } catch (ReflectionException) { continue; } if (! $listener->isInstantiable()) { continue; } foreach ($listener->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { if ((! Str::is('handle*', $method->name) && ! Str::is('__invoke', $method->name)) || ! isset($method->getParameters()[0])) { continue; } $listenerEvents[$listener->name.'@'.$method->name] = Reflector::getParameterClassNames($method->getParameters()[0]); } } return array_filter($listenerEvents); } /** * Extract the class name from the given file path. * * @param \SplFileInfo $file * @param string $basePath * @return string */ protected static function classFromFile(SplFileInfo $file, $basePath) { if (static::$guessClassNamesUsingCallback) { return call_user_func(static::$guessClassNamesUsingCallback, $file, $basePath); } $class = trim(Str::replaceFirst($basePath, '', $file->getRealPath()), DIRECTORY_SEPARATOR); return str_replace( [DIRECTORY_SEPARATOR, ucfirst(basename(app()->path())).'\\'], ['\\', app()->getNamespace()], ucfirst(Str::replaceLast('.php', '', $class)) ); } /** * Specify a callback to be used to guess class names. * * @param callable(SplFileInfo, string): string $callback * @return void */ public static function guessClassNamesUsing(callable $callback) { static::$guessClassNamesUsingCallback = $callback; } } src/Illuminate/Foundation/Events/LocaleUpdated.php 0000644 00000000542 15107327037 0016235 0 ustar 00 <?php namespace Illuminate\Foundation\Events; class LocaleUpdated { /** * The new locale. * * @var string */ public $locale; /** * Create a new event instance. * * @param string $locale * @return void */ public function __construct($locale) { $this->locale = $locale; } } src/Illuminate/Foundation/Auth/EmailVerificationRequest.php 0000644 00000002607 15107327037 0020133 0 ustar 00 <?php namespace Illuminate\Foundation\Auth; use Illuminate\Auth\Events\Verified; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Validator; class EmailVerificationRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { if (! hash_equals((string) $this->user()->getKey(), (string) $this->route('id'))) { return false; } if (! hash_equals(sha1($this->user()->getEmailForVerification()), (string) $this->route('hash'))) { return false; } return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ // ]; } /** * Fulfill the email verification request. * * @return void */ public function fulfill() { if (! $this->user()->hasVerifiedEmail()) { $this->user()->markEmailAsVerified(); event(new Verified($this->user())); } } /** * Configure the validator instance. * * @param \Illuminate\Validation\Validator $validator * @return \Illuminate\Validation\Validator */ public function withValidator(Validator $validator) { return $validator; } } src/Illuminate/Foundation/Auth/User.php 0000644 00000001254 15107327037 0014103 0 ustar 00 <?php namespace Illuminate\Foundation\Auth; use Illuminate\Auth\Authenticatable; use Illuminate\Auth\MustVerifyEmail; use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Auth\Access\Authorizable; class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract { use Authenticatable, Authorizable, CanResetPassword, MustVerifyEmail; } src/Illuminate/Foundation/Auth/Access/AuthorizesRequests.php 0000644 00000007267 15107327037 0020271 0 ustar 00 <?php namespace Illuminate\Foundation\Auth\Access; use Illuminate\Contracts\Auth\Access\Gate; use Illuminate\Support\Str; trait AuthorizesRequests { /** * Authorize a given action for the current user. * * @param mixed $ability * @param mixed|array $arguments * @return \Illuminate\Auth\Access\Response * * @throws \Illuminate\Auth\Access\AuthorizationException */ public function authorize($ability, $arguments = []) { [$ability, $arguments] = $this->parseAbilityAndArguments($ability, $arguments); return app(Gate::class)->authorize($ability, $arguments); } /** * Authorize a given action for a user. * * @param \Illuminate\Contracts\Auth\Authenticatable|mixed $user * @param mixed $ability * @param mixed|array $arguments * @return \Illuminate\Auth\Access\Response * * @throws \Illuminate\Auth\Access\AuthorizationException */ public function authorizeForUser($user, $ability, $arguments = []) { [$ability, $arguments] = $this->parseAbilityAndArguments($ability, $arguments); return app(Gate::class)->forUser($user)->authorize($ability, $arguments); } /** * Guesses the ability's name if it wasn't provided. * * @param mixed $ability * @param mixed|array $arguments * @return array */ protected function parseAbilityAndArguments($ability, $arguments) { if (is_string($ability) && ! str_contains($ability, '\\')) { return [$ability, $arguments]; } $method = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2]['function']; return [$this->normalizeGuessedAbilityName($method), $ability]; } /** * Normalize the ability name that has been guessed from the method name. * * @param string $ability * @return string */ protected function normalizeGuessedAbilityName($ability) { $map = $this->resourceAbilityMap(); return $map[$ability] ?? $ability; } /** * Authorize a resource action based on the incoming request. * * @param string|array $model * @param string|array|null $parameter * @param array $options * @param \Illuminate\Http\Request|null $request * @return void */ public function authorizeResource($model, $parameter = null, array $options = [], $request = null) { $model = is_array($model) ? implode(',', $model) : $model; $parameter = is_array($parameter) ? implode(',', $parameter) : $parameter; $parameter = $parameter ?: Str::snake(class_basename($model)); $middleware = []; foreach ($this->resourceAbilityMap() as $method => $ability) { $modelName = in_array($method, $this->resourceMethodsWithoutModels()) ? $model : $parameter; $middleware["can:{$ability},{$modelName}"][] = $method; } foreach ($middleware as $middlewareName => $methods) { $this->middleware($middlewareName, $options)->only($methods); } } /** * Get the map of resource methods to ability names. * * @return array */ protected function resourceAbilityMap() { return [ 'index' => 'viewAny', 'show' => 'view', 'create' => 'create', 'store' => 'create', 'edit' => 'update', 'update' => 'update', 'destroy' => 'delete', ]; } /** * Get the list of resource methods which do not have model parameters. * * @return array */ protected function resourceMethodsWithoutModels() { return ['index', 'create', 'store']; } } src/Illuminate/Foundation/Auth/Access/Authorizable.php 0000644 00000002574 15107327037 0017025 0 ustar 00 <?php namespace Illuminate\Foundation\Auth\Access; use Illuminate\Contracts\Auth\Access\Gate; trait Authorizable { /** * Determine if the entity has the given abilities. * * @param iterable|string $abilities * @param array|mixed $arguments * @return bool */ public function can($abilities, $arguments = []) { return app(Gate::class)->forUser($this)->check($abilities, $arguments); } /** * Determine if the entity has any of the given abilities. * * @param iterable|string $abilities * @param array|mixed $arguments * @return bool */ public function canAny($abilities, $arguments = []) { return app(Gate::class)->forUser($this)->any($abilities, $arguments); } /** * Determine if the entity does not have the given abilities. * * @param iterable|string $abilities * @param array|mixed $arguments * @return bool */ public function cant($abilities, $arguments = []) { return ! $this->can($abilities, $arguments); } /** * Determine if the entity does not have the given abilities. * * @param iterable|string $abilities * @param array|mixed $arguments * @return bool */ public function cannot($abilities, $arguments = []) { return $this->cant($abilities, $arguments); } } src/Illuminate/Foundation/Support/Providers/EventServiceProvider.php 0000644 00000007146 15107327037 0022040 0 ustar 00 <?php namespace Illuminate\Foundation\Support\Providers; use Illuminate\Foundation\Events\DiscoverEvents; use Illuminate\Support\Facades\Event; use Illuminate\Support\ServiceProvider; class EventServiceProvider extends ServiceProvider { /** * The event handler mappings for the application. * * @var array<string, array<int, string>> */ protected $listen = []; /** * The subscribers to register. * * @var array */ protected $subscribe = []; /** * The model observers to register. * * @var array<string, string|object|array<int, string|object>> */ protected $observers = []; /** * Register the application's event listeners. * * @return void */ public function register() { $this->booting(function () { $events = $this->getEvents(); foreach ($events as $event => $listeners) { foreach (array_unique($listeners, SORT_REGULAR) as $listener) { Event::listen($event, $listener); } } foreach ($this->subscribe as $subscriber) { Event::subscribe($subscriber); } foreach ($this->observers as $model => $observers) { $model::observe($observers); } }); } /** * Boot any application services. * * @return void */ public function boot() { // } /** * Get the events and handlers. * * @return array */ public function listens() { return $this->listen; } /** * Get the discovered events and listeners for the application. * * @return array */ public function getEvents() { if ($this->app->eventsAreCached()) { $cache = require $this->app->getCachedEventsPath(); return $cache[get_class($this)] ?? []; } else { return array_merge_recursive( $this->discoveredEvents(), $this->listens() ); } } /** * Get the discovered events for the application. * * @return array */ protected function discoveredEvents() { return $this->shouldDiscoverEvents() ? $this->discoverEvents() : []; } /** * Determine if events and listeners should be automatically discovered. * * @return bool */ public function shouldDiscoverEvents() { return false; } /** * Discover the events and listeners for the application. * * @return array */ public function discoverEvents() { return collect($this->discoverEventsWithin()) ->reject(function ($directory) { return ! is_dir($directory); }) ->reduce(function ($discovered, $directory) { return array_merge_recursive( $discovered, DiscoverEvents::within($directory, $this->eventDiscoveryBasePath()) ); }, []); } /** * Get the listener directories that should be used to discover events. * * @return array */ protected function discoverEventsWithin() { return [ $this->app->path('Listeners'), ]; } /** * Get the base path to be used during event discovery. * * @return string */ protected function eventDiscoveryBasePath() { return base_path(); } } src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php 0000644 00000006106 15107327037 0022050 0 ustar 00 <?php namespace Illuminate\Foundation\Support\Providers; use Closure; use Illuminate\Contracts\Routing\UrlGenerator; use Illuminate\Routing\Router; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Traits\ForwardsCalls; /** * @mixin \Illuminate\Routing\Router */ class RouteServiceProvider extends ServiceProvider { use ForwardsCalls; /** * The controller namespace for the application. * * @var string|null */ protected $namespace; /** * The callback that should be used to load the application's routes. * * @var \Closure|null */ protected $loadRoutesUsing; /** * Register any application services. * * @return void */ public function register() { $this->booted(function () { $this->setRootControllerNamespace(); if ($this->routesAreCached()) { $this->loadCachedRoutes(); } else { $this->loadRoutes(); $this->app->booted(function () { $this->app['router']->getRoutes()->refreshNameLookups(); $this->app['router']->getRoutes()->refreshActionLookups(); }); } }); } /** * Bootstrap any application services. * * @return void */ public function boot() { // } /** * Register the callback that will be used to load the application's routes. * * @param \Closure $routesCallback * @return $this */ protected function routes(Closure $routesCallback) { $this->loadRoutesUsing = $routesCallback; return $this; } /** * Set the root controller namespace for the application. * * @return void */ protected function setRootControllerNamespace() { if (! is_null($this->namespace)) { $this->app[UrlGenerator::class]->setRootControllerNamespace($this->namespace); } } /** * Determine if the application routes are cached. * * @return bool */ protected function routesAreCached() { return $this->app->routesAreCached(); } /** * Load the cached routes for the application. * * @return void */ protected function loadCachedRoutes() { $this->app->booted(function () { require $this->app->getCachedRoutesPath(); }); } /** * Load the application routes. * * @return void */ protected function loadRoutes() { if (! is_null($this->loadRoutesUsing)) { $this->app->call($this->loadRoutesUsing); } elseif (method_exists($this, 'map')) { $this->app->call([$this, 'map']); } } /** * Pass dynamic methods onto the router instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->forwardCallTo( $this->app->make(Router::class), $method, $parameters ); } } src/Illuminate/Foundation/Support/Providers/AuthServiceProvider.php 0000644 00000001755 15107327037 0021660 0 ustar 00 <?php namespace Illuminate\Foundation\Support\Providers; use Illuminate\Support\Facades\Gate; use Illuminate\Support\ServiceProvider; class AuthServiceProvider extends ServiceProvider { /** * The policy mappings for the application. * * @var array<class-string, class-string> */ protected $policies = []; /** * Register the application's policies. * * @return void */ public function register() { $this->booting(function () { $this->registerPolicies(); }); } /** * Register the application's policies. * * @return void */ public function registerPolicies() { foreach ($this->policies() as $model => $policy) { Gate::policy($model, $policy); } } /** * Get the policies defined on the provider. * * @return array<class-string, class-string> */ public function policies() { return $this->policies; } } src/Illuminate/Foundation/Mix.php 0000644 00000004072 15107327037 0013022 0 ustar 00 <?php namespace Illuminate\Foundation; use Exception; use Illuminate\Support\HtmlString; use Illuminate\Support\Str; class Mix { /** * Get the path to a versioned Mix file. * * @param string $path * @param string $manifestDirectory * @return \Illuminate\Support\HtmlString|string * * @throws \Exception */ public function __invoke($path, $manifestDirectory = '') { static $manifests = []; if (! str_starts_with($path, '/')) { $path = "/{$path}"; } if ($manifestDirectory && ! str_starts_with($manifestDirectory, '/')) { $manifestDirectory = "/{$manifestDirectory}"; } if (is_file(public_path($manifestDirectory.'/hot'))) { $url = rtrim(file_get_contents(public_path($manifestDirectory.'/hot'))); $customUrl = app('config')->get('app.mix_hot_proxy_url'); if (! empty($customUrl)) { return new HtmlString("{$customUrl}{$path}"); } if (Str::startsWith($url, ['http://', 'https://'])) { return new HtmlString(Str::after($url, ':').$path); } return new HtmlString("//localhost:8080{$path}"); } $manifestPath = public_path($manifestDirectory.'/mix-manifest.json'); if (! isset($manifests[$manifestPath])) { if (! is_file($manifestPath)) { throw new Exception("Mix manifest not found at: {$manifestPath}"); } $manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true); } $manifest = $manifests[$manifestPath]; if (! isset($manifest[$path])) { $exception = new Exception("Unable to locate Mix file: {$path}."); if (! app('config')->get('app.debug')) { report($exception); return $path; } else { throw $exception; } } return new HtmlString(app('config')->get('app.mix_url').$manifestDirectory.$manifest[$path]); } } src/Illuminate/Foundation/Bootstrap/RegisterProviders.php 0000644 00000000577 15107327037 0017732 0 ustar 00 <?php namespace Illuminate\Foundation\Bootstrap; use Illuminate\Contracts\Foundation\Application; class RegisterProviders { /** * Bootstrap the given application. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function bootstrap(Application $app) { $app->registerConfiguredProviders(); } } src/Illuminate/Foundation/Bootstrap/HandleExceptions.php 0000644 00000017406 15107327037 0017504 0 ustar 00 <?php namespace Illuminate\Foundation\Bootstrap; use ErrorException; use Exception; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Foundation\Application; use Illuminate\Log\LogManager; use Monolog\Handler\NullHandler; use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\ErrorHandler\Error\FatalError; use Throwable; class HandleExceptions { /** * Reserved memory so that errors can be displayed properly on memory exhaustion. * * @var string|null */ public static $reservedMemory; /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected static $app; /** * Bootstrap the given application. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function bootstrap(Application $app) { self::$reservedMemory = str_repeat('x', 32768); static::$app = $app; error_reporting(-1); set_error_handler($this->forwardsTo('handleError')); set_exception_handler($this->forwardsTo('handleException')); register_shutdown_function($this->forwardsTo('handleShutdown')); if (! $app->environment('testing')) { ini_set('display_errors', 'Off'); } } /** * Report PHP deprecations, or convert PHP errors to ErrorException instances. * * @param int $level * @param string $message * @param string $file * @param int $line * @return void * * @throws \ErrorException */ public function handleError($level, $message, $file = '', $line = 0) { if ($this->isDeprecation($level)) { $this->handleDeprecationError($message, $file, $line, $level); } elseif (error_reporting() & $level) { throw new ErrorException($message, 0, $level, $file, $line); } } /** * Reports a deprecation to the "deprecations" logger. * * @param string $message * @param string $file * @param int $line * @param int $level * @return void */ public function handleDeprecationError($message, $file, $line, $level = E_DEPRECATED) { if ($this->shouldIgnoreDeprecationErrors()) { return; } try { $logger = static::$app->make(LogManager::class); } catch (Exception) { return; } $this->ensureDeprecationLoggerIsConfigured(); $options = static::$app['config']->get('logging.deprecations') ?? []; with($logger->channel('deprecations'), function ($log) use ($message, $file, $line, $level, $options) { if ($options['trace'] ?? false) { $log->warning((string) new ErrorException($message, 0, $level, $file, $line)); } else { $log->warning(sprintf('%s in %s on line %s', $message, $file, $line )); } }); } /** * Determine if deprecation errors should be ignored. * * @return bool */ protected function shouldIgnoreDeprecationErrors() { return ! class_exists(LogManager::class) || ! static::$app->hasBeenBootstrapped() || static::$app->runningUnitTests(); } /** * Ensure the "deprecations" logger is configured. * * @return void */ protected function ensureDeprecationLoggerIsConfigured() { with(static::$app['config'], function ($config) { if ($config->get('logging.channels.deprecations')) { return; } $this->ensureNullLogDriverIsConfigured(); if (is_array($options = $config->get('logging.deprecations'))) { $driver = $options['channel'] ?? 'null'; } else { $driver = $options ?? 'null'; } $config->set('logging.channels.deprecations', $config->get("logging.channels.{$driver}")); }); } /** * Ensure the "null" log driver is configured. * * @return void */ protected function ensureNullLogDriverIsConfigured() { with(static::$app['config'], function ($config) { if ($config->get('logging.channels.null')) { return; } $config->set('logging.channels.null', [ 'driver' => 'monolog', 'handler' => NullHandler::class, ]); }); } /** * Handle an uncaught exception from the application. * * Note: Most exceptions can be handled via the try / catch block in * the HTTP and Console kernels. But, fatal error exceptions must * be handled differently since they are not normal exceptions. * * @param \Throwable $e * @return void */ public function handleException(Throwable $e) { self::$reservedMemory = null; try { $this->getExceptionHandler()->report($e); } catch (Exception) { $exceptionHandlerFailed = true; } if (static::$app->runningInConsole()) { $this->renderForConsole($e); if ($exceptionHandlerFailed ?? false) { exit(1); } } else { $this->renderHttpResponse($e); } } /** * Render an exception to the console. * * @param \Throwable $e * @return void */ protected function renderForConsole(Throwable $e) { $this->getExceptionHandler()->renderForConsole(new ConsoleOutput, $e); } /** * Render an exception as an HTTP response and send it. * * @param \Throwable $e * @return void */ protected function renderHttpResponse(Throwable $e) { $this->getExceptionHandler()->render(static::$app['request'], $e)->send(); } /** * Handle the PHP shutdown event. * * @return void */ public function handleShutdown() { self::$reservedMemory = null; if (! is_null($error = error_get_last()) && $this->isFatal($error['type'])) { $this->handleException($this->fatalErrorFromPhpError($error, 0)); } } /** * Create a new fatal error instance from an error array. * * @param array $error * @param int|null $traceOffset * @return \Symfony\Component\ErrorHandler\Error\FatalError */ protected function fatalErrorFromPhpError(array $error, $traceOffset = null) { return new FatalError($error['message'], 0, $error, $traceOffset); } /** * Forward a method call to the given method if an application instance exists. * * @return callable */ protected function forwardsTo($method) { return fn (...$arguments) => static::$app ? $this->{$method}(...$arguments) : false; } /** * Determine if the error level is a deprecation. * * @param int $level * @return bool */ protected function isDeprecation($level) { return in_array($level, [E_DEPRECATED, E_USER_DEPRECATED]); } /** * Determine if the error type is fatal. * * @param int $type * @return bool */ protected function isFatal($type) { return in_array($type, [E_COMPILE_ERROR, E_CORE_ERROR, E_ERROR, E_PARSE]); } /** * Get an instance of the exception handler. * * @return \Illuminate\Contracts\Debug\ExceptionHandler */ protected function getExceptionHandler() { return static::$app->make(ExceptionHandler::class); } /** * Clear the local application instance from memory. * * @return void */ public static function forgetApp() { static::$app = null; } } src/Illuminate/Foundation/Bootstrap/SetRequestForConsole.php 0000644 00000001517 15107327037 0020341 0 ustar 00 <?php namespace Illuminate\Foundation\Bootstrap; use Illuminate\Contracts\Foundation\Application; use Illuminate\Http\Request; class SetRequestForConsole { /** * Bootstrap the given application. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function bootstrap(Application $app) { $uri = $app->make('config')->get('app.url', 'http://localhost'); $components = parse_url($uri); $server = $_SERVER; if (isset($components['path'])) { $server = array_merge($server, [ 'SCRIPT_FILENAME' => $components['path'], 'SCRIPT_NAME' => $components['path'], ]); } $app->instance('request', Request::create( $uri, 'GET', [], [], [], $server )); } } src/Illuminate/Foundation/Bootstrap/RegisterFacades.php 0000644 00000001333 15107327037 0017272 0 ustar 00 <?php namespace Illuminate\Foundation\Bootstrap; use Illuminate\Contracts\Foundation\Application; use Illuminate\Foundation\AliasLoader; use Illuminate\Foundation\PackageManifest; use Illuminate\Support\Facades\Facade; class RegisterFacades { /** * Bootstrap the given application. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function bootstrap(Application $app) { Facade::clearResolvedInstances(); Facade::setFacadeApplication($app); AliasLoader::getInstance(array_merge( $app->make('config')->get('app.aliases', []), $app->make(PackageManifest::class)->aliases() ))->register(); } } src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php 0000644 00000007071 15107327037 0017653 0 ustar 00 <?php namespace Illuminate\Foundation\Bootstrap; use Exception; use Illuminate\Config\Repository; use Illuminate\Contracts\Config\Repository as RepositoryContract; use Illuminate\Contracts\Foundation\Application; use SplFileInfo; use Symfony\Component\Finder\Finder; class LoadConfiguration { /** * Bootstrap the given application. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function bootstrap(Application $app) { $items = []; // First we will see if we have a cache configuration file. If we do, we'll load // the configuration items from that file so that it is very quick. Otherwise // we will need to spin through every configuration file and load them all. if (file_exists($cached = $app->getCachedConfigPath())) { $items = require $cached; $loadedFromCache = true; } // Next we will spin through all of the configuration files in the configuration // directory and load each one into the repository. This will make all of the // options available to the developer for use in various parts of this app. $app->instance('config', $config = new Repository($items)); if (! isset($loadedFromCache)) { $this->loadConfigurationFiles($app, $config); } // Finally, we will set the application's environment based on the configuration // values that were loaded. We will pass a callback which will be used to get // the environment in a web context where an "--env" switch is not present. $app->detectEnvironment(fn () => $config->get('app.env', 'production')); date_default_timezone_set($config->get('app.timezone', 'UTC')); mb_internal_encoding('UTF-8'); } /** * Load the configuration items from all of the files. * * @param \Illuminate\Contracts\Foundation\Application $app * @param \Illuminate\Contracts\Config\Repository $repository * @return void * * @throws \Exception */ protected function loadConfigurationFiles(Application $app, RepositoryContract $repository) { $files = $this->getConfigurationFiles($app); if (! isset($files['app'])) { throw new Exception('Unable to load the "app" configuration file.'); } foreach ($files as $key => $path) { $repository->set($key, require $path); } } /** * Get all of the configuration files for the application. * * @param \Illuminate\Contracts\Foundation\Application $app * @return array */ protected function getConfigurationFiles(Application $app) { $files = []; $configPath = realpath($app->configPath()); foreach (Finder::create()->files()->name('*.php')->in($configPath) as $file) { $directory = $this->getNestedDirectory($file, $configPath); $files[$directory.basename($file->getRealPath(), '.php')] = $file->getRealPath(); } ksort($files, SORT_NATURAL); return $files; } /** * Get the configuration file nesting path. * * @param \SplFileInfo $file * @param string $configPath * @return string */ protected function getNestedDirectory(SplFileInfo $file, $configPath) { $directory = $file->getPath(); if ($nested = trim(str_replace($configPath, '', $directory), DIRECTORY_SEPARATOR)) { $nested = str_replace(DIRECTORY_SEPARATOR, '.', $nested).'.'; } return $nested; } } src/Illuminate/Foundation/Bootstrap/BootProviders.php 0000644 00000000544 15107327037 0017043 0 ustar 00 <?php namespace Illuminate\Foundation\Bootstrap; use Illuminate\Contracts\Foundation\Application; class BootProviders { /** * Bootstrap the given application. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function bootstrap(Application $app) { $app->boot(); } } src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php 0000644 00000005325 15107327037 0021201 0 ustar 00 <?php namespace Illuminate\Foundation\Bootstrap; use Dotenv\Dotenv; use Dotenv\Exception\InvalidFileException; use Illuminate\Contracts\Foundation\Application; use Illuminate\Support\Env; use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Output\ConsoleOutput; class LoadEnvironmentVariables { /** * Bootstrap the given application. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function bootstrap(Application $app) { if ($app->configurationIsCached()) { return; } $this->checkForSpecificEnvironmentFile($app); try { $this->createDotenv($app)->safeLoad(); } catch (InvalidFileException $e) { $this->writeErrorAndDie($e); } } /** * Detect if a custom environment file matching the APP_ENV exists. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ protected function checkForSpecificEnvironmentFile($app) { if ($app->runningInConsole() && ($input = new ArgvInput)->hasParameterOption('--env') && $this->setEnvironmentFilePath($app, $app->environmentFile().'.'.$input->getParameterOption('--env'))) { return; } $environment = Env::get('APP_ENV'); if (! $environment) { return; } $this->setEnvironmentFilePath( $app, $app->environmentFile().'.'.$environment ); } /** * Load a custom environment file. * * @param \Illuminate\Contracts\Foundation\Application $app * @param string $file * @return bool */ protected function setEnvironmentFilePath($app, $file) { if (is_file($app->environmentPath().'/'.$file)) { $app->loadEnvironmentFrom($file); return true; } return false; } /** * Create a Dotenv instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return \Dotenv\Dotenv */ protected function createDotenv($app) { return Dotenv::create( Env::getRepository(), $app->environmentPath(), $app->environmentFile() ); } /** * Write the error information to the screen and exit. * * @param \Dotenv\Exception\InvalidFileException $e * @return void */ protected function writeErrorAndDie(InvalidFileException $e) { $output = (new ConsoleOutput)->getErrorOutput(); $output->writeln('The environment file is invalid!'); $output->writeln($e->getMessage()); http_response_code(500); exit(1); } } src/Illuminate/Foundation/Bus/Dispatchable.php 0000644 00000005403 15107327037 0015400 0 ustar 00 <?php namespace Illuminate\Foundation\Bus; use Closure; use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Support\Fluent; trait Dispatchable { /** * Dispatch the job with the given arguments. * * @param mixed ...$arguments * @return \Illuminate\Foundation\Bus\PendingDispatch */ public static function dispatch(...$arguments) { return new PendingDispatch(new static(...$arguments)); } /** * Dispatch the job with the given arguments if the given truth test passes. * * @param bool|\Closure $boolean * @param mixed ...$arguments * @return \Illuminate\Foundation\Bus\PendingDispatch|\Illuminate\Support\Fluent */ public static function dispatchIf($boolean, ...$arguments) { if ($boolean instanceof Closure) { $dispatchable = new static(...$arguments); return value($boolean, $dispatchable) ? new PendingDispatch($dispatchable) : new Fluent; } return value($boolean) ? new PendingDispatch(new static(...$arguments)) : new Fluent; } /** * Dispatch the job with the given arguments unless the given truth test passes. * * @param bool|\Closure $boolean * @param mixed ...$arguments * @return \Illuminate\Foundation\Bus\PendingDispatch|\Illuminate\Support\Fluent */ public static function dispatchUnless($boolean, ...$arguments) { if ($boolean instanceof Closure) { $dispatchable = new static(...$arguments); return ! value($boolean, $dispatchable) ? new PendingDispatch($dispatchable) : new Fluent; } return ! value($boolean) ? new PendingDispatch(new static(...$arguments)) : new Fluent; } /** * Dispatch a command to its appropriate handler in the current process. * * Queueable jobs will be dispatched to the "sync" queue. * * @param mixed ...$arguments * @return mixed */ public static function dispatchSync(...$arguments) { return app(Dispatcher::class)->dispatchSync(new static(...$arguments)); } /** * Dispatch a command to its appropriate handler after the current process. * * @param mixed ...$arguments * @return mixed */ public static function dispatchAfterResponse(...$arguments) { return self::dispatch(...$arguments)->afterResponse(); } /** * Set the jobs that should run if this job is successful. * * @param array $chain * @return \Illuminate\Foundation\Bus\PendingChain */ public static function withChain($chain) { return new PendingChain(static::class, $chain); } } src/Illuminate/Foundation/Bus/PendingClosureDispatch.php 0000644 00000000564 15107327037 0017421 0 ustar 00 <?php namespace Illuminate\Foundation\Bus; use Closure; class PendingClosureDispatch extends PendingDispatch { /** * Add a callback to be executed if the job fails. * * @param \Closure $callback * @return $this */ public function catch(Closure $callback) { $this->job->onFailure($callback); return $this; } } src/Illuminate/Foundation/Bus/PendingChain.php 0000644 00000007047 15107327037 0015352 0 ustar 00 <?php namespace Illuminate\Foundation\Bus; use Closure; use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Queue\CallQueuedClosure; use Laravel\SerializableClosure\SerializableClosure; class PendingChain { /** * The class name of the job being dispatched. * * @var mixed */ public $job; /** * The jobs to be chained. * * @var array */ public $chain; /** * The name of the connection the chain should be sent to. * * @var string|null */ public $connection; /** * The name of the queue the chain should be sent to. * * @var string|null */ public $queue; /** * The number of seconds before the chain should be made available. * * @var \DateTimeInterface|\DateInterval|int|null */ public $delay; /** * The callbacks to be executed on failure. * * @var array */ public $catchCallbacks = []; /** * Create a new PendingChain instance. * * @param mixed $job * @param array $chain * @return void */ public function __construct($job, $chain) { $this->job = $job; $this->chain = $chain; } /** * Set the desired connection for the job. * * @param string|null $connection * @return $this */ public function onConnection($connection) { $this->connection = $connection; return $this; } /** * Set the desired queue for the job. * * @param string|null $queue * @return $this */ public function onQueue($queue) { $this->queue = $queue; return $this; } /** * Set the desired delay in seconds for the chain. * * @param \DateTimeInterface|\DateInterval|int|null $delay * @return $this */ public function delay($delay) { $this->delay = $delay; return $this; } /** * Add a callback to be executed on job failure. * * @param callable $callback * @return $this */ public function catch($callback) { $this->catchCallbacks[] = $callback instanceof Closure ? new SerializableClosure($callback) : $callback; return $this; } /** * Get the "catch" callbacks that have been registered. * * @return array */ public function catchCallbacks() { return $this->catchCallbacks ?? []; } /** * Dispatch the job with the given arguments. * * @return \Illuminate\Foundation\Bus\PendingDispatch */ public function dispatch() { if (is_string($this->job)) { $firstJob = new $this->job(...func_get_args()); } elseif ($this->job instanceof Closure) { $firstJob = CallQueuedClosure::create($this->job); } else { $firstJob = $this->job; } if ($this->connection) { $firstJob->chainConnection = $this->connection; $firstJob->connection = $firstJob->connection ?: $this->connection; } if ($this->queue) { $firstJob->chainQueue = $this->queue; $firstJob->queue = $firstJob->queue ?: $this->queue; } if ($this->delay) { $firstJob->delay = ! is_null($firstJob->delay) ? $firstJob->delay : $this->delay; } $firstJob->chain($this->chain); $firstJob->chainCatchCallbacks = $this->catchCallbacks(); return app(Dispatcher::class)->dispatch($firstJob); } } src/Illuminate/Foundation/Bus/PendingDispatch.php 0000644 00000007661 15107327037 0016071 0 ustar 00 <?php namespace Illuminate\Foundation\Bus; use Illuminate\Bus\UniqueLock; use Illuminate\Container\Container; use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Contracts\Queue\ShouldBeUnique; class PendingDispatch { /** * The job. * * @var mixed */ protected $job; /** * Indicates if the job should be dispatched immediately after sending the response. * * @var bool */ protected $afterResponse = false; /** * Create a new pending job dispatch. * * @param mixed $job * @return void */ public function __construct($job) { $this->job = $job; } /** * Set the desired connection for the job. * * @param string|null $connection * @return $this */ public function onConnection($connection) { $this->job->onConnection($connection); return $this; } /** * Set the desired queue for the job. * * @param string|null $queue * @return $this */ public function onQueue($queue) { $this->job->onQueue($queue); return $this; } /** * Set the desired connection for the chain. * * @param string|null $connection * @return $this */ public function allOnConnection($connection) { $this->job->allOnConnection($connection); return $this; } /** * Set the desired queue for the chain. * * @param string|null $queue * @return $this */ public function allOnQueue($queue) { $this->job->allOnQueue($queue); return $this; } /** * Set the desired delay in seconds for the job. * * @param \DateTimeInterface|\DateInterval|int|null $delay * @return $this */ public function delay($delay) { $this->job->delay($delay); return $this; } /** * Indicate that the job should be dispatched after all database transactions have committed. * * @return $this */ public function afterCommit() { $this->job->afterCommit(); return $this; } /** * Indicate that the job should not wait until database transactions have been committed before dispatching. * * @return $this */ public function beforeCommit() { $this->job->beforeCommit(); return $this; } /** * Set the jobs that should run if this job is successful. * * @param array $chain * @return $this */ public function chain($chain) { $this->job->chain($chain); return $this; } /** * Indicate that the job should be dispatched after the response is sent to the browser. * * @return $this */ public function afterResponse() { $this->afterResponse = true; return $this; } /** * Determine if the job should be dispatched. * * @return bool */ protected function shouldDispatch() { if (! $this->job instanceof ShouldBeUnique) { return true; } return (new UniqueLock(Container::getInstance()->make(Cache::class))) ->acquire($this->job); } /** * Dynamically proxy methods to the underlying job. * * @param string $method * @param array $parameters * @return $this */ public function __call($method, $parameters) { $this->job->{$method}(...$parameters); return $this; } /** * Handle the object's destruction. * * @return void */ public function __destruct() { if (! $this->shouldDispatch()) { return; } elseif ($this->afterResponse) { app(Dispatcher::class)->dispatchAfterResponse($this->job); } else { app(Dispatcher::class)->dispatch($this->job); } } } src/Illuminate/Foundation/Bus/DispatchesJobs.php 0000644 00000001075 15107327037 0015723 0 ustar 00 <?php namespace Illuminate\Foundation\Bus; trait DispatchesJobs { /** * Dispatch a job to its appropriate handler. * * @param mixed $job * @return mixed */ protected function dispatch($job) { return dispatch($job); } /** * Dispatch a job to its appropriate handler in the current process. * * Queueable jobs will be dispatched to the "sync" queue. * * @param mixed $job * @return mixed */ public function dispatchSync($job) { return dispatch_sync($job); } } src/Illuminate/Foundation/AliasLoader.php 0000644 00000012046 15107327037 0014445 0 ustar 00 <?php namespace Illuminate\Foundation; class AliasLoader { /** * The array of class aliases. * * @var array */ protected $aliases; /** * Indicates if a loader has been registered. * * @var bool */ protected $registered = false; /** * The namespace for all real-time facades. * * @var string */ protected static $facadeNamespace = 'Facades\\'; /** * The singleton instance of the loader. * * @var \Illuminate\Foundation\AliasLoader */ protected static $instance; /** * Create a new AliasLoader instance. * * @param array $aliases * @return void */ private function __construct($aliases) { $this->aliases = $aliases; } /** * Get or create the singleton alias loader instance. * * @param array $aliases * @return \Illuminate\Foundation\AliasLoader */ public static function getInstance(array $aliases = []) { if (is_null(static::$instance)) { return static::$instance = new static($aliases); } $aliases = array_merge(static::$instance->getAliases(), $aliases); static::$instance->setAliases($aliases); return static::$instance; } /** * Load a class alias if it is registered. * * @param string $alias * @return bool|null */ public function load($alias) { if (static::$facadeNamespace && str_starts_with($alias, static::$facadeNamespace)) { $this->loadFacade($alias); return true; } if (isset($this->aliases[$alias])) { return class_alias($this->aliases[$alias], $alias); } } /** * Load a real-time facade for the given alias. * * @param string $alias * @return void */ protected function loadFacade($alias) { require $this->ensureFacadeExists($alias); } /** * Ensure that the given alias has an existing real-time facade class. * * @param string $alias * @return string */ protected function ensureFacadeExists($alias) { if (is_file($path = storage_path('framework/cache/facade-'.sha1($alias).'.php'))) { return $path; } file_put_contents($path, $this->formatFacadeStub( $alias, file_get_contents(__DIR__.'/stubs/facade.stub') )); return $path; } /** * Format the facade stub with the proper namespace and class. * * @param string $alias * @param string $stub * @return string */ protected function formatFacadeStub($alias, $stub) { $replacements = [ str_replace('/', '\\', dirname(str_replace('\\', '/', $alias))), class_basename($alias), substr($alias, strlen(static::$facadeNamespace)), ]; return str_replace( ['DummyNamespace', 'DummyClass', 'DummyTarget'], $replacements, $stub ); } /** * Add an alias to the loader. * * @param string $alias * @param string $class * @return void */ public function alias($alias, $class) { $this->aliases[$alias] = $class; } /** * Register the loader on the auto-loader stack. * * @return void */ public function register() { if (! $this->registered) { $this->prependToLoaderStack(); $this->registered = true; } } /** * Prepend the load method to the auto-loader stack. * * @return void */ protected function prependToLoaderStack() { spl_autoload_register([$this, 'load'], true, true); } /** * Get the registered aliases. * * @return array */ public function getAliases() { return $this->aliases; } /** * Set the registered aliases. * * @param array $aliases * @return void */ public function setAliases(array $aliases) { $this->aliases = $aliases; } /** * Indicates if the loader has been registered. * * @return bool */ public function isRegistered() { return $this->registered; } /** * Set the "registered" state of the loader. * * @param bool $value * @return void */ public function setRegistered($value) { $this->registered = $value; } /** * Set the real-time facade namespace. * * @param string $namespace * @return void */ public static function setFacadeNamespace($namespace) { static::$facadeNamespace = rtrim($namespace, '\\').'\\'; } /** * Set the value of the singleton alias loader. * * @param \Illuminate\Foundation\AliasLoader $loader * @return void */ public static function setInstance($loader) { static::$instance = $loader; } /** * Clone method. * * @return void */ private function __clone() { // } } src/Illuminate/Foundation/Application.php 0000644 00000120471 15107327037 0014532 0 ustar 00 <?php namespace Illuminate\Foundation; use Closure; use Illuminate\Container\Container; use Illuminate\Contracts\Foundation\Application as ApplicationContract; use Illuminate\Contracts\Foundation\CachesConfiguration; use Illuminate\Contracts\Foundation\CachesRoutes; use Illuminate\Contracts\Foundation\MaintenanceMode as MaintenanceModeContract; use Illuminate\Contracts\Http\Kernel as HttpKernelContract; use Illuminate\Events\EventServiceProvider; use Illuminate\Filesystem\Filesystem; use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables; use Illuminate\Foundation\Events\LocaleUpdated; use Illuminate\Http\Request; use Illuminate\Log\LogServiceProvider; use Illuminate\Routing\RoutingServiceProvider; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Env; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use RuntimeException; use Symfony\Component\HttpFoundation\Request as SymfonyRequest; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\HttpKernelInterface; class Application extends Container implements ApplicationContract, CachesConfiguration, CachesRoutes, HttpKernelInterface { use Macroable; /** * The Laravel framework version. * * @var string */ const VERSION = '10.30.1'; /** * The base path for the Laravel installation. * * @var string */ protected $basePath; /** * Indicates if the application has been bootstrapped before. * * @var bool */ protected $hasBeenBootstrapped = false; /** * Indicates if the application has "booted". * * @var bool */ protected $booted = false; /** * The array of booting callbacks. * * @var callable[] */ protected $bootingCallbacks = []; /** * The array of booted callbacks. * * @var callable[] */ protected $bootedCallbacks = []; /** * The array of terminating callbacks. * * @var callable[] */ protected $terminatingCallbacks = []; /** * All of the registered service providers. * * @var \Illuminate\Support\ServiceProvider[] */ protected $serviceProviders = []; /** * The names of the loaded service providers. * * @var array */ protected $loadedProviders = []; /** * The deferred services and their providers. * * @var array */ protected $deferredServices = []; /** * The custom bootstrap path defined by the developer. * * @var string */ protected $bootstrapPath; /** * The custom application path defined by the developer. * * @var string */ protected $appPath; /** * The custom configuration path defined by the developer. * * @var string */ protected $configPath; /** * The custom database path defined by the developer. * * @var string */ protected $databasePath; /** * The custom language file path defined by the developer. * * @var string */ protected $langPath; /** * The custom public / web path defined by the developer. * * @var string */ protected $publicPath; /** * The custom storage path defined by the developer. * * @var string */ protected $storagePath; /** * The custom environment path defined by the developer. * * @var string */ protected $environmentPath; /** * The environment file to load during bootstrapping. * * @var string */ protected $environmentFile = '.env'; /** * Indicates if the application is running in the console. * * @var bool|null */ protected $isRunningInConsole; /** * The application namespace. * * @var string */ protected $namespace; /** * The prefixes of absolute cache paths for use during normalization. * * @var string[] */ protected $absoluteCachePathPrefixes = ['/', '\\']; /** * Create a new Illuminate application instance. * * @param string|null $basePath * @return void */ public function __construct($basePath = null) { if ($basePath) { $this->setBasePath($basePath); } $this->registerBaseBindings(); $this->registerBaseServiceProviders(); $this->registerCoreContainerAliases(); } /** * Get the version number of the application. * * @return string */ public function version() { return static::VERSION; } /** * Register the basic bindings into the container. * * @return void */ protected function registerBaseBindings() { static::setInstance($this); $this->instance('app', $this); $this->instance(Container::class, $this); $this->singleton(Mix::class); $this->singleton(PackageManifest::class, fn () => new PackageManifest( new Filesystem, $this->basePath(), $this->getCachedPackagesPath() )); } /** * Register all of the base service providers. * * @return void */ protected function registerBaseServiceProviders() { $this->register(new EventServiceProvider($this)); $this->register(new LogServiceProvider($this)); $this->register(new RoutingServiceProvider($this)); } /** * Run the given array of bootstrap classes. * * @param string[] $bootstrappers * @return void */ public function bootstrapWith(array $bootstrappers) { $this->hasBeenBootstrapped = true; foreach ($bootstrappers as $bootstrapper) { $this['events']->dispatch('bootstrapping: '.$bootstrapper, [$this]); $this->make($bootstrapper)->bootstrap($this); $this['events']->dispatch('bootstrapped: '.$bootstrapper, [$this]); } } /** * Register a callback to run after loading the environment. * * @param \Closure $callback * @return void */ public function afterLoadingEnvironment(Closure $callback) { $this->afterBootstrapping( LoadEnvironmentVariables::class, $callback ); } /** * Register a callback to run before a bootstrapper. * * @param string $bootstrapper * @param \Closure $callback * @return void */ public function beforeBootstrapping($bootstrapper, Closure $callback) { $this['events']->listen('bootstrapping: '.$bootstrapper, $callback); } /** * Register a callback to run after a bootstrapper. * * @param string $bootstrapper * @param \Closure $callback * @return void */ public function afterBootstrapping($bootstrapper, Closure $callback) { $this['events']->listen('bootstrapped: '.$bootstrapper, $callback); } /** * Determine if the application has been bootstrapped before. * * @return bool */ public function hasBeenBootstrapped() { return $this->hasBeenBootstrapped; } /** * Set the base path for the application. * * @param string $basePath * @return $this */ public function setBasePath($basePath) { $this->basePath = rtrim($basePath, '\/'); $this->bindPathsInContainer(); return $this; } /** * Bind all of the application paths in the container. * * @return void */ protected function bindPathsInContainer() { $this->instance('path', $this->path()); $this->instance('path.base', $this->basePath()); $this->instance('path.config', $this->configPath()); $this->instance('path.database', $this->databasePath()); $this->instance('path.public', $this->publicPath()); $this->instance('path.resources', $this->resourcePath()); $this->instance('path.storage', $this->storagePath()); $this->useBootstrapPath(value(function () { return is_dir($directory = $this->basePath('.laravel')) ? $directory : $this->basePath('bootstrap'); })); $this->useLangPath(value(function () { return is_dir($directory = $this->resourcePath('lang')) ? $directory : $this->basePath('lang'); })); } /** * Get the path to the application "app" directory. * * @param string $path * @return string */ public function path($path = '') { return $this->joinPaths($this->appPath ?: $this->basePath('app'), $path); } /** * Set the application directory. * * @param string $path * @return $this */ public function useAppPath($path) { $this->appPath = $path; $this->instance('path', $path); return $this; } /** * Get the base path of the Laravel installation. * * @param string $path * @return string */ public function basePath($path = '') { return $this->joinPaths($this->basePath, $path); } /** * Get the path to the bootstrap directory. * * @param string $path * @return string */ public function bootstrapPath($path = '') { return $this->joinPaths($this->bootstrapPath, $path); } /** * Set the bootstrap file directory. * * @param string $path * @return $this */ public function useBootstrapPath($path) { $this->bootstrapPath = $path; $this->instance('path.bootstrap', $path); return $this; } /** * Get the path to the application configuration files. * * @param string $path * @return string */ public function configPath($path = '') { return $this->joinPaths($this->configPath ?: $this->basePath('config'), $path); } /** * Set the configuration directory. * * @param string $path * @return $this */ public function useConfigPath($path) { $this->configPath = $path; $this->instance('path.config', $path); return $this; } /** * Get the path to the database directory. * * @param string $path * @return string */ public function databasePath($path = '') { return $this->joinPaths($this->databasePath ?: $this->basePath('database'), $path); } /** * Set the database directory. * * @param string $path * @return $this */ public function useDatabasePath($path) { $this->databasePath = $path; $this->instance('path.database', $path); return $this; } /** * Get the path to the language files. * * @param string $path * @return string */ public function langPath($path = '') { return $this->joinPaths($this->langPath, $path); } /** * Set the language file directory. * * @param string $path * @return $this */ public function useLangPath($path) { $this->langPath = $path; $this->instance('path.lang', $path); return $this; } /** * Get the path to the public / web directory. * * @param string $path * @return string */ public function publicPath($path = '') { return $this->joinPaths($this->publicPath ?: $this->basePath('public'), $path); } /** * Set the public / web directory. * * @param string $path * @return $this */ public function usePublicPath($path) { $this->publicPath = $path; $this->instance('path.public', $path); return $this; } /** * Get the path to the storage directory. * * @param string $path * @return string */ public function storagePath($path = '') { if (isset($_ENV['LARAVEL_STORAGE_PATH'])) { return $this->joinPaths($this->storagePath ?: $_ENV['LARAVEL_STORAGE_PATH'], $path); } return $this->joinPaths($this->storagePath ?: $this->basePath('storage'), $path); } /** * Set the storage directory. * * @param string $path * @return $this */ public function useStoragePath($path) { $this->storagePath = $path; $this->instance('path.storage', $path); return $this; } /** * Get the path to the resources directory. * * @param string $path * @return string */ public function resourcePath($path = '') { return $this->joinPaths($this->basePath('resources'), $path); } /** * Get the path to the views directory. * * This method returns the first configured path in the array of view paths. * * @param string $path * @return string */ public function viewPath($path = '') { $viewPath = rtrim($this['config']->get('view.paths')[0], DIRECTORY_SEPARATOR); return $this->joinPaths($viewPath, $path); } /** * Join the given paths together. * * @param string $basePath * @param string $path * @return string */ public function joinPaths($basePath, $path = '') { return $basePath.($path != '' ? DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR) : ''); } /** * Get the path to the environment file directory. * * @return string */ public function environmentPath() { return $this->environmentPath ?: $this->basePath; } /** * Set the directory for the environment file. * * @param string $path * @return $this */ public function useEnvironmentPath($path) { $this->environmentPath = $path; return $this; } /** * Set the environment file to be loaded during bootstrapping. * * @param string $file * @return $this */ public function loadEnvironmentFrom($file) { $this->environmentFile = $file; return $this; } /** * Get the environment file the application is using. * * @return string */ public function environmentFile() { return $this->environmentFile ?: '.env'; } /** * Get the fully qualified path to the environment file. * * @return string */ public function environmentFilePath() { return $this->environmentPath().DIRECTORY_SEPARATOR.$this->environmentFile(); } /** * Get or check the current application environment. * * @param string|array ...$environments * @return string|bool */ public function environment(...$environments) { if (count($environments) > 0) { $patterns = is_array($environments[0]) ? $environments[0] : $environments; return Str::is($patterns, $this['env']); } return $this['env']; } /** * Determine if the application is in the local environment. * * @return bool */ public function isLocal() { return $this['env'] === 'local'; } /** * Determine if the application is in the production environment. * * @return bool */ public function isProduction() { return $this['env'] === 'production'; } /** * Detect the application's current environment. * * @param \Closure $callback * @return string */ public function detectEnvironment(Closure $callback) { $args = $_SERVER['argv'] ?? null; return $this['env'] = (new EnvironmentDetector)->detect($callback, $args); } /** * Determine if the application is running in the console. * * @return bool */ public function runningInConsole() { if ($this->isRunningInConsole === null) { $this->isRunningInConsole = Env::get('APP_RUNNING_IN_CONSOLE') ?? (\PHP_SAPI === 'cli' || \PHP_SAPI === 'phpdbg'); } return $this->isRunningInConsole; } /** * Determine if the application is running any of the given console commands. * * @param string|array ...$commands * @return bool */ public function runningConsoleCommand(...$commands) { if (! $this->runningInConsole()) { return false; } return in_array( $_SERVER['argv'][1] ?? null, is_array($commands[0]) ? $commands[0] : $commands ); } /** * Determine if the application is running unit tests. * * @return bool */ public function runningUnitTests() { return $this->bound('env') && $this['env'] === 'testing'; } /** * Determine if the application is running with debug mode enabled. * * @return bool */ public function hasDebugModeEnabled() { return (bool) $this['config']->get('app.debug'); } /** * Register all of the configured providers. * * @return void */ public function registerConfiguredProviders() { $providers = Collection::make($this->make('config')->get('app.providers')) ->partition(fn ($provider) => str_starts_with($provider, 'Illuminate\\')); $providers->splice(1, 0, [$this->make(PackageManifest::class)->providers()]); (new ProviderRepository($this, new Filesystem, $this->getCachedServicesPath())) ->load($providers->collapse()->toArray()); } /** * Register a service provider with the application. * * @param \Illuminate\Support\ServiceProvider|string $provider * @param bool $force * @return \Illuminate\Support\ServiceProvider */ public function register($provider, $force = false) { if (($registered = $this->getProvider($provider)) && ! $force) { return $registered; } // If the given "provider" is a string, we will resolve it, passing in the // application instance automatically for the developer. This is simply // a more convenient way of specifying your service provider classes. if (is_string($provider)) { $provider = $this->resolveProvider($provider); } $provider->register(); // If there are bindings / singletons set as properties on the provider we // will spin through them and register them with the application, which // serves as a convenience layer while registering a lot of bindings. if (property_exists($provider, 'bindings')) { foreach ($provider->bindings as $key => $value) { $this->bind($key, $value); } } if (property_exists($provider, 'singletons')) { foreach ($provider->singletons as $key => $value) { $key = is_int($key) ? $value : $key; $this->singleton($key, $value); } } $this->markAsRegistered($provider); // If the application has already booted, we will call this boot method on // the provider class so it has an opportunity to do its boot logic and // will be ready for any usage by this developer's application logic. if ($this->isBooted()) { $this->bootProvider($provider); } return $provider; } /** * Get the registered service provider instance if it exists. * * @param \Illuminate\Support\ServiceProvider|string $provider * @return \Illuminate\Support\ServiceProvider|null */ public function getProvider($provider) { return array_values($this->getProviders($provider))[0] ?? null; } /** * Get the registered service provider instances if any exist. * * @param \Illuminate\Support\ServiceProvider|string $provider * @return array */ public function getProviders($provider) { $name = is_string($provider) ? $provider : get_class($provider); return Arr::where($this->serviceProviders, fn ($value) => $value instanceof $name); } /** * Resolve a service provider instance from the class name. * * @param string $provider * @return \Illuminate\Support\ServiceProvider */ public function resolveProvider($provider) { return new $provider($this); } /** * Mark the given provider as registered. * * @param \Illuminate\Support\ServiceProvider $provider * @return void */ protected function markAsRegistered($provider) { $this->serviceProviders[] = $provider; $this->loadedProviders[get_class($provider)] = true; } /** * Load and boot all of the remaining deferred providers. * * @return void */ public function loadDeferredProviders() { // We will simply spin through each of the deferred providers and register each // one and boot them if the application has booted. This should make each of // the remaining services available to this application for immediate use. foreach ($this->deferredServices as $service => $provider) { $this->loadDeferredProvider($service); } $this->deferredServices = []; } /** * Load the provider for a deferred service. * * @param string $service * @return void */ public function loadDeferredProvider($service) { if (! $this->isDeferredService($service)) { return; } $provider = $this->deferredServices[$service]; // If the service provider has not already been loaded and registered we can // register it with the application and remove the service from this list // of deferred services, since it will already be loaded on subsequent. if (! isset($this->loadedProviders[$provider])) { $this->registerDeferredProvider($provider, $service); } } /** * Register a deferred provider and service. * * @param string $provider * @param string|null $service * @return void */ public function registerDeferredProvider($provider, $service = null) { // Once the provider that provides the deferred service has been registered we // will remove it from our local list of the deferred services with related // providers so that this container does not try to resolve it out again. if ($service) { unset($this->deferredServices[$service]); } $this->register($instance = new $provider($this)); if (! $this->isBooted()) { $this->booting(function () use ($instance) { $this->bootProvider($instance); }); } } /** * Resolve the given type from the container. * * @param string $abstract * @param array $parameters * @return mixed */ public function make($abstract, array $parameters = []) { $this->loadDeferredProviderIfNeeded($abstract = $this->getAlias($abstract)); return parent::make($abstract, $parameters); } /** * Resolve the given type from the container. * * @param string $abstract * @param array $parameters * @param bool $raiseEvents * @return mixed */ protected function resolve($abstract, $parameters = [], $raiseEvents = true) { $this->loadDeferredProviderIfNeeded($abstract = $this->getAlias($abstract)); return parent::resolve($abstract, $parameters, $raiseEvents); } /** * Load the deferred provider if the given type is a deferred service and the instance has not been loaded. * * @param string $abstract * @return void */ protected function loadDeferredProviderIfNeeded($abstract) { if ($this->isDeferredService($abstract) && ! isset($this->instances[$abstract])) { $this->loadDeferredProvider($abstract); } } /** * Determine if the given abstract type has been bound. * * @param string $abstract * @return bool */ public function bound($abstract) { return $this->isDeferredService($abstract) || parent::bound($abstract); } /** * Determine if the application has booted. * * @return bool */ public function isBooted() { return $this->booted; } /** * Boot the application's service providers. * * @return void */ public function boot() { if ($this->isBooted()) { return; } // Once the application has booted we will also fire some "booted" callbacks // for any listeners that need to do work after this initial booting gets // finished. This is useful when ordering the boot-up processes we run. $this->fireAppCallbacks($this->bootingCallbacks); array_walk($this->serviceProviders, function ($p) { $this->bootProvider($p); }); $this->booted = true; $this->fireAppCallbacks($this->bootedCallbacks); } /** * Boot the given service provider. * * @param \Illuminate\Support\ServiceProvider $provider * @return void */ protected function bootProvider(ServiceProvider $provider) { $provider->callBootingCallbacks(); if (method_exists($provider, 'boot')) { $this->call([$provider, 'boot']); } $provider->callBootedCallbacks(); } /** * Register a new boot listener. * * @param callable $callback * @return void */ public function booting($callback) { $this->bootingCallbacks[] = $callback; } /** * Register a new "booted" listener. * * @param callable $callback * @return void */ public function booted($callback) { $this->bootedCallbacks[] = $callback; if ($this->isBooted()) { $callback($this); } } /** * Call the booting callbacks for the application. * * @param callable[] $callbacks * @return void */ protected function fireAppCallbacks(array &$callbacks) { $index = 0; while ($index < count($callbacks)) { $callbacks[$index]($this); $index++; } } /** * {@inheritdoc} * * @return \Symfony\Component\HttpFoundation\Response */ public function handle(SymfonyRequest $request, int $type = self::MAIN_REQUEST, bool $catch = true): SymfonyResponse { return $this[HttpKernelContract::class]->handle(Request::createFromBase($request)); } /** * Determine if middleware has been disabled for the application. * * @return bool */ public function shouldSkipMiddleware() { return $this->bound('middleware.disable') && $this->make('middleware.disable') === true; } /** * Get the path to the cached services.php file. * * @return string */ public function getCachedServicesPath() { return $this->normalizeCachePath('APP_SERVICES_CACHE', 'cache/services.php'); } /** * Get the path to the cached packages.php file. * * @return string */ public function getCachedPackagesPath() { return $this->normalizeCachePath('APP_PACKAGES_CACHE', 'cache/packages.php'); } /** * Determine if the application configuration is cached. * * @return bool */ public function configurationIsCached() { return is_file($this->getCachedConfigPath()); } /** * Get the path to the configuration cache file. * * @return string */ public function getCachedConfigPath() { return $this->normalizeCachePath('APP_CONFIG_CACHE', 'cache/config.php'); } /** * Determine if the application routes are cached. * * @return bool */ public function routesAreCached() { return $this['files']->exists($this->getCachedRoutesPath()); } /** * Get the path to the routes cache file. * * @return string */ public function getCachedRoutesPath() { return $this->normalizeCachePath('APP_ROUTES_CACHE', 'cache/routes-v7.php'); } /** * Determine if the application events are cached. * * @return bool */ public function eventsAreCached() { return $this['files']->exists($this->getCachedEventsPath()); } /** * Get the path to the events cache file. * * @return string */ public function getCachedEventsPath() { return $this->normalizeCachePath('APP_EVENTS_CACHE', 'cache/events.php'); } /** * Normalize a relative or absolute path to a cache file. * * @param string $key * @param string $default * @return string */ protected function normalizeCachePath($key, $default) { if (is_null($env = Env::get($key))) { return $this->bootstrapPath($default); } return Str::startsWith($env, $this->absoluteCachePathPrefixes) ? $env : $this->basePath($env); } /** * Add new prefix to list of absolute path prefixes. * * @param string $prefix * @return $this */ public function addAbsoluteCachePathPrefix($prefix) { $this->absoluteCachePathPrefixes[] = $prefix; return $this; } /** * Get an instance of the maintenance mode manager implementation. * * @return \Illuminate\Contracts\Foundation\MaintenanceMode */ public function maintenanceMode() { return $this->make(MaintenanceModeContract::class); } /** * Determine if the application is currently down for maintenance. * * @return bool */ public function isDownForMaintenance() { return $this->maintenanceMode()->active(); } /** * Throw an HttpException with the given data. * * @param int $code * @param string $message * @param array $headers * @return never * * @throws \Symfony\Component\HttpKernel\Exception\HttpException * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ public function abort($code, $message = '', array $headers = []) { if ($code == 404) { throw new NotFoundHttpException($message, null, 0, $headers); } throw new HttpException($code, $message, null, $headers); } /** * Register a terminating callback with the application. * * @param callable|string $callback * @return $this */ public function terminating($callback) { $this->terminatingCallbacks[] = $callback; return $this; } /** * Terminate the application. * * @return void */ public function terminate() { $index = 0; while ($index < count($this->terminatingCallbacks)) { $this->call($this->terminatingCallbacks[$index]); $index++; } } /** * Get the service providers that have been loaded. * * @return array */ public function getLoadedProviders() { return $this->loadedProviders; } /** * Determine if the given service provider is loaded. * * @param string $provider * @return bool */ public function providerIsLoaded(string $provider) { return isset($this->loadedProviders[$provider]); } /** * Get the application's deferred services. * * @return array */ public function getDeferredServices() { return $this->deferredServices; } /** * Set the application's deferred services. * * @param array $services * @return void */ public function setDeferredServices(array $services) { $this->deferredServices = $services; } /** * Add an array of services to the application's deferred services. * * @param array $services * @return void */ public function addDeferredServices(array $services) { $this->deferredServices = array_merge($this->deferredServices, $services); } /** * Determine if the given service is a deferred service. * * @param string $service * @return bool */ public function isDeferredService($service) { return isset($this->deferredServices[$service]); } /** * Configure the real-time facade namespace. * * @param string $namespace * @return void */ public function provideFacades($namespace) { AliasLoader::setFacadeNamespace($namespace); } /** * Get the current application locale. * * @return string */ public function getLocale() { return $this['config']->get('app.locale'); } /** * Get the current application locale. * * @return string */ public function currentLocale() { return $this->getLocale(); } /** * Get the current application fallback locale. * * @return string */ public function getFallbackLocale() { return $this['config']->get('app.fallback_locale'); } /** * Set the current application locale. * * @param string $locale * @return void */ public function setLocale($locale) { $this['config']->set('app.locale', $locale); $this['translator']->setLocale($locale); $this['events']->dispatch(new LocaleUpdated($locale)); } /** * Set the current application fallback locale. * * @param string $fallbackLocale * @return void */ public function setFallbackLocale($fallbackLocale) { $this['config']->set('app.fallback_locale', $fallbackLocale); $this['translator']->setFallback($fallbackLocale); } /** * Determine if the application locale is the given locale. * * @param string $locale * @return bool */ public function isLocale($locale) { return $this->getLocale() == $locale; } /** * Register the core class aliases in the container. * * @return void */ public function registerCoreContainerAliases() { foreach ([ 'app' => [self::class, \Illuminate\Contracts\Container\Container::class, \Illuminate\Contracts\Foundation\Application::class, \Psr\Container\ContainerInterface::class], 'auth' => [\Illuminate\Auth\AuthManager::class, \Illuminate\Contracts\Auth\Factory::class], 'auth.driver' => [\Illuminate\Contracts\Auth\Guard::class], 'blade.compiler' => [\Illuminate\View\Compilers\BladeCompiler::class], 'cache' => [\Illuminate\Cache\CacheManager::class, \Illuminate\Contracts\Cache\Factory::class], 'cache.store' => [\Illuminate\Cache\Repository::class, \Illuminate\Contracts\Cache\Repository::class, \Psr\SimpleCache\CacheInterface::class], 'cache.psr6' => [\Symfony\Component\Cache\Adapter\Psr16Adapter::class, \Symfony\Component\Cache\Adapter\AdapterInterface::class, \Psr\Cache\CacheItemPoolInterface::class], 'config' => [\Illuminate\Config\Repository::class, \Illuminate\Contracts\Config\Repository::class], 'cookie' => [\Illuminate\Cookie\CookieJar::class, \Illuminate\Contracts\Cookie\Factory::class, \Illuminate\Contracts\Cookie\QueueingFactory::class], 'db' => [\Illuminate\Database\DatabaseManager::class, \Illuminate\Database\ConnectionResolverInterface::class], 'db.connection' => [\Illuminate\Database\Connection::class, \Illuminate\Database\ConnectionInterface::class], 'db.schema' => [\Illuminate\Database\Schema\Builder::class], 'encrypter' => [\Illuminate\Encryption\Encrypter::class, \Illuminate\Contracts\Encryption\Encrypter::class, \Illuminate\Contracts\Encryption\StringEncrypter::class], 'events' => [\Illuminate\Events\Dispatcher::class, \Illuminate\Contracts\Events\Dispatcher::class], 'files' => [\Illuminate\Filesystem\Filesystem::class], 'filesystem' => [\Illuminate\Filesystem\FilesystemManager::class, \Illuminate\Contracts\Filesystem\Factory::class], 'filesystem.disk' => [\Illuminate\Contracts\Filesystem\Filesystem::class], 'filesystem.cloud' => [\Illuminate\Contracts\Filesystem\Cloud::class], 'hash' => [\Illuminate\Hashing\HashManager::class], 'hash.driver' => [\Illuminate\Contracts\Hashing\Hasher::class], 'translator' => [\Illuminate\Translation\Translator::class, \Illuminate\Contracts\Translation\Translator::class], 'log' => [\Illuminate\Log\LogManager::class, \Psr\Log\LoggerInterface::class], 'mail.manager' => [\Illuminate\Mail\MailManager::class, \Illuminate\Contracts\Mail\Factory::class], 'mailer' => [\Illuminate\Mail\Mailer::class, \Illuminate\Contracts\Mail\Mailer::class, \Illuminate\Contracts\Mail\MailQueue::class], 'auth.password' => [\Illuminate\Auth\Passwords\PasswordBrokerManager::class, \Illuminate\Contracts\Auth\PasswordBrokerFactory::class], 'auth.password.broker' => [\Illuminate\Auth\Passwords\PasswordBroker::class, \Illuminate\Contracts\Auth\PasswordBroker::class], 'queue' => [\Illuminate\Queue\QueueManager::class, \Illuminate\Contracts\Queue\Factory::class, \Illuminate\Contracts\Queue\Monitor::class], 'queue.connection' => [\Illuminate\Contracts\Queue\Queue::class], 'queue.failer' => [\Illuminate\Queue\Failed\FailedJobProviderInterface::class], 'redirect' => [\Illuminate\Routing\Redirector::class], 'redis' => [\Illuminate\Redis\RedisManager::class, \Illuminate\Contracts\Redis\Factory::class], 'redis.connection' => [\Illuminate\Redis\Connections\Connection::class, \Illuminate\Contracts\Redis\Connection::class], 'request' => [\Illuminate\Http\Request::class, \Symfony\Component\HttpFoundation\Request::class], 'router' => [\Illuminate\Routing\Router::class, \Illuminate\Contracts\Routing\Registrar::class, \Illuminate\Contracts\Routing\BindingRegistrar::class], 'session' => [\Illuminate\Session\SessionManager::class], 'session.store' => [\Illuminate\Session\Store::class, \Illuminate\Contracts\Session\Session::class], 'url' => [\Illuminate\Routing\UrlGenerator::class, \Illuminate\Contracts\Routing\UrlGenerator::class], 'validator' => [\Illuminate\Validation\Factory::class, \Illuminate\Contracts\Validation\Factory::class], 'view' => [\Illuminate\View\Factory::class, \Illuminate\Contracts\View\Factory::class], ] as $key => $aliases) { foreach ($aliases as $alias) { $this->alias($key, $alias); } } } /** * Flush the container of all bindings and resolved instances. * * @return void */ public function flush() { parent::flush(); $this->buildStack = []; $this->loadedProviders = []; $this->bootedCallbacks = []; $this->bootingCallbacks = []; $this->deferredServices = []; $this->reboundCallbacks = []; $this->serviceProviders = []; $this->resolvingCallbacks = []; $this->terminatingCallbacks = []; $this->beforeResolvingCallbacks = []; $this->afterResolvingCallbacks = []; $this->globalBeforeResolvingCallbacks = []; $this->globalResolvingCallbacks = []; $this->globalAfterResolvingCallbacks = []; } /** * Get the application namespace. * * @return string * * @throws \RuntimeException */ public function getNamespace() { if (! is_null($this->namespace)) { return $this->namespace; } $composer = json_decode(file_get_contents($this->basePath('composer.json')), true); foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) { foreach ((array) $path as $pathChoice) { if (realpath($this->path()) === realpath($this->basePath($pathChoice))) { return $this->namespace = $namespace; } } } throw new RuntimeException('Unable to detect application namespace.'); } } src/Illuminate/Foundation/Inspiring.php 0000644 00000015062 15107327037 0014230 0 ustar 00 <?php namespace Illuminate\Foundation; use Illuminate\Support\Collection; /* .~))>> .~)>> .~))))>>> .~))>> ___ .~))>>)))>> .-~))>> .~)))))>> .-~))>>)> .~)))>>))))>> .-~)>>)> ) .~))>>))))>> .-~)))))>>)> ( )@@*) //)>)))))) .-~))))>>)> ).@(@@ //))>>))) .-~))>>)))))>>)> (( @.@). //))))) .-~)>>)))))>>)> )) )@@*.@@ ) //)>))) //))))))>>))))>>)> (( ((@@@.@@ |/))))) //)))))>>)))>>)> )) @@*. )@@ ) (\_(\-\b |))>)) //)))>>)))))))>>)> (( @@@(.@(@ . _/`-` ~|b |>))) //)>>)))))))>>)> )* @@@ )@* (@) (@) /\b|))) //))))))>>))))>> (( @. )@( @ . _/ / / \b)) //))>>)))))>>>_._ )@@ (@@*)@@. (6///6)- / ^ \b)//))))))>>)))>> ~~-. ( @jgs@@. @@@.*@_ VvvvvV// ^ \b/)>>))))>> _. `bb ((@@ @@@*.(@@ . - | o |' \ ( ^ \b)))>> .' b`, ((@@).*@@ )@ ) \^^^/ (( ^ ~)_ \ / b `, (@@. (@@ ). `-' ((( ^ `\ \ \ \ \| b `. (*.@* / (((( \| | | \ . b `. / / ((((( \ \ / _.-~\ Y, b ; / / / (((((( \ \.-~ _.`" _.-~`, b ; / / `(((((() ) (((((~ `, b ; _/ _/ `"""/ /' ; b ; _.-~_.-~ / /' _.'~bb _.' ((((~~ / /' _.'~bb.--~ (((( __.-~bb.-~ .' b .~~ :bb ,' ~~~~ */ class Inspiring { /** * Get an inspiring quote. * * Taylor & Dayle made this commit from Jungfraujoch. (11,333 ft.) * * May McGinnis always control the board. #LaraconUS2015 * * RIP Charlie - Feb 6, 2018 * * @return string */ public static function quote() { return static::quotes() ->map(fn ($quote) => static::formatForConsole($quote)) ->random(); } /** * Get the collection of inspiring quotes. * * @return \Illuminate\Support\Collection */ public static function quotes() { return Collection::make([ 'Act only according to that maxim whereby you can, at the same time, will that it should become a universal law. - Immanuel Kant', 'An unexamined life is not worth living. - Socrates', 'Be present above all else. - Naval Ravikant', 'Do what you can, with what you have, where you are. - Theodore Roosevelt', 'Happiness is not something readymade. It comes from your own actions. - Dalai Lama', 'He who is contented is rich. - Laozi', 'I begin to speak only when I am certain what I will say is not better left unsaid. - Cato the Younger', 'I have not failed. I\'ve just found 10,000 ways that won\'t work. - Thomas Edison', 'If you do not have a consistent goal in life, you can not live it in a consistent way. - Marcus Aurelius', 'It is never too late to be what you might have been. - George Eliot', 'It is not the man who has too little, but the man who craves more, that is poor. - Seneca', 'It is quality rather than quantity that matters. - Lucius Annaeus Seneca', 'Knowing is not enough; we must apply. Being willing is not enough; we must do. - Leonardo da Vinci', 'Let all your things have their places; let each part of your business have its time. - Benjamin Franklin', 'Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi', 'No surplus words or unnecessary actions. - Marcus Aurelius', 'Nothing worth having comes easy. - Theodore Roosevelt', 'Order your soul. Reduce your wants. - Augustine', 'People find pleasure in different ways. I find it in keeping my mind clear. - Marcus Aurelius', 'Simplicity is an acquired taste. - Katharine Gerould', 'Simplicity is the consequence of refined emotions. - Jean D\'Alembert', 'Simplicity is the essence of happiness. - Cedric Bledsoe', 'Simplicity is the ultimate sophistication. - Leonardo da Vinci', 'Smile, breathe, and go slowly. - Thich Nhat Hanh', 'The only way to do great work is to love what you do. - Steve Jobs', 'The whole future lies in uncertainty: live immediately. - Seneca', 'Very little is needed to make a happy life. - Marcus Aurelius', 'Waste no more time arguing what a good man should be, be one. - Marcus Aurelius', 'Well begun is half done. - Aristotle', 'When there is no desire, all things are at peace. - Laozi', 'Walk as if you are kissing the Earth with your feet. - Thich Nhat Hanh', 'Because you are alive, everything is possible. - Thich Nhat Hanh', 'Breathing in, I calm body and mind. Breathing out, I smile. - Thich Nhat Hanh', 'Life is available only in the present moment. - Thich Nhat Hanh', 'The best way to take care of the future is to take care of the present moment. - Thich Nhat Hanh', 'Nothing in life is to be feared, it is only to be understood. Now is the time to understand more, so that we may fear less. - Marie Curie', 'The biggest battle is the war against ignorance. - Mustafa Kemal Atatürk', 'Always remember that you are absolutely unique. Just like everyone else. - Margaret Mead', 'You must be the change you wish to see in the world. - Mahatma Gandhi', ]); } /** * Formats the given quote for a pretty console output. * * @param string $quote * @return string */ protected static function formatForConsole($quote) { [$text, $author] = str($quote)->explode('-'); return sprintf( "\n <options=bold>“ %s ”</>\n <fg=gray>— %s</>\n", trim($text), trim($author), ); } } src/Illuminate/Foundation/Testing/DatabaseTransactions.php 0000644 00000003074 15107327037 0020000 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; trait DatabaseTransactions { /** * Handle database transactions on the specified connections. * * @return void */ public function beginDatabaseTransaction() { $database = $this->app->make('db'); $this->app->instance('db.transactions', $transactionsManager = new DatabaseTransactionsManager); foreach ($this->connectionsToTransact() as $name) { $connection = $database->connection($name); $connection->setTransactionManager($transactionsManager); $dispatcher = $connection->getEventDispatcher(); $connection->unsetEventDispatcher(); $connection->beginTransaction(); $connection->setEventDispatcher($dispatcher); } $this->beforeApplicationDestroyed(function () use ($database) { foreach ($this->connectionsToTransact() as $name) { $connection = $database->connection($name); $dispatcher = $connection->getEventDispatcher(); $connection->unsetEventDispatcher(); $connection->rollBack(); $connection->setEventDispatcher($dispatcher); $connection->disconnect(); } }); } /** * The database connections that should have transactions. * * @return array */ protected function connectionsToTransact() { return property_exists($this, 'connectionsToTransact') ? $this->connectionsToTransact : [null]; } } src/Illuminate/Foundation/Testing/DatabaseTransactionsManager.php 0000644 00000002543 15107327037 0021273 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; use Illuminate\Database\DatabaseTransactionsManager as BaseManager; class DatabaseTransactionsManager extends BaseManager { /** * Register a transaction callback. * * @param callable $callback * @return void */ public function addCallback($callback) { // If there are no transactions, we'll run the callbacks right away. Also, we'll run it // right away when we're in test mode and we only have the wrapping transaction. For // every other case, we'll queue up the callback to run after the commit happens. if ($this->callbackApplicableTransactions()->count() === 0) { return $callback(); } $this->transactions->last()->addCallback($callback); } /** * Get the transactions that are applicable to callbacks. * * @return \Illuminate\Support\Collection<int, \Illuminate\Database\DatabaseTransactionRecord> */ public function callbackApplicableTransactions() { return $this->transactions->skip(1)->values(); } /** * Determine if after commit callbacks should be executed for the given transaction level. * * @param int $level * @return bool */ public function afterCommitCallbacksShouldBeExecuted($level) { return $level === 1; } } src/Illuminate/Foundation/Testing/DatabaseTruncation.php 0000644 00000011727 15107327037 0017462 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; use Illuminate\Contracts\Console\Kernel; use Illuminate\Database\ConnectionInterface; use Illuminate\Foundation\Testing\Traits\CanConfigureMigrationCommands; trait DatabaseTruncation { use CanConfigureMigrationCommands; /** * The cached names of the database tables for each connection. * * @var array */ protected static array $allTables; /** * Truncate the database tables for all configured connections. * * @return void */ protected function truncateDatabaseTables(): void { $this->beforeTruncatingDatabase(); // Migrate and seed the database on first run... if (! RefreshDatabaseState::$migrated) { $this->artisan('migrate:fresh', $this->migrateFreshUsing()); $this->app[Kernel::class]->setArtisan(null); RefreshDatabaseState::$migrated = true; return; } // Always clear any test data on subsequent runs... $this->truncateTablesForAllConnections(); if ($seeder = $this->seeder()) { // Use a specific seeder class... $this->artisan('db:seed', ['--class' => $seeder]); } elseif ($this->shouldSeed()) { // Use the default seeder class... $this->artisan('db:seed'); } $this->afterTruncatingDatabase(); } /** * Truncate the database tables for all configured connections. * * @return void */ protected function truncateTablesForAllConnections(): void { $database = $this->app->make('db'); collect($this->connectionsToTruncate()) ->each(function ($name) use ($database) { $connection = $database->connection($name); $connection->getSchemaBuilder()->withoutForeignKeyConstraints( fn () => $this->truncateTablesForConnection($connection, $name) ); }); } /** * Truncate the database tables for the given database connection. * * @param \Illuminate\Database\ConnectionInterface $connection * @param string|null $name * @return void */ protected function truncateTablesForConnection(ConnectionInterface $connection, ?string $name): void { $dispatcher = $connection->getEventDispatcher(); $connection->unsetEventDispatcher(); collect(static::$allTables[$name] ??= $connection->getDoctrineSchemaManager()->listTableNames()) ->when( property_exists($this, 'tablesToTruncate'), fn ($tables) => $tables->intersect($this->tablesToTruncate), fn ($tables) => $tables->diff($this->exceptTables($name)) ) ->filter(fn ($table) => $connection->table($this->withoutTablePrefix($connection, $table))->exists()) ->each(fn ($table) => $connection->table($this->withoutTablePrefix($connection, $table))->truncate()); $connection->setEventDispatcher($dispatcher); } /** * Remove the table prefix from a table name, if it exists. * * @param \Illuminate\Database\ConnectionInterface $connection * @param string $table * @return string */ protected function withoutTablePrefix(ConnectionInterface $connection, string $table) { $prefix = $connection->getTablePrefix(); return strpos($table, $prefix) === 0 ? substr($table, strlen($prefix)) : $table; } /** * The database connections that should have their tables truncated. * * @return array */ protected function connectionsToTruncate(): array { return property_exists($this, 'connectionsToTruncate') ? $this->connectionsToTruncate : [null]; } /** * Get the tables that should not be truncated. * * @param string|null $connectionName * @return array */ protected function exceptTables(?string $connectionName): array { if (property_exists($this, 'exceptTables')) { $migrationsTable = $this->app['config']->get('database.migrations'); if (array_is_list($this->exceptTables ?? [])) { return array_merge( $this->exceptTables ?? [], [$migrationsTable], ); } return array_merge( $this->exceptTables[$connectionName] ?? [], [$migrationsTable], ); } return [$this->app['config']->get('database.migrations')]; } /** * Perform any work that should take place before the database has started truncating. * * @return void */ protected function beforeTruncatingDatabase(): void { // } /** * Perform any work that should take place once the database has finished truncating. * * @return void */ protected function afterTruncatingDatabase(): void { // } } src/Illuminate/Foundation/Testing/WithoutEvents.php 0000644 00000000710 15107327037 0016525 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; use Exception; trait WithoutEvents { /** * Prevent all event handles from being executed. * * @throws \Exception */ public function disableEventsForAllTests() { if (method_exists($this, 'withoutEvents')) { $this->withoutEvents(); } else { throw new Exception('Unable to disable events. ApplicationTrait not used.'); } } } src/Illuminate/Foundation/Testing/DatabaseMigrations.php 0000644 00000001271 15107327037 0017441 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; use Illuminate\Contracts\Console\Kernel; use Illuminate\Foundation\Testing\Traits\CanConfigureMigrationCommands; trait DatabaseMigrations { use CanConfigureMigrationCommands; /** * Define hooks to migrate the database before and after each test. * * @return void */ public function runDatabaseMigrations() { $this->artisan('migrate:fresh', $this->migrateFreshUsing()); $this->app[Kernel::class]->setArtisan(null); $this->beforeApplicationDestroyed(function () { $this->artisan('migrate:rollback'); RefreshDatabaseState::$migrated = false; }); } } src/Illuminate/Foundation/Testing/RefreshDatabase.php 0000644 00000007357 15107327037 0016736 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; use Illuminate\Contracts\Console\Kernel; use Illuminate\Foundation\Testing\Traits\CanConfigureMigrationCommands; trait RefreshDatabase { use CanConfigureMigrationCommands; /** * Define hooks to migrate the database before and after each test. * * @return void */ public function refreshDatabase() { $this->beforeRefreshingDatabase(); $this->usingInMemoryDatabase() ? $this->refreshInMemoryDatabase() : $this->refreshTestDatabase(); $this->afterRefreshingDatabase(); } /** * Determine if an in-memory database is being used. * * @return bool */ protected function usingInMemoryDatabase() { $default = config('database.default'); return config("database.connections.$default.database") === ':memory:'; } /** * Refresh the in-memory database. * * @return void */ protected function refreshInMemoryDatabase() { $this->artisan('migrate', $this->migrateUsing()); $this->app[Kernel::class]->setArtisan(null); } /** * The parameters that should be used when running "migrate". * * @return array */ protected function migrateUsing() { return [ '--seed' => $this->shouldSeed(), '--seeder' => $this->seeder(), ]; } /** * Refresh a conventional test database. * * @return void */ protected function refreshTestDatabase() { if (! RefreshDatabaseState::$migrated) { $this->artisan('migrate:fresh', $this->migrateFreshUsing()); $this->app[Kernel::class]->setArtisan(null); RefreshDatabaseState::$migrated = true; } $this->beginDatabaseTransaction(); } /** * Begin a database transaction on the testing database. * * @return void */ public function beginDatabaseTransaction() { $database = $this->app->make('db'); $this->app->instance('db.transactions', $transactionsManager = new DatabaseTransactionsManager); foreach ($this->connectionsToTransact() as $name) { $connection = $database->connection($name); $connection->setTransactionManager($transactionsManager); $dispatcher = $connection->getEventDispatcher(); $connection->unsetEventDispatcher(); $connection->beginTransaction(); $connection->setEventDispatcher($dispatcher); } $this->beforeApplicationDestroyed(function () use ($database) { foreach ($this->connectionsToTransact() as $name) { $connection = $database->connection($name); $dispatcher = $connection->getEventDispatcher(); $connection->unsetEventDispatcher(); $connection->rollBack(); $connection->setEventDispatcher($dispatcher); $connection->disconnect(); } }); } /** * The database connections that should have transactions. * * @return array */ protected function connectionsToTransact() { return property_exists($this, 'connectionsToTransact') ? $this->connectionsToTransact : [null]; } /** * Perform any work that should take place before the database has started refreshing. * * @return void */ protected function beforeRefreshingDatabase() { // ... } /** * Perform any work that should take place once the database has finished refreshing. * * @return void */ protected function afterRefreshingDatabase() { // ... } } src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php 0000644 00000003715 15107327037 0021572 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Concerns; use Illuminate\Console\OutputStyle; use Illuminate\Contracts\Console\Kernel; use Illuminate\Testing\PendingCommand; trait InteractsWithConsole { /** * Indicates if the console output should be mocked. * * @var bool */ public $mockConsoleOutput = true; /** * All of the expected output lines. * * @var array */ public $expectedOutput = []; /** * All of the expected text to be present in the output. * * @var array */ public $expectedOutputSubstrings = []; /** * All of the output lines that aren't expected to be displayed. * * @var array */ public $unexpectedOutput = []; /** * All of the text that is not expected to be present in the output. * * @var array */ public $unexpectedOutputSubstrings = []; /** * All of the expected output tables. * * @var array */ public $expectedTables = []; /** * All of the expected questions. * * @var array */ public $expectedQuestions = []; /** * All of the expected choice questions. * * @var array */ public $expectedChoices = []; /** * Call artisan command and return code. * * @param string $command * @param array $parameters * @return \Illuminate\Testing\PendingCommand|int */ public function artisan($command, $parameters = []) { if (! $this->mockConsoleOutput) { return $this->app[Kernel::class]->call($command, $parameters); } return new PendingCommand($this, $this->app, $command, $parameters); } /** * Disable mocking the console output. * * @return $this */ protected function withoutMockingConsoleOutput() { $this->mockConsoleOutput = false; $this->app->offsetUnset(OutputStyle::class); return $this; } } src/Illuminate/Foundation/Testing/Concerns/InteractsWithRedis.php 0000644 00000006001 15107327037 0021225 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Concerns; use Exception; use Illuminate\Foundation\Application; use Illuminate\Redis\RedisManager; use Illuminate\Support\Env; trait InteractsWithRedis { /** * Indicate connection failed if redis is not available. * * @var bool */ private static $connectionFailedOnceWithDefaultsSkip = false; /** * Redis manager instance. * * @var \Illuminate\Redis\RedisManager[] */ private $redis; /** * Setup redis connection. * * @return void */ public function setUpRedis() { if (! extension_loaded('redis')) { $this->markTestSkipped('The redis extension is not installed. Please install the extension to enable '.__CLASS__); } if (static::$connectionFailedOnceWithDefaultsSkip) { $this->markTestSkipped('Trying default host/port failed, please set environment variable REDIS_HOST & REDIS_PORT to enable '.__CLASS__); } $app = $this->app ?? new Application; $host = Env::get('REDIS_HOST', '127.0.0.1'); $port = Env::get('REDIS_PORT', 6379); foreach (static::redisDriverProvider() as $driver) { $this->redis[$driver[0]] = new RedisManager($app, $driver[0], [ 'cluster' => false, 'options' => [ 'prefix' => 'test_', ], 'default' => [ 'host' => $host, 'port' => $port, 'database' => 5, 'timeout' => 0.5, 'name' => 'default', ], ]); } try { $this->redis['phpredis']->connection()->flushdb(); } catch (Exception) { if ($host === '127.0.0.1' && $port === 6379 && Env::get('REDIS_HOST') === null) { static::$connectionFailedOnceWithDefaultsSkip = true; $this->markTestSkipped('Trying default host/port failed, please set environment variable REDIS_HOST & REDIS_PORT to enable '.__CLASS__); } } } /** * Teardown redis connection. * * @return void */ public function tearDownRedis() { if (isset($this->redis['phpredis'])) { $this->redis['phpredis']->connection()->flushdb(); } foreach (static::redisDriverProvider() as $driver) { if (isset($this->redis[$driver[0]])) { $this->redis[$driver[0]]->connection()->disconnect(); } } } /** * Get redis driver provider. * * @return array */ public static function redisDriverProvider() { return [ ['predis'], ['phpredis'], ]; } /** * Run test if redis is available. * * @param callable $callback * @return void */ public function ifRedisAvailable($callback) { $this->setUpRedis(); $callback(); $this->tearDownRedis(); } } src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php 0000644 00000044540 15107327037 0021114 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Concerns; use Illuminate\Contracts\Http\Kernel as HttpKernel; use Illuminate\Cookie\CookieValuePrefix; use Illuminate\Http\Request; use Illuminate\Testing\LoggedExceptionCollection; use Illuminate\Testing\TestResponse; use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile; use Symfony\Component\HttpFoundation\Request as SymfonyRequest; trait MakesHttpRequests { /** * Additional headers for the request. * * @var array */ protected $defaultHeaders = []; /** * Additional cookies for the request. * * @var array */ protected $defaultCookies = []; /** * Additional cookies will not be encrypted for the request. * * @var array */ protected $unencryptedCookies = []; /** * Additional server variables for the request. * * @var array */ protected $serverVariables = []; /** * Indicates whether redirects should be followed. * * @var bool */ protected $followRedirects = false; /** * Indicates whether cookies should be encrypted. * * @var bool */ protected $encryptCookies = true; /** * Indicated whether JSON requests should be performed "with credentials" (cookies). * * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials * * @var bool */ protected $withCredentials = false; /** * The latest test response (if any). * * @var \Illuminate\Testing\TestResponse|null */ public static $latestResponse; /** * Define additional headers to be sent with the request. * * @param array $headers * @return $this */ public function withHeaders(array $headers) { $this->defaultHeaders = array_merge($this->defaultHeaders, $headers); return $this; } /** * Add a header to be sent with the request. * * @param string $name * @param string $value * @return $this */ public function withHeader(string $name, string $value) { $this->defaultHeaders[$name] = $value; return $this; } /** * Add an authorization token for the request. * * @param string $token * @param string $type * @return $this */ public function withToken(string $token, string $type = 'Bearer') { return $this->withHeader('Authorization', $type.' '.$token); } /** * Add a basic authentication header to the request with the given credentials. * * @param string $username * @param string $password * @return $this */ public function withBasicAuth(string $username, string $password) { return $this->withToken(base64_encode("$username:$password"), 'Basic'); } /** * Remove the authorization token from the request. * * @return $this */ public function withoutToken() { unset($this->defaultHeaders['Authorization']); return $this; } /** * Flush all the configured headers. * * @return $this */ public function flushHeaders() { $this->defaultHeaders = []; return $this; } /** * Define a set of server variables to be sent with the requests. * * @param array $server * @return $this */ public function withServerVariables(array $server) { $this->serverVariables = $server; return $this; } /** * Disable middleware for the test. * * @param string|array|null $middleware * @return $this */ public function withoutMiddleware($middleware = null) { if (is_null($middleware)) { $this->app->instance('middleware.disable', true); return $this; } foreach ((array) $middleware as $abstract) { $this->app->instance($abstract, new class { public function handle($request, $next) { return $next($request); } }); } return $this; } /** * Enable the given middleware for the test. * * @param string|array|null $middleware * @return $this */ public function withMiddleware($middleware = null) { if (is_null($middleware)) { unset($this->app['middleware.disable']); return $this; } foreach ((array) $middleware as $abstract) { unset($this->app[$abstract]); } return $this; } /** * Define additional cookies to be sent with the request. * * @param array $cookies * @return $this */ public function withCookies(array $cookies) { $this->defaultCookies = array_merge($this->defaultCookies, $cookies); return $this; } /** * Add a cookie to be sent with the request. * * @param string $name * @param string $value * @return $this */ public function withCookie(string $name, string $value) { $this->defaultCookies[$name] = $value; return $this; } /** * Define additional cookies will not be encrypted before sending with the request. * * @param array $cookies * @return $this */ public function withUnencryptedCookies(array $cookies) { $this->unencryptedCookies = array_merge($this->unencryptedCookies, $cookies); return $this; } /** * Add a cookie will not be encrypted before sending with the request. * * @param string $name * @param string $value * @return $this */ public function withUnencryptedCookie(string $name, string $value) { $this->unencryptedCookies[$name] = $value; return $this; } /** * Automatically follow any redirects returned from the response. * * @return $this */ public function followingRedirects() { $this->followRedirects = true; return $this; } /** * Include cookies and authorization headers for JSON requests. * * @return $this */ public function withCredentials() { $this->withCredentials = true; return $this; } /** * Disable automatic encryption of cookie values. * * @return $this */ public function disableCookieEncryption() { $this->encryptCookies = false; return $this; } /** * Set the referer header and previous URL session value in order to simulate a previous request. * * @param string $url * @return $this */ public function from(string $url) { $this->app['session']->setPreviousUrl($url); return $this->withHeader('referer', $url); } /** * Set the Precognition header to "true". * * @return $this */ public function withPrecognition() { return $this->withHeader('Precognition', 'true'); } /** * Visit the given URI with a GET request. * * @param string $uri * @param array $headers * @return \Illuminate\Testing\TestResponse */ public function get($uri, array $headers = []) { $server = $this->transformHeadersToServerVars($headers); $cookies = $this->prepareCookiesForRequest(); return $this->call('GET', $uri, [], $cookies, [], $server); } /** * Visit the given URI with a GET request, expecting a JSON response. * * @param string $uri * @param array $headers * @param int $options * @return \Illuminate\Testing\TestResponse */ public function getJson($uri, array $headers = [], $options = 0) { return $this->json('GET', $uri, [], $headers, $options); } /** * Visit the given URI with a POST request. * * @param string $uri * @param array $data * @param array $headers * @return \Illuminate\Testing\TestResponse */ public function post($uri, array $data = [], array $headers = []) { $server = $this->transformHeadersToServerVars($headers); $cookies = $this->prepareCookiesForRequest(); return $this->call('POST', $uri, $data, $cookies, [], $server); } /** * Visit the given URI with a POST request, expecting a JSON response. * * @param string $uri * @param array $data * @param array $headers * @param int $options * @return \Illuminate\Testing\TestResponse */ public function postJson($uri, array $data = [], array $headers = [], $options = 0) { return $this->json('POST', $uri, $data, $headers, $options); } /** * Visit the given URI with a PUT request. * * @param string $uri * @param array $data * @param array $headers * @return \Illuminate\Testing\TestResponse */ public function put($uri, array $data = [], array $headers = []) { $server = $this->transformHeadersToServerVars($headers); $cookies = $this->prepareCookiesForRequest(); return $this->call('PUT', $uri, $data, $cookies, [], $server); } /** * Visit the given URI with a PUT request, expecting a JSON response. * * @param string $uri * @param array $data * @param array $headers * @param int $options * @return \Illuminate\Testing\TestResponse */ public function putJson($uri, array $data = [], array $headers = [], $options = 0) { return $this->json('PUT', $uri, $data, $headers, $options); } /** * Visit the given URI with a PATCH request. * * @param string $uri * @param array $data * @param array $headers * @return \Illuminate\Testing\TestResponse */ public function patch($uri, array $data = [], array $headers = []) { $server = $this->transformHeadersToServerVars($headers); $cookies = $this->prepareCookiesForRequest(); return $this->call('PATCH', $uri, $data, $cookies, [], $server); } /** * Visit the given URI with a PATCH request, expecting a JSON response. * * @param string $uri * @param array $data * @param array $headers * @param int $options * @return \Illuminate\Testing\TestResponse */ public function patchJson($uri, array $data = [], array $headers = [], $options = 0) { return $this->json('PATCH', $uri, $data, $headers, $options); } /** * Visit the given URI with a DELETE request. * * @param string $uri * @param array $data * @param array $headers * @return \Illuminate\Testing\TestResponse */ public function delete($uri, array $data = [], array $headers = []) { $server = $this->transformHeadersToServerVars($headers); $cookies = $this->prepareCookiesForRequest(); return $this->call('DELETE', $uri, $data, $cookies, [], $server); } /** * Visit the given URI with a DELETE request, expecting a JSON response. * * @param string $uri * @param array $data * @param array $headers * @param int $options * @return \Illuminate\Testing\TestResponse */ public function deleteJson($uri, array $data = [], array $headers = [], $options = 0) { return $this->json('DELETE', $uri, $data, $headers, $options); } /** * Visit the given URI with an OPTIONS request. * * @param string $uri * @param array $data * @param array $headers * @return \Illuminate\Testing\TestResponse */ public function options($uri, array $data = [], array $headers = []) { $server = $this->transformHeadersToServerVars($headers); $cookies = $this->prepareCookiesForRequest(); return $this->call('OPTIONS', $uri, $data, $cookies, [], $server); } /** * Visit the given URI with an OPTIONS request, expecting a JSON response. * * @param string $uri * @param array $data * @param array $headers * @param int $options * @return \Illuminate\Testing\TestResponse */ public function optionsJson($uri, array $data = [], array $headers = [], $options = 0) { return $this->json('OPTIONS', $uri, $data, $headers, $options); } /** * Visit the given URI with a HEAD request. * * @param string $uri * @param array $headers * @return \Illuminate\Testing\TestResponse */ public function head($uri, array $headers = []) { $server = $this->transformHeadersToServerVars($headers); $cookies = $this->prepareCookiesForRequest(); return $this->call('HEAD', $uri, [], $cookies, [], $server); } /** * Call the given URI with a JSON request. * * @param string $method * @param string $uri * @param array $data * @param array $headers * @param int $options * @return \Illuminate\Testing\TestResponse */ public function json($method, $uri, array $data = [], array $headers = [], $options = 0) { $files = $this->extractFilesFromDataArray($data); $content = json_encode($data, $options); $headers = array_merge([ 'CONTENT_LENGTH' => mb_strlen($content, '8bit'), 'CONTENT_TYPE' => 'application/json', 'Accept' => 'application/json', ], $headers); return $this->call( $method, $uri, [], $this->prepareCookiesForJsonRequest(), $files, $this->transformHeadersToServerVars($headers), $content ); } /** * Call the given URI and return the Response. * * @param string $method * @param string $uri * @param array $parameters * @param array $cookies * @param array $files * @param array $server * @param string|null $content * @return \Illuminate\Testing\TestResponse */ public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null) { $kernel = $this->app->make(HttpKernel::class); $files = array_merge($files, $this->extractFilesFromDataArray($parameters)); $symfonyRequest = SymfonyRequest::create( $this->prepareUrlForRequest($uri), $method, $parameters, $cookies, $files, array_replace($this->serverVariables, $server), $content ); $response = $kernel->handle( $request = Request::createFromBase($symfonyRequest) ); $kernel->terminate($request, $response); if ($this->followRedirects) { $response = $this->followRedirects($response); } return static::$latestResponse = $this->createTestResponse($response); } /** * Turn the given URI into a fully qualified URL. * * @param string $uri * @return string */ protected function prepareUrlForRequest($uri) { if (str_starts_with($uri, '/')) { $uri = substr($uri, 1); } return trim(url($uri), '/'); } /** * Transform headers array to array of $_SERVER vars with HTTP_* format. * * @param array $headers * @return array */ protected function transformHeadersToServerVars(array $headers) { return collect(array_merge($this->defaultHeaders, $headers))->mapWithKeys(function ($value, $name) { $name = strtr(strtoupper($name), '-', '_'); return [$this->formatServerHeaderKey($name) => $value]; })->all(); } /** * Format the header name for the server array. * * @param string $name * @return string */ protected function formatServerHeaderKey($name) { if (! str_starts_with($name, 'HTTP_') && $name !== 'CONTENT_TYPE' && $name !== 'REMOTE_ADDR') { return 'HTTP_'.$name; } return $name; } /** * Extract the file uploads from the given data array. * * @param array $data * @return array */ protected function extractFilesFromDataArray(&$data) { $files = []; foreach ($data as $key => $value) { if ($value instanceof SymfonyUploadedFile) { $files[$key] = $value; unset($data[$key]); } if (is_array($value)) { $files[$key] = $this->extractFilesFromDataArray($value); $data[$key] = $value; } } return $files; } /** * If enabled, encrypt cookie values for request. * * @return array */ protected function prepareCookiesForRequest() { if (! $this->encryptCookies) { return array_merge($this->defaultCookies, $this->unencryptedCookies); } return collect($this->defaultCookies)->map(function ($value, $key) { return encrypt(CookieValuePrefix::create($key, app('encrypter')->getKey()).$value, false); })->merge($this->unencryptedCookies)->all(); } /** * If enabled, add cookies for JSON requests. * * @return array */ protected function prepareCookiesForJsonRequest() { return $this->withCredentials ? $this->prepareCookiesForRequest() : []; } /** * Follow a redirect chain until a non-redirect is received. * * @param \Illuminate\Http\Response|\Illuminate\Testing\TestResponse $response * @return \Illuminate\Http\Response|\Illuminate\Testing\TestResponse */ protected function followRedirects($response) { $this->followRedirects = false; while ($response->isRedirect()) { $response = $this->get($response->headers->get('Location')); } return $response; } /** * Create the test response instance from the given response. * * @param \Illuminate\Http\Response $response * @return \Illuminate\Testing\TestResponse */ protected function createTestResponse($response) { return tap(TestResponse::fromBaseResponse($response), function ($response) { $response->withExceptions( $this->app->bound(LoggedExceptionCollection::class) ? $this->app->make(LoggedExceptionCollection::class) : new LoggedExceptionCollection ); }); } } src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php 0000644 00000004532 15107327037 0021263 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Concerns; use Illuminate\Support\Facades\View as ViewFacade; use Illuminate\Support\MessageBag; use Illuminate\Support\ViewErrorBag; use Illuminate\Testing\TestComponent; use Illuminate\Testing\TestView; use Illuminate\View\View; trait InteractsWithViews { /** * Create a new TestView from the given view. * * @param string $view * @param \Illuminate\Contracts\Support\Arrayable|array $data * @return \Illuminate\Testing\TestView */ protected function view(string $view, $data = []) { return new TestView(view($view, $data)); } /** * Render the contents of the given Blade template string. * * @param string $template * @param \Illuminate\Contracts\Support\Arrayable|array $data * @return \Illuminate\Testing\TestView */ protected function blade(string $template, $data = []) { $tempDirectory = sys_get_temp_dir(); if (! in_array($tempDirectory, ViewFacade::getFinder()->getPaths())) { ViewFacade::addLocation(sys_get_temp_dir()); } $tempFileInfo = pathinfo(tempnam($tempDirectory, 'laravel-blade')); $tempFile = $tempFileInfo['dirname'].'/'.$tempFileInfo['filename'].'.blade.php'; file_put_contents($tempFile, $template); return new TestView(view($tempFileInfo['filename'], $data)); } /** * Render the given view component. * * @param string $componentClass * @param \Illuminate\Contracts\Support\Arrayable|array $data * @return \Illuminate\Testing\TestComponent */ protected function component(string $componentClass, $data = []) { $component = $this->app->make($componentClass, $data); $view = value($component->resolveView(), $data); $view = $view instanceof View ? $view->with($component->data()) : view($view, $component->data()); return new TestComponent($component, $view); } /** * Populate the shared view error bag with the given errors. * * @param array $errors * @param string $key * @return $this */ protected function withViewErrors(array $errors, $key = 'default') { ViewFacade::share('errors', (new ViewErrorBag)->put($key, new MessageBag($errors))); return $this; } } src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php 0000644 00000002772 15107327037 0021070 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Concerns; use Illuminate\Foundation\Testing\Wormhole; use Illuminate\Support\Carbon; trait InteractsWithTime { /** * Freeze time. * * @param callable|null $callback * @return mixed */ public function freezeTime($callback = null) { return $this->travelTo(Carbon::now(), $callback); } /** * Freeze time at the beginning of the current second. * * @param callable|null $callback * @return mixed */ public function freezeSecond($callback = null) { return $this->travelTo(Carbon::now()->startOfSecond(), $callback); } /** * Begin travelling to another time. * * @param int $value * @return \Illuminate\Foundation\Testing\Wormhole */ public function travel($value) { return new Wormhole($value); } /** * Travel to another time. * * @param \DateTimeInterface|\Closure|\Illuminate\Support\Carbon|string|bool|null $date * @param callable|null $callback * @return mixed */ public function travelTo($date, $callback = null) { Carbon::setTestNow($date); if ($callback) { return tap($callback($date), function () { Carbon::setTestNow(); }); } } /** * Travel back to the current time. * * @return \DateTimeInterface */ public function travelBack() { return Wormhole::back(); } } src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php 0000644 00000011375 15107327037 0022113 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Concerns; use Closure; use Illuminate\Foundation\Mix; use Illuminate\Foundation\Vite; use Illuminate\Support\Facades\Facade; use Illuminate\Support\HtmlString; use Mockery; trait InteractsWithContainer { /** * The original Vite handler. * * @var \Illuminate\Foundation\Vite|null */ protected $originalVite; /** * The original Laravel Mix handler. * * @var \Illuminate\Foundation\Mix|null */ protected $originalMix; /** * Register an instance of an object in the container. * * @param string $abstract * @param object $instance * @return object */ protected function swap($abstract, $instance) { return $this->instance($abstract, $instance); } /** * Register an instance of an object in the container. * * @param string $abstract * @param object $instance * @return object */ protected function instance($abstract, $instance) { $this->app->instance($abstract, $instance); return $instance; } /** * Mock an instance of an object in the container. * * @param string $abstract * @param \Closure|null $mock * @return \Mockery\MockInterface */ protected function mock($abstract, Closure $mock = null) { return $this->instance($abstract, Mockery::mock(...array_filter(func_get_args()))); } /** * Mock a partial instance of an object in the container. * * @param string $abstract * @param \Closure|null $mock * @return \Mockery\MockInterface */ protected function partialMock($abstract, Closure $mock = null) { return $this->instance($abstract, Mockery::mock(...array_filter(func_get_args()))->makePartial()); } /** * Spy an instance of an object in the container. * * @param string $abstract * @param \Closure|null $mock * @return \Mockery\MockInterface */ protected function spy($abstract, Closure $mock = null) { return $this->instance($abstract, Mockery::spy(...array_filter(func_get_args()))); } /** * Instruct the container to forget a previously mocked / spied instance of an object. * * @param string $abstract * @return $this */ protected function forgetMock($abstract) { $this->app->forgetInstance($abstract); return $this; } /** * Register an empty handler for Vite in the container. * * @return $this */ protected function withoutVite() { if ($this->originalVite == null) { $this->originalVite = app(Vite::class); } Facade::clearResolvedInstance(Vite::class); $this->swap(Vite::class, new class { public function __invoke() { return ''; } public function __call($name, $arguments) { return ''; } public function __toString() { return ''; } public function useIntegrityKey() { return $this; } public function useBuildDirectory() { return $this; } public function useHotFile() { return $this; } public function withEntryPoints() { return $this; } public function useScriptTagAttributes() { return $this; } public function useStyleTagAttributes() { return $this; } public function preloadedAssets() { return []; } }); return $this; } /** * Restore Vite in the container. * * @return $this */ protected function withVite() { if ($this->originalVite) { $this->app->instance(Vite::class, $this->originalVite); } return $this; } /** * Register an empty handler for Laravel Mix in the container. * * @return $this */ protected function withoutMix() { if ($this->originalMix == null) { $this->originalMix = app(Mix::class); } $this->swap(Mix::class, function () { return new HtmlString(''); }); return $this; } /** * Restore Laravel Mix in the container. * * @return $this */ protected function withMix() { if ($this->originalMix) { $this->app->instance(Mix::class, $this->originalMix); } return $this; } } src/Illuminate/Foundation/Testing/Concerns/InteractsWithExceptionHandling.php 0000644 00000013041 15107327037 0023564 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Concerns; use Closure; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Testing\Assert; use Illuminate\Validation\ValidationException; use Symfony\Component\Console\Application as ConsoleApplication; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Throwable; trait InteractsWithExceptionHandling { /** * The original exception handler. * * @var \Illuminate\Contracts\Debug\ExceptionHandler|null */ protected $originalExceptionHandler; /** * Restore exception handling. * * @return $this */ protected function withExceptionHandling() { if ($this->originalExceptionHandler) { $this->app->instance(ExceptionHandler::class, $this->originalExceptionHandler); } return $this; } /** * Only handle the given exceptions via the exception handler. * * @param array $exceptions * @return $this */ protected function handleExceptions(array $exceptions) { return $this->withoutExceptionHandling($exceptions); } /** * Only handle validation exceptions via the exception handler. * * @return $this */ protected function handleValidationExceptions() { return $this->handleExceptions([ValidationException::class]); } /** * Disable exception handling for the test. * * @param array $except * @return $this */ protected function withoutExceptionHandling(array $except = []) { if ($this->originalExceptionHandler == null) { $this->originalExceptionHandler = app(ExceptionHandler::class); } $this->app->instance(ExceptionHandler::class, new class($this->originalExceptionHandler, $except) implements ExceptionHandler { protected $except; protected $originalHandler; /** * Create a new class instance. * * @param \Illuminate\Contracts\Debug\ExceptionHandler $originalHandler * @param array $except * @return void */ public function __construct($originalHandler, $except = []) { $this->except = $except; $this->originalHandler = $originalHandler; } /** * Report or log an exception. * * @param \Throwable $e * @return void * * @throws \Exception */ public function report(Throwable $e) { // } /** * Determine if the exception should be reported. * * @param \Throwable $e * @return bool */ public function shouldReport(Throwable $e) { return false; } /** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Throwable $e * @return \Symfony\Component\HttpFoundation\Response * * @throws \Throwable */ public function render($request, Throwable $e) { foreach ($this->except as $class) { if ($e instanceof $class) { return $this->originalHandler->render($request, $e); } } if ($e instanceof NotFoundHttpException) { throw new NotFoundHttpException( "{$request->method()} {$request->url()}", $e, is_int($e->getCode()) ? $e->getCode() : 0 ); } throw $e; } /** * Render an exception to the console. * * @param \Symfony\Component\Console\Output\OutputInterface $output * @param \Throwable $e * @return void */ public function renderForConsole($output, Throwable $e) { (new ConsoleApplication)->renderThrowable($e, $output); } }); return $this; } /** * Assert that the given callback throws an exception with the given message when invoked. * * @param \Closure $test * @param class-string<\Throwable> $expectedClass * @param string|null $expectedMessage * @return $this */ protected function assertThrows(Closure $test, string $expectedClass = Throwable::class, ?string $expectedMessage = null) { try { $test(); $thrown = false; } catch (Throwable $exception) { $thrown = $exception instanceof $expectedClass; $actualMessage = $exception->getMessage(); } Assert::assertTrue( $thrown, sprintf('Failed asserting that exception of type "%s" was thrown.', $expectedClass) ); if (isset($expectedMessage)) { if (! isset($actualMessage)) { Assert::fail( sprintf( 'Failed asserting that exception of type "%s" with message "%s" was thrown.', $expectedClass, $expectedMessage ) ); } else { Assert::assertStringContainsString($expectedMessage, $actualMessage); } } return $this; } } src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php 0000644 00000022744 15107327037 0021677 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Concerns; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Events\QueryExecuted; use Illuminate\Support\Arr; use Illuminate\Support\Facades\DB; use Illuminate\Testing\Constraints\CountInDatabase; use Illuminate\Testing\Constraints\HasInDatabase; use Illuminate\Testing\Constraints\NotSoftDeletedInDatabase; use Illuminate\Testing\Constraints\SoftDeletedInDatabase; use PHPUnit\Framework\Constraint\LogicalNot as ReverseConstraint; trait InteractsWithDatabase { /** * Assert that a given where condition exists in the database. * * @param \Illuminate\Database\Eloquent\Model|string $table * @param array $data * @param string|null $connection * @return $this */ protected function assertDatabaseHas($table, array $data, $connection = null) { $this->assertThat( $this->getTable($table), new HasInDatabase($this->getConnection($connection, $table), $data) ); return $this; } /** * Assert that a given where condition does not exist in the database. * * @param \Illuminate\Database\Eloquent\Model|string $table * @param array $data * @param string|null $connection * @return $this */ protected function assertDatabaseMissing($table, array $data, $connection = null) { $constraint = new ReverseConstraint( new HasInDatabase($this->getConnection($connection, $table), $data) ); $this->assertThat($this->getTable($table), $constraint); return $this; } /** * Assert the count of table entries. * * @param \Illuminate\Database\Eloquent\Model|string $table * @param int $count * @param string|null $connection * @return $this */ protected function assertDatabaseCount($table, int $count, $connection = null) { $this->assertThat( $this->getTable($table), new CountInDatabase($this->getConnection($connection, $table), $count) ); return $this; } /** * Assert that the given table has no entries. * * @param \Illuminate\Database\Eloquent\Model|string $table * @param string|null $connection * @return $this */ protected function assertDatabaseEmpty($table, $connection = null) { $this->assertThat( $this->getTable($table), new CountInDatabase($this->getConnection($connection, $table), 0) ); return $this; } /** * Assert the given record has been "soft deleted". * * @param \Illuminate\Database\Eloquent\Model|string $table * @param array $data * @param string|null $connection * @param string|null $deletedAtColumn * @return $this */ protected function assertSoftDeleted($table, array $data = [], $connection = null, $deletedAtColumn = 'deleted_at') { if ($this->isSoftDeletableModel($table)) { return $this->assertSoftDeleted( $table->getTable(), array_merge($data, [$table->getKeyName() => $table->getKey()]), $table->getConnectionName(), $table->getDeletedAtColumn() ); } $this->assertThat( $this->getTable($table), new SoftDeletedInDatabase( $this->getConnection($connection, $table), $data, $this->getDeletedAtColumn($table, $deletedAtColumn) ) ); return $this; } /** * Assert the given record has not been "soft deleted". * * @param \Illuminate\Database\Eloquent\Model|string $table * @param array $data * @param string|null $connection * @param string|null $deletedAtColumn * @return $this */ protected function assertNotSoftDeleted($table, array $data = [], $connection = null, $deletedAtColumn = 'deleted_at') { if ($this->isSoftDeletableModel($table)) { return $this->assertNotSoftDeleted( $table->getTable(), array_merge($data, [$table->getKeyName() => $table->getKey()]), $table->getConnectionName(), $table->getDeletedAtColumn() ); } $this->assertThat( $this->getTable($table), new NotSoftDeletedInDatabase( $this->getConnection($connection, $table), $data, $this->getDeletedAtColumn($table, $deletedAtColumn) ) ); return $this; } /** * Assert the given model exists in the database. * * @param \Illuminate\Database\Eloquent\Model $model * @return $this */ protected function assertModelExists($model) { return $this->assertDatabaseHas( $model->getTable(), [$model->getKeyName() => $model->getKey()], $model->getConnectionName() ); } /** * Assert the given model does not exist in the database. * * @param \Illuminate\Database\Eloquent\Model $model * @return $this */ protected function assertModelMissing($model) { return $this->assertDatabaseMissing( $model->getTable(), [$model->getKeyName() => $model->getKey()], $model->getConnectionName() ); } /** * Specify the number of database queries that should occur throughout the test. * * @param int $expected * @param string|null $connection * @return $this */ public function expectsDatabaseQueryCount($expected, $connection = null) { with($this->getConnection($connection), function ($connectionInstance) use ($expected, $connection) { $actual = 0; $connectionInstance->listen(function (QueryExecuted $event) use (&$actual, $connectionInstance, $connection) { if (is_null($connection) || $connectionInstance === $event->connection) { $actual++; } }); $this->beforeApplicationDestroyed(function () use (&$actual, $expected, $connectionInstance) { $this->assertSame( $actual, $expected, "Expected {$expected} database queries on the [{$connectionInstance->getName()}] connection. {$actual} occurred." ); }); }); return $this; } /** * Determine if the argument is a soft deletable model. * * @param mixed $model * @return bool */ protected function isSoftDeletableModel($model) { return $model instanceof Model && in_array(SoftDeletes::class, class_uses_recursive($model)); } /** * Cast a JSON string to a database compatible type. * * @param array|object|string $value * @return \Illuminate\Contracts\Database\Query\Expression */ public function castAsJson($value) { if ($value instanceof Jsonable) { $value = $value->toJson(); } elseif (is_array($value) || is_object($value)) { $value = json_encode($value); } $value = DB::connection()->getPdo()->quote($value); return DB::raw( DB::connection()->getQueryGrammar()->compileJsonValueCast($value) ); } /** * Get the database connection. * * @param string|null $connection * @param string|null $table * @return \Illuminate\Database\Connection */ protected function getConnection($connection = null, $table = null) { $database = $this->app->make('db'); $connection = $connection ?: $this->getTableConnection($table) ?: $database->getDefaultConnection(); return $database->connection($connection); } /** * Get the table name from the given model or string. * * @param \Illuminate\Database\Eloquent\Model|string $table * @return string */ protected function getTable($table) { return $this->newModelFor($table)?->getTable() ?: $table; } /** * Get the table connection specified in the given model. * * @param \Illuminate\Database\Eloquent\Model|string $table * @return string|null */ protected function getTableConnection($table) { return $this->newModelFor($table)?->getConnectionName(); } /** * Get the table column name used for soft deletes. * * @param string $table * @param string $defaultColumnName * @return string */ protected function getDeletedAtColumn($table, $defaultColumnName = 'deleted_at') { return $this->newModelFor($table)?->getDeletedAtColumn() ?: $defaultColumnName; } /** * Get the model entity from the given model or string. * * @param \Illuminate\Database\Eloquent\Model|string $table * @return \Illuminate\Database\Eloquent\Model|null */ protected function newModelFor($table) { return is_subclass_of($table, Model::class) ? (new $table) : null; } /** * Seed a given database connection. * * @param array|string $class * @return $this */ public function seed($class = 'Database\\Seeders\\DatabaseSeeder') { foreach (Arr::wrap($class) as $class) { $this->artisan('db:seed', ['--class' => $class, '--no-interaction' => true]); } return $this; } } src/Illuminate/Foundation/Testing/Concerns/InteractsWithDeprecationHandling.php 0000644 00000002314 15107327037 0024064 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Concerns; use ErrorException; trait InteractsWithDeprecationHandling { /** * The original deprecation handler. * * @var callable|null */ protected $originalDeprecationHandler; /** * Restore deprecation handling. * * @return $this */ protected function withDeprecationHandling() { if ($this->originalDeprecationHandler) { set_error_handler(tap($this->originalDeprecationHandler, function () { $this->originalDeprecationHandler = null; })); } return $this; } /** * Disable deprecation handling for the test. * * @return $this */ protected function withoutDeprecationHandling() { if ($this->originalDeprecationHandler == null) { $this->originalDeprecationHandler = set_error_handler(function ($level, $message, $file = '', $line = 0) { if (in_array($level, [E_DEPRECATED, E_USER_DEPRECATED]) || (error_reporting() & $level)) { throw new ErrorException($message, 0, $level, $file, $line); } }); } return $this; } } src/Illuminate/Foundation/Testing/Concerns/InteractsWithSession.php 0000644 00000002176 15107327037 0021613 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Concerns; trait InteractsWithSession { /** * Set the session to the given array. * * @param array $data * @return $this */ public function withSession(array $data) { $this->session($data); return $this; } /** * Set the session to the given array. * * @param array $data * @return $this */ public function session(array $data) { $this->startSession(); foreach ($data as $key => $value) { $this->app['session']->put($key, $value); } return $this; } /** * Start the session for the application. * * @return $this */ protected function startSession() { if (! $this->app['session']->isStarted()) { $this->app['session']->start(); } return $this; } /** * Flush all of the current session data. * * @return $this */ public function flushSession() { $this->startSession(); $this->app['session']->flush(); return $this; } } src/Illuminate/Foundation/Testing/Concerns/InteractsWithAuthentication.php 0000644 00000007656 15107327037 0023157 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Concerns; use Illuminate\Contracts\Auth\Authenticatable as UserContract; trait InteractsWithAuthentication { /** * Set the currently logged in user for the application. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param string|null $guard * @return $this */ public function actingAs(UserContract $user, $guard = null) { return $this->be($user, $guard); } /** * Set the currently logged in user for the application. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param string|null $guard * @return $this */ public function be(UserContract $user, $guard = null) { if (isset($user->wasRecentlyCreated) && $user->wasRecentlyCreated) { $user->wasRecentlyCreated = false; } $this->app['auth']->guard($guard)->setUser($user); $this->app['auth']->shouldUse($guard); return $this; } /** * Assert that the user is authenticated. * * @param string|null $guard * @return $this */ public function assertAuthenticated($guard = null) { $this->assertTrue($this->isAuthenticated($guard), 'The user is not authenticated'); return $this; } /** * Assert that the user is not authenticated. * * @param string|null $guard * @return $this */ public function assertGuest($guard = null) { $this->assertFalse($this->isAuthenticated($guard), 'The user is authenticated'); return $this; } /** * Return true if the user is authenticated, false otherwise. * * @param string|null $guard * @return bool */ protected function isAuthenticated($guard = null) { return $this->app->make('auth')->guard($guard)->check(); } /** * Assert that the user is authenticated as the given user. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param string|null $guard * @return $this */ public function assertAuthenticatedAs($user, $guard = null) { $expected = $this->app->make('auth')->guard($guard)->user(); $this->assertNotNull($expected, 'The current user is not authenticated.'); $this->assertInstanceOf( get_class($expected), $user, 'The currently authenticated user is not who was expected' ); $this->assertSame( $expected->getAuthIdentifier(), $user->getAuthIdentifier(), 'The currently authenticated user is not who was expected' ); return $this; } /** * Assert that the given credentials are valid. * * @param array $credentials * @param string|null $guard * @return $this */ public function assertCredentials(array $credentials, $guard = null) { $this->assertTrue( $this->hasCredentials($credentials, $guard), 'The given credentials are invalid.' ); return $this; } /** * Assert that the given credentials are invalid. * * @param array $credentials * @param string|null $guard * @return $this */ public function assertInvalidCredentials(array $credentials, $guard = null) { $this->assertFalse( $this->hasCredentials($credentials, $guard), 'The given credentials are valid.' ); return $this; } /** * Return true if the credentials are valid, false otherwise. * * @param array $credentials * @param string|null $guard * @return bool */ protected function hasCredentials(array $credentials, $guard = null) { $provider = $this->app->make('auth')->guard($guard)->getProvider(); $user = $provider->retrieveByCredentials($credentials); return $user && $provider->validateCredentials($user, $credentials); } } src/Illuminate/Foundation/Testing/TestCase.php 0000644 00000020105 15107327037 0015410 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; use Carbon\CarbonImmutable; use Illuminate\Console\Application as Artisan; use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Bootstrap\HandleExceptions; use Illuminate\Queue\Queue; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\ParallelTesting; use Illuminate\Support\Sleep; use Illuminate\Support\Str; use Illuminate\View\Component; use Mockery; use Mockery\Exception\InvalidCountException; use PHPUnit\Framework\TestCase as BaseTestCase; use Throwable; abstract class TestCase extends BaseTestCase { use Concerns\InteractsWithContainer, Concerns\MakesHttpRequests, Concerns\InteractsWithAuthentication, Concerns\InteractsWithConsole, Concerns\InteractsWithDatabase, Concerns\InteractsWithDeprecationHandling, Concerns\InteractsWithExceptionHandling, Concerns\InteractsWithSession, Concerns\InteractsWithTime, Concerns\InteractsWithViews; /** * The Illuminate application instance. * * @var \Illuminate\Foundation\Application */ protected $app; /** * The callbacks that should be run after the application is created. * * @var array */ protected $afterApplicationCreatedCallbacks = []; /** * The callbacks that should be run before the application is destroyed. * * @var array */ protected $beforeApplicationDestroyedCallbacks = []; /** * The exception thrown while running an application destruction callback. * * @var \Throwable */ protected $callbackException; /** * Indicates if we have made it through the base setUp function. * * @var bool */ protected $setUpHasRun = false; /** * Creates the application. * * Needs to be implemented by subclasses. * * @return \Symfony\Component\HttpKernel\HttpKernelInterface */ abstract public function createApplication(); /** * Setup the test environment. * * @return void */ protected function setUp(): void { static::$latestResponse = null; Facade::clearResolvedInstances(); if (! $this->app) { $this->refreshApplication(); ParallelTesting::callSetUpTestCaseCallbacks($this); } $this->setUpTraits(); foreach ($this->afterApplicationCreatedCallbacks as $callback) { $callback(); } Model::setEventDispatcher($this->app['events']); $this->setUpHasRun = true; } /** * Refresh the application instance. * * @return void */ protected function refreshApplication() { $this->app = $this->createApplication(); } /** * Boot the testing helper traits. * * @return array */ protected function setUpTraits() { $uses = array_flip(class_uses_recursive(static::class)); if (isset($uses[RefreshDatabase::class])) { $this->refreshDatabase(); } if (isset($uses[DatabaseMigrations::class])) { $this->runDatabaseMigrations(); } if (isset($uses[DatabaseTruncation::class])) { $this->truncateDatabaseTables(); } if (isset($uses[DatabaseTransactions::class])) { $this->beginDatabaseTransaction(); } if (isset($uses[WithoutMiddleware::class])) { $this->disableMiddlewareForAllTests(); } if (isset($uses[WithoutEvents::class])) { $this->disableEventsForAllTests(); } if (isset($uses[WithFaker::class])) { $this->setUpFaker(); } foreach ($uses as $trait) { if (method_exists($this, $method = 'setUp'.class_basename($trait))) { $this->{$method}(); } if (method_exists($this, $method = 'tearDown'.class_basename($trait))) { $this->beforeApplicationDestroyed(fn () => $this->{$method}()); } } return $uses; } /** * {@inheritdoc} */ protected function runTest(): mixed { $result = null; try { $result = parent::runTest(); } catch (Throwable $e) { if (! is_null(static::$latestResponse)) { static::$latestResponse->transformNotSuccessfulException($e); } throw $e; } return $result; } /** * Clean up the testing environment before the next test. * * @return void * * @throws \Mockery\Exception\InvalidCountException */ protected function tearDown(): void { if ($this->app) { $this->callBeforeApplicationDestroyedCallbacks(); ParallelTesting::callTearDownTestCaseCallbacks($this); $this->app->flush(); $this->app = null; } $this->setUpHasRun = false; if (property_exists($this, 'serverVariables')) { $this->serverVariables = []; } if (property_exists($this, 'defaultHeaders')) { $this->defaultHeaders = []; } if (class_exists('Mockery')) { if ($container = Mockery::getContainer()) { $this->addToAssertionCount($container->mockery_getExpectationCount()); } try { Mockery::close(); } catch (InvalidCountException $e) { if (! Str::contains($e->getMethodName(), ['doWrite', 'askQuestion'])) { throw $e; } } } if (class_exists(Carbon::class)) { Carbon::setTestNow(); } if (class_exists(CarbonImmutable::class)) { CarbonImmutable::setTestNow(); } $this->afterApplicationCreatedCallbacks = []; $this->beforeApplicationDestroyedCallbacks = []; $this->originalExceptionHandler = null; $this->originalDeprecationHandler = null; Artisan::forgetBootstrappers(); Component::flushCache(); Component::forgetComponentsResolver(); Component::forgetFactory(); Queue::createPayloadUsing(null); HandleExceptions::forgetApp(); Sleep::fake(false); if ($this->callbackException) { throw $this->callbackException; } } /** * Clean up the testing environment before the next test case. * * @return void */ public static function tearDownAfterClass(): void { static::$latestResponse = null; foreach ([ \PHPUnit\Util\Annotation\Registry::class, \PHPUnit\Metadata\Annotation\Parser\Registry::class, ] as $class) { if (class_exists($class)) { (function () { $this->classDocBlocks = []; $this->methodDocBlocks = []; })->call($class::getInstance()); } } } /** * Register a callback to be run after the application is created. * * @param callable $callback * @return void */ public function afterApplicationCreated(callable $callback) { $this->afterApplicationCreatedCallbacks[] = $callback; if ($this->setUpHasRun) { $callback(); } } /** * Register a callback to be run before the application is destroyed. * * @param callable $callback * @return void */ protected function beforeApplicationDestroyed(callable $callback) { $this->beforeApplicationDestroyedCallbacks[] = $callback; } /** * Execute the application's pre-destruction callbacks. * * @return void */ protected function callBeforeApplicationDestroyedCallbacks() { foreach ($this->beforeApplicationDestroyedCallbacks as $callback) { try { $callback(); } catch (Throwable $e) { if (! $this->callbackException) { $this->callbackException = $e; } } } } } src/Illuminate/Foundation/Testing/WithoutMiddleware.php 0000644 00000000764 15107327037 0017347 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; use Exception; trait WithoutMiddleware { /** * Prevent all middleware from being executed for this test class. * * @throws \Exception */ public function disableMiddlewareForAllTests() { if (method_exists($this, 'withoutMiddleware')) { $this->withoutMiddleware(); } else { throw new Exception('Unable to disable middleware. MakesHttpRequests trait not used.'); } } } src/Illuminate/Foundation/Testing/Traits/CanConfigureMigrationCommands.php 0000644 00000003037 15107327037 0023047 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Traits; trait CanConfigureMigrationCommands { /** * The parameters that should be used when running "migrate:fresh". * * @return array */ protected function migrateFreshUsing() { $seeder = $this->seeder(); return array_merge( [ '--drop-views' => $this->shouldDropViews(), '--drop-types' => $this->shouldDropTypes(), ], $seeder ? ['--seeder' => $seeder] : ['--seed' => $this->shouldSeed()] ); } /** * Determine if views should be dropped when refreshing the database. * * @return bool */ protected function shouldDropViews() { return property_exists($this, 'dropViews') ? $this->dropViews : false; } /** * Determine if types should be dropped when refreshing the database. * * @return bool */ protected function shouldDropTypes() { return property_exists($this, 'dropTypes') ? $this->dropTypes : false; } /** * Determine if the seed task should be run when refreshing the database. * * @return bool */ protected function shouldSeed() { return property_exists($this, 'seed') ? $this->seed : false; } /** * Determine the specific seeder class that should be used when refreshing the database. * * @return mixed */ protected function seeder() { return property_exists($this, 'seeder') ? $this->seeder : false; } } src/Illuminate/Foundation/Testing/RefreshDatabaseState.php 0000644 00000000545 15107327037 0017727 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; class RefreshDatabaseState { /** * Indicates if the test database has been migrated. * * @var bool */ public static $migrated = false; /** * Indicates if a lazy refresh hook has been invoked. * * @var bool */ public static $lazilyRefreshed = false; } src/Illuminate/Foundation/Testing/LazilyRefreshDatabase.php 0000644 00000001371 15107327037 0020111 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; trait LazilyRefreshDatabase { use RefreshDatabase { refreshDatabase as baseRefreshDatabase; } /** * Define hooks to migrate the database before and after each test. * * @return void */ public function refreshDatabase() { $database = $this->app->make('db'); $database->beforeExecuting(function () { if (RefreshDatabaseState::$lazilyRefreshed) { return; } RefreshDatabaseState::$lazilyRefreshed = true; $this->baseRefreshDatabase(); }); $this->beforeApplicationDestroyed(function () { RefreshDatabaseState::$lazilyRefreshed = false; }); } } src/Illuminate/Foundation/Testing/WithConsoleEvents.php 0000644 00000000530 15107327037 0017320 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; use Illuminate\Contracts\Console\Kernel as ConsoleKernel; trait WithConsoleEvents { /** * Register console events. * * @return void */ protected function setUpWithConsoleEvents() { $this->app[ConsoleKernel::class]->rerouteSymfonyCommandEvents(); } } src/Illuminate/Foundation/Testing/Wormhole.php 0000644 00000012245 15107327037 0015477 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; use Illuminate\Support\Carbon; class Wormhole { /** * The amount of time to travel. * * @var int */ public $value; /** * Create a new wormhole instance. * * @param int $value * @return void */ public function __construct($value) { $this->value = $value; } /** * Travel forward the given number of milliseconds. * * @param callable|null $callback * @return mixed */ public function millisecond($callback = null) { return $this->milliseconds($callback); } /** * Travel forward the given number of milliseconds. * * @param callable|null $callback * @return mixed */ public function milliseconds($callback = null) { Carbon::setTestNow(Carbon::now()->addMilliseconds($this->value)); return $this->handleCallback($callback); } /** * Travel forward the given number of seconds. * * @param callable|null $callback * @return mixed */ public function second($callback = null) { return $this->seconds($callback); } /** * Travel forward the given number of seconds. * * @param callable|null $callback * @return mixed */ public function seconds($callback = null) { Carbon::setTestNow(Carbon::now()->addSeconds($this->value)); return $this->handleCallback($callback); } /** * Travel forward the given number of minutes. * * @param callable|null $callback * @return mixed */ public function minute($callback = null) { return $this->minutes($callback); } /** * Travel forward the given number of minutes. * * @param callable|null $callback * @return mixed */ public function minutes($callback = null) { Carbon::setTestNow(Carbon::now()->addMinutes($this->value)); return $this->handleCallback($callback); } /** * Travel forward the given number of hours. * * @param callable|null $callback * @return mixed */ public function hour($callback = null) { return $this->hours($callback); } /** * Travel forward the given number of hours. * * @param callable|null $callback * @return mixed */ public function hours($callback = null) { Carbon::setTestNow(Carbon::now()->addHours($this->value)); return $this->handleCallback($callback); } /** * Travel forward the given number of days. * * @param callable|null $callback * @return mixed */ public function day($callback = null) { return $this->days($callback); } /** * Travel forward the given number of days. * * @param callable|null $callback * @return mixed */ public function days($callback = null) { Carbon::setTestNow(Carbon::now()->addDays($this->value)); return $this->handleCallback($callback); } /** * Travel forward the given number of weeks. * * @param callable|null $callback * @return mixed */ public function week($callback = null) { return $this->weeks($callback); } /** * Travel forward the given number of weeks. * * @param callable|null $callback * @return mixed */ public function weeks($callback = null) { Carbon::setTestNow(Carbon::now()->addWeeks($this->value)); return $this->handleCallback($callback); } /** * Travel forward the given number of months. * * @param callable|null $callback * @return mixed */ public function month($callback = null) { return $this->months($callback); } /** * Travel forward the given number of months. * * @param callable|null $callback * @return mixed */ public function months($callback = null) { Carbon::setTestNow(Carbon::now()->addMonths($this->value)); return $this->handleCallback($callback); } /** * Travel forward the given number of years. * * @param callable|null $callback * @return mixed */ public function year($callback = null) { return $this->years($callback); } /** * Travel forward the given number of years. * * @param callable|null $callback * @return mixed */ public function years($callback = null) { Carbon::setTestNow(Carbon::now()->addYears($this->value)); return $this->handleCallback($callback); } /** * Travel back to the current time. * * @return \DateTimeInterface */ public static function back() { Carbon::setTestNow(); return Carbon::now(); } /** * Handle the given optional execution callback. * * @param callable|null $callback * @return mixed */ protected function handleCallback($callback) { if ($callback) { return tap($callback(), function () { Carbon::setTestNow(); }); } } } src/Illuminate/Foundation/Testing/WithFaker.php 0000644 00000002325 15107327037 0015565 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; use Faker\Factory; use Faker\Generator; trait WithFaker { /** * The Faker instance. * * @var \Faker\Generator */ protected $faker; /** * Setup up the Faker instance. * * @return void */ protected function setUpFaker() { $this->faker = $this->makeFaker(); } /** * Get the default Faker instance for a given locale. * * @param string|null $locale * @return \Faker\Generator */ protected function faker($locale = null) { return is_null($locale) ? $this->faker : $this->makeFaker($locale); } /** * Create a Faker instance for the given locale. * * @param string|null $locale * @return \Faker\Generator */ protected function makeFaker($locale = null) { if (isset($this->app)) { $locale ??= $this->app->make('config')->get('app.faker_locale', Factory::DEFAULT_LOCALE); if ($this->app->bound(Generator::class)) { return $this->app->make(Generator::class, ['locale' => $locale]); } } return Factory::create($locale ?? Factory::DEFAULT_LOCALE); } } src/Illuminate/Foundation/Validation/ValidatesRequests.php 0000644 00000005616 15107327037 0020034 0 ustar 00 <?php namespace Illuminate\Foundation\Validation; use Illuminate\Contracts\Validation\Factory; use Illuminate\Foundation\Precognition; use Illuminate\Http\Request; use Illuminate\Validation\ValidationException; trait ValidatesRequests { /** * Run the validation routine against the given validator. * * @param \Illuminate\Contracts\Validation\Validator|array $validator * @param \Illuminate\Http\Request|null $request * @return array * * @throws \Illuminate\Validation\ValidationException */ public function validateWith($validator, Request $request = null) { $request = $request ?: request(); if (is_array($validator)) { $validator = $this->getValidationFactory()->make($request->all(), $validator); } if ($request->isPrecognitive()) { $validator->after(Precognition::afterValidationHook($request)) ->setRules( $request->filterPrecognitiveRules($validator->getRulesWithoutPlaceholders()) ); } return $validator->validate(); } /** * Validate the given request with the given rules. * * @param \Illuminate\Http\Request $request * @param array $rules * @param array $messages * @param array $attributes * @return array * * @throws \Illuminate\Validation\ValidationException */ public function validate(Request $request, array $rules, array $messages = [], array $attributes = []) { $validator = $this->getValidationFactory()->make( $request->all(), $rules, $messages, $attributes ); if ($request->isPrecognitive()) { $validator->after(Precognition::afterValidationHook($request)) ->setRules( $request->filterPrecognitiveRules($validator->getRulesWithoutPlaceholders()) ); } return $validator->validate(); } /** * Validate the given request with the given rules. * * @param string $errorBag * @param \Illuminate\Http\Request $request * @param array $rules * @param array $messages * @param array $attributes * @return array * * @throws \Illuminate\Validation\ValidationException */ public function validateWithBag($errorBag, Request $request, array $rules, array $messages = [], array $attributes = []) { try { return $this->validate($request, $rules, $messages, $attributes); } catch (ValidationException $e) { $e->errorBag = $errorBag; throw $e; } } /** * Get a validation factory instance. * * @return \Illuminate\Contracts\Validation\Factory */ protected function getValidationFactory() { return app(Factory::class); } } src/Illuminate/Foundation/PackageManifest.php 0000644 00000010524 15107327037 0015306 0 ustar 00 <?php namespace Illuminate\Foundation; use Exception; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Env; class PackageManifest { /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ public $files; /** * The base path. * * @var string */ public $basePath; /** * The vendor path. * * @var string */ public $vendorPath; /** * The manifest path. * * @var string|null */ public $manifestPath; /** * The loaded manifest array. * * @var array */ public $manifest; /** * Create a new package manifest instance. * * @param \Illuminate\Filesystem\Filesystem $files * @param string $basePath * @param string $manifestPath * @return void */ public function __construct(Filesystem $files, $basePath, $manifestPath) { $this->files = $files; $this->basePath = $basePath; $this->manifestPath = $manifestPath; $this->vendorPath = Env::get('COMPOSER_VENDOR_DIR') ?: $basePath.'/vendor'; } /** * Get all of the service provider class names for all packages. * * @return array */ public function providers() { return $this->config('providers'); } /** * Get all of the aliases for all packages. * * @return array */ public function aliases() { return $this->config('aliases'); } /** * Get all of the values for all packages for the given configuration name. * * @param string $key * @return array */ public function config($key) { return collect($this->getManifest())->flatMap(function ($configuration) use ($key) { return (array) ($configuration[$key] ?? []); })->filter()->all(); } /** * Get the current package manifest. * * @return array */ protected function getManifest() { if (! is_null($this->manifest)) { return $this->manifest; } if (! is_file($this->manifestPath)) { $this->build(); } return $this->manifest = is_file($this->manifestPath) ? $this->files->getRequire($this->manifestPath) : []; } /** * Build the manifest and write it to disk. * * @return void */ public function build() { $packages = []; if ($this->files->exists($path = $this->vendorPath.'/composer/installed.json')) { $installed = json_decode($this->files->get($path), true); $packages = $installed['packages'] ?? $installed; } $ignoreAll = in_array('*', $ignore = $this->packagesToIgnore()); $this->write(collect($packages)->mapWithKeys(function ($package) { return [$this->format($package['name']) => $package['extra']['laravel'] ?? []]; })->each(function ($configuration) use (&$ignore) { $ignore = array_merge($ignore, $configuration['dont-discover'] ?? []); })->reject(function ($configuration, $package) use ($ignore, $ignoreAll) { return $ignoreAll || in_array($package, $ignore); })->filter()->all()); } /** * Format the given package name. * * @param string $package * @return string */ protected function format($package) { return str_replace($this->vendorPath.'/', '', $package); } /** * Get all of the package names that should be ignored. * * @return array */ protected function packagesToIgnore() { if (! is_file($this->basePath.'/composer.json')) { return []; } return json_decode(file_get_contents( $this->basePath.'/composer.json' ), true)['extra']['laravel']['dont-discover'] ?? []; } /** * Write the given manifest array to disk. * * @param array $manifest * @return void * * @throws \Exception */ protected function write(array $manifest) { if (! is_writable($dirname = dirname($this->manifestPath))) { throw new Exception("The {$dirname} directory must be present and writable."); } $this->files->replace( $this->manifestPath, '<?php return '.var_export($manifest, true).';' ); } } src/Illuminate/Foundation/Http/HtmlDumper.php 0000644 00000007117 15107327037 0015270 0 ustar 00 <?php namespace Illuminate\Foundation\Http; use Illuminate\Foundation\Concerns\ResolvesDumpSource; use Symfony\Component\VarDumper\Caster\ReflectionCaster; use Symfony\Component\VarDumper\Cloner\Data; use Symfony\Component\VarDumper\Cloner\VarCloner; use Symfony\Component\VarDumper\Dumper\HtmlDumper as BaseHtmlDumper; use Symfony\Component\VarDumper\VarDumper; class HtmlDumper extends BaseHtmlDumper { use ResolvesDumpSource; /** * Where the source should be placed on "expanded" kind of dumps. * * @var string */ const EXPANDED_SEPARATOR = 'class=sf-dump-expanded>'; /** * Where the source should be placed on "non expanded" kind of dumps. * * @var string */ const NON_EXPANDED_SEPARATOR = "\n</pre><script>"; /** * The base path of the application. * * @var string */ protected $basePath; /** * The compiled view path of the application. * * @var string */ protected $compiledViewPath; /** * If the dumper is currently dumping. * * @var bool */ protected $dumping = false; /** * Create a new HTML dumper instance. * * @param string $basePath * @param string $compiledViewPath * @return void */ public function __construct($basePath, $compiledViewPath) { parent::__construct(); $this->basePath = $basePath; $this->compiledViewPath = $compiledViewPath; } /** * Create a new HTML dumper instance and register it as the default dumper. * * @param string $basePath * @param string $compiledViewPath * @return void */ public static function register($basePath, $compiledViewPath) { $cloner = tap(new VarCloner())->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO); $dumper = new static($basePath, $compiledViewPath); VarDumper::setHandler(fn ($value) => $dumper->dumpWithSource($cloner->cloneVar($value))); } /** * Dump a variable with its source file / line. * * @param \Symfony\Component\VarDumper\Cloner\Data $data * @return void */ public function dumpWithSource(Data $data) { if ($this->dumping) { $this->dump($data); return; } $this->dumping = true; $output = (string) $this->dump($data, true); $output = match (true) { str_contains($output, static::EXPANDED_SEPARATOR) => str_replace( static::EXPANDED_SEPARATOR, static::EXPANDED_SEPARATOR.$this->getDumpSourceContent(), $output, ), str_contains($output, static::NON_EXPANDED_SEPARATOR) => str_replace( static::NON_EXPANDED_SEPARATOR, $this->getDumpSourceContent().static::NON_EXPANDED_SEPARATOR, $output, ), default => $output, }; fwrite($this->outputStream, $output); $this->dumping = false; } /** * Get the dump's source HTML content. * * @return string */ protected function getDumpSourceContent() { if (is_null($dumpSource = $this->resolveDumpSource())) { return ''; } [$file, $relativeFile, $line] = $dumpSource; $source = sprintf('%s%s', $relativeFile, is_null($line) ? '' : ":$line"); if ($href = $this->resolveSourceHref($file, $line)) { $source = sprintf('<a href="%s">%s</a>', $href, $source); } return sprintf('<span style="color: #A0A0A0;"> // %s</span>', $source); } } src/Illuminate/Foundation/Http/Events/RequestHandled.php 0000644 00000001155 15107327037 0017357 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Events; class RequestHandled { /** * The request instance. * * @var \Illuminate\Http\Request */ public $request; /** * The response instance. * * @var \Illuminate\Http\Response */ public $response; /** * Create a new event instance. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Response $response * @return void */ public function __construct($request, $response) { $this->request = $request; $this->response = $response; } } src/Illuminate/Foundation/Http/FormRequest.php 0000644 00000015763 15107327037 0015471 0 ustar 00 <?php namespace Illuminate\Foundation\Http; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\Access\Response; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Validation\Factory as ValidationFactory; use Illuminate\Contracts\Validation\ValidatesWhenResolved; use Illuminate\Contracts\Validation\Validator; use Illuminate\Http\Request; use Illuminate\Routing\Redirector; use Illuminate\Validation\ValidatesWhenResolvedTrait; class FormRequest extends Request implements ValidatesWhenResolved { use ValidatesWhenResolvedTrait; /** * The container instance. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * The redirector instance. * * @var \Illuminate\Routing\Redirector */ protected $redirector; /** * The URI to redirect to if validation fails. * * @var string */ protected $redirect; /** * The route to redirect to if validation fails. * * @var string */ protected $redirectRoute; /** * The controller action to redirect to if validation fails. * * @var string */ protected $redirectAction; /** * The key to be used for the view error bag. * * @var string */ protected $errorBag = 'default'; /** * Indicates whether validation should stop after the first rule failure. * * @var bool */ protected $stopOnFirstFailure = false; /** * The validator instance. * * @var \Illuminate\Contracts\Validation\Validator */ protected $validator; /** * Get the validator instance for the request. * * @return \Illuminate\Contracts\Validation\Validator */ protected function getValidatorInstance() { if ($this->validator) { return $this->validator; } $factory = $this->container->make(ValidationFactory::class); if (method_exists($this, 'validator')) { $validator = $this->container->call([$this, 'validator'], compact('factory')); } else { $validator = $this->createDefaultValidator($factory); } if (method_exists($this, 'withValidator')) { $this->withValidator($validator); } if (method_exists($this, 'after')) { $validator->after($this->container->call( $this->after(...), ['validator' => $validator] )); } $this->setValidator($validator); return $this->validator; } /** * Create the default validator instance. * * @param \Illuminate\Contracts\Validation\Factory $factory * @return \Illuminate\Contracts\Validation\Validator */ protected function createDefaultValidator(ValidationFactory $factory) { $rules = method_exists($this, 'rules') ? $this->container->call([$this, 'rules']) : []; $validator = $factory->make( $this->validationData(), $rules, $this->messages(), $this->attributes() )->stopOnFirstFailure($this->stopOnFirstFailure); if ($this->isPrecognitive()) { $validator->setRules( $this->filterPrecognitiveRules($validator->getRulesWithoutPlaceholders()) ); } return $validator; } /** * Get data to be validated from the request. * * @return array */ public function validationData() { return $this->all(); } /** * Handle a failed validation attempt. * * @param \Illuminate\Contracts\Validation\Validator $validator * @return void * * @throws \Illuminate\Validation\ValidationException */ protected function failedValidation(Validator $validator) { $exception = $validator->getException(); throw (new $exception($validator)) ->errorBag($this->errorBag) ->redirectTo($this->getRedirectUrl()); } /** * Get the URL to redirect to on a validation error. * * @return string */ protected function getRedirectUrl() { $url = $this->redirector->getUrlGenerator(); if ($this->redirect) { return $url->to($this->redirect); } elseif ($this->redirectRoute) { return $url->route($this->redirectRoute); } elseif ($this->redirectAction) { return $url->action($this->redirectAction); } return $url->previous(); } /** * Determine if the request passes the authorization check. * * @return bool * * @throws \Illuminate\Auth\Access\AuthorizationException */ protected function passesAuthorization() { if (method_exists($this, 'authorize')) { $result = $this->container->call([$this, 'authorize']); return $result instanceof Response ? $result->authorize() : $result; } return true; } /** * Handle a failed authorization attempt. * * @return void * * @throws \Illuminate\Auth\Access\AuthorizationException */ protected function failedAuthorization() { throw new AuthorizationException; } /** * Get a validated input container for the validated input. * * @param array|null $keys * @return \Illuminate\Support\ValidatedInput|array */ public function safe(array $keys = null) { return is_array($keys) ? $this->validator->safe()->only($keys) : $this->validator->safe(); } /** * Get the validated data from the request. * * @param array|int|string|null $key * @param mixed $default * @return mixed */ public function validated($key = null, $default = null) { return data_get($this->validator->validated(), $key, $default); } /** * Get custom messages for validator errors. * * @return array */ public function messages() { return []; } /** * Get custom attributes for validator errors. * * @return array */ public function attributes() { return []; } /** * Set the Validator instance. * * @param \Illuminate\Contracts\Validation\Validator $validator * @return $this */ public function setValidator(Validator $validator) { $this->validator = $validator; return $this; } /** * Set the Redirector instance. * * @param \Illuminate\Routing\Redirector $redirector * @return $this */ public function setRedirector(Redirector $redirector) { $this->redirector = $redirector; return $this; } /** * Set the container implementation. * * @param \Illuminate\Contracts\Container\Container $container * @return $this */ public function setContainer(Container $container) { $this->container = $container; return $this; } } src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php 0000644 00000013573 15107327037 0020352 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Middleware; use Closure; use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Support\Responsable; use Illuminate\Cookie\CookieValuePrefix; use Illuminate\Cookie\Middleware\EncryptCookies; use Illuminate\Session\TokenMismatchException; use Illuminate\Support\InteractsWithTime; use Symfony\Component\HttpFoundation\Cookie; class VerifyCsrfToken { use InteractsWithTime; /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The encrypter implementation. * * @var \Illuminate\Contracts\Encryption\Encrypter */ protected $encrypter; /** * The URIs that should be excluded from CSRF verification. * * @var array<int, string> */ protected $except = []; /** * Indicates whether the XSRF-TOKEN cookie should be set on the response. * * @var bool */ protected $addHttpCookie = true; /** * Create a new middleware instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter * @return void */ public function __construct(Application $app, Encrypter $encrypter) { $this->app = $app; $this->encrypter = $encrypter; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed * * @throws \Illuminate\Session\TokenMismatchException */ public function handle($request, Closure $next) { if ( $this->isReading($request) || $this->runningUnitTests() || $this->inExceptArray($request) || $this->tokensMatch($request) ) { return tap($next($request), function ($response) use ($request) { if ($this->shouldAddXsrfTokenCookie()) { $this->addCookieToResponse($request, $response); } }); } throw new TokenMismatchException('CSRF token mismatch.'); } /** * Determine if the HTTP request uses a ‘read’ verb. * * @param \Illuminate\Http\Request $request * @return bool */ protected function isReading($request) { return in_array($request->method(), ['HEAD', 'GET', 'OPTIONS']); } /** * Determine if the application is running unit tests. * * @return bool */ protected function runningUnitTests() { return $this->app->runningInConsole() && $this->app->runningUnitTests(); } /** * Determine if the request has a URI that should pass through CSRF verification. * * @param \Illuminate\Http\Request $request * @return bool */ protected function inExceptArray($request) { foreach ($this->except as $except) { if ($except !== '/') { $except = trim($except, '/'); } if ($request->fullUrlIs($except) || $request->is($except)) { return true; } } return false; } /** * Determine if the session and input CSRF tokens match. * * @param \Illuminate\Http\Request $request * @return bool */ protected function tokensMatch($request) { $token = $this->getTokenFromRequest($request); return is_string($request->session()->token()) && is_string($token) && hash_equals($request->session()->token(), $token); } /** * Get the CSRF token from the request. * * @param \Illuminate\Http\Request $request * @return string|null */ protected function getTokenFromRequest($request) { $token = $request->input('_token') ?: $request->header('X-CSRF-TOKEN'); if (! $token && $header = $request->header('X-XSRF-TOKEN')) { try { $token = CookieValuePrefix::remove($this->encrypter->decrypt($header, static::serialized())); } catch (DecryptException) { $token = ''; } } return $token; } /** * Determine if the cookie should be added to the response. * * @return bool */ public function shouldAddXsrfTokenCookie() { return $this->addHttpCookie; } /** * Add the CSRF token to the response cookies. * * @param \Illuminate\Http\Request $request * @param \Symfony\Component\HttpFoundation\Response $response * @return \Symfony\Component\HttpFoundation\Response */ protected function addCookieToResponse($request, $response) { $config = config('session'); if ($response instanceof Responsable) { $response = $response->toResponse($request); } $response->headers->setCookie($this->newCookie($request, $config)); return $response; } /** * Create a new "XSRF-TOKEN" cookie that contains the CSRF token. * * @param \Illuminate\Http\Request $request * @param array $config * @return \Symfony\Component\HttpFoundation\Cookie */ protected function newCookie($request, $config) { return new Cookie( 'XSRF-TOKEN', $request->session()->token(), $this->availableAt(60 * $config['lifetime']), $config['path'], $config['domain'], $config['secure'], false, false, $config['same_site'] ?? null ); } /** * Determine if the cookie contents should be serialized. * * @return bool */ public static function serialized() { return EncryptCookies::serialized('XSRF-TOKEN'); } } src/Illuminate/Foundation/Http/Middleware/HandlePrecognitiveRequests.php 0000644 00000004621 15107327037 0022567 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Middleware; use Illuminate\Container\Container; use Illuminate\Foundation\Routing\PrecognitionCallableDispatcher; use Illuminate\Foundation\Routing\PrecognitionControllerDispatcher; use Illuminate\Routing\Contracts\CallableDispatcher as CallableDispatcherContract; use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract; class HandlePrecognitiveRequests { /** * The container instance. * * @var \Illuminate\Container\Container */ protected $container; /** * Create a new middleware instance. * * @param \Illuminate\Container\Container $container * @return void */ public function __construct(Container $container) { $this->container = $container; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return \Illuminate\Http\Response */ public function handle($request, $next) { if (! $request->isAttemptingPrecognition()) { return $this->appendVaryHeader($request, $next($request)); } $this->prepareForPrecognition($request); return tap($next($request), function ($response) use ($request) { $response->headers->set('Precognition', 'true'); $this->appendVaryHeader($request, $response); }); } /** * Prepare to handle a precognitive request. * * @param \Illuminate\Http\Request $request * @return void */ protected function prepareForPrecognition($request) { $request->attributes->set('precognitive', true); $this->container->bind(CallableDispatcherContract::class, fn ($app) => new PrecognitionCallableDispatcher($app)); $this->container->bind(ControllerDispatcherContract::class, fn ($app) => new PrecognitionControllerDispatcher($app)); } /** * Append the appropriate "Vary" header to the given response. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Response $response * @return \Illuminate\Http\Response */ protected function appendVaryHeader($request, $response) { return tap($response, fn () => $response->headers->set('Vary', implode(', ', array_filter([ $response->headers->get('Vary'), 'Precognition', ])))); } } src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php 0000644 00000002306 15107327037 0020511 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Middleware; use Closure; use Illuminate\Http\Exceptions\PostTooLargeException; class ValidatePostSize { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed * * @throws \Illuminate\Http\Exceptions\PostTooLargeException */ public function handle($request, Closure $next) { $max = $this->getPostMaxSize(); if ($max > 0 && $request->server('CONTENT_LENGTH') > $max) { throw new PostTooLargeException; } return $next($request); } /** * Determine the server 'post_max_size' as bytes. * * @return int */ protected function getPostMaxSize() { if (is_numeric($postMaxSize = ini_get('post_max_size'))) { return (int) $postMaxSize; } $metric = strtoupper(substr($postMaxSize, -1)); $postMaxSize = (int) $postMaxSize; return match ($metric) { 'K' => $postMaxSize * 1024, 'M' => $postMaxSize * 1048576, 'G' => $postMaxSize * 1073741824, default => $postMaxSize, }; } } src/Illuminate/Foundation/Http/Middleware/TrimStrings.php 0000644 00000002745 15107327037 0017553 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Middleware; use Closure; class TrimStrings extends TransformsRequest { /** * All of the registered skip callbacks. * * @var array */ protected static $skipCallbacks = []; /** * The attributes that should not be trimmed. * * @var array<int, string> */ protected $except = [ // ]; /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { foreach (static::$skipCallbacks as $callback) { if ($callback($request)) { return $next($request); } } return parent::handle($request, $next); } /** * Transform the given value. * * @param string $key * @param mixed $value * @return mixed */ protected function transform($key, $value) { if (in_array($key, $this->except, true) || ! is_string($value)) { return $value; } return preg_replace('~^[\s\x{FEFF}\x{200B}]+|[\s\x{FEFF}\x{200B}]+$~u', '', $value) ?? trim($value); } /** * Register a callback that instructs the middleware to be skipped. * * @param \Closure $callback * @return void */ public static function skipWhen(Closure $callback) { static::$skipCallbacks[] = $callback; } } src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php 0000644 00000000213 15107327037 0021706 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Middleware; class CheckForMaintenanceMode extends PreventRequestsDuringMaintenance { // } src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php 0000644 00000011275 15107327037 0023757 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Middleware; use Closure; use ErrorException; use Illuminate\Contracts\Foundation\Application; use Illuminate\Foundation\Http\MaintenanceModeBypassCookie; use Symfony\Component\HttpKernel\Exception\HttpException; class PreventRequestsDuringMaintenance { /** * The application implementation. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The URIs that should be accessible while maintenance mode is enabled. * * @var array<int, string> */ protected $except = []; /** * Create a new middleware instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function __construct(Application $app) { $this->app = $app; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed * * @throws \Symfony\Component\HttpKernel\Exception\HttpException * @throws \ErrorException */ public function handle($request, Closure $next) { if ($this->inExceptArray($request)) { return $next($request); } if ($this->app->maintenanceMode()->active()) { try { $data = $this->app->maintenanceMode()->data(); } catch (ErrorException $exception) { if (! $this->app->maintenanceMode()->active()) { return $next($request); } throw $exception; } if (isset($data['secret']) && $request->path() === $data['secret']) { return $this->bypassResponse($data['secret']); } if ($this->hasValidBypassCookie($request, $data)) { return $next($request); } if (isset($data['redirect'])) { $path = $data['redirect'] === '/' ? $data['redirect'] : trim($data['redirect'], '/'); if ($request->path() !== $path) { return redirect($path); } } if (isset($data['template'])) { return response( $data['template'], $data['status'] ?? 503, $this->getHeaders($data) ); } throw new HttpException( $data['status'] ?? 503, 'Service Unavailable', null, $this->getHeaders($data) ); } return $next($request); } /** * Determine if the incoming request has a maintenance mode bypass cookie. * * @param \Illuminate\Http\Request $request * @param array $data * @return bool */ protected function hasValidBypassCookie($request, array $data) { return isset($data['secret']) && $request->cookie('laravel_maintenance') && MaintenanceModeBypassCookie::isValid( $request->cookie('laravel_maintenance'), $data['secret'] ); } /** * Determine if the request has a URI that should be accessible in maintenance mode. * * @param \Illuminate\Http\Request $request * @return bool */ protected function inExceptArray($request) { foreach ($this->getExcludedPaths() as $except) { if ($except !== '/') { $except = trim($except, '/'); } if ($request->fullUrlIs($except) || $request->is($except)) { return true; } } return false; } /** * Redirect the user back to the root of the application with a maintenance mode bypass cookie. * * @param string $secret * @return \Illuminate\Http\RedirectResponse */ protected function bypassResponse(string $secret) { return redirect('/')->withCookie( MaintenanceModeBypassCookie::create($secret) ); } /** * Get the headers that should be sent with the response. * * @param array $data * @return array */ protected function getHeaders($data) { $headers = isset($data['retry']) ? ['Retry-After' => $data['retry']] : []; if (isset($data['refresh'])) { $headers['Refresh'] = $data['refresh']; } return $headers; } /** * Get the URIs that should be accessible even when maintenance mode is enabled. * * @return array */ public function getExcludedPaths() { return $this->except; } } src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php 0000644 00000002257 15107327037 0022433 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Middleware; use Closure; class ConvertEmptyStringsToNull extends TransformsRequest { /** * All of the registered skip callbacks. * * @var array */ protected static $skipCallbacks = []; /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { foreach (static::$skipCallbacks as $callback) { if ($callback($request)) { return $next($request); } } return parent::handle($request, $next); } /** * Transform the given value. * * @param string $key * @param mixed $value * @return mixed */ protected function transform($key, $value) { return $value === '' ? null : $value; } /** * Register a callback that instructs the middleware to be skipped. * * @param \Closure $callback * @return void */ public static function skipWhen(Closure $callback) { static::$skipCallbacks[] = $callback; } } src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php 0000644 00000004060 15107327037 0020765 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Middleware; use Closure; use Symfony\Component\HttpFoundation\ParameterBag; class TransformsRequest { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $this->clean($request); return $next($request); } /** * Clean the request's data. * * @param \Illuminate\Http\Request $request * @return void */ protected function clean($request) { $this->cleanParameterBag($request->query); if ($request->isJson()) { $this->cleanParameterBag($request->json()); } elseif ($request->request !== $request->query) { $this->cleanParameterBag($request->request); } } /** * Clean the data in the parameter bag. * * @param \Symfony\Component\HttpFoundation\ParameterBag $bag * @return void */ protected function cleanParameterBag(ParameterBag $bag) { $bag->replace($this->cleanArray($bag->all())); } /** * Clean the data in the given array. * * @param array $data * @param string $keyPrefix * @return array */ protected function cleanArray(array $data, $keyPrefix = '') { foreach ($data as $key => $value) { $data[$key] = $this->cleanValue($keyPrefix.$key, $value); } return $data; } /** * Clean the given value. * * @param string $key * @param mixed $value * @return mixed */ protected function cleanValue($key, $value) { if (is_array($value)) { return $this->cleanArray($value, $key.'.'); } return $this->transform($key, $value); } /** * Transform the given value. * * @param string $key * @param mixed $value * @return mixed */ protected function transform($key, $value) { return $value; } } src/Illuminate/Foundation/Http/MaintenanceModeBypassCookie.php 0000644 00000002463 15107327037 0020551 0 ustar 00 <?php namespace Illuminate\Foundation\Http; use Illuminate\Support\Carbon; use Symfony\Component\HttpFoundation\Cookie; class MaintenanceModeBypassCookie { /** * Create a new maintenance mode bypass cookie. * * @param string $key * @return \Symfony\Component\HttpFoundation\Cookie */ public static function create(string $key) { $expiresAt = Carbon::now()->addHours(12); return new Cookie('laravel_maintenance', base64_encode(json_encode([ 'expires_at' => $expiresAt->getTimestamp(), 'mac' => hash_hmac('sha256', $expiresAt->getTimestamp(), $key), ])), $expiresAt, config('session.path'), config('session.domain')); } /** * Determine if the given maintenance mode bypass cookie is valid. * * @param string $cookie * @param string $key * @return bool */ public static function isValid(string $cookie, string $key) { $payload = json_decode(base64_decode($cookie), true); return is_array($payload) && is_numeric($payload['expires_at'] ?? null) && isset($payload['mac']) && hash_equals(hash_hmac('sha256', $payload['expires_at'], $key), $payload['mac']) && (int) $payload['expires_at'] >= Carbon::now()->getTimestamp(); } } src/Illuminate/Foundation/Http/Kernel.php 0000644 00000035202 15107327037 0014423 0 ustar 00 <?php namespace Illuminate\Foundation\Http; use Carbon\CarbonInterval; use DateTimeInterface; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Http\Kernel as KernelContract; use Illuminate\Foundation\Http\Events\RequestHandled; use Illuminate\Routing\Pipeline; use Illuminate\Routing\Router; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Facade; use Illuminate\Support\InteractsWithTime; use InvalidArgumentException; use Throwable; class Kernel implements KernelContract { use InteractsWithTime; /** * The application implementation. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The router instance. * * @var \Illuminate\Routing\Router */ protected $router; /** * The bootstrap classes for the application. * * @var string[] */ protected $bootstrappers = [ \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class, \Illuminate\Foundation\Bootstrap\LoadConfiguration::class, \Illuminate\Foundation\Bootstrap\HandleExceptions::class, \Illuminate\Foundation\Bootstrap\RegisterFacades::class, \Illuminate\Foundation\Bootstrap\RegisterProviders::class, \Illuminate\Foundation\Bootstrap\BootProviders::class, ]; /** * The application's middleware stack. * * @var array<int, class-string|string> */ protected $middleware = []; /** * The application's route middleware groups. * * @var array<string, array<int, class-string|string>> */ protected $middlewareGroups = []; /** * The application's route middleware. * * @var array<string, class-string|string> * * @deprecated */ protected $routeMiddleware = []; /** * The application's middleware aliases. * * @var array<string, class-string|string> */ protected $middlewareAliases = []; /** * All of the registered request duration handlers. * * @var array */ protected $requestLifecycleDurationHandlers = []; /** * When the kernel starting handling the current request. * * @var \Illuminate\Support\Carbon|null */ protected $requestStartedAt; /** * The priority-sorted list of middleware. * * Forces non-global middleware to always be in the given order. * * @var string[] */ protected $middlewarePriority = [ \Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests::class, \Illuminate\Cookie\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class, \Illuminate\Routing\Middleware\ThrottleRequests::class, \Illuminate\Routing\Middleware\ThrottleRequestsWithRedis::class, \Illuminate\Contracts\Session\Middleware\AuthenticatesSessions::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, \Illuminate\Auth\Middleware\Authorize::class, ]; /** * Create a new HTTP kernel instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @param \Illuminate\Routing\Router $router * @return void */ public function __construct(Application $app, Router $router) { $this->app = $app; $this->router = $router; $this->syncMiddlewareToRouter(); } /** * Handle an incoming HTTP request. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function handle($request) { $this->requestStartedAt = Carbon::now(); try { $request->enableHttpMethodParameterOverride(); $response = $this->sendRequestThroughRouter($request); } catch (Throwable $e) { $this->reportException($e); $response = $this->renderException($request, $e); } $this->app['events']->dispatch( new RequestHandled($request, $response) ); return $response; } /** * Send the given request through the middleware / router. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ protected function sendRequestThroughRouter($request) { $this->app->instance('request', $request); Facade::clearResolvedInstance('request'); $this->bootstrap(); return (new Pipeline($this->app)) ->send($request) ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware) ->then($this->dispatchToRouter()); } /** * Bootstrap the application for HTTP requests. * * @return void */ public function bootstrap() { if (! $this->app->hasBeenBootstrapped()) { $this->app->bootstrapWith($this->bootstrappers()); } } /** * Get the route dispatcher callback. * * @return \Closure */ protected function dispatchToRouter() { return function ($request) { $this->app->instance('request', $request); return $this->router->dispatch($request); }; } /** * Call the terminate method on any terminable middleware. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Response $response * @return void */ public function terminate($request, $response) { $this->terminateMiddleware($request, $response); $this->app->terminate(); if ($this->requestStartedAt === null) { return; } $this->requestStartedAt->setTimezone($this->app['config']->get('app.timezone') ?? 'UTC'); foreach ($this->requestLifecycleDurationHandlers as ['threshold' => $threshold, 'handler' => $handler]) { $end ??= Carbon::now(); if ($this->requestStartedAt->diffInMilliseconds($end) > $threshold) { $handler($this->requestStartedAt, $request, $response); } } $this->requestStartedAt = null; } /** * Call the terminate method on any terminable middleware. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Response $response * @return void */ protected function terminateMiddleware($request, $response) { $middlewares = $this->app->shouldSkipMiddleware() ? [] : array_merge( $this->gatherRouteMiddleware($request), $this->middleware ); foreach ($middlewares as $middleware) { if (! is_string($middleware)) { continue; } [$name] = $this->parseMiddleware($middleware); $instance = $this->app->make($name); if (method_exists($instance, 'terminate')) { $instance->terminate($request, $response); } } } /** * Register a callback to be invoked when the requests lifecycle duration exceeds a given amount of time. * * @param \DateTimeInterface|\Carbon\CarbonInterval|float|int $threshold * @param callable $handler * @return void */ public function whenRequestLifecycleIsLongerThan($threshold, $handler) { $threshold = $threshold instanceof DateTimeInterface ? $this->secondsUntil($threshold) * 1000 : $threshold; $threshold = $threshold instanceof CarbonInterval ? $threshold->totalMilliseconds : $threshold; $this->requestLifecycleDurationHandlers[] = [ 'threshold' => $threshold, 'handler' => $handler, ]; } /** * When the request being handled started. * * @return \Illuminate\Support\Carbon|null */ public function requestStartedAt() { return $this->requestStartedAt; } /** * Gather the route middleware for the given request. * * @param \Illuminate\Http\Request $request * @return array */ protected function gatherRouteMiddleware($request) { if ($route = $request->route()) { return $this->router->gatherRouteMiddleware($route); } return []; } /** * Parse a middleware string to get the name and parameters. * * @param string $middleware * @return array */ protected function parseMiddleware($middleware) { [$name, $parameters] = array_pad(explode(':', $middleware, 2), 2, []); if (is_string($parameters)) { $parameters = explode(',', $parameters); } return [$name, $parameters]; } /** * Determine if the kernel has a given middleware. * * @param string $middleware * @return bool */ public function hasMiddleware($middleware) { return in_array($middleware, $this->middleware); } /** * Add a new middleware to the beginning of the stack if it does not already exist. * * @param string $middleware * @return $this */ public function prependMiddleware($middleware) { if (array_search($middleware, $this->middleware) === false) { array_unshift($this->middleware, $middleware); } return $this; } /** * Add a new middleware to end of the stack if it does not already exist. * * @param string $middleware * @return $this */ public function pushMiddleware($middleware) { if (array_search($middleware, $this->middleware) === false) { $this->middleware[] = $middleware; } return $this; } /** * Prepend the given middleware to the given middleware group. * * @param string $group * @param string $middleware * @return $this * * @throws \InvalidArgumentException */ public function prependMiddlewareToGroup($group, $middleware) { if (! isset($this->middlewareGroups[$group])) { throw new InvalidArgumentException("The [{$group}] middleware group has not been defined."); } if (array_search($middleware, $this->middlewareGroups[$group]) === false) { array_unshift($this->middlewareGroups[$group], $middleware); } $this->syncMiddlewareToRouter(); return $this; } /** * Append the given middleware to the given middleware group. * * @param string $group * @param string $middleware * @return $this * * @throws \InvalidArgumentException */ public function appendMiddlewareToGroup($group, $middleware) { if (! isset($this->middlewareGroups[$group])) { throw new InvalidArgumentException("The [{$group}] middleware group has not been defined."); } if (array_search($middleware, $this->middlewareGroups[$group]) === false) { $this->middlewareGroups[$group][] = $middleware; } $this->syncMiddlewareToRouter(); return $this; } /** * Prepend the given middleware to the middleware priority list. * * @param string $middleware * @return $this */ public function prependToMiddlewarePriority($middleware) { if (! in_array($middleware, $this->middlewarePriority)) { array_unshift($this->middlewarePriority, $middleware); } $this->syncMiddlewareToRouter(); return $this; } /** * Append the given middleware to the middleware priority list. * * @param string $middleware * @return $this */ public function appendToMiddlewarePriority($middleware) { if (! in_array($middleware, $this->middlewarePriority)) { $this->middlewarePriority[] = $middleware; } $this->syncMiddlewareToRouter(); return $this; } /** * Sync the current state of the middleware to the router. * * @return void */ protected function syncMiddlewareToRouter() { $this->router->middlewarePriority = $this->middlewarePriority; foreach ($this->middlewareGroups as $key => $middleware) { $this->router->middlewareGroup($key, $middleware); } foreach (array_merge($this->routeMiddleware, $this->middlewareAliases) as $key => $middleware) { $this->router->aliasMiddleware($key, $middleware); } } /** * Get the priority-sorted list of middleware. * * @return array */ public function getMiddlewarePriority() { return $this->middlewarePriority; } /** * Get the bootstrap classes for the application. * * @return array */ protected function bootstrappers() { return $this->bootstrappers; } /** * Report the exception to the exception handler. * * @param \Throwable $e * @return void */ protected function reportException(Throwable $e) { $this->app[ExceptionHandler::class]->report($e); } /** * Render the exception to a response. * * @param \Illuminate\Http\Request $request * @param \Throwable $e * @return \Symfony\Component\HttpFoundation\Response */ protected function renderException($request, Throwable $e) { return $this->app[ExceptionHandler::class]->render($request, $e); } /** * Get the application's route middleware groups. * * @return array */ public function getMiddlewareGroups() { return $this->middlewareGroups; } /** * Get the application's route middleware aliases. * * @return array * * @deprecated */ public function getRouteMiddleware() { return $this->getMiddlewareAliases(); } /** * Get the application's route middleware aliases. * * @return array */ public function getMiddlewareAliases() { return array_merge($this->routeMiddleware, $this->middlewareAliases); } /** * Get the Laravel application instance. * * @return \Illuminate\Contracts\Foundation\Application */ public function getApplication() { return $this->app; } /** * Set the Laravel application instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return $this */ public function setApplication(Application $app) { $this->app = $app; return $this; } } src/Illuminate/Foundation/stubs/facade.stub 0000644 00000000460 15107327037 0015013 0 ustar 00 <?php namespace DummyNamespace; use Illuminate\Support\Facades\Facade; /** * @see \DummyTarget */ class DummyClass extends Facade { /** * Get the registered name of the component. */ protected static function getFacadeAccessor(): string { return 'DummyTarget'; } } src/Illuminate/Console/ContainerCommandLoader.php 0000644 00000003440 15107327037 0016127 0 ustar 00 <?php namespace Illuminate\Console; use Psr\Container\ContainerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\CommandLoader\CommandLoaderInterface; use Symfony\Component\Console\Exception\CommandNotFoundException; class ContainerCommandLoader implements CommandLoaderInterface { /** * The container instance. * * @var \Psr\Container\ContainerInterface */ protected $container; /** * A map of command names to classes. * * @var array */ protected $commandMap; /** * Create a new command loader instance. * * @param \Psr\Container\ContainerInterface $container * @param array $commandMap * @return void */ public function __construct(ContainerInterface $container, array $commandMap) { $this->container = $container; $this->commandMap = $commandMap; } /** * Resolve a command from the container. * * @param string $name * @return \Symfony\Component\Console\Command\Command * * @throws \Symfony\Component\Console\Exception\CommandNotFoundException */ public function get(string $name): Command { if (! $this->has($name)) { throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name)); } return $this->container->get($this->commandMap[$name]); } /** * Determines if a command exists. * * @param string $name * @return bool */ public function has(string $name): bool { return $name && isset($this->commandMap[$name]); } /** * Get the command names. * * @return string[] */ public function getNames(): array { return array_keys($this->commandMap); } } src/Illuminate/Console/Contracts/NewLineAware.php 0000644 00000000577 15107327037 0016050 0 ustar 00 <?php namespace Illuminate\Console\Contracts; interface NewLineAware { /** * How many trailing newlines were written. * * @return int */ public function newLinesWritten(); /** * Whether a newline has already been written. * * @return bool * * @deprecated use newLinesWritten */ public function newLineWritten(); } src/Illuminate/Console/CacheCommandMutex.php 0000644 00000007130 15107327037 0015104 0 ustar 00 <?php namespace Illuminate\Console; use Carbon\CarbonInterval; use Illuminate\Cache\DynamoDbStore; use Illuminate\Contracts\Cache\Factory as Cache; use Illuminate\Contracts\Cache\LockProvider; use Illuminate\Support\InteractsWithTime; class CacheCommandMutex implements CommandMutex { use InteractsWithTime; /** * The cache factory implementation. * * @var \Illuminate\Contracts\Cache\Factory */ public $cache; /** * The cache store that should be used. * * @var string|null */ public $store = null; /** * Create a new command mutex. * * @param \Illuminate\Contracts\Cache\Factory $cache */ public function __construct(Cache $cache) { $this->cache = $cache; } /** * Attempt to obtain a command mutex for the given command. * * @param \Illuminate\Console\Command $command * @return bool */ public function create($command) { $store = $this->cache->store($this->store); $expiresAt = method_exists($command, 'isolationLockExpiresAt') ? $command->isolationLockExpiresAt() : CarbonInterval::hour(); if ($this->shouldUseLocks($store->getStore())) { return $store->getStore()->lock( $this->commandMutexName($command), $this->secondsUntil($expiresAt) )->get(); } return $store->add($this->commandMutexName($command), true, $expiresAt); } /** * Determine if a command mutex exists for the given command. * * @param \Illuminate\Console\Command $command * @return bool */ public function exists($command) { $store = $this->cache->store($this->store); if ($this->shouldUseLocks($store->getStore())) { $lock = $store->getStore()->lock($this->commandMutexName($command)); return tap(! $lock->get(), function ($exists) use ($lock) { if ($exists) { $lock->release(); } }); } return $this->cache->store($this->store)->has($this->commandMutexName($command)); } /** * Release the mutex for the given command. * * @param \Illuminate\Console\Command $command * @return bool */ public function forget($command) { $store = $this->cache->store($this->store); if ($this->shouldUseLocks($store->getStore())) { return $store->getStore()->lock($this->commandMutexName($command))->forceRelease(); } return $this->cache->store($this->store)->forget($this->commandMutexName($command)); } /** * Get the isolatable command mutex name. * * @param \Illuminate\Console\Command $command * @return string */ protected function commandMutexName($command) { $baseName = 'framework'.DIRECTORY_SEPARATOR.'command-'.$command->getName(); return method_exists($command, 'isolatableId') ? $baseName.'-'.$command->isolatableId() : $baseName; } /** * Specify the cache store that should be used. * * @param string|null $store * @return $this */ public function useStore($store) { $this->store = $store; return $this; } /** * Determine if the given store should use locks for command mutexes. * * @param \Illuminate\Contracts\Cache\Store $store * @return bool */ protected function shouldUseLocks($store) { return $store instanceof LockProvider && ! $store instanceof DynamoDbStore; } } src/Illuminate/Console/CommandMutex.php 0000644 00000001207 15107327037 0014157 0 ustar 00 <?php namespace Illuminate\Console; interface CommandMutex { /** * Attempt to obtain a command mutex for the given command. * * @param \Illuminate\Console\Command $command * @return bool */ public function create($command); /** * Determine if a command mutex exists for the given command. * * @param \Illuminate\Console\Command $command * @return bool */ public function exists($command); /** * Release the mutex for the given command. * * @param \Illuminate\Console\Command $command * @return bool */ public function forget($command); } src/Illuminate/Console/OutputStyle.php 0000644 00000010647 15107327037 0014107 0 ustar 00 <?php namespace Illuminate\Console; use Illuminate\Console\Contracts\NewLineAware; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Style\SymfonyStyle; class OutputStyle extends SymfonyStyle implements NewLineAware { /** * The output instance. * * @var \Symfony\Component\Console\Output\OutputInterface */ private $output; /** * The number of trailing new lines written by the last output. * * This is initialized as 1 to account for the new line written by the shell after executing a command. * * @var int */ protected $newLinesWritten = 1; /** * If the last output written wrote a new line. * * @var bool * * @deprecated use $newLinesWritten */ protected $newLineWritten = false; /** * Create a new Console OutputStyle instance. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ public function __construct(InputInterface $input, OutputInterface $output) { $this->output = $output; parent::__construct($input, $output); } /** * {@inheritdoc} */ public function askQuestion(Question $question): mixed { try { return parent::askQuestion($question); } finally { $this->newLinesWritten++; } } /** * {@inheritdoc} */ public function write(string|iterable $messages, bool $newline = false, int $options = 0) { $this->newLinesWritten = $this->trailingNewLineCount($messages) + (int) $newline; $this->newLineWritten = $this->newLinesWritten > 0; parent::write($messages, $newline, $options); } /** * {@inheritdoc} * * @return void */ public function writeln(string|iterable $messages, int $type = self::OUTPUT_NORMAL) { $this->newLinesWritten = $this->trailingNewLineCount($messages) + 1; $this->newLineWritten = true; parent::writeln($messages, $type); } /** * {@inheritdoc} * * @return void */ public function newLine(int $count = 1) { $this->newLinesWritten += $count; $this->newLineWritten = $this->newLinesWritten > 0; parent::newLine($count); } /** * {@inheritdoc} */ public function newLinesWritten() { if ($this->output instanceof static) { return $this->output->newLinesWritten(); } return $this->newLinesWritten; } /** * {@inheritdoc} * * @deprecated use newLinesWritten */ public function newLineWritten() { if ($this->output instanceof static && $this->output->newLineWritten()) { return true; } return $this->newLineWritten; } /* * Count the number of trailing new lines in a string. * * @param string|iterable $messages * @return int */ protected function trailingNewLineCount($messages) { if (is_iterable($messages)) { $string = ''; foreach ($messages as $message) { $string .= $message.PHP_EOL; } } else { $string = $messages; } return strlen($string) - strlen(rtrim($string, PHP_EOL)); } /** * Returns whether verbosity is quiet (-q). * * @return bool */ public function isQuiet(): bool { return $this->output->isQuiet(); } /** * Returns whether verbosity is verbose (-v). * * @return bool */ public function isVerbose(): bool { return $this->output->isVerbose(); } /** * Returns whether verbosity is very verbose (-vv). * * @return bool */ public function isVeryVerbose(): bool { return $this->output->isVeryVerbose(); } /** * Returns whether verbosity is debug (-vvv). * * @return bool */ public function isDebug(): bool { return $this->output->isDebug(); } /** * Get the underlying Symfony output implementation. * * @return \Symfony\Component\Console\Output\OutputInterface */ public function getOutput() { return $this->output; } } src/Illuminate/Console/Concerns/CallsCommands.php 0000644 00000005740 15107327037 0016056 0 ustar 00 <?php namespace Illuminate\Console\Concerns; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Output\OutputInterface; trait CallsCommands { /** * Resolve the console command instance for the given command. * * @param \Symfony\Component\Console\Command\Command|string $command * @return \Symfony\Component\Console\Command\Command */ abstract protected function resolveCommand($command); /** * Call another console command. * * @param \Symfony\Component\Console\Command\Command|string $command * @param array $arguments * @return int */ public function call($command, array $arguments = []) { return $this->runCommand($command, $arguments, $this->output); } /** * Call another console command without output. * * @param \Symfony\Component\Console\Command\Command|string $command * @param array $arguments * @return int */ public function callSilent($command, array $arguments = []) { return $this->runCommand($command, $arguments, new NullOutput); } /** * Call another console command without output. * * @param \Symfony\Component\Console\Command\Command|string $command * @param array $arguments * @return int */ public function callSilently($command, array $arguments = []) { return $this->callSilent($command, $arguments); } /** * Run the given the console command. * * @param \Symfony\Component\Console\Command\Command|string $command * @param array $arguments * @param \Symfony\Component\Console\Output\OutputInterface $output * @return int */ protected function runCommand($command, array $arguments, OutputInterface $output) { $arguments['command'] = $command; $result = $this->resolveCommand($command)->run( $this->createInputFromArguments($arguments), $output ); $this->restorePrompts(); return $result; } /** * Create an input instance from the given arguments. * * @param array $arguments * @return \Symfony\Component\Console\Input\ArrayInput */ protected function createInputFromArguments(array $arguments) { return tap(new ArrayInput(array_merge($this->context(), $arguments)), function ($input) { if ($input->getParameterOption('--no-interaction')) { $input->setInteractive(false); } }); } /** * Get all of the context passed to the command. * * @return array */ protected function context() { return collect($this->option())->only([ 'ansi', 'no-ansi', 'no-interaction', 'quiet', 'verbose', ])->filter()->mapWithKeys(function ($value, $key) { return ["--{$key}" => $value]; })->all(); } } src/Illuminate/Console/Concerns/InteractsWithIO.php 0000644 00000026247 15107327037 0016363 0 ustar 00 <?php namespace Illuminate\Console\Concerns; use Closure; use Illuminate\Console\OutputStyle; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Str; use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ChoiceQuestion; use Symfony\Component\Console\Question\Question; trait InteractsWithIO { /** * The console components factory. * * @var \Illuminate\Console\View\Components\Factory * * @internal This property is not meant to be used or overwritten outside the framework. */ protected $components; /** * The input interface implementation. * * @var \Symfony\Component\Console\Input\InputInterface */ protected $input; /** * The output interface implementation. * * @var \Illuminate\Console\OutputStyle */ protected $output; /** * The default verbosity of output commands. * * @var int */ protected $verbosity = OutputInterface::VERBOSITY_NORMAL; /** * The mapping between human readable verbosity levels and Symfony's OutputInterface. * * @var array */ protected $verbosityMap = [ 'v' => OutputInterface::VERBOSITY_VERBOSE, 'vv' => OutputInterface::VERBOSITY_VERY_VERBOSE, 'vvv' => OutputInterface::VERBOSITY_DEBUG, 'quiet' => OutputInterface::VERBOSITY_QUIET, 'normal' => OutputInterface::VERBOSITY_NORMAL, ]; /** * Determine if the given argument is present. * * @param string|int $name * @return bool */ public function hasArgument($name) { return $this->input->hasArgument($name); } /** * Get the value of a command argument. * * @param string|null $key * @return array|string|bool|null */ public function argument($key = null) { if (is_null($key)) { return $this->input->getArguments(); } return $this->input->getArgument($key); } /** * Get all of the arguments passed to the command. * * @return array */ public function arguments() { return $this->argument(); } /** * Determine if the given option is present. * * @param string $name * @return bool */ public function hasOption($name) { return $this->input->hasOption($name); } /** * Get the value of a command option. * * @param string|null $key * @return string|array|bool|null */ public function option($key = null) { if (is_null($key)) { return $this->input->getOptions(); } return $this->input->getOption($key); } /** * Get all of the options passed to the command. * * @return array */ public function options() { return $this->option(); } /** * Confirm a question with the user. * * @param string $question * @param bool $default * @return bool */ public function confirm($question, $default = false) { return $this->output->confirm($question, $default); } /** * Prompt the user for input. * * @param string $question * @param string|null $default * @return mixed */ public function ask($question, $default = null) { return $this->output->ask($question, $default); } /** * Prompt the user for input with auto completion. * * @param string $question * @param array|callable $choices * @param string|null $default * @return mixed */ public function anticipate($question, $choices, $default = null) { return $this->askWithCompletion($question, $choices, $default); } /** * Prompt the user for input with auto completion. * * @param string $question * @param array|callable $choices * @param string|null $default * @return mixed */ public function askWithCompletion($question, $choices, $default = null) { $question = new Question($question, $default); is_callable($choices) ? $question->setAutocompleterCallback($choices) : $question->setAutocompleterValues($choices); return $this->output->askQuestion($question); } /** * Prompt the user for input but hide the answer from the console. * * @param string $question * @param bool $fallback * @return mixed */ public function secret($question, $fallback = true) { $question = new Question($question); $question->setHidden(true)->setHiddenFallback($fallback); return $this->output->askQuestion($question); } /** * Give the user a single choice from an array of answers. * * @param string $question * @param array $choices * @param string|int|null $default * @param mixed|null $attempts * @param bool $multiple * @return string|array */ public function choice($question, array $choices, $default = null, $attempts = null, $multiple = false) { $question = new ChoiceQuestion($question, $choices, $default); $question->setMaxAttempts($attempts)->setMultiselect($multiple); return $this->output->askQuestion($question); } /** * Format input to textual table. * * @param array $headers * @param \Illuminate\Contracts\Support\Arrayable|array $rows * @param \Symfony\Component\Console\Helper\TableStyle|string $tableStyle * @param array $columnStyles * @return void */ public function table($headers, $rows, $tableStyle = 'default', array $columnStyles = []) { $table = new Table($this->output); if ($rows instanceof Arrayable) { $rows = $rows->toArray(); } $table->setHeaders((array) $headers)->setRows($rows)->setStyle($tableStyle); foreach ($columnStyles as $columnIndex => $columnStyle) { $table->setColumnStyle($columnIndex, $columnStyle); } $table->render(); } /** * Execute a given callback while advancing a progress bar. * * @param iterable|int $totalSteps * @param \Closure $callback * @return mixed|void */ public function withProgressBar($totalSteps, Closure $callback) { $bar = $this->output->createProgressBar( is_iterable($totalSteps) ? count($totalSteps) : $totalSteps ); $bar->start(); if (is_iterable($totalSteps)) { foreach ($totalSteps as $value) { $callback($value, $bar); $bar->advance(); } } else { $callback($bar); } $bar->finish(); if (is_iterable($totalSteps)) { return $totalSteps; } } /** * Write a string as information output. * * @param string $string * @param int|string|null $verbosity * @return void */ public function info($string, $verbosity = null) { $this->line($string, 'info', $verbosity); } /** * Write a string as standard output. * * @param string $string * @param string|null $style * @param int|string|null $verbosity * @return void */ public function line($string, $style = null, $verbosity = null) { $styled = $style ? "<$style>$string</$style>" : $string; $this->output->writeln($styled, $this->parseVerbosity($verbosity)); } /** * Write a string as comment output. * * @param string $string * @param int|string|null $verbosity * @return void */ public function comment($string, $verbosity = null) { $this->line($string, 'comment', $verbosity); } /** * Write a string as question output. * * @param string $string * @param int|string|null $verbosity * @return void */ public function question($string, $verbosity = null) { $this->line($string, 'question', $verbosity); } /** * Write a string as error output. * * @param string $string * @param int|string|null $verbosity * @return void */ public function error($string, $verbosity = null) { $this->line($string, 'error', $verbosity); } /** * Write a string as warning output. * * @param string $string * @param int|string|null $verbosity * @return void */ public function warn($string, $verbosity = null) { if (! $this->output->getFormatter()->hasStyle('warning')) { $style = new OutputFormatterStyle('yellow'); $this->output->getFormatter()->setStyle('warning', $style); } $this->line($string, 'warning', $verbosity); } /** * Write a string in an alert box. * * @param string $string * @param int|string|null $verbosity * @return void */ public function alert($string, $verbosity = null) { $length = Str::length(strip_tags($string)) + 12; $this->comment(str_repeat('*', $length), $verbosity); $this->comment('* '.$string.' *', $verbosity); $this->comment(str_repeat('*', $length), $verbosity); $this->comment('', $verbosity); } /** * Write a blank line. * * @param int $count * @return $this */ public function newLine($count = 1) { $this->output->newLine($count); return $this; } /** * Set the input interface implementation. * * @param \Symfony\Component\Console\Input\InputInterface $input * @return void */ public function setInput(InputInterface $input) { $this->input = $input; } /** * Set the output interface implementation. * * @param \Illuminate\Console\OutputStyle $output * @return void */ public function setOutput(OutputStyle $output) { $this->output = $output; } /** * Set the verbosity level. * * @param string|int $level * @return void */ protected function setVerbosity($level) { $this->verbosity = $this->parseVerbosity($level); } /** * Get the verbosity level in terms of Symfony's OutputInterface level. * * @param string|int|null $level * @return int */ protected function parseVerbosity($level = null) { if (isset($this->verbosityMap[$level])) { $level = $this->verbosityMap[$level]; } elseif (! is_int($level)) { $level = $this->verbosity; } return $level; } /** * Get the output implementation. * * @return \Illuminate\Console\OutputStyle */ public function getOutput() { return $this->output; } /** * Get the output component factory implementation. * * @return \Illuminate\Console\View\Components\Factory */ public function outputComponents() { return $this->components; } } src/Illuminate/Console/Concerns/ConfiguresPrompts.php 0000644 00000012573 15107327037 0017031 0 ustar 00 <?php namespace Illuminate\Console\Concerns; use Laravel\Prompts\ConfirmPrompt; use Laravel\Prompts\MultiSearchPrompt; use Laravel\Prompts\MultiSelectPrompt; use Laravel\Prompts\PasswordPrompt; use Laravel\Prompts\Prompt; use Laravel\Prompts\SearchPrompt; use Laravel\Prompts\SelectPrompt; use Laravel\Prompts\SuggestPrompt; use Laravel\Prompts\TextPrompt; use Symfony\Component\Console\Input\InputInterface; trait ConfiguresPrompts { /** * Configure the prompt fallbacks. * * @param \Symfony\Component\Console\Input\InputInterface $input * @return void */ protected function configurePrompts(InputInterface $input) { Prompt::setOutput($this->output); Prompt::interactive(($input->isInteractive() && defined('STDIN') && stream_isatty(STDIN)) || $this->laravel->runningUnitTests()); Prompt::fallbackWhen(windows_os() || $this->laravel->runningUnitTests()); TextPrompt::fallbackUsing(fn (TextPrompt $prompt) => $this->promptUntilValid( fn () => $this->components->ask($prompt->label, $prompt->default ?: null) ?? '', $prompt->required, $prompt->validate )); PasswordPrompt::fallbackUsing(fn (PasswordPrompt $prompt) => $this->promptUntilValid( fn () => $this->components->secret($prompt->label) ?? '', $prompt->required, $prompt->validate )); ConfirmPrompt::fallbackUsing(fn (ConfirmPrompt $prompt) => $this->promptUntilValid( fn () => $this->components->confirm($prompt->label, $prompt->default), $prompt->required, $prompt->validate )); SelectPrompt::fallbackUsing(fn (SelectPrompt $prompt) => $this->promptUntilValid( fn () => $this->components->choice($prompt->label, $prompt->options, $prompt->default), false, $prompt->validate )); MultiSelectPrompt::fallbackUsing(function (MultiSelectPrompt $prompt) { if ($prompt->default !== []) { return $this->promptUntilValid( fn () => $this->components->choice($prompt->label, $prompt->options, implode(',', $prompt->default), multiple: true), $prompt->required, $prompt->validate ); } return $this->promptUntilValid( fn () => collect($this->components->choice($prompt->label, ['' => 'None', ...$prompt->options], 'None', multiple: true)) ->reject('') ->all(), $prompt->required, $prompt->validate ); }); SuggestPrompt::fallbackUsing(fn (SuggestPrompt $prompt) => $this->promptUntilValid( fn () => $this->components->askWithCompletion($prompt->label, $prompt->options, $prompt->default ?: null) ?? '', $prompt->required, $prompt->validate )); SearchPrompt::fallbackUsing(fn (SearchPrompt $prompt) => $this->promptUntilValid( function () use ($prompt) { $query = $this->components->ask($prompt->label); $options = ($prompt->options)($query); return $this->components->choice($prompt->label, $options); }, false, $prompt->validate )); MultiSearchPrompt::fallbackUsing(fn (MultiSearchPrompt $prompt) => $this->promptUntilValid( function () use ($prompt) { $query = $this->components->ask($prompt->label); $options = ($prompt->options)($query); if ($prompt->required === false) { if (array_is_list($options)) { return collect($this->components->choice($prompt->label, ['None', ...$options], 'None', multiple: true)) ->reject('None') ->values() ->all(); } return collect($this->components->choice($prompt->label, ['' => 'None', ...$options], '', multiple: true)) ->reject('') ->values() ->all(); } return $this->components->choice($prompt->label, $options, multiple: true); }, $prompt->required, $prompt->validate )); } /** * Prompt the user until the given validation callback passes. * * @param \Closure $prompt * @param bool|string $required * @param \Closure|null $validate * @return mixed */ protected function promptUntilValid($prompt, $required, $validate) { while (true) { $result = $prompt(); if ($required && ($result === '' || $result === [] || $result === false)) { $this->components->error(is_string($required) ? $required : 'Required.'); continue; } if ($validate) { $error = $validate($result); if (is_string($error) && strlen($error) > 0) { $this->components->error($error); continue; } } return $result; } } /** * Restore the prompts output. * * @return void */ protected function restorePrompts() { Prompt::setOutput($this->output); } } src/Illuminate/Console/Concerns/CreatesMatchingTest.php 0000644 00000002235 15107327037 0017233 0 ustar 00 <?php namespace Illuminate\Console\Concerns; use Illuminate\Support\Str; use Symfony\Component\Console\Input\InputOption; trait CreatesMatchingTest { /** * Add the standard command options for generating matching tests. * * @return void */ protected function addTestOptions() { foreach (['test' => 'PHPUnit', 'pest' => 'Pest'] as $option => $name) { $this->getDefinition()->addOption(new InputOption( $option, null, InputOption::VALUE_NONE, "Generate an accompanying {$name} test for the {$this->type}" )); } } /** * Create the matching test case if requested. * * @param string $path * @return bool */ protected function handleTestCreation($path) { if (! $this->option('test') && ! $this->option('pest')) { return false; } return $this->callSilent('make:test', [ 'name' => Str::of($path)->after($this->laravel['path'])->beforeLast('.php')->append('Test')->replace('\\', '/'), '--pest' => $this->option('pest'), ]) == 0; } } src/Illuminate/Console/Concerns/PromptsForMissingInput.php 0000644 00000006604 15107327037 0020023 0 ustar 00 <?php namespace Illuminate\Console\Concerns; use Closure; use Illuminate\Contracts\Console\PromptsForMissingInput as PromptsForMissingInputContract; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use function Laravel\Prompts\text; trait PromptsForMissingInput { /** * Interact with the user before validating the input. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ protected function interact(InputInterface $input, OutputInterface $output) { parent::interact($input, $output); if ($this instanceof PromptsForMissingInputContract) { $this->promptForMissingArguments($input, $output); } } /** * Prompt the user for any missing arguments. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ protected function promptForMissingArguments(InputInterface $input, OutputInterface $output) { $prompted = collect($this->getDefinition()->getArguments()) ->filter(fn ($argument) => $argument->isRequired() && is_null($input->getArgument($argument->getName()))) ->filter(fn ($argument) => $argument->getName() !== 'command') ->each(function ($argument) use ($input) { $label = $this->promptForMissingArgumentsUsing()[$argument->getName()] ?? 'What is '.lcfirst($argument->getDescription() ?: ('the '.$argument->getName())).'?'; if ($label instanceof Closure) { return $input->setArgument($argument->getName(), $label()); } if (is_array($label)) { [$label, $placeholder] = $label; } $input->setArgument($argument->getName(), text( label: $label, placeholder: $placeholder ?? '', validate: fn ($value) => empty($value) ? "The {$argument->getName()} is required." : null, )); }) ->isNotEmpty(); if ($prompted) { $this->afterPromptingForMissingArguments($input, $output); } } /** * Prompt for missing input arguments using the returned questions. * * @return array */ protected function promptForMissingArgumentsUsing() { return []; } /** * Perform actions after the user was prompted for missing arguments. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) { // } /** * Whether the input contains any options that differ from the default values. * * @param \Symfony\Component\Console\Input\InputInterface $input * @return bool */ protected function didReceiveOptions(InputInterface $input) { return collect($this->getDefinition()->getOptions()) ->reject(fn ($option) => $input->getOption($option->getName()) === $option->getDefault()) ->isNotEmpty(); } } src/Illuminate/Console/Concerns/InteractsWithSignals.php 0000644 00000002235 15107327037 0017443 0 ustar 00 <?php namespace Illuminate\Console\Concerns; use Illuminate\Console\Signals; use Illuminate\Support\Arr; trait InteractsWithSignals { /** * The signal registrar instance. * * @var \Illuminate\Console\Signals|null */ protected $signals; /** * Define a callback to be run when the given signal(s) occurs. * * @param iterable<array-key, int>|int $signals * @param callable(int $signal): void $callback * @return void */ public function trap($signals, $callback) { Signals::whenAvailable(function () use ($signals, $callback) { $this->signals ??= new Signals( $this->getApplication()->getSignalRegistry(), ); collect(Arr::wrap($signals)) ->each(fn ($signal) => $this->signals->register($signal, $callback)); }); } /** * Untrap signal handlers set within the command's handler. * * @return void * * @internal */ public function untrap() { if (! is_null($this->signals)) { $this->signals->unregister(); $this->signals = null; } } } src/Illuminate/Console/Concerns/HasParameters.php 0000644 00000002603 15107327037 0016070 0 ustar 00 <?php namespace Illuminate\Console\Concerns; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; trait HasParameters { /** * Specify the arguments and options on the command. * * @return void */ protected function specifyParameters() { // We will loop through all of the arguments and options for the command and // set them all on the base command instance. This specifies what can get // passed into these commands as "parameters" to control the execution. foreach ($this->getArguments() as $arguments) { if ($arguments instanceof InputArgument) { $this->getDefinition()->addArgument($arguments); } else { $this->addArgument(...$arguments); } } foreach ($this->getOptions() as $options) { if ($options instanceof InputOption) { $this->getDefinition()->addOption($options); } else { $this->addOption(...$options); } } } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return []; } /** * Get the console command options. * * @return array */ protected function getOptions() { return []; } } src/Illuminate/Console/resources/views/components/line.php 0000644 00000000460 15107327037 0020041 0 ustar 00 <div class="mx-2 mb-1 mt-<?php echo $marginTop ?>"> <span class="px-1 bg-<?php echo $bgColor ?> text-<?php echo $fgColor ?> uppercase"><?php echo $title ?></span> <span class="<?php if ($title) { echo 'ml-1'; } ?>"> <?php echo htmlspecialchars($content) ?> </span> </div> src/Illuminate/Console/resources/views/components/two-column-detail.php 0000644 00000000504 15107327037 0022455 0 ustar 00 <div class="flex mx-2 max-w-150"> <span> <?php echo htmlspecialchars($first) ?> </span> <span class="flex-1 content-repeat-[.] text-gray ml-1"></span> <?php if ($second !== '') { ?> <span class="ml-1"> <?php echo htmlspecialchars($second) ?> </span> <?php } ?> </div> src/Illuminate/Console/resources/views/components/alert.php 0000644 00000000203 15107327037 0020214 0 ustar 00 <div class="w-full mx-2 py-1 mt-1 bg-yellow text-black text-center uppercase"> <?php echo htmlspecialchars($content) ?> </div> src/Illuminate/Console/resources/views/components/bullet-list.php 0000644 00000000270 15107327037 0021351 0 ustar 00 <div> <?php foreach ($elements as $element) { ?> <div class="text-gray mx-2"> ⇂ <?php echo htmlspecialchars($element) ?> </div> <?php } ?> </div> src/Illuminate/Console/Scheduling/ManagesFrequencies.php 0000644 00000034752 15107327037 0017423 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Illuminate\Support\Carbon; use InvalidArgumentException; trait ManagesFrequencies { /** * The Cron expression representing the event's frequency. * * @param string $expression * @return $this */ public function cron($expression) { $this->expression = $expression; return $this; } /** * Schedule the event to run between start and end time. * * @param string $startTime * @param string $endTime * @return $this */ public function between($startTime, $endTime) { return $this->when($this->inTimeInterval($startTime, $endTime)); } /** * Schedule the event to not run between start and end time. * * @param string $startTime * @param string $endTime * @return $this */ public function unlessBetween($startTime, $endTime) { return $this->skip($this->inTimeInterval($startTime, $endTime)); } /** * Schedule the event to run between start and end time. * * @param string $startTime * @param string $endTime * @return \Closure */ private function inTimeInterval($startTime, $endTime) { [$now, $startTime, $endTime] = [ Carbon::now($this->timezone), Carbon::parse($startTime, $this->timezone), Carbon::parse($endTime, $this->timezone), ]; if ($endTime->lessThan($startTime)) { if ($startTime->greaterThan($now)) { $startTime = $startTime->subDay(1); } else { $endTime = $endTime->addDay(1); } } return fn () => $now->between($startTime, $endTime); } /** * Schedule the event to run every second. * * @return $this */ public function everySecond() { return $this->repeatEvery(1); } /** * Schedule the event to run every two seconds. * * @return $this */ public function everyTwoSeconds() { return $this->repeatEvery(2); } /** * Schedule the event to run every five seconds. * * @return $this */ public function everyFiveSeconds() { return $this->repeatEvery(5); } /** * Schedule the event to run every ten seconds. * * @return $this */ public function everyTenSeconds() { return $this->repeatEvery(10); } /** * Schedule the event to run every fifteen seconds. * * @return $this */ public function everyFifteenSeconds() { return $this->repeatEvery(15); } /** * Schedule the event to run every twenty seconds. * * @return $this */ public function everyTwentySeconds() { return $this->repeatEvery(20); } /** * Schedule the event to run every thirty seconds. * * @return $this */ public function everyThirtySeconds() { return $this->repeatEvery(30); } /** * Schedule the event to run multiple times per minute. * * @param int $seconds * @return $this */ protected function repeatEvery($seconds) { if (60 % $seconds !== 0) { throw new InvalidArgumentException("The seconds [$seconds] are not evenly divisible by 60."); } $this->repeatSeconds = $seconds; return $this->everyMinute(); } /** * Schedule the event to run every minute. * * @return $this */ public function everyMinute() { return $this->spliceIntoPosition(1, '*'); } /** * Schedule the event to run every two minutes. * * @return $this */ public function everyTwoMinutes() { return $this->spliceIntoPosition(1, '*/2'); } /** * Schedule the event to run every three minutes. * * @return $this */ public function everyThreeMinutes() { return $this->spliceIntoPosition(1, '*/3'); } /** * Schedule the event to run every four minutes. * * @return $this */ public function everyFourMinutes() { return $this->spliceIntoPosition(1, '*/4'); } /** * Schedule the event to run every five minutes. * * @return $this */ public function everyFiveMinutes() { return $this->spliceIntoPosition(1, '*/5'); } /** * Schedule the event to run every ten minutes. * * @return $this */ public function everyTenMinutes() { return $this->spliceIntoPosition(1, '*/10'); } /** * Schedule the event to run every fifteen minutes. * * @return $this */ public function everyFifteenMinutes() { return $this->spliceIntoPosition(1, '*/15'); } /** * Schedule the event to run every thirty minutes. * * @return $this */ public function everyThirtyMinutes() { return $this->spliceIntoPosition(1, '0,30'); } /** * Schedule the event to run hourly. * * @return $this */ public function hourly() { return $this->spliceIntoPosition(1, 0); } /** * Schedule the event to run hourly at a given offset in the hour. * * @param array|string|int $offset * @return $this */ public function hourlyAt($offset) { return $this->hourBasedSchedule($offset, '*'); } /** * Schedule the event to run every odd hour. * * @param array|string|int $offset * @return $this */ public function everyOddHour($offset = 0) { return $this->hourBasedSchedule($offset, '1-23/2'); } /** * Schedule the event to run every two hours. * * @param array|string|int $offset * @return $this */ public function everyTwoHours($offset = 0) { return $this->hourBasedSchedule($offset, '*/2'); } /** * Schedule the event to run every three hours. * * @param array|string|int $offset * @return $this */ public function everyThreeHours($offset = 0) { return $this->hourBasedSchedule($offset, '*/3'); } /** * Schedule the event to run every four hours. * * @param array|string|int $offset * @return $this */ public function everyFourHours($offset = 0) { return $this->hourBasedSchedule($offset, '*/4'); } /** * Schedule the event to run every six hours. * * @param array|string|int $offset * @return $this */ public function everySixHours($offset = 0) { return $this->hourBasedSchedule($offset, '*/6'); } /** * Schedule the event to run daily. * * @return $this */ public function daily() { return $this->hourBasedSchedule(0, 0); } /** * Schedule the command at a given time. * * @param string $time * @return $this */ public function at($time) { return $this->dailyAt($time); } /** * Schedule the event to run daily at a given time (10:00, 19:30, etc). * * @param string $time * @return $this */ public function dailyAt($time) { $segments = explode(':', $time); return $this->hourBasedSchedule( count($segments) === 2 ? (int) $segments[1] : '0', (int) $segments[0] ); } /** * Schedule the event to run twice daily. * * @param int $first * @param int $second * @return $this */ public function twiceDaily($first = 1, $second = 13) { return $this->twiceDailyAt($first, $second, 0); } /** * Schedule the event to run twice daily at a given offset. * * @param int $first * @param int $second * @param int $offset * @return $this */ public function twiceDailyAt($first = 1, $second = 13, $offset = 0) { $hours = $first.','.$second; return $this->hourBasedSchedule($offset, $hours); } /** * Schedule the event to run at the given minutes and hours. * * @param array|string|int $minutes * @param array|string|int $hours * @return $this */ protected function hourBasedSchedule($minutes, $hours) { $minutes = is_array($minutes) ? implode(',', $minutes) : $minutes; $hours = is_array($hours) ? implode(',', $hours) : $hours; return $this->spliceIntoPosition(1, $minutes) ->spliceIntoPosition(2, $hours); } /** * Schedule the event to run only on weekdays. * * @return $this */ public function weekdays() { return $this->days(Schedule::MONDAY.'-'.Schedule::FRIDAY); } /** * Schedule the event to run only on weekends. * * @return $this */ public function weekends() { return $this->days(Schedule::SATURDAY.','.Schedule::SUNDAY); } /** * Schedule the event to run only on Mondays. * * @return $this */ public function mondays() { return $this->days(Schedule::MONDAY); } /** * Schedule the event to run only on Tuesdays. * * @return $this */ public function tuesdays() { return $this->days(Schedule::TUESDAY); } /** * Schedule the event to run only on Wednesdays. * * @return $this */ public function wednesdays() { return $this->days(Schedule::WEDNESDAY); } /** * Schedule the event to run only on Thursdays. * * @return $this */ public function thursdays() { return $this->days(Schedule::THURSDAY); } /** * Schedule the event to run only on Fridays. * * @return $this */ public function fridays() { return $this->days(Schedule::FRIDAY); } /** * Schedule the event to run only on Saturdays. * * @return $this */ public function saturdays() { return $this->days(Schedule::SATURDAY); } /** * Schedule the event to run only on Sundays. * * @return $this */ public function sundays() { return $this->days(Schedule::SUNDAY); } /** * Schedule the event to run weekly. * * @return $this */ public function weekly() { return $this->spliceIntoPosition(1, 0) ->spliceIntoPosition(2, 0) ->spliceIntoPosition(5, 0); } /** * Schedule the event to run weekly on a given day and time. * * @param array|mixed $dayOfWeek * @param string $time * @return $this */ public function weeklyOn($dayOfWeek, $time = '0:0') { $this->dailyAt($time); return $this->days($dayOfWeek); } /** * Schedule the event to run monthly. * * @return $this */ public function monthly() { return $this->spliceIntoPosition(1, 0) ->spliceIntoPosition(2, 0) ->spliceIntoPosition(3, 1); } /** * Schedule the event to run monthly on a given day and time. * * @param int $dayOfMonth * @param string $time * @return $this */ public function monthlyOn($dayOfMonth = 1, $time = '0:0') { $this->dailyAt($time); return $this->spliceIntoPosition(3, $dayOfMonth); } /** * Schedule the event to run twice monthly at a given time. * * @param int $first * @param int $second * @param string $time * @return $this */ public function twiceMonthly($first = 1, $second = 16, $time = '0:0') { $daysOfMonth = $first.','.$second; $this->dailyAt($time); return $this->spliceIntoPosition(3, $daysOfMonth); } /** * Schedule the event to run on the last day of the month. * * @param string $time * @return $this */ public function lastDayOfMonth($time = '0:0') { $this->dailyAt($time); return $this->spliceIntoPosition(3, Carbon::now()->endOfMonth()->day); } /** * Schedule the event to run quarterly. * * @return $this */ public function quarterly() { return $this->spliceIntoPosition(1, 0) ->spliceIntoPosition(2, 0) ->spliceIntoPosition(3, 1) ->spliceIntoPosition(4, '1-12/3'); } /** * Schedule the event to run quarterly on a given day and time. * * @param int $dayOfQuarter * @param int $time * @return $this */ public function quarterlyOn($dayOfQuarter = 1, $time = '0:0') { $this->dailyAt($time); return $this->spliceIntoPosition(3, $dayOfQuarter) ->spliceIntoPosition(4, '1-12/3'); } /** * Schedule the event to run yearly. * * @return $this */ public function yearly() { return $this->spliceIntoPosition(1, 0) ->spliceIntoPosition(2, 0) ->spliceIntoPosition(3, 1) ->spliceIntoPosition(4, 1); } /** * Schedule the event to run yearly on a given month, day, and time. * * @param int $month * @param int|string $dayOfMonth * @param string $time * @return $this */ public function yearlyOn($month = 1, $dayOfMonth = 1, $time = '0:0') { $this->dailyAt($time); return $this->spliceIntoPosition(3, $dayOfMonth) ->spliceIntoPosition(4, $month); } /** * Set the days of the week the command should run on. * * @param array|mixed $days * @return $this */ public function days($days) { $days = is_array($days) ? $days : func_get_args(); return $this->spliceIntoPosition(5, implode(',', $days)); } /** * Set the timezone the date should be evaluated on. * * @param \DateTimeZone|string $timezone * @return $this */ public function timezone($timezone) { $this->timezone = $timezone; return $this; } /** * Splice the given value into the given position of the expression. * * @param int $position * @param string $value * @return $this */ protected function spliceIntoPosition($position, $value) { $segments = preg_split("/\s+/", $this->expression); $segments[$position - 1] = $value; return $this->cron(implode(' ', $segments)); } } src/Illuminate/Console/Scheduling/Schedule.php 0000644 00000024104 15107327037 0015400 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Closure; use DateTimeInterface; use Illuminate\Bus\UniqueLock; use Illuminate\Console\Application; use Illuminate\Container\Container; use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\CallQueuedClosure; use Illuminate\Support\ProcessUtils; use Illuminate\Support\Traits\Macroable; use RuntimeException; class Schedule { use Macroable; const SUNDAY = 0; const MONDAY = 1; const TUESDAY = 2; const WEDNESDAY = 3; const THURSDAY = 4; const FRIDAY = 5; const SATURDAY = 6; /** * All of the events on the schedule. * * @var \Illuminate\Console\Scheduling\Event[] */ protected $events = []; /** * The event mutex implementation. * * @var \Illuminate\Console\Scheduling\EventMutex */ protected $eventMutex; /** * The scheduling mutex implementation. * * @var \Illuminate\Console\Scheduling\SchedulingMutex */ protected $schedulingMutex; /** * The timezone the date should be evaluated on. * * @var \DateTimeZone|string */ protected $timezone; /** * The job dispatcher implementation. * * @var \Illuminate\Contracts\Bus\Dispatcher */ protected $dispatcher; /** * The cache of mutex results. * * @var array<string, bool> */ protected $mutexCache = []; /** * Create a new schedule instance. * * @param \DateTimeZone|string|null $timezone * @return void * * @throws \RuntimeException */ public function __construct($timezone = null) { $this->timezone = $timezone; if (! class_exists(Container::class)) { throw new RuntimeException( 'A container implementation is required to use the scheduler. Please install the illuminate/container package.' ); } $container = Container::getInstance(); $this->eventMutex = $container->bound(EventMutex::class) ? $container->make(EventMutex::class) : $container->make(CacheEventMutex::class); $this->schedulingMutex = $container->bound(SchedulingMutex::class) ? $container->make(SchedulingMutex::class) : $container->make(CacheSchedulingMutex::class); } /** * Add a new callback event to the schedule. * * @param string|callable $callback * @param array $parameters * @return \Illuminate\Console\Scheduling\CallbackEvent */ public function call($callback, array $parameters = []) { $this->events[] = $event = new CallbackEvent( $this->eventMutex, $callback, $parameters, $this->timezone ); return $event; } /** * Add a new Artisan command event to the schedule. * * @param string $command * @param array $parameters * @return \Illuminate\Console\Scheduling\Event */ public function command($command, array $parameters = []) { if (class_exists($command)) { $command = Container::getInstance()->make($command); return $this->exec( Application::formatCommandString($command->getName()), $parameters, )->description($command->getDescription()); } return $this->exec( Application::formatCommandString($command), $parameters ); } /** * Add a new job callback event to the schedule. * * @param object|string $job * @param string|null $queue * @param string|null $connection * @return \Illuminate\Console\Scheduling\CallbackEvent */ public function job($job, $queue = null, $connection = null) { return $this->call(function () use ($job, $queue, $connection) { $job = is_string($job) ? Container::getInstance()->make($job) : $job; if ($job instanceof ShouldQueue) { $this->dispatchToQueue($job, $queue ?? $job->queue, $connection ?? $job->connection); } else { $this->dispatchNow($job); } })->name(is_string($job) ? $job : get_class($job)); } /** * Dispatch the given job to the queue. * * @param object $job * @param string|null $queue * @param string|null $connection * @return void * * @throws \RuntimeException */ protected function dispatchToQueue($job, $queue, $connection) { if ($job instanceof Closure) { if (! class_exists(CallQueuedClosure::class)) { throw new RuntimeException( 'To enable support for closure jobs, please install the illuminate/queue package.' ); } $job = CallQueuedClosure::create($job); } if ($job instanceof ShouldBeUnique) { return $this->dispatchUniqueJobToQueue($job, $queue, $connection); } $this->getDispatcher()->dispatch( $job->onConnection($connection)->onQueue($queue) ); } /** * Dispatch the given unique job to the queue. * * @param object $job * @param string|null $queue * @param string|null $connection * @return void * * @throws \RuntimeException */ protected function dispatchUniqueJobToQueue($job, $queue, $connection) { if (! Container::getInstance()->bound(Cache::class)) { throw new RuntimeException('Cache driver not available. Scheduling unique jobs not supported.'); } if (! (new UniqueLock(Container::getInstance()->make(Cache::class)))->acquire($job)) { return; } $this->getDispatcher()->dispatch( $job->onConnection($connection)->onQueue($queue) ); } /** * Dispatch the given job right now. * * @param object $job * @return void */ protected function dispatchNow($job) { $this->getDispatcher()->dispatchNow($job); } /** * Add a new command event to the schedule. * * @param string $command * @param array $parameters * @return \Illuminate\Console\Scheduling\Event */ public function exec($command, array $parameters = []) { if (count($parameters)) { $command .= ' '.$this->compileParameters($parameters); } $this->events[] = $event = new Event($this->eventMutex, $command, $this->timezone); return $event; } /** * Compile parameters for a command. * * @param array $parameters * @return string */ protected function compileParameters(array $parameters) { return collect($parameters)->map(function ($value, $key) { if (is_array($value)) { return $this->compileArrayInput($key, $value); } if (! is_numeric($value) && ! preg_match('/^(-.$|--.*)/i', $value)) { $value = ProcessUtils::escapeArgument($value); } return is_numeric($key) ? $value : "{$key}={$value}"; })->implode(' '); } /** * Compile array input for a command. * * @param string|int $key * @param array $value * @return string */ public function compileArrayInput($key, $value) { $value = collect($value)->map(function ($value) { return ProcessUtils::escapeArgument($value); }); if (str_starts_with($key, '--')) { $value = $value->map(function ($value) use ($key) { return "{$key}={$value}"; }); } elseif (str_starts_with($key, '-')) { $value = $value->map(function ($value) use ($key) { return "{$key} {$value}"; }); } return $value->implode(' '); } /** * Determine if the server is allowed to run this event. * * @param \Illuminate\Console\Scheduling\Event $event * @param \DateTimeInterface $time * @return bool */ public function serverShouldRun(Event $event, DateTimeInterface $time) { return $this->mutexCache[$event->mutexName()] ??= $this->schedulingMutex->create($event, $time); } /** * Get all of the events on the schedule that are due. * * @param \Illuminate\Contracts\Foundation\Application $app * @return \Illuminate\Support\Collection */ public function dueEvents($app) { return collect($this->events)->filter->isDue($app); } /** * Get all of the events on the schedule. * * @return \Illuminate\Console\Scheduling\Event[] */ public function events() { return $this->events; } /** * Specify the cache store that should be used to store mutexes. * * @param string $store * @return $this */ public function useCache($store) { if ($this->eventMutex instanceof CacheAware) { $this->eventMutex->useStore($store); } if ($this->schedulingMutex instanceof CacheAware) { $this->schedulingMutex->useStore($store); } return $this; } /** * Get the job dispatcher, if available. * * @return \Illuminate\Contracts\Bus\Dispatcher * * @throws \RuntimeException */ protected function getDispatcher() { if ($this->dispatcher === null) { try { $this->dispatcher = Container::getInstance()->make(Dispatcher::class); } catch (BindingResolutionException $e) { throw new RuntimeException( 'Unable to resolve the dispatcher from the service container. Please bind it or install the illuminate/bus package.', is_int($e->getCode()) ? $e->getCode() : 0, $e ); } } return $this->dispatcher; } } src/Illuminate/Console/Scheduling/ScheduleRunCommand.php 0000644 00000016303 15107327037 0017366 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Illuminate\Console\Application; use Illuminate\Console\Command; use Illuminate\Console\Events\ScheduledTaskFailed; use Illuminate\Console\Events\ScheduledTaskFinished; use Illuminate\Console\Events\ScheduledTaskSkipped; use Illuminate\Console\Events\ScheduledTaskStarting; use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Date; use Illuminate\Support\Sleep; use Symfony\Component\Console\Attribute\AsCommand; use Throwable; #[AsCommand(name: 'schedule:run')] class ScheduleRunCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'schedule:run'; /** * The console command description. * * @var string */ protected $description = 'Run the scheduled commands'; /** * The schedule instance. * * @var \Illuminate\Console\Scheduling\Schedule */ protected $schedule; /** * The 24 hour timestamp this scheduler command started running. * * @var \Illuminate\Support\Carbon */ protected $startedAt; /** * Check if any events ran. * * @var bool */ protected $eventsRan = false; /** * The event dispatcher. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $dispatcher; /** * The exception handler. * * @var \Illuminate\Contracts\Debug\ExceptionHandler */ protected $handler; /** * The cache store implementation. * * @var \Illuminate\Contracts\Cache\Repository */ protected $cache; /** * The PHP binary used by the command. * * @var string */ protected $phpBinary; /** * Create a new command instance. * * @return void */ public function __construct() { $this->startedAt = Date::now(); parent::__construct(); } /** * Execute the console command. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher * @param \Illuminate\Contracts\Cache\Repository $cache * @param \Illuminate\Contracts\Debug\ExceptionHandler $handler * @return void */ public function handle(Schedule $schedule, Dispatcher $dispatcher, Cache $cache, ExceptionHandler $handler) { $this->schedule = $schedule; $this->dispatcher = $dispatcher; $this->cache = $cache; $this->handler = $handler; $this->phpBinary = Application::phpBinary(); $this->clearInterruptSignal(); $this->newLine(); $events = $this->schedule->dueEvents($this->laravel); foreach ($events as $event) { if (! $event->filtersPass($this->laravel)) { $this->dispatcher->dispatch(new ScheduledTaskSkipped($event)); continue; } if ($event->onOneServer) { $this->runSingleServerEvent($event); } else { $this->runEvent($event); } $this->eventsRan = true; } if ($events->contains->isRepeatable()) { $this->repeatEvents($events->filter->isRepeatable()); } if (! $this->eventsRan) { $this->components->info('No scheduled commands are ready to run.'); } else { $this->newLine(); } } /** * Run the given single server event. * * @param \Illuminate\Console\Scheduling\Event $event * @return void */ protected function runSingleServerEvent($event) { if ($this->schedule->serverShouldRun($event, $this->startedAt)) { $this->runEvent($event); } else { $this->components->info(sprintf( 'Skipping [%s], as command already run on another server.', $event->getSummaryForDisplay() )); } } /** * Run the given event. * * @param \Illuminate\Console\Scheduling\Event $event * @return void */ protected function runEvent($event) { $summary = $event->getSummaryForDisplay(); $command = $event instanceof CallbackEvent ? $summary : trim(str_replace($this->phpBinary, '', $event->command)); $description = sprintf( '<fg=gray>%s</> Running [%s]%s', Carbon::now()->format('Y-m-d H:i:s'), $command, $event->runInBackground ? ' in background' : '', ); $this->components->task($description, function () use ($event) { $this->dispatcher->dispatch(new ScheduledTaskStarting($event)); $start = microtime(true); try { $event->run($this->laravel); $this->dispatcher->dispatch(new ScheduledTaskFinished( $event, round(microtime(true) - $start, 2) )); $this->eventsRan = true; } catch (Throwable $e) { $this->dispatcher->dispatch(new ScheduledTaskFailed($event, $e)); $this->handler->report($e); } return $event->exitCode == 0; }); if (! $event instanceof CallbackEvent) { $this->components->bulletList([ $event->getSummaryForDisplay(), ]); } } /** * Run the given repeating events. * * @param \Illuminate\Support\Collection<\Illuminate\Console\Scheduling\Event> $events * @return void */ protected function repeatEvents($events) { $hasEnteredMaintenanceMode = false; while (Date::now()->lte($this->startedAt->endOfMinute())) { foreach ($events as $event) { if ($this->shouldInterrupt()) { return; } if (! $event->shouldRepeatNow()) { continue; } $hasEnteredMaintenanceMode = $hasEnteredMaintenanceMode || $this->laravel->isDownForMaintenance(); if ($hasEnteredMaintenanceMode && ! $event->runsInMaintenanceMode()) { continue; } if (! $event->filtersPass($this->laravel)) { $this->dispatcher->dispatch(new ScheduledTaskSkipped($event)); continue; } if ($event->onOneServer) { $this->runSingleServerEvent($event); } else { $this->runEvent($event); } $this->eventsRan = true; } Sleep::usleep(100000); } } /** * Determine if the schedule run should be interrupted. * * @return bool */ protected function shouldInterrupt() { return $this->cache->get('illuminate:schedule:interrupt', false); } /** * Ensure the interrupt signal is cleared. * * @return bool */ protected function clearInterruptSignal() { $this->cache->forget('illuminate:schedule:interrupt'); } } src/Illuminate/Console/Scheduling/ScheduleWorkCommand.php 0000644 00000004431 15107327037 0017543 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Illuminate\Console\Command; use Illuminate\Support\Carbon; use Illuminate\Support\ProcessUtils; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Process\Process; #[AsCommand(name: 'schedule:work')] class ScheduleWorkCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'schedule:work {--run-output-file= : The file to direct <info>schedule:run</info> output to}'; /** * The console command description. * * @var string */ protected $description = 'Start the schedule worker'; /** * Execute the console command. * * @return void */ public function handle() { $this->components->info( 'Running scheduled tasks every minute.', $this->getLaravel()->isLocal() ? OutputInterface::VERBOSITY_NORMAL : OutputInterface::VERBOSITY_VERBOSE ); [$lastExecutionStartedAt, $executions] = [Carbon::now()->subMinutes(10), []]; $command = implode(' ', array_map(fn ($arg) => ProcessUtils::escapeArgument($arg), [ PHP_BINARY, defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan', 'schedule:run', ])); if ($this->option('run-output-file')) { $command .= ' >> '.ProcessUtils::escapeArgument($this->option('run-output-file')).' 2>&1'; } while (true) { usleep(100 * 1000); if (Carbon::now()->second === 0 && ! Carbon::now()->startOfMinute()->equalTo($lastExecutionStartedAt)) { $executions[] = $execution = Process::fromShellCommandline($command); $execution->start(); $lastExecutionStartedAt = Carbon::now()->startOfMinute(); } foreach ($executions as $key => $execution) { $output = $execution->getIncrementalOutput(). $execution->getIncrementalErrorOutput(); $this->output->write(ltrim($output, "\n")); if (! $execution->isRunning()) { unset($executions[$key]); } } } } } src/Illuminate/Console/Scheduling/SchedulingMutex.php 0000644 00000001222 15107327037 0016750 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use DateTimeInterface; interface SchedulingMutex { /** * Attempt to obtain a scheduling mutex for the given event. * * @param \Illuminate\Console\Scheduling\Event $event * @param \DateTimeInterface $time * @return bool */ public function create(Event $event, DateTimeInterface $time); /** * Determine if a scheduling mutex exists for the given event. * * @param \Illuminate\Console\Scheduling\Event $event * @param \DateTimeInterface $time * @return bool */ public function exists(Event $event, DateTimeInterface $time); } src/Illuminate/Console/Scheduling/CallbackEvent.php 0000644 00000011416 15107327037 0016344 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Illuminate\Contracts\Container\Container; use Illuminate\Support\Reflector; use InvalidArgumentException; use LogicException; use RuntimeException; use Throwable; class CallbackEvent extends Event { /** * The callback to call. * * @var string */ protected $callback; /** * The parameters to pass to the method. * * @var array */ protected $parameters; /** * The result of the callback's execution. * * @var mixed */ protected $result; /** * The exception that was thrown when calling the callback, if any. * * @var \Throwable|null */ protected $exception; /** * Create a new event instance. * * @param \Illuminate\Console\Scheduling\EventMutex $mutex * @param string|callable $callback * @param array $parameters * @param \DateTimeZone|string|null $timezone * @return void * * @throws \InvalidArgumentException */ public function __construct(EventMutex $mutex, $callback, array $parameters = [], $timezone = null) { if (! is_string($callback) && ! Reflector::isCallable($callback)) { throw new InvalidArgumentException( 'Invalid scheduled callback event. Must be a string or callable.' ); } $this->mutex = $mutex; $this->callback = $callback; $this->parameters = $parameters; $this->timezone = $timezone; } /** * Run the callback event. * * @param \Illuminate\Contracts\Container\Container $container * @return mixed * * @throws \Throwable */ public function run(Container $container) { parent::run($container); if ($this->exception) { throw $this->exception; } return $this->result; } /** * Determine if the event should skip because another process is overlapping. * * @return bool */ public function shouldSkipDueToOverlapping() { return $this->description && parent::shouldSkipDueToOverlapping(); } /** * Indicate that the callback should run in the background. * * @return void * * @throws \RuntimeException */ public function runInBackground() { throw new RuntimeException('Scheduled closures can not be run in the background.'); } /** * Run the callback. * * @param \Illuminate\Contracts\Container\Container $container * @return int */ protected function execute($container) { try { $this->result = is_object($this->callback) ? $container->call([$this->callback, '__invoke'], $this->parameters) : $container->call($this->callback, $this->parameters); return $this->result === false ? 1 : 0; } catch (Throwable $e) { $this->exception = $e; return 1; } } /** * Do not allow the event to overlap each other. * * The expiration time of the underlying cache lock may be specified in minutes. * * @param int $expiresAt * @return $this * * @throws \LogicException */ public function withoutOverlapping($expiresAt = 1440) { if (! isset($this->description)) { throw new LogicException( "A scheduled event name is required to prevent overlapping. Use the 'name' method before 'withoutOverlapping'." ); } return parent::withoutOverlapping($expiresAt); } /** * Allow the event to only run on one server for each cron expression. * * @return $this * * @throws \LogicException */ public function onOneServer() { if (! isset($this->description)) { throw new LogicException( "A scheduled event name is required to only run on one server. Use the 'name' method before 'onOneServer'." ); } return parent::onOneServer(); } /** * Get the summary of the event for display. * * @return string */ public function getSummaryForDisplay() { if (is_string($this->description)) { return $this->description; } return is_string($this->callback) ? $this->callback : 'Callback'; } /** * Get the mutex name for the scheduled command. * * @return string */ public function mutexName() { return 'framework/schedule-'.sha1($this->description ?? ''); } /** * Clear the mutex for the event. * * @return void */ protected function removeMutex() { if ($this->description) { parent::removeMutex(); } } } src/Illuminate/Console/Scheduling/ScheduleTestCommand.php 0000644 00000006527 15107327037 0017550 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Illuminate\Console\Application; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; use function Laravel\Prompts\select; #[AsCommand(name: 'schedule:test')] class ScheduleTestCommand extends Command { /** * The console command name. * * @var string */ protected $signature = 'schedule:test {--name= : The name of the scheduled command to run}'; /** * The console command description. * * @var string */ protected $description = 'Run a scheduled command'; /** * Execute the console command. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ public function handle(Schedule $schedule) { $phpBinary = Application::phpBinary(); $commands = $schedule->events(); $commandNames = []; foreach ($commands as $command) { $commandNames[] = $command->command ?? $command->getSummaryForDisplay(); } if (empty($commandNames)) { return $this->components->info('No scheduled commands have been defined.'); } if (! empty($name = $this->option('name'))) { $commandBinary = $phpBinary.' '.Application::artisanBinary(); $matches = array_filter($commandNames, function ($commandName) use ($commandBinary, $name) { return trim(str_replace($commandBinary, '', $commandName)) === $name; }); if (count($matches) !== 1) { $this->components->info('No matching scheduled command found.'); return; } $index = key($matches); } else { $index = $this->getSelectedCommandByIndex($commandNames); } $event = $commands[$index]; $summary = $event->getSummaryForDisplay(); $command = $event instanceof CallbackEvent ? $summary : trim(str_replace($phpBinary, '', $event->command)); $description = sprintf( 'Running [%s]%s', $command, $event->runInBackground ? ' in background' : '', ); $this->components->task($description, fn () => $event->run($this->laravel)); if (! $event instanceof CallbackEvent) { $this->components->bulletList([$event->getSummaryForDisplay()]); } $this->newLine(); } /** * Get the selected command name by index. * * @param array $commandNames * @return int */ protected function getSelectedCommandByIndex(array $commandNames) { if (count($commandNames) !== count(array_unique($commandNames))) { // Some commands (likely closures) have the same name, append unique indexes to each one... $uniqueCommandNames = array_map(function ($index, $value) { return "$value [$index]"; }, array_keys($commandNames), $commandNames); $selectedCommand = select('Which command would you like to run?', $uniqueCommandNames); preg_match('/\[(\d+)\]/', $selectedCommand, $choice); return (int) $choice[1]; } else { return array_search( select('Which command would you like to run?', $commandNames), $commandNames ); } } } src/Illuminate/Console/Scheduling/CacheSchedulingMutex.php 0000644 00000003273 15107327037 0017704 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use DateTimeInterface; use Illuminate\Contracts\Cache\Factory as Cache; class CacheSchedulingMutex implements SchedulingMutex, CacheAware { /** * The cache factory implementation. * * @var \Illuminate\Contracts\Cache\Factory */ public $cache; /** * The cache store that should be used. * * @var string|null */ public $store; /** * Create a new scheduling strategy. * * @param \Illuminate\Contracts\Cache\Factory $cache * @return void */ public function __construct(Cache $cache) { $this->cache = $cache; } /** * Attempt to obtain a scheduling mutex for the given event. * * @param \Illuminate\Console\Scheduling\Event $event * @param \DateTimeInterface $time * @return bool */ public function create(Event $event, DateTimeInterface $time) { return $this->cache->store($this->store)->add( $event->mutexName().$time->format('Hi'), true, 3600 ); } /** * Determine if a scheduling mutex exists for the given event. * * @param \Illuminate\Console\Scheduling\Event $event * @param \DateTimeInterface $time * @return bool */ public function exists(Event $event, DateTimeInterface $time) { return $this->cache->store($this->store)->has( $event->mutexName().$time->format('Hi') ); } /** * Specify the cache store that should be used. * * @param string $store * @return $this */ public function useStore($store) { $this->store = $store; return $this; } } src/Illuminate/Console/Scheduling/CacheAware.php 0000644 00000000356 15107327037 0015632 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; interface CacheAware { /** * Specify the cache store that should be used. * * @param string $store * @return $this */ public function useStore($store); } src/Illuminate/Console/Scheduling/CacheEventMutex.php 0000644 00000005660 15107327037 0016702 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Illuminate\Cache\DynamoDbStore; use Illuminate\Contracts\Cache\Factory as Cache; use Illuminate\Contracts\Cache\LockProvider; class CacheEventMutex implements EventMutex, CacheAware { /** * The cache repository implementation. * * @var \Illuminate\Contracts\Cache\Factory */ public $cache; /** * The cache store that should be used. * * @var string|null */ public $store; /** * Create a new overlapping strategy. * * @param \Illuminate\Contracts\Cache\Factory $cache * @return void */ public function __construct(Cache $cache) { $this->cache = $cache; } /** * Attempt to obtain an event mutex for the given event. * * @param \Illuminate\Console\Scheduling\Event $event * @return bool */ public function create(Event $event) { if ($this->shouldUseLocks($this->cache->store($this->store)->getStore())) { return $this->cache->store($this->store)->getStore() ->lock($event->mutexName(), $event->expiresAt * 60) ->acquire(); } return $this->cache->store($this->store)->add( $event->mutexName(), true, $event->expiresAt * 60 ); } /** * Determine if an event mutex exists for the given event. * * @param \Illuminate\Console\Scheduling\Event $event * @return bool */ public function exists(Event $event) { if ($this->shouldUseLocks($this->cache->store($this->store)->getStore())) { return ! $this->cache->store($this->store)->getStore() ->lock($event->mutexName(), $event->expiresAt * 60) ->get(fn () => true); } return $this->cache->store($this->store)->has($event->mutexName()); } /** * Clear the event mutex for the given event. * * @param \Illuminate\Console\Scheduling\Event $event * @return void */ public function forget(Event $event) { if ($this->shouldUseLocks($this->cache->store($this->store)->getStore())) { $this->cache->store($this->store)->getStore() ->lock($event->mutexName(), $event->expiresAt * 60) ->forceRelease(); return; } $this->cache->store($this->store)->forget($event->mutexName()); } /** * Determine if the given store should use locks for cache event mutexes. * * @param \Illuminate\Contracts\Cache\Store $store * @return bool */ protected function shouldUseLocks($store) { return $store instanceof LockProvider && ! $store instanceof DynamoDbStore; } /** * Specify the cache store that should be used. * * @param string $store * @return $this */ public function useStore($store) { $this->store = $store; return $this; } } src/Illuminate/Console/Scheduling/ScheduleClearCacheCommand.php 0000644 00000002075 15107327037 0020575 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Illuminate\Console\Command; class ScheduleClearCacheCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'schedule:clear-cache'; /** * The console command description. * * @var string */ protected $description = 'Delete the cached mutex files created by scheduler'; /** * Execute the console command. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ public function handle(Schedule $schedule) { $mutexCleared = false; foreach ($schedule->events($this->laravel) as $event) { if ($event->mutex->exists($event)) { $this->components->info(sprintf('Deleting mutex for [%s]', $event->command)); $event->mutex->forget($event); $mutexCleared = true; } } if (! $mutexCleared) { $this->components->info('No mutex files were found.'); } } } src/Illuminate/Console/Scheduling/ScheduleFinishCommand.php 0000644 00000002470 15107327037 0020042 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Illuminate\Console\Command; use Illuminate\Console\Events\ScheduledBackgroundTaskFinished; use Illuminate\Contracts\Events\Dispatcher; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'schedule:finish')] class ScheduleFinishCommand extends Command { /** * The console command name. * * @var string */ protected $signature = 'schedule:finish {id} {code=0}'; /** * The console command description. * * @var string */ protected $description = 'Handle the completion of a scheduled command'; /** * Indicates whether the command should be shown in the Artisan command list. * * @var bool */ protected $hidden = true; /** * Execute the console command. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ public function handle(Schedule $schedule) { collect($schedule->events())->filter(function ($value) { return $value->mutexName() == $this->argument('id'); })->each(function ($event) { $event->finish($this->laravel, $this->argument('code')); $this->laravel->make(Dispatcher::class)->dispatch(new ScheduledBackgroundTaskFinished($event)); }); } } src/Illuminate/Console/Scheduling/Event.php 0000644 00000057530 15107327037 0014736 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Closure; use Cron\CronExpression; use GuzzleHttp\Client as HttpClient; use GuzzleHttp\Exception\TransferException; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Mail\Mailer; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Date; use Illuminate\Support\Reflector; use Illuminate\Support\Stringable; use Illuminate\Support\Traits\Macroable; use Illuminate\Support\Traits\ReflectsClosures; use Psr\Http\Client\ClientExceptionInterface; use Symfony\Component\Process\Process; use Throwable; class Event { use Macroable, ManagesFrequencies, ReflectsClosures; /** * The command string. * * @var string|null */ public $command; /** * The cron expression representing the event's frequency. * * @var string */ public $expression = '* * * * *'; /** * How often to repeat the event during a minute. * * @var int|null */ public $repeatSeconds = null; /** * The timezone the date should be evaluated on. * * @var \DateTimeZone|string */ public $timezone; /** * The user the command should run as. * * @var string|null */ public $user; /** * The list of environments the command should run under. * * @var array */ public $environments = []; /** * Indicates if the command should run in maintenance mode. * * @var bool */ public $evenInMaintenanceMode = false; /** * Indicates if the command should not overlap itself. * * @var bool */ public $withoutOverlapping = false; /** * Indicates if the command should only be allowed to run on one server for each cron expression. * * @var bool */ public $onOneServer = false; /** * The number of minutes the mutex should be valid. * * @var int */ public $expiresAt = 1440; /** * Indicates if the command should run in the background. * * @var bool */ public $runInBackground = false; /** * The array of filter callbacks. * * @var array */ protected $filters = []; /** * The array of reject callbacks. * * @var array */ protected $rejects = []; /** * The location that output should be sent to. * * @var string */ public $output = '/dev/null'; /** * Indicates whether output should be appended. * * @var bool */ public $shouldAppendOutput = false; /** * The array of callbacks to be run before the event is started. * * @var array */ protected $beforeCallbacks = []; /** * The array of callbacks to be run after the event is finished. * * @var array */ protected $afterCallbacks = []; /** * The human readable description of the event. * * @var string|null */ public $description; /** * The event mutex implementation. * * @var \Illuminate\Console\Scheduling\EventMutex */ public $mutex; /** * The mutex name resolver callback. * * @var \Closure|null */ public $mutexNameResolver; /** * The last time the event was checked for eligibility to run. * * Utilized by sub-minute repeated events. * * @var \Illuminate\Support\Carbon|null */ protected $lastChecked; /** * The exit status code of the command. * * @var int|null */ public $exitCode; /** * Create a new event instance. * * @param \Illuminate\Console\Scheduling\EventMutex $mutex * @param string $command * @param \DateTimeZone|string|null $timezone * @return void */ public function __construct(EventMutex $mutex, $command, $timezone = null) { $this->mutex = $mutex; $this->command = $command; $this->timezone = $timezone; $this->output = $this->getDefaultOutput(); } /** * Get the default output depending on the OS. * * @return string */ public function getDefaultOutput() { return (DIRECTORY_SEPARATOR === '\\') ? 'NUL' : '/dev/null'; } /** * Run the given event. * * @param \Illuminate\Contracts\Container\Container $container * @return void * * @throws \Throwable */ public function run(Container $container) { if ($this->shouldSkipDueToOverlapping()) { return; } $exitCode = $this->start($container); if (! $this->runInBackground) { $this->finish($container, $exitCode); } } /** * Determine if the event should skip because another process is overlapping. * * @return bool */ public function shouldSkipDueToOverlapping() { return $this->withoutOverlapping && ! $this->mutex->create($this); } /** * Determine if the event has been configured to repeat multiple times per minute. * * @return bool */ public function isRepeatable() { return ! is_null($this->repeatSeconds); } /** * Determine if the event is ready to repeat. * * @return bool */ public function shouldRepeatNow() { return $this->isRepeatable() && $this->lastChecked?->diffInSeconds() >= $this->repeatSeconds; } /** * Run the command process. * * @param \Illuminate\Contracts\Container\Container $container * @return int * * @throws \Throwable */ protected function start($container) { try { $this->callBeforeCallbacks($container); return $this->execute($container); } catch (Throwable $exception) { $this->removeMutex(); throw $exception; } } /** * Run the command process. * * @param \Illuminate\Contracts\Container\Container $container * @return int */ protected function execute($container) { return Process::fromShellCommandline( $this->buildCommand(), base_path(), null, null, null )->run(); } /** * Mark the command process as finished and run callbacks/cleanup. * * @param \Illuminate\Contracts\Container\Container $container * @param int $exitCode * @return void */ public function finish(Container $container, $exitCode) { $this->exitCode = (int) $exitCode; try { $this->callAfterCallbacks($container); } finally { $this->removeMutex(); } } /** * Call all of the "before" callbacks for the event. * * @param \Illuminate\Contracts\Container\Container $container * @return void */ public function callBeforeCallbacks(Container $container) { foreach ($this->beforeCallbacks as $callback) { $container->call($callback); } } /** * Call all of the "after" callbacks for the event. * * @param \Illuminate\Contracts\Container\Container $container * @return void */ public function callAfterCallbacks(Container $container) { foreach ($this->afterCallbacks as $callback) { $container->call($callback); } } /** * Build the command string. * * @return string */ public function buildCommand() { return (new CommandBuilder)->buildCommand($this); } /** * Determine if the given event should run based on the Cron expression. * * @param \Illuminate\Contracts\Foundation\Application $app * @return bool */ public function isDue($app) { if (! $this->runsInMaintenanceMode() && $app->isDownForMaintenance()) { return false; } return $this->expressionPasses() && $this->runsInEnvironment($app->environment()); } /** * Determine if the event runs in maintenance mode. * * @return bool */ public function runsInMaintenanceMode() { return $this->evenInMaintenanceMode; } /** * Determine if the Cron expression passes. * * @return bool */ protected function expressionPasses() { $date = Date::now(); if ($this->timezone) { $date = $date->setTimezone($this->timezone); } return (new CronExpression($this->expression))->isDue($date->toDateTimeString()); } /** * Determine if the event runs in the given environment. * * @param string $environment * @return bool */ public function runsInEnvironment($environment) { return empty($this->environments) || in_array($environment, $this->environments); } /** * Determine if the filters pass for the event. * * @param \Illuminate\Contracts\Foundation\Application $app * @return bool */ public function filtersPass($app) { $this->lastChecked = Date::now(); foreach ($this->filters as $callback) { if (! $app->call($callback)) { return false; } } foreach ($this->rejects as $callback) { if ($app->call($callback)) { return false; } } return true; } /** * Ensure that the output is stored on disk in a log file. * * @return $this */ public function storeOutput() { $this->ensureOutputIsBeingCaptured(); return $this; } /** * Send the output of the command to a given location. * * @param string $location * @param bool $append * @return $this */ public function sendOutputTo($location, $append = false) { $this->output = $location; $this->shouldAppendOutput = $append; return $this; } /** * Append the output of the command to a given location. * * @param string $location * @return $this */ public function appendOutputTo($location) { return $this->sendOutputTo($location, true); } /** * E-mail the results of the scheduled operation. * * @param array|mixed $addresses * @param bool $onlyIfOutputExists * @return $this * * @throws \LogicException */ public function emailOutputTo($addresses, $onlyIfOutputExists = false) { $this->ensureOutputIsBeingCaptured(); $addresses = Arr::wrap($addresses); return $this->then(function (Mailer $mailer) use ($addresses, $onlyIfOutputExists) { $this->emailOutput($mailer, $addresses, $onlyIfOutputExists); }); } /** * E-mail the results of the scheduled operation if it produces output. * * @param array|mixed $addresses * @return $this * * @throws \LogicException */ public function emailWrittenOutputTo($addresses) { return $this->emailOutputTo($addresses, true); } /** * E-mail the results of the scheduled operation if it fails. * * @param array|mixed $addresses * @return $this */ public function emailOutputOnFailure($addresses) { $this->ensureOutputIsBeingCaptured(); $addresses = Arr::wrap($addresses); return $this->onFailure(function (Mailer $mailer) use ($addresses) { $this->emailOutput($mailer, $addresses, false); }); } /** * Ensure that the command output is being captured. * * @return void */ protected function ensureOutputIsBeingCaptured() { if (is_null($this->output) || $this->output == $this->getDefaultOutput()) { $this->sendOutputTo(storage_path('logs/schedule-'.sha1($this->mutexName()).'.log')); } } /** * E-mail the output of the event to the recipients. * * @param \Illuminate\Contracts\Mail\Mailer $mailer * @param array $addresses * @param bool $onlyIfOutputExists * @return void */ protected function emailOutput(Mailer $mailer, $addresses, $onlyIfOutputExists = false) { $text = is_file($this->output) ? file_get_contents($this->output) : ''; if ($onlyIfOutputExists && empty($text)) { return; } $mailer->raw($text, function ($m) use ($addresses) { $m->to($addresses)->subject($this->getEmailSubject()); }); } /** * Get the e-mail subject line for output results. * * @return string */ protected function getEmailSubject() { if ($this->description) { return $this->description; } return "Scheduled Job Output For [{$this->command}]"; } /** * Register a callback to ping a given URL before the job runs. * * @param string $url * @return $this */ public function pingBefore($url) { return $this->before($this->pingCallback($url)); } /** * Register a callback to ping a given URL before the job runs if the given condition is true. * * @param bool $value * @param string $url * @return $this */ public function pingBeforeIf($value, $url) { return $value ? $this->pingBefore($url) : $this; } /** * Register a callback to ping a given URL after the job runs. * * @param string $url * @return $this */ public function thenPing($url) { return $this->then($this->pingCallback($url)); } /** * Register a callback to ping a given URL after the job runs if the given condition is true. * * @param bool $value * @param string $url * @return $this */ public function thenPingIf($value, $url) { return $value ? $this->thenPing($url) : $this; } /** * Register a callback to ping a given URL if the operation succeeds. * * @param string $url * @return $this */ public function pingOnSuccess($url) { return $this->onSuccess($this->pingCallback($url)); } /** * Register a callback to ping a given URL if the operation fails. * * @param string $url * @return $this */ public function pingOnFailure($url) { return $this->onFailure($this->pingCallback($url)); } /** * Get the callback that pings the given URL. * * @param string $url * @return \Closure */ protected function pingCallback($url) { return function (Container $container, HttpClient $http) use ($url) { try { $http->request('GET', $url); } catch (ClientExceptionInterface|TransferException $e) { $container->make(ExceptionHandler::class)->report($e); } }; } /** * State that the command should run in the background. * * @return $this */ public function runInBackground() { $this->runInBackground = true; return $this; } /** * Set which user the command should run as. * * @param string $user * @return $this */ public function user($user) { $this->user = $user; return $this; } /** * Limit the environments the command should run in. * * @param array|mixed $environments * @return $this */ public function environments($environments) { $this->environments = is_array($environments) ? $environments : func_get_args(); return $this; } /** * State that the command should run even in maintenance mode. * * @return $this */ public function evenInMaintenanceMode() { $this->evenInMaintenanceMode = true; return $this; } /** * Do not allow the event to overlap each other. * * The expiration time of the underlying cache lock may be specified in minutes. * * @param int $expiresAt * @return $this */ public function withoutOverlapping($expiresAt = 1440) { $this->withoutOverlapping = true; $this->expiresAt = $expiresAt; return $this->skip(function () { return $this->mutex->exists($this); }); } /** * Allow the event to only run on one server for each cron expression. * * @return $this */ public function onOneServer() { $this->onOneServer = true; return $this; } /** * Register a callback to further filter the schedule. * * @param \Closure|bool $callback * @return $this */ public function when($callback) { $this->filters[] = Reflector::isCallable($callback) ? $callback : function () use ($callback) { return $callback; }; return $this; } /** * Register a callback to further filter the schedule. * * @param \Closure|bool $callback * @return $this */ public function skip($callback) { $this->rejects[] = Reflector::isCallable($callback) ? $callback : function () use ($callback) { return $callback; }; return $this; } /** * Register a callback to be called before the operation. * * @param \Closure $callback * @return $this */ public function before(Closure $callback) { $this->beforeCallbacks[] = $callback; return $this; } /** * Register a callback to be called after the operation. * * @param \Closure $callback * @return $this */ public function after(Closure $callback) { return $this->then($callback); } /** * Register a callback to be called after the operation. * * @param \Closure $callback * @return $this */ public function then(Closure $callback) { $parameters = $this->closureParameterTypes($callback); if (Arr::get($parameters, 'output') === Stringable::class) { return $this->thenWithOutput($callback); } $this->afterCallbacks[] = $callback; return $this; } /** * Register a callback that uses the output after the job runs. * * @param \Closure $callback * @param bool $onlyIfOutputExists * @return $this */ public function thenWithOutput(Closure $callback, $onlyIfOutputExists = false) { $this->ensureOutputIsBeingCaptured(); return $this->then($this->withOutputCallback($callback, $onlyIfOutputExists)); } /** * Register a callback to be called if the operation succeeds. * * @param \Closure $callback * @return $this */ public function onSuccess(Closure $callback) { $parameters = $this->closureParameterTypes($callback); if (Arr::get($parameters, 'output') === Stringable::class) { return $this->onSuccessWithOutput($callback); } return $this->then(function (Container $container) use ($callback) { if ($this->exitCode === 0) { $container->call($callback); } }); } /** * Register a callback that uses the output if the operation succeeds. * * @param \Closure $callback * @param bool $onlyIfOutputExists * @return $this */ public function onSuccessWithOutput(Closure $callback, $onlyIfOutputExists = false) { $this->ensureOutputIsBeingCaptured(); return $this->onSuccess($this->withOutputCallback($callback, $onlyIfOutputExists)); } /** * Register a callback to be called if the operation fails. * * @param \Closure $callback * @return $this */ public function onFailure(Closure $callback) { $parameters = $this->closureParameterTypes($callback); if (Arr::get($parameters, 'output') === Stringable::class) { return $this->onFailureWithOutput($callback); } return $this->then(function (Container $container) use ($callback) { if ($this->exitCode !== 0) { $container->call($callback); } }); } /** * Register a callback that uses the output if the operation fails. * * @param \Closure $callback * @param bool $onlyIfOutputExists * @return $this */ public function onFailureWithOutput(Closure $callback, $onlyIfOutputExists = false) { $this->ensureOutputIsBeingCaptured(); return $this->onFailure($this->withOutputCallback($callback, $onlyIfOutputExists)); } /** * Get a callback that provides output. * * @param \Closure $callback * @param bool $onlyIfOutputExists * @return \Closure */ protected function withOutputCallback(Closure $callback, $onlyIfOutputExists = false) { return function (Container $container) use ($callback, $onlyIfOutputExists) { $output = $this->output && is_file($this->output) ? file_get_contents($this->output) : ''; return $onlyIfOutputExists && empty($output) ? null : $container->call($callback, ['output' => new Stringable($output)]); }; } /** * Set the human-friendly description of the event. * * @param string $description * @return $this */ public function name($description) { return $this->description($description); } /** * Set the human-friendly description of the event. * * @param string $description * @return $this */ public function description($description) { $this->description = $description; return $this; } /** * Get the summary of the event for display. * * @return string */ public function getSummaryForDisplay() { if (is_string($this->description)) { return $this->description; } return $this->buildCommand(); } /** * Determine the next due date for an event. * * @param \DateTimeInterface|string $currentTime * @param int $nth * @param bool $allowCurrentDate * @return \Illuminate\Support\Carbon */ public function nextRunDate($currentTime = 'now', $nth = 0, $allowCurrentDate = false) { return Date::instance((new CronExpression($this->getExpression())) ->getNextRunDate($currentTime, $nth, $allowCurrentDate, $this->timezone)); } /** * Get the Cron expression for the event. * * @return string */ public function getExpression() { return $this->expression; } /** * Set the event mutex implementation to be used. * * @param \Illuminate\Console\Scheduling\EventMutex $mutex * @return $this */ public function preventOverlapsUsing(EventMutex $mutex) { $this->mutex = $mutex; return $this; } /** * Get the mutex name for the scheduled command. * * @return string */ public function mutexName() { $mutexNameResolver = $this->mutexNameResolver; if (! is_null($mutexNameResolver) && is_callable($mutexNameResolver)) { return $mutexNameResolver($this); } return 'framework'.DIRECTORY_SEPARATOR.'schedule-'.sha1($this->expression.$this->command); } /** * Set the mutex name or name resolver callback. * * @param \Closure|string $mutexName * @return $this */ public function createMutexNameUsing(Closure|string $mutexName) { $this->mutexNameResolver = is_string($mutexName) ? fn () => $mutexName : $mutexName; return $this; } /** * Delete the mutex for the event. * * @return void */ protected function removeMutex() { if ($this->withoutOverlapping) { $this->mutex->forget($this); } } } src/Illuminate/Console/Scheduling/ScheduleListCommand.php 0000644 00000022316 15107327037 0017536 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Closure; use Cron\CronExpression; use DateTimeZone; use Illuminate\Console\Application; use Illuminate\Console\Command; use Illuminate\Support\Carbon; use ReflectionClass; use ReflectionFunction; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Terminal; #[AsCommand(name: 'schedule:list')] class ScheduleListCommand extends Command { /** * The console command name. * * @var string */ protected $signature = 'schedule:list {--timezone= : The timezone that times should be displayed in} {--next : Sort the listed tasks by their next due date} '; /** * The console command description. * * @var string */ protected $description = 'List all scheduled tasks'; /** * The terminal width resolver callback. * * @var \Closure|null */ protected static $terminalWidthResolver; /** * Execute the console command. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void * * @throws \Exception */ public function handle(Schedule $schedule) { $events = collect($schedule->events()); if ($events->isEmpty()) { $this->components->info('No scheduled tasks have been defined.'); return; } $terminalWidth = self::getTerminalWidth(); $expressionSpacing = $this->getCronExpressionSpacing($events); $repeatExpressionSpacing = $this->getRepeatExpressionSpacing($events); $timezone = new DateTimeZone($this->option('timezone') ?? config('app.timezone')); $events = $this->sortEvents($events, $timezone); $events = $events->map(function ($event) use ($terminalWidth, $expressionSpacing, $repeatExpressionSpacing, $timezone) { return $this->listEvent($event, $terminalWidth, $expressionSpacing, $repeatExpressionSpacing, $timezone); }); $this->line( $events->flatten()->filter()->prepend('')->push('')->toArray() ); } /** * Get the spacing to be used on each event row. * * @param \Illuminate\Support\Collection $events * @return array<int, int> */ private function getCronExpressionSpacing($events) { $rows = $events->map(fn ($event) => array_map('mb_strlen', preg_split("/\s+/", $event->expression))); return collect($rows[0] ?? [])->keys()->map(fn ($key) => $rows->max($key))->all(); } /** * Get the spacing to be used on each event row. * * @param \Illuminate\Support\Collection $events * @return int */ private function getRepeatExpressionSpacing($events) { return $events->map(fn ($event) => mb_strlen($this->getRepeatExpression($event)))->max(); } /** * List the given even in the console. * * @param \Illuminate\Console\Scheduling\Event * @param int $terminalWidth * @param array $expressionSpacing * @param int $repeatExpressionSpacing * @param array $repeatExpressionSpacing * @param \DateTimeZone $timezone * @return \Illuminate\Support\DateTimeZone */ private function listEvent($event, $terminalWidth, $expressionSpacing, $repeatExpressionSpacing, $timezone) { $expression = $this->formatCronExpression($event->expression, $expressionSpacing); $repeatExpression = str_pad($this->getRepeatExpression($event), $repeatExpressionSpacing); $command = $event->command ?? ''; $description = $event->description ?? ''; if (! $this->output->isVerbose()) { $command = str_replace([Application::phpBinary(), Application::artisanBinary()], [ 'php', preg_replace("#['\"]#", '', Application::artisanBinary()), ], $command); } if ($event instanceof CallbackEvent) { $command = $event->getSummaryForDisplay(); if (in_array($command, ['Closure', 'Callback'])) { $command = 'Closure at: '.$this->getClosureLocation($event); } } $command = mb_strlen($command) > 1 ? "{$command} " : ''; $nextDueDateLabel = 'Next Due:'; $nextDueDate = $this->getNextDueDateForEvent($event, $timezone); $nextDueDate = $this->output->isVerbose() ? $nextDueDate->format('Y-m-d H:i:s P') : $nextDueDate->diffForHumans(); $hasMutex = $event->mutex->exists($event) ? 'Has Mutex › ' : ''; $dots = str_repeat('.', max( $terminalWidth - mb_strlen($expression.$repeatExpression.$command.$nextDueDateLabel.$nextDueDate.$hasMutex) - 8, 0 )); // Highlight the parameters... $command = preg_replace("#(php artisan [\w\-:]+) (.+)#", '$1 <fg=yellow;options=bold>$2</>', $command); return [sprintf( ' <fg=yellow>%s</> <fg=#6C7280>%s</> %s<fg=#6C7280>%s %s%s %s</>', $expression, $repeatExpression, $command, $dots, $hasMutex, $nextDueDateLabel, $nextDueDate ), $this->output->isVerbose() && mb_strlen($description) > 1 ? sprintf( ' <fg=#6C7280>%s%s %s</>', str_repeat(' ', mb_strlen($expression) + 2), '⇁', $description ) : '']; } /** * Get the repeat expression for an event. * * @param \Illuminate\Console\Scheduling\Event $event * @return string */ private function getRepeatExpression($event) { return $event->isRepeatable() ? "{$event->repeatSeconds}s " : ''; } /** * Sort the events by due date if option set. * * @param \Illuminate\Support\Collection $events * @param \DateTimeZone $timezone * @return \Illuminate\Support\Collection */ private function sortEvents(\Illuminate\Support\Collection $events, DateTimeZone $timezone) { return $this->option('next') ? $events->sortBy(fn ($event) => $this->getNextDueDateForEvent($event, $timezone)) : $events; } /** * Get the next due date for an event. * * @param \Illuminate\Console\Scheduling\Event $event * @param \DateTimeZone $timezone * @return \Illuminate\Support\Carbon */ private function getNextDueDateForEvent($event, DateTimeZone $timezone) { $nextDueDate = Carbon::instance( (new CronExpression($event->expression)) ->getNextRunDate(Carbon::now()->setTimezone($event->timezone)) ->setTimezone($timezone) ); if (! $event->isRepeatable()) { return $nextDueDate; } $previousDueDate = Carbon::instance( (new CronExpression($event->expression)) ->getPreviousRunDate(Carbon::now()->setTimezone($event->timezone), allowCurrentDate: true) ->setTimezone($timezone) ); $now = Carbon::now()->setTimezone($event->timezone); if (! $now->copy()->startOfMinute()->eq($previousDueDate)) { return $nextDueDate; } return $now ->endOfSecond() ->ceilSeconds($event->repeatSeconds); } /** * Format the cron expression based on the spacing provided. * * @param string $expression * @param array<int, int> $spacing * @return string */ private function formatCronExpression($expression, $spacing) { $expressions = preg_split("/\s+/", $expression); return collect($spacing) ->map(fn ($length, $index) => str_pad($expressions[$index], $length)) ->implode(' '); } /** * Get the file and line number for the event closure. * * @param \Illuminate\Console\Scheduling\CallbackEvent $event * @return string */ private function getClosureLocation(CallbackEvent $event) { $callback = (new ReflectionClass($event))->getProperty('callback')->getValue($event); if ($callback instanceof Closure) { $function = new ReflectionFunction($callback); return sprintf( '%s:%s', str_replace($this->laravel->basePath().DIRECTORY_SEPARATOR, '', $function->getFileName() ?: ''), $function->getStartLine() ); } if (is_string($callback)) { return $callback; } if (is_array($callback)) { $className = is_string($callback[0]) ? $callback[0] : $callback[0]::class; return sprintf('%s::%s', $className, $callback[1]); } return sprintf('%s::__invoke', $callback::class); } /** * Get the terminal width. * * @return int */ public static function getTerminalWidth() { return is_null(static::$terminalWidthResolver) ? (new Terminal)->getWidth() : call_user_func(static::$terminalWidthResolver); } /** * Set a callback that should be used when resolving the terminal width. * * @param \Closure|null $resolver * @return void */ public static function resolveTerminalWidthUsing($resolver) { static::$terminalWidthResolver = $resolver; } } src/Illuminate/Console/Scheduling/CommandBuilder.php 0000644 00000004275 15107327037 0016540 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Illuminate\Console\Application; use Illuminate\Support\ProcessUtils; class CommandBuilder { /** * Build the command for the given event. * * @param \Illuminate\Console\Scheduling\Event $event * @return string */ public function buildCommand(Event $event) { if ($event->runInBackground) { return $this->buildBackgroundCommand($event); } return $this->buildForegroundCommand($event); } /** * Build the command for running the event in the foreground. * * @param \Illuminate\Console\Scheduling\Event $event * @return string */ protected function buildForegroundCommand(Event $event) { $output = ProcessUtils::escapeArgument($event->output); return $this->ensureCorrectUser( $event, $event->command.($event->shouldAppendOutput ? ' >> ' : ' > ').$output.' 2>&1' ); } /** * Build the command for running the event in the background. * * @param \Illuminate\Console\Scheduling\Event $event * @return string */ protected function buildBackgroundCommand(Event $event) { $output = ProcessUtils::escapeArgument($event->output); $redirect = $event->shouldAppendOutput ? ' >> ' : ' > '; $finished = Application::formatCommandString('schedule:finish').' "'.$event->mutexName().'"'; if (windows_os()) { return 'start /b cmd /v:on /c "('.$event->command.' & '.$finished.' ^!ERRORLEVEL^!)'.$redirect.$output.' 2>&1"'; } return $this->ensureCorrectUser($event, '('.$event->command.$redirect.$output.' 2>&1 ; '.$finished.' "$?") > ' .ProcessUtils::escapeArgument($event->getDefaultOutput()).' 2>&1 &' ); } /** * Finalize the event's command syntax with the correct user. * * @param \Illuminate\Console\Scheduling\Event $event * @param string $command * @return string */ protected function ensureCorrectUser(Event $event, $command) { return $event->user && ! windows_os() ? 'sudo -u '.$event->user.' -- sh -c \''.$command.'\'' : $command; } } src/Illuminate/Console/Scheduling/ScheduleInterruptCommand.php 0000644 00000002406 15107327037 0020615 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Illuminate\Console\Command; use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Support\Facades\Date; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'schedule:interrupt')] class ScheduleInterruptCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'schedule:interrupt'; /** * The console command description. * * @var string */ protected $description = 'Interrupt the current schedule run'; /** * The cache store implementation. * * @var \Illuminate\Contracts\Cache\Repository */ protected $cache; /** * Create a new schedule interrupt command. * * @param \Illuminate\Contracts\Cache\Repository $cache * @return void */ public function __construct(Cache $cache) { parent::__construct(); $this->cache = $cache; } /** * Execute the console command. * * @return void */ public function handle() { $this->cache->put('illuminate:schedule:interrupt', true, Date::now()->endOfMinute()); $this->components->info('Broadcasting schedule interrupt signal.'); } } src/Illuminate/Console/Scheduling/EventMutex.php 0000644 00000001255 15107327037 0015752 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; interface EventMutex { /** * Attempt to obtain an event mutex for the given event. * * @param \Illuminate\Console\Scheduling\Event $event * @return bool */ public function create(Event $event); /** * Determine if an event mutex exists for the given event. * * @param \Illuminate\Console\Scheduling\Event $event * @return bool */ public function exists(Event $event); /** * Clear the event mutex for the given event. * * @param \Illuminate\Console\Scheduling\Event $event * @return void */ public function forget(Event $event); } src/Illuminate/Console/composer.json 0000644 00000003227 15107327037 0013573 0 ustar 00 { "name": "illuminate/console", "description": "The Illuminate Console package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "ext-mbstring": "*", "illuminate/collections": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0", "laravel/prompts": "^0.1.9", "illuminate/support": "^10.0", "illuminate/view": "^10.0", "nunomaduro/termwind": "^1.13", "symfony/console": "^6.2", "symfony/process": "^6.2" }, "autoload": { "psr-4": { "Illuminate\\Console\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "ext-pcntl": "Required to use signal trapping.", "dragonmantank/cron-expression": "Required to use scheduler (^3.3.2).", "guzzlehttp/guzzle": "Required to use the ping methods on schedules (^7.5).", "illuminate/bus": "Required to use the scheduled job dispatcher (^10.0).", "illuminate/container": "Required to use the scheduler (^10.0).", "illuminate/filesystem": "Required to use the generator command (^10.0).", "illuminate/queue": "Required to use closures for scheduled jobs (^10.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Console/Events/ScheduledTaskFinished.php 0000644 00000001215 15107327037 0017216 0 ustar 00 <?php namespace Illuminate\Console\Events; use Illuminate\Console\Scheduling\Event; class ScheduledTaskFinished { /** * The scheduled event that ran. * * @var \Illuminate\Console\Scheduling\Event */ public $task; /** * The runtime of the scheduled event. * * @var float */ public $runtime; /** * Create a new event instance. * * @param \Illuminate\Console\Scheduling\Event $task * @param float $runtime * @return void */ public function __construct(Event $task, $runtime) { $this->task = $task; $this->runtime = $runtime; } } src/Illuminate/Console/Events/ScheduledTaskSkipped.php 0000644 00000000727 15107327037 0017073 0 ustar 00 <?php namespace Illuminate\Console\Events; use Illuminate\Console\Scheduling\Event; class ScheduledTaskSkipped { /** * The scheduled event being run. * * @var \Illuminate\Console\Scheduling\Event */ public $task; /** * Create a new event instance. * * @param \Illuminate\Console\Scheduling\Event $task * @return void */ public function __construct(Event $task) { $this->task = $task; } } src/Illuminate/Console/Events/ScheduledBackgroundTaskFinished.php 0000644 00000000741 15107327037 0021221 0 ustar 00 <?php namespace Illuminate\Console\Events; use Illuminate\Console\Scheduling\Event; class ScheduledBackgroundTaskFinished { /** * The scheduled event that ran. * * @var \Illuminate\Console\Scheduling\Event */ public $task; /** * Create a new event instance. * * @param \Illuminate\Console\Scheduling\Event $task * @return void */ public function __construct(Event $task) { $this->task = $task; } } src/Illuminate/Console/Events/ArtisanStarting.php 0000644 00000000652 15107327037 0016142 0 ustar 00 <?php namespace Illuminate\Console\Events; class ArtisanStarting { /** * The Artisan application instance. * * @var \Illuminate\Console\Application */ public $artisan; /** * Create a new event instance. * * @param \Illuminate\Console\Application $artisan * @return void */ public function __construct($artisan) { $this->artisan = $artisan; } } src/Illuminate/Console/Events/CommandStarting.php 0000644 00000002002 15107327037 0016106 0 ustar 00 <?php namespace Illuminate\Console\Events; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class CommandStarting { /** * The command name. * * @var string */ public $command; /** * The console input implementation. * * @var \Symfony\Component\Console\Input\InputInterface|null */ public $input; /** * The command output implementation. * * @var \Symfony\Component\Console\Output\OutputInterface|null */ public $output; /** * Create a new event instance. * * @param string $command * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ public function __construct($command, InputInterface $input, OutputInterface $output) { $this->input = $input; $this->output = $output; $this->command = $command; } } src/Illuminate/Console/Events/ScheduledTaskFailed.php 0000644 00000001266 15107327037 0016657 0 ustar 00 <?php namespace Illuminate\Console\Events; use Illuminate\Console\Scheduling\Event; use Throwable; class ScheduledTaskFailed { /** * The scheduled event that failed. * * @var \Illuminate\Console\Scheduling\Event */ public $task; /** * The exception that was thrown. * * @var \Throwable */ public $exception; /** * Create a new event instance. * * @param \Illuminate\Console\Scheduling\Event $task * @param \Throwable $exception * @return void */ public function __construct(Event $task, Throwable $exception) { $this->task = $task; $this->exception = $exception; } } src/Illuminate/Console/Events/ScheduledTaskStarting.php 0000644 00000000730 15107327037 0017261 0 ustar 00 <?php namespace Illuminate\Console\Events; use Illuminate\Console\Scheduling\Event; class ScheduledTaskStarting { /** * The scheduled event being run. * * @var \Illuminate\Console\Scheduling\Event */ public $task; /** * Create a new event instance. * * @param \Illuminate\Console\Scheduling\Event $task * @return void */ public function __construct(Event $task) { $this->task = $task; } } src/Illuminate/Console/Events/CommandFinished.php 0000644 00000002254 15107327037 0016055 0 ustar 00 <?php namespace Illuminate\Console\Events; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class CommandFinished { /** * The command name. * * @var string */ public $command; /** * The console input implementation. * * @var \Symfony\Component\Console\Input\InputInterface|null */ public $input; /** * The command output implementation. * * @var \Symfony\Component\Console\Output\OutputInterface|null */ public $output; /** * The command exit code. * * @var int */ public $exitCode; /** * Create a new event instance. * * @param string $command * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @param int $exitCode * @return void */ public function __construct($command, InputInterface $input, OutputInterface $output, $exitCode) { $this->input = $input; $this->output = $output; $this->command = $command; $this->exitCode = $exitCode; } } src/Illuminate/Console/QuestionHelper.php 0000644 00000005007 15107327037 0014527 0 ustar 00 <?php namespace Illuminate\Console; use Illuminate\Console\View\Components\TwoColumnDetail; use Symfony\Component\Console\Formatter\OutputFormatter; use Symfony\Component\Console\Helper\SymfonyQuestionHelper; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ChoiceQuestion; use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Question\Question; class QuestionHelper extends SymfonyQuestionHelper { /** * {@inheritdoc} * * @return void */ protected function writePrompt(OutputInterface $output, Question $question) { $text = OutputFormatter::escapeTrailingBackslash($question->getQuestion()); $text = $this->ensureEndsWithPunctuation($text); $text = " <fg=default;options=bold>$text</></>"; $default = $question->getDefault(); if ($question->isMultiline()) { $text .= sprintf(' (press %s to continue)', 'Windows' == PHP_OS_FAMILY ? '<comment>Ctrl+Z</comment> then <comment>Enter</comment>' : '<comment>Ctrl+D</comment>'); } switch (true) { case null === $default: $text = sprintf('<info>%s</info>', $text); break; case $question instanceof ConfirmationQuestion: $text = sprintf('<info>%s (yes/no)</info> [<comment>%s</comment>]', $text, $default ? 'yes' : 'no'); break; case $question instanceof ChoiceQuestion: $choices = $question->getChoices(); $text = sprintf('<info>%s</info> [<comment>%s</comment>]', $text, OutputFormatter::escape($choices[$default] ?? $default)); break; default: $text = sprintf('<info>%s</info> [<comment>%s</comment>]', $text, OutputFormatter::escape($default)); break; } $output->writeln($text); if ($question instanceof ChoiceQuestion) { foreach ($question->getChoices() as $key => $value) { with(new TwoColumnDetail($output))->render($value, $key); } } $output->write('<options=bold>❯ </>'); } /** * Ensures the given string ends with punctuation. * * @param string $string * @return string */ protected function ensureEndsWithPunctuation($string) { if (! str($string)->endsWith(['?', ':', '!', '.'])) { return "$string:"; } return $string; } } src/Illuminate/Console/Parser.php 0000644 00000011157 15107327037 0013017 0 ustar 00 <?php namespace Illuminate\Console; use InvalidArgumentException; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; class Parser { /** * Parse the given console command definition into an array. * * @param string $expression * @return array * * @throws \InvalidArgumentException */ public static function parse(string $expression) { $name = static::name($expression); if (preg_match_all('/\{\s*(.*?)\s*\}/', $expression, $matches) && count($matches[1])) { return array_merge([$name], static::parameters($matches[1])); } return [$name, [], []]; } /** * Extract the name of the command from the expression. * * @param string $expression * @return string * * @throws \InvalidArgumentException */ protected static function name(string $expression) { if (! preg_match('/[^\s]+/', $expression, $matches)) { throw new InvalidArgumentException('Unable to determine command name from signature.'); } return $matches[0]; } /** * Extract all parameters from the tokens. * * @param array $tokens * @return array */ protected static function parameters(array $tokens) { $arguments = []; $options = []; foreach ($tokens as $token) { if (preg_match('/^-{2,}(.*)/', $token, $matches)) { $options[] = static::parseOption($matches[1]); } else { $arguments[] = static::parseArgument($token); } } return [$arguments, $options]; } /** * Parse an argument expression. * * @param string $token * @return \Symfony\Component\Console\Input\InputArgument */ protected static function parseArgument(string $token) { [$token, $description] = static::extractDescription($token); switch (true) { case str_ends_with($token, '?*'): return new InputArgument(trim($token, '?*'), InputArgument::IS_ARRAY, $description); case str_ends_with($token, '*'): return new InputArgument(trim($token, '*'), InputArgument::IS_ARRAY | InputArgument::REQUIRED, $description); case str_ends_with($token, '?'): return new InputArgument(trim($token, '?'), InputArgument::OPTIONAL, $description); case preg_match('/(.+)\=\*(.+)/', $token, $matches): return new InputArgument($matches[1], InputArgument::IS_ARRAY, $description, preg_split('/,\s?/', $matches[2])); case preg_match('/(.+)\=(.+)/', $token, $matches): return new InputArgument($matches[1], InputArgument::OPTIONAL, $description, $matches[2]); default: return new InputArgument($token, InputArgument::REQUIRED, $description); } } /** * Parse an option expression. * * @param string $token * @return \Symfony\Component\Console\Input\InputOption */ protected static function parseOption(string $token) { [$token, $description] = static::extractDescription($token); $matches = preg_split('/\s*\|\s*/', $token, 2); $shortcut = null; if (isset($matches[1])) { $shortcut = $matches[0]; $token = $matches[1]; } switch (true) { case str_ends_with($token, '='): return new InputOption(trim($token, '='), $shortcut, InputOption::VALUE_OPTIONAL, $description); case str_ends_with($token, '=*'): return new InputOption(trim($token, '=*'), $shortcut, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $description); case preg_match('/(.+)\=\*(.+)/', $token, $matches): return new InputOption($matches[1], $shortcut, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $description, preg_split('/,\s?/', $matches[2])); case preg_match('/(.+)\=(.+)/', $token, $matches): return new InputOption($matches[1], $shortcut, InputOption::VALUE_OPTIONAL, $description, $matches[2]); default: return new InputOption($token, $shortcut, InputOption::VALUE_NONE, $description); } } /** * Parse the token into its token and description segments. * * @param string $token * @return array */ protected static function extractDescription(string $token) { $parts = preg_split('/\s+:\s+/', trim($token), 2); return count($parts) === 2 ? $parts : [$token, '']; } } src/Illuminate/Console/GeneratorCommand.php 0000644 00000032621 15107327037 0015007 0 ustar 00 <?php namespace Illuminate\Console; use Illuminate\Console\Concerns\CreatesMatchingTest; use Illuminate\Contracts\Console\PromptsForMissingInput; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Str; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Finder\Finder; abstract class GeneratorCommand extends Command implements PromptsForMissingInput { /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * The type of class being generated. * * @var string */ protected $type; /** * Reserved names that cannot be used for generation. * * @var string[] */ protected $reservedNames = [ '__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'enum', 'eval', 'exit', 'extends', 'false', 'final', 'finally', 'fn', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'match', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'readonly', 'require', 'require_once', 'return', 'self', 'static', 'switch', 'throw', 'trait', 'true', 'try', 'unset', 'use', 'var', 'while', 'xor', 'yield', '__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__', ]; /** * Create a new controller creator command instance. * * @param \Illuminate\Filesystem\Filesystem $files * @return void */ public function __construct(Filesystem $files) { parent::__construct(); if (in_array(CreatesMatchingTest::class, class_uses_recursive($this))) { $this->addTestOptions(); } $this->files = $files; } /** * Get the stub file for the generator. * * @return string */ abstract protected function getStub(); /** * Execute the console command. * * @return bool|null * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function handle() { // First we need to ensure that the given name is not a reserved word within the PHP // language and that the class name will actually be valid. If it is not valid we // can error now and prevent from polluting the filesystem using invalid files. if ($this->isReservedName($this->getNameInput())) { $this->components->error('The name "'.$this->getNameInput().'" is reserved by PHP.'); return false; } $name = $this->qualifyClass($this->getNameInput()); $path = $this->getPath($name); // Next, We will check to see if the class already exists. If it does, we don't want // to create the class and overwrite the user's code. So, we will bail out so the // code is untouched. Otherwise, we will continue generating this class' files. if ((! $this->hasOption('force') || ! $this->option('force')) && $this->alreadyExists($this->getNameInput())) { $this->components->error($this->type.' already exists.'); return false; } // Next, we will generate the path to the location where this class' file should get // written. Then, we will build the class and make the proper replacements on the // stub files so that it gets the correctly formatted namespace and class name. $this->makeDirectory($path); $this->files->put($path, $this->sortImports($this->buildClass($name))); $info = $this->type; if (in_array(CreatesMatchingTest::class, class_uses_recursive($this))) { if ($this->handleTestCreation($path)) { $info .= ' and test'; } } $this->components->info(sprintf('%s [%s] created successfully.', $info, $path)); } /** * Parse the class name and format according to the root namespace. * * @param string $name * @return string */ protected function qualifyClass($name) { $name = ltrim($name, '\\/'); $name = str_replace('/', '\\', $name); $rootNamespace = $this->rootNamespace(); if (Str::startsWith($name, $rootNamespace)) { return $name; } return $this->qualifyClass( $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name ); } /** * Qualify the given model class base name. * * @param string $model * @return string */ protected function qualifyModel(string $model) { $model = ltrim($model, '\\/'); $model = str_replace('/', '\\', $model); $rootNamespace = $this->rootNamespace(); if (Str::startsWith($model, $rootNamespace)) { return $model; } return is_dir(app_path('Models')) ? $rootNamespace.'Models\\'.$model : $rootNamespace.$model; } /** * Get a list of possible model names. * * @return array<int, string> */ protected function possibleModels() { $modelPath = is_dir(app_path('Models')) ? app_path('Models') : app_path(); return collect((new Finder)->files()->depth(0)->in($modelPath)) ->map(fn ($file) => $file->getBasename('.php')) ->sort() ->values() ->all(); } /** * Get a list of possible event names. * * @return array<int, string> */ protected function possibleEvents() { $eventPath = app_path('Events'); if (! is_dir($eventPath)) { return []; } return collect((new Finder)->files()->depth(0)->in($eventPath)) ->map(fn ($file) => $file->getBasename('.php')) ->sort() ->values() ->all(); } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace; } /** * Determine if the class already exists. * * @param string $rawName * @return bool */ protected function alreadyExists($rawName) { return $this->files->exists($this->getPath($this->qualifyClass($rawName))); } /** * Get the destination class path. * * @param string $name * @return string */ protected function getPath($name) { $name = Str::replaceFirst($this->rootNamespace(), '', $name); return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php'; } /** * Build the directory for the class if necessary. * * @param string $path * @return string */ protected function makeDirectory($path) { if (! $this->files->isDirectory(dirname($path))) { $this->files->makeDirectory(dirname($path), 0777, true, true); } return $path; } /** * Build the class with the given name. * * @param string $name * @return string * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ protected function buildClass($name) { $stub = $this->files->get($this->getStub()); return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); } /** * Replace the namespace for the given stub. * * @param string $stub * @param string $name * @return $this */ protected function replaceNamespace(&$stub, $name) { $searches = [ ['DummyNamespace', 'DummyRootNamespace', 'NamespacedDummyUserModel'], ['{{ namespace }}', '{{ rootNamespace }}', '{{ namespacedUserModel }}'], ['{{namespace}}', '{{rootNamespace}}', '{{namespacedUserModel}}'], ]; foreach ($searches as $search) { $stub = str_replace( $search, [$this->getNamespace($name), $this->rootNamespace(), $this->userProviderModel()], $stub ); } return $this; } /** * Get the full namespace for a given class, without the class name. * * @param string $name * @return string */ protected function getNamespace($name) { return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\'); } /** * Replace the class name for the given stub. * * @param string $stub * @param string $name * @return string */ protected function replaceClass($stub, $name) { $class = str_replace($this->getNamespace($name).'\\', '', $name); return str_replace(['DummyClass', '{{ class }}', '{{class}}'], $class, $stub); } /** * Alphabetically sorts the imports for the given stub. * * @param string $stub * @return string */ protected function sortImports($stub) { if (preg_match('/(?P<imports>(?:^use [^;{]+;$\n?)+)/m', $stub, $match)) { $imports = explode("\n", trim($match['imports'])); sort($imports); return str_replace(trim($match['imports']), implode("\n", $imports), $stub); } return $stub; } /** * Get the desired class name from the input. * * @return string */ protected function getNameInput() { return trim($this->argument('name')); } /** * Get the root namespace for the class. * * @return string */ protected function rootNamespace() { return $this->laravel->getNamespace(); } /** * Get the model for the default guard's user provider. * * @return string|null */ protected function userProviderModel() { $config = $this->laravel['config']; $provider = $config->get('auth.guards.'.$config->get('auth.defaults.guard').'.provider'); return $config->get("auth.providers.{$provider}.model"); } /** * Checks whether the given name is reserved. * * @param string $name * @return bool */ protected function isReservedName($name) { return in_array( strtolower($name), collect($this->reservedNames) ->transform(fn ($name) => strtolower($name)) ->all() ); } /** * Get the first view directory path from the application configuration. * * @param string $path * @return string */ protected function viewPath($path = '') { $views = $this->laravel['config']['view.paths'][0] ?? resource_path('views'); return $views.($path ? DIRECTORY_SEPARATOR.$path : $path); } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return [ ['name', InputArgument::REQUIRED, 'The name of the '.strtolower($this->type)], ]; } /** * Prompt for missing input arguments using the returned questions. * * @return array */ protected function promptForMissingArgumentsUsing() { return [ 'name' => [ 'What should the '.strtolower($this->type).' be named?', match ($this->type) { 'Cast' => 'E.g. Json', 'Channel' => 'E.g. OrderChannel', 'Console command' => 'E.g. SendEmails', 'Component' => 'E.g. Alert', 'Controller' => 'E.g. UserController', 'Event' => 'E.g. PodcastProcessed', 'Exception' => 'E.g. InvalidOrderException', 'Factory' => 'E.g. PostFactory', 'Job' => 'E.g. ProcessPodcast', 'Listener' => 'E.g. SendPodcastNotification', 'Mailable' => 'E.g. OrderShipped', 'Middleware' => 'E.g. EnsureTokenIsValid', 'Model' => 'E.g. Flight', 'Notification' => 'E.g. InvoicePaid', 'Observer' => 'E.g. UserObserver', 'Policy' => 'E.g. PostPolicy', 'Provider' => 'E.g. ElasticServiceProvider', 'Request' => 'E.g. StorePodcastRequest', 'Resource' => 'E.g. UserResource', 'Rule' => 'E.g. Uppercase', 'Scope' => 'E.g. TrendingScope', 'Seeder' => 'E.g. UserSeeder', 'Test' => 'E.g. UserTest', default => '', }, ], ]; } } src/Illuminate/Console/BufferedConsoleOutput.php 0000644 00000001405 15107327037 0016044 0 ustar 00 <?php namespace Illuminate\Console; use Symfony\Component\Console\Output\ConsoleOutput; class BufferedConsoleOutput extends ConsoleOutput { /** * The current buffer. * * @var string */ protected $buffer = ''; /** * Empties the buffer and returns its content. * * @return string */ public function fetch() { return tap($this->buffer, function () { $this->buffer = ''; }); } /** * {@inheritdoc} * * @return void */ protected function doWrite(string $message, bool $newline) { $this->buffer .= $message; if ($newline) { $this->buffer .= \PHP_EOL; } return parent::doWrite($message, $newline); } } src/Illuminate/Console/ConfirmableTrait.php 0000644 00000002443 15107327037 0015006 0 ustar 00 <?php namespace Illuminate\Console; use function Laravel\Prompts\confirm; trait ConfirmableTrait { /** * Confirm before proceeding with the action. * * This method only asks for confirmation in production. * * @param string $warning * @param \Closure|bool|null $callback * @return bool */ public function confirmToProceed($warning = 'Application In Production', $callback = null) { $callback = is_null($callback) ? $this->getDefaultConfirmCallback() : $callback; $shouldConfirm = value($callback); if ($shouldConfirm) { if ($this->hasOption('force') && $this->option('force')) { return true; } $this->components->alert($warning); $confirmed = confirm('Are you sure you want to run this command?', default: false); if (! $confirmed) { $this->components->warn('Command cancelled.'); return false; } } return true; } /** * Get the default confirmation callback. * * @return \Closure */ protected function getDefaultConfirmCallback() { return function () { return $this->getLaravel()->environment() === 'production'; }; } } src/Illuminate/Console/LICENSE.md 0000644 00000002063 15107327037 0012452 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Console/Command.php 0000644 00000017556 15107327037 0013152 0 ustar 00 <?php namespace Illuminate\Console; use Illuminate\Console\View\Components\Factory; use Illuminate\Contracts\Console\Isolatable; use Illuminate\Support\Traits\Macroable; use Symfony\Component\Console\Command\Command as SymfonyCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class Command extends SymfonyCommand { use Concerns\CallsCommands, Concerns\ConfiguresPrompts, Concerns\HasParameters, Concerns\InteractsWithIO, Concerns\InteractsWithSignals, Concerns\PromptsForMissingInput, Macroable; /** * The Laravel application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $laravel; /** * The name and signature of the console command. * * @var string */ protected $signature; /** * The console command name. * * @var string */ protected $name; /** * The console command description. * * @var string|null */ protected $description; /** * The console command help text. * * @var string */ protected $help; /** * Indicates whether the command should be shown in the Artisan command list. * * @var bool */ protected $hidden = false; /** * Indicates whether only one instance of the command can run at any given time. * * @var bool */ protected $isolated = false; /** * The default exit code for isolated commands. * * @var int */ protected $isolatedExitCode = self::SUCCESS; /** * The console command name aliases. * * @var array */ protected $aliases; /** * Create a new console command instance. * * @return void */ public function __construct() { // We will go ahead and set the name, description, and parameters on console // commands just to make things a little easier on the developer. This is // so they don't have to all be manually specified in the constructors. if (isset($this->signature)) { $this->configureUsingFluentDefinition(); } else { parent::__construct($this->name); } // Once we have constructed the command, we'll set the description and other // related properties of the command. If a signature wasn't used to build // the command we'll set the arguments and the options on this command. if (! isset($this->description)) { $this->setDescription((string) static::getDefaultDescription()); } else { $this->setDescription((string) $this->description); } $this->setHelp((string) $this->help); $this->setHidden($this->isHidden()); if (isset($this->aliases)) { $this->setAliases((array) $this->aliases); } if (! isset($this->signature)) { $this->specifyParameters(); } if ($this instanceof Isolatable) { $this->configureIsolation(); } } /** * Configure the console command using a fluent definition. * * @return void */ protected function configureUsingFluentDefinition() { [$name, $arguments, $options] = Parser::parse($this->signature); parent::__construct($this->name = $name); // After parsing the signature we will spin through the arguments and options // and set them on this command. These will already be changed into proper // instances of these "InputArgument" and "InputOption" Symfony classes. $this->getDefinition()->addArguments($arguments); $this->getDefinition()->addOptions($options); } /** * Configure the console command for isolation. * * @return void */ protected function configureIsolation() { $this->getDefinition()->addOption(new InputOption( 'isolated', null, InputOption::VALUE_OPTIONAL, 'Do not run the command if another instance of the command is already running', $this->isolated )); } /** * Run the console command. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return int */ public function run(InputInterface $input, OutputInterface $output): int { $this->output = $output instanceof OutputStyle ? $output : $this->laravel->make( OutputStyle::class, ['input' => $input, 'output' => $output] ); $this->components = $this->laravel->make(Factory::class, ['output' => $this->output]); $this->configurePrompts($input); try { return parent::run( $this->input = $input, $this->output ); } finally { $this->untrap(); } } /** * Execute the console command. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { if ($this instanceof Isolatable && $this->option('isolated') !== false && ! $this->commandIsolationMutex()->create($this)) { $this->comment(sprintf( 'The [%s] command is already running.', $this->getName() )); return (int) (is_numeric($this->option('isolated')) ? $this->option('isolated') : $this->isolatedExitCode); } $method = method_exists($this, 'handle') ? 'handle' : '__invoke'; try { return (int) $this->laravel->call([$this, $method]); } finally { if ($this instanceof Isolatable && $this->option('isolated') !== false) { $this->commandIsolationMutex()->forget($this); } } } /** * Get a command isolation mutex instance for the command. * * @return \Illuminate\Console\CommandMutex */ protected function commandIsolationMutex() { return $this->laravel->bound(CommandMutex::class) ? $this->laravel->make(CommandMutex::class) : $this->laravel->make(CacheCommandMutex::class); } /** * Resolve the console command instance for the given command. * * @param \Symfony\Component\Console\Command\Command|string $command * @return \Symfony\Component\Console\Command\Command */ protected function resolveCommand($command) { if (! class_exists($command)) { return $this->getApplication()->find($command); } $command = $this->laravel->make($command); if ($command instanceof SymfonyCommand) { $command->setApplication($this->getApplication()); } if ($command instanceof self) { $command->setLaravel($this->getLaravel()); } return $command; } /** * {@inheritdoc} * * @return bool */ public function isHidden(): bool { return $this->hidden; } /** * {@inheritdoc} */ public function setHidden(bool $hidden = true): static { parent::setHidden($this->hidden = $hidden); return $this; } /** * Get the Laravel application instance. * * @return \Illuminate\Contracts\Foundation\Application */ public function getLaravel() { return $this->laravel; } /** * Set the Laravel application instance. * * @param \Illuminate\Contracts\Container\Container $laravel * @return void */ public function setLaravel($laravel) { $this->laravel = $laravel; } } src/Illuminate/Console/MigrationGeneratorCommand.php 0000644 00000005373 15107327037 0016665 0 ustar 00 <?php namespace Illuminate\Console; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Composer; abstract class MigrationGeneratorCommand extends Command { /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * The Composer instance. * * @var \Illuminate\Support\Composer * * @deprecated Will be removed in a future Laravel version. */ protected $composer; /** * Create a new migration generator command instance. * * @param \Illuminate\Filesystem\Filesystem $files * @param \Illuminate\Support\Composer $composer * @return void */ public function __construct(Filesystem $files, Composer $composer) { parent::__construct(); $this->files = $files; $this->composer = $composer; } /** * Get the migration table name. * * @return string */ abstract protected function migrationTableName(); /** * Get the path to the migration stub file. * * @return string */ abstract protected function migrationStubFile(); /** * Execute the console command. * * @return int */ public function handle() { $table = $this->migrationTableName(); if ($this->migrationExists($table)) { $this->components->error('Migration already exists.'); return 1; } $this->replaceMigrationPlaceholders( $this->createBaseMigration($table), $table ); $this->components->info('Migration created successfully.'); return 0; } /** * Create a base migration file for the table. * * @param string $table * @return string */ protected function createBaseMigration($table) { return $this->laravel['migration.creator']->create( 'create_'.$table.'_table', $this->laravel->databasePath('/migrations') ); } /** * Replace the placeholders in the generated migration file. * * @param string $path * @param string $table * @return void */ protected function replaceMigrationPlaceholders($path, $table) { $stub = str_replace( '{{table}}', $table, $this->files->get($this->migrationStubFile()) ); $this->files->put($path, $stub); } /** * Determine whether a migration for the table already exists. * * @param string $table * @return bool */ protected function migrationExists($table) { return count($this->files->glob( $this->laravel->joinPaths($this->laravel->databasePath('migrations'), '*_*_*_*_create_'.$table.'_table.php') )) !== 0; } } src/Illuminate/Console/Signals.php 0000644 00000007034 15107327037 0013162 0 ustar 00 <?php namespace Illuminate\Console; /** * @internal */ class Signals { /** * The signal registry instance. * * @var \Symfony\Component\Console\SignalRegistry\SignalRegistry */ protected $registry; /** * The signal registry's previous list of handlers. * * @var array<int, array<int, callable>>|null */ protected $previousHandlers; /** * The current availability resolver, if any. * * @var (callable(): bool)|null */ protected static $availabilityResolver; /** * Create a new signal registrar instance. * * @param \Symfony\Component\Console\SignalRegistry\SignalRegistry $registry * @return void */ public function __construct($registry) { $this->registry = $registry; $this->previousHandlers = $this->getHandlers(); } /** * Register a new signal handler. * * @param int $signal * @param callable(int $signal): void $callback * @return void */ public function register($signal, $callback) { $this->previousHandlers[$signal] ??= $this->initializeSignal($signal); with($this->getHandlers(), function ($handlers) use ($signal) { $handlers[$signal] ??= $this->initializeSignal($signal); $this->setHandlers($handlers); }); $this->registry->register($signal, $callback); with($this->getHandlers(), function ($handlers) use ($signal) { $lastHandlerInserted = array_pop($handlers[$signal]); array_unshift($handlers[$signal], $lastHandlerInserted); $this->setHandlers($handlers); }); } /** * Gets the signal's existing handler in array format. * * @return array<int, callable(int $signal): void> */ protected function initializeSignal($signal) { return is_callable($existingHandler = pcntl_signal_get_handler($signal)) ? [$existingHandler] : null; } /** * Unregister the current signal handlers. * * @return void */ public function unregister() { $previousHandlers = $this->previousHandlers; foreach ($previousHandlers as $signal => $handler) { if (is_null($handler)) { pcntl_signal($signal, SIG_DFL); unset($previousHandlers[$signal]); } } $this->setHandlers($previousHandlers); } /** * Execute the given callback if "signals" should be used and are available. * * @param callable $callback * @return void */ public static function whenAvailable($callback) { $resolver = static::$availabilityResolver; if ($resolver()) { $callback(); } } /** * Get the registry's handlers. * * @return array<int, array<int, callable>> */ protected function getHandlers() { return (fn () => $this->signalHandlers) ->call($this->registry); } /** * Set the registry's handlers. * * @param array<int, array<int, callable(int $signal):void>> $handlers * @return void */ protected function setHandlers($handlers) { (fn () => $this->signalHandlers = $handlers) ->call($this->registry); } /** * Set the availability resolver. * * @param callable(): bool * @return void */ public static function resolveAvailabilityUsing($resolver) { static::$availabilityResolver = $resolver; } } src/Illuminate/Console/Application.php 0000644 00000020406 15107327037 0014023 0 ustar 00 <?php namespace Illuminate\Console; use Closure; use Illuminate\Console\Events\ArtisanStarting; use Illuminate\Contracts\Console\Application as ApplicationContract; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\ProcessUtils; use Symfony\Component\Console\Application as SymfonyApplication; use Symfony\Component\Console\Command\Command as SymfonyCommand; use Symfony\Component\Console\Exception\CommandNotFoundException; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\StringInput; use Symfony\Component\Console\Output\BufferedOutput; use Symfony\Component\Process\PhpExecutableFinder; class Application extends SymfonyApplication implements ApplicationContract { /** * The Laravel application instance. * * @var \Illuminate\Contracts\Container\Container */ protected $laravel; /** * The event dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * The output from the previous command. * * @var \Symfony\Component\Console\Output\BufferedOutput */ protected $lastOutput; /** * The console application bootstrappers. * * @var array */ protected static $bootstrappers = []; /** * A map of command names to classes. * * @var array */ protected $commandMap = []; /** * Create a new Artisan console application. * * @param \Illuminate\Contracts\Container\Container $laravel * @param \Illuminate\Contracts\Events\Dispatcher $events * @param string $version * @return void */ public function __construct(Container $laravel, Dispatcher $events, $version) { parent::__construct('Laravel Framework', $version); $this->laravel = $laravel; $this->events = $events; $this->setAutoExit(false); $this->setCatchExceptions(false); $this->events->dispatch(new ArtisanStarting($this)); $this->bootstrap(); } /** * Determine the proper PHP executable. * * @return string */ public static function phpBinary() { return ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false)); } /** * Determine the proper Artisan executable. * * @return string */ public static function artisanBinary() { return ProcessUtils::escapeArgument(defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan'); } /** * Format the given command as a fully-qualified executable command. * * @param string $string * @return string */ public static function formatCommandString($string) { return sprintf('%s %s %s', static::phpBinary(), static::artisanBinary(), $string); } /** * Register a console "starting" bootstrapper. * * @param \Closure $callback * @return void */ public static function starting(Closure $callback) { static::$bootstrappers[] = $callback; } /** * Bootstrap the console application. * * @return void */ protected function bootstrap() { foreach (static::$bootstrappers as $bootstrapper) { $bootstrapper($this); } } /** * Clear the console application bootstrappers. * * @return void */ public static function forgetBootstrappers() { static::$bootstrappers = []; } /** * Run an Artisan console command by name. * * @param string $command * @param array $parameters * @param \Symfony\Component\Console\Output\OutputInterface|null $outputBuffer * @return int * * @throws \Symfony\Component\Console\Exception\CommandNotFoundException */ public function call($command, array $parameters = [], $outputBuffer = null) { [$command, $input] = $this->parseCommand($command, $parameters); if (! $this->has($command)) { throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $command)); } return $this->run( $input, $this->lastOutput = $outputBuffer ?: new BufferedOutput ); } /** * Parse the incoming Artisan command and its input. * * @param string $command * @param array $parameters * @return array */ protected function parseCommand($command, $parameters) { if (is_subclass_of($command, SymfonyCommand::class)) { $callingClass = true; $command = $this->laravel->make($command)->getName(); } if (! isset($callingClass) && empty($parameters)) { $command = $this->getCommandName($input = new StringInput($command)); } else { array_unshift($parameters, $command); $input = new ArrayInput($parameters); } return [$command, $input]; } /** * Get the output for the last run command. * * @return string */ public function output() { return $this->lastOutput && method_exists($this->lastOutput, 'fetch') ? $this->lastOutput->fetch() : ''; } /** * Add a command to the console. * * @param \Symfony\Component\Console\Command\Command $command * @return \Symfony\Component\Console\Command\Command */ public function add(SymfonyCommand $command) { if ($command instanceof Command) { $command->setLaravel($this->laravel); } return $this->addToParent($command); } /** * Add the command to the parent instance. * * @param \Symfony\Component\Console\Command\Command $command * @return \Symfony\Component\Console\Command\Command */ protected function addToParent(SymfonyCommand $command) { return parent::add($command); } /** * Add a command, resolving through the application. * * @param \Illuminate\Console\Command|string $command * @return \Symfony\Component\Console\Command\Command|null */ public function resolve($command) { if (is_subclass_of($command, SymfonyCommand::class) && ($commandName = $command::getDefaultName())) { $this->commandMap[$commandName] = $command; return null; } if ($command instanceof Command) { return $this->add($command); } return $this->add($this->laravel->make($command)); } /** * Resolve an array of commands through the application. * * @param array|mixed $commands * @return $this */ public function resolveCommands($commands) { $commands = is_array($commands) ? $commands : func_get_args(); foreach ($commands as $command) { $this->resolve($command); } return $this; } /** * Set the container command loader for lazy resolution. * * @return $this */ public function setContainerCommandLoader() { $this->setCommandLoader(new ContainerCommandLoader($this->laravel, $this->commandMap)); return $this; } /** * Get the default input definition for the application. * * This is used to add the --env option to every available command. * * @return \Symfony\Component\Console\Input\InputDefinition */ protected function getDefaultInputDefinition(): InputDefinition { return tap(parent::getDefaultInputDefinition(), function ($definition) { $definition->addOption($this->getEnvironmentOption()); }); } /** * Get the global environment option for the definition. * * @return \Symfony\Component\Console\Input\InputOption */ protected function getEnvironmentOption() { $message = 'The environment the command should run under'; return new InputOption('--env', null, InputOption::VALUE_OPTIONAL, $message); } /** * Get the Laravel application instance. * * @return \Illuminate\Contracts\Foundation\Application */ public function getLaravel() { return $this->laravel; } } src/Illuminate/Console/View/Components/Error.php 0000644 00000000735 15107327037 0015713 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use Symfony\Component\Console\Output\OutputInterface; class Error extends Component { /** * Renders the component using the given arguments. * * @param string $string * @param int $verbosity * @return void */ public function render($string, $verbosity = OutputInterface::VERBOSITY_NORMAL) { with(new Line($this->output))->render('error', $string, $verbosity); } } src/Illuminate/Console/View/Components/Ask.php 0000644 00000000633 15107327037 0015335 0 ustar 00 <?php namespace Illuminate\Console\View\Components; class Ask extends Component { /** * Renders the component using the given arguments. * * @param string $question * @param string $default * @return mixed */ public function render($question, $default = null) { return $this->usingQuestionHelper(fn () => $this->output->ask($question, $default)); } } src/Illuminate/Console/View/Components/Warn.php 0000644 00000000750 15107327037 0015526 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use Symfony\Component\Console\Output\OutputInterface; class Warn extends Component { /** * Renders the component using the given arguments. * * @param string $string * @param int $verbosity * @return void */ public function render($string, $verbosity = OutputInterface::VERBOSITY_NORMAL) { with(new Line($this->output)) ->render('warn', $string, $verbosity); } } src/Illuminate/Console/View/Components/Secret.php 0000644 00000001077 15107327037 0016047 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use Symfony\Component\Console\Question\Question; class Secret extends Component { /** * Renders the component using the given arguments. * * @param string $question * @param bool $fallback * @return mixed */ public function render($question, $fallback = true) { $question = new Question($question); $question->setHidden(true)->setHiddenFallback($fallback); return $this->usingQuestionHelper(fn () => $this->output->askQuestion($question)); } } src/Illuminate/Console/View/Components/Component.php 0000644 00000005643 15107327037 0016567 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use Illuminate\Console\OutputStyle; use Illuminate\Console\QuestionHelper; use ReflectionClass; use Symfony\Component\Console\Helper\SymfonyQuestionHelper; use function Termwind\render; use function Termwind\renderUsing; abstract class Component { /** * The output style implementation. * * @var \Illuminate\Console\OutputStyle */ protected $output; /** * The list of mutators to apply on the view data. * * @var array<int, callable(string): string> */ protected $mutators; /** * Creates a new component instance. * * @param \Illuminate\Console\OutputStyle $output * @return void */ public function __construct($output) { $this->output = $output; } /** * Renders the given view. * * @param string $view * @param \Illuminate\Contracts\Support\Arrayable|array $data * @param int $verbosity * @return void */ protected function renderView($view, $data, $verbosity) { renderUsing($this->output); render((string) $this->compile($view, $data), $verbosity); } /** * Compile the given view contents. * * @param string $view * @param array $data * @return void */ protected function compile($view, $data) { extract($data); ob_start(); include __DIR__."/../../resources/views/components/$view.php"; return tap(ob_get_contents(), function () { ob_end_clean(); }); } /** * Mutates the given data with the given set of mutators. * * @param array<int, string>|string $data * @param array<int, callable(string): string> $mutators * @return array<int, string>|string */ protected function mutate($data, $mutators) { foreach ($mutators as $mutator) { $mutator = new $mutator; if (is_iterable($data)) { foreach ($data as $key => $value) { $data[$key] = $mutator($value); } } else { $data = $mutator($data); } } return $data; } /** * Eventually performs a question using the component's question helper. * * @param callable $callable * @return mixed */ protected function usingQuestionHelper($callable) { $property = with(new ReflectionClass(OutputStyle::class)) ->getParentClass() ->getProperty('questionHelper'); $currentHelper = $property->isInitialized($this->output) ? $property->getValue($this->output) : new SymfonyQuestionHelper(); $property->setValue($this->output, new QuestionHelper); try { return $callable(); } finally { $property->setValue($this->output, $currentHelper); } } } src/Illuminate/Console/View/Components/Info.php 0000644 00000000733 15107327037 0015513 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use Symfony\Component\Console\Output\OutputInterface; class Info extends Component { /** * Renders the component using the given arguments. * * @param string $string * @param int $verbosity * @return void */ public function render($string, $verbosity = OutputInterface::VERBOSITY_NORMAL) { with(new Line($this->output))->render('info', $string, $verbosity); } } src/Illuminate/Console/View/Components/Choice.php 0000644 00000001460 15107327037 0016010 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use Symfony\Component\Console\Question\ChoiceQuestion; class Choice extends Component { /** * Renders the component using the given arguments. * * @param string $question * @param array<array-key, string> $choices * @param mixed $default * @param int $attempts * @param bool $multiple * @return mixed */ public function render($question, $choices, $default = null, $attempts = null, $multiple = false) { return $this->usingQuestionHelper( fn () => $this->output->askQuestion( (new ChoiceQuestion($question, $choices, $default)) ->setMaxAttempts($attempts) ->setMultiselect($multiple) ), ); } } src/Illuminate/Console/View/Components/Task.php 0000644 00000003421 15107327037 0015517 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use Symfony\Component\Console\Output\OutputInterface; use Throwable; use function Termwind\terminal; class Task extends Component { /** * Renders the component using the given arguments. * * @param string $description * @param (callable(): bool)|null $task * @param int $verbosity * @return void */ public function render($description, $task = null, $verbosity = OutputInterface::VERBOSITY_NORMAL) { $description = $this->mutate($description, [ Mutators\EnsureDynamicContentIsHighlighted::class, Mutators\EnsureNoPunctuation::class, Mutators\EnsureRelativePaths::class, ]); $descriptionWidth = mb_strlen(preg_replace("/\<[\w=#\/\;,:.&,%?]+\>|\\e\[\d+m/", '$1', $description) ?? ''); $this->output->write(" $description ", false, $verbosity); $startTime = microtime(true); $result = false; try { $result = ($task ?: fn () => true)(); } catch (Throwable $e) { throw $e; } finally { $runTime = $task ? (' '.number_format((microtime(true) - $startTime) * 1000).'ms') : ''; $runTimeWidth = mb_strlen($runTime); $width = min(terminal()->width(), 150); $dots = max($width - $descriptionWidth - $runTimeWidth - 10, 0); $this->output->write(str_repeat('<fg=gray>.</>', $dots), false, $verbosity); $this->output->write("<fg=gray>$runTime</>", false, $verbosity); $this->output->writeln( $result !== false ? ' <fg=green;options=bold>DONE</>' : ' <fg=red;options=bold>FAIL</>', $verbosity, ); } } } src/Illuminate/Console/View/Components/BulletList.php 0000644 00000001352 15107327037 0016701 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use Symfony\Component\Console\Output\OutputInterface; class BulletList extends Component { /** * Renders the component using the given arguments. * * @param array<int, string> $elements * @param int $verbosity * @return void */ public function render($elements, $verbosity = OutputInterface::VERBOSITY_NORMAL) { $elements = $this->mutate($elements, [ Mutators\EnsureDynamicContentIsHighlighted::class, Mutators\EnsureNoPunctuation::class, Mutators\EnsureRelativePaths::class, ]); $this->renderView('bullet-list', [ 'elements' => $elements, ], $verbosity); } } src/Illuminate/Console/View/Components/AskWithCompletion.php 0000644 00000001357 15107327037 0020227 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use Symfony\Component\Console\Question\Question; class AskWithCompletion extends Component { /** * Renders the component using the given arguments. * * @param string $question * @param array|callable $choices * @param string $default * @return mixed */ public function render($question, $choices, $default = null) { $question = new Question($question, $default); is_callable($choices) ? $question->setAutocompleterCallback($choices) : $question->setAutocompleterValues($choices); return $this->usingQuestionHelper( fn () => $this->output->askQuestion($question) ); } } src/Illuminate/Console/View/Components/Mutators/EnsurePunctuation.php 0000644 00000000620 15107327037 0022124 0 ustar 00 <?php namespace Illuminate\Console\View\Components\Mutators; class EnsurePunctuation { /** * Ensures the given string ends with punctuation. * * @param string $string * @return string */ public function __invoke($string) { if (! str($string)->endsWith(['.', '?', '!', ':'])) { return "$string."; } return $string; } } src/Illuminate/Console/View/Components/Mutators/EnsureNoPunctuation.php 0000644 00000000655 15107327037 0022431 0 ustar 00 <?php namespace Illuminate\Console\View\Components\Mutators; class EnsureNoPunctuation { /** * Ensures the given string does not end with punctuation. * * @param string $string * @return string */ public function __invoke($string) { if (str($string)->endsWith(['.', '?', '!', ':'])) { return substr_replace($string, '', -1); } return $string; } } src/Illuminate/Console/View/Components/Mutators/EnsureRelativePaths.php 0000644 00000000676 15107327037 0022401 0 ustar 00 <?php namespace Illuminate\Console\View\Components\Mutators; class EnsureRelativePaths { /** * Ensures the given string only contains relative paths. * * @param string $string * @return string */ public function __invoke($string) { if (function_exists('app') && app()->has('path.base')) { $string = str_replace(base_path().'/', '', $string); } return $string; } } src/Illuminate/Console/View/Components/Mutators/EnsureDynamicContentIsHighlighted.php 0000644 00000000575 15107327037 0025200 0 ustar 00 <?php namespace Illuminate\Console\View\Components\Mutators; class EnsureDynamicContentIsHighlighted { /** * Highlight dynamic content within the given string. * * @param string $string * @return string */ public function __invoke($string) { return preg_replace('/\[([^\]]+)\]/', '<options=bold>[$1]</>', (string) $string); } } src/Illuminate/Console/View/Components/TwoColumnDetail.php 0000644 00000002005 15107327037 0017664 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use Symfony\Component\Console\Output\OutputInterface; class TwoColumnDetail extends Component { /** * Renders the component using the given arguments. * * @param string $first * @param string|null $second * @param int $verbosity * @return void */ public function render($first, $second = null, $verbosity = OutputInterface::VERBOSITY_NORMAL) { $first = $this->mutate($first, [ Mutators\EnsureDynamicContentIsHighlighted::class, Mutators\EnsureNoPunctuation::class, Mutators\EnsureRelativePaths::class, ]); $second = $this->mutate($second, [ Mutators\EnsureDynamicContentIsHighlighted::class, Mutators\EnsureNoPunctuation::class, Mutators\EnsureRelativePaths::class, ]); $this->renderView('two-column-detail', [ 'first' => $first, 'second' => $second, ], $verbosity); } } src/Illuminate/Console/View/Components/Alert.php 0000644 00000001306 15107327037 0015664 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use Symfony\Component\Console\Output\OutputInterface; class Alert extends Component { /** * Renders the component using the given arguments. * * @param string $string * @param int $verbosity * @return void */ public function render($string, $verbosity = OutputInterface::VERBOSITY_NORMAL) { $string = $this->mutate($string, [ Mutators\EnsureDynamicContentIsHighlighted::class, Mutators\EnsurePunctuation::class, Mutators\EnsureRelativePaths::class, ]); $this->renderView('alert', [ 'content' => $string, ], $verbosity); } } src/Illuminate/Console/View/Components/Factory.php 0000644 00000004664 15107327037 0016236 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use InvalidArgumentException; /** * @method void alert(string $string, int $verbosity = \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL) * @method mixed ask(string $question, string $default = null) * @method mixed askWithCompletion(string $question, array|callable $choices, string $default = null) * @method void bulletList(array $elements, int $verbosity = \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL) * @method mixed choice(string $question, array $choices, $default = null, int $attempts = null, bool $multiple = false) * @method bool confirm(string $question, bool $default = false) * @method void error(string $string, int $verbosity = \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL) * @method void info(string $string, int $verbosity = \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL) * @method void line(string $style, string $string, int $verbosity = \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL) * @method void task(string $description, ?callable $task = null, int $verbosity = \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL) * @method void twoColumnDetail(string $first, ?string $second = null, int $verbosity = \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL) * @method void warn(string $string, int $verbosity = \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL) */ class Factory { /** * The output interface implementation. * * @var \Illuminate\Console\OutputStyle */ protected $output; /** * Creates a new factory instance. * * @param \Illuminate\Console\OutputStyle $output * @return void */ public function __construct($output) { $this->output = $output; } /** * Dynamically handle calls into the component instance. * * @param string $method * @param array $parameters * @return mixed * * @throws \InvalidArgumentException */ public function __call($method, $parameters) { $component = '\Illuminate\Console\View\Components\\'.ucfirst($method); throw_unless(class_exists($component), new InvalidArgumentException(sprintf( 'Console component [%s] not found.', $method ))); return with(new $component($this->output))->render(...$parameters); } } src/Illuminate/Console/View/Components/Confirm.php 0000644 00000000670 15107327037 0016215 0 ustar 00 <?php namespace Illuminate\Console\View\Components; class Confirm extends Component { /** * Renders the component using the given arguments. * * @param string $question * @param bool $default * @return bool */ public function render($question, $default = false) { return $this->usingQuestionHelper( fn () => $this->output->confirm($question, $default), ); } } src/Illuminate/Console/View/Components/Line.php 0000644 00000002714 15107327037 0015510 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use Illuminate\Console\Contracts\NewLineAware; use Symfony\Component\Console\Output\OutputInterface; class Line extends Component { /** * The possible line styles. * * @var array<string, array<string, string>> */ protected static $styles = [ 'info' => [ 'bgColor' => 'blue', 'fgColor' => 'white', 'title' => 'info', ], 'warn' => [ 'bgColor' => 'yellow', 'fgColor' => 'black', 'title' => 'warn', ], 'error' => [ 'bgColor' => 'red', 'fgColor' => 'white', 'title' => 'error', ], ]; /** * Renders the component using the given arguments. * * @param string $style * @param string $string * @param int $verbosity * @return void */ public function render($style, $string, $verbosity = OutputInterface::VERBOSITY_NORMAL) { $string = $this->mutate($string, [ Mutators\EnsureDynamicContentIsHighlighted::class, Mutators\EnsurePunctuation::class, Mutators\EnsureRelativePaths::class, ]); $this->renderView('line', array_merge(static::$styles[$style], [ 'marginTop' => $this->output instanceof NewLineAware ? max(0, 2 - $this->output->newLinesWritten()) : 1, 'content' => $string, ]), $verbosity); } } src/Illuminate/Cache/TagSet.php 0000644 00000004725 15107327037 0012356 0 ustar 00 <?php namespace Illuminate\Cache; use Illuminate\Contracts\Cache\Store; class TagSet { /** * The cache store implementation. * * @var \Illuminate\Contracts\Cache\Store */ protected $store; /** * The tag names. * * @var array */ protected $names = []; /** * Create a new TagSet instance. * * @param \Illuminate\Contracts\Cache\Store $store * @param array $names * @return void */ public function __construct(Store $store, array $names = []) { $this->store = $store; $this->names = $names; } /** * Reset all tags in the set. * * @return void */ public function reset() { array_walk($this->names, [$this, 'resetTag']); } /** * Reset the tag and return the new tag identifier. * * @param string $name * @return string */ public function resetTag($name) { $this->store->forever($this->tagKey($name), $id = str_replace('.', '', uniqid('', true))); return $id; } /** * Flush all the tags in the set. * * @return void */ public function flush() { array_walk($this->names, [$this, 'flushTag']); } /** * Flush the tag from the cache. * * @param string $name */ public function flushTag($name) { $this->store->forget($this->tagKey($name)); } /** * Get a unique namespace that changes when any of the tags are flushed. * * @return string */ public function getNamespace() { return implode('|', $this->tagIds()); } /** * Get an array of tag identifiers for all of the tags in the set. * * @return array */ protected function tagIds() { return array_map([$this, 'tagId'], $this->names); } /** * Get the unique tag identifier for a given tag. * * @param string $name * @return string */ public function tagId($name) { return $this->store->get($this->tagKey($name)) ?: $this->resetTag($name); } /** * Get the tag identifier key for a given tag. * * @param string $name * @return string */ public function tagKey($name) { return 'tag:'.$name.':key'; } /** * Get all of the tag names in the set. * * @return array */ public function getNames() { return $this->names; } } src/Illuminate/Cache/MemcachedLock.php 0000644 00000002655 15107327037 0013646 0 ustar 00 <?php namespace Illuminate\Cache; class MemcachedLock extends Lock { /** * The Memcached instance. * * @var \Memcached */ protected $memcached; /** * Create a new lock instance. * * @param \Memcached $memcached * @param string $name * @param int $seconds * @param string|null $owner * @return void */ public function __construct($memcached, $name, $seconds, $owner = null) { parent::__construct($name, $seconds, $owner); $this->memcached = $memcached; } /** * Attempt to acquire the lock. * * @return bool */ public function acquire() { return $this->memcached->add( $this->name, $this->owner, $this->seconds ); } /** * Release the lock. * * @return bool */ public function release() { if ($this->isOwnedByCurrentProcess()) { return $this->memcached->delete($this->name); } return false; } /** * Releases this lock in disregard of ownership. * * @return void */ public function forceRelease() { $this->memcached->delete($this->name); } /** * Returns the owner value written into the driver for this lock. * * @return mixed */ protected function getCurrentOwner() { return $this->memcached->get($this->name); } } src/Illuminate/Cache/MemcachedConnector.php 0000644 00000004516 15107327037 0014706 0 ustar 00 <?php namespace Illuminate\Cache; use Memcached; class MemcachedConnector { /** * Create a new Memcached connection. * * @param array $servers * @param string|null $connectionId * @param array $options * @param array $credentials * @return \Memcached */ public function connect(array $servers, $connectionId = null, array $options = [], array $credentials = []) { $memcached = $this->getMemcached( $connectionId, $credentials, $options ); if (! $memcached->getServerList()) { // For each server in the array, we'll just extract the configuration and add // the server to the Memcached connection. Once we have added all of these // servers we'll verify the connection is successful and return it back. foreach ($servers as $server) { $memcached->addServer( $server['host'], $server['port'], $server['weight'] ); } } return $memcached; } /** * Get a new Memcached instance. * * @param string|null $connectionId * @param array $credentials * @param array $options * @return \Memcached */ protected function getMemcached($connectionId, array $credentials, array $options) { $memcached = $this->createMemcachedInstance($connectionId); if (count($credentials) === 2) { $this->setCredentials($memcached, $credentials); } if (count($options)) { $memcached->setOptions($options); } return $memcached; } /** * Create the Memcached instance. * * @param string|null $connectionId * @return \Memcached */ protected function createMemcachedInstance($connectionId) { return empty($connectionId) ? new Memcached : new Memcached($connectionId); } /** * Set the SASL credentials on the Memcached connection. * * @param \Memcached $memcached * @param array $credentials * @return void */ protected function setCredentials($memcached, $credentials) { [$username, $password] = $credentials; $memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true); $memcached->setSaslAuthData($username, $password); } } src/Illuminate/Cache/NullStore.php 0000644 00000004537 15107327037 0013117 0 ustar 00 <?php namespace Illuminate\Cache; use Illuminate\Contracts\Cache\LockProvider; class NullStore extends TaggableStore implements LockProvider { use RetrievesMultipleKeys; /** * Retrieve an item from the cache by key. * * @param string $key * @return void */ public function get($key) { // } /** * Store an item in the cache for a given number of seconds. * * @param string $key * @param mixed $value * @param int $seconds * @return bool */ public function put($key, $value, $seconds) { return false; } /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return bool */ public function increment($key, $value = 1) { return false; } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return bool */ public function decrement($key, $value = 1) { return false; } /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return bool */ public function forever($key, $value) { return false; } /** * Get a lock instance. * * @param string $name * @param int $seconds * @param string|null $owner * @return \Illuminate\Contracts\Cache\Lock */ public function lock($name, $seconds = 0, $owner = null) { return new NoLock($name, $seconds, $owner); } /** * Restore a lock instance using the owner identifier. * * @param string $name * @param string $owner * @return \Illuminate\Contracts\Cache\Lock */ public function restoreLock($name, $owner) { return $this->lock($name, 0, $owner); } /** * Remove an item from the cache. * * @param string $key * @return bool */ public function forget($key) { return true; } /** * Remove all items from the cache. * * @return bool */ public function flush() { return true; } /** * Get the cache key prefix. * * @return string */ public function getPrefix() { return ''; } } src/Illuminate/Cache/Console/ClearCommand.php 0000644 00000006564 15107327037 0015121 0 ustar 00 <?php namespace Illuminate\Cache\Console; use Illuminate\Cache\CacheManager; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'cache:clear')] class ClearCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'cache:clear'; /** * The console command description. * * @var string */ protected $description = 'Flush the application cache'; /** * The cache manager instance. * * @var \Illuminate\Cache\CacheManager */ protected $cache; /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * Create a new cache clear command instance. * * @param \Illuminate\Cache\CacheManager $cache * @param \Illuminate\Filesystem\Filesystem $files * @return void */ public function __construct(CacheManager $cache, Filesystem $files) { parent::__construct(); $this->cache = $cache; $this->files = $files; } /** * Execute the console command. * * @return void */ public function handle() { $this->laravel['events']->dispatch( 'cache:clearing', [$this->argument('store'), $this->tags()] ); $successful = $this->cache()->flush(); $this->flushFacades(); if (! $successful) { return $this->components->error('Failed to clear cache. Make sure you have the appropriate permissions.'); } $this->laravel['events']->dispatch( 'cache:cleared', [$this->argument('store'), $this->tags()] ); $this->components->info('Application cache cleared successfully.'); } /** * Flush the real-time facades stored in the cache directory. * * @return void */ public function flushFacades() { if (! $this->files->exists($storagePath = storage_path('framework/cache'))) { return; } foreach ($this->files->files($storagePath) as $file) { if (preg_match('/facade-.*\.php$/', $file)) { $this->files->delete($file); } } } /** * Get the cache instance for the command. * * @return \Illuminate\Cache\Repository */ protected function cache() { $cache = $this->cache->store($this->argument('store')); return empty($this->tags()) ? $cache : $cache->tags($this->tags()); } /** * Get the tags passed to the command. * * @return array */ protected function tags() { return array_filter(explode(',', $this->option('tags') ?? '')); } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return [ ['store', InputArgument::OPTIONAL, 'The name of the store you would like to clear'], ]; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['tags', null, InputOption::VALUE_OPTIONAL, 'The cache tags you would like to clear', null], ]; } } src/Illuminate/Cache/Console/ForgetCommand.php 0000644 00000002434 15107327037 0015311 0 ustar 00 <?php namespace Illuminate\Cache\Console; use Illuminate\Cache\CacheManager; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'cache:forget')] class ForgetCommand extends Command { /** * The console command name. * * @var string */ protected $signature = 'cache:forget {key : The key to remove} {store? : The store to remove the key from}'; /** * The console command description. * * @var string */ protected $description = 'Remove an item from the cache'; /** * The cache manager instance. * * @var \Illuminate\Cache\CacheManager */ protected $cache; /** * Create a new cache clear command instance. * * @param \Illuminate\Cache\CacheManager $cache * @return void */ public function __construct(CacheManager $cache) { parent::__construct(); $this->cache = $cache; } /** * Execute the console command. * * @return void */ public function handle() { $this->cache->store($this->argument('store'))->forget( $this->argument('key') ); $this->components->info('The ['.$this->argument('key').'] key has been removed from the cache.'); } } src/Illuminate/Cache/Console/CacheTableCommand.php 0000644 00000001551 15107327037 0016035 0 ustar 00 <?php namespace Illuminate\Cache\Console; use Illuminate\Console\MigrationGeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'cache:table')] class CacheTableCommand extends MigrationGeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'cache:table'; /** * The console command description. * * @var string */ protected $description = 'Create a migration for the cache database table'; /** * Get the migration table name. * * @return string */ protected function migrationTableName() { return 'cache'; } /** * Get the path to the migration stub file. * * @return string */ protected function migrationStubFile() { return __DIR__.'/stubs/cache.stub'; } } src/Illuminate/Cache/Console/PruneStaleTagsCommand.php 0000644 00000002653 15107327037 0016767 0 ustar 00 <?php namespace Illuminate\Cache\Console; use Illuminate\Cache\CacheManager; use Illuminate\Cache\RedisStore; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputArgument; #[AsCommand(name: 'cache:prune-stale-tags')] class PruneStaleTagsCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'cache:prune-stale-tags'; /** * The console command description. * * @var string */ protected $description = 'Prune stale cache tags from the cache (Redis only)'; /** * Execute the console command. * * @param \Illuminate\Cache\CacheManager $cache * @return void */ public function handle(CacheManager $cache) { $cache = $cache->store($this->argument('store')); if (! $cache->getStore() instanceof RedisStore) { $this->components->error('Pruning cache tags is only necessary when using Redis.'); return 1; } $cache->flushStaleTags(); $this->components->info('Stale cache tags pruned successfully.'); } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return [ ['store', InputArgument::OPTIONAL, 'The name of the store you would like to prune tags from'], ]; } } src/Illuminate/Cache/Console/stubs/cache.stub 0000644 00000001521 15107327037 0015151 0 ustar 00 <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('cache', function (Blueprint $table) { $table->string('key')->primary(); $table->mediumText('value'); $table->integer('expiration'); }); Schema::create('cache_locks', function (Blueprint $table) { $table->string('key')->primary(); $table->string('owner'); $table->integer('expiration'); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('cache'); Schema::dropIfExists('cache_locks'); } }; src/Illuminate/Cache/ArrayStore.php 0000644 00000011262 15107327037 0013254 0 ustar 00 <?php namespace Illuminate\Cache; use Illuminate\Contracts\Cache\LockProvider; use Illuminate\Support\Carbon; use Illuminate\Support\InteractsWithTime; class ArrayStore extends TaggableStore implements LockProvider { use InteractsWithTime, RetrievesMultipleKeys; /** * The array of stored values. * * @var array */ protected $storage = []; /** * The array of locks. * * @var array */ public $locks = []; /** * Indicates if values are serialized within the store. * * @var bool */ protected $serializesValues; /** * Create a new Array store. * * @param bool $serializesValues * @return void */ public function __construct($serializesValues = false) { $this->serializesValues = $serializesValues; } /** * Retrieve an item from the cache by key. * * @param string|array $key * @return mixed */ public function get($key) { if (! isset($this->storage[$key])) { return; } $item = $this->storage[$key]; $expiresAt = $item['expiresAt'] ?? 0; if ($expiresAt !== 0 && (Carbon::now()->getPreciseTimestamp(3) / 1000) >= $expiresAt) { $this->forget($key); return; } return $this->serializesValues ? unserialize($item['value']) : $item['value']; } /** * Store an item in the cache for a given number of seconds. * * @param string $key * @param mixed $value * @param int $seconds * @return bool */ public function put($key, $value, $seconds) { $this->storage[$key] = [ 'value' => $this->serializesValues ? serialize($value) : $value, 'expiresAt' => $this->calculateExpiration($seconds), ]; return true; } /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int */ public function increment($key, $value = 1) { if (! is_null($existing = $this->get($key))) { return tap(((int) $existing) + $value, function ($incremented) use ($key) { $value = $this->serializesValues ? serialize($incremented) : $incremented; $this->storage[$key]['value'] = $value; }); } $this->forever($key, $value); return $value; } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int */ public function decrement($key, $value = 1) { return $this->increment($key, $value * -1); } /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return bool */ public function forever($key, $value) { return $this->put($key, $value, 0); } /** * Remove an item from the cache. * * @param string $key * @return bool */ public function forget($key) { if (array_key_exists($key, $this->storage)) { unset($this->storage[$key]); return true; } return false; } /** * Remove all items from the cache. * * @return bool */ public function flush() { $this->storage = []; return true; } /** * Get the cache key prefix. * * @return string */ public function getPrefix() { return ''; } /** * Get the expiration time of the key. * * @param int $seconds * @return float */ protected function calculateExpiration($seconds) { return $this->toTimestamp($seconds); } /** * Get the UNIX timestamp, with milliseconds, for the given number of seconds in the future. * * @param int $seconds * @return float */ protected function toTimestamp($seconds) { return $seconds > 0 ? (Carbon::now()->getPreciseTimestamp(3) / 1000) + $seconds : 0; } /** * Get a lock instance. * * @param string $name * @param int $seconds * @param string|null $owner * @return \Illuminate\Contracts\Cache\Lock */ public function lock($name, $seconds = 0, $owner = null) { return new ArrayLock($this, $name, $seconds, $owner); } /** * Restore a lock instance using the owner identifier. * * @param string $name * @param string $owner * @return \Illuminate\Contracts\Cache\Lock */ public function restoreLock($name, $owner) { return $this->lock($name, 0, $owner); } } src/Illuminate/Cache/TaggedCache.php 0000644 00000005001 15107327037 0013272 0 ustar 00 <?php namespace Illuminate\Cache; use Illuminate\Contracts\Cache\Store; class TaggedCache extends Repository { use RetrievesMultipleKeys { putMany as putManyAlias; } /** * The tag set instance. * * @var \Illuminate\Cache\TagSet */ protected $tags; /** * Create a new tagged cache instance. * * @param \Illuminate\Contracts\Cache\Store $store * @param \Illuminate\Cache\TagSet $tags * @return void */ public function __construct(Store $store, TagSet $tags) { parent::__construct($store); $this->tags = $tags; } /** * Store multiple items in the cache for a given number of seconds. * * @param array $values * @param int|null $ttl * @return bool */ public function putMany(array $values, $ttl = null) { if ($ttl === null) { return $this->putManyForever($values); } return $this->putManyAlias($values, $ttl); } /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function increment($key, $value = 1) { return $this->store->increment($this->itemKey($key), $value); } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function decrement($key, $value = 1) { return $this->store->decrement($this->itemKey($key), $value); } /** * Remove all items from the cache. * * @return bool */ public function flush() { $this->tags->reset(); return true; } /** * {@inheritdoc} */ protected function itemKey($key) { return $this->taggedItemKey($key); } /** * Get a fully qualified key for a tagged item. * * @param string $key * @return string */ public function taggedItemKey($key) { return sha1($this->tags->getNamespace()).':'.$key; } /** * Fire an event for this cache instance. * * @param \Illuminate\Cache\Events\CacheEvent $event * @return void */ protected function event($event) { parent::event($event->setTags($this->tags->getNames())); } /** * Get the tag set instance. * * @return \Illuminate\Cache\TagSet */ public function getTags() { return $this->tags; } } src/Illuminate/Cache/MemcachedStore.php 0000644 00000014266 15107327037 0014053 0 ustar 00 <?php namespace Illuminate\Cache; use Illuminate\Contracts\Cache\LockProvider; use Illuminate\Support\InteractsWithTime; use Memcached; use ReflectionMethod; class MemcachedStore extends TaggableStore implements LockProvider { use InteractsWithTime; /** * The Memcached instance. * * @var \Memcached */ protected $memcached; /** * A string that should be prepended to keys. * * @var string */ protected $prefix; /** * Indicates whether we are using Memcached version >= 3.0.0. * * @var bool */ protected $onVersionThree; /** * Create a new Memcached store. * * @param \Memcached $memcached * @param string $prefix * @return void */ public function __construct($memcached, $prefix = '') { $this->setPrefix($prefix); $this->memcached = $memcached; $this->onVersionThree = (new ReflectionMethod('Memcached', 'getMulti')) ->getNumberOfParameters() == 2; } /** * Retrieve an item from the cache by key. * * @param string $key * @return mixed */ public function get($key) { $value = $this->memcached->get($this->prefix.$key); if ($this->memcached->getResultCode() == 0) { return $value; } } /** * Retrieve multiple items from the cache by key. * * Items not found in the cache will have a null value. * * @param array $keys * @return array */ public function many(array $keys) { $prefixedKeys = array_map(function ($key) { return $this->prefix.$key; }, $keys); if ($this->onVersionThree) { $values = $this->memcached->getMulti($prefixedKeys, Memcached::GET_PRESERVE_ORDER); } else { $null = null; $values = $this->memcached->getMulti($prefixedKeys, $null, Memcached::GET_PRESERVE_ORDER); } if ($this->memcached->getResultCode() != 0) { return array_fill_keys($keys, null); } return array_combine($keys, $values); } /** * Store an item in the cache for a given number of seconds. * * @param string $key * @param mixed $value * @param int $seconds * @return bool */ public function put($key, $value, $seconds) { return $this->memcached->set( $this->prefix.$key, $value, $this->calculateExpiration($seconds) ); } /** * Store multiple items in the cache for a given number of seconds. * * @param array $values * @param int $seconds * @return bool */ public function putMany(array $values, $seconds) { $prefixedValues = []; foreach ($values as $key => $value) { $prefixedValues[$this->prefix.$key] = $value; } return $this->memcached->setMulti( $prefixedValues, $this->calculateExpiration($seconds) ); } /** * Store an item in the cache if the key doesn't exist. * * @param string $key * @param mixed $value * @param int $seconds * @return bool */ public function add($key, $value, $seconds) { return $this->memcached->add( $this->prefix.$key, $value, $this->calculateExpiration($seconds) ); } /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function increment($key, $value = 1) { return $this->memcached->increment($this->prefix.$key, $value); } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function decrement($key, $value = 1) { return $this->memcached->decrement($this->prefix.$key, $value); } /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return bool */ public function forever($key, $value) { return $this->put($key, $value, 0); } /** * Get a lock instance. * * @param string $name * @param int $seconds * @param string|null $owner * @return \Illuminate\Contracts\Cache\Lock */ public function lock($name, $seconds = 0, $owner = null) { return new MemcachedLock($this->memcached, $this->prefix.$name, $seconds, $owner); } /** * Restore a lock instance using the owner identifier. * * @param string $name * @param string $owner * @return \Illuminate\Contracts\Cache\Lock */ public function restoreLock($name, $owner) { return $this->lock($name, 0, $owner); } /** * Remove an item from the cache. * * @param string $key * @return bool */ public function forget($key) { return $this->memcached->delete($this->prefix.$key); } /** * Remove all items from the cache. * * @return bool */ public function flush() { return $this->memcached->flush(); } /** * Get the expiration time of the key. * * @param int $seconds * @return int */ protected function calculateExpiration($seconds) { return $this->toTimestamp($seconds); } /** * Get the UNIX timestamp for the given number of seconds. * * @param int $seconds * @return int */ protected function toTimestamp($seconds) { return $seconds > 0 ? $this->availableAt($seconds) : 0; } /** * Get the underlying Memcached connection. * * @return \Memcached */ public function getMemcached() { return $this->memcached; } /** * Get the cache key prefix. * * @return string */ public function getPrefix() { return $this->prefix; } /** * Set the cache key prefix. * * @param string $prefix * @return void */ public function setPrefix($prefix) { $this->prefix = ! empty($prefix) ? $prefix.':' : ''; } } src/Illuminate/Cache/FileLock.php 0000644 00000000417 15107327037 0012651 0 ustar 00 <?php namespace Illuminate\Cache; class FileLock extends CacheLock { /** * Attempt to acquire the lock. * * @return bool */ public function acquire() { return $this->store->add($this->name, $this->owner, $this->seconds); } } src/Illuminate/Cache/RateLimiting/Unlimited.php 0000644 00000000400 15107327037 0015473 0 ustar 00 <?php namespace Illuminate\Cache\RateLimiting; class Unlimited extends GlobalLimit { /** * Create a new limit instance. * * @return void */ public function __construct() { parent::__construct(PHP_INT_MAX); } } src/Illuminate/Cache/RateLimiting/Limit.php 0000644 00000005275 15107327037 0014636 0 ustar 00 <?php namespace Illuminate\Cache\RateLimiting; class Limit { /** * The rate limit signature key. * * @var mixed */ public $key; /** * The maximum number of attempts allowed within the given number of minutes. * * @var int */ public $maxAttempts; /** * The number of minutes until the rate limit is reset. * * @var int */ public $decayMinutes; /** * The response generator callback. * * @var callable */ public $responseCallback; /** * Create a new limit instance. * * @param mixed $key * @param int $maxAttempts * @param int $decayMinutes * @return void */ public function __construct($key = '', int $maxAttempts = 60, int $decayMinutes = 1) { $this->key = $key; $this->maxAttempts = $maxAttempts; $this->decayMinutes = $decayMinutes; } /** * Create a new rate limit. * * @param int $maxAttempts * @return static */ public static function perMinute($maxAttempts) { return new static('', $maxAttempts); } /** * Create a new rate limit using minutes as decay time. * * @param int $decayMinutes * @param int $maxAttempts * @return static */ public static function perMinutes($decayMinutes, $maxAttempts) { return new static('', $maxAttempts, $decayMinutes); } /** * Create a new rate limit using hours as decay time. * * @param int $maxAttempts * @param int $decayHours * @return static */ public static function perHour($maxAttempts, $decayHours = 1) { return new static('', $maxAttempts, 60 * $decayHours); } /** * Create a new rate limit using days as decay time. * * @param int $maxAttempts * @param int $decayDays * @return static */ public static function perDay($maxAttempts, $decayDays = 1) { return new static('', $maxAttempts, 60 * 24 * $decayDays); } /** * Create a new unlimited rate limit. * * @return static */ public static function none() { return new Unlimited; } /** * Set the key of the rate limit. * * @param mixed $key * @return $this */ public function by($key) { $this->key = $key; return $this; } /** * Set the callback that should generate the response when the limit is exceeded. * * @param callable $callback * @return $this */ public function response(callable $callback) { $this->responseCallback = $callback; return $this; } } src/Illuminate/Cache/RateLimiting/GlobalLimit.php 0000644 00000000572 15107327037 0015752 0 ustar 00 <?php namespace Illuminate\Cache\RateLimiting; class GlobalLimit extends Limit { /** * Create a new limit instance. * * @param int $maxAttempts * @param int $decayMinutes * @return void */ public function __construct(int $maxAttempts, int $decayMinutes = 1) { parent::__construct('', $maxAttempts, $decayMinutes); } } src/Illuminate/Cache/RedisLock.php 0000644 00000003356 15107327037 0013045 0 ustar 00 <?php namespace Illuminate\Cache; class RedisLock extends Lock { /** * The Redis factory implementation. * * @var \Illuminate\Redis\Connections\Connection */ protected $redis; /** * Create a new lock instance. * * @param \Illuminate\Redis\Connections\Connection $redis * @param string $name * @param int $seconds * @param string|null $owner * @return void */ public function __construct($redis, $name, $seconds, $owner = null) { parent::__construct($name, $seconds, $owner); $this->redis = $redis; } /** * Attempt to acquire the lock. * * @return bool */ public function acquire() { if ($this->seconds > 0) { return $this->redis->set($this->name, $this->owner, 'EX', $this->seconds, 'NX') == true; } return $this->redis->setnx($this->name, $this->owner) === 1; } /** * Release the lock. * * @return bool */ public function release() { return (bool) $this->redis->eval(LuaScripts::releaseLock(), 1, $this->name, $this->owner); } /** * Releases this lock in disregard of ownership. * * @return void */ public function forceRelease() { $this->redis->del($this->name); } /** * Returns the owner value written into the driver for this lock. * * @return string */ protected function getCurrentOwner() { return $this->redis->get($this->name); } /** * Get the name of the Redis connection being used to manage the lock. * * @return string */ public function getConnectionName() { return $this->redis->getName(); } } src/Illuminate/Cache/ApcWrapper.php 0000644 00000003552 15107327037 0013230 0 ustar 00 <?php namespace Illuminate\Cache; class ApcWrapper { /** * Indicates if APCu is supported. * * @var bool */ protected $apcu = false; /** * Create a new APC wrapper instance. * * @return void */ public function __construct() { $this->apcu = function_exists('apcu_fetch'); } /** * Get an item from the cache. * * @param string $key * @return mixed */ public function get($key) { return $this->apcu ? apcu_fetch($key) : apc_fetch($key); } /** * Store an item in the cache. * * @param string $key * @param mixed $value * @param int $seconds * @return array|bool */ public function put($key, $value, $seconds) { return $this->apcu ? apcu_store($key, $value, $seconds) : apc_store($key, $value, $seconds); } /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function increment($key, $value) { return $this->apcu ? apcu_inc($key, $value) : apc_inc($key, $value); } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function decrement($key, $value) { return $this->apcu ? apcu_dec($key, $value) : apc_dec($key, $value); } /** * Remove an item from the cache. * * @param string $key * @return bool */ public function delete($key) { return $this->apcu ? apcu_delete($key) : apc_delete($key); } /** * Remove all items from the cache. * * @return bool */ public function flush() { return $this->apcu ? apcu_clear_cache() : apc_clear_cache('user'); } } src/Illuminate/Cache/RateLimiter.php 0000644 00000011665 15107327037 0013411 0 ustar 00 <?php namespace Illuminate\Cache; use Closure; use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Support\InteractsWithTime; class RateLimiter { use InteractsWithTime; /** * The cache store implementation. * * @var \Illuminate\Contracts\Cache\Repository */ protected $cache; /** * The configured limit object resolvers. * * @var array */ protected $limiters = []; /** * Create a new rate limiter instance. * * @param \Illuminate\Contracts\Cache\Repository $cache * @return void */ public function __construct(Cache $cache) { $this->cache = $cache; } /** * Register a named limiter configuration. * * @param string $name * @param \Closure $callback * @return $this */ public function for(string $name, Closure $callback) { $this->limiters[$name] = $callback; return $this; } /** * Get the given named rate limiter. * * @param string $name * @return \Closure|null */ public function limiter(string $name) { return $this->limiters[$name] ?? null; } /** * Attempts to execute a callback if it's not limited. * * @param string $key * @param int $maxAttempts * @param \Closure $callback * @param int $decaySeconds * @return mixed */ public function attempt($key, $maxAttempts, Closure $callback, $decaySeconds = 60) { if ($this->tooManyAttempts($key, $maxAttempts)) { return false; } if (is_null($result = $callback())) { $result = true; } return tap($result, function () use ($key, $decaySeconds) { $this->hit($key, $decaySeconds); }); } /** * Determine if the given key has been "accessed" too many times. * * @param string $key * @param int $maxAttempts * @return bool */ public function tooManyAttempts($key, $maxAttempts) { if ($this->attempts($key) >= $maxAttempts) { if ($this->cache->has($this->cleanRateLimiterKey($key).':timer')) { return true; } $this->resetAttempts($key); } return false; } /** * Increment the counter for a given key for a given decay time. * * @param string $key * @param int $decaySeconds * @return int */ public function hit($key, $decaySeconds = 60) { $key = $this->cleanRateLimiterKey($key); $this->cache->add( $key.':timer', $this->availableAt($decaySeconds), $decaySeconds ); $added = $this->cache->add($key, 0, $decaySeconds); $hits = (int) $this->cache->increment($key); if (! $added && $hits == 1) { $this->cache->put($key, 1, $decaySeconds); } return $hits; } /** * Get the number of attempts for the given key. * * @param string $key * @return mixed */ public function attempts($key) { $key = $this->cleanRateLimiterKey($key); return $this->cache->get($key, 0); } /** * Reset the number of attempts for the given key. * * @param string $key * @return mixed */ public function resetAttempts($key) { $key = $this->cleanRateLimiterKey($key); return $this->cache->forget($key); } /** * Get the number of retries left for the given key. * * @param string $key * @param int $maxAttempts * @return int */ public function remaining($key, $maxAttempts) { $key = $this->cleanRateLimiterKey($key); $attempts = $this->attempts($key); return $maxAttempts - $attempts; } /** * Get the number of retries left for the given key. * * @param string $key * @param int $maxAttempts * @return int */ public function retriesLeft($key, $maxAttempts) { return $this->remaining($key, $maxAttempts); } /** * Clear the hits and lockout timer for the given key. * * @param string $key * @return void */ public function clear($key) { $key = $this->cleanRateLimiterKey($key); $this->resetAttempts($key); $this->cache->forget($key.':timer'); } /** * Get the number of seconds until the "key" is accessible again. * * @param string $key * @return int */ public function availableIn($key) { $key = $this->cleanRateLimiterKey($key); return max(0, $this->cache->get($key.':timer') - $this->currentTime()); } /** * Clean the rate limiter key from unicode characters. * * @param string $key * @return string */ public function cleanRateLimiterKey($key) { return preg_replace('/&([a-z])[a-z]+;/i', '$1', htmlentities($key)); } } src/Illuminate/Cache/DatabaseStore.php 0000644 00000025044 15107327037 0013705 0 ustar 00 <?php namespace Illuminate\Cache; use Closure; use Exception; use Illuminate\Contracts\Cache\LockProvider; use Illuminate\Contracts\Cache\Store; use Illuminate\Database\ConnectionInterface; use Illuminate\Database\PostgresConnection; use Illuminate\Database\QueryException; use Illuminate\Support\InteractsWithTime; use Illuminate\Support\Str; class DatabaseStore implements LockProvider, Store { use InteractsWithTime, RetrievesMultipleKeys; /** * The database connection instance. * * @var \Illuminate\Database\ConnectionInterface */ protected $connection; /** * The database connection instance that should be used to manage locks. * * @var \Illuminate\Database\ConnectionInterface */ protected $lockConnection; /** * The name of the cache table. * * @var string */ protected $table; /** * A string that should be prepended to keys. * * @var string */ protected $prefix; /** * The name of the cache locks table. * * @var string */ protected $lockTable; /** * An array representation of the lock lottery odds. * * @var array */ protected $lockLottery; /** * The default number of seconds that a lock should be held. * * @var int */ protected $defaultLockTimeoutInSeconds; /** * Create a new database store. * * @param \Illuminate\Database\ConnectionInterface $connection * @param string $table * @param string $prefix * @param string $lockTable * @param array $lockLottery * @return void */ public function __construct(ConnectionInterface $connection, $table, $prefix = '', $lockTable = 'cache_locks', $lockLottery = [2, 100], $defaultLockTimeoutInSeconds = 86400) { $this->table = $table; $this->prefix = $prefix; $this->connection = $connection; $this->lockTable = $lockTable; $this->lockLottery = $lockLottery; $this->defaultLockTimeoutInSeconds = $defaultLockTimeoutInSeconds; } /** * Retrieve an item from the cache by key. * * @param string|array $key * @return mixed */ public function get($key) { $prefixed = $this->prefix.$key; $cache = $this->table()->where('key', '=', $prefixed)->first(); // If we have a cache record we will check the expiration time against current // time on the system and see if the record has expired. If it has, we will // remove the records from the database table so it isn't returned again. if (is_null($cache)) { return; } $cache = is_array($cache) ? (object) $cache : $cache; // If this cache expiration date is past the current time, we will remove this // item from the cache. Then we will return a null value since the cache is // expired. We will use "Carbon" to make this comparison with the column. if ($this->currentTime() >= $cache->expiration) { $this->forget($key); return; } return $this->unserialize($cache->value); } /** * Store an item in the cache for a given number of seconds. * * @param string $key * @param mixed $value * @param int $seconds * @return bool */ public function put($key, $value, $seconds) { $key = $this->prefix.$key; $value = $this->serialize($value); $expiration = $this->getTime() + $seconds; try { return $this->table()->insert(compact('key', 'value', 'expiration')); } catch (Exception) { $result = $this->table()->where('key', $key)->update(compact('value', 'expiration')); return $result > 0; } } /** * Store an item in the cache if the key doesn't exist. * * @param string $key * @param mixed $value * @param int $seconds * @return bool */ public function add($key, $value, $seconds) { $key = $this->prefix.$key; $value = $this->serialize($value); $expiration = $this->getTime() + $seconds; try { return $this->table()->insert(compact('key', 'value', 'expiration')); } catch (QueryException) { return $this->table() ->where('key', $key) ->where('expiration', '<=', $this->getTime()) ->update([ 'value' => $value, 'expiration' => $expiration, ]) >= 1; } } /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function increment($key, $value = 1) { return $this->incrementOrDecrement($key, $value, function ($current, $value) { return $current + $value; }); } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function decrement($key, $value = 1) { return $this->incrementOrDecrement($key, $value, function ($current, $value) { return $current - $value; }); } /** * Increment or decrement an item in the cache. * * @param string $key * @param mixed $value * @param \Closure $callback * @return int|bool */ protected function incrementOrDecrement($key, $value, Closure $callback) { return $this->connection->transaction(function () use ($key, $value, $callback) { $prefixed = $this->prefix.$key; $cache = $this->table()->where('key', $prefixed) ->lockForUpdate()->first(); // If there is no value in the cache, we will return false here. Otherwise the // value will be decrypted and we will proceed with this function to either // increment or decrement this value based on the given action callbacks. if (is_null($cache)) { return false; } $cache = is_array($cache) ? (object) $cache : $cache; $current = $this->unserialize($cache->value); // Here we'll call this callback function that was given to the function which // is used to either increment or decrement the function. We use a callback // so we do not have to recreate all this logic in each of the functions. $new = $callback((int) $current, $value); if (! is_numeric($current)) { return false; } // Here we will update the values in the table. We will also encrypt the value // since database cache values are encrypted by default with secure storage // that can't be easily read. We will return the new value after storing. $this->table()->where('key', $prefixed)->update([ 'value' => $this->serialize($new), ]); return $new; }); } /** * Get the current system time. * * @return int */ protected function getTime() { return $this->currentTime(); } /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return bool */ public function forever($key, $value) { return $this->put($key, $value, 315360000); } /** * Get a lock instance. * * @param string $name * @param int $seconds * @param string|null $owner * @return \Illuminate\Contracts\Cache\Lock */ public function lock($name, $seconds = 0, $owner = null) { return new DatabaseLock( $this->lockConnection ?? $this->connection, $this->lockTable, $this->prefix.$name, $seconds, $owner, $this->lockLottery, $this->defaultLockTimeoutInSeconds ); } /** * Restore a lock instance using the owner identifier. * * @param string $name * @param string $owner * @return \Illuminate\Contracts\Cache\Lock */ public function restoreLock($name, $owner) { return $this->lock($name, 0, $owner); } /** * Remove an item from the cache. * * @param string $key * @return bool */ public function forget($key) { $this->table()->where('key', '=', $this->prefix.$key)->delete(); return true; } /** * Remove all items from the cache. * * @return bool */ public function flush() { $this->table()->delete(); return true; } /** * Get a query builder for the cache table. * * @return \Illuminate\Database\Query\Builder */ protected function table() { return $this->connection->table($this->table); } /** * Get the underlying database connection. * * @return \Illuminate\Database\ConnectionInterface */ public function getConnection() { return $this->connection; } /** * Specify the name of the connection that should be used to manage locks. * * @param \Illuminate\Database\ConnectionInterface $connection * @return $this */ public function setLockConnection($connection) { $this->lockConnection = $connection; return $this; } /** * Get the cache key prefix. * * @return string */ public function getPrefix() { return $this->prefix; } /** * Serialize the given value. * * @param mixed $value * @return string */ protected function serialize($value) { $result = serialize($value); if ($this->connection instanceof PostgresConnection && str_contains($result, "\0")) { $result = base64_encode($result); } return $result; } /** * Unserialize the given value. * * @param string $value * @return mixed */ protected function unserialize($value) { if ($this->connection instanceof PostgresConnection && ! Str::contains($value, [':', ';'])) { $value = base64_decode($value); } return unserialize($value); } } src/Illuminate/Cache/LuaScripts.php 0000644 00000000716 15107327037 0013254 0 ustar 00 <?php namespace Illuminate\Cache; class LuaScripts { /** * Get the Lua script to atomically release a lock. * * KEYS[1] - The name of the lock * ARGV[1] - The owner key of the lock instance trying to release it * * @return string */ public static function releaseLock() { return <<<'LUA' if redis.call("get",KEYS[1]) == ARGV[1] then return redis.call("del",KEYS[1]) else return 0 end LUA; } } src/Illuminate/Cache/ArrayLock.php 0000644 00000004040 15107327037 0013044 0 ustar 00 <?php namespace Illuminate\Cache; use Illuminate\Support\Carbon; class ArrayLock extends Lock { /** * The parent array cache store. * * @var \Illuminate\Cache\ArrayStore */ protected $store; /** * Create a new lock instance. * * @param \Illuminate\Cache\ArrayStore $store * @param string $name * @param int $seconds * @param string|null $owner * @return void */ public function __construct($store, $name, $seconds, $owner = null) { parent::__construct($name, $seconds, $owner); $this->store = $store; } /** * Attempt to acquire the lock. * * @return bool */ public function acquire() { $expiration = $this->store->locks[$this->name]['expiresAt'] ?? Carbon::now()->addSecond(); if ($this->exists() && $expiration->isFuture()) { return false; } $this->store->locks[$this->name] = [ 'owner' => $this->owner, 'expiresAt' => $this->seconds === 0 ? null : Carbon::now()->addSeconds($this->seconds), ]; return true; } /** * Determine if the current lock exists. * * @return bool */ protected function exists() { return isset($this->store->locks[$this->name]); } /** * Release the lock. * * @return bool */ public function release() { if (! $this->exists()) { return false; } if (! $this->isOwnedByCurrentProcess()) { return false; } $this->forceRelease(); return true; } /** * Returns the owner value written into the driver for this lock. * * @return string */ protected function getCurrentOwner() { return $this->store->locks[$this->name]['owner']; } /** * Releases this lock in disregard of ownership. * * @return void */ public function forceRelease() { unset($this->store->locks[$this->name]); } } src/Illuminate/Cache/RetrievesMultipleKeys.php 0000644 00000002203 15107327037 0015474 0 ustar 00 <?php namespace Illuminate\Cache; trait RetrievesMultipleKeys { /** * Retrieve multiple items from the cache by key. * * Items not found in the cache will have a null value. * * @param array $keys * @return array */ public function many(array $keys) { $return = []; $keys = collect($keys)->mapWithKeys(function ($value, $key) { return [is_string($key) ? $key : $value => is_string($key) ? $value : null]; })->all(); foreach ($keys as $key => $default) { $return[$key] = $this->get($key, $default); } return $return; } /** * Store multiple items in the cache for a given number of seconds. * * @param array $values * @param int $seconds * @return bool */ public function putMany(array $values, $seconds) { $manyResult = null; foreach ($values as $key => $value) { $result = $this->put($key, $value, $seconds); $manyResult = is_null($manyResult) ? $result : $result && $manyResult; } return $manyResult ?: false; } } src/Illuminate/Cache/CacheManager.php 0000644 00000025216 15107327037 0013463 0 ustar 00 <?php namespace Illuminate\Cache; use Aws\DynamoDb\DynamoDbClient; use Closure; use Illuminate\Contracts\Cache\Factory as FactoryContract; use Illuminate\Contracts\Cache\Store; use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; use Illuminate\Support\Arr; use InvalidArgumentException; /** * @mixin \Illuminate\Cache\Repository * @mixin \Illuminate\Contracts\Cache\LockProvider */ class CacheManager implements FactoryContract { /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The array of resolved cache stores. * * @var array */ protected $stores = []; /** * The registered custom driver creators. * * @var array */ protected $customCreators = []; /** * Create a new Cache manager instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function __construct($app) { $this->app = $app; } /** * Get a cache store instance by name, wrapped in a repository. * * @param string|null $name * @return \Illuminate\Contracts\Cache\Repository */ public function store($name = null) { $name = $name ?: $this->getDefaultDriver(); return $this->stores[$name] ??= $this->resolve($name); } /** * Get a cache driver instance. * * @param string|null $driver * @return \Illuminate\Contracts\Cache\Repository */ public function driver($driver = null) { return $this->store($driver); } /** * Resolve the given store. * * @param string $name * @return \Illuminate\Contracts\Cache\Repository * * @throws \InvalidArgumentException */ public function resolve($name) { $config = $this->getConfig($name); if (is_null($config)) { throw new InvalidArgumentException("Cache store [{$name}] is not defined."); } if (isset($this->customCreators[$config['driver']])) { return $this->callCustomCreator($config); } $driverMethod = 'create'.ucfirst($config['driver']).'Driver'; if (method_exists($this, $driverMethod)) { return $this->{$driverMethod}($config); } throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported."); } /** * Call a custom driver creator. * * @param array $config * @return mixed */ protected function callCustomCreator(array $config) { return $this->customCreators[$config['driver']]($this->app, $config); } /** * Create an instance of the APC cache driver. * * @param array $config * @return \Illuminate\Cache\Repository */ protected function createApcDriver(array $config) { $prefix = $this->getPrefix($config); return $this->repository(new ApcStore(new ApcWrapper, $prefix)); } /** * Create an instance of the array cache driver. * * @param array $config * @return \Illuminate\Cache\Repository */ protected function createArrayDriver(array $config) { return $this->repository(new ArrayStore($config['serialize'] ?? false)); } /** * Create an instance of the file cache driver. * * @param array $config * @return \Illuminate\Cache\Repository */ protected function createFileDriver(array $config) { return $this->repository( (new FileStore($this->app['files'], $config['path'], $config['permission'] ?? null)) ->setLockDirectory($config['lock_path'] ?? null) ); } /** * Create an instance of the Memcached cache driver. * * @param array $config * @return \Illuminate\Cache\Repository */ protected function createMemcachedDriver(array $config) { $prefix = $this->getPrefix($config); $memcached = $this->app['memcached.connector']->connect( $config['servers'], $config['persistent_id'] ?? null, $config['options'] ?? [], array_filter($config['sasl'] ?? []) ); return $this->repository(new MemcachedStore($memcached, $prefix)); } /** * Create an instance of the Null cache driver. * * @return \Illuminate\Cache\Repository */ protected function createNullDriver() { return $this->repository(new NullStore); } /** * Create an instance of the Redis cache driver. * * @param array $config * @return \Illuminate\Cache\Repository */ protected function createRedisDriver(array $config) { $redis = $this->app['redis']; $connection = $config['connection'] ?? 'default'; $store = new RedisStore($redis, $this->getPrefix($config), $connection); return $this->repository( $store->setLockConnection($config['lock_connection'] ?? $connection) ); } /** * Create an instance of the database cache driver. * * @param array $config * @return \Illuminate\Cache\Repository */ protected function createDatabaseDriver(array $config) { $connection = $this->app['db']->connection($config['connection'] ?? null); $store = new DatabaseStore( $connection, $config['table'], $this->getPrefix($config), $config['lock_table'] ?? 'cache_locks', $config['lock_lottery'] ?? [2, 100], $config['lock_timeout'] ?? 86400, ); return $this->repository($store->setLockConnection( $this->app['db']->connection($config['lock_connection'] ?? $config['connection'] ?? null) )); } /** * Create an instance of the DynamoDB cache driver. * * @param array $config * @return \Illuminate\Cache\Repository */ protected function createDynamodbDriver(array $config) { $client = $this->newDynamodbClient($config); return $this->repository( new DynamoDbStore( $client, $config['table'], $config['attributes']['key'] ?? 'key', $config['attributes']['value'] ?? 'value', $config['attributes']['expiration'] ?? 'expires_at', $this->getPrefix($config) ) ); } /** * Create new DynamoDb Client instance. * * @return \Aws\DynamoDb\DynamoDbClient */ protected function newDynamodbClient(array $config) { $dynamoConfig = [ 'region' => $config['region'], 'version' => 'latest', 'endpoint' => $config['endpoint'] ?? null, ]; if (! empty($config['key']) && ! empty($config['secret'])) { $dynamoConfig['credentials'] = Arr::only( $config, ['key', 'secret'] ); } if (! empty($config['token'])) { $dynamoConfig['credentials']['token'] = $config['token']; } return new DynamoDbClient($dynamoConfig); } /** * Create a new cache repository with the given implementation. * * @param \Illuminate\Contracts\Cache\Store $store * @return \Illuminate\Cache\Repository */ public function repository(Store $store) { return tap(new Repository($store), function ($repository) { $this->setEventDispatcher($repository); }); } /** * Set the event dispatcher on the given repository instance. * * @param \Illuminate\Cache\Repository $repository * @return void */ protected function setEventDispatcher(Repository $repository) { if (! $this->app->bound(DispatcherContract::class)) { return; } $repository->setEventDispatcher( $this->app[DispatcherContract::class] ); } /** * Re-set the event dispatcher on all resolved cache repositories. * * @return void */ public function refreshEventDispatcher() { array_map([$this, 'setEventDispatcher'], $this->stores); } /** * Get the cache prefix. * * @param array $config * @return string */ protected function getPrefix(array $config) { return $config['prefix'] ?? $this->app['config']['cache.prefix']; } /** * Get the cache connection configuration. * * @param string $name * @return array|null */ protected function getConfig($name) { if (! is_null($name) && $name !== 'null') { return $this->app['config']["cache.stores.{$name}"]; } return ['driver' => 'null']; } /** * Get the default cache driver name. * * @return string */ public function getDefaultDriver() { return $this->app['config']['cache.default']; } /** * Set the default cache driver name. * * @param string $name * @return void */ public function setDefaultDriver($name) { $this->app['config']['cache.default'] = $name; } /** * Unset the given driver instances. * * @param array|string|null $name * @return $this */ public function forgetDriver($name = null) { $name ??= $this->getDefaultDriver(); foreach ((array) $name as $cacheName) { if (isset($this->stores[$cacheName])) { unset($this->stores[$cacheName]); } } return $this; } /** * Disconnect the given driver and remove from local cache. * * @param string|null $name * @return void */ public function purge($name = null) { $name ??= $this->getDefaultDriver(); unset($this->stores[$name]); } /** * Register a custom driver creator Closure. * * @param string $driver * @param \Closure $callback * @return $this */ public function extend($driver, Closure $callback) { $this->customCreators[$driver] = $callback->bindTo($this, $this); return $this; } /** * Set the application instance used by the manager. * * @param \Illuminate\Contracts\Foundation\Application $app * @return $this */ public function setApplication($app) { $this->app = $app; return $this; } /** * Dynamically call the default driver instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->store()->$method(...$parameters); } } src/Illuminate/Cache/DynamoDbStore.php 0000644 00000035207 15107327037 0013700 0 ustar 00 <?php namespace Illuminate\Cache; use Aws\DynamoDb\DynamoDbClient; use Aws\DynamoDb\Exception\DynamoDbException; use Illuminate\Contracts\Cache\LockProvider; use Illuminate\Contracts\Cache\Store; use Illuminate\Support\Carbon; use Illuminate\Support\InteractsWithTime; use Illuminate\Support\Str; use RuntimeException; class DynamoDbStore implements LockProvider, Store { use InteractsWithTime; /** * The DynamoDB client instance. * * @var \Aws\DynamoDb\DynamoDbClient */ protected $dynamo; /** * The table name. * * @var string */ protected $table; /** * The name of the attribute that should hold the key. * * @var string */ protected $keyAttribute; /** * The name of the attribute that should hold the value. * * @var string */ protected $valueAttribute; /** * The name of the attribute that should hold the expiration timestamp. * * @var string */ protected $expirationAttribute; /** * A string that should be prepended to keys. * * @var string */ protected $prefix; /** * Create a new store instance. * * @param \Aws\DynamoDb\DynamoDbClient $dynamo * @param string $table * @param string $keyAttribute * @param string $valueAttribute * @param string $expirationAttribute * @param string $prefix * @return void */ public function __construct(DynamoDbClient $dynamo, $table, $keyAttribute = 'key', $valueAttribute = 'value', $expirationAttribute = 'expires_at', $prefix = '') { $this->table = $table; $this->dynamo = $dynamo; $this->keyAttribute = $keyAttribute; $this->valueAttribute = $valueAttribute; $this->expirationAttribute = $expirationAttribute; $this->setPrefix($prefix); } /** * Retrieve an item from the cache by key. * * @param string $key * @return mixed */ public function get($key) { $response = $this->dynamo->getItem([ 'TableName' => $this->table, 'ConsistentRead' => false, 'Key' => [ $this->keyAttribute => [ 'S' => $this->prefix.$key, ], ], ]); if (! isset($response['Item'])) { return; } if ($this->isExpired($response['Item'])) { return; } if (isset($response['Item'][$this->valueAttribute])) { return $this->unserialize( $response['Item'][$this->valueAttribute]['S'] ?? $response['Item'][$this->valueAttribute]['N'] ?? null ); } } /** * Retrieve multiple items from the cache by key. * * Items not found in the cache will have a null value. * * @param array $keys * @return array */ public function many(array $keys) { if (count($keys) === 0) { return []; } $prefixedKeys = array_map(function ($key) { return $this->prefix.$key; }, $keys); $response = $this->dynamo->batchGetItem([ 'RequestItems' => [ $this->table => [ 'ConsistentRead' => false, 'Keys' => collect($prefixedKeys)->map(function ($key) { return [ $this->keyAttribute => [ 'S' => $key, ], ]; })->all(), ], ], ]); $now = Carbon::now(); return array_merge(collect(array_flip($keys))->map(function () { // })->all(), collect($response['Responses'][$this->table])->mapWithKeys(function ($response) use ($now) { if ($this->isExpired($response, $now)) { $value = null; } else { $value = $this->unserialize( $response[$this->valueAttribute]['S'] ?? $response[$this->valueAttribute]['N'] ?? null ); } return [Str::replaceFirst($this->prefix, '', $response[$this->keyAttribute]['S']) => $value]; })->all()); } /** * Determine if the given item is expired. * * @param array $item * @param \DateTimeInterface|null $expiration * @return bool */ protected function isExpired(array $item, $expiration = null) { $expiration = $expiration ?: Carbon::now(); return isset($item[$this->expirationAttribute]) && $expiration->getTimestamp() >= $item[$this->expirationAttribute]['N']; } /** * Store an item in the cache for a given number of seconds. * * @param string $key * @param mixed $value * @param int $seconds * @return bool */ public function put($key, $value, $seconds) { $this->dynamo->putItem([ 'TableName' => $this->table, 'Item' => [ $this->keyAttribute => [ 'S' => $this->prefix.$key, ], $this->valueAttribute => [ $this->type($value) => $this->serialize($value), ], $this->expirationAttribute => [ 'N' => (string) $this->toTimestamp($seconds), ], ], ]); return true; } /** * Store multiple items in the cache for a given number of seconds. * * @param array $values * @param int $seconds * @return bool */ public function putMany(array $values, $seconds) { if (count($values) === 0) { return true; } $expiration = $this->toTimestamp($seconds); $this->dynamo->batchWriteItem([ 'RequestItems' => [ $this->table => collect($values)->map(function ($value, $key) use ($expiration) { return [ 'PutRequest' => [ 'Item' => [ $this->keyAttribute => [ 'S' => $this->prefix.$key, ], $this->valueAttribute => [ $this->type($value) => $this->serialize($value), ], $this->expirationAttribute => [ 'N' => (string) $expiration, ], ], ], ]; })->values()->all(), ], ]); return true; } /** * Store an item in the cache if the key doesn't exist. * * @param string $key * @param mixed $value * @param int $seconds * @return bool */ public function add($key, $value, $seconds) { try { $this->dynamo->putItem([ 'TableName' => $this->table, 'Item' => [ $this->keyAttribute => [ 'S' => $this->prefix.$key, ], $this->valueAttribute => [ $this->type($value) => $this->serialize($value), ], $this->expirationAttribute => [ 'N' => (string) $this->toTimestamp($seconds), ], ], 'ConditionExpression' => 'attribute_not_exists(#key) OR #expires_at < :now', 'ExpressionAttributeNames' => [ '#key' => $this->keyAttribute, '#expires_at' => $this->expirationAttribute, ], 'ExpressionAttributeValues' => [ ':now' => [ 'N' => (string) Carbon::now()->getTimestamp(), ], ], ]); return true; } catch (DynamoDbException $e) { if (str_contains($e->getMessage(), 'ConditionalCheckFailed')) { return false; } throw $e; } } /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function increment($key, $value = 1) { try { $response = $this->dynamo->updateItem([ 'TableName' => $this->table, 'Key' => [ $this->keyAttribute => [ 'S' => $this->prefix.$key, ], ], 'ConditionExpression' => 'attribute_exists(#key) AND #expires_at > :now', 'UpdateExpression' => 'SET #value = #value + :amount', 'ExpressionAttributeNames' => [ '#key' => $this->keyAttribute, '#value' => $this->valueAttribute, '#expires_at' => $this->expirationAttribute, ], 'ExpressionAttributeValues' => [ ':now' => [ 'N' => (string) Carbon::now()->getTimestamp(), ], ':amount' => [ 'N' => (string) $value, ], ], 'ReturnValues' => 'UPDATED_NEW', ]); return (int) $response['Attributes'][$this->valueAttribute]['N']; } catch (DynamoDbException $e) { if (str_contains($e->getMessage(), 'ConditionalCheckFailed')) { return false; } throw $e; } } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function decrement($key, $value = 1) { try { $response = $this->dynamo->updateItem([ 'TableName' => $this->table, 'Key' => [ $this->keyAttribute => [ 'S' => $this->prefix.$key, ], ], 'ConditionExpression' => 'attribute_exists(#key) AND #expires_at > :now', 'UpdateExpression' => 'SET #value = #value - :amount', 'ExpressionAttributeNames' => [ '#key' => $this->keyAttribute, '#value' => $this->valueAttribute, '#expires_at' => $this->expirationAttribute, ], 'ExpressionAttributeValues' => [ ':now' => [ 'N' => (string) Carbon::now()->getTimestamp(), ], ':amount' => [ 'N' => (string) $value, ], ], 'ReturnValues' => 'UPDATED_NEW', ]); return (int) $response['Attributes'][$this->valueAttribute]['N']; } catch (DynamoDbException $e) { if (str_contains($e->getMessage(), 'ConditionalCheckFailed')) { return false; } throw $e; } } /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return bool */ public function forever($key, $value) { return $this->put($key, $value, Carbon::now()->addYears(5)->getTimestamp()); } /** * Get a lock instance. * * @param string $name * @param int $seconds * @param string|null $owner * @return \Illuminate\Contracts\Cache\Lock */ public function lock($name, $seconds = 0, $owner = null) { return new DynamoDbLock($this, $this->prefix.$name, $seconds, $owner); } /** * Restore a lock instance using the owner identifier. * * @param string $name * @param string $owner * @return \Illuminate\Contracts\Cache\Lock */ public function restoreLock($name, $owner) { return $this->lock($name, 0, $owner); } /** * Remove an item from the cache. * * @param string $key * @return bool */ public function forget($key) { $this->dynamo->deleteItem([ 'TableName' => $this->table, 'Key' => [ $this->keyAttribute => [ 'S' => $this->prefix.$key, ], ], ]); return true; } /** * Remove all items from the cache. * * @return bool * * @throws \RuntimeException */ public function flush() { throw new RuntimeException('DynamoDb does not support flushing an entire table. Please create a new table.'); } /** * Get the UNIX timestamp for the given number of seconds. * * @param int $seconds * @return int */ protected function toTimestamp($seconds) { return $seconds > 0 ? $this->availableAt($seconds) : Carbon::now()->getTimestamp(); } /** * Serialize the value. * * @param mixed $value * @return mixed */ protected function serialize($value) { return is_numeric($value) ? (string) $value : serialize($value); } /** * Unserialize the value. * * @param mixed $value * @return mixed */ protected function unserialize($value) { if (filter_var($value, FILTER_VALIDATE_INT) !== false) { return (int) $value; } if (is_numeric($value)) { return (float) $value; } return unserialize($value); } /** * Get the DynamoDB type for the given value. * * @param mixed $value * @return string */ protected function type($value) { return is_numeric($value) ? 'N' : 'S'; } /** * Get the cache key prefix. * * @return string */ public function getPrefix() { return $this->prefix; } /** * Set the cache key prefix. * * @param string $prefix * @return void */ public function setPrefix($prefix) { $this->prefix = ! empty($prefix) ? $prefix.':' : ''; } /** * Get the DynamoDb Client instance. * * @return \Aws\DynamoDb\DynamoDbClient */ public function getClient() { return $this->dynamo; } } src/Illuminate/Cache/RedisTagSet.php 0000644 00000006165 15107327037 0013345 0 ustar 00 <?php namespace Illuminate\Cache; use Illuminate\Support\Carbon; use Illuminate\Support\LazyCollection; class RedisTagSet extends TagSet { /** * Add a reference entry to the tag set's underlying sorted set. * * @param string $key * @param int $ttl * @param string $updateWhen * @return void */ public function addEntry(string $key, int $ttl = 0, $updateWhen = null) { $ttl = $ttl > 0 ? Carbon::now()->addSeconds($ttl)->getTimestamp() : -1; foreach ($this->tagIds() as $tagKey) { if ($updateWhen) { $this->store->connection()->zadd($this->store->getPrefix().$tagKey, $updateWhen, $ttl, $key); } else { $this->store->connection()->zadd($this->store->getPrefix().$tagKey, $ttl, $key); } } } /** * Get all of the cache entry keys for the tag set. * * @return \Illuminate\Support\LazyCollection */ public function entries() { return LazyCollection::make(function () { foreach ($this->tagIds() as $tagKey) { $cursor = $defaultCursorValue = '0'; do { [$cursor, $entries] = $this->store->connection()->zscan( $this->store->getPrefix().$tagKey, $cursor, ['match' => '*', 'count' => 1000] ); if (! is_array($entries)) { break; } $entries = array_unique(array_keys($entries)); if (count($entries) === 0) { continue; } foreach ($entries as $entry) { yield $entry; } } while (((string) $cursor) !== $defaultCursorValue); } }); } /** * Remove the stale entries from the tag set. * * @return void */ public function flushStaleEntries() { $this->store->connection()->pipeline(function ($pipe) { foreach ($this->tagIds() as $tagKey) { $pipe->zremrangebyscore($this->store->getPrefix().$tagKey, 0, Carbon::now()->getTimestamp()); } }); } /** * Flush the tag from the cache. * * @param string $name */ public function flushTag($name) { return $this->resetTag($name); } /** * Reset the tag and return the new tag identifier. * * @param string $name * @return string */ public function resetTag($name) { $this->store->forget($this->tagKey($name)); return $this->tagId($name); } /** * Get the unique tag identifier for a given tag. * * @param string $name * @return string */ public function tagId($name) { return "tag:{$name}:entries"; } /** * Get the tag identifier key for a given tag. * * @param string $name * @return string */ public function tagKey($name) { return "tag:{$name}:entries"; } } src/Illuminate/Cache/NoLock.php 0000644 00000001264 15107327037 0012347 0 ustar 00 <?php namespace Illuminate\Cache; class NoLock extends Lock { /** * Attempt to acquire the lock. * * @return bool */ public function acquire() { return true; } /** * Release the lock. * * @return bool */ public function release() { return true; } /** * Releases this lock in disregard of ownership. * * @return void */ public function forceRelease() { // } /** * Returns the owner value written into the driver for this lock. * * @return mixed */ protected function getCurrentOwner() { return $this->owner; } } src/Illuminate/Cache/FileStore.php 0000644 00000023154 15107327037 0013060 0 ustar 00 <?php namespace Illuminate\Cache; use Exception; use Illuminate\Contracts\Cache\LockProvider; use Illuminate\Contracts\Cache\Store; use Illuminate\Contracts\Filesystem\LockTimeoutException; use Illuminate\Filesystem\Filesystem; use Illuminate\Filesystem\LockableFile; use Illuminate\Support\InteractsWithTime; class FileStore implements Store, LockProvider { use InteractsWithTime, RetrievesMultipleKeys; /** * The Illuminate Filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * The file cache directory. * * @var string */ protected $directory; /** * The file cache lock directory. * * @var string|null */ protected $lockDirectory; /** * Octal representation of the cache file permissions. * * @var int|null */ protected $filePermission; /** * Create a new file cache store instance. * * @param \Illuminate\Filesystem\Filesystem $files * @param string $directory * @param int|null $filePermission * @return void */ public function __construct(Filesystem $files, $directory, $filePermission = null) { $this->files = $files; $this->directory = $directory; $this->filePermission = $filePermission; } /** * Retrieve an item from the cache by key. * * @param string|array $key * @return mixed */ public function get($key) { return $this->getPayload($key)['data'] ?? null; } /** * Store an item in the cache for a given number of seconds. * * @param string $key * @param mixed $value * @param int $seconds * @return bool */ public function put($key, $value, $seconds) { $this->ensureCacheDirectoryExists($path = $this->path($key)); $result = $this->files->put( $path, $this->expiration($seconds).serialize($value), true ); if ($result !== false && $result > 0) { $this->ensurePermissionsAreCorrect($path); return true; } return false; } /** * Store an item in the cache if the key doesn't exist. * * @param string $key * @param mixed $value * @param int $seconds * @return bool */ public function add($key, $value, $seconds) { $this->ensureCacheDirectoryExists($path = $this->path($key)); $file = new LockableFile($path, 'c+'); try { $file->getExclusiveLock(); } catch (LockTimeoutException) { $file->close(); return false; } $expire = $file->read(10); if (empty($expire) || $this->currentTime() >= $expire) { $file->truncate() ->write($this->expiration($seconds).serialize($value)) ->close(); $this->ensurePermissionsAreCorrect($path); return true; } $file->close(); return false; } /** * Create the file cache directory if necessary. * * @param string $path * @return void */ protected function ensureCacheDirectoryExists($path) { $directory = dirname($path); if (! $this->files->exists($directory)) { $this->files->makeDirectory($directory, 0777, true, true); // We're creating two levels of directories (e.g. 7e/24), so we check them both... $this->ensurePermissionsAreCorrect($directory); $this->ensurePermissionsAreCorrect(dirname($directory)); } } /** * Ensure the created node has the correct permissions. * * @param string $path * @return void */ protected function ensurePermissionsAreCorrect($path) { if (is_null($this->filePermission) || intval($this->files->chmod($path), 8) == $this->filePermission) { return; } $this->files->chmod($path, $this->filePermission); } /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int */ public function increment($key, $value = 1) { $raw = $this->getPayload($key); return tap(((int) $raw['data']) + $value, function ($newValue) use ($key, $raw) { $this->put($key, $newValue, $raw['time'] ?? 0); }); } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int */ public function decrement($key, $value = 1) { return $this->increment($key, $value * -1); } /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return bool */ public function forever($key, $value) { return $this->put($key, $value, 0); } /** * Get a lock instance. * * @param string $name * @param int $seconds * @param string|null $owner * @return \Illuminate\Contracts\Cache\Lock */ public function lock($name, $seconds = 0, $owner = null) { $this->ensureCacheDirectoryExists($this->lockDirectory ?? $this->directory); return new FileLock( new static($this->files, $this->lockDirectory ?? $this->directory, $this->filePermission), $name, $seconds, $owner ); } /** * Restore a lock instance using the owner identifier. * * @param string $name * @param string $owner * @return \Illuminate\Contracts\Cache\Lock */ public function restoreLock($name, $owner) { return $this->lock($name, 0, $owner); } /** * Remove an item from the cache. * * @param string $key * @return bool */ public function forget($key) { if ($this->files->exists($file = $this->path($key))) { return $this->files->delete($file); } return false; } /** * Remove all items from the cache. * * @return bool */ public function flush() { if (! $this->files->isDirectory($this->directory)) { return false; } foreach ($this->files->directories($this->directory) as $directory) { $deleted = $this->files->deleteDirectory($directory); if (! $deleted || $this->files->exists($directory)) { return false; } } return true; } /** * Retrieve an item and expiry time from the cache by key. * * @param string $key * @return array */ protected function getPayload($key) { $path = $this->path($key); // If the file doesn't exist, we obviously cannot return the cache so we will // just return null. Otherwise, we'll get the contents of the file and get // the expiration UNIX timestamps from the start of the file's contents. try { $expire = substr( $contents = $this->files->get($path, true), 0, 10 ); } catch (Exception) { return $this->emptyPayload(); } // If the current time is greater than expiration timestamps we will delete // the file and return null. This helps clean up the old files and keeps // this directory much cleaner for us as old files aren't hanging out. if ($this->currentTime() >= $expire) { $this->forget($key); return $this->emptyPayload(); } try { $data = unserialize(substr($contents, 10)); } catch (Exception) { $this->forget($key); return $this->emptyPayload(); } // Next, we'll extract the number of seconds that are remaining for a cache // so that we can properly retain the time for things like the increment // operation that may be performed on this cache on a later operation. $time = $expire - $this->currentTime(); return compact('data', 'time'); } /** * Get a default empty payload for the cache. * * @return array */ protected function emptyPayload() { return ['data' => null, 'time' => null]; } /** * Get the full path for the given cache key. * * @param string $key * @return string */ public function path($key) { $parts = array_slice(str_split($hash = sha1($key), 2), 0, 2); return $this->directory.'/'.implode('/', $parts).'/'.$hash; } /** * Get the expiration time based on the given seconds. * * @param int $seconds * @return int */ protected function expiration($seconds) { $time = $this->availableAt($seconds); return $seconds === 0 || $time > 9999999999 ? 9999999999 : $time; } /** * Get the Filesystem instance. * * @return \Illuminate\Filesystem\Filesystem */ public function getFilesystem() { return $this->files; } /** * Get the working directory of the cache. * * @return string */ public function getDirectory() { return $this->directory; } /** * Set the cache directory where locks should be stored. * * @param string|null $lockDirectory * @return $this */ public function setLockDirectory($lockDirectory) { $this->lockDirectory = $lockDirectory; return $this; } /** * Get the cache key prefix. * * @return string */ public function getPrefix() { return ''; } } src/Illuminate/Cache/CacheServiceProvider.php 0000644 00000002506 15107327037 0015221 0 ustar 00 <?php namespace Illuminate\Cache; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; use Symfony\Component\Cache\Adapter\Psr16Adapter; class CacheServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton('cache', function ($app) { return new CacheManager($app); }); $this->app->singleton('cache.store', function ($app) { return $app['cache']->driver(); }); $this->app->singleton('cache.psr6', function ($app) { return new Psr16Adapter($app['cache.store']); }); $this->app->singleton('memcached.connector', function () { return new MemcachedConnector; }); $this->app->singleton(RateLimiter::class, function ($app) { return new RateLimiter($app->make('cache')->driver( $app['config']->get('cache.limiter') )); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return [ 'cache', 'cache.store', 'cache.psr6', 'memcached.connector', RateLimiter::class, ]; } } src/Illuminate/Cache/CacheLock.php 0000644 00000003455 15107327037 0013002 0 ustar 00 <?php namespace Illuminate\Cache; class CacheLock extends Lock { /** * The cache store implementation. * * @var \Illuminate\Contracts\Cache\Store */ protected $store; /** * Create a new lock instance. * * @param \Illuminate\Contracts\Cache\Store $store * @param string $name * @param int $seconds * @param string|null $owner * @return void */ public function __construct($store, $name, $seconds, $owner = null) { parent::__construct($name, $seconds, $owner); $this->store = $store; } /** * Attempt to acquire the lock. * * @return bool */ public function acquire() { if (method_exists($this->store, 'add') && $this->seconds > 0) { return $this->store->add( $this->name, $this->owner, $this->seconds ); } if (! is_null($this->store->get($this->name))) { return false; } return ($this->seconds > 0) ? $this->store->put($this->name, $this->owner, $this->seconds) : $this->store->forever($this->name, $this->owner); } /** * Release the lock. * * @return bool */ public function release() { if ($this->isOwnedByCurrentProcess()) { return $this->store->forget($this->name); } return false; } /** * Releases this lock regardless of ownership. * * @return void */ public function forceRelease() { $this->store->forget($this->name); } /** * Returns the owner value written into the driver for this lock. * * @return mixed */ protected function getCurrentOwner() { return $this->store->get($this->name); } } src/Illuminate/Cache/composer.json 0000644 00000002761 15107327037 0013176 0 ustar 00 { "name": "illuminate/cache", "description": "The Illuminate Cache package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/collections": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0", "illuminate/support": "^10.0" }, "provide": { "psr/simple-cache-implementation": "1.0|2.0|3.0" }, "autoload": { "psr-4": { "Illuminate\\Cache\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "ext-apcu": "Required to use the APC cache driver.", "ext-filter": "Required to use the DynamoDb cache driver.", "ext-memcached": "Required to use the memcache cache driver.", "illuminate/database": "Required to use the database cache driver (^10.0).", "illuminate/filesystem": "Required to use the file cache driver (^10.0).", "illuminate/redis": "Required to use the redis cache driver (^10.0).", "symfony/cache": "Required to use PSR-6 cache bridge (^6.2)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Cache/Events/CacheEvent.php 0000644 00000001345 15107327037 0014433 0 ustar 00 <?php namespace Illuminate\Cache\Events; abstract class CacheEvent { /** * The key of the event. * * @var string */ public $key; /** * The tags that were assigned to the key. * * @var array */ public $tags; /** * Create a new event instance. * * @param string $key * @param array $tags * @return void */ public function __construct($key, array $tags = []) { $this->key = $key; $this->tags = $tags; } /** * Set the tags for the cache event. * * @param array $tags * @return $this */ public function setTags($tags) { $this->tags = $tags; return $this; } } src/Illuminate/Cache/Events/KeyForgotten.php 0000644 00000000134 15107327037 0015041 0 ustar 00 <?php namespace Illuminate\Cache\Events; class KeyForgotten extends CacheEvent { // } src/Illuminate/Cache/Events/CacheHit.php 0000644 00000000755 15107327037 0014102 0 ustar 00 <?php namespace Illuminate\Cache\Events; class CacheHit extends CacheEvent { /** * The value that was retrieved. * * @var mixed */ public $value; /** * Create a new event instance. * * @param string $key * @param mixed $value * @param array $tags * @return void */ public function __construct($key, $value, array $tags = []) { parent::__construct($key, $tags); $this->value = $value; } } src/Illuminate/Cache/Events/KeyWritten.php 0000644 00000001265 15107327037 0014534 0 ustar 00 <?php namespace Illuminate\Cache\Events; class KeyWritten extends CacheEvent { /** * The value that was written. * * @var mixed */ public $value; /** * The number of seconds the key should be valid. * * @var int|null */ public $seconds; /** * Create a new event instance. * * @param string $key * @param mixed $value * @param int|null $seconds * @param array $tags * @return void */ public function __construct($key, $value, $seconds = null, $tags = []) { parent::__construct($key, $tags); $this->value = $value; $this->seconds = $seconds; } } src/Illuminate/Cache/Events/CacheMissed.php 0000644 00000000133 15107327037 0014570 0 ustar 00 <?php namespace Illuminate\Cache\Events; class CacheMissed extends CacheEvent { // } src/Illuminate/Cache/ApcStore.php 0000644 00000005032 15107327037 0012677 0 ustar 00 <?php namespace Illuminate\Cache; class ApcStore extends TaggableStore { use RetrievesMultipleKeys; /** * The APC wrapper instance. * * @var \Illuminate\Cache\ApcWrapper */ protected $apc; /** * A string that should be prepended to keys. * * @var string */ protected $prefix; /** * Create a new APC store. * * @param \Illuminate\Cache\ApcWrapper $apc * @param string $prefix * @return void */ public function __construct(ApcWrapper $apc, $prefix = '') { $this->apc = $apc; $this->prefix = $prefix; } /** * Retrieve an item from the cache by key. * * @param string|array $key * @return mixed */ public function get($key) { $value = $this->apc->get($this->prefix.$key); if ($value !== false) { return $value; } } /** * Store an item in the cache for a given number of seconds. * * @param string $key * @param mixed $value * @param int $seconds * @return bool */ public function put($key, $value, $seconds) { return $this->apc->put($this->prefix.$key, $value, $seconds); } /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function increment($key, $value = 1) { return $this->apc->increment($this->prefix.$key, $value); } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function decrement($key, $value = 1) { return $this->apc->decrement($this->prefix.$key, $value); } /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return bool */ public function forever($key, $value) { return $this->put($key, $value, 0); } /** * Remove an item from the cache. * * @param string $key * @return bool */ public function forget($key) { return $this->apc->delete($this->prefix.$key); } /** * Remove all items from the cache. * * @return bool */ public function flush() { return $this->apc->flush(); } /** * Get the cache key prefix. * * @return string */ public function getPrefix() { return $this->prefix; } } src/Illuminate/Cache/DatabaseLock.php 0000644 00000007413 15107327037 0013501 0 ustar 00 <?php namespace Illuminate\Cache; use Illuminate\Database\Connection; use Illuminate\Database\QueryException; class DatabaseLock extends Lock { /** * The database connection instance. * * @var \Illuminate\Database\Connection */ protected $connection; /** * The database table name. * * @var string */ protected $table; /** * The prune probability odds. * * @var array */ protected $lottery; /** * The default number of seconds that a lock should be held. * * @var int */ protected $defaultTimeoutInSeconds; /** * Create a new lock instance. * * @param \Illuminate\Database\Connection $connection * @param string $table * @param string $name * @param int $seconds * @param string|null $owner * @param array $lottery * @return void */ public function __construct(Connection $connection, $table, $name, $seconds, $owner = null, $lottery = [2, 100], $defaultTimeoutInSeconds = 86400) { parent::__construct($name, $seconds, $owner); $this->connection = $connection; $this->table = $table; $this->lottery = $lottery; $this->defaultTimeoutInSeconds = $defaultTimeoutInSeconds; } /** * Attempt to acquire the lock. * * @return bool */ public function acquire() { try { $this->connection->table($this->table)->insert([ 'key' => $this->name, 'owner' => $this->owner, 'expiration' => $this->expiresAt(), ]); $acquired = true; } catch (QueryException) { $updated = $this->connection->table($this->table) ->where('key', $this->name) ->where(function ($query) { return $query->where('owner', $this->owner)->orWhere('expiration', '<=', time()); })->update([ 'owner' => $this->owner, 'expiration' => $this->expiresAt(), ]); $acquired = $updated >= 1; } if (random_int(1, $this->lottery[1]) <= $this->lottery[0]) { $this->connection->table($this->table)->where('expiration', '<=', time())->delete(); } return $acquired; } /** * Get the UNIX timestamp indicating when the lock should expire. * * @return int */ protected function expiresAt() { $lockTimeout = $this->seconds > 0 ? $this->seconds : $this->defaultTimeoutInSeconds; return time() + $lockTimeout; } /** * Release the lock. * * @return bool */ public function release() { if ($this->isOwnedByCurrentProcess()) { $this->connection->table($this->table) ->where('key', $this->name) ->where('owner', $this->owner) ->delete(); return true; } return false; } /** * Releases this lock in disregard of ownership. * * @return void */ public function forceRelease() { $this->connection->table($this->table) ->where('key', $this->name) ->delete(); } /** * Returns the owner value written into the driver for this lock. * * @return string */ protected function getCurrentOwner() { return optional($this->connection->table($this->table)->where('key', $this->name)->first())->owner; } /** * Get the name of the database connection being used to manage the lock. * * @return string */ public function getConnectionName() { return $this->connection->getName(); } } src/Illuminate/Cache/RedisTaggedCache.php 0000644 00000005457 15107327037 0014300 0 ustar 00 <?php namespace Illuminate\Cache; class RedisTaggedCache extends TaggedCache { /** * Store an item in the cache if the key does not exist. * * @param string $key * @param mixed $value * @param \DateTimeInterface|\DateInterval|int|null $ttl * @return bool */ public function add($key, $value, $ttl = null) { $this->tags->addEntry( $this->itemKey($key), ! is_null($ttl) ? $this->getSeconds($ttl) : 0 ); return parent::add($key, $value, $ttl); } /** * Store an item in the cache. * * @param string $key * @param mixed $value * @param \DateTimeInterface|\DateInterval|int|null $ttl * @return bool */ public function put($key, $value, $ttl = null) { if (is_null($ttl)) { return $this->forever($key, $value); } $this->tags->addEntry( $this->itemKey($key), $this->getSeconds($ttl) ); return parent::put($key, $value, $ttl); } /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function increment($key, $value = 1) { $this->tags->addEntry($this->itemKey($key), updateWhen: 'NX'); return parent::increment($key, $value); } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function decrement($key, $value = 1) { $this->tags->addEntry($this->itemKey($key), updateWhen: 'NX'); return parent::decrement($key, $value); } /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return bool */ public function forever($key, $value) { $this->tags->addEntry($this->itemKey($key)); return parent::forever($key, $value); } /** * Remove all items from the cache. * * @return bool */ public function flush() { $this->flushValues(); $this->tags->flush(); return true; } /** * Flush the individual cache entries for the tags. * * @return void */ protected function flushValues() { $entries = $this->tags->entries() ->map(fn (string $key) => $this->store->getPrefix().$key) ->chunk(1000); foreach ($entries as $cacheKeys) { $this->store->connection()->del(...$cacheKeys); } } /** * Remove all stale reference entries from the tag set. * * @return bool */ public function flushStale() { $this->tags->flushStaleEntries(); return true; } } src/Illuminate/Cache/HasCacheLock.php 0000644 00000001251 15107327037 0013426 0 ustar 00 <?php namespace Illuminate\Cache; trait HasCacheLock { /** * Get a lock instance. * * @param string $name * @param int $seconds * @param string|null $owner * @return \Illuminate\Contracts\Cache\Lock */ public function lock($name, $seconds = 0, $owner = null) { return new CacheLock($this, $name, $seconds, $owner); } /** * Restore a lock instance using the owner identifier. * * @param string $name * @param string $owner * @return \Illuminate\Contracts\Cache\Lock */ public function restoreLock($name, $owner) { return $this->lock($name, 0, $owner); } } src/Illuminate/Cache/Repository.php 0000644 00000041317 15107327037 0013344 0 ustar 00 <?php namespace Illuminate\Cache; use ArrayAccess; use BadMethodCallException; use Closure; use DateTimeInterface; use Illuminate\Cache\Events\CacheHit; use Illuminate\Cache\Events\CacheMissed; use Illuminate\Cache\Events\KeyForgotten; use Illuminate\Cache\Events\KeyWritten; use Illuminate\Contracts\Cache\Repository as CacheContract; use Illuminate\Contracts\Cache\Store; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\Carbon; use Illuminate\Support\InteractsWithTime; use Illuminate\Support\Traits\Macroable; /** * @mixin \Illuminate\Contracts\Cache\Store */ class Repository implements ArrayAccess, CacheContract { use InteractsWithTime; use Macroable { __call as macroCall; } /** * The cache store implementation. * * @var \Illuminate\Contracts\Cache\Store */ protected $store; /** * The event dispatcher implementation. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * The default number of seconds to store items. * * @var int|null */ protected $default = 3600; /** * Create a new cache repository instance. * * @param \Illuminate\Contracts\Cache\Store $store * @return void */ public function __construct(Store $store) { $this->store = $store; } /** * Determine if an item exists in the cache. * * @param array|string $key * @return bool */ public function has($key): bool { return ! is_null($this->get($key)); } /** * Determine if an item doesn't exist in the cache. * * @param string $key * @return bool */ public function missing($key) { return ! $this->has($key); } /** * Retrieve an item from the cache by key. * * @template TCacheValue * * @param array|string $key * @param TCacheValue|(\Closure(): TCacheValue) $default * @return (TCacheValue is null ? mixed : TCacheValue) */ public function get($key, $default = null): mixed { if (is_array($key)) { return $this->many($key); } $value = $this->store->get($this->itemKey($key)); // If we could not find the cache value, we will fire the missed event and get // the default value for this cache value. This default could be a callback // so we will execute the value function which will resolve it if needed. if (is_null($value)) { $this->event(new CacheMissed($key)); $value = value($default); } else { $this->event(new CacheHit($key, $value)); } return $value; } /** * Retrieve multiple items from the cache by key. * * Items not found in the cache will have a null value. * * @param array $keys * @return array */ public function many(array $keys) { $values = $this->store->many(collect($keys)->map(function ($value, $key) { return is_string($key) ? $key : $value; })->values()->all()); return collect($values)->map(function ($value, $key) use ($keys) { return $this->handleManyResult($keys, $key, $value); })->all(); } /** * {@inheritdoc} * * @return iterable */ public function getMultiple($keys, $default = null): iterable { $defaults = []; foreach ($keys as $key) { $defaults[$key] = $default; } return $this->many($defaults); } /** * Handle a result for the "many" method. * * @param array $keys * @param string $key * @param mixed $value * @return mixed */ protected function handleManyResult($keys, $key, $value) { // If we could not find the cache value, we will fire the missed event and get // the default value for this cache value. This default could be a callback // so we will execute the value function which will resolve it if needed. if (is_null($value)) { $this->event(new CacheMissed($key)); return (isset($keys[$key]) && ! array_is_list($keys)) ? value($keys[$key]) : null; } // If we found a valid value we will fire the "hit" event and return the value // back from this function. The "hit" event gives developers an opportunity // to listen for every possible cache "hit" throughout this applications. $this->event(new CacheHit($key, $value)); return $value; } /** * Retrieve an item from the cache and delete it. * * @template TCacheValue * * @param array|string $key * @param TCacheValue|(\Closure(): TCacheValue) $default * @return (TCacheValue is null ? mixed : TCacheValue) */ public function pull($key, $default = null) { return tap($this->get($key, $default), function () use ($key) { $this->forget($key); }); } /** * Store an item in the cache. * * @param array|string $key * @param mixed $value * @param \DateTimeInterface|\DateInterval|int|null $ttl * @return bool */ public function put($key, $value, $ttl = null) { if (is_array($key)) { return $this->putMany($key, $value); } if ($ttl === null) { return $this->forever($key, $value); } $seconds = $this->getSeconds($ttl); if ($seconds <= 0) { return $this->forget($key); } $result = $this->store->put($this->itemKey($key), $value, $seconds); if ($result) { $this->event(new KeyWritten($key, $value, $seconds)); } return $result; } /** * {@inheritdoc} * * @return bool */ public function set($key, $value, $ttl = null): bool { return $this->put($key, $value, $ttl); } /** * Store multiple items in the cache for a given number of seconds. * * @param array $values * @param \DateTimeInterface|\DateInterval|int|null $ttl * @return bool */ public function putMany(array $values, $ttl = null) { if ($ttl === null) { return $this->putManyForever($values); } $seconds = $this->getSeconds($ttl); if ($seconds <= 0) { return $this->deleteMultiple(array_keys($values)); } $result = $this->store->putMany($values, $seconds); if ($result) { foreach ($values as $key => $value) { $this->event(new KeyWritten($key, $value, $seconds)); } } return $result; } /** * Store multiple items in the cache indefinitely. * * @param array $values * @return bool */ protected function putManyForever(array $values) { $result = true; foreach ($values as $key => $value) { if (! $this->forever($key, $value)) { $result = false; } } return $result; } /** * {@inheritdoc} * * @return bool */ public function setMultiple($values, $ttl = null): bool { return $this->putMany(is_array($values) ? $values : iterator_to_array($values), $ttl); } /** * Store an item in the cache if the key does not exist. * * @param string $key * @param mixed $value * @param \DateTimeInterface|\DateInterval|int|null $ttl * @return bool */ public function add($key, $value, $ttl = null) { $seconds = null; if ($ttl !== null) { $seconds = $this->getSeconds($ttl); if ($seconds <= 0) { return false; } // If the store has an "add" method we will call the method on the store so it // has a chance to override this logic. Some drivers better support the way // this operation should work with a total "atomic" implementation of it. if (method_exists($this->store, 'add')) { return $this->store->add( $this->itemKey($key), $value, $seconds ); } } // If the value did not exist in the cache, we will put the value in the cache // so it exists for subsequent requests. Then, we will return true so it is // easy to know if the value gets added. Otherwise, we will return false. if (is_null($this->get($key))) { return $this->put($key, $value, $seconds); } return false; } /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function increment($key, $value = 1) { return $this->store->increment($key, $value); } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function decrement($key, $value = 1) { return $this->store->decrement($key, $value); } /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return bool */ public function forever($key, $value) { $result = $this->store->forever($this->itemKey($key), $value); if ($result) { $this->event(new KeyWritten($key, $value)); } return $result; } /** * Get an item from the cache, or execute the given Closure and store the result. * * @template TCacheValue * * @param string $key * @param \Closure|\DateTimeInterface|\DateInterval|int|null $ttl * @param \Closure(): TCacheValue $callback * @return TCacheValue */ public function remember($key, $ttl, Closure $callback) { $value = $this->get($key); // If the item exists in the cache we will just return this immediately and if // not we will execute the given Closure and cache the result of that for a // given number of seconds so it's available for all subsequent requests. if (! is_null($value)) { return $value; } $value = $callback(); $this->put($key, $value, value($ttl, $value)); return $value; } /** * Get an item from the cache, or execute the given Closure and store the result forever. * * @template TCacheValue * * @param string $key * @param \Closure(): TCacheValue $callback * @return TCacheValue */ public function sear($key, Closure $callback) { return $this->rememberForever($key, $callback); } /** * Get an item from the cache, or execute the given Closure and store the result forever. * * @template TCacheValue * * @param string $key * @param \Closure(): TCacheValue $callback * @return TCacheValue */ public function rememberForever($key, Closure $callback) { $value = $this->get($key); // If the item exists in the cache we will just return this immediately // and if not we will execute the given Closure and cache the result // of that forever so it is available for all subsequent requests. if (! is_null($value)) { return $value; } $this->forever($key, $value = $callback()); return $value; } /** * Remove an item from the cache. * * @param string $key * @return bool */ public function forget($key) { return tap($this->store->forget($this->itemKey($key)), function ($result) use ($key) { if ($result) { $this->event(new KeyForgotten($key)); } }); } /** * {@inheritdoc} * * @return bool */ public function delete($key): bool { return $this->forget($key); } /** * {@inheritdoc} * * @return bool */ public function deleteMultiple($keys): bool { $result = true; foreach ($keys as $key) { if (! $this->forget($key)) { $result = false; } } return $result; } /** * {@inheritdoc} * * @return bool */ public function clear(): bool { return $this->store->flush(); } /** * Begin executing a new tags operation if the store supports it. * * @param array|mixed $names * @return \Illuminate\Cache\TaggedCache * * @throws \BadMethodCallException */ public function tags($names) { if (! $this->supportsTags()) { throw new BadMethodCallException('This cache store does not support tagging.'); } $cache = $this->store->tags(is_array($names) ? $names : func_get_args()); if (! is_null($this->events)) { $cache->setEventDispatcher($this->events); } return $cache->setDefaultCacheTime($this->default); } /** * Format the key for a cache item. * * @param string $key * @return string */ protected function itemKey($key) { return $key; } /** * Calculate the number of seconds for the given TTL. * * @param \DateTimeInterface|\DateInterval|int $ttl * @return int */ protected function getSeconds($ttl) { $duration = $this->parseDateInterval($ttl); if ($duration instanceof DateTimeInterface) { $duration = Carbon::now()->diffInRealSeconds($duration, false); } return (int) ($duration > 0 ? $duration : 0); } /** * Determine if the current store supports tags. * * @return bool */ public function supportsTags() { return method_exists($this->store, 'tags'); } /** * Get the default cache time. * * @return int|null */ public function getDefaultCacheTime() { return $this->default; } /** * Set the default cache time in seconds. * * @param int|null $seconds * @return $this */ public function setDefaultCacheTime($seconds) { $this->default = $seconds; return $this; } /** * Get the cache store implementation. * * @return \Illuminate\Contracts\Cache\Store */ public function getStore() { return $this->store; } /** * Set the cache store implementation. * * @param \Illuminate\Contracts\Cache\Store $store * @return static */ public function setStore($store) { $this->store = $store; return $this; } /** * Fire an event for this cache instance. * * @param object|string $event * @return void */ protected function event($event) { $this->events?->dispatch($event); } /** * Get the event dispatcher instance. * * @return \Illuminate\Contracts\Events\Dispatcher */ public function getEventDispatcher() { return $this->events; } /** * Set the event dispatcher instance. * * @param \Illuminate\Contracts\Events\Dispatcher $events * @return void */ public function setEventDispatcher(Dispatcher $events) { $this->events = $events; } /** * Determine if a cached value exists. * * @param string $key * @return bool */ public function offsetExists($key): bool { return $this->has($key); } /** * Retrieve an item from the cache by key. * * @param string $key * @return mixed */ public function offsetGet($key): mixed { return $this->get($key); } /** * Store an item in the cache for the default time. * * @param string $key * @param mixed $value * @return void */ public function offsetSet($key, $value): void { $this->put($key, $value, $this->default); } /** * Remove an item from the cache. * * @param string $key * @return void */ public function offsetUnset($key): void { $this->forget($key); } /** * Handle dynamic calls into macros or pass missing methods to the store. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } return $this->store->$method(...$parameters); } /** * Clone cache repository instance. * * @return void */ public function __clone() { $this->store = clone $this->store; } } src/Illuminate/Cache/TaggableStore.php 0000644 00000000645 15107327037 0013707 0 ustar 00 <?php namespace Illuminate\Cache; use Illuminate\Contracts\Cache\Store; abstract class TaggableStore implements Store { /** * Begin executing a new tags operation. * * @param array|mixed $names * @return \Illuminate\Cache\TaggedCache */ public function tags($names) { return new TaggedCache($this, new TagSet($this, is_array($names) ? $names : func_get_args())); } } src/Illuminate/Cache/LICENSE.md 0000644 00000002063 15107327037 0012053 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Cache/DynamoDbLock.php 0000644 00000003056 15107327037 0013471 0 ustar 00 <?php namespace Illuminate\Cache; class DynamoDbLock extends Lock { /** * The DynamoDB client instance. * * @var \Illuminate\Cache\DynamoDbStore */ protected $dynamo; /** * Create a new lock instance. * * @param \Illuminate\Cache\DynamoDbStore $dynamo * @param string $name * @param int $seconds * @param string|null $owner * @return void */ public function __construct(DynamoDbStore $dynamo, $name, $seconds, $owner = null) { parent::__construct($name, $seconds, $owner); $this->dynamo = $dynamo; } /** * Attempt to acquire the lock. * * @return bool */ public function acquire() { if ($this->seconds > 0) { return $this->dynamo->add($this->name, $this->owner, $this->seconds); } return $this->dynamo->add($this->name, $this->owner, 86400); } /** * Release the lock. * * @return bool */ public function release() { if ($this->isOwnedByCurrentProcess()) { return $this->dynamo->forget($this->name); } return false; } /** * Release this lock in disregard of ownership. * * @return void */ public function forceRelease() { $this->dynamo->forget($this->name); } /** * Returns the owner value written into the driver for this lock. * * @return mixed */ protected function getCurrentOwner() { return $this->dynamo->get($this->name); } } src/Illuminate/Cache/RedisStore.php 0000644 00000024213 15107327037 0013244 0 ustar 00 <?php namespace Illuminate\Cache; use Illuminate\Contracts\Cache\LockProvider; use Illuminate\Contracts\Redis\Factory as Redis; use Illuminate\Redis\Connections\PhpRedisConnection; use Illuminate\Redis\Connections\PredisConnection; use Illuminate\Support\LazyCollection; use Illuminate\Support\Str; class RedisStore extends TaggableStore implements LockProvider { /** * The Redis factory implementation. * * @var \Illuminate\Contracts\Redis\Factory */ protected $redis; /** * A string that should be prepended to keys. * * @var string */ protected $prefix; /** * The Redis connection instance that should be used to manage locks. * * @var string */ protected $connection; /** * The name of the connection that should be used for locks. * * @var string */ protected $lockConnection; /** * Create a new Redis store. * * @param \Illuminate\Contracts\Redis\Factory $redis * @param string $prefix * @param string $connection * @return void */ public function __construct(Redis $redis, $prefix = '', $connection = 'default') { $this->redis = $redis; $this->setPrefix($prefix); $this->setConnection($connection); } /** * Retrieve an item from the cache by key. * * @param string|array $key * @return mixed */ public function get($key) { $value = $this->connection()->get($this->prefix.$key); return ! is_null($value) ? $this->unserialize($value) : null; } /** * Retrieve multiple items from the cache by key. * * Items not found in the cache will have a null value. * * @param array $keys * @return array */ public function many(array $keys) { if (count($keys) === 0) { return []; } $results = []; $values = $this->connection()->mget(array_map(function ($key) { return $this->prefix.$key; }, $keys)); foreach ($values as $index => $value) { $results[$keys[$index]] = ! is_null($value) ? $this->unserialize($value) : null; } return $results; } /** * Store an item in the cache for a given number of seconds. * * @param string $key * @param mixed $value * @param int $seconds * @return bool */ public function put($key, $value, $seconds) { return (bool) $this->connection()->setex( $this->prefix.$key, (int) max(1, $seconds), $this->serialize($value) ); } /** * Store multiple items in the cache for a given number of seconds. * * @param array $values * @param int $seconds * @return bool */ public function putMany(array $values, $seconds) { $serializedValues = []; foreach ($values as $key => $value) { $serializedValues[$this->prefix.$key] = $this->serialize($value); } $this->connection()->multi(); $manyResult = null; foreach ($serializedValues as $key => $value) { $result = (bool) $this->connection()->setex( $key, (int) max(1, $seconds), $value ); $manyResult = is_null($manyResult) ? $result : $result && $manyResult; } $this->connection()->exec(); return $manyResult ?: false; } /** * Store an item in the cache if the key doesn't exist. * * @param string $key * @param mixed $value * @param int $seconds * @return bool */ public function add($key, $value, $seconds) { $lua = "return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])"; return (bool) $this->connection()->eval( $lua, 1, $this->prefix.$key, $this->serialize($value), (int) max(1, $seconds) ); } /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int */ public function increment($key, $value = 1) { return $this->connection()->incrby($this->prefix.$key, $value); } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int */ public function decrement($key, $value = 1) { return $this->connection()->decrby($this->prefix.$key, $value); } /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return bool */ public function forever($key, $value) { return (bool) $this->connection()->set($this->prefix.$key, $this->serialize($value)); } /** * Get a lock instance. * * @param string $name * @param int $seconds * @param string|null $owner * @return \Illuminate\Contracts\Cache\Lock */ public function lock($name, $seconds = 0, $owner = null) { $lockName = $this->prefix.$name; $lockConnection = $this->lockConnection(); if ($lockConnection instanceof PhpRedisConnection) { return new PhpRedisLock($lockConnection, $lockName, $seconds, $owner); } return new RedisLock($lockConnection, $lockName, $seconds, $owner); } /** * Restore a lock instance using the owner identifier. * * @param string $name * @param string $owner * @return \Illuminate\Contracts\Cache\Lock */ public function restoreLock($name, $owner) { return $this->lock($name, 0, $owner); } /** * Remove an item from the cache. * * @param string $key * @return bool */ public function forget($key) { return (bool) $this->connection()->del($this->prefix.$key); } /** * Remove all items from the cache. * * @return bool */ public function flush() { $this->connection()->flushdb(); return true; } /** * Remove all expired tag set entries. * * @return void */ public function flushStaleTags() { foreach ($this->currentTags()->chunk(1000) as $tags) { $this->tags($tags->all())->flushStale(); } } /** * Begin executing a new tags operation. * * @param array|mixed $names * @return \Illuminate\Cache\RedisTaggedCache */ public function tags($names) { return new RedisTaggedCache( $this, new RedisTagSet($this, is_array($names) ? $names : func_get_args()) ); } /** * Get a collection of all of the cache tags currently being used. * * @param int $chunkSize * @return \Illuminate\Support\LazyCollection */ protected function currentTags($chunkSize = 1000) { $connection = $this->connection(); // Connections can have a global prefix... $connectionPrefix = match (true) { $connection instanceof PhpRedisConnection => $connection->_prefix(''), $connection instanceof PredisConnection => $connection->getOptions()->prefix ?: '', default => '', }; $prefix = $connectionPrefix.$this->getPrefix(); return LazyCollection::make(function () use ($connection, $chunkSize, $prefix) { $cursor = $defaultCursorValue = '0'; do { [$cursor, $tagsChunk] = $connection->scan( $cursor, ['match' => $prefix.'tag:*:entries', 'count' => $chunkSize] ); if (! is_array($tagsChunk)) { break; } $tagsChunk = array_unique($tagsChunk); if (empty($tagsChunk)) { continue; } foreach ($tagsChunk as $tag) { yield $tag; } } while (((string) $cursor) !== $defaultCursorValue); })->map(fn (string $tagKey) => Str::match('/^'.preg_quote($prefix, '/').'tag:(.*):entries$/', $tagKey)); } /** * Get the Redis connection instance. * * @return \Illuminate\Redis\Connections\Connection */ public function connection() { return $this->redis->connection($this->connection); } /** * Get the Redis connection instance that should be used to manage locks. * * @return \Illuminate\Redis\Connections\Connection */ public function lockConnection() { return $this->redis->connection($this->lockConnection ?? $this->connection); } /** * Specify the name of the connection that should be used to store data. * * @param string $connection * @return void */ public function setConnection($connection) { $this->connection = $connection; } /** * Specify the name of the connection that should be used to manage locks. * * @param string $connection * @return $this */ public function setLockConnection($connection) { $this->lockConnection = $connection; return $this; } /** * Get the Redis database instance. * * @return \Illuminate\Contracts\Redis\Factory */ public function getRedis() { return $this->redis; } /** * Get the cache key prefix. * * @return string */ public function getPrefix() { return $this->prefix; } /** * Set the cache key prefix. * * @param string $prefix * @return void */ public function setPrefix($prefix) { $this->prefix = ! empty($prefix) ? $prefix.':' : ''; } /** * Serialize the value. * * @param mixed $value * @return mixed */ protected function serialize($value) { return is_numeric($value) && ! in_array($value, [INF, -INF]) && ! is_nan($value) ? $value : serialize($value); } /** * Unserialize the value. * * @param mixed $value * @return mixed */ protected function unserialize($value) { return is_numeric($value) ? $value : unserialize($value); } } src/Illuminate/Cache/PhpRedisLock.php 0000644 00000001475 15107327037 0013515 0 ustar 00 <?php namespace Illuminate\Cache; use Illuminate\Redis\Connections\PhpRedisConnection; class PhpRedisLock extends RedisLock { /** * Create a new phpredis lock instance. * * @param \Illuminate\Redis\Connections\PhpRedisConnection $redis * @param string $name * @param int $seconds * @param string|null $owner * @return void */ public function __construct(PhpRedisConnection $redis, string $name, int $seconds, ?string $owner = null) { parent::__construct($redis, $name, $seconds, $owner); } /** * {@inheritDoc} */ public function release() { return (bool) $this->redis->eval( LuaScripts::releaseLock(), 1, $this->name, ...$this->redis->pack([$this->owner]) ); } } src/Illuminate/Cache/Lock.php 0000644 00000007000 15107327037 0012044 0 ustar 00 <?php namespace Illuminate\Cache; use Illuminate\Contracts\Cache\Lock as LockContract; use Illuminate\Contracts\Cache\LockTimeoutException; use Illuminate\Support\InteractsWithTime; use Illuminate\Support\Sleep; use Illuminate\Support\Str; abstract class Lock implements LockContract { use InteractsWithTime; /** * The name of the lock. * * @var string */ protected $name; /** * The number of seconds the lock should be maintained. * * @var int */ protected $seconds; /** * The scope identifier of this lock. * * @var string */ protected $owner; /** * The number of milliseconds to wait before re-attempting to acquire a lock while blocking. * * @var int */ protected $sleepMilliseconds = 250; /** * Create a new lock instance. * * @param string $name * @param int $seconds * @param string|null $owner * @return void */ public function __construct($name, $seconds, $owner = null) { if (is_null($owner)) { $owner = Str::random(); } $this->name = $name; $this->owner = $owner; $this->seconds = $seconds; } /** * Attempt to acquire the lock. * * @return bool */ abstract public function acquire(); /** * Release the lock. * * @return bool */ abstract public function release(); /** * Returns the owner value written into the driver for this lock. * * @return string */ abstract protected function getCurrentOwner(); /** * Attempt to acquire the lock. * * @param callable|null $callback * @return mixed */ public function get($callback = null) { $result = $this->acquire(); if ($result && is_callable($callback)) { try { return $callback(); } finally { $this->release(); } } return $result; } /** * Attempt to acquire the lock for the given number of seconds. * * @param int $seconds * @param callable|null $callback * @return mixed * * @throws \Illuminate\Contracts\Cache\LockTimeoutException */ public function block($seconds, $callback = null) { $starting = $this->currentTime(); while (! $this->acquire()) { Sleep::usleep($this->sleepMilliseconds * 1000); if ($this->currentTime() - $seconds >= $starting) { throw new LockTimeoutException; } } if (is_callable($callback)) { try { return $callback(); } finally { $this->release(); } } return true; } /** * Returns the current owner of the lock. * * @return string */ public function owner() { return $this->owner; } /** * Determines whether this lock is allowed to release the lock in the driver. * * @return bool */ protected function isOwnedByCurrentProcess() { return $this->getCurrentOwner() === $this->owner; } /** * Specify the number of milliseconds to sleep in between blocked lock acquisition attempts. * * @param int $milliseconds * @return $this */ public function betweenBlockedAttemptsSleepFor($milliseconds) { $this->sleepMilliseconds = $milliseconds; return $this; } } src/Illuminate/Hashing/AbstractHasher.php 0000644 00000001344 15107327037 0014435 0 ustar 00 <?php namespace Illuminate\Hashing; abstract class AbstractHasher { /** * Get information about the given hashed value. * * @param string $hashedValue * @return array */ public function info($hashedValue) { return password_get_info($hashedValue); } /** * Check the given plain value against a hash. * * @param string $value * @param string|null $hashedValue * @param array $options * @return bool */ public function check($value, $hashedValue, array $options = []) { if (is_null($hashedValue) || strlen($hashedValue) === 0) { return false; } return password_verify($value, $hashedValue); } } src/Illuminate/Hashing/HashManager.php 0000644 00000005041 15107327037 0013713 0 ustar 00 <?php namespace Illuminate\Hashing; use Illuminate\Contracts\Hashing\Hasher; use Illuminate\Support\Manager; /** * @mixin \Illuminate\Contracts\Hashing\Hasher */ class HashManager extends Manager implements Hasher { /** * Create an instance of the Bcrypt hash Driver. * * @return \Illuminate\Hashing\BcryptHasher */ public function createBcryptDriver() { return new BcryptHasher($this->config->get('hashing.bcrypt') ?? []); } /** * Create an instance of the Argon2i hash Driver. * * @return \Illuminate\Hashing\ArgonHasher */ public function createArgonDriver() { return new ArgonHasher($this->config->get('hashing.argon') ?? []); } /** * Create an instance of the Argon2id hash Driver. * * @return \Illuminate\Hashing\Argon2IdHasher */ public function createArgon2idDriver() { return new Argon2IdHasher($this->config->get('hashing.argon') ?? []); } /** * Get information about the given hashed value. * * @param string $hashedValue * @return array */ public function info($hashedValue) { return $this->driver()->info($hashedValue); } /** * Hash the given value. * * @param string $value * @param array $options * @return string */ public function make($value, array $options = []) { return $this->driver()->make($value, $options); } /** * Check the given plain value against a hash. * * @param string $value * @param string $hashedValue * @param array $options * @return bool */ public function check($value, $hashedValue, array $options = []) { return $this->driver()->check($value, $hashedValue, $options); } /** * Check if the given hash has been hashed using the given options. * * @param string $hashedValue * @param array $options * @return bool */ public function needsRehash($hashedValue, array $options = []) { return $this->driver()->needsRehash($hashedValue, $options); } /** * Determine if a given string is already hashed. * * @param string $value * @return bool */ public function isHashed($value) { return password_get_info($value)['algo'] !== null; } /** * Get the default driver name. * * @return string */ public function getDefaultDriver() { return $this->config->get('hashing.driver', 'bcrypt'); } } src/Illuminate/Hashing/BcryptHasher.php 0000644 00000007106 15107327037 0014137 0 ustar 00 <?php namespace Illuminate\Hashing; use Illuminate\Contracts\Hashing\Hasher as HasherContract; use RuntimeException; class BcryptHasher extends AbstractHasher implements HasherContract { /** * The default cost factor. * * @var int */ protected $rounds = 12; /** * Indicates whether to perform an algorithm check. * * @var bool */ protected $verifyAlgorithm = false; /** * Create a new hasher instance. * * @param array $options * @return void */ public function __construct(array $options = []) { $this->rounds = $options['rounds'] ?? $this->rounds; $this->verifyAlgorithm = $options['verify'] ?? $this->verifyAlgorithm; } /** * Hash the given value. * * @param string $value * @param array $options * @return string * * @throws \RuntimeException */ public function make($value, array $options = []) { $hash = password_hash($value, PASSWORD_BCRYPT, [ 'cost' => $this->cost($options), ]); if ($hash === false) { throw new RuntimeException('Bcrypt hashing not supported.'); } return $hash; } /** * Check the given plain value against a hash. * * @param string $value * @param string $hashedValue * @param array $options * @return bool * * @throws \RuntimeException */ public function check($value, $hashedValue, array $options = []) { if ($this->verifyAlgorithm && ! $this->isUsingCorrectAlgorithm($hashedValue)) { throw new RuntimeException('This password does not use the Bcrypt algorithm.'); } return parent::check($value, $hashedValue, $options); } /** * Check if the given hash has been hashed using the given options. * * @param string $hashedValue * @param array $options * @return bool */ public function needsRehash($hashedValue, array $options = []) { return password_needs_rehash($hashedValue, PASSWORD_BCRYPT, [ 'cost' => $this->cost($options), ]); } /** * Verifies that the configuration is less than or equal to what is configured. * * @internal */ public function verifyConfiguration($value) { return $this->isUsingCorrectAlgorithm($value) && $this->isUsingValidOptions($value); } /** * Verify the hashed value's algorithm. * * @param string $hashedValue * @return bool */ protected function isUsingCorrectAlgorithm($hashedValue) { return $this->info($hashedValue)['algoName'] === 'bcrypt'; } /** * Verify the hashed value's options. * * @param string $hashedValue * @return bool */ protected function isUsingValidOptions($hashedValue) { ['options' => $options] = $this->info($hashedValue); if (! is_int($options['cost'] ?? null)) { return false; } if ($options['cost'] > $this->rounds) { return false; } return true; } /** * Set the default password work factor. * * @param int $rounds * @return $this */ public function setRounds($rounds) { $this->rounds = (int) $rounds; return $this; } /** * Extract the cost value from the options array. * * @param array $options * @return int */ protected function cost(array $options = []) { return $options['rounds'] ?? $this->rounds; } } src/Illuminate/Hashing/Argon2IdHasher.php 0000644 00000002306 15107327037 0014276 0 ustar 00 <?php namespace Illuminate\Hashing; use RuntimeException; class Argon2IdHasher extends ArgonHasher { /** * Check the given plain value against a hash. * * @param string $value * @param string|null $hashedValue * @param array $options * @return bool * * @throws \RuntimeException */ public function check($value, $hashedValue, array $options = []) { if ($this->verifyAlgorithm && ! $this->isUsingCorrectAlgorithm($hashedValue)) { throw new RuntimeException('This password does not use the Argon2id algorithm.'); } if (is_null($hashedValue) || strlen($hashedValue) === 0) { return false; } return password_verify($value, $hashedValue); } /** * Verify the hashed value's algorithm. * * @param string $hashedValue * @return bool */ protected function isUsingCorrectAlgorithm($hashedValue) { return $this->info($hashedValue)['algoName'] === 'argon2id'; } /** * Get the algorithm that should be used for hashing. * * @return int */ protected function algorithm() { return PASSWORD_ARGON2ID; } } src/Illuminate/Hashing/composer.json 0000644 00000001465 15107327037 0013554 0 ustar 00 { "name": "illuminate/hashing", "description": "The Illuminate Hashing package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/contracts": "^10.0", "illuminate/support": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Hashing\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Hashing/HashServiceProvider.php 0000644 00000001353 15107327037 0015456 0 ustar 00 <?php namespace Illuminate\Hashing; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; class HashServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton('hash', function ($app) { return new HashManager($app); }); $this->app->singleton('hash.driver', function ($app) { return $app['hash']->driver(); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return ['hash', 'hash.driver']; } } src/Illuminate/Hashing/LICENSE.md 0000644 00000002063 15107327037 0012431 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Hashing/ArgonHasher.php 0000644 00000013134 15107327037 0013740 0 ustar 00 <?php namespace Illuminate\Hashing; use Illuminate\Contracts\Hashing\Hasher as HasherContract; use RuntimeException; class ArgonHasher extends AbstractHasher implements HasherContract { /** * The default memory cost factor. * * @var int */ protected $memory = 1024; /** * The default time cost factor. * * @var int */ protected $time = 2; /** * The default threads factor. * * @var int */ protected $threads = 2; /** * Indicates whether to perform an algorithm check. * * @var bool */ protected $verifyAlgorithm = false; /** * Create a new hasher instance. * * @param array $options * @return void */ public function __construct(array $options = []) { $this->time = $options['time'] ?? $this->time; $this->memory = $options['memory'] ?? $this->memory; $this->threads = $this->threads($options); $this->verifyAlgorithm = $options['verify'] ?? $this->verifyAlgorithm; } /** * Hash the given value. * * @param string $value * @param array $options * @return string * * @throws \RuntimeException */ public function make($value, array $options = []) { $hash = @password_hash($value, $this->algorithm(), [ 'memory_cost' => $this->memory($options), 'time_cost' => $this->time($options), 'threads' => $this->threads($options), ]); if (! is_string($hash)) { throw new RuntimeException('Argon2 hashing not supported.'); } return $hash; } /** * Get the algorithm that should be used for hashing. * * @return int */ protected function algorithm() { return PASSWORD_ARGON2I; } /** * Check the given plain value against a hash. * * @param string $value * @param string $hashedValue * @param array $options * @return bool * * @throws \RuntimeException */ public function check($value, $hashedValue, array $options = []) { if ($this->verifyAlgorithm && ! $this->isUsingCorrectAlgorithm($hashedValue)) { throw new RuntimeException('This password does not use the Argon2i algorithm.'); } return parent::check($value, $hashedValue, $options); } /** * Check if the given hash has been hashed using the given options. * * @param string $hashedValue * @param array $options * @return bool */ public function needsRehash($hashedValue, array $options = []) { return password_needs_rehash($hashedValue, $this->algorithm(), [ 'memory_cost' => $this->memory($options), 'time_cost' => $this->time($options), 'threads' => $this->threads($options), ]); } /** * Verifies that the configuration is less than or equal to what is configured. * * @internal */ public function verifyConfiguration($value) { return $this->isUsingCorrectAlgorithm($value) && $this->isUsingValidOptions($value); } /** * Verify the hashed value's algorithm. * * @param string $hashedValue * @return bool */ protected function isUsingCorrectAlgorithm($hashedValue) { return $this->info($hashedValue)['algoName'] === 'argon2i'; } /** * Verify the hashed value's options. * * @param string $hashedValue * @return bool */ protected function isUsingValidOptions($hashedValue) { ['options' => $options] = $this->info($hashedValue); if ( ! is_int($options['memory_cost'] ?? null) || ! is_int($options['time_cost'] ?? null) || ! is_int($options['threads'] ?? null) ) { return false; } if ( $options['memory_cost'] > $this->memory || $options['time_cost'] > $this->time || $options['threads'] > $this->threads ) { return false; } return true; } /** * Set the default password memory factor. * * @param int $memory * @return $this */ public function setMemory(int $memory) { $this->memory = $memory; return $this; } /** * Set the default password timing factor. * * @param int $time * @return $this */ public function setTime(int $time) { $this->time = $time; return $this; } /** * Set the default password threads factor. * * @param int $threads * @return $this */ public function setThreads(int $threads) { $this->threads = $threads; return $this; } /** * Extract the memory cost value from the options array. * * @param array $options * @return int */ protected function memory(array $options) { return $options['memory'] ?? $this->memory; } /** * Extract the time cost value from the options array. * * @param array $options * @return int */ protected function time(array $options) { return $options['time'] ?? $this->time; } /** * Extract the thread's value from the options array. * * @param array $options * @return int */ protected function threads(array $options) { if (defined('PASSWORD_ARGON2_PROVIDER') && PASSWORD_ARGON2_PROVIDER === 'sodium') { return 1; } return $options['threads'] ?? $this->threads; } } src/Illuminate/Routing/Redirector.php 0000644 00000016271 15107327037 0013714 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Http\RedirectResponse; use Illuminate\Session\Store as SessionStore; use Illuminate\Support\Traits\Macroable; class Redirector { use Macroable; /** * The URL generator instance. * * @var \Illuminate\Routing\UrlGenerator */ protected $generator; /** * The session store instance. * * @var \Illuminate\Session\Store */ protected $session; /** * Create a new Redirector instance. * * @param \Illuminate\Routing\UrlGenerator $generator * @return void */ public function __construct(UrlGenerator $generator) { $this->generator = $generator; } /** * Create a new redirect response to the previous location. * * @param int $status * @param array $headers * @param mixed $fallback * @return \Illuminate\Http\RedirectResponse */ public function back($status = 302, $headers = [], $fallback = false) { return $this->createRedirect($this->generator->previous($fallback), $status, $headers); } /** * Create a new redirect response to the current URI. * * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function refresh($status = 302, $headers = []) { return $this->to($this->generator->getRequest()->path(), $status, $headers); } /** * Create a new redirect response, while putting the current URL in the session. * * @param string $path * @param int $status * @param array $headers * @param bool|null $secure * @return \Illuminate\Http\RedirectResponse */ public function guest($path, $status = 302, $headers = [], $secure = null) { $request = $this->generator->getRequest(); $intended = $request->isMethod('GET') && $request->route() && ! $request->expectsJson() ? $this->generator->full() : $this->generator->previous(); if ($intended) { $this->setIntendedUrl($intended); } return $this->to($path, $status, $headers, $secure); } /** * Create a new redirect response to the previously intended location. * * @param mixed $default * @param int $status * @param array $headers * @param bool|null $secure * @return \Illuminate\Http\RedirectResponse */ public function intended($default = '/', $status = 302, $headers = [], $secure = null) { $path = $this->session->pull('url.intended', $default); return $this->to($path, $status, $headers, $secure); } /** * Create a new redirect response to the given path. * * @param string $path * @param int $status * @param array $headers * @param bool|null $secure * @return \Illuminate\Http\RedirectResponse */ public function to($path, $status = 302, $headers = [], $secure = null) { return $this->createRedirect($this->generator->to($path, [], $secure), $status, $headers); } /** * Create a new redirect response to an external URL (no validation). * * @param string $path * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function away($path, $status = 302, $headers = []) { return $this->createRedirect($path, $status, $headers); } /** * Create a new redirect response to the given HTTPS path. * * @param string $path * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function secure($path, $status = 302, $headers = []) { return $this->to($path, $status, $headers, true); } /** * Create a new redirect response to a named route. * * @param string $route * @param mixed $parameters * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function route($route, $parameters = [], $status = 302, $headers = []) { return $this->to($this->generator->route($route, $parameters), $status, $headers); } /** * Create a new redirect response to a signed named route. * * @param string $route * @param mixed $parameters * @param \DateTimeInterface|\DateInterval|int|null $expiration * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function signedRoute($route, $parameters = [], $expiration = null, $status = 302, $headers = []) { return $this->to($this->generator->signedRoute($route, $parameters, $expiration), $status, $headers); } /** * Create a new redirect response to a signed named route. * * @param string $route * @param \DateTimeInterface|\DateInterval|int|null $expiration * @param mixed $parameters * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function temporarySignedRoute($route, $expiration, $parameters = [], $status = 302, $headers = []) { return $this->to($this->generator->temporarySignedRoute($route, $expiration, $parameters), $status, $headers); } /** * Create a new redirect response to a controller action. * * @param string|array $action * @param mixed $parameters * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function action($action, $parameters = [], $status = 302, $headers = []) { return $this->to($this->generator->action($action, $parameters), $status, $headers); } /** * Create a new redirect response. * * @param string $path * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ protected function createRedirect($path, $status, $headers) { return tap(new RedirectResponse($path, $status, $headers), function ($redirect) { if (isset($this->session)) { $redirect->setSession($this->session); } $redirect->setRequest($this->generator->getRequest()); }); } /** * Get the URL generator instance. * * @return \Illuminate\Routing\UrlGenerator */ public function getUrlGenerator() { return $this->generator; } /** * Set the active session store. * * @param \Illuminate\Session\Store $session * @return void */ public function setSession(SessionStore $session) { $this->session = $session; } /** * Get the "intended" URL from the session. * * @return string|null */ public function getIntendedUrl() { return $this->session->get('url.intended'); } /** * Set the "intended" URL in the session. * * @param string $url * @return $this */ public function setIntendedUrl($url) { $this->session->put('url.intended', $url); return $this; } } src/Illuminate/Routing/RouteRegistrar.php 0000644 00000020453 15107327037 0014570 0 ustar 00 <?php namespace Illuminate\Routing; use BadMethodCallException; use Closure; use Illuminate\Support\Arr; use Illuminate\Support\Reflector; use InvalidArgumentException; /** * @method \Illuminate\Routing\Route any(string $uri, \Closure|array|string|null $action = null) * @method \Illuminate\Routing\Route delete(string $uri, \Closure|array|string|null $action = null) * @method \Illuminate\Routing\Route get(string $uri, \Closure|array|string|null $action = null) * @method \Illuminate\Routing\Route options(string $uri, \Closure|array|string|null $action = null) * @method \Illuminate\Routing\Route patch(string $uri, \Closure|array|string|null $action = null) * @method \Illuminate\Routing\Route post(string $uri, \Closure|array|string|null $action = null) * @method \Illuminate\Routing\Route put(string $uri, \Closure|array|string|null $action = null) * @method \Illuminate\Routing\RouteRegistrar as(string $value) * @method \Illuminate\Routing\RouteRegistrar controller(string $controller) * @method \Illuminate\Routing\RouteRegistrar domain(string $value) * @method \Illuminate\Routing\RouteRegistrar middleware(array|string|null $middleware) * @method \Illuminate\Routing\RouteRegistrar name(string $value) * @method \Illuminate\Routing\RouteRegistrar namespace(string|null $value) * @method \Illuminate\Routing\RouteRegistrar prefix(string $prefix) * @method \Illuminate\Routing\RouteRegistrar scopeBindings() * @method \Illuminate\Routing\RouteRegistrar where(array $where) * @method \Illuminate\Routing\RouteRegistrar withoutMiddleware(array|string $middleware) * @method \Illuminate\Routing\RouteRegistrar withoutScopedBindings() */ class RouteRegistrar { use CreatesRegularExpressionRouteConstraints; /** * The router instance. * * @var \Illuminate\Routing\Router */ protected $router; /** * The attributes to pass on to the router. * * @var array */ protected $attributes = []; /** * The methods to dynamically pass through to the router. * * @var string[] */ protected $passthru = [ 'get', 'post', 'put', 'patch', 'delete', 'options', 'any', ]; /** * The attributes that can be set through this class. * * @var string[] */ protected $allowedAttributes = [ 'as', 'controller', 'domain', 'middleware', 'name', 'namespace', 'prefix', 'scopeBindings', 'where', 'withoutMiddleware', ]; /** * The attributes that are aliased. * * @var array */ protected $aliases = [ 'name' => 'as', 'scopeBindings' => 'scope_bindings', 'withoutMiddleware' => 'excluded_middleware', ]; /** * Create a new route registrar instance. * * @param \Illuminate\Routing\Router $router * @return void */ public function __construct(Router $router) { $this->router = $router; } /** * Set the value for a given attribute. * * @param string $key * @param mixed $value * @return $this * * @throws \InvalidArgumentException */ public function attribute($key, $value) { if (! in_array($key, $this->allowedAttributes)) { throw new InvalidArgumentException("Attribute [{$key}] does not exist."); } if ($key === 'middleware') { foreach ($value as $index => $middleware) { $value[$index] = (string) $middleware; } } $attributeKey = Arr::get($this->aliases, $key, $key); if ($key === 'withoutMiddleware') { $value = array_merge( (array) ($this->attributes[$attributeKey] ?? []), Arr::wrap($value) ); } $this->attributes[$attributeKey] = $value; return $this; } /** * Route a resource to a controller. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\PendingResourceRegistration */ public function resource($name, $controller, array $options = []) { return $this->router->resource($name, $controller, $this->attributes + $options); } /** * Route an API resource to a controller. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\PendingResourceRegistration */ public function apiResource($name, $controller, array $options = []) { return $this->router->apiResource($name, $controller, $this->attributes + $options); } /** * Route a singleton resource to a controller. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\PendingSingletonResourceRegistration */ public function singleton($name, $controller, array $options = []) { return $this->router->singleton($name, $controller, $this->attributes + $options); } /** * Route an API singleton resource to a controller. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\PendingSingletonResourceRegistration */ public function apiSingleton($name, $controller, array $options = []) { return $this->router->apiSingleton($name, $controller, $this->attributes + $options); } /** * Create a route group with shared attributes. * * @param \Closure|array|string $callback * @return $this */ public function group($callback) { $this->router->group($this->attributes, $callback); return $this; } /** * Register a new route with the given verbs. * * @param array|string $methods * @param string $uri * @param \Closure|array|string|null $action * @return \Illuminate\Routing\Route */ public function match($methods, $uri, $action = null) { return $this->router->match($methods, $uri, $this->compileAction($action)); } /** * Register a new route with the router. * * @param string $method * @param string $uri * @param \Closure|array|string|null $action * @return \Illuminate\Routing\Route */ protected function registerRoute($method, $uri, $action = null) { if (! is_array($action)) { $action = array_merge($this->attributes, $action ? ['uses' => $action] : []); } return $this->router->{$method}($uri, $this->compileAction($action)); } /** * Compile the action into an array including the attributes. * * @param \Closure|array|string|null $action * @return array */ protected function compileAction($action) { if (is_null($action)) { return $this->attributes; } if (is_string($action) || $action instanceof Closure) { $action = ['uses' => $action]; } if (is_array($action) && array_is_list($action) && Reflector::isCallable($action)) { if (strncmp($action[0], '\\', 1)) { $action[0] = '\\'.$action[0]; } $action = [ 'uses' => $action[0].'@'.$action[1], 'controller' => $action[0].'@'.$action[1], ]; } return array_merge($this->attributes, $action); } /** * Dynamically handle calls into the route registrar. * * @param string $method * @param array $parameters * @return \Illuminate\Routing\Route|$this * * @throws \BadMethodCallException */ public function __call($method, $parameters) { if (in_array($method, $this->passthru)) { return $this->registerRoute($method, ...$parameters); } if (in_array($method, $this->allowedAttributes)) { if ($method === 'middleware') { return $this->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters); } return $this->attribute($method, array_key_exists(0, $parameters) ? $parameters[0] : true); } throw new BadMethodCallException(sprintf( 'Method %s::%s does not exist.', static::class, $method )); } } src/Illuminate/Routing/RouteDependencyResolverTrait.php 0000644 00000000207 15107327037 0017425 0 ustar 00 <?php namespace Illuminate\Routing; /** * @deprecated */ trait RouteDependencyResolverTrait { use ResolvesRouteDependencies; } src/Illuminate/Routing/Controller.php 0000644 00000003174 15107327037 0013733 0 ustar 00 <?php namespace Illuminate\Routing; use BadMethodCallException; abstract class Controller { /** * The middleware registered on the controller. * * @var array */ protected $middleware = []; /** * Register middleware on the controller. * * @param \Closure|array|string $middleware * @param array $options * @return \Illuminate\Routing\ControllerMiddlewareOptions */ public function middleware($middleware, array $options = []) { foreach ((array) $middleware as $m) { $this->middleware[] = [ 'middleware' => $m, 'options' => &$options, ]; } return new ControllerMiddlewareOptions($options); } /** * Get the middleware assigned to the controller. * * @return array */ public function getMiddleware() { return $this->middleware; } /** * Execute an action on the controller. * * @param string $method * @param array $parameters * @return \Symfony\Component\HttpFoundation\Response */ public function callAction($method, $parameters) { return $this->{$method}(...array_values($parameters)); } /** * Handle calls to missing methods on the controller. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public function __call($method, $parameters) { throw new BadMethodCallException(sprintf( 'Method %s::%s does not exist.', static::class, $method )); } } src/Illuminate/Routing/Contracts/CallableDispatcher.php 0000644 00000000526 15107327037 0017254 0 ustar 00 <?php namespace Illuminate\Routing\Contracts; use Illuminate\Routing\Route; interface CallableDispatcher { /** * Dispatch a request to a given callable. * * @param \Illuminate\Routing\Route $route * @param callable $callable * @return mixed */ public function dispatch(Route $route, $callable); } src/Illuminate/Routing/Contracts/ControllerDispatcher.php 0000644 00000001205 15107327037 0017673 0 ustar 00 <?php namespace Illuminate\Routing\Contracts; use Illuminate\Routing\Route; interface ControllerDispatcher { /** * Dispatch a request to a given controller and method. * * @param \Illuminate\Routing\Route $route * @param mixed $controller * @param string $method * @return mixed */ public function dispatch(Route $route, $controller, $method); /** * Get the middleware for the controller instance. * * @param \Illuminate\Routing\Controller $controller * @param string $method * @return array */ public function getMiddleware($controller, $method); } src/Illuminate/Routing/Console/ControllerMakeCommand.php 0000644 00000025701 15107327037 0017432 0 ustar 00 <?php namespace Illuminate\Routing\Console; use Illuminate\Console\Concerns\CreatesMatchingTest; use Illuminate\Console\GeneratorCommand; use InvalidArgumentException; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use function Laravel\Prompts\confirm; use function Laravel\Prompts\select; use function Laravel\Prompts\suggest; #[AsCommand(name: 'make:controller')] class ControllerMakeCommand extends GeneratorCommand { use CreatesMatchingTest; /** * The console command name. * * @var string */ protected $name = 'make:controller'; /** * The console command description. * * @var string */ protected $description = 'Create a new controller class'; /** * The type of class being generated. * * @var string */ protected $type = 'Controller'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { $stub = null; if ($type = $this->option('type')) { $stub = "/stubs/controller.{$type}.stub"; } elseif ($this->option('parent')) { $stub = $this->option('singleton') ? '/stubs/controller.nested.singleton.stub' : '/stubs/controller.nested.stub'; } elseif ($this->option('model')) { $stub = '/stubs/controller.model.stub'; } elseif ($this->option('invokable')) { $stub = '/stubs/controller.invokable.stub'; } elseif ($this->option('singleton')) { $stub = '/stubs/controller.singleton.stub'; } elseif ($this->option('resource')) { $stub = '/stubs/controller.stub'; } if ($this->option('api') && is_null($stub)) { $stub = '/stubs/controller.api.stub'; } elseif ($this->option('api') && ! is_null($stub) && ! $this->option('invokable')) { $stub = str_replace('.stub', '.api.stub', $stub); } $stub ??= '/stubs/controller.plain.stub'; return $this->resolveStubPath($stub); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Http\Controllers'; } /** * Build the class with the given name. * * Remove the base controller import if we are already in the base namespace. * * @param string $name * @return string */ protected function buildClass($name) { $controllerNamespace = $this->getNamespace($name); $replace = []; if ($this->option('parent')) { $replace = $this->buildParentReplacements(); } if ($this->option('model')) { $replace = $this->buildModelReplacements($replace); } if ($this->option('creatable')) { $replace['abort(404);'] = '//'; } $replace["use {$controllerNamespace}\Controller;\n"] = ''; return str_replace( array_keys($replace), array_values($replace), parent::buildClass($name) ); } /** * Build the replacements for a parent controller. * * @return array */ protected function buildParentReplacements() { $parentModelClass = $this->parseModel($this->option('parent')); if (! class_exists($parentModelClass) && confirm("A {$parentModelClass} model does not exist. Do you want to generate it?", default: true)) { $this->call('make:model', ['name' => $parentModelClass]); } return [ 'ParentDummyFullModelClass' => $parentModelClass, '{{ namespacedParentModel }}' => $parentModelClass, '{{namespacedParentModel}}' => $parentModelClass, 'ParentDummyModelClass' => class_basename($parentModelClass), '{{ parentModel }}' => class_basename($parentModelClass), '{{parentModel}}' => class_basename($parentModelClass), 'ParentDummyModelVariable' => lcfirst(class_basename($parentModelClass)), '{{ parentModelVariable }}' => lcfirst(class_basename($parentModelClass)), '{{parentModelVariable}}' => lcfirst(class_basename($parentModelClass)), ]; } /** * Build the model replacement values. * * @param array $replace * @return array */ protected function buildModelReplacements(array $replace) { $modelClass = $this->parseModel($this->option('model')); if (! class_exists($modelClass) && confirm("A {$modelClass} model does not exist. Do you want to generate it?", default: true)) { $this->call('make:model', ['name' => $modelClass]); } $replace = $this->buildFormRequestReplacements($replace, $modelClass); return array_merge($replace, [ 'DummyFullModelClass' => $modelClass, '{{ namespacedModel }}' => $modelClass, '{{namespacedModel}}' => $modelClass, 'DummyModelClass' => class_basename($modelClass), '{{ model }}' => class_basename($modelClass), '{{model}}' => class_basename($modelClass), 'DummyModelVariable' => lcfirst(class_basename($modelClass)), '{{ modelVariable }}' => lcfirst(class_basename($modelClass)), '{{modelVariable}}' => lcfirst(class_basename($modelClass)), ]); } /** * Get the fully-qualified model class name. * * @param string $model * @return string * * @throws \InvalidArgumentException */ protected function parseModel($model) { if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) { throw new InvalidArgumentException('Model name contains invalid characters.'); } return $this->qualifyModel($model); } /** * Build the model replacement values. * * @param array $replace * @param string $modelClass * @return array */ protected function buildFormRequestReplacements(array $replace, $modelClass) { [$namespace, $storeRequestClass, $updateRequestClass] = [ 'Illuminate\\Http', 'Request', 'Request', ]; if ($this->option('requests')) { $namespace = 'App\\Http\\Requests'; [$storeRequestClass, $updateRequestClass] = $this->generateFormRequests( $modelClass, $storeRequestClass, $updateRequestClass ); } $namespacedRequests = $namespace.'\\'.$storeRequestClass.';'; if ($storeRequestClass !== $updateRequestClass) { $namespacedRequests .= PHP_EOL.'use '.$namespace.'\\'.$updateRequestClass.';'; } return array_merge($replace, [ '{{ storeRequest }}' => $storeRequestClass, '{{storeRequest}}' => $storeRequestClass, '{{ updateRequest }}' => $updateRequestClass, '{{updateRequest}}' => $updateRequestClass, '{{ namespacedStoreRequest }}' => $namespace.'\\'.$storeRequestClass, '{{namespacedStoreRequest}}' => $namespace.'\\'.$storeRequestClass, '{{ namespacedUpdateRequest }}' => $namespace.'\\'.$updateRequestClass, '{{namespacedUpdateRequest}}' => $namespace.'\\'.$updateRequestClass, '{{ namespacedRequests }}' => $namespacedRequests, '{{namespacedRequests}}' => $namespacedRequests, ]); } /** * Generate the form requests for the given model and classes. * * @param string $modelClass * @param string $storeRequestClass * @param string $updateRequestClass * @return array */ protected function generateFormRequests($modelClass, $storeRequestClass, $updateRequestClass) { $storeRequestClass = 'Store'.class_basename($modelClass).'Request'; $this->call('make:request', [ 'name' => $storeRequestClass, ]); $updateRequestClass = 'Update'.class_basename($modelClass).'Request'; $this->call('make:request', [ 'name' => $updateRequestClass, ]); return [$storeRequestClass, $updateRequestClass]; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['api', null, InputOption::VALUE_NONE, 'Exclude the create and edit methods from the controller'], ['type', null, InputOption::VALUE_REQUIRED, 'Manually specify the controller stub file to use'], ['force', null, InputOption::VALUE_NONE, 'Create the class even if the controller already exists'], ['invokable', 'i', InputOption::VALUE_NONE, 'Generate a single method, invokable controller class'], ['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a resource controller for the given model'], ['parent', 'p', InputOption::VALUE_OPTIONAL, 'Generate a nested resource controller class'], ['resource', 'r', InputOption::VALUE_NONE, 'Generate a resource controller class'], ['requests', 'R', InputOption::VALUE_NONE, 'Generate FormRequest classes for store and update'], ['singleton', 's', InputOption::VALUE_NONE, 'Generate a singleton resource controller class'], ['creatable', null, InputOption::VALUE_NONE, 'Indicate that a singleton resource should be creatable'], ]; } /** * Interact further with the user if they were prompted for missing arguments. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) { if ($this->didReceiveOptions($input)) { return; } $type = select('Which type of controller would you like?', [ 'empty' => 'Empty', 'resource' => 'Resource', 'singleton' => 'Singleton', 'api' => 'API', 'invokable' => 'Invokable', ]); if ($type !== 'empty') { $input->setOption($type, true); } if (in_array($type, ['api', 'resource', 'singleton'])) { $model = suggest( "What model should this $type controller be for? (Optional)", $this->possibleModels() ); if ($model) { $input->setOption('model', $model); } } } } src/Illuminate/Routing/Console/MiddlewareMakeCommand.php 0000644 00000002706 15107327037 0017364 0 ustar 00 <?php namespace Illuminate\Routing\Console; use Illuminate\Console\Concerns\CreatesMatchingTest; use Illuminate\Console\GeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'make:middleware')] class MiddlewareMakeCommand extends GeneratorCommand { use CreatesMatchingTest; /** * The console command name. * * @var string */ protected $name = 'make:middleware'; /** * The console command description. * * @var string */ protected $description = 'Create a new middleware class'; /** * The type of class being generated. * * @var string */ protected $type = 'Middleware'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->resolveStubPath('/stubs/middleware.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Http\Middleware'; } } src/Illuminate/Routing/Console/stubs/controller.invokable.stub 0000644 00000000430 15107327037 0020664 0 ustar 00 <?php namespace {{ namespace }}; use {{ rootNamespace }}Http\Controllers\Controller; use Illuminate\Http\Request; class {{ class }} extends Controller { /** * Handle the incoming request. */ public function __invoke(Request $request) { // } } src/Illuminate/Routing/Console/stubs/controller.nested.singleton.stub 0000644 00000002323 15107327037 0022200 0 ustar 00 <?php namespace {{ namespace }}; use {{ namespacedModel }}; use {{ rootNamespace }}Http\Controllers\Controller; use Illuminate\Http\Request; use {{ namespacedParentModel }}; class {{ class }} extends Controller { /** * Show the form for creating the new resource. */ public function create({{ parentModel }} ${{ parentModelVariable }}): never { abort(404); } /** * Store the newly created resource in storage. */ public function store(Request $request, {{ parentModel }} ${{ parentModelVariable }}): never { abort(404); } /** * Display the resource. */ public function show({{ parentModel }} ${{ parentModelVariable }}) { // } /** * Show the form for editing the resource. */ public function edit({{ parentModel }} ${{ parentModelVariable }}) { // } /** * Update the resource in storage. */ public function update(Request $request, {{ parentModel }} ${{ parentModelVariable }}) { // } /** * Remove the resource from storage. */ public function destroy({{ parentModel }} ${{ parentModelVariable }}): never { abort(404); } } src/Illuminate/Routing/Console/stubs/controller.plain.stub 0000644 00000000245 15107327037 0020021 0 ustar 00 <?php namespace {{ namespace }}; use {{ rootNamespace }}Http\Controllers\Controller; use Illuminate\Http\Request; class {{ class }} extends Controller { // } src/Illuminate/Routing/Console/stubs/controller.stub 0000644 00000002027 15107327037 0016717 0 ustar 00 <?php namespace {{ namespace }}; use {{ rootNamespace }}Http\Controllers\Controller; use Illuminate\Http\Request; class {{ class }} extends Controller { /** * Display a listing of the resource. */ public function index() { // } /** * Show the form for creating a new resource. */ public function create() { // } /** * Store a newly created resource in storage. */ public function store(Request $request) { // } /** * Display the specified resource. */ public function show(string $id) { // } /** * Show the form for editing the specified resource. */ public function edit(string $id) { // } /** * Update the specified resource in storage. */ public function update(Request $request, string $id) { // } /** * Remove the specified resource from storage. */ public function destroy(string $id) { // } } src/Illuminate/Routing/Console/stubs/controller.nested.singleton.api.stub 0000644 00000001600 15107327037 0022745 0 ustar 00 <?php namespace {{ namespace }}; use {{ namespacedModel }}; use {{ rootNamespace }}Http\Controllers\Controller; use Illuminate\Http\Request; use {{ namespacedParentModel }}; class {{ class }} extends Controller { /** * Store the newly created resource in storage. */ public function store(Request $request, {{ parentModel }} ${{ parentModelVariable }}): never { abort(404); } /** * Display the resource. */ public function show({{ parentModel }} ${{ parentModelVariable }}) { // } /** * Update the resource in storage. */ public function update(Request $request, {{ parentModel }} ${{ parentModelVariable }}) { // } /** * Remove the resource from storage. */ public function destroy({{ parentModel }} ${{ parentModelVariable }}): never { abort(404); } } src/Illuminate/Routing/Console/stubs/controller.nested.api.stub 0000644 00000002174 15107327037 0020753 0 ustar 00 <?php namespace {{ namespace }}; use {{ namespacedModel }}; use {{ rootNamespace }}Http\Controllers\Controller; use {{ namespacedParentModel }}; use Illuminate\Http\Request; class {{ class }} extends Controller { /** * Display a listing of the resource. */ public function index({{ parentModel }} ${{ parentModelVariable }}) { // } /** * Store a newly created resource in storage. */ public function store(Request $request, {{ parentModel }} ${{ parentModelVariable }}) { // } /** * Display the specified resource. */ public function show({{ parentModel }} ${{ parentModelVariable }}, {{ model }} ${{ modelVariable }}) { // } /** * Update the specified resource in storage. */ public function update(Request $request, {{ parentModel }} ${{ parentModelVariable }}, {{ model }} ${{ modelVariable }}) { // } /** * Remove the specified resource from storage. */ public function destroy({{ parentModel }} ${{ parentModelVariable }}, {{ model }} ${{ modelVariable }}) { // } } src/Illuminate/Routing/Console/stubs/controller.singleton.stub 0000644 00000001607 15107327037 0020723 0 ustar 00 <?php namespace {{ namespace }}; use {{ rootNamespace }}Http\Controllers\Controller; use Illuminate\Http\Request; class {{ class }} extends Controller { /** * Show the form for creating the resource. */ public function create(): never { abort(404); } /** * Store the newly created resource in storage. */ public function store(Request $request): never { abort(404); } /** * Display the resource. */ public function show() { // } /** * Show the form for editing the resource. */ public function edit() { // } /** * Update the resource in storage. */ public function update(Request $request) { // } /** * Remove the resource from storage. */ public function destroy(): never { abort(404); } } src/Illuminate/Routing/Console/stubs/controller.api.stub 0000644 00000001432 15107327037 0017466 0 ustar 00 <?php namespace {{ namespace }}; use {{ rootNamespace }}Http\Controllers\Controller; use Illuminate\Http\Request; class {{ class }} extends Controller { /** * Display a listing of the resource. */ public function index() { // } /** * Store a newly created resource in storage. */ public function store(Request $request) { // } /** * Display the specified resource. */ public function show(string $id) { // } /** * Update the specified resource in storage. */ public function update(Request $request, string $id) { // } /** * Remove the specified resource from storage. */ public function destroy(string $id) { // } } src/Illuminate/Routing/Console/stubs/controller.model.stub 0000644 00000002241 15107327037 0020014 0 ustar 00 <?php namespace {{ namespace }}; use {{ namespacedModel }}; use {{ rootNamespace }}Http\Controllers\Controller; use {{ namespacedRequests }} class {{ class }} extends Controller { /** * Display a listing of the resource. */ public function index() { // } /** * Show the form for creating a new resource. */ public function create() { // } /** * Store a newly created resource in storage. */ public function store({{ storeRequest }} $request) { // } /** * Display the specified resource. */ public function show({{ model }} ${{ modelVariable }}) { // } /** * Show the form for editing the specified resource. */ public function edit({{ model }} ${{ modelVariable }}) { // } /** * Update the specified resource in storage. */ public function update({{ updateRequest }} $request, {{ model }} ${{ modelVariable }}) { // } /** * Remove the specified resource from storage. */ public function destroy({{ model }} ${{ modelVariable }}) { // } } src/Illuminate/Routing/Console/stubs/controller.model.api.stub 0000644 00000001616 15107327037 0020571 0 ustar 00 <?php namespace {{ namespace }}; use {{ namespacedModel }}; use {{ rootNamespace }}Http\Controllers\Controller; use {{ namespacedRequests }} class {{ class }} extends Controller { /** * Display a listing of the resource. */ public function index() { // } /** * Store a newly created resource in storage. */ public function store({{ storeRequest }} $request) { // } /** * Display the specified resource. */ public function show({{ model }} ${{ modelVariable }}) { // } /** * Update the specified resource in storage. */ public function update({{ updateRequest }} $request, {{ model }} ${{ modelVariable }}) { // } /** * Remove the specified resource from storage. */ public function destroy({{ model }} ${{ modelVariable }}) { // } } src/Illuminate/Routing/Console/stubs/controller.singleton.api.stub 0000644 00000001220 15107327037 0021462 0 ustar 00 <?php namespace {{ namespace }}; use {{ rootNamespace }}Http\Controllers\Controller; use Illuminate\Http\Request; class {{ class }} extends Controller { /** * Store the newly created resource in storage. */ public function store(Request $request): never { abort(404); } /** * Display the resource. */ public function show() { // } /** * Update the resource in storage. */ public function update(Request $request) { // } /** * Remove the resource from storage. */ public function destroy(): never { abort(404); } } src/Illuminate/Routing/Console/stubs/controller.nested.stub 0000644 00000002751 15107327037 0020204 0 ustar 00 <?php namespace {{ namespace }}; use {{ namespacedModel }}; use {{ rootNamespace }}Http\Controllers\Controller; use {{ namespacedParentModel }}; use Illuminate\Http\Request; class {{ class }} extends Controller { /** * Display a listing of the resource. */ public function index({{ parentModel }} ${{ parentModelVariable }}) { // } /** * Show the form for creating a new resource. */ public function create({{ parentModel }} ${{ parentModelVariable }}) { // } /** * Store a newly created resource in storage. */ public function store(Request $request, {{ parentModel }} ${{ parentModelVariable }}) { // } /** * Display the specified resource. */ public function show({{ parentModel }} ${{ parentModelVariable }}, {{ model }} ${{ modelVariable }}) { // } /** * Show the form for editing the specified resource. */ public function edit({{ parentModel }} ${{ parentModelVariable }}, {{ model }} ${{ modelVariable }}) { // } /** * Update the specified resource in storage. */ public function update(Request $request, {{ parentModel }} ${{ parentModelVariable }}, {{ model }} ${{ modelVariable }}) { // } /** * Remove the specified resource from storage. */ public function destroy({{ parentModel }} ${{ parentModelVariable }}, {{ model }} ${{ modelVariable }}) { // } } src/Illuminate/Routing/Console/stubs/middleware.stub 0000644 00000000646 15107327037 0016656 0 ustar 00 <?php namespace {{ namespace }}; use Closure; use Illuminate\Http\Request; use Symfony\Component\HttpFoundation\Response; class {{ class }} { /** * Handle an incoming request. * * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next */ public function handle(Request $request, Closure $next): Response { return $next($request); } } src/Illuminate/Routing/Exceptions/InvalidSignatureException.php 0000644 00000000537 15107327037 0021060 0 ustar 00 <?php namespace Illuminate\Routing\Exceptions; use Symfony\Component\HttpKernel\Exception\HttpException; class InvalidSignatureException extends HttpException { /** * Create a new exception instance. * * @return void */ public function __construct() { parent::__construct(403, 'Invalid signature.'); } } src/Illuminate/Routing/Exceptions/StreamedResponseException.php 0000644 00000001751 15107327037 0021072 0 ustar 00 <?php namespace Illuminate\Routing\Exceptions; use Illuminate\Http\Response; use RuntimeException; use Throwable; class StreamedResponseException extends RuntimeException { /** * The actual exception thrown during the stream. * * @var \Throwable */ public $originalException; /** * Create a new exception instance. * * @param \Throwable $originalException * @return void */ public function __construct(Throwable $originalException) { $this->originalException = $originalException; parent::__construct($originalException->getMessage()); } /** * Render the exception. * * @return \Illuminate\Http\Response */ public function render() { return new Response(''); } /** * Get the actual exception thrown during the stream. * * @return \Throwable */ public function getInnerException() { return $this->originalException; } } src/Illuminate/Routing/Exceptions/UrlGenerationException.php 0000644 00000001643 15107327037 0020365 0 ustar 00 <?php namespace Illuminate\Routing\Exceptions; use Exception; use Illuminate\Routing\Route; use Illuminate\Support\Str; class UrlGenerationException extends Exception { /** * Create a new exception for missing route parameters. * * @param \Illuminate\Routing\Route $route * @param array $parameters * @return static */ public static function forMissingParameters(Route $route, array $parameters = []) { $parameterLabel = Str::plural('parameter', count($parameters)); $message = sprintf( 'Missing required %s for [Route: %s] [URI: %s]', $parameterLabel, $route->getName(), $route->uri() ); if (count($parameters) > 0) { $message .= sprintf(' [Missing %s: %s]', $parameterLabel, implode(', ', $parameters)); } $message .= '.'; return new static($message); } } src/Illuminate/Routing/Exceptions/BackedEnumCaseNotFoundException.php 0000644 00000000706 15107327037 0022055 0 ustar 00 <?php namespace Illuminate\Routing\Exceptions; use RuntimeException; class BackedEnumCaseNotFoundException extends RuntimeException { /** * Create a new exception instance. * * @param string $backedEnumClass * @param string $case * @return void */ public function __construct($backedEnumClass, $case) { parent::__construct("Case [{$case}] not found on Backed Enum [{$backedEnumClass}]."); } } src/Illuminate/Routing/SortedMiddleware.php 0000644 00000007405 15107327037 0015047 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Support\Collection; class SortedMiddleware extends Collection { /** * Create a new Sorted Middleware container. * * @param array $priorityMap * @param \Illuminate\Support\Collection|array $middlewares * @return void */ public function __construct(array $priorityMap, $middlewares) { if ($middlewares instanceof Collection) { $middlewares = $middlewares->all(); } $this->items = $this->sortMiddleware($priorityMap, $middlewares); } /** * Sort the middlewares by the given priority map. * * Each call to this method makes one discrete middleware movement if necessary. * * @param array $priorityMap * @param array $middlewares * @return array */ protected function sortMiddleware($priorityMap, $middlewares) { $lastIndex = 0; foreach ($middlewares as $index => $middleware) { if (! is_string($middleware)) { continue; } $priorityIndex = $this->priorityMapIndex($priorityMap, $middleware); if (! is_null($priorityIndex)) { // This middleware is in the priority map. If we have encountered another middleware // that was also in the priority map and was at a lower priority than the current // middleware, we will move this middleware to be above the previous encounter. if (isset($lastPriorityIndex) && $priorityIndex < $lastPriorityIndex) { return $this->sortMiddleware( $priorityMap, array_values($this->moveMiddleware($middlewares, $index, $lastIndex)) ); } // This middleware is in the priority map; but, this is the first middleware we have // encountered from the map thus far. We'll save its current index plus its index // from the priority map so we can compare against them on the next iterations. $lastIndex = $index; $lastPriorityIndex = $priorityIndex; } } return Router::uniqueMiddleware($middlewares); } /** * Calculate the priority map index of the middleware. * * @param array $priorityMap * @param string $middleware * @return int|null */ protected function priorityMapIndex($priorityMap, $middleware) { foreach ($this->middlewareNames($middleware) as $name) { $priorityIndex = array_search($name, $priorityMap); if ($priorityIndex !== false) { return $priorityIndex; } } } /** * Resolve the middleware names to look for in the priority array. * * @param string $middleware * @return \Generator */ protected function middlewareNames($middleware) { $stripped = head(explode(':', $middleware)); yield $stripped; $interfaces = @class_implements($stripped); if ($interfaces !== false) { foreach ($interfaces as $interface) { yield $interface; } } $parents = @class_parents($stripped); if ($parents !== false) { foreach ($parents as $parent) { yield $parent; } } } /** * Splice a middleware into a new position and remove the old entry. * * @param array $middlewares * @param int $from * @param int $to * @return array */ protected function moveMiddleware($middlewares, $from, $to) { array_splice($middlewares, $to, 0, $middlewares[$from]); unset($middlewares[$from + 1]); return $middlewares; } } src/Illuminate/Routing/Router.php 0000644 00000116325 15107327037 0013073 0 ustar 00 <?php namespace Illuminate\Routing; use ArrayObject; use Closure; use Illuminate\Container\Container; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Routing\BindingRegistrar; use Illuminate\Contracts\Routing\Registrar as RegistrarContract; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Contracts\Support\Responsable; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Routing\Events\PreparingResponse; use Illuminate\Routing\Events\ResponsePrepared; use Illuminate\Routing\Events\RouteMatched; use Illuminate\Routing\Events\Routing; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Illuminate\Support\Stringable; use Illuminate\Support\Traits\Macroable; use JsonSerializable; use Psr\Http\Message\ResponseInterface as PsrResponseInterface; use ReflectionClass; use stdClass; use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; /** * @mixin \Illuminate\Routing\RouteRegistrar */ class Router implements BindingRegistrar, RegistrarContract { use Macroable { __call as macroCall; } /** * The event dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * The IoC container instance. * * @var \Illuminate\Container\Container */ protected $container; /** * The route collection instance. * * @var \Illuminate\Routing\RouteCollectionInterface */ protected $routes; /** * The currently dispatched route instance. * * @var \Illuminate\Routing\Route|null */ protected $current; /** * The request currently being dispatched. * * @var \Illuminate\Http\Request */ protected $currentRequest; /** * All of the short-hand keys for middlewares. * * @var array */ protected $middleware = []; /** * All of the middleware groups. * * @var array */ protected $middlewareGroups = []; /** * The priority-sorted list of middleware. * * Forces the listed middleware to always be in the given order. * * @var array */ public $middlewarePriority = []; /** * The registered route value binders. * * @var array */ protected $binders = []; /** * The globally available parameter patterns. * * @var array */ protected $patterns = []; /** * The route group attribute stack. * * @var array */ protected $groupStack = []; /** * All of the verbs supported by the router. * * @var string[] */ public static $verbs = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']; /** * Create a new Router instance. * * @param \Illuminate\Contracts\Events\Dispatcher $events * @param \Illuminate\Container\Container|null $container * @return void */ public function __construct(Dispatcher $events, Container $container = null) { $this->events = $events; $this->routes = new RouteCollection; $this->container = $container ?: new Container; } /** * Register a new GET route with the router. * * @param string $uri * @param array|string|callable|null $action * @return \Illuminate\Routing\Route */ public function get($uri, $action = null) { return $this->addRoute(['GET', 'HEAD'], $uri, $action); } /** * Register a new POST route with the router. * * @param string $uri * @param array|string|callable|null $action * @return \Illuminate\Routing\Route */ public function post($uri, $action = null) { return $this->addRoute('POST', $uri, $action); } /** * Register a new PUT route with the router. * * @param string $uri * @param array|string|callable|null $action * @return \Illuminate\Routing\Route */ public function put($uri, $action = null) { return $this->addRoute('PUT', $uri, $action); } /** * Register a new PATCH route with the router. * * @param string $uri * @param array|string|callable|null $action * @return \Illuminate\Routing\Route */ public function patch($uri, $action = null) { return $this->addRoute('PATCH', $uri, $action); } /** * Register a new DELETE route with the router. * * @param string $uri * @param array|string|callable|null $action * @return \Illuminate\Routing\Route */ public function delete($uri, $action = null) { return $this->addRoute('DELETE', $uri, $action); } /** * Register a new OPTIONS route with the router. * * @param string $uri * @param array|string|callable|null $action * @return \Illuminate\Routing\Route */ public function options($uri, $action = null) { return $this->addRoute('OPTIONS', $uri, $action); } /** * Register a new route responding to all verbs. * * @param string $uri * @param array|string|callable|null $action * @return \Illuminate\Routing\Route */ public function any($uri, $action = null) { return $this->addRoute(self::$verbs, $uri, $action); } /** * Register a new fallback route with the router. * * @param array|string|callable|null $action * @return \Illuminate\Routing\Route */ public function fallback($action) { $placeholder = 'fallbackPlaceholder'; return $this->addRoute( 'GET', "{{$placeholder}}", $action )->where($placeholder, '.*')->fallback(); } /** * Create a redirect from one URI to another. * * @param string $uri * @param string $destination * @param int $status * @return \Illuminate\Routing\Route */ public function redirect($uri, $destination, $status = 302) { return $this->any($uri, '\Illuminate\Routing\RedirectController') ->defaults('destination', $destination) ->defaults('status', $status); } /** * Create a permanent redirect from one URI to another. * * @param string $uri * @param string $destination * @return \Illuminate\Routing\Route */ public function permanentRedirect($uri, $destination) { return $this->redirect($uri, $destination, 301); } /** * Register a new route that returns a view. * * @param string $uri * @param string $view * @param array $data * @param int|array $status * @param array $headers * @return \Illuminate\Routing\Route */ public function view($uri, $view, $data = [], $status = 200, array $headers = []) { return $this->match(['GET', 'HEAD'], $uri, '\Illuminate\Routing\ViewController') ->setDefaults([ 'view' => $view, 'data' => $data, 'status' => is_array($status) ? 200 : $status, 'headers' => is_array($status) ? $status : $headers, ]); } /** * Register a new route with the given verbs. * * @param array|string $methods * @param string $uri * @param array|string|callable|null $action * @return \Illuminate\Routing\Route */ public function match($methods, $uri, $action = null) { return $this->addRoute(array_map('strtoupper', (array) $methods), $uri, $action); } /** * Register an array of resource controllers. * * @param array $resources * @param array $options * @return void */ public function resources(array $resources, array $options = []) { foreach ($resources as $name => $controller) { $this->resource($name, $controller, $options); } } /** * Route a resource to a controller. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\PendingResourceRegistration */ public function resource($name, $controller, array $options = []) { if ($this->container && $this->container->bound(ResourceRegistrar::class)) { $registrar = $this->container->make(ResourceRegistrar::class); } else { $registrar = new ResourceRegistrar($this); } return new PendingResourceRegistration( $registrar, $name, $controller, $options ); } /** * Register an array of API resource controllers. * * @param array $resources * @param array $options * @return void */ public function apiResources(array $resources, array $options = []) { foreach ($resources as $name => $controller) { $this->apiResource($name, $controller, $options); } } /** * Route an API resource to a controller. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\PendingResourceRegistration */ public function apiResource($name, $controller, array $options = []) { $only = ['index', 'show', 'store', 'update', 'destroy']; if (isset($options['except'])) { $only = array_diff($only, (array) $options['except']); } return $this->resource($name, $controller, array_merge([ 'only' => $only, ], $options)); } /** * Register an array of singleton resource controllers. * * @param array $singletons * @param array $options * @return void */ public function singletons(array $singletons, array $options = []) { foreach ($singletons as $name => $controller) { $this->singleton($name, $controller, $options); } } /** * Route a singleton resource to a controller. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\PendingSingletonResourceRegistration */ public function singleton($name, $controller, array $options = []) { if ($this->container && $this->container->bound(ResourceRegistrar::class)) { $registrar = $this->container->make(ResourceRegistrar::class); } else { $registrar = new ResourceRegistrar($this); } return new PendingSingletonResourceRegistration( $registrar, $name, $controller, $options ); } /** * Register an array of API singleton resource controllers. * * @param array $singletons * @param array $options * @return void */ public function apiSingletons(array $singletons, array $options = []) { foreach ($singletons as $name => $controller) { $this->apiSingleton($name, $controller, $options); } } /** * Route an API singleton resource to a controller. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\PendingSingletonResourceRegistration */ public function apiSingleton($name, $controller, array $options = []) { $only = ['store', 'show', 'update', 'destroy']; if (isset($options['except'])) { $only = array_diff($only, (array) $options['except']); } return $this->singleton($name, $controller, array_merge([ 'only' => $only, ], $options)); } /** * Create a route group with shared attributes. * * @param array $attributes * @param \Closure|array|string $routes * @return $this */ public function group(array $attributes, $routes) { foreach (Arr::wrap($routes) as $groupRoutes) { $this->updateGroupStack($attributes); // Once we have updated the group stack, we'll load the provided routes and // merge in the group's attributes when the routes are created. After we // have created the routes, we will pop the attributes off the stack. $this->loadRoutes($groupRoutes); array_pop($this->groupStack); } return $this; } /** * Update the group stack with the given attributes. * * @param array $attributes * @return void */ protected function updateGroupStack(array $attributes) { if ($this->hasGroupStack()) { $attributes = $this->mergeWithLastGroup($attributes); } $this->groupStack[] = $attributes; } /** * Merge the given array with the last group stack. * * @param array $new * @param bool $prependExistingPrefix * @return array */ public function mergeWithLastGroup($new, $prependExistingPrefix = true) { return RouteGroup::merge($new, end($this->groupStack), $prependExistingPrefix); } /** * Load the provided routes. * * @param \Closure|string $routes * @return void */ protected function loadRoutes($routes) { if ($routes instanceof Closure) { $routes($this); } else { (new RouteFileRegistrar($this))->register($routes); } } /** * Get the prefix from the last group on the stack. * * @return string */ public function getLastGroupPrefix() { if ($this->hasGroupStack()) { $last = end($this->groupStack); return $last['prefix'] ?? ''; } return ''; } /** * Add a route to the underlying route collection. * * @param array|string $methods * @param string $uri * @param array|string|callable|null $action * @return \Illuminate\Routing\Route */ public function addRoute($methods, $uri, $action) { return $this->routes->add($this->createRoute($methods, $uri, $action)); } /** * Create a new route instance. * * @param array|string $methods * @param string $uri * @param mixed $action * @return \Illuminate\Routing\Route */ protected function createRoute($methods, $uri, $action) { // If the route is routing to a controller we will parse the route action into // an acceptable array format before registering it and creating this route // instance itself. We need to build the Closure that will call this out. if ($this->actionReferencesController($action)) { $action = $this->convertToControllerAction($action); } $route = $this->newRoute( $methods, $this->prefix($uri), $action ); // If we have groups that need to be merged, we will merge them now after this // route has already been created and is ready to go. After we're done with // the merge we will be ready to return the route back out to the caller. if ($this->hasGroupStack()) { $this->mergeGroupAttributesIntoRoute($route); } $this->addWhereClausesToRoute($route); return $route; } /** * Determine if the action is routing to a controller. * * @param mixed $action * @return bool */ protected function actionReferencesController($action) { if (! $action instanceof Closure) { return is_string($action) || (isset($action['uses']) && is_string($action['uses'])); } return false; } /** * Add a controller based route action to the action array. * * @param array|string $action * @return array */ protected function convertToControllerAction($action) { if (is_string($action)) { $action = ['uses' => $action]; } // Here we'll merge any group "controller" and "uses" statements if necessary so that // the action has the proper clause for this property. Then, we can simply set the // name of this controller on the action plus return the action array for usage. if ($this->hasGroupStack()) { $action['uses'] = $this->prependGroupController($action['uses']); $action['uses'] = $this->prependGroupNamespace($action['uses']); } // Here we will set this controller name on the action array just so we always // have a copy of it for reference if we need it. This can be used while we // search for a controller name or do some other type of fetch operation. $action['controller'] = $action['uses']; return $action; } /** * Prepend the last group namespace onto the use clause. * * @param string $class * @return string */ protected function prependGroupNamespace($class) { $group = end($this->groupStack); return isset($group['namespace']) && ! str_starts_with($class, '\\') && ! str_starts_with($class, $group['namespace']) ? $group['namespace'].'\\'.$class : $class; } /** * Prepend the last group controller onto the use clause. * * @param string $class * @return string */ protected function prependGroupController($class) { $group = end($this->groupStack); if (! isset($group['controller'])) { return $class; } if (class_exists($class)) { return $class; } if (str_contains($class, '@')) { return $class; } return $group['controller'].'@'.$class; } /** * Create a new Route object. * * @param array|string $methods * @param string $uri * @param mixed $action * @return \Illuminate\Routing\Route */ public function newRoute($methods, $uri, $action) { return (new Route($methods, $uri, $action)) ->setRouter($this) ->setContainer($this->container); } /** * Prefix the given URI with the last prefix. * * @param string $uri * @return string */ protected function prefix($uri) { return trim(trim($this->getLastGroupPrefix(), '/').'/'.trim($uri, '/'), '/') ?: '/'; } /** * Add the necessary where clauses to the route based on its initial registration. * * @param \Illuminate\Routing\Route $route * @return \Illuminate\Routing\Route */ protected function addWhereClausesToRoute($route) { $route->where(array_merge( $this->patterns, $route->getAction()['where'] ?? [] )); return $route; } /** * Merge the group stack with the controller action. * * @param \Illuminate\Routing\Route $route * @return void */ protected function mergeGroupAttributesIntoRoute($route) { $route->setAction($this->mergeWithLastGroup( $route->getAction(), $prependExistingPrefix = false )); } /** * Return the response returned by the given route. * * @param string $name * @return \Symfony\Component\HttpFoundation\Response */ public function respondWithRoute($name) { $route = tap($this->routes->getByName($name))->bind($this->currentRequest); return $this->runRoute($this->currentRequest, $route); } /** * Dispatch the request to the application. * * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function dispatch(Request $request) { $this->currentRequest = $request; return $this->dispatchToRoute($request); } /** * Dispatch the request to a route and return the response. * * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function dispatchToRoute(Request $request) { return $this->runRoute($request, $this->findRoute($request)); } /** * Find the route matching a given request. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Routing\Route */ protected function findRoute($request) { $this->events->dispatch(new Routing($request)); $this->current = $route = $this->routes->match($request); $route->setContainer($this->container); $this->container->instance(Route::class, $route); return $route; } /** * Return the response for the given route. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Routing\Route $route * @return \Symfony\Component\HttpFoundation\Response */ protected function runRoute(Request $request, Route $route) { $request->setRouteResolver(fn () => $route); $this->events->dispatch(new RouteMatched($route, $request)); return $this->prepareResponse($request, $this->runRouteWithinStack($route, $request) ); } /** * Run the given route within a Stack "onion" instance. * * @param \Illuminate\Routing\Route $route * @param \Illuminate\Http\Request $request * @return mixed */ protected function runRouteWithinStack(Route $route, Request $request) { $shouldSkipMiddleware = $this->container->bound('middleware.disable') && $this->container->make('middleware.disable') === true; $middleware = $shouldSkipMiddleware ? [] : $this->gatherRouteMiddleware($route); return (new Pipeline($this->container)) ->send($request) ->through($middleware) ->then(fn ($request) => $this->prepareResponse( $request, $route->run() )); } /** * Gather the middleware for the given route with resolved class names. * * @param \Illuminate\Routing\Route $route * @return array */ public function gatherRouteMiddleware(Route $route) { return $this->resolveMiddleware($route->gatherMiddleware(), $route->excludedMiddleware()); } /** * Resolve a flat array of middleware classes from the provided array. * * @param array $middleware * @param array $excluded * @return array */ public function resolveMiddleware(array $middleware, array $excluded = []) { $excluded = collect($excluded)->map(function ($name) { return (array) MiddlewareNameResolver::resolve($name, $this->middleware, $this->middlewareGroups); })->flatten()->values()->all(); $middleware = collect($middleware)->map(function ($name) { return (array) MiddlewareNameResolver::resolve($name, $this->middleware, $this->middlewareGroups); })->flatten()->reject(function ($name) use ($excluded) { if (empty($excluded)) { return false; } if ($name instanceof Closure) { return false; } if (in_array($name, $excluded, true)) { return true; } if (! class_exists($name)) { return false; } $reflection = new ReflectionClass($name); return collect($excluded)->contains( fn ($exclude) => class_exists($exclude) && $reflection->isSubclassOf($exclude) ); })->values(); return $this->sortMiddleware($middleware); } /** * Sort the given middleware by priority. * * @param \Illuminate\Support\Collection $middlewares * @return array */ protected function sortMiddleware(Collection $middlewares) { return (new SortedMiddleware($this->middlewarePriority, $middlewares))->all(); } /** * Create a response instance from the given value. * * @param \Symfony\Component\HttpFoundation\Request $request * @param mixed $response * @return \Symfony\Component\HttpFoundation\Response */ public function prepareResponse($request, $response) { $this->events->dispatch(new PreparingResponse($request, $response)); return tap(static::toResponse($request, $response), function ($response) use ($request) { $this->events->dispatch(new ResponsePrepared($request, $response)); }); } /** * Static version of prepareResponse. * * @param \Symfony\Component\HttpFoundation\Request $request * @param mixed $response * @return \Symfony\Component\HttpFoundation\Response */ public static function toResponse($request, $response) { if ($response instanceof Responsable) { $response = $response->toResponse($request); } if ($response instanceof PsrResponseInterface) { $response = (new HttpFoundationFactory)->createResponse($response); } elseif ($response instanceof Model && $response->wasRecentlyCreated) { $response = new JsonResponse($response, 201); } elseif ($response instanceof Stringable) { $response = new Response($response->__toString(), 200, ['Content-Type' => 'text/html']); } elseif (! $response instanceof SymfonyResponse && ($response instanceof Arrayable || $response instanceof Jsonable || $response instanceof ArrayObject || $response instanceof JsonSerializable || $response instanceof stdClass || is_array($response))) { $response = new JsonResponse($response); } elseif (! $response instanceof SymfonyResponse) { $response = new Response($response, 200, ['Content-Type' => 'text/html']); } if ($response->getStatusCode() === Response::HTTP_NOT_MODIFIED) { $response->setNotModified(); } return $response->prepare($request); } /** * Substitute the route bindings onto the route. * * @param \Illuminate\Routing\Route $route * @return \Illuminate\Routing\Route * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> * @throws \Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException */ public function substituteBindings($route) { foreach ($route->parameters() as $key => $value) { if (isset($this->binders[$key])) { $route->setParameter($key, $this->performBinding($key, $value, $route)); } } return $route; } /** * Substitute the implicit route bindings for the given route. * * @param \Illuminate\Routing\Route $route * @return void * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> * @throws \Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException */ public function substituteImplicitBindings($route) { ImplicitRouteBinding::resolveForRoute($this->container, $route); } /** * Call the binding callback for the given key. * * @param string $key * @param string $value * @param \Illuminate\Routing\Route $route * @return mixed * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ protected function performBinding($key, $value, $route) { return call_user_func($this->binders[$key], $value, $route); } /** * Register a route matched event listener. * * @param string|callable $callback * @return void */ public function matched($callback) { $this->events->listen(Events\RouteMatched::class, $callback); } /** * Get all of the defined middleware short-hand names. * * @return array */ public function getMiddleware() { return $this->middleware; } /** * Register a short-hand name for a middleware. * * @param string $name * @param string $class * @return $this */ public function aliasMiddleware($name, $class) { $this->middleware[$name] = $class; return $this; } /** * Check if a middlewareGroup with the given name exists. * * @param string $name * @return bool */ public function hasMiddlewareGroup($name) { return array_key_exists($name, $this->middlewareGroups); } /** * Get all of the defined middleware groups. * * @return array */ public function getMiddlewareGroups() { return $this->middlewareGroups; } /** * Register a group of middleware. * * @param string $name * @param array $middleware * @return $this */ public function middlewareGroup($name, array $middleware) { $this->middlewareGroups[$name] = $middleware; return $this; } /** * Add a middleware to the beginning of a middleware group. * * If the middleware is already in the group, it will not be added again. * * @param string $group * @param string $middleware * @return $this */ public function prependMiddlewareToGroup($group, $middleware) { if (isset($this->middlewareGroups[$group]) && ! in_array($middleware, $this->middlewareGroups[$group])) { array_unshift($this->middlewareGroups[$group], $middleware); } return $this; } /** * Add a middleware to the end of a middleware group. * * If the middleware is already in the group, it will not be added again. * * @param string $group * @param string $middleware * @return $this */ public function pushMiddlewareToGroup($group, $middleware) { if (! array_key_exists($group, $this->middlewareGroups)) { $this->middlewareGroups[$group] = []; } if (! in_array($middleware, $this->middlewareGroups[$group])) { $this->middlewareGroups[$group][] = $middleware; } return $this; } /** * Remove the given middleware from the specified group. * * @param string $group * @param string $middleware * @return $this */ public function removeMiddlewareFromGroup($group, $middleware) { if (! $this->hasMiddlewareGroup($group)) { return $this; } $reversedMiddlewaresArray = array_flip($this->middlewareGroups[$group]); if (! array_key_exists($middleware, $reversedMiddlewaresArray)) { return $this; } $middlewareKey = $reversedMiddlewaresArray[$middleware]; unset($this->middlewareGroups[$group][$middlewareKey]); return $this; } /** * Flush the router's middleware groups. * * @return $this */ public function flushMiddlewareGroups() { $this->middlewareGroups = []; return $this; } /** * Add a new route parameter binder. * * @param string $key * @param string|callable $binder * @return void */ public function bind($key, $binder) { $this->binders[str_replace('-', '_', $key)] = RouteBinding::forCallback( $this->container, $binder ); } /** * Register a model binder for a wildcard. * * @param string $key * @param string $class * @param \Closure|null $callback * @return void */ public function model($key, $class, Closure $callback = null) { $this->bind($key, RouteBinding::forModel($this->container, $class, $callback)); } /** * Get the binding callback for a given binding. * * @param string $key * @return \Closure|null */ public function getBindingCallback($key) { if (isset($this->binders[$key = str_replace('-', '_', $key)])) { return $this->binders[$key]; } } /** * Get the global "where" patterns. * * @return array */ public function getPatterns() { return $this->patterns; } /** * Set a global where pattern on all routes. * * @param string $key * @param string $pattern * @return void */ public function pattern($key, $pattern) { $this->patterns[$key] = $pattern; } /** * Set a group of global where patterns on all routes. * * @param array $patterns * @return void */ public function patterns($patterns) { foreach ($patterns as $key => $pattern) { $this->pattern($key, $pattern); } } /** * Determine if the router currently has a group stack. * * @return bool */ public function hasGroupStack() { return ! empty($this->groupStack); } /** * Get the current group stack for the router. * * @return array */ public function getGroupStack() { return $this->groupStack; } /** * Get a route parameter for the current route. * * @param string $key * @param string|null $default * @return mixed */ public function input($key, $default = null) { return $this->current()->parameter($key, $default); } /** * Get the request currently being dispatched. * * @return \Illuminate\Http\Request */ public function getCurrentRequest() { return $this->currentRequest; } /** * Get the currently dispatched route instance. * * @return \Illuminate\Routing\Route|null */ public function getCurrentRoute() { return $this->current(); } /** * Get the currently dispatched route instance. * * @return \Illuminate\Routing\Route|null */ public function current() { return $this->current; } /** * Check if a route with the given name exists. * * @param string|array $name * @return bool */ public function has($name) { $names = is_array($name) ? $name : func_get_args(); foreach ($names as $value) { if (! $this->routes->hasNamedRoute($value)) { return false; } } return true; } /** * Get the current route name. * * @return string|null */ public function currentRouteName() { return $this->current() ? $this->current()->getName() : null; } /** * Alias for the "currentRouteNamed" method. * * @param mixed ...$patterns * @return bool */ public function is(...$patterns) { return $this->currentRouteNamed(...$patterns); } /** * Determine if the current route matches a pattern. * * @param mixed ...$patterns * @return bool */ public function currentRouteNamed(...$patterns) { return $this->current() && $this->current()->named(...$patterns); } /** * Get the current route action. * * @return string|null */ public function currentRouteAction() { if ($this->current()) { return $this->current()->getAction()['controller'] ?? null; } } /** * Alias for the "currentRouteUses" method. * * @param array ...$patterns * @return bool */ public function uses(...$patterns) { foreach ($patterns as $pattern) { if (Str::is($pattern, $this->currentRouteAction())) { return true; } } return false; } /** * Determine if the current route action matches a given action. * * @param string $action * @return bool */ public function currentRouteUses($action) { return $this->currentRouteAction() == $action; } /** * Set the unmapped global resource parameters to singular. * * @param bool $singular * @return void */ public function singularResourceParameters($singular = true) { ResourceRegistrar::singularParameters($singular); } /** * Set the global resource parameter mapping. * * @param array $parameters * @return void */ public function resourceParameters(array $parameters = []) { ResourceRegistrar::setParameters($parameters); } /** * Get or set the verbs used in the resource URIs. * * @param array $verbs * @return array|null */ public function resourceVerbs(array $verbs = []) { return ResourceRegistrar::verbs($verbs); } /** * Get the underlying route collection. * * @return \Illuminate\Routing\RouteCollectionInterface */ public function getRoutes() { return $this->routes; } /** * Set the route collection instance. * * @param \Illuminate\Routing\RouteCollection $routes * @return void */ public function setRoutes(RouteCollection $routes) { foreach ($routes as $route) { $route->setRouter($this)->setContainer($this->container); } $this->routes = $routes; $this->container->instance('routes', $this->routes); } /** * Set the compiled route collection instance. * * @param array $routes * @return void */ public function setCompiledRoutes(array $routes) { $this->routes = (new CompiledRouteCollection($routes['compiled'], $routes['attributes'])) ->setRouter($this) ->setContainer($this->container); $this->container->instance('routes', $this->routes); } /** * Remove any duplicate middleware from the given array. * * @param array $middleware * @return array */ public static function uniqueMiddleware(array $middleware) { $seen = []; $result = []; foreach ($middleware as $value) { $key = \is_object($value) ? \spl_object_id($value) : $value; if (! isset($seen[$key])) { $seen[$key] = true; $result[] = $value; } } return $result; } /** * Set the container instance used by the router. * * @param \Illuminate\Container\Container $container * @return $this */ public function setContainer(Container $container) { $this->container = $container; return $this; } /** * Dynamically handle calls into the router instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } if ($method === 'middleware') { return (new RouteRegistrar($this))->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters); } if ($method !== 'where' && Str::startsWith($method, 'where')) { return (new RouteRegistrar($this))->{$method}(...$parameters); } return (new RouteRegistrar($this))->attribute($method, array_key_exists(0, $parameters) ? $parameters[0] : true); } } src/Illuminate/Routing/ResourceRegistrar.php 0000644 00000051173 15107327037 0015264 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Support\Str; class ResourceRegistrar { /** * The router instance. * * @var \Illuminate\Routing\Router */ protected $router; /** * The default actions for a resourceful controller. * * @var string[] */ protected $resourceDefaults = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy']; /** * The default actions for a singleton resource controller. * * @var string[] */ protected $singletonResourceDefaults = ['show', 'edit', 'update']; /** * The parameters set for this resource instance. * * @var array|string */ protected $parameters; /** * The global parameter mapping. * * @var array */ protected static $parameterMap = []; /** * Singular global parameters. * * @var bool */ protected static $singularParameters = true; /** * The verbs used in the resource URIs. * * @var array */ protected static $verbs = [ 'create' => 'create', 'edit' => 'edit', ]; /** * Create a new resource registrar instance. * * @param \Illuminate\Routing\Router $router * @return void */ public function __construct(Router $router) { $this->router = $router; } /** * Route a resource to a controller. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\RouteCollection */ public function register($name, $controller, array $options = []) { if (isset($options['parameters']) && ! isset($this->parameters)) { $this->parameters = $options['parameters']; } // If the resource name contains a slash, we will assume the developer wishes to // register these resource routes with a prefix so we will set that up out of // the box so they don't have to mess with it. Otherwise, we will continue. if (str_contains($name, '/')) { $this->prefixedResource($name, $controller, $options); return; } // We need to extract the base resource from the resource name. Nested resources // are supported in the framework, but we need to know what name to use for a // place-holder on the route parameters, which should be the base resources. $base = $this->getResourceWildcard(last(explode('.', $name))); $defaults = $this->resourceDefaults; $collection = new RouteCollection; $resourceMethods = $this->getResourceMethods($defaults, $options); foreach ($resourceMethods as $m) { $route = $this->{'addResource'.ucfirst($m)}( $name, $base, $controller, $options ); if (isset($options['bindingFields'])) { $this->setResourceBindingFields($route, $options['bindingFields']); } if (isset($options['trashed']) && in_array($m, ! empty($options['trashed']) ? $options['trashed'] : array_intersect($resourceMethods, ['show', 'edit', 'update']))) { $route->withTrashed(); } $collection->add($route); } return $collection; } /** * Route a singleton resource to a controller. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\RouteCollection */ public function singleton($name, $controller, array $options = []) { if (isset($options['parameters']) && ! isset($this->parameters)) { $this->parameters = $options['parameters']; } // If the resource name contains a slash, we will assume the developer wishes to // register these singleton routes with a prefix so we will set that up out of // the box so they don't have to mess with it. Otherwise, we will continue. if (str_contains($name, '/')) { $this->prefixedSingleton($name, $controller, $options); return; } $defaults = $this->singletonResourceDefaults; if (isset($options['creatable'])) { $defaults = array_merge($defaults, ['create', 'store', 'destroy']); } elseif (isset($options['destroyable'])) { $defaults = array_merge($defaults, ['destroy']); } $collection = new RouteCollection; $resourceMethods = $this->getResourceMethods($defaults, $options); foreach ($resourceMethods as $m) { $route = $this->{'addSingleton'.ucfirst($m)}( $name, $controller, $options ); if (isset($options['bindingFields'])) { $this->setResourceBindingFields($route, $options['bindingFields']); } $collection->add($route); } return $collection; } /** * Build a set of prefixed resource routes. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\Router */ protected function prefixedResource($name, $controller, array $options) { [$name, $prefix] = $this->getResourcePrefix($name); // We need to extract the base resource from the resource name. Nested resources // are supported in the framework, but we need to know what name to use for a // place-holder on the route parameters, which should be the base resources. $callback = function ($me) use ($name, $controller, $options) { $me->resource($name, $controller, $options); }; return $this->router->group(compact('prefix'), $callback); } /** * Build a set of prefixed singleton routes. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\Router */ protected function prefixedSingleton($name, $controller, array $options) { [$name, $prefix] = $this->getResourcePrefix($name); // We need to extract the base resource from the resource name. Nested resources // are supported in the framework, but we need to know what name to use for a // place-holder on the route parameters, which should be the base resources. $callback = function ($me) use ($name, $controller, $options) { $me->singleton($name, $controller, $options); }; return $this->router->group(compact('prefix'), $callback); } /** * Extract the resource and prefix from a resource name. * * @param string $name * @return array */ protected function getResourcePrefix($name) { $segments = explode('/', $name); // To get the prefix, we will take all of the name segments and implode them on // a slash. This will generate a proper URI prefix for us. Then we take this // last segment, which will be considered the final resources name we use. $prefix = implode('/', array_slice($segments, 0, -1)); return [end($segments), $prefix]; } /** * Get the applicable resource methods. * * @param array $defaults * @param array $options * @return array */ protected function getResourceMethods($defaults, $options) { $methods = $defaults; if (isset($options['only'])) { $methods = array_intersect($methods, (array) $options['only']); } if (isset($options['except'])) { $methods = array_diff($methods, (array) $options['except']); } return array_values($methods); } /** * Add the index method for a resourceful route. * * @param string $name * @param string $base * @param string $controller * @param array $options * @return \Illuminate\Routing\Route */ protected function addResourceIndex($name, $base, $controller, $options) { $uri = $this->getResourceUri($name); unset($options['missing']); $action = $this->getResourceAction($name, $controller, 'index', $options); return $this->router->get($uri, $action); } /** * Add the create method for a resourceful route. * * @param string $name * @param string $base * @param string $controller * @param array $options * @return \Illuminate\Routing\Route */ protected function addResourceCreate($name, $base, $controller, $options) { $uri = $this->getResourceUri($name).'/'.static::$verbs['create']; unset($options['missing']); $action = $this->getResourceAction($name, $controller, 'create', $options); return $this->router->get($uri, $action); } /** * Add the store method for a resourceful route. * * @param string $name * @param string $base * @param string $controller * @param array $options * @return \Illuminate\Routing\Route */ protected function addResourceStore($name, $base, $controller, $options) { $uri = $this->getResourceUri($name); unset($options['missing']); $action = $this->getResourceAction($name, $controller, 'store', $options); return $this->router->post($uri, $action); } /** * Add the show method for a resourceful route. * * @param string $name * @param string $base * @param string $controller * @param array $options * @return \Illuminate\Routing\Route */ protected function addResourceShow($name, $base, $controller, $options) { $name = $this->getShallowName($name, $options); $uri = $this->getResourceUri($name).'/{'.$base.'}'; $action = $this->getResourceAction($name, $controller, 'show', $options); return $this->router->get($uri, $action); } /** * Add the edit method for a resourceful route. * * @param string $name * @param string $base * @param string $controller * @param array $options * @return \Illuminate\Routing\Route */ protected function addResourceEdit($name, $base, $controller, $options) { $name = $this->getShallowName($name, $options); $uri = $this->getResourceUri($name).'/{'.$base.'}/'.static::$verbs['edit']; $action = $this->getResourceAction($name, $controller, 'edit', $options); return $this->router->get($uri, $action); } /** * Add the update method for a resourceful route. * * @param string $name * @param string $base * @param string $controller * @param array $options * @return \Illuminate\Routing\Route */ protected function addResourceUpdate($name, $base, $controller, $options) { $name = $this->getShallowName($name, $options); $uri = $this->getResourceUri($name).'/{'.$base.'}'; $action = $this->getResourceAction($name, $controller, 'update', $options); return $this->router->match(['PUT', 'PATCH'], $uri, $action); } /** * Add the destroy method for a resourceful route. * * @param string $name * @param string $base * @param string $controller * @param array $options * @return \Illuminate\Routing\Route */ protected function addResourceDestroy($name, $base, $controller, $options) { $name = $this->getShallowName($name, $options); $uri = $this->getResourceUri($name).'/{'.$base.'}'; $action = $this->getResourceAction($name, $controller, 'destroy', $options); return $this->router->delete($uri, $action); } /** * Add the create method for a singleton route. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\Route */ protected function addSingletonCreate($name, $controller, $options) { $uri = $this->getResourceUri($name).'/'.static::$verbs['create']; unset($options['missing']); $action = $this->getResourceAction($name, $controller, 'create', $options); return $this->router->get($uri, $action); } /** * Add the store method for a singleton route. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\Route */ protected function addSingletonStore($name, $controller, $options) { $uri = $this->getResourceUri($name); unset($options['missing']); $action = $this->getResourceAction($name, $controller, 'store', $options); return $this->router->post($uri, $action); } /** * Add the show method for a singleton route. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\Route */ protected function addSingletonShow($name, $controller, $options) { $uri = $this->getResourceUri($name); unset($options['missing']); $action = $this->getResourceAction($name, $controller, 'show', $options); return $this->router->get($uri, $action); } /** * Add the edit method for a singleton route. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\Route */ protected function addSingletonEdit($name, $controller, $options) { $name = $this->getShallowName($name, $options); $uri = $this->getResourceUri($name).'/'.static::$verbs['edit']; $action = $this->getResourceAction($name, $controller, 'edit', $options); return $this->router->get($uri, $action); } /** * Add the update method for a singleton route. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\Route */ protected function addSingletonUpdate($name, $controller, $options) { $name = $this->getShallowName($name, $options); $uri = $this->getResourceUri($name); $action = $this->getResourceAction($name, $controller, 'update', $options); return $this->router->match(['PUT', 'PATCH'], $uri, $action); } /** * Add the destroy method for a singleton route. * * @param string $name * @param string $controller * @param array $options * @return \Illuminate\Routing\Route */ protected function addSingletonDestroy($name, $controller, $options) { $name = $this->getShallowName($name, $options); $uri = $this->getResourceUri($name); $action = $this->getResourceAction($name, $controller, 'destroy', $options); return $this->router->delete($uri, $action); } /** * Get the name for a given resource with shallowness applied when applicable. * * @param string $name * @param array $options * @return string */ protected function getShallowName($name, $options) { return isset($options['shallow']) && $options['shallow'] ? last(explode('.', $name)) : $name; } /** * Set the route's binding fields if the resource is scoped. * * @param \Illuminate\Routing\Route $route * @param array $bindingFields * @return void */ protected function setResourceBindingFields($route, $bindingFields) { preg_match_all('/(?<={).*?(?=})/', $route->uri, $matches); $fields = array_fill_keys($matches[0], null); $route->setBindingFields(array_replace( $fields, array_intersect_key($bindingFields, $fields) )); } /** * Get the base resource URI for a given resource. * * @param string $resource * @return string */ public function getResourceUri($resource) { if (! str_contains($resource, '.')) { return $resource; } // Once we have built the base URI, we'll remove the parameter holder for this // base resource name so that the individual route adders can suffix these // paths however they need to, as some do not have any parameters at all. $segments = explode('.', $resource); $uri = $this->getNestedResourceUri($segments); return str_replace('/{'.$this->getResourceWildcard(end($segments)).'}', '', $uri); } /** * Get the URI for a nested resource segment array. * * @param array $segments * @return string */ protected function getNestedResourceUri(array $segments) { // We will spin through the segments and create a place-holder for each of the // resource segments, as well as the resource itself. Then we should get an // entire string for the resource URI that contains all nested resources. return implode('/', array_map(function ($s) { return $s.'/{'.$this->getResourceWildcard($s).'}'; }, $segments)); } /** * Format a resource parameter for usage. * * @param string $value * @return string */ public function getResourceWildcard($value) { if (isset($this->parameters[$value])) { $value = $this->parameters[$value]; } elseif (isset(static::$parameterMap[$value])) { $value = static::$parameterMap[$value]; } elseif ($this->parameters === 'singular' || static::$singularParameters) { $value = Str::singular($value); } return str_replace('-', '_', $value); } /** * Get the action array for a resource route. * * @param string $resource * @param string $controller * @param string $method * @param array $options * @return array */ protected function getResourceAction($resource, $controller, $method, $options) { $name = $this->getResourceRouteName($resource, $method, $options); $action = ['as' => $name, 'uses' => $controller.'@'.$method]; if (isset($options['middleware'])) { $action['middleware'] = $options['middleware']; } if (isset($options['excluded_middleware'])) { $action['excluded_middleware'] = $options['excluded_middleware']; } if (isset($options['wheres'])) { $action['where'] = $options['wheres']; } if (isset($options['missing'])) { $action['missing'] = $options['missing']; } return $action; } /** * Get the name for a given resource. * * @param string $resource * @param string $method * @param array $options * @return string */ protected function getResourceRouteName($resource, $method, $options) { $name = $resource; // If the names array has been provided to us we will check for an entry in the // array first. We will also check for the specific method within this array // so the names may be specified on a more "granular" level using methods. if (isset($options['names'])) { if (is_string($options['names'])) { $name = $options['names']; } elseif (isset($options['names'][$method])) { return $options['names'][$method]; } } // If a global prefix has been assigned to all names for this resource, we will // grab that so we can prepend it onto the name when we create this name for // the resource action. Otherwise we'll just use an empty string for here. $prefix = isset($options['as']) ? $options['as'].'.' : ''; return trim(sprintf('%s%s.%s', $prefix, $name, $method), '.'); } /** * Set or unset the unmapped global parameters to singular. * * @param bool $singular * @return void */ public static function singularParameters($singular = true) { static::$singularParameters = (bool) $singular; } /** * Get the global parameter map. * * @return array */ public static function getParameters() { return static::$parameterMap; } /** * Set the global parameter mapping. * * @param array $parameters * @return void */ public static function setParameters(array $parameters = []) { static::$parameterMap = $parameters; } /** * Get or set the action verbs used in the resource URIs. * * @param array $verbs * @return array */ public static function verbs(array $verbs = []) { if (empty($verbs)) { return static::$verbs; } static::$verbs = array_merge(static::$verbs, $verbs); } } src/Illuminate/Routing/CompiledRouteCollection.php 0000644 00000021444 15107327037 0016377 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Container\Container; use Illuminate\Http\Request; use Illuminate\Support\Collection; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Routing\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Matcher\CompiledUrlMatcher; use Symfony\Component\Routing\RequestContext; class CompiledRouteCollection extends AbstractRouteCollection { /** * The compiled routes collection. * * @var array */ protected $compiled = []; /** * An array of the route attributes keyed by name. * * @var array */ protected $attributes = []; /** * The dynamically added routes that were added after loading the cached, compiled routes. * * @var \Illuminate\Routing\RouteCollection|null */ protected $routes; /** * The router instance used by the route. * * @var \Illuminate\Routing\Router */ protected $router; /** * The container instance used by the route. * * @var \Illuminate\Container\Container */ protected $container; /** * Create a new CompiledRouteCollection instance. * * @param array $compiled * @param array $attributes * @return void */ public function __construct(array $compiled, array $attributes) { $this->compiled = $compiled; $this->attributes = $attributes; $this->routes = new RouteCollection; } /** * Add a Route instance to the collection. * * @param \Illuminate\Routing\Route $route * @return \Illuminate\Routing\Route */ public function add(Route $route) { return $this->routes->add($route); } /** * Refresh the name look-up table. * * This is done in case any names are fluently defined or if routes are overwritten. * * @return void */ public function refreshNameLookups() { // } /** * Refresh the action look-up table. * * This is done in case any actions are overwritten with new controllers. * * @return void */ public function refreshActionLookups() { // } /** * Find the first route matching a given request. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Routing\Route * * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ public function match(Request $request) { $matcher = new CompiledUrlMatcher( $this->compiled, (new RequestContext)->fromRequest( $trimmedRequest = $this->requestWithoutTrailingSlash($request) ) ); $route = null; try { if ($result = $matcher->matchRequest($trimmedRequest)) { $route = $this->getByName($result['_route']); } } catch (ResourceNotFoundException|MethodNotAllowedException) { try { return $this->routes->match($request); } catch (NotFoundHttpException) { // } } if ($route && $route->isFallback) { try { $dynamicRoute = $this->routes->match($request); if (! $dynamicRoute->isFallback) { $route = $dynamicRoute; } } catch (NotFoundHttpException|MethodNotAllowedHttpException) { // } } return $this->handleMatchedRoute($request, $route); } /** * Get a cloned instance of the given request without any trailing slash on the URI. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Request */ protected function requestWithoutTrailingSlash(Request $request) { $trimmedRequest = $request->duplicate(); $parts = explode('?', $request->server->get('REQUEST_URI'), 2); $trimmedRequest->server->set( 'REQUEST_URI', rtrim($parts[0], '/').(isset($parts[1]) ? '?'.$parts[1] : '') ); return $trimmedRequest; } /** * Get routes from the collection by method. * * @param string|null $method * @return \Illuminate\Routing\Route[] */ public function get($method = null) { return $this->getRoutesByMethod()[$method] ?? []; } /** * Determine if the route collection contains a given named route. * * @param string $name * @return bool */ public function hasNamedRoute($name) { return isset($this->attributes[$name]) || $this->routes->hasNamedRoute($name); } /** * Get a route instance by its name. * * @param string $name * @return \Illuminate\Routing\Route|null */ public function getByName($name) { if (isset($this->attributes[$name])) { return $this->newRoute($this->attributes[$name]); } return $this->routes->getByName($name); } /** * Get a route instance by its controller action. * * @param string $action * @return \Illuminate\Routing\Route|null */ public function getByAction($action) { $attributes = collect($this->attributes)->first(function (array $attributes) use ($action) { if (isset($attributes['action']['controller'])) { return trim($attributes['action']['controller'], '\\') === $action; } return $attributes['action']['uses'] === $action; }); if ($attributes) { return $this->newRoute($attributes); } return $this->routes->getByAction($action); } /** * Get all of the routes in the collection. * * @return \Illuminate\Routing\Route[] */ public function getRoutes() { return collect($this->attributes) ->map(function (array $attributes) { return $this->newRoute($attributes); }) ->merge($this->routes->getRoutes()) ->values() ->all(); } /** * Get all of the routes keyed by their HTTP verb / method. * * @return array */ public function getRoutesByMethod() { return collect($this->getRoutes()) ->groupBy(function (Route $route) { return $route->methods(); }) ->map(function (Collection $routes) { return $routes->mapWithKeys(function (Route $route) { return [$route->getDomain().$route->uri => $route]; })->all(); }) ->all(); } /** * Get all of the routes keyed by their name. * * @return \Illuminate\Routing\Route[] */ public function getRoutesByName() { return collect($this->getRoutes()) ->keyBy(function (Route $route) { return $route->getName(); }) ->all(); } /** * Resolve an array of attributes to a Route instance. * * @param array $attributes * @return \Illuminate\Routing\Route */ protected function newRoute(array $attributes) { if (empty($attributes['action']['prefix'] ?? '')) { $baseUri = $attributes['uri']; } else { $prefix = trim($attributes['action']['prefix'], '/'); $baseUri = trim(implode( '/', array_slice( explode('/', trim($attributes['uri'], '/')), count($prefix !== '' ? explode('/', $prefix) : []) ) ), '/'); } return $this->router->newRoute($attributes['methods'], $baseUri === '' ? '/' : $baseUri, $attributes['action']) ->setFallback($attributes['fallback']) ->setDefaults($attributes['defaults']) ->setWheres($attributes['wheres']) ->setBindingFields($attributes['bindingFields']) ->block($attributes['lockSeconds'] ?? null, $attributes['waitSeconds'] ?? null) ->withTrashed($attributes['withTrashed'] ?? false); } /** * Set the router instance on the route. * * @param \Illuminate\Routing\Router $router * @return $this */ public function setRouter(Router $router) { $this->router = $router; return $this; } /** * Set the container instance on the route. * * @param \Illuminate\Container\Container $container * @return $this */ public function setContainer(Container $container) { $this->container = $container; return $this; } } src/Illuminate/Routing/Route.php 0000644 00000076567 15107327037 0012726 0 ustar 00 <?php namespace Illuminate\Routing; use Closure; use Illuminate\Container\Container; use Illuminate\Http\Exceptions\HttpResponseException; use Illuminate\Http\Request; use Illuminate\Routing\Contracts\CallableDispatcher; use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract; use Illuminate\Routing\Controllers\HasMiddleware; use Illuminate\Routing\Matching\HostValidator; use Illuminate\Routing\Matching\MethodValidator; use Illuminate\Routing\Matching\SchemeValidator; use Illuminate\Routing\Matching\UriValidator; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use Laravel\SerializableClosure\SerializableClosure; use LogicException; use Symfony\Component\Routing\Route as SymfonyRoute; class Route { use CreatesRegularExpressionRouteConstraints, FiltersControllerMiddleware, Macroable, ResolvesRouteDependencies; /** * The URI pattern the route responds to. * * @var string */ public $uri; /** * The HTTP methods the route responds to. * * @var array */ public $methods; /** * The route action array. * * @var array */ public $action; /** * Indicates whether the route is a fallback route. * * @var bool */ public $isFallback = false; /** * The controller instance. * * @var mixed */ public $controller; /** * The default values for the route. * * @var array */ public $defaults = []; /** * The regular expression requirements. * * @var array */ public $wheres = []; /** * The array of matched parameters. * * @var array|null */ public $parameters; /** * The parameter names for the route. * * @var array|null */ public $parameterNames; /** * The array of the matched parameters' original values. * * @var array */ protected $originalParameters; /** * Indicates "trashed" models can be retrieved when resolving implicit model bindings for this route. * * @var bool */ protected $withTrashedBindings = false; /** * Indicates the maximum number of seconds the route should acquire a session lock for. * * @var int|null */ protected $lockSeconds; /** * Indicates the maximum number of seconds the route should wait while attempting to acquire a session lock. * * @var int|null */ protected $waitSeconds; /** * The computed gathered middleware. * * @var array|null */ public $computedMiddleware; /** * The compiled version of the route. * * @var \Symfony\Component\Routing\CompiledRoute */ public $compiled; /** * The router instance used by the route. * * @var \Illuminate\Routing\Router */ protected $router; /** * The container instance used by the route. * * @var \Illuminate\Container\Container */ protected $container; /** * The fields that implicit binding should use for a given parameter. * * @var array */ protected $bindingFields = []; /** * The validators used by the routes. * * @var array */ public static $validators; /** * Create a new Route instance. * * @param array|string $methods * @param string $uri * @param \Closure|array $action * @return void */ public function __construct($methods, $uri, $action) { $this->uri = $uri; $this->methods = (array) $methods; $this->action = Arr::except($this->parseAction($action), ['prefix']); if (in_array('GET', $this->methods) && ! in_array('HEAD', $this->methods)) { $this->methods[] = 'HEAD'; } $this->prefix(is_array($action) ? Arr::get($action, 'prefix') : ''); } /** * Parse the route action into a standard array. * * @param callable|array|null $action * @return array * * @throws \UnexpectedValueException */ protected function parseAction($action) { return RouteAction::parse($this->uri, $action); } /** * Run the route action and return the response. * * @return mixed */ public function run() { $this->container = $this->container ?: new Container; try { if ($this->isControllerAction()) { return $this->runController(); } return $this->runCallable(); } catch (HttpResponseException $e) { return $e->getResponse(); } } /** * Checks whether the route's action is a controller. * * @return bool */ protected function isControllerAction() { return is_string($this->action['uses']) && ! $this->isSerializedClosure(); } /** * Run the route action and return the response. * * @return mixed */ protected function runCallable() { $callable = $this->action['uses']; if ($this->isSerializedClosure()) { $callable = unserialize($this->action['uses'])->getClosure(); } return $this->container[CallableDispatcher::class]->dispatch($this, $callable); } /** * Determine if the route action is a serialized Closure. * * @return bool */ protected function isSerializedClosure() { return RouteAction::containsSerializedClosure($this->action); } /** * Run the route action and return the response. * * @return mixed * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ protected function runController() { return $this->controllerDispatcher()->dispatch( $this, $this->getController(), $this->getControllerMethod() ); } /** * Get the controller instance for the route. * * @return mixed */ public function getController() { if (! $this->controller) { $class = $this->getControllerClass(); $this->controller = $this->container->make(ltrim($class, '\\')); } return $this->controller; } /** * Get the controller class used for the route. * * @return string|null */ public function getControllerClass() { return $this->isControllerAction() ? $this->parseControllerCallback()[0] : null; } /** * Get the controller method used for the route. * * @return string */ protected function getControllerMethod() { return $this->parseControllerCallback()[1]; } /** * Parse the controller. * * @return array */ protected function parseControllerCallback() { return Str::parseCallback($this->action['uses']); } /** * Flush the cached container instance on the route. * * @return void */ public function flushController() { $this->computedMiddleware = null; $this->controller = null; } /** * Determine if the route matches a given request. * * @param \Illuminate\Http\Request $request * @param bool $includingMethod * @return bool */ public function matches(Request $request, $includingMethod = true) { $this->compileRoute(); foreach (self::getValidators() as $validator) { if (! $includingMethod && $validator instanceof MethodValidator) { continue; } if (! $validator->matches($this, $request)) { return false; } } return true; } /** * Compile the route into a Symfony CompiledRoute instance. * * @return \Symfony\Component\Routing\CompiledRoute */ protected function compileRoute() { if (! $this->compiled) { $this->compiled = $this->toSymfonyRoute()->compile(); } return $this->compiled; } /** * Bind the route to a given request for execution. * * @param \Illuminate\Http\Request $request * @return $this */ public function bind(Request $request) { $this->compileRoute(); $this->parameters = (new RouteParameterBinder($this)) ->parameters($request); $this->originalParameters = $this->parameters; return $this; } /** * Determine if the route has parameters. * * @return bool */ public function hasParameters() { return isset($this->parameters); } /** * Determine a given parameter exists from the route. * * @param string $name * @return bool */ public function hasParameter($name) { if ($this->hasParameters()) { return array_key_exists($name, $this->parameters()); } return false; } /** * Get a given parameter from the route. * * @param string $name * @param string|object|null $default * @return string|object|null */ public function parameter($name, $default = null) { return Arr::get($this->parameters(), $name, $default); } /** * Get original value of a given parameter from the route. * * @param string $name * @param string|null $default * @return string|null */ public function originalParameter($name, $default = null) { return Arr::get($this->originalParameters(), $name, $default); } /** * Set a parameter to the given value. * * @param string $name * @param string|object|null $value * @return void */ public function setParameter($name, $value) { $this->parameters(); $this->parameters[$name] = $value; } /** * Unset a parameter on the route if it is set. * * @param string $name * @return void */ public function forgetParameter($name) { $this->parameters(); unset($this->parameters[$name]); } /** * Get the key / value list of parameters for the route. * * @return array * * @throws \LogicException */ public function parameters() { if (isset($this->parameters)) { return $this->parameters; } throw new LogicException('Route is not bound.'); } /** * Get the key / value list of original parameters for the route. * * @return array * * @throws \LogicException */ public function originalParameters() { if (isset($this->originalParameters)) { return $this->originalParameters; } throw new LogicException('Route is not bound.'); } /** * Get the key / value list of parameters without null values. * * @return array */ public function parametersWithoutNulls() { return array_filter($this->parameters(), fn ($p) => ! is_null($p)); } /** * Get all of the parameter names for the route. * * @return array */ public function parameterNames() { if (isset($this->parameterNames)) { return $this->parameterNames; } return $this->parameterNames = $this->compileParameterNames(); } /** * Get the parameter names for the route. * * @return array */ protected function compileParameterNames() { preg_match_all('/\{(.*?)\}/', $this->getDomain().$this->uri, $matches); return array_map(fn ($m) => trim($m, '?'), $matches[1]); } /** * Get the parameters that are listed in the route / controller signature. * * @param array $conditions * @return array */ public function signatureParameters($conditions = []) { if (is_string($conditions)) { $conditions = ['subClass' => $conditions]; } return RouteSignatureParameters::fromAction($this->action, $conditions); } /** * Get the binding field for the given parameter. * * @param string|int $parameter * @return string|null */ public function bindingFieldFor($parameter) { $fields = is_int($parameter) ? array_values($this->bindingFields) : $this->bindingFields; return $fields[$parameter] ?? null; } /** * Get the binding fields for the route. * * @return array */ public function bindingFields() { return $this->bindingFields ?? []; } /** * Set the binding fields for the route. * * @param array $bindingFields * @return $this */ public function setBindingFields(array $bindingFields) { $this->bindingFields = $bindingFields; return $this; } /** * Get the parent parameter of the given parameter. * * @param string $parameter * @return string */ public function parentOfParameter($parameter) { $key = array_search($parameter, array_keys($this->parameters)); if ($key === 0) { return; } return array_values($this->parameters)[$key - 1]; } /** * Allow "trashed" models to be retrieved when resolving implicit model bindings for this route. * * @param bool $withTrashed * @return $this */ public function withTrashed($withTrashed = true) { $this->withTrashedBindings = $withTrashed; return $this; } /** * Determines if the route allows "trashed" models to be retrieved when resolving implicit model bindings. * * @return bool */ public function allowsTrashedBindings() { return $this->withTrashedBindings; } /** * Set a default value for the route. * * @param string $key * @param mixed $value * @return $this */ public function defaults($key, $value) { $this->defaults[$key] = $value; return $this; } /** * Set the default values for the route. * * @param array $defaults * @return $this */ public function setDefaults(array $defaults) { $this->defaults = $defaults; return $this; } /** * Set a regular expression requirement on the route. * * @param array|string $name * @param string|null $expression * @return $this */ public function where($name, $expression = null) { foreach ($this->parseWhere($name, $expression) as $name => $expression) { $this->wheres[$name] = $expression; } return $this; } /** * Parse arguments to the where method into an array. * * @param array|string $name * @param string $expression * @return array */ protected function parseWhere($name, $expression) { return is_array($name) ? $name : [$name => $expression]; } /** * Set a list of regular expression requirements on the route. * * @param array $wheres * @return $this */ public function setWheres(array $wheres) { foreach ($wheres as $name => $expression) { $this->where($name, $expression); } return $this; } /** * Mark this route as a fallback route. * * @return $this */ public function fallback() { $this->isFallback = true; return $this; } /** * Set the fallback value. * * @param bool $isFallback * @return $this */ public function setFallback($isFallback) { $this->isFallback = $isFallback; return $this; } /** * Get the HTTP verbs the route responds to. * * @return array */ public function methods() { return $this->methods; } /** * Determine if the route only responds to HTTP requests. * * @return bool */ public function httpOnly() { return in_array('http', $this->action, true); } /** * Determine if the route only responds to HTTPS requests. * * @return bool */ public function httpsOnly() { return $this->secure(); } /** * Determine if the route only responds to HTTPS requests. * * @return bool */ public function secure() { return in_array('https', $this->action, true); } /** * Get or set the domain for the route. * * @param string|null $domain * @return $this|string|null */ public function domain($domain = null) { if (is_null($domain)) { return $this->getDomain(); } $parsed = RouteUri::parse($domain); $this->action['domain'] = $parsed->uri; $this->bindingFields = array_merge( $this->bindingFields, $parsed->bindingFields ); return $this; } /** * Get the domain defined for the route. * * @return string|null */ public function getDomain() { return isset($this->action['domain']) ? str_replace(['http://', 'https://'], '', $this->action['domain']) : null; } /** * Get the prefix of the route instance. * * @return string|null */ public function getPrefix() { return $this->action['prefix'] ?? null; } /** * Add a prefix to the route URI. * * @param string $prefix * @return $this */ public function prefix($prefix) { $prefix ??= ''; $this->updatePrefixOnAction($prefix); $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); return $this->setUri($uri !== '/' ? trim($uri, '/') : $uri); } /** * Update the "prefix" attribute on the action array. * * @param string $prefix * @return void */ protected function updatePrefixOnAction($prefix) { if (! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) { $this->action['prefix'] = $newPrefix; } } /** * Get the URI associated with the route. * * @return string */ public function uri() { return $this->uri; } /** * Set the URI that the route responds to. * * @param string $uri * @return $this */ public function setUri($uri) { $this->uri = $this->parseUri($uri); return $this; } /** * Parse the route URI and normalize / store any implicit binding fields. * * @param string $uri * @return string */ protected function parseUri($uri) { $this->bindingFields = []; return tap(RouteUri::parse($uri), function ($uri) { $this->bindingFields = $uri->bindingFields; })->uri; } /** * Get the name of the route instance. * * @return string|null */ public function getName() { return $this->action['as'] ?? null; } /** * Add or change the route name. * * @param string $name * @return $this */ public function name($name) { $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; return $this; } /** * Determine whether the route's name matches the given patterns. * * @param mixed ...$patterns * @return bool */ public function named(...$patterns) { if (is_null($routeName = $this->getName())) { return false; } foreach ($patterns as $pattern) { if (Str::is($pattern, $routeName)) { return true; } } return false; } /** * Set the handler for the route. * * @param \Closure|array|string $action * @return $this */ public function uses($action) { if (is_array($action)) { $action = $action[0].'@'.$action[1]; } $action = is_string($action) ? $this->addGroupNamespaceToStringUses($action) : $action; return $this->setAction(array_merge($this->action, $this->parseAction([ 'uses' => $action, 'controller' => $action, ]))); } /** * Parse a string based action for the "uses" fluent method. * * @param string $action * @return string */ protected function addGroupNamespaceToStringUses($action) { $groupStack = last($this->router->getGroupStack()); if (isset($groupStack['namespace']) && ! str_starts_with($action, '\\')) { return $groupStack['namespace'].'\\'.$action; } return $action; } /** * Get the action name for the route. * * @return string */ public function getActionName() { return $this->action['controller'] ?? 'Closure'; } /** * Get the method name of the route action. * * @return string */ public function getActionMethod() { return Arr::last(explode('@', $this->getActionName())); } /** * Get the action array or one of its properties for the route. * * @param string|null $key * @return mixed */ public function getAction($key = null) { return Arr::get($this->action, $key); } /** * Set the action array for the route. * * @param array $action * @return $this */ public function setAction(array $action) { $this->action = $action; if (isset($this->action['domain'])) { $this->domain($this->action['domain']); } return $this; } /** * Get the value of the action that should be taken on a missing model exception. * * @return \Closure|null */ public function getMissing() { $missing = $this->action['missing'] ?? null; return is_string($missing) && Str::startsWith($missing, [ 'O:47:"Laravel\\SerializableClosure\\SerializableClosure', 'O:55:"Laravel\\SerializableClosure\\UnsignedSerializableClosure', ]) ? unserialize($missing) : $missing; } /** * Define the callable that should be invoked on a missing model exception. * * @param \Closure $missing * @return $this */ public function missing($missing) { $this->action['missing'] = $missing; return $this; } /** * Get all middleware, including the ones from the controller. * * @return array */ public function gatherMiddleware() { if (! is_null($this->computedMiddleware)) { return $this->computedMiddleware; } $this->computedMiddleware = []; return $this->computedMiddleware = Router::uniqueMiddleware(array_merge( $this->middleware(), $this->controllerMiddleware() )); } /** * Get or set the middlewares attached to the route. * * @param array|string|null $middleware * @return $this|array */ public function middleware($middleware = null) { if (is_null($middleware)) { return (array) ($this->action['middleware'] ?? []); } if (! is_array($middleware)) { $middleware = func_get_args(); } foreach ($middleware as $index => $value) { $middleware[$index] = (string) $value; } $this->action['middleware'] = array_merge( (array) ($this->action['middleware'] ?? []), $middleware ); return $this; } /** * Specify that the "Authorize" / "can" middleware should be applied to the route with the given options. * * @param string $ability * @param array|string $models * @return $this */ public function can($ability, $models = []) { return empty($models) ? $this->middleware(['can:'.$ability]) : $this->middleware(['can:'.$ability.','.implode(',', Arr::wrap($models))]); } /** * Get the middleware for the route's controller. * * @return array */ public function controllerMiddleware() { if (! $this->isControllerAction()) { return []; } [$controllerClass, $controllerMethod] = [ $this->getControllerClass(), $this->getControllerMethod(), ]; if (is_a($controllerClass, HasMiddleware::class, true)) { return $this->staticallyProvidedControllerMiddleware( $controllerClass, $controllerMethod ); } if (method_exists($controllerClass, 'getMiddleware')) { return $this->controllerDispatcher()->getMiddleware( $this->getController(), $controllerMethod ); } return []; } /** * Get the statically provided controller middleware for the given class and method. * * @param string $class * @param string $method * @return array */ protected function staticallyProvidedControllerMiddleware(string $class, string $method) { return collect($class::middleware())->reject(function ($middleware) use ($method) { return static::methodExcludedByOptions( $method, ['only' => $middleware->only, 'except' => $middleware->except] ); })->map->middleware->values()->all(); } /** * Specify middleware that should be removed from the given route. * * @param array|string $middleware * @return $this */ public function withoutMiddleware($middleware) { $this->action['excluded_middleware'] = array_merge( (array) ($this->action['excluded_middleware'] ?? []), Arr::wrap($middleware) ); return $this; } /** * Get the middleware that should be removed from the route. * * @return array */ public function excludedMiddleware() { return (array) ($this->action['excluded_middleware'] ?? []); } /** * Indicate that the route should enforce scoping of multiple implicit Eloquent bindings. * * @return $this */ public function scopeBindings() { $this->action['scope_bindings'] = true; return $this; } /** * Indicate that the route should not enforce scoping of multiple implicit Eloquent bindings. * * @return $this */ public function withoutScopedBindings() { $this->action['scope_bindings'] = false; return $this; } /** * Determine if the route should enforce scoping of multiple implicit Eloquent bindings. * * @return bool */ public function enforcesScopedBindings() { return (bool) ($this->action['scope_bindings'] ?? false); } /** * Determine if the route should prevent scoping of multiple implicit Eloquent bindings. * * @return bool */ public function preventsScopedBindings() { return isset($this->action['scope_bindings']) && $this->action['scope_bindings'] === false; } /** * Specify that the route should not allow concurrent requests from the same session. * * @param int|null $lockSeconds * @param int|null $waitSeconds * @return $this */ public function block($lockSeconds = 10, $waitSeconds = 10) { $this->lockSeconds = $lockSeconds; $this->waitSeconds = $waitSeconds; return $this; } /** * Specify that the route should allow concurrent requests from the same session. * * @return $this */ public function withoutBlocking() { return $this->block(null, null); } /** * Get the maximum number of seconds the route's session lock should be held for. * * @return int|null */ public function locksFor() { return $this->lockSeconds; } /** * Get the maximum number of seconds to wait while attempting to acquire a session lock. * * @return int|null */ public function waitsFor() { return $this->waitSeconds; } /** * Get the dispatcher for the route's controller. * * @return \Illuminate\Routing\Contracts\ControllerDispatcher */ public function controllerDispatcher() { if ($this->container->bound(ControllerDispatcherContract::class)) { return $this->container->make(ControllerDispatcherContract::class); } return new ControllerDispatcher($this->container); } /** * Get the route validators for the instance. * * @return array */ public static function getValidators() { if (isset(static::$validators)) { return static::$validators; } // To match the route, we will use a chain of responsibility pattern with the // validator implementations. We will spin through each one making sure it // passes and then we will know if the route as a whole matches request. return static::$validators = [ new UriValidator, new MethodValidator, new SchemeValidator, new HostValidator, ]; } /** * Convert the route to a Symfony route. * * @return \Symfony\Component\Routing\Route */ public function toSymfonyRoute() { return new SymfonyRoute( preg_replace('/\{(\w+?)\?\}/', '{$1}', $this->uri()), $this->getOptionalParameterNames(), $this->wheres, ['utf8' => true], $this->getDomain() ?: '', [], $this->methods ); } /** * Get the optional parameter names for the route. * * @return array */ protected function getOptionalParameterNames() { preg_match_all('/\{(\w+?)\?\}/', $this->uri(), $matches); return isset($matches[1]) ? array_fill_keys($matches[1], null) : []; } /** * Get the compiled version of the route. * * @return \Symfony\Component\Routing\CompiledRoute */ public function getCompiled() { return $this->compiled; } /** * Set the router instance on the route. * * @param \Illuminate\Routing\Router $router * @return $this */ public function setRouter(Router $router) { $this->router = $router; return $this; } /** * Set the container instance on the route. * * @param \Illuminate\Container\Container $container * @return $this */ public function setContainer(Container $container) { $this->container = $container; return $this; } /** * Prepare the route instance for serialization. * * @return void * * @throws \LogicException */ public function prepareForSerialization() { if ($this->action['uses'] instanceof Closure) { $this->action['uses'] = serialize( SerializableClosure::unsigned($this->action['uses']) ); } if (isset($this->action['missing']) && $this->action['missing'] instanceof Closure) { $this->action['missing'] = serialize( SerializableClosure::unsigned($this->action['missing']) ); } $this->compileRoute(); unset($this->router, $this->container); } /** * Dynamically access route parameters. * * @param string $key * @return mixed */ public function __get($key) { return $this->parameter($key); } } src/Illuminate/Routing/PendingSingletonResourceRegistration.php 0000644 00000012435 15107327037 0021162 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Support\Arr; use Illuminate\Support\Traits\Macroable; class PendingSingletonResourceRegistration { use CreatesRegularExpressionRouteConstraints, Macroable; /** * The resource registrar. * * @var \Illuminate\Routing\ResourceRegistrar */ protected $registrar; /** * The resource name. * * @var string */ protected $name; /** * The resource controller. * * @var string */ protected $controller; /** * The resource options. * * @var array */ protected $options = []; /** * The resource's registration status. * * @var bool */ protected $registered = false; /** * Create a new pending singleton resource registration instance. * * @param \Illuminate\Routing\ResourceRegistrar $registrar * @param string $name * @param string $controller * @param array $options * @return void */ public function __construct(ResourceRegistrar $registrar, $name, $controller, array $options) { $this->name = $name; $this->options = $options; $this->registrar = $registrar; $this->controller = $controller; } /** * Set the methods the controller should apply to. * * @param array|string|mixed $methods * @return \Illuminate\Routing\PendingSingletonResourceRegistration */ public function only($methods) { $this->options['only'] = is_array($methods) ? $methods : func_get_args(); return $this; } /** * Set the methods the controller should exclude. * * @param array|string|mixed $methods * @return \Illuminate\Routing\PendingSingletonResourceRegistration */ public function except($methods) { $this->options['except'] = is_array($methods) ? $methods : func_get_args(); return $this; } /** * Indicate that the resource should have creation and storage routes. * * @return $this */ public function creatable() { $this->options['creatable'] = true; return $this; } /** * Indicate that the resource should have a deletion route. * * @return $this */ public function destroyable() { $this->options['destroyable'] = true; return $this; } /** * Set the route names for controller actions. * * @param array|string $names * @return \Illuminate\Routing\PendingSingletonResourceRegistration */ public function names($names) { $this->options['names'] = $names; return $this; } /** * Set the route name for a controller action. * * @param string $method * @param string $name * @return \Illuminate\Routing\PendingSingletonResourceRegistration */ public function name($method, $name) { $this->options['names'][$method] = $name; return $this; } /** * Override the route parameter names. * * @param array|string $parameters * @return \Illuminate\Routing\PendingSingletonResourceRegistration */ public function parameters($parameters) { $this->options['parameters'] = $parameters; return $this; } /** * Override a route parameter's name. * * @param string $previous * @param string $new * @return \Illuminate\Routing\PendingSingletonResourceRegistration */ public function parameter($previous, $new) { $this->options['parameters'][$previous] = $new; return $this; } /** * Add middleware to the resource routes. * * @param mixed $middleware * @return \Illuminate\Routing\PendingSingletonResourceRegistration */ public function middleware($middleware) { $middleware = Arr::wrap($middleware); foreach ($middleware as $key => $value) { $middleware[$key] = (string) $value; } $this->options['middleware'] = $middleware; return $this; } /** * Specify middleware that should be removed from the resource routes. * * @param array|string $middleware * @return $this|array */ public function withoutMiddleware($middleware) { $this->options['excluded_middleware'] = array_merge( (array) ($this->options['excluded_middleware'] ?? []), Arr::wrap($middleware) ); return $this; } /** * Add "where" constraints to the resource routes. * * @param mixed $wheres * @return \Illuminate\Routing\PendingSingletonResourceRegistration */ public function where($wheres) { $this->options['wheres'] = $wheres; return $this; } /** * Register the singleton resource route. * * @return \Illuminate\Routing\RouteCollection */ public function register() { $this->registered = true; return $this->registrar->singleton( $this->name, $this->controller, $this->options ); } /** * Handle the object's destruction. * * @return void */ public function __destruct() { if (! $this->registered) { $this->register(); } } } src/Illuminate/Routing/RouteCollectionInterface.php 0000644 00000004434 15107327037 0016543 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Http\Request; interface RouteCollectionInterface { /** * Add a Route instance to the collection. * * @param \Illuminate\Routing\Route $route * @return \Illuminate\Routing\Route */ public function add(Route $route); /** * Refresh the name look-up table. * * This is done in case any names are fluently defined or if routes are overwritten. * * @return void */ public function refreshNameLookups(); /** * Refresh the action look-up table. * * This is done in case any actions are overwritten with new controllers. * * @return void */ public function refreshActionLookups(); /** * Find the first route matching a given request. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Routing\Route * * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ public function match(Request $request); /** * Get routes from the collection by method. * * @param string|null $method * @return \Illuminate\Routing\Route[] */ public function get($method = null); /** * Determine if the route collection contains a given named route. * * @param string $name * @return bool */ public function hasNamedRoute($name); /** * Get a route instance by its name. * * @param string $name * @return \Illuminate\Routing\Route|null */ public function getByName($name); /** * Get a route instance by its controller action. * * @param string $action * @return \Illuminate\Routing\Route|null */ public function getByAction($action); /** * Get all of the routes in the collection. * * @return \Illuminate\Routing\Route[] */ public function getRoutes(); /** * Get all of the routes keyed by their HTTP verb / method. * * @return array */ public function getRoutesByMethod(); /** * Get all of the routes keyed by their name. * * @return \Illuminate\Routing\Route[] */ public function getRoutesByName(); } src/Illuminate/Routing/RouteUrlGenerator.php 0000644 00000022064 15107327037 0015237 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Routing\Exceptions\UrlGenerationException; use Illuminate\Support\Arr; class RouteUrlGenerator { /** * The URL generator instance. * * @var \Illuminate\Routing\UrlGenerator */ protected $url; /** * The request instance. * * @var \Illuminate\Http\Request */ protected $request; /** * The named parameter defaults. * * @var array */ public $defaultParameters = []; /** * Characters that should not be URL encoded. * * @var array */ public $dontEncode = [ '%2F' => '/', '%40' => '@', '%3A' => ':', '%3B' => ';', '%2C' => ',', '%3D' => '=', '%2B' => '+', '%21' => '!', '%2A' => '*', '%7C' => '|', '%3F' => '?', '%26' => '&', '%23' => '#', '%25' => '%', ]; /** * Create a new Route URL generator. * * @param \Illuminate\Routing\UrlGenerator $url * @param \Illuminate\Http\Request $request * @return void */ public function __construct($url, $request) { $this->url = $url; $this->request = $request; } /** * Generate a URL for the given route. * * @param \Illuminate\Routing\Route $route * @param array $parameters * @param bool $absolute * @return string * * @throws \Illuminate\Routing\Exceptions\UrlGenerationException */ public function to($route, $parameters = [], $absolute = false) { $domain = $this->getRouteDomain($route, $parameters); // First we will construct the entire URI including the root and query string. Once it // has been constructed, we'll make sure we don't have any missing parameters or we // will need to throw the exception to let the developers know one was not given. $uri = $this->addQueryString($this->url->format( $root = $this->replaceRootParameters($route, $domain, $parameters), $this->replaceRouteParameters($route->uri(), $parameters), $route ), $parameters); if (preg_match_all('/{(.*?)}/', $uri, $matchedMissingParameters)) { throw UrlGenerationException::forMissingParameters($route, $matchedMissingParameters[1]); } // Once we have ensured that there are no missing parameters in the URI we will encode // the URI and prepare it for returning to the developer. If the URI is supposed to // be absolute, we will return it as-is. Otherwise we will remove the URL's root. $uri = strtr(rawurlencode($uri), $this->dontEncode); if (! $absolute) { $uri = preg_replace('#^(//|[^/?])+#', '', $uri); if ($base = $this->request->getBaseUrl()) { $uri = preg_replace('#^'.$base.'#i', '', $uri); } return '/'.ltrim($uri, '/'); } return $uri; } /** * Get the formatted domain for a given route. * * @param \Illuminate\Routing\Route $route * @param array $parameters * @return string */ protected function getRouteDomain($route, &$parameters) { return $route->getDomain() ? $this->formatDomain($route, $parameters) : null; } /** * Format the domain and port for the route and request. * * @param \Illuminate\Routing\Route $route * @param array $parameters * @return string */ protected function formatDomain($route, &$parameters) { return $this->addPortToDomain( $this->getRouteScheme($route).$route->getDomain() ); } /** * Get the scheme for the given route. * * @param \Illuminate\Routing\Route $route * @return string */ protected function getRouteScheme($route) { if ($route->httpOnly()) { return 'http://'; } elseif ($route->httpsOnly()) { return 'https://'; } return $this->url->formatScheme(); } /** * Add the port to the domain if necessary. * * @param string $domain * @return string */ protected function addPortToDomain($domain) { $secure = $this->request->isSecure(); $port = (int) $this->request->getPort(); return ($secure && $port === 443) || (! $secure && $port === 80) ? $domain : $domain.':'.$port; } /** * Replace the parameters on the root path. * * @param \Illuminate\Routing\Route $route * @param string $domain * @param array $parameters * @return string */ protected function replaceRootParameters($route, $domain, &$parameters) { $scheme = $this->getRouteScheme($route); return $this->replaceRouteParameters( $this->url->formatRoot($scheme, $domain), $parameters ); } /** * Replace all of the wildcard parameters for a route path. * * @param string $path * @param array $parameters * @return string */ protected function replaceRouteParameters($path, array &$parameters) { $path = $this->replaceNamedParameters($path, $parameters); $path = preg_replace_callback('/\{.*?\}/', function ($match) use (&$parameters) { // Reset only the numeric keys... $parameters = array_merge($parameters); return (! isset($parameters[0]) && ! str_ends_with($match[0], '?}')) ? $match[0] : Arr::pull($parameters, 0); }, $path); return trim(preg_replace('/\{.*?\?\}/', '', $path), '/'); } /** * Replace all of the named parameters in the path. * * @param string $path * @param array $parameters * @return string */ protected function replaceNamedParameters($path, &$parameters) { return preg_replace_callback('/\{(.*?)(\?)?\}/', function ($m) use (&$parameters) { if (isset($parameters[$m[1]]) && $parameters[$m[1]] !== '') { return Arr::pull($parameters, $m[1]); } elseif (isset($this->defaultParameters[$m[1]])) { return $this->defaultParameters[$m[1]]; } elseif (isset($parameters[$m[1]])) { Arr::pull($parameters, $m[1]); } return $m[0]; }, $path); } /** * Add a query string to the URI. * * @param string $uri * @param array $parameters * @return mixed|string */ protected function addQueryString($uri, array $parameters) { // If the URI has a fragment we will move it to the end of this URI since it will // need to come after any query string that may be added to the URL else it is // not going to be available. We will remove it then append it back on here. if (! is_null($fragment = parse_url($uri, PHP_URL_FRAGMENT))) { $uri = preg_replace('/#.*/', '', $uri); } $uri .= $this->getRouteQueryString($parameters); return is_null($fragment) ? $uri : $uri."#{$fragment}"; } /** * Get the query string for a given route. * * @param array $parameters * @return string */ protected function getRouteQueryString(array $parameters) { // First we will get all of the string parameters that are remaining after we // have replaced the route wildcards. We'll then build a query string from // these string parameters then use it as a starting point for the rest. if (count($parameters) === 0) { return ''; } $query = Arr::query( $keyed = $this->getStringParameters($parameters) ); // Lastly, if there are still parameters remaining, we will fetch the numeric // parameters that are in the array and add them to the query string or we // will make the initial query string if it wasn't started with strings. if (count($keyed) < count($parameters)) { $query .= '&'.implode( '&', $this->getNumericParameters($parameters) ); } $query = trim($query, '&'); return $query === '' ? '' : "?{$query}"; } /** * Get the string parameters from a given list. * * @param array $parameters * @return array */ protected function getStringParameters(array $parameters) { return array_filter($parameters, 'is_string', ARRAY_FILTER_USE_KEY); } /** * Get the numeric parameters from a given list. * * @param array $parameters * @return array */ protected function getNumericParameters(array $parameters) { return array_filter($parameters, 'is_numeric', ARRAY_FILTER_USE_KEY); } /** * Set the default named parameters used by the URL generator. * * @param array $defaults * @return void */ public function defaults(array $defaults) { $this->defaultParameters = array_merge( $this->defaultParameters, $defaults ); } } src/Illuminate/Routing/AbstractRouteCollection.php 0000644 00000021031 15107327037 0016376 0 ustar 00 <?php namespace Illuminate\Routing; use ArrayIterator; use Countable; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Str; use IteratorAggregate; use LogicException; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper; use Symfony\Component\Routing\RouteCollection as SymfonyRouteCollection; use Traversable; abstract class AbstractRouteCollection implements Countable, IteratorAggregate, RouteCollectionInterface { /** * Handle the matched route. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Routing\Route|null $route * @return \Illuminate\Routing\Route * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ protected function handleMatchedRoute(Request $request, $route) { if (! is_null($route)) { return $route->bind($request); } // If no route was found we will now check if a matching route is specified by // another HTTP verb. If it is we will need to throw a MethodNotAllowed and // inform the user agent of which HTTP verb it should use for this route. $others = $this->checkForAlternateVerbs($request); if (count($others) > 0) { return $this->getRouteForMethods($request, $others); } throw new NotFoundHttpException(sprintf( 'The route %s could not be found.', $request->path() )); } /** * Determine if any routes match on another HTTP verb. * * @param \Illuminate\Http\Request $request * @return array */ protected function checkForAlternateVerbs($request) { $methods = array_diff(Router::$verbs, [$request->getMethod()]); // Here we will spin through all verbs except for the current request verb and // check to see if any routes respond to them. If they do, we will return a // proper error response with the correct headers on the response string. return array_values(array_filter( $methods, function ($method) use ($request) { return ! is_null($this->matchAgainstRoutes($this->get($method), $request, false)); } )); } /** * Determine if a route in the array matches the request. * * @param \Illuminate\Routing\Route[] $routes * @param \Illuminate\Http\Request $request * @param bool $includingMethod * @return \Illuminate\Routing\Route|null */ protected function matchAgainstRoutes(array $routes, $request, $includingMethod = true) { [$fallbacks, $routes] = collect($routes)->partition(function ($route) { return $route->isFallback; }); return $routes->merge($fallbacks)->first( fn (Route $route) => $route->matches($request, $includingMethod) ); } /** * Get a route (if necessary) that responds when other available methods are present. * * @param \Illuminate\Http\Request $request * @param string[] $methods * @return \Illuminate\Routing\Route * * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException */ protected function getRouteForMethods($request, array $methods) { if ($request->isMethod('OPTIONS')) { return (new Route('OPTIONS', $request->path(), function () use ($methods) { return new Response('', 200, ['Allow' => implode(',', $methods)]); }))->bind($request); } $this->requestMethodNotAllowed($request, $methods, $request->method()); } /** * Throw a method not allowed HTTP exception. * * @param \Illuminate\Http\Request $request * @param array $others * @param string $method * @return void * * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException */ protected function requestMethodNotAllowed($request, array $others, $method) { throw new MethodNotAllowedHttpException( $others, sprintf( 'The %s method is not supported for route %s. Supported methods: %s.', $method, $request->path(), implode(', ', $others) ) ); } /** * Throw a method not allowed HTTP exception. * * @param array $others * @param string $method * @return void * * @deprecated use requestMethodNotAllowed * * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException */ protected function methodNotAllowed(array $others, $method) { throw new MethodNotAllowedHttpException( $others, sprintf( 'The %s method is not supported for this route. Supported methods: %s.', $method, implode(', ', $others) ) ); } /** * Compile the routes for caching. * * @return array */ public function compile() { $compiled = $this->dumper()->getCompiledRoutes(); $attributes = []; foreach ($this->getRoutes() as $route) { $attributes[$route->getName()] = [ 'methods' => $route->methods(), 'uri' => $route->uri(), 'action' => $route->getAction(), 'fallback' => $route->isFallback, 'defaults' => $route->defaults, 'wheres' => $route->wheres, 'bindingFields' => $route->bindingFields(), 'lockSeconds' => $route->locksFor(), 'waitSeconds' => $route->waitsFor(), 'withTrashed' => $route->allowsTrashedBindings(), ]; } return compact('compiled', 'attributes'); } /** * Return the CompiledUrlMatcherDumper instance for the route collection. * * @return \Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper */ public function dumper() { return new CompiledUrlMatcherDumper($this->toSymfonyRouteCollection()); } /** * Convert the collection to a Symfony RouteCollection instance. * * @return \Symfony\Component\Routing\RouteCollection */ public function toSymfonyRouteCollection() { $symfonyRoutes = new SymfonyRouteCollection; $routes = $this->getRoutes(); foreach ($routes as $route) { if (! $route->isFallback) { $symfonyRoutes = $this->addToSymfonyRoutesCollection($symfonyRoutes, $route); } } foreach ($routes as $route) { if ($route->isFallback) { $symfonyRoutes = $this->addToSymfonyRoutesCollection($symfonyRoutes, $route); } } return $symfonyRoutes; } /** * Add a route to the SymfonyRouteCollection instance. * * @param \Symfony\Component\Routing\RouteCollection $symfonyRoutes * @param \Illuminate\Routing\Route $route * @return \Symfony\Component\Routing\RouteCollection * * @throws \LogicException */ protected function addToSymfonyRoutesCollection(SymfonyRouteCollection $symfonyRoutes, Route $route) { $name = $route->getName(); if ( ! is_null($name) && str_ends_with($name, '.') && ! is_null($symfonyRoutes->get($name)) ) { $name = null; } if (! $name) { $route->name($this->generateRouteName()); $this->add($route); } elseif (! is_null($symfonyRoutes->get($name))) { throw new LogicException("Unable to prepare route [{$route->uri}] for serialization. Another route has already been assigned name [{$name}]."); } $symfonyRoutes->add($route->getName(), $route->toSymfonyRoute()); return $symfonyRoutes; } /** * Get a randomly generated route name. * * @return string */ protected function generateRouteName() { return 'generated::'.Str::random(); } /** * Get an iterator for the items. * * @return \ArrayIterator */ public function getIterator(): Traversable { return new ArrayIterator($this->getRoutes()); } /** * Count the number of items in the collection. * * @return int */ public function count(): int { return count($this->getRoutes()); } } src/Illuminate/Routing/RouteAction.php 0000644 00000006610 15107327037 0014042 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Support\Arr; use Illuminate\Support\Reflector; use Illuminate\Support\Str; use LogicException; use UnexpectedValueException; class RouteAction { /** * Parse the given action into an array. * * @param string $uri * @param mixed $action * @return array */ public static function parse($uri, $action) { // If no action is passed in right away, we assume the user will make use of // fluent routing. In that case, we set a default closure, to be executed // if the user never explicitly sets an action to handle the given uri. if (is_null($action)) { return static::missingAction($uri); } // If the action is already a Closure instance, we will just set that instance // as the "uses" property, because there is nothing else we need to do when // it is available. Otherwise we will need to find it in the action list. if (Reflector::isCallable($action, true)) { return ! is_array($action) ? ['uses' => $action] : [ 'uses' => $action[0].'@'.$action[1], 'controller' => $action[0].'@'.$action[1], ]; } // If no "uses" property has been set, we will dig through the array to find a // Closure instance within this list. We will set the first Closure we come // across into the "uses" property that will get fired off by this route. elseif (! isset($action['uses'])) { $action['uses'] = static::findCallable($action); } if (! static::containsSerializedClosure($action) && is_string($action['uses']) && ! str_contains($action['uses'], '@')) { $action['uses'] = static::makeInvokable($action['uses']); } return $action; } /** * Get an action for a route that has no action. * * @param string $uri * @return array * * @throws \LogicException */ protected static function missingAction($uri) { return ['uses' => function () use ($uri) { throw new LogicException("Route for [{$uri}] has no action."); }]; } /** * Find the callable in an action array. * * @param array $action * @return callable */ protected static function findCallable(array $action) { return Arr::first($action, function ($value, $key) { return Reflector::isCallable($value) && is_numeric($key); }); } /** * Make an action for an invokable controller. * * @param string $action * @return string * * @throws \UnexpectedValueException */ protected static function makeInvokable($action) { if (! method_exists($action, '__invoke')) { throw new UnexpectedValueException("Invalid route action: [{$action}]."); } return $action.'@__invoke'; } /** * Determine if the given array actions contain a serialized Closure. * * @param array $action * @return bool */ public static function containsSerializedClosure(array $action) { return is_string($action['uses']) && Str::startsWith($action['uses'], [ 'O:47:"Laravel\\SerializableClosure\\SerializableClosure', 'O:55:"Laravel\\SerializableClosure\\UnsignedSerializableClosure', ]) !== false; } } src/Illuminate/Routing/RouteUri.php 0000644 00000002444 15107327037 0013365 0 ustar 00 <?php namespace Illuminate\Routing; class RouteUri { /** * The route URI. * * @var string */ public $uri; /** * The fields that should be used when resolving bindings. * * @var array */ public $bindingFields = []; /** * Create a new route URI instance. * * @param string $uri * @param array $bindingFields * @return void */ public function __construct(string $uri, array $bindingFields = []) { $this->uri = $uri; $this->bindingFields = $bindingFields; } /** * Parse the given URI. * * @param string $uri * @return static */ public static function parse($uri) { preg_match_all('/\{([\w\:]+?)\??\}/', $uri, $matches); $bindingFields = []; foreach ($matches[0] as $match) { if (! str_contains($match, ':')) { continue; } $segments = explode(':', trim($match, '{}?')); $bindingFields[$segments[0]] = $segments[1]; $uri = str_contains($match, '?') ? str_replace($match, '{'.$segments[0].'?}', $uri) : str_replace($match, '{'.$segments[0].'}', $uri); } return new static($uri, $bindingFields); } } src/Illuminate/Routing/RouteBinding.php 0000644 00000005343 15107327037 0014201 0 ustar 00 <?php namespace Illuminate\Routing; use Closure; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Str; class RouteBinding { /** * Create a Route model binding for a given callback. * * @param \Illuminate\Container\Container $container * @param \Closure|string $binder * @return \Closure */ public static function forCallback($container, $binder) { if (is_string($binder)) { return static::createClassBinding($container, $binder); } return $binder; } /** * Create a class based binding using the IoC container. * * @param \Illuminate\Container\Container $container * @param string $binding * @return \Closure */ protected static function createClassBinding($container, $binding) { return function ($value, $route) use ($container, $binding) { // If the binding has an @ sign, we will assume it's being used to delimit // the class name from the bind method name. This allows for bindings // to run multiple bind methods in a single class for convenience. [$class, $method] = Str::parseCallback($binding, 'bind'); $callable = [$container->make($class), $method]; return $callable($value, $route); }; } /** * Create a Route model binding for a model. * * @param \Illuminate\Container\Container $container * @param string $class * @param \Closure|null $callback * @return \Closure * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public static function forModel($container, $class, $callback = null) { return function ($value) use ($container, $class, $callback) { if (is_null($value)) { return; } // For model binders, we will attempt to retrieve the models using the first // method on the model instance. If we cannot retrieve the models we'll // throw a not found exception otherwise we will return the instance. $instance = $container->make($class); if ($model = $instance->resolveRouteBinding($value)) { return $model; } // If a callback was supplied to the method we will call that to determine // what we should do when the model is not found. This just gives these // developer a little greater flexibility to decide what will happen. if ($callback instanceof Closure) { return $callback($value); } throw (new ModelNotFoundException)->setModel($class); }; } } src/Illuminate/Routing/Controllers/HasMiddleware.php 0000644 00000000423 15107327037 0016621 0 ustar 00 <?php namespace Illuminate\Routing\Controllers; interface HasMiddleware { /** * Get the middleware that should be assigned to the controller. * * @return \Illuminate\Routing\Controllers\Middleware|array */ public static function middleware(); } src/Illuminate/Routing/Controllers/Middleware.php 0000644 00000002464 15107327037 0016174 0 ustar 00 <?php namespace Illuminate\Routing\Controllers; use Closure; use Illuminate\Support\Arr; class Middleware { /** * The middleware that should be assigned. * * @var \Closure|string|array */ public $middleware; /** * The controller methods the middleware should only apply to. * * @var array|null */ public $only; /** * The controller methods the middleware should not apply to. * * @var array|null */ public $except; /** * Create a new controller middleware definition. * * @param \Closure|string|array $middleware * @return void */ public function __construct(Closure|string|array $middleware) { $this->middleware = $middleware; } /** * Specify the only controller methods the middleware should apply to. * * @param array|string $only * @return $this */ public function only(array|string $only) { $this->only = Arr::wrap($only); return $this; } /** * Specify the controller methods the middleware should not apply to. * * @param array|string $except * @return $this */ public function except(array|string $except) { $this->except = Arr::wrap($except); return $this; } } src/Illuminate/Routing/RouteCollection.php 0000644 00000015613 15107327037 0014723 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Container\Container; use Illuminate\Http\Request; use Illuminate\Support\Arr; class RouteCollection extends AbstractRouteCollection { /** * An array of the routes keyed by method. * * @var array */ protected $routes = []; /** * A flattened array of all of the routes. * * @var \Illuminate\Routing\Route[] */ protected $allRoutes = []; /** * A look-up table of routes by their names. * * @var \Illuminate\Routing\Route[] */ protected $nameList = []; /** * A look-up table of routes by controller action. * * @var \Illuminate\Routing\Route[] */ protected $actionList = []; /** * Add a Route instance to the collection. * * @param \Illuminate\Routing\Route $route * @return \Illuminate\Routing\Route */ public function add(Route $route) { $this->addToCollections($route); $this->addLookups($route); return $route; } /** * Add the given route to the arrays of routes. * * @param \Illuminate\Routing\Route $route * @return void */ protected function addToCollections($route) { $domainAndUri = $route->getDomain().$route->uri(); foreach ($route->methods() as $method) { $this->routes[$method][$domainAndUri] = $route; } $this->allRoutes[$method.$domainAndUri] = $route; } /** * Add the route to any look-up tables if necessary. * * @param \Illuminate\Routing\Route $route * @return void */ protected function addLookups($route) { // If the route has a name, we will add it to the name look-up table so that we // will quickly be able to find any route associate with a name and not have // to iterate through every route every time we need to perform a look-up. if ($name = $route->getName()) { $this->nameList[$name] = $route; } // When the route is routing to a controller we will also store the action that // is used by the route. This will let us reverse route to controllers while // processing a request and easily generate URLs to the given controllers. $action = $route->getAction(); if (isset($action['controller'])) { $this->addToActionList($action, $route); } } /** * Add a route to the controller action dictionary. * * @param array $action * @param \Illuminate\Routing\Route $route * @return void */ protected function addToActionList($action, $route) { $this->actionList[trim($action['controller'], '\\')] = $route; } /** * Refresh the name look-up table. * * This is done in case any names are fluently defined or if routes are overwritten. * * @return void */ public function refreshNameLookups() { $this->nameList = []; foreach ($this->allRoutes as $route) { if ($route->getName()) { $this->nameList[$route->getName()] = $route; } } } /** * Refresh the action look-up table. * * This is done in case any actions are overwritten with new controllers. * * @return void */ public function refreshActionLookups() { $this->actionList = []; foreach ($this->allRoutes as $route) { if (isset($route->getAction()['controller'])) { $this->addToActionList($route->getAction(), $route); } } } /** * Find the first route matching a given request. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Routing\Route * * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ public function match(Request $request) { $routes = $this->get($request->getMethod()); // First, we will see if we can find a matching route for this current request // method. If we can, great, we can just return it so that it can be called // by the consumer. Otherwise we will check for routes with another verb. $route = $this->matchAgainstRoutes($routes, $request); return $this->handleMatchedRoute($request, $route); } /** * Get routes from the collection by method. * * @param string|null $method * @return \Illuminate\Routing\Route[] */ public function get($method = null) { return is_null($method) ? $this->getRoutes() : Arr::get($this->routes, $method, []); } /** * Determine if the route collection contains a given named route. * * @param string $name * @return bool */ public function hasNamedRoute($name) { return ! is_null($this->getByName($name)); } /** * Get a route instance by its name. * * @param string $name * @return \Illuminate\Routing\Route|null */ public function getByName($name) { return $this->nameList[$name] ?? null; } /** * Get a route instance by its controller action. * * @param string $action * @return \Illuminate\Routing\Route|null */ public function getByAction($action) { return $this->actionList[$action] ?? null; } /** * Get all of the routes in the collection. * * @return \Illuminate\Routing\Route[] */ public function getRoutes() { return array_values($this->allRoutes); } /** * Get all of the routes keyed by their HTTP verb / method. * * @return array */ public function getRoutesByMethod() { return $this->routes; } /** * Get all of the routes keyed by their name. * * @return \Illuminate\Routing\Route[] */ public function getRoutesByName() { return $this->nameList; } /** * Convert the collection to a Symfony RouteCollection instance. * * @return \Symfony\Component\Routing\RouteCollection */ public function toSymfonyRouteCollection() { $symfonyRoutes = parent::toSymfonyRouteCollection(); $this->refreshNameLookups(); return $symfonyRoutes; } /** * Convert the collection to a CompiledRouteCollection instance. * * @param \Illuminate\Routing\Router $router * @param \Illuminate\Container\Container $container * @return \Illuminate\Routing\CompiledRouteCollection */ public function toCompiledRouteCollection(Router $router, Container $container) { ['compiled' => $compiled, 'attributes' => $attributes] = $this->compile(); return (new CompiledRouteCollection($compiled, $attributes)) ->setRouter($router) ->setContainer($container); } } src/Illuminate/Routing/RouteGroup.php 0000644 00000005262 15107327037 0013723 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Support\Arr; class RouteGroup { /** * Merge route groups into a new array. * * @param array $new * @param array $old * @param bool $prependExistingPrefix * @return array */ public static function merge($new, $old, $prependExistingPrefix = true) { if (isset($new['domain'])) { unset($old['domain']); } if (isset($new['controller'])) { unset($old['controller']); } $new = array_merge(static::formatAs($new, $old), [ 'namespace' => static::formatNamespace($new, $old), 'prefix' => static::formatPrefix($new, $old, $prependExistingPrefix), 'where' => static::formatWhere($new, $old), ]); return array_merge_recursive(Arr::except( $old, ['namespace', 'prefix', 'where', 'as'] ), $new); } /** * Format the namespace for the new group attributes. * * @param array $new * @param array $old * @return string|null */ protected static function formatNamespace($new, $old) { if (isset($new['namespace'])) { return isset($old['namespace']) && ! str_starts_with($new['namespace'], '\\') ? trim($old['namespace'], '\\').'\\'.trim($new['namespace'], '\\') : trim($new['namespace'], '\\'); } return $old['namespace'] ?? null; } /** * Format the prefix for the new group attributes. * * @param array $new * @param array $old * @param bool $prependExistingPrefix * @return string|null */ protected static function formatPrefix($new, $old, $prependExistingPrefix = true) { $old = $old['prefix'] ?? ''; if ($prependExistingPrefix) { return isset($new['prefix']) ? trim($old, '/').'/'.trim($new['prefix'], '/') : $old; } return isset($new['prefix']) ? trim($new['prefix'], '/').'/'.trim($old, '/') : $old; } /** * Format the "wheres" for the new group attributes. * * @param array $new * @param array $old * @return array */ protected static function formatWhere($new, $old) { return array_merge( $old['where'] ?? [], $new['where'] ?? [] ); } /** * Format the "as" clause of the new group attributes. * * @param array $new * @param array $old * @return array */ protected static function formatAs($new, $old) { if (isset($old['as'])) { $new['as'] = $old['as'].($new['as'] ?? ''); } return $new; } } src/Illuminate/Routing/ResponseFactory.php 0000644 00000017633 15107327037 0014743 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Contracts\Routing\ResponseFactory as FactoryContract; use Illuminate\Contracts\View\Factory as ViewFactory; use Illuminate\Http\JsonResponse; use Illuminate\Http\Response; use Illuminate\Routing\Exceptions\StreamedResponseException; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\StreamedResponse; use Throwable; class ResponseFactory implements FactoryContract { use Macroable; /** * The view factory instance. * * @var \Illuminate\Contracts\View\Factory */ protected $view; /** * The redirector instance. * * @var \Illuminate\Routing\Redirector */ protected $redirector; /** * Create a new response factory instance. * * @param \Illuminate\Contracts\View\Factory $view * @param \Illuminate\Routing\Redirector $redirector * @return void */ public function __construct(ViewFactory $view, Redirector $redirector) { $this->view = $view; $this->redirector = $redirector; } /** * Create a new response instance. * * @param mixed $content * @param int $status * @param array $headers * @return \Illuminate\Http\Response */ public function make($content = '', $status = 200, array $headers = []) { return new Response($content, $status, $headers); } /** * Create a new "no content" response. * * @param int $status * @param array $headers * @return \Illuminate\Http\Response */ public function noContent($status = 204, array $headers = []) { return $this->make('', $status, $headers); } /** * Create a new response for a given view. * * @param string|array $view * @param array $data * @param int $status * @param array $headers * @return \Illuminate\Http\Response */ public function view($view, $data = [], $status = 200, array $headers = []) { if (is_array($view)) { return $this->make($this->view->first($view, $data), $status, $headers); } return $this->make($this->view->make($view, $data), $status, $headers); } /** * Create a new JSON response instance. * * @param mixed $data * @param int $status * @param array $headers * @param int $options * @return \Illuminate\Http\JsonResponse */ public function json($data = [], $status = 200, array $headers = [], $options = 0) { return new JsonResponse($data, $status, $headers, $options); } /** * Create a new JSONP response instance. * * @param string $callback * @param mixed $data * @param int $status * @param array $headers * @param int $options * @return \Illuminate\Http\JsonResponse */ public function jsonp($callback, $data = [], $status = 200, array $headers = [], $options = 0) { return $this->json($data, $status, $headers, $options)->setCallback($callback); } /** * Create a new streamed response instance. * * @param callable $callback * @param int $status * @param array $headers * @return \Symfony\Component\HttpFoundation\StreamedResponse */ public function stream($callback, $status = 200, array $headers = []) { return new StreamedResponse($callback, $status, $headers); } /** * Create a new streamed response instance as a file download. * * @param callable $callback * @param string|null $name * @param array $headers * @param string|null $disposition * @return \Symfony\Component\HttpFoundation\StreamedResponse */ public function streamDownload($callback, $name = null, array $headers = [], $disposition = 'attachment') { $withWrappedException = function () use ($callback) { try { $callback(); } catch (Throwable $e) { throw new StreamedResponseException($e); } }; $response = new StreamedResponse($withWrappedException, 200, $headers); if (! is_null($name)) { $response->headers->set('Content-Disposition', $response->headers->makeDisposition( $disposition, $name, $this->fallbackName($name) )); } return $response; } /** * Create a new file download response. * * @param \SplFileInfo|string $file * @param string|null $name * @param array $headers * @param string|null $disposition * @return \Symfony\Component\HttpFoundation\BinaryFileResponse */ public function download($file, $name = null, array $headers = [], $disposition = 'attachment') { $response = new BinaryFileResponse($file, 200, $headers, true, $disposition); if (! is_null($name)) { return $response->setContentDisposition($disposition, $name, $this->fallbackName($name)); } return $response; } /** * Convert the string to ASCII characters that are equivalent to the given name. * * @param string $name * @return string */ protected function fallbackName($name) { return str_replace('%', '', Str::ascii($name)); } /** * Return the raw contents of a binary file. * * @param \SplFileInfo|string $file * @param array $headers * @return \Symfony\Component\HttpFoundation\BinaryFileResponse */ public function file($file, array $headers = []) { return new BinaryFileResponse($file, 200, $headers); } /** * Create a new redirect response to the given path. * * @param string $path * @param int $status * @param array $headers * @param bool|null $secure * @return \Illuminate\Http\RedirectResponse */ public function redirectTo($path, $status = 302, $headers = [], $secure = null) { return $this->redirector->to($path, $status, $headers, $secure); } /** * Create a new redirect response to a named route. * * @param string $route * @param mixed $parameters * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function redirectToRoute($route, $parameters = [], $status = 302, $headers = []) { return $this->redirector->route($route, $parameters, $status, $headers); } /** * Create a new redirect response to a controller action. * * @param array|string $action * @param mixed $parameters * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function redirectToAction($action, $parameters = [], $status = 302, $headers = []) { return $this->redirector->action($action, $parameters, $status, $headers); } /** * Create a new redirect response, while putting the current URL in the session. * * @param string $path * @param int $status * @param array $headers * @param bool|null $secure * @return \Illuminate\Http\RedirectResponse */ public function redirectGuest($path, $status = 302, $headers = [], $secure = null) { return $this->redirector->guest($path, $status, $headers, $secure); } /** * Create a new redirect response to the previously intended location. * * @param string $default * @param int $status * @param array $headers * @param bool|null $secure * @return \Illuminate\Http\RedirectResponse */ public function redirectToIntended($default = '/', $status = 302, $headers = [], $secure = null) { return $this->redirector->intended($default, $status, $headers, $secure); } } src/Illuminate/Routing/FiltersControllerMiddleware.php 0000644 00000001014 15107327037 0017251 0 ustar 00 <?php namespace Illuminate\Routing; trait FiltersControllerMiddleware { /** * Determine if the given options exclude a particular method. * * @param string $method * @param array $options * @return bool */ public static function methodExcludedByOptions($method, array $options) { return (isset($options['only']) && ! in_array($method, (array) $options['only'])) || (! empty($options['except']) && in_array($method, (array) $options['except'])); } } src/Illuminate/Routing/UrlGenerator.php 0000644 00000053776 15107327037 0014236 0 ustar 00 <?php namespace Illuminate\Routing; use BackedEnum; use Closure; use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract; use Illuminate\Contracts\Routing\UrlRoutable; use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Carbon; use Illuminate\Support\InteractsWithTime; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; use Symfony\Component\Routing\Exception\RouteNotFoundException; class UrlGenerator implements UrlGeneratorContract { use InteractsWithTime, Macroable; /** * The route collection. * * @var \Illuminate\Routing\RouteCollectionInterface */ protected $routes; /** * The request instance. * * @var \Illuminate\Http\Request */ protected $request; /** * The asset root URL. * * @var string */ protected $assetRoot; /** * The forced URL root. * * @var string */ protected $forcedRoot; /** * The forced scheme for URLs. * * @var string */ protected $forceScheme; /** * A cached copy of the URL root for the current request. * * @var string|null */ protected $cachedRoot; /** * A cached copy of the URL scheme for the current request. * * @var string|null */ protected $cachedScheme; /** * The root namespace being applied to controller actions. * * @var string */ protected $rootNamespace; /** * The session resolver callable. * * @var callable */ protected $sessionResolver; /** * The encryption key resolver callable. * * @var callable */ protected $keyResolver; /** * The missing named route resolver callable. * * @var callable */ protected $missingNamedRouteResolver; /** * The callback to use to format hosts. * * @var \Closure */ protected $formatHostUsing; /** * The callback to use to format paths. * * @var \Closure */ protected $formatPathUsing; /** * The route URL generator instance. * * @var \Illuminate\Routing\RouteUrlGenerator|null */ protected $routeGenerator; /** * Create a new URL Generator instance. * * @param \Illuminate\Routing\RouteCollectionInterface $routes * @param \Illuminate\Http\Request $request * @param string|null $assetRoot * @return void */ public function __construct(RouteCollectionInterface $routes, Request $request, $assetRoot = null) { $this->routes = $routes; $this->assetRoot = $assetRoot; $this->setRequest($request); } /** * Get the full URL for the current request. * * @return string */ public function full() { return $this->request->fullUrl(); } /** * Get the current URL for the request. * * @return string */ public function current() { return $this->to($this->request->getPathInfo()); } /** * Get the URL for the previous request. * * @param mixed $fallback * @return string */ public function previous($fallback = false) { $referrer = $this->request->headers->get('referer'); $url = $referrer ? $this->to($referrer) : $this->getPreviousUrlFromSession(); if ($url) { return $url; } elseif ($fallback) { return $this->to($fallback); } return $this->to('/'); } /** * Get the previous path info for the request. * * @param mixed $fallback * @return string */ public function previousPath($fallback = false) { $previousPath = str_replace($this->to('/'), '', rtrim(preg_replace('/\?.*/', '', $this->previous($fallback)), '/')); return $previousPath === '' ? '/' : $previousPath; } /** * Get the previous URL from the session if possible. * * @return string|null */ protected function getPreviousUrlFromSession() { $session = $this->getSession(); return $session ? $session->previousUrl() : null; } /** * Generate an absolute URL to the given path. * * @param string $path * @param mixed $extra * @param bool|null $secure * @return string */ public function to($path, $extra = [], $secure = null) { // First we will check if the URL is already a valid URL. If it is we will not // try to generate a new one but will simply return the URL as is, which is // convenient since developers do not always have to check if it's valid. if ($this->isValidUrl($path)) { return $path; } $tail = implode('/', array_map( 'rawurlencode', (array) $this->formatParameters($extra)) ); // Once we have the scheme we will compile the "tail" by collapsing the values // into a single string delimited by slashes. This just makes it convenient // for passing the array of parameters to this URL as a list of segments. $root = $this->formatRoot($this->formatScheme($secure)); [$path, $query] = $this->extractQueryString($path); return $this->format( $root, '/'.trim($path.'/'.$tail, '/') ).$query; } /** * Generate a secure, absolute URL to the given path. * * @param string $path * @param array $parameters * @return string */ public function secure($path, $parameters = []) { return $this->to($path, $parameters, true); } /** * Generate the URL to an application asset. * * @param string $path * @param bool|null $secure * @return string */ public function asset($path, $secure = null) { if ($this->isValidUrl($path)) { return $path; } // Once we get the root URL, we will check to see if it contains an index.php // file in the paths. If it does, we will remove it since it is not needed // for asset paths, but only for routes to endpoints in the application. $root = $this->assetRoot ?: $this->formatRoot($this->formatScheme($secure)); return Str::finish($this->removeIndex($root), '/').trim($path, '/'); } /** * Generate the URL to a secure asset. * * @param string $path * @return string */ public function secureAsset($path) { return $this->asset($path, true); } /** * Generate the URL to an asset from a custom root domain such as CDN, etc. * * @param string $root * @param string $path * @param bool|null $secure * @return string */ public function assetFrom($root, $path, $secure = null) { // Once we get the root URL, we will check to see if it contains an index.php // file in the paths. If it does, we will remove it since it is not needed // for asset paths, but only for routes to endpoints in the application. $root = $this->formatRoot($this->formatScheme($secure), $root); return $this->removeIndex($root).'/'.trim($path, '/'); } /** * Remove the index.php file from a path. * * @param string $root * @return string */ protected function removeIndex($root) { $i = 'index.php'; return str_contains($root, $i) ? str_replace('/'.$i, '', $root) : $root; } /** * Get the default scheme for a raw URL. * * @param bool|null $secure * @return string */ public function formatScheme($secure = null) { if (! is_null($secure)) { return $secure ? 'https://' : 'http://'; } if (is_null($this->cachedScheme)) { $this->cachedScheme = $this->forceScheme ?: $this->request->getScheme().'://'; } return $this->cachedScheme; } /** * Create a signed route URL for a named route. * * @param string $name * @param mixed $parameters * @param \DateTimeInterface|\DateInterval|int|null $expiration * @param bool $absolute * @return string * * @throws \InvalidArgumentException */ public function signedRoute($name, $parameters = [], $expiration = null, $absolute = true) { $this->ensureSignedRouteParametersAreNotReserved( $parameters = Arr::wrap($parameters) ); if ($expiration) { $parameters = $parameters + ['expires' => $this->availableAt($expiration)]; } ksort($parameters); $key = call_user_func($this->keyResolver); return $this->route($name, $parameters + [ 'signature' => hash_hmac('sha256', $this->route($name, $parameters, $absolute), $key), ], $absolute); } /** * Ensure the given signed route parameters are not reserved. * * @param mixed $parameters * @return void */ protected function ensureSignedRouteParametersAreNotReserved($parameters) { if (array_key_exists('signature', $parameters)) { throw new InvalidArgumentException( '"Signature" is a reserved parameter when generating signed routes. Please rename your route parameter.' ); } if (array_key_exists('expires', $parameters)) { throw new InvalidArgumentException( '"Expires" is a reserved parameter when generating signed routes. Please rename your route parameter.' ); } } /** * Create a temporary signed route URL for a named route. * * @param string $name * @param \DateTimeInterface|\DateInterval|int $expiration * @param array $parameters * @param bool $absolute * @return string */ public function temporarySignedRoute($name, $expiration, $parameters = [], $absolute = true) { return $this->signedRoute($name, $parameters, $expiration, $absolute); } /** * Determine if the given request has a valid signature. * * @param \Illuminate\Http\Request $request * @param bool $absolute * @param array $ignoreQuery * @return bool */ public function hasValidSignature(Request $request, $absolute = true, array $ignoreQuery = []) { return $this->hasCorrectSignature($request, $absolute, $ignoreQuery) && $this->signatureHasNotExpired($request); } /** * Determine if the given request has a valid signature for a relative URL. * * @param \Illuminate\Http\Request $request * @param array $ignoreQuery * @return bool */ public function hasValidRelativeSignature(Request $request, array $ignoreQuery = []) { return $this->hasValidSignature($request, false, $ignoreQuery); } /** * Determine if the signature from the given request matches the URL. * * @param \Illuminate\Http\Request $request * @param bool $absolute * @param array $ignoreQuery * @return bool */ public function hasCorrectSignature(Request $request, $absolute = true, array $ignoreQuery = []) { $ignoreQuery[] = 'signature'; $url = $absolute ? $request->url() : '/'.$request->path(); $queryString = collect(explode('&', (string) $request->server->get('QUERY_STRING'))) ->reject(fn ($parameter) => in_array(Str::before($parameter, '='), $ignoreQuery)) ->join('&'); $original = rtrim($url.'?'.$queryString, '?'); $signature = hash_hmac('sha256', $original, call_user_func($this->keyResolver)); return hash_equals($signature, (string) $request->query('signature', '')); } /** * Determine if the expires timestamp from the given request is not from the past. * * @param \Illuminate\Http\Request $request * @return bool */ public function signatureHasNotExpired(Request $request) { $expires = $request->query('expires'); return ! ($expires && Carbon::now()->getTimestamp() > $expires); } /** * Get the URL to a named route. * * @param string $name * @param mixed $parameters * @param bool $absolute * @return string * * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException */ public function route($name, $parameters = [], $absolute = true) { if (! is_null($route = $this->routes->getByName($name))) { return $this->toRoute($route, $parameters, $absolute); } if (! is_null($this->missingNamedRouteResolver) && ! is_null($url = call_user_func($this->missingNamedRouteResolver, $name, $parameters, $absolute))) { return $url; } throw new RouteNotFoundException("Route [{$name}] not defined."); } /** * Get the URL for a given route instance. * * @param \Illuminate\Routing\Route $route * @param mixed $parameters * @param bool $absolute * @return string * * @throws \Illuminate\Routing\Exceptions\UrlGenerationException */ public function toRoute($route, $parameters, $absolute) { $parameters = collect(Arr::wrap($parameters))->map(function ($value, $key) use ($route) { $value = $value instanceof UrlRoutable && $route->bindingFieldFor($key) ? $value->{$route->bindingFieldFor($key)} : $value; return $value instanceof BackedEnum ? $value->value : $value; })->all(); return $this->routeUrl()->to( $route, $this->formatParameters($parameters), $absolute ); } /** * Get the URL to a controller action. * * @param string|array $action * @param mixed $parameters * @param bool $absolute * @return string * * @throws \InvalidArgumentException */ public function action($action, $parameters = [], $absolute = true) { if (is_null($route = $this->routes->getByAction($action = $this->formatAction($action)))) { throw new InvalidArgumentException("Action {$action} not defined."); } return $this->toRoute($route, $parameters, $absolute); } /** * Format the given controller action. * * @param string|array $action * @return string */ protected function formatAction($action) { if (is_array($action)) { $action = '\\'.implode('@', $action); } if ($this->rootNamespace && ! str_starts_with($action, '\\')) { return $this->rootNamespace.'\\'.$action; } return trim($action, '\\'); } /** * Format the array of URL parameters. * * @param mixed|array $parameters * @return array */ public function formatParameters($parameters) { $parameters = Arr::wrap($parameters); foreach ($parameters as $key => $parameter) { if ($parameter instanceof UrlRoutable) { $parameters[$key] = $parameter->getRouteKey(); } } return $parameters; } /** * Extract the query string from the given path. * * @param string $path * @return array */ protected function extractQueryString($path) { if (($queryPosition = strpos($path, '?')) !== false) { return [ substr($path, 0, $queryPosition), substr($path, $queryPosition), ]; } return [$path, '']; } /** * Get the base URL for the request. * * @param string $scheme * @param string|null $root * @return string */ public function formatRoot($scheme, $root = null) { if (is_null($root)) { if (is_null($this->cachedRoot)) { $this->cachedRoot = $this->forcedRoot ?: $this->request->root(); } $root = $this->cachedRoot; } $start = str_starts_with($root, 'http://') ? 'http://' : 'https://'; return preg_replace('~'.$start.'~', $scheme, $root, 1); } /** * Format the given URL segments into a single URL. * * @param string $root * @param string $path * @param \Illuminate\Routing\Route|null $route * @return string */ public function format($root, $path, $route = null) { $path = '/'.trim($path, '/'); if ($this->formatHostUsing) { $root = call_user_func($this->formatHostUsing, $root, $route); } if ($this->formatPathUsing) { $path = call_user_func($this->formatPathUsing, $path, $route); } return trim($root.$path, '/'); } /** * Determine if the given path is a valid URL. * * @param string $path * @return bool */ public function isValidUrl($path) { if (! preg_match('~^(#|//|https?://|(mailto|tel|sms):)~', $path)) { return filter_var($path, FILTER_VALIDATE_URL) !== false; } return true; } /** * Get the Route URL generator instance. * * @return \Illuminate\Routing\RouteUrlGenerator */ protected function routeUrl() { if (! $this->routeGenerator) { $this->routeGenerator = new RouteUrlGenerator($this, $this->request); } return $this->routeGenerator; } /** * Set the default named parameters used by the URL generator. * * @param array $defaults * @return void */ public function defaults(array $defaults) { $this->routeUrl()->defaults($defaults); } /** * Get the default named parameters used by the URL generator. * * @return array */ public function getDefaultParameters() { return $this->routeUrl()->defaultParameters; } /** * Force the scheme for URLs. * * @param string|null $scheme * @return void */ public function forceScheme($scheme) { $this->cachedScheme = null; $this->forceScheme = $scheme ? $scheme.'://' : null; } /** * Set the forced root URL. * * @param string|null $root * @return void */ public function forceRootUrl($root) { $this->forcedRoot = $root ? rtrim($root, '/') : null; $this->cachedRoot = null; } /** * Set a callback to be used to format the host of generated URLs. * * @param \Closure $callback * @return $this */ public function formatHostUsing(Closure $callback) { $this->formatHostUsing = $callback; return $this; } /** * Set a callback to be used to format the path of generated URLs. * * @param \Closure $callback * @return $this */ public function formatPathUsing(Closure $callback) { $this->formatPathUsing = $callback; return $this; } /** * Get the path formatter being used by the URL generator. * * @return \Closure */ public function pathFormatter() { return $this->formatPathUsing ?: function ($path) { return $path; }; } /** * Get the request instance. * * @return \Illuminate\Http\Request */ public function getRequest() { return $this->request; } /** * Set the current request instance. * * @param \Illuminate\Http\Request $request * @return void */ public function setRequest(Request $request) { $this->request = $request; $this->cachedRoot = null; $this->cachedScheme = null; tap(optional($this->routeGenerator)->defaultParameters ?: [], function ($defaults) { $this->routeGenerator = null; if (! empty($defaults)) { $this->defaults($defaults); } }); } /** * Set the route collection. * * @param \Illuminate\Routing\RouteCollectionInterface $routes * @return $this */ public function setRoutes(RouteCollectionInterface $routes) { $this->routes = $routes; return $this; } /** * Get the session implementation from the resolver. * * @return \Illuminate\Session\Store|null */ protected function getSession() { if ($this->sessionResolver) { return call_user_func($this->sessionResolver); } } /** * Set the session resolver for the generator. * * @param callable $sessionResolver * @return $this */ public function setSessionResolver(callable $sessionResolver) { $this->sessionResolver = $sessionResolver; return $this; } /** * Set the encryption key resolver. * * @param callable $keyResolver * @return $this */ public function setKeyResolver(callable $keyResolver) { $this->keyResolver = $keyResolver; return $this; } /** * Clone a new instance of the URL generator with a different encryption key resolver. * * @param callable $keyResolver * @return \Illuminate\Routing\UrlGenerator */ public function withKeyResolver(callable $keyResolver) { return (clone $this)->setKeyResolver($keyResolver); } /** * Set the callback that should be used to attempt to resolve missing named routes. * * @param callable $missingNamedRouteResolver * @return $this */ public function resolveMissingNamedRoutesUsing(callable $missingNamedRouteResolver) { $this->missingNamedRouteResolver = $missingNamedRouteResolver; return $this; } /** * Get the root controller namespace. * * @return string */ public function getRootControllerNamespace() { return $this->rootNamespace; } /** * Set the root controller namespace. * * @param string $rootNamespace * @return $this */ public function setRootControllerNamespace($rootNamespace) { $this->rootNamespace = $rootNamespace; return $this; } } src/Illuminate/Routing/composer.json 0000644 00000002731 15107327037 0013617 0 ustar 00 { "name": "illuminate/routing", "description": "The Illuminate Routing package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "ext-filter": "*", "ext-hash": "*", "illuminate/collections": "^10.0", "illuminate/container": "^10.0", "illuminate/contracts": "^10.0", "illuminate/http": "^10.0", "illuminate/macroable": "^10.0", "illuminate/pipeline": "^10.0", "illuminate/session": "^10.0", "illuminate/support": "^10.0", "symfony/http-foundation": "^6.2", "symfony/http-kernel": "^6.2", "symfony/routing": "^6.2" }, "autoload": { "psr-4": { "Illuminate\\Routing\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "illuminate/console": "Required to use the make commands (^10.0).", "nyholm/psr7": "Required to use PSR-7 bridging features (^1.2).", "symfony/psr-http-message-bridge": "Required to use PSR-7 bridging features (^2.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Routing/Events/RouteMatched.php 0000644 00000001121 15107327037 0015426 0 ustar 00 <?php namespace Illuminate\Routing\Events; class RouteMatched { /** * The route instance. * * @var \Illuminate\Routing\Route */ public $route; /** * The request instance. * * @var \Illuminate\Http\Request */ public $request; /** * Create a new event instance. * * @param \Illuminate\Routing\Route $route * @param \Illuminate\Http\Request $request * @return void */ public function __construct($route, $request) { $this->route = $route; $this->request = $request; } } src/Illuminate/Routing/Events/ResponsePrepared.php 0000644 00000001253 15107327037 0016331 0 ustar 00 <?php namespace Illuminate\Routing\Events; class ResponsePrepared { /** * The request instance. * * @var \Symfony\Component\HttpFoundation\Request */ public $request; /** * The response instance. * * @var \Symfony\Component\HttpFoundation\Response */ public $response; /** * Create a new event instance. * * @param \Symfony\Component\HttpFoundation\Request $request * @param \Symfony\Component\HttpFoundation\Response $response * @return void */ public function __construct($request, $response) { $this->request = $request; $this->response = $response; } } src/Illuminate/Routing/Events/PreparingResponse.php 0000644 00000001142 15107327037 0016513 0 ustar 00 <?php namespace Illuminate\Routing\Events; class PreparingResponse { /** * The request instance. * * @var \Symfony\Component\HttpFoundation\Request */ public $request; /** * The response instance. * * @var mixed */ public $response; /** * Create a new event instance. * * @param \Symfony\Component\HttpFoundation\Request $request * @param mixed $response * @return void */ public function __construct($request, $response) { $this->request = $request; $this->response = $response; } } src/Illuminate/Routing/Events/Routing.php 0000644 00000000610 15107327037 0014473 0 ustar 00 <?php namespace Illuminate\Routing\Events; class Routing { /** * The request instance. * * @var \Illuminate\Http\Request */ public $request; /** * Create a new event instance. * * @param \Illuminate\Http\Request $request * @return void */ public function __construct($request) { $this->request = $request; } } src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php 0000644 00000004611 15107327037 0022044 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Support\Arr; trait CreatesRegularExpressionRouteConstraints { /** * Specify that the given route parameters must be alphabetic. * * @param array|string $parameters * @return $this */ public function whereAlpha($parameters) { return $this->assignExpressionToParameters($parameters, '[a-zA-Z]+'); } /** * Specify that the given route parameters must be alphanumeric. * * @param array|string $parameters * @return $this */ public function whereAlphaNumeric($parameters) { return $this->assignExpressionToParameters($parameters, '[a-zA-Z0-9]+'); } /** * Specify that the given route parameters must be numeric. * * @param array|string $parameters * @return $this */ public function whereNumber($parameters) { return $this->assignExpressionToParameters($parameters, '[0-9]+'); } /** * Specify that the given route parameters must be ULIDs. * * @param array|string $parameters * @return $this */ public function whereUlid($parameters) { return $this->assignExpressionToParameters($parameters, '[0-7][0-9a-hjkmnp-tv-zA-HJKMNP-TV-Z]{25}'); } /** * Specify that the given route parameters must be UUIDs. * * @param array|string $parameters * @return $this */ public function whereUuid($parameters) { return $this->assignExpressionToParameters($parameters, '[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}'); } /** * Specify that the given route parameters must be one of the given values. * * @param array|string $parameters * @param array $values * @return $this */ public function whereIn($parameters, array $values) { return $this->assignExpressionToParameters($parameters, implode('|', $values)); } /** * Apply the given regular expression to the given parameters. * * @param array|string $parameters * @param string $expression * @return $this */ protected function assignExpressionToParameters($parameters, $expression) { return $this->where(collect(Arr::wrap($parameters)) ->mapWithKeys(fn ($parameter) => [$parameter => $expression]) ->all()); } } src/Illuminate/Routing/LICENSE.md 0000644 00000002063 15107327037 0012477 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Routing/Matching/SchemeValidator.php 0000644 00000001153 15107327037 0016407 0 ustar 00 <?php namespace Illuminate\Routing\Matching; use Illuminate\Http\Request; use Illuminate\Routing\Route; class SchemeValidator implements ValidatorInterface { /** * Validate a given rule against a route and request. * * @param \Illuminate\Routing\Route $route * @param \Illuminate\Http\Request $request * @return bool */ public function matches(Route $route, Request $request) { if ($route->httpOnly()) { return ! $request->secure(); } elseif ($route->secure()) { return $request->secure(); } return true; } } src/Illuminate/Routing/Matching/UriValidator.php 0000644 00000001100 15107327037 0015732 0 ustar 00 <?php namespace Illuminate\Routing\Matching; use Illuminate\Http\Request; use Illuminate\Routing\Route; class UriValidator implements ValidatorInterface { /** * Validate a given rule against a route and request. * * @param \Illuminate\Routing\Route $route * @param \Illuminate\Http\Request $request * @return bool */ public function matches(Route $route, Request $request) { $path = rtrim($request->getPathInfo(), '/') ?: '/'; return preg_match($route->getCompiled()->getRegex(), rawurldecode($path)); } } src/Illuminate/Routing/Matching/HostValidator.php 0000644 00000001161 15107327037 0016117 0 ustar 00 <?php namespace Illuminate\Routing\Matching; use Illuminate\Http\Request; use Illuminate\Routing\Route; class HostValidator implements ValidatorInterface { /** * Validate a given rule against a route and request. * * @param \Illuminate\Routing\Route $route * @param \Illuminate\Http\Request $request * @return bool */ public function matches(Route $route, Request $request) { $hostRegex = $route->getCompiled()->getHostRegex(); if (is_null($hostRegex)) { return true; } return preg_match($hostRegex, $request->getHost()); } } src/Illuminate/Routing/Matching/ValidatorInterface.php 0000644 00000000621 15107327037 0017102 0 ustar 00 <?php namespace Illuminate\Routing\Matching; use Illuminate\Http\Request; use Illuminate\Routing\Route; interface ValidatorInterface { /** * Validate a given rule against a route and request. * * @param \Illuminate\Routing\Route $route * @param \Illuminate\Http\Request $request * @return bool */ public function matches(Route $route, Request $request); } src/Illuminate/Routing/Matching/MethodValidator.php 0000644 00000000766 15107327037 0016434 0 ustar 00 <?php namespace Illuminate\Routing\Matching; use Illuminate\Http\Request; use Illuminate\Routing\Route; class MethodValidator implements ValidatorInterface { /** * Validate a given rule against a route and request. * * @param \Illuminate\Routing\Route $route * @param \Illuminate\Http\Request $request * @return bool */ public function matches(Route $route, Request $request) { return in_array($request->getMethod(), $route->methods()); } } src/Illuminate/Routing/ViewController.php 0000644 00000002645 15107327037 0014570 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Contracts\Routing\ResponseFactory; class ViewController extends Controller { /** * The response factory implementation. * * @var \Illuminate\Contracts\Routing\ResponseFactory */ protected $response; /** * Create a new controller instance. * * @param \Illuminate\Contracts\Routing\ResponseFactory $response * @return void */ public function __construct(ResponseFactory $response) { $this->response = $response; } /** * Invoke the controller method. * * @param mixed ...$args * @return \Illuminate\Http\Response */ public function __invoke(...$args) { $routeParameters = array_filter($args, function ($key) { return ! in_array($key, ['view', 'data', 'status', 'headers']); }, ARRAY_FILTER_USE_KEY); $args['data'] = array_merge($args['data'], $routeParameters); return $this->response->view( $args['view'], $args['data'], $args['status'], $args['headers'] ); } /** * Execute an action on the controller. * * @param string $method * @param array $parameters * @return \Symfony\Component\HttpFoundation\Response */ public function callAction($method, $parameters) { return $this->{$method}(...$parameters); } } src/Illuminate/Routing/Middleware/ValidateSignature.php 0000644 00000004157 15107327037 0017302 0 ustar 00 <?php namespace Illuminate\Routing\Middleware; use Closure; use Illuminate\Routing\Exceptions\InvalidSignatureException; use Illuminate\Support\Arr; class ValidateSignature { /** * The names of the parameters that should be ignored. * * @var array<int, string> */ protected $ignore = [ // ]; /** * Specify that the URL signature is for a relative URL. * * @param array|string $ignore * @return string */ public static function relative($ignore = []) { $ignore = Arr::wrap($ignore); return static::class.':'.implode(',', empty($ignore) ? ['relative'] : ['relative', ...$ignore]); } /** * Specify that the URL signature is for an absolute URL. * * @param array|string $ignore * @return class-string */ public static function absolute($ignore = []) { $ignore = Arr::wrap($ignore); return empty($ignore) ? static::class : static::class.':'.implode(',', $ignore); } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param array|null $args * @return \Illuminate\Http\Response * * @throws \Illuminate\Routing\Exceptions\InvalidSignatureException */ public function handle($request, Closure $next, ...$args) { [$relative, $ignore] = $this->parseArguments($args); if ($request->hasValidSignatureWhileIgnoring($ignore, ! $relative)) { return $next($request); } throw new InvalidSignatureException; } /** * Parse the additional arguments given to the middleware. * * @param array $args * @return array */ protected function parseArguments(array $args) { $relative = ! empty($args) && $args[0] === 'relative'; if ($relative) { array_shift($args); } $ignore = array_merge( property_exists($this, 'except') ? $this->except : $this->ignore, $args ); return [$relative, $ignore]; } } src/Illuminate/Routing/Middleware/ThrottleRequestsWithRedis.php 0000644 00000006640 15107327037 0021052 0 ustar 00 <?php namespace Illuminate\Routing\Middleware; use Closure; use Illuminate\Cache\RateLimiter; use Illuminate\Contracts\Redis\Factory as Redis; use Illuminate\Redis\Limiters\DurationLimiter; class ThrottleRequestsWithRedis extends ThrottleRequests { /** * The Redis factory implementation. * * @var \Illuminate\Contracts\Redis\Factory */ protected $redis; /** * The timestamp of the end of the current duration by key. * * @var array */ public $decaysAt = []; /** * The number of remaining slots by key. * * @var array */ public $remaining = []; /** * Create a new request throttler. * * @param \Illuminate\Cache\RateLimiter $limiter * @param \Illuminate\Contracts\Redis\Factory $redis * @return void */ public function __construct(RateLimiter $limiter, Redis $redis) { parent::__construct($limiter); $this->redis = $redis; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param array $limits * @return \Symfony\Component\HttpFoundation\Response * * @throws \Illuminate\Http\Exceptions\ThrottleRequestsException */ protected function handleRequest($request, Closure $next, array $limits) { foreach ($limits as $limit) { if ($this->tooManyAttempts($limit->key, $limit->maxAttempts, $limit->decayMinutes)) { throw $this->buildException($request, $limit->key, $limit->maxAttempts, $limit->responseCallback); } } $response = $next($request); foreach ($limits as $limit) { $response = $this->addHeaders( $response, $limit->maxAttempts, $this->calculateRemainingAttempts($limit->key, $limit->maxAttempts) ); } return $response; } /** * Determine if the given key has been "accessed" too many times. * * @param string $key * @param int $maxAttempts * @param int $decayMinutes * @return mixed */ protected function tooManyAttempts($key, $maxAttempts, $decayMinutes) { $limiter = new DurationLimiter( $this->getRedisConnection(), $key, $maxAttempts, $decayMinutes * 60 ); return tap(! $limiter->acquire(), function () use ($key, $limiter) { [$this->decaysAt[$key], $this->remaining[$key]] = [ $limiter->decaysAt, $limiter->remaining, ]; }); } /** * Calculate the number of remaining attempts. * * @param string $key * @param int $maxAttempts * @param int|null $retryAfter * @return int */ protected function calculateRemainingAttempts($key, $maxAttempts, $retryAfter = null) { return is_null($retryAfter) ? $this->remaining[$key] : 0; } /** * Get the number of seconds until the lock is released. * * @param string $key * @return int */ protected function getTimeUntilNextRetry($key) { return $this->decaysAt[$key] - $this->currentTime(); } /** * Get the Redis connection that should be used for throttling. * * @return \Illuminate\Redis\Connections\Connection */ protected function getRedisConnection() { return $this->redis->connection(); } } src/Illuminate/Routing/Middleware/ThrottleRequests.php 0000644 00000023520 15107327037 0017223 0 ustar 00 <?php namespace Illuminate\Routing\Middleware; use Closure; use Illuminate\Cache\RateLimiter; use Illuminate\Cache\RateLimiting\Unlimited; use Illuminate\Http\Exceptions\HttpResponseException; use Illuminate\Http\Exceptions\ThrottleRequestsException; use Illuminate\Support\Arr; use Illuminate\Support\InteractsWithTime; use RuntimeException; use Symfony\Component\HttpFoundation\Response; class ThrottleRequests { use InteractsWithTime; /** * The rate limiter instance. * * @var \Illuminate\Cache\RateLimiter */ protected $limiter; /** * Indicates if the rate limiter keys should be hashed. * * @var bool */ protected static $shouldHashKeys = true; /** * Create a new request throttler. * * @param \Illuminate\Cache\RateLimiter $limiter * @return void */ public function __construct(RateLimiter $limiter) { $this->limiter = $limiter; } /** * Specify the named rate limiter to use for the middleware. * * @param string $name * @return string */ public static function using($name) { return static::class.':'.$name; } /** * Specify the rate limiter configuration for the middleware. * * @param int $maxAttempts * @param int $decayMinutes * @param string $prefix * @return string * * @named-arguments-supported */ public static function with($maxAttempts = 60, $decayMinutes = 1, $prefix = '') { return static::class.':'.implode(',', func_get_args()); } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param int|string $maxAttempts * @param float|int $decayMinutes * @param string $prefix * @return \Symfony\Component\HttpFoundation\Response * * @throws \Illuminate\Http\Exceptions\ThrottleRequestsException */ public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1, $prefix = '') { if (is_string($maxAttempts) && func_num_args() === 3 && ! is_null($limiter = $this->limiter->limiter($maxAttempts))) { return $this->handleRequestUsingNamedLimiter($request, $next, $maxAttempts, $limiter); } return $this->handleRequest( $request, $next, [ (object) [ 'key' => $prefix.$this->resolveRequestSignature($request), 'maxAttempts' => $this->resolveMaxAttempts($request, $maxAttempts), 'decayMinutes' => $decayMinutes, 'responseCallback' => null, ], ] ); } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string $limiterName * @param \Closure $limiter * @return \Symfony\Component\HttpFoundation\Response * * @throws \Illuminate\Http\Exceptions\ThrottleRequestsException */ protected function handleRequestUsingNamedLimiter($request, Closure $next, $limiterName, Closure $limiter) { $limiterResponse = $limiter($request); if ($limiterResponse instanceof Response) { return $limiterResponse; } elseif ($limiterResponse instanceof Unlimited) { return $next($request); } return $this->handleRequest( $request, $next, collect(Arr::wrap($limiterResponse))->map(function ($limit) use ($limiterName) { return (object) [ 'key' => self::$shouldHashKeys ? md5($limiterName.$limit->key) : $limiterName.':'.$limit->key, 'maxAttempts' => $limit->maxAttempts, 'decayMinutes' => $limit->decayMinutes, 'responseCallback' => $limit->responseCallback, ]; })->all() ); } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param array $limits * @return \Symfony\Component\HttpFoundation\Response * * @throws \Illuminate\Http\Exceptions\ThrottleRequestsException */ protected function handleRequest($request, Closure $next, array $limits) { foreach ($limits as $limit) { if ($this->limiter->tooManyAttempts($limit->key, $limit->maxAttempts)) { throw $this->buildException($request, $limit->key, $limit->maxAttempts, $limit->responseCallback); } $this->limiter->hit($limit->key, $limit->decayMinutes * 60); } $response = $next($request); foreach ($limits as $limit) { $response = $this->addHeaders( $response, $limit->maxAttempts, $this->calculateRemainingAttempts($limit->key, $limit->maxAttempts) ); } return $response; } /** * Resolve the number of attempts if the user is authenticated or not. * * @param \Illuminate\Http\Request $request * @param int|string $maxAttempts * @return int */ protected function resolveMaxAttempts($request, $maxAttempts) { if (str_contains($maxAttempts, '|')) { $maxAttempts = explode('|', $maxAttempts, 2)[$request->user() ? 1 : 0]; } if (! is_numeric($maxAttempts) && $request->user()) { $maxAttempts = $request->user()->{$maxAttempts}; } return (int) $maxAttempts; } /** * Resolve request signature. * * @param \Illuminate\Http\Request $request * @return string * * @throws \RuntimeException */ protected function resolveRequestSignature($request) { if ($user = $request->user()) { return $this->formatIdentifier($user->getAuthIdentifier()); } elseif ($route = $request->route()) { return $this->formatIdentifier($route->getDomain().'|'.$request->ip()); } throw new RuntimeException('Unable to generate the request signature. Route unavailable.'); } /** * Create a 'too many attempts' exception. * * @param \Illuminate\Http\Request $request * @param string $key * @param int $maxAttempts * @param callable|null $responseCallback * @return \Illuminate\Http\Exceptions\ThrottleRequestsException|\Illuminate\Http\Exceptions\HttpResponseException */ protected function buildException($request, $key, $maxAttempts, $responseCallback = null) { $retryAfter = $this->getTimeUntilNextRetry($key); $headers = $this->getHeaders( $maxAttempts, $this->calculateRemainingAttempts($key, $maxAttempts, $retryAfter), $retryAfter ); return is_callable($responseCallback) ? new HttpResponseException($responseCallback($request, $headers)) : new ThrottleRequestsException('Too Many Attempts.', null, $headers); } /** * Get the number of seconds until the next retry. * * @param string $key * @return int */ protected function getTimeUntilNextRetry($key) { return $this->limiter->availableIn($key); } /** * Add the limit header information to the given response. * * @param \Symfony\Component\HttpFoundation\Response $response * @param int $maxAttempts * @param int $remainingAttempts * @param int|null $retryAfter * @return \Symfony\Component\HttpFoundation\Response */ protected function addHeaders(Response $response, $maxAttempts, $remainingAttempts, $retryAfter = null) { $response->headers->add( $this->getHeaders($maxAttempts, $remainingAttempts, $retryAfter, $response) ); return $response; } /** * Get the limit headers information. * * @param int $maxAttempts * @param int $remainingAttempts * @param int|null $retryAfter * @param \Symfony\Component\HttpFoundation\Response|null $response * @return array */ protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = null, ?Response $response = null) { if ($response && ! is_null($response->headers->get('X-RateLimit-Remaining')) && (int) $response->headers->get('X-RateLimit-Remaining') <= (int) $remainingAttempts) { return []; } $headers = [ 'X-RateLimit-Limit' => $maxAttempts, 'X-RateLimit-Remaining' => $remainingAttempts, ]; if (! is_null($retryAfter)) { $headers['Retry-After'] = $retryAfter; $headers['X-RateLimit-Reset'] = $this->availableAt($retryAfter); } return $headers; } /** * Calculate the number of remaining attempts. * * @param string $key * @param int $maxAttempts * @param int|null $retryAfter * @return int */ protected function calculateRemainingAttempts($key, $maxAttempts, $retryAfter = null) { return is_null($retryAfter) ? $this->limiter->retriesLeft($key, $maxAttempts) : 0; } /** * Format the given identifier based on the configured hashing settings. * * @param string $value * @return string */ private function formatIdentifier($value) { return self::$shouldHashKeys ? sha1($value) : $value; } /** * Specify whether rate limiter keys should be hashed. * * @param bool $shouldHashKeys * @return void */ public static function shouldHashKeys(bool $shouldHashKeys = true) { self::$shouldHashKeys = $shouldHashKeys; } } src/Illuminate/Routing/Middleware/SubstituteBindings.php 0000644 00000002250 15107327037 0017510 0 ustar 00 <?php namespace Illuminate\Routing\Middleware; use Closure; use Illuminate\Contracts\Routing\Registrar; use Illuminate\Database\Eloquent\ModelNotFoundException; class SubstituteBindings { /** * The router instance. * * @var \Illuminate\Contracts\Routing\Registrar */ protected $router; /** * Create a new bindings substitutor. * * @param \Illuminate\Contracts\Routing\Registrar $router * @return void */ public function __construct(Registrar $router) { $this->router = $router; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { try { $this->router->substituteBindings($route = $request->route()); $this->router->substituteImplicitBindings($route); } catch (ModelNotFoundException $exception) { if ($route->getMissing()) { return $route->getMissing()($request, $exception); } throw $exception; } return $next($request); } } src/Illuminate/Routing/RoutingServiceProvider.php 0000644 00000015064 15107327037 0016274 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract; use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract; use Illuminate\Contracts\View\Factory as ViewFactoryContract; use Illuminate\Routing\Contracts\CallableDispatcher as CallableDispatcherContract; use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract; use Illuminate\Support\ServiceProvider; use Nyholm\Psr7\Factory\Psr17Factory; use Nyholm\Psr7\Response as PsrResponse; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; class RoutingServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { $this->registerRouter(); $this->registerUrlGenerator(); $this->registerRedirector(); $this->registerPsrRequest(); $this->registerPsrResponse(); $this->registerResponseFactory(); $this->registerCallableDispatcher(); $this->registerControllerDispatcher(); } /** * Register the router instance. * * @return void */ protected function registerRouter() { $this->app->singleton('router', function ($app) { return new Router($app['events'], $app); }); } /** * Register the URL generator service. * * @return void */ protected function registerUrlGenerator() { $this->app->singleton('url', function ($app) { $routes = $app['router']->getRoutes(); // The URL generator needs the route collection that exists on the router. // Keep in mind this is an object, so we're passing by references here // and all the registered routes will be available to the generator. $app->instance('routes', $routes); return new UrlGenerator( $routes, $app->rebinding( 'request', $this->requestRebinder() ), $app['config']['app.asset_url'] ); }); $this->app->extend('url', function (UrlGeneratorContract $url, $app) { // Next we will set a few service resolvers on the URL generator so it can // get the information it needs to function. This just provides some of // the convenience features to this URL generator like "signed" URLs. $url->setSessionResolver(function () { return $this->app['session'] ?? null; }); $url->setKeyResolver(function () { return $this->app->make('config')->get('app.key'); }); // If the route collection is "rebound", for example, when the routes stay // cached for the application, we will need to rebind the routes on the // URL generator instance so it has the latest version of the routes. $app->rebinding('routes', function ($app, $routes) { $app['url']->setRoutes($routes); }); return $url; }); } /** * Get the URL generator request rebinder. * * @return \Closure */ protected function requestRebinder() { return function ($app, $request) { $app['url']->setRequest($request); }; } /** * Register the Redirector service. * * @return void */ protected function registerRedirector() { $this->app->singleton('redirect', function ($app) { $redirector = new Redirector($app['url']); // If the session is set on the application instance, we'll inject it into // the redirector instance. This allows the redirect responses to allow // for the quite convenient "with" methods that flash to the session. if (isset($app['session.store'])) { $redirector->setSession($app['session.store']); } return $redirector; }); } /** * Register a binding for the PSR-7 request implementation. * * @return void * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ protected function registerPsrRequest() { $this->app->bind(ServerRequestInterface::class, function ($app) { if (class_exists(Psr17Factory::class) && class_exists(PsrHttpFactory::class)) { $psr17Factory = new Psr17Factory; return with((new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory)) ->createRequest($illuminateRequest = $app->make('request')), fn ($request) => $request->withParsedBody( array_merge($request->getParsedBody() ?? [], $illuminateRequest->getPayload()->all()) )); } throw new BindingResolutionException('Unable to resolve PSR request. Please install the symfony/psr-http-message-bridge and nyholm/psr7 packages.'); }); } /** * Register a binding for the PSR-7 response implementation. * * @return void * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ protected function registerPsrResponse() { $this->app->bind(ResponseInterface::class, function () { if (class_exists(PsrResponse::class)) { return new PsrResponse; } throw new BindingResolutionException('Unable to resolve PSR response. Please install the nyholm/psr7 package.'); }); } /** * Register the response factory implementation. * * @return void */ protected function registerResponseFactory() { $this->app->singleton(ResponseFactoryContract::class, function ($app) { return new ResponseFactory($app[ViewFactoryContract::class], $app['redirect']); }); } /** * Register the callable dispatcher. * * @return void */ protected function registerCallableDispatcher() { $this->app->singleton(CallableDispatcherContract::class, function ($app) { return new CallableDispatcher($app); }); } /** * Register the controller dispatcher. * * @return void */ protected function registerControllerDispatcher() { $this->app->singleton(ControllerDispatcherContract::class, function ($app) { return new ControllerDispatcher($app); }); } } src/Illuminate/Routing/CallableDispatcher.php 0000644 00000002532 15107327037 0015313 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Container\Container; use Illuminate\Routing\Contracts\CallableDispatcher as CallableDispatcherContract; use ReflectionFunction; class CallableDispatcher implements CallableDispatcherContract { use ResolvesRouteDependencies; /** * The container instance. * * @var \Illuminate\Container\Container */ protected $container; /** * Create a new callable dispatcher instance. * * @param \Illuminate\Container\Container $container * @return void */ public function __construct(Container $container) { $this->container = $container; } /** * Dispatch a request to a given callable. * * @param \Illuminate\Routing\Route $route * @param callable $callable * @return mixed */ public function dispatch(Route $route, $callable) { return $callable(...array_values($this->resolveParameters($route, $callable))); } /** * Resolve the parameters for the callable. * * @param \Illuminate\Routing\Route $route * @param callable $callable * @return array */ protected function resolveParameters(Route $route, $callable) { return $this->resolveMethodDependencies($route->parametersWithoutNulls(), new ReflectionFunction($callable)); } } src/Illuminate/Routing/Pipeline.php 0000644 00000002763 15107327037 0013360 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Support\Responsable; use Illuminate\Http\Request; use Illuminate\Pipeline\Pipeline as BasePipeline; use Throwable; /** * This extended pipeline catches any exceptions that occur during each slice. * * The exceptions are converted to HTTP responses for proper middleware handling. */ class Pipeline extends BasePipeline { /** * Handles the value returned from each pipe before passing it to the next. * * @param mixed $carry * @return mixed */ protected function handleCarry($carry) { return $carry instanceof Responsable ? $carry->toResponse($this->getContainer()->make(Request::class)) : $carry; } /** * Handle the given exception. * * @param mixed $passable * @param \Throwable $e * @return mixed * * @throws \Throwable */ protected function handleException($passable, Throwable $e) { if (! $this->container->bound(ExceptionHandler::class) || ! $passable instanceof Request) { throw $e; } $handler = $this->container->make(ExceptionHandler::class); $handler->report($e); $response = $handler->render($passable, $e); if (is_object($response) && method_exists($response, 'withException')) { $response->withException($e); } return $this->handleCarry($response); } } src/Illuminate/Routing/ResolvesRouteDependencies.php 0000644 00000007015 15107327037 0016736 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Support\Arr; use Illuminate\Support\Reflector; use ReflectionClass; use ReflectionFunctionAbstract; use ReflectionMethod; use ReflectionParameter; use stdClass; trait ResolvesRouteDependencies { /** * Resolve the object method's type-hinted dependencies. * * @param array $parameters * @param object $instance * @param string $method * @return array */ protected function resolveClassMethodDependencies(array $parameters, $instance, $method) { if (! method_exists($instance, $method)) { return $parameters; } return $this->resolveMethodDependencies( $parameters, new ReflectionMethod($instance, $method) ); } /** * Resolve the given method's type-hinted dependencies. * * @param array $parameters * @param \ReflectionFunctionAbstract $reflector * @return array */ public function resolveMethodDependencies(array $parameters, ReflectionFunctionAbstract $reflector) { $instanceCount = 0; $values = array_values($parameters); $skippableValue = new stdClass; foreach ($reflector->getParameters() as $key => $parameter) { $instance = $this->transformDependency($parameter, $parameters, $skippableValue); if ($instance !== $skippableValue) { $instanceCount++; $this->spliceIntoParameters($parameters, $key, $instance); } elseif (! isset($values[$key - $instanceCount]) && $parameter->isDefaultValueAvailable()) { $this->spliceIntoParameters($parameters, $key, $parameter->getDefaultValue()); } } return $parameters; } /** * Attempt to transform the given parameter into a class instance. * * @param \ReflectionParameter $parameter * @param array $parameters * @param object $skippableValue * @return mixed */ protected function transformDependency(ReflectionParameter $parameter, $parameters, $skippableValue) { $className = Reflector::getParameterClassName($parameter); // If the parameter has a type-hinted class, we will check to see if it is already in // the list of parameters. If it is we will just skip it as it is probably a model // binding and we do not want to mess with those; otherwise, we resolve it here. if ($className && ! $this->alreadyInParameters($className, $parameters)) { $isEnum = (new ReflectionClass($className))->isEnum(); return $parameter->isDefaultValueAvailable() ? ($isEnum ? $parameter->getDefaultValue() : null) : $this->container->make($className); } return $skippableValue; } /** * Determine if an object of the given class is in a list of parameters. * * @param string $class * @param array $parameters * @return bool */ protected function alreadyInParameters($class, array $parameters) { return ! is_null(Arr::first($parameters, fn ($value) => $value instanceof $class)); } /** * Splice the given value into the parameter list. * * @param array $parameters * @param string $offset * @param mixed $value * @return void */ protected function spliceIntoParameters(array &$parameters, $offset, $value) { array_splice( $parameters, $offset, 0, [$value] ); } } src/Illuminate/Routing/MiddlewareNameResolver.php 0000644 00000006152 15107327037 0016207 0 ustar 00 <?php namespace Illuminate\Routing; use Closure; class MiddlewareNameResolver { /** * Resolve the middleware name to a class name(s) preserving passed parameters. * * @param \Closure|string $name * @param array $map * @param array $middlewareGroups * @return \Closure|string|array */ public static function resolve($name, $map, $middlewareGroups) { // When the middleware is simply a Closure, we will return this Closure instance // directly so that Closures can be registered as middleware inline, which is // convenient on occasions when the developers are experimenting with them. if ($name instanceof Closure) { return $name; } if (isset($map[$name]) && $map[$name] instanceof Closure) { return $map[$name]; } // If the middleware is the name of a middleware group, we will return the array // of middlewares that belong to the group. This allows developers to group a // set of middleware under single keys that can be conveniently referenced. if (isset($middlewareGroups[$name])) { return static::parseMiddlewareGroup($name, $map, $middlewareGroups); } // Finally, when the middleware is simply a string mapped to a class name the // middleware name will get parsed into the full class name and parameters // which may be run using the Pipeline which accepts this string format. [$name, $parameters] = array_pad(explode(':', $name, 2), 2, null); return ($map[$name] ?? $name).(! is_null($parameters) ? ':'.$parameters : ''); } /** * Parse the middleware group and format it for usage. * * @param string $name * @param array $map * @param array $middlewareGroups * @return array */ protected static function parseMiddlewareGroup($name, $map, $middlewareGroups) { $results = []; foreach ($middlewareGroups[$name] as $middleware) { // If the middleware is another middleware group we will pull in the group and // merge its middleware into the results. This allows groups to conveniently // reference other groups without needing to repeat all their middlewares. if (isset($middlewareGroups[$middleware])) { $results = array_merge($results, static::parseMiddlewareGroup( $middleware, $map, $middlewareGroups )); continue; } [$middleware, $parameters] = array_pad( explode(':', $middleware, 2), 2, null ); // If this middleware is actually a route middleware, we will extract the full // class name out of the middleware list now. Then we'll add the parameters // back onto this class' name so the pipeline will properly extract them. if (isset($map[$middleware])) { $middleware = $map[$middleware]; } $results[] = $middleware.($parameters ? ':'.$parameters : ''); } return $results; } } src/Illuminate/Routing/RouteFileRegistrar.php 0000644 00000001201 15107327037 0015356 0 ustar 00 <?php namespace Illuminate\Routing; class RouteFileRegistrar { /** * The router instance. * * @var \Illuminate\Routing\Router */ protected $router; /** * Create a new route file registrar instance. * * @param \Illuminate\Routing\Router $router * @return void */ public function __construct(Router $router) { $this->router = $router; } /** * Require the given routes file. * * @param string $routes * @return void */ public function register($routes) { $router = $this->router; require $routes; } } src/Illuminate/Routing/ImplicitRouteBinding.php 0000644 00000010443 15107327037 0015671 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Contracts\Routing\UrlRoutable; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException; use Illuminate\Support\Reflector; use Illuminate\Support\Str; class ImplicitRouteBinding { /** * Resolve the implicit route bindings for the given route. * * @param \Illuminate\Container\Container $container * @param \Illuminate\Routing\Route $route * @return void * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> * @throws \Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException */ public static function resolveForRoute($container, $route) { $parameters = $route->parameters(); $route = static::resolveBackedEnumsForRoute($route, $parameters); foreach ($route->signatureParameters(['subClass' => UrlRoutable::class]) as $parameter) { if (! $parameterName = static::getParameterName($parameter->getName(), $parameters)) { continue; } $parameterValue = $parameters[$parameterName]; if ($parameterValue instanceof UrlRoutable) { continue; } $instance = $container->make(Reflector::getParameterClassName($parameter)); $parent = $route->parentOfParameter($parameterName); $routeBindingMethod = $route->allowsTrashedBindings() && in_array(SoftDeletes::class, class_uses_recursive($instance)) ? 'resolveSoftDeletableRouteBinding' : 'resolveRouteBinding'; if ($parent instanceof UrlRoutable && ! $route->preventsScopedBindings() && ($route->enforcesScopedBindings() || array_key_exists($parameterName, $route->bindingFields()))) { $childRouteBindingMethod = $route->allowsTrashedBindings() && in_array(SoftDeletes::class, class_uses_recursive($instance)) ? 'resolveSoftDeletableChildRouteBinding' : 'resolveChildRouteBinding'; if (! $model = $parent->{$childRouteBindingMethod}( $parameterName, $parameterValue, $route->bindingFieldFor($parameterName) )) { throw (new ModelNotFoundException)->setModel(get_class($instance), [$parameterValue]); } } elseif (! $model = $instance->{$routeBindingMethod}($parameterValue, $route->bindingFieldFor($parameterName))) { throw (new ModelNotFoundException)->setModel(get_class($instance), [$parameterValue]); } $route->setParameter($parameterName, $model); } } /** * Resolve the Backed Enums route bindings for the route. * * @param \Illuminate\Routing\Route $route * @param array $parameters * @return \Illuminate\Routing\Route * * @throws \Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException */ protected static function resolveBackedEnumsForRoute($route, $parameters) { foreach ($route->signatureParameters(['backedEnum' => true]) as $parameter) { if (! $parameterName = static::getParameterName($parameter->getName(), $parameters)) { continue; } $parameterValue = $parameters[$parameterName]; $backedEnumClass = $parameter->getType()?->getName(); $backedEnum = $backedEnumClass::tryFrom((string) $parameterValue); if (is_null($backedEnum)) { throw new BackedEnumCaseNotFoundException($backedEnumClass, $parameterValue); } $route->setParameter($parameterName, $backedEnum); } return $route; } /** * Return the parameter name if it exists in the given parameters. * * @param string $name * @param array $parameters * @return string|null */ protected static function getParameterName($name, $parameters) { if (array_key_exists($name, $parameters)) { return $name; } if (array_key_exists($snakedName = Str::snake($name), $parameters)) { return $snakedName; } } } src/Illuminate/Routing/RedirectController.php 0000644 00000002264 15107327037 0015414 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Str; class RedirectController extends Controller { /** * Invoke the controller method. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Routing\UrlGenerator $url * @return \Illuminate\Http\RedirectResponse */ public function __invoke(Request $request, UrlGenerator $url) { $parameters = collect($request->route()->parameters()); $status = $parameters->get('status'); $destination = $parameters->get('destination'); $parameters->forget('status')->forget('destination'); $route = (new Route('GET', $destination, [ 'as' => 'laravel_route_redirect_destination', ]))->bind($request); $parameters = $parameters->only( $route->getCompiled()->getPathVariables() )->all(); $url = $url->toRoute($route, $parameters, false); if (! str_starts_with($destination, '/') && str_starts_with($url, '/')) { $url = Str::after($url, '/'); } return new RedirectResponse($url, $status); } } src/Illuminate/Routing/ControllerMiddlewareOptions.php 0000644 00000001761 15107327037 0017305 0 ustar 00 <?php namespace Illuminate\Routing; class ControllerMiddlewareOptions { /** * The middleware options. * * @var array */ protected $options; /** * Create a new middleware option instance. * * @param array $options * @return void */ public function __construct(array &$options) { $this->options = &$options; } /** * Set the controller methods the middleware should apply to. * * @param array|string|mixed $methods * @return $this */ public function only($methods) { $this->options['only'] = is_array($methods) ? $methods : func_get_args(); return $this; } /** * Set the controller methods the middleware should exclude. * * @param array|string|mixed $methods * @return $this */ public function except($methods) { $this->options['except'] = is_array($methods) ? $methods : func_get_args(); return $this; } } src/Illuminate/Routing/PendingResourceRegistration.php 0000644 00000014005 15107327037 0017272 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Support\Arr; use Illuminate\Support\Traits\Macroable; class PendingResourceRegistration { use CreatesRegularExpressionRouteConstraints, Macroable; /** * The resource registrar. * * @var \Illuminate\Routing\ResourceRegistrar */ protected $registrar; /** * The resource name. * * @var string */ protected $name; /** * The resource controller. * * @var string */ protected $controller; /** * The resource options. * * @var array */ protected $options = []; /** * The resource's registration status. * * @var bool */ protected $registered = false; /** * Create a new pending resource registration instance. * * @param \Illuminate\Routing\ResourceRegistrar $registrar * @param string $name * @param string $controller * @param array $options * @return void */ public function __construct(ResourceRegistrar $registrar, $name, $controller, array $options) { $this->name = $name; $this->options = $options; $this->registrar = $registrar; $this->controller = $controller; } /** * Set the methods the controller should apply to. * * @param array|string|mixed $methods * @return \Illuminate\Routing\PendingResourceRegistration */ public function only($methods) { $this->options['only'] = is_array($methods) ? $methods : func_get_args(); return $this; } /** * Set the methods the controller should exclude. * * @param array|string|mixed $methods * @return \Illuminate\Routing\PendingResourceRegistration */ public function except($methods) { $this->options['except'] = is_array($methods) ? $methods : func_get_args(); return $this; } /** * Set the route names for controller actions. * * @param array|string $names * @return \Illuminate\Routing\PendingResourceRegistration */ public function names($names) { $this->options['names'] = $names; return $this; } /** * Set the route name for a controller action. * * @param string $method * @param string $name * @return \Illuminate\Routing\PendingResourceRegistration */ public function name($method, $name) { $this->options['names'][$method] = $name; return $this; } /** * Override the route parameter names. * * @param array|string $parameters * @return \Illuminate\Routing\PendingResourceRegistration */ public function parameters($parameters) { $this->options['parameters'] = $parameters; return $this; } /** * Override a route parameter's name. * * @param string $previous * @param string $new * @return \Illuminate\Routing\PendingResourceRegistration */ public function parameter($previous, $new) { $this->options['parameters'][$previous] = $new; return $this; } /** * Add middleware to the resource routes. * * @param mixed $middleware * @return \Illuminate\Routing\PendingResourceRegistration */ public function middleware($middleware) { $middleware = Arr::wrap($middleware); foreach ($middleware as $key => $value) { $middleware[$key] = (string) $value; } $this->options['middleware'] = $middleware; return $this; } /** * Specify middleware that should be removed from the resource routes. * * @param array|string $middleware * @return $this|array */ public function withoutMiddleware($middleware) { $this->options['excluded_middleware'] = array_merge( (array) ($this->options['excluded_middleware'] ?? []), Arr::wrap($middleware) ); return $this; } /** * Add "where" constraints to the resource routes. * * @param mixed $wheres * @return \Illuminate\Routing\PendingResourceRegistration */ public function where($wheres) { $this->options['wheres'] = $wheres; return $this; } /** * Indicate that the resource routes should have "shallow" nesting. * * @param bool $shallow * @return \Illuminate\Routing\PendingResourceRegistration */ public function shallow($shallow = true) { $this->options['shallow'] = $shallow; return $this; } /** * Define the callable that should be invoked on a missing model exception. * * @param callable $callback * @return $this */ public function missing($callback) { $this->options['missing'] = $callback; return $this; } /** * Indicate that the resource routes should be scoped using the given binding fields. * * @param array $fields * @return \Illuminate\Routing\PendingResourceRegistration */ public function scoped(array $fields = []) { $this->options['bindingFields'] = $fields; return $this; } /** * Define which routes should allow "trashed" models to be retrieved when resolving implicit model bindings. * * @param array $methods * @return \Illuminate\Routing\PendingResourceRegistration */ public function withTrashed(array $methods = []) { $this->options['trashed'] = $methods; return $this; } /** * Register the resource route. * * @return \Illuminate\Routing\RouteCollection */ public function register() { $this->registered = true; return $this->registrar->register( $this->name, $this->controller, $this->options ); } /** * Handle the object's destruction. * * @return void */ public function __destruct() { if (! $this->registered) { $this->register(); } } } src/Illuminate/Routing/RouteSignatureParameters.php 0000644 00000003146 15107327037 0016613 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Support\Reflector; use Illuminate\Support\Str; use ReflectionFunction; use ReflectionMethod; class RouteSignatureParameters { /** * Extract the route action's signature parameters. * * @param array $action * @param array $conditions * @return array */ public static function fromAction(array $action, $conditions = []) { $callback = RouteAction::containsSerializedClosure($action) ? unserialize($action['uses'])->getClosure() : $action['uses']; $parameters = is_string($callback) ? static::fromClassMethodString($callback) : (new ReflectionFunction($callback))->getParameters(); return match (true) { ! empty($conditions['subClass']) => array_filter($parameters, fn ($p) => Reflector::isParameterSubclassOf($p, $conditions['subClass'])), ! empty($conditions['backedEnum']) => array_filter($parameters, fn ($p) => Reflector::isParameterBackedEnumWithStringBackingType($p)), default => $parameters, }; } /** * Get the parameters for the given class / method by string. * * @param string $uses * @return array */ protected static function fromClassMethodString($uses) { [$class, $method] = Str::parseCallback($uses); if (! method_exists($class, $method) && Reflector::isCallable($class, $method)) { return []; } return (new ReflectionMethod($class, $method))->getParameters(); } } src/Illuminate/Routing/ControllerDispatcher.php 0000644 00000004317 15107327037 0015742 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Container\Container; use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract; class ControllerDispatcher implements ControllerDispatcherContract { use FiltersControllerMiddleware, ResolvesRouteDependencies; /** * The container instance. * * @var \Illuminate\Container\Container */ protected $container; /** * Create a new controller dispatcher instance. * * @param \Illuminate\Container\Container $container * @return void */ public function __construct(Container $container) { $this->container = $container; } /** * Dispatch a request to a given controller and method. * * @param \Illuminate\Routing\Route $route * @param mixed $controller * @param string $method * @return mixed */ public function dispatch(Route $route, $controller, $method) { $parameters = $this->resolveParameters($route, $controller, $method); if (method_exists($controller, 'callAction')) { return $controller->callAction($method, $parameters); } return $controller->{$method}(...array_values($parameters)); } /** * Resolve the parameters for the controller. * * @param \Illuminate\Routing\Route $route * @param mixed $controller * @param string $method * @return array */ protected function resolveParameters(Route $route, $controller, $method) { return $this->resolveClassMethodDependencies( $route->parametersWithoutNulls(), $controller, $method ); } /** * Get the middleware for the controller instance. * * @param \Illuminate\Routing\Controller $controller * @param string $method * @return array */ public function getMiddleware($controller, $method) { if (! method_exists($controller, 'getMiddleware')) { return []; } return collect($controller->getMiddleware())->reject(function ($data) use ($method) { return static::methodExcludedByOptions($method, $data['options']); })->pluck('middleware')->all(); } } src/Illuminate/Routing/RouteParameterBinder.php 0000644 00000006056 15107327037 0015675 0 ustar 00 <?php namespace Illuminate\Routing; use Illuminate\Support\Arr; class RouteParameterBinder { /** * The route instance. * * @var \Illuminate\Routing\Route */ protected $route; /** * Create a new Route parameter binder instance. * * @param \Illuminate\Routing\Route $route * @return void */ public function __construct($route) { $this->route = $route; } /** * Get the parameters for the route. * * @param \Illuminate\Http\Request $request * @return array */ public function parameters($request) { $parameters = $this->bindPathParameters($request); // If the route has a regular expression for the host part of the URI, we will // compile that and get the parameter matches for this domain. We will then // merge them into this parameters array so that this array is completed. if (! is_null($this->route->compiled->getHostRegex())) { $parameters = $this->bindHostParameters( $request, $parameters ); } return $this->replaceDefaults($parameters); } /** * Get the parameter matches for the path portion of the URI. * * @param \Illuminate\Http\Request $request * @return array */ protected function bindPathParameters($request) { $path = '/'.ltrim($request->decodedPath(), '/'); preg_match($this->route->compiled->getRegex(), $path, $matches); return $this->matchToKeys(array_slice($matches, 1)); } /** * Extract the parameter list from the host part of the request. * * @param \Illuminate\Http\Request $request * @param array $parameters * @return array */ protected function bindHostParameters($request, $parameters) { preg_match($this->route->compiled->getHostRegex(), $request->getHost(), $matches); return array_merge($this->matchToKeys(array_slice($matches, 1)), $parameters); } /** * Combine a set of parameter matches with the route's keys. * * @param array $matches * @return array */ protected function matchToKeys(array $matches) { if (empty($parameterNames = $this->route->parameterNames())) { return []; } $parameters = array_intersect_key($matches, array_flip($parameterNames)); return array_filter($parameters, function ($value) { return is_string($value) && strlen($value) > 0; }); } /** * Replace null parameters with their defaults. * * @param array $parameters * @return array */ protected function replaceDefaults(array $parameters) { foreach ($parameters as $key => $value) { $parameters[$key] = $value ?? Arr::get($this->route->defaults, $key); } foreach ($this->route->defaults as $key => $value) { if (! isset($parameters[$key])) { $parameters[$key] = $value; } } return $parameters; } } src/Illuminate/Translation/FileLoader.php 0000644 00000012052 15107327037 0014460 0 ustar 00 <?php namespace Illuminate\Translation; use Illuminate\Contracts\Translation\Loader; use Illuminate\Filesystem\Filesystem; use RuntimeException; class FileLoader implements Loader { /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * The default paths for the loader. * * @var array */ protected $paths; /** * All of the registered paths to JSON translation files. * * @var array */ protected $jsonPaths = []; /** * All of the namespace hints. * * @var array */ protected $hints = []; /** * Create a new file loader instance. * * @param \Illuminate\Filesystem\Filesystem $files * @param array|string $path * @return void */ public function __construct(Filesystem $files, array|string $path) { $this->files = $files; $this->paths = is_string($path) ? [$path] : $path; } /** * Load the messages for the given locale. * * @param string $locale * @param string $group * @param string|null $namespace * @return array */ public function load($locale, $group, $namespace = null) { if ($group === '*' && $namespace === '*') { return $this->loadJsonPaths($locale); } if (is_null($namespace) || $namespace === '*') { return $this->loadPaths($this->paths, $locale, $group); } return $this->loadNamespaced($locale, $group, $namespace); } /** * Load a namespaced translation group. * * @param string $locale * @param string $group * @param string $namespace * @return array */ protected function loadNamespaced($locale, $group, $namespace) { if (isset($this->hints[$namespace])) { $lines = $this->loadPaths([$this->hints[$namespace]], $locale, $group); return $this->loadNamespaceOverrides($lines, $locale, $group, $namespace); } return []; } /** * Load a local namespaced translation group for overrides. * * @param array $lines * @param string $locale * @param string $group * @param string $namespace * @return array */ protected function loadNamespaceOverrides(array $lines, $locale, $group, $namespace) { return collect($this->paths) ->reduce(function ($output, $path) use ($lines, $locale, $group, $namespace) { $file = "{$path}/vendor/{$namespace}/{$locale}/{$group}.php"; if ($this->files->exists($file)) { $lines = array_replace_recursive($lines, $this->files->getRequire($file)); } return $lines; }, []); } /** * Load a locale from a given path. * * @param array $paths * @param string $locale * @param string $group * @return array */ protected function loadPaths(array $paths, $locale, $group) { return collect($paths) ->reduce(function ($output, $path) use ($locale, $group) { if ($this->files->exists($full = "{$path}/{$locale}/{$group}.php")) { $output = array_replace_recursive($output, $this->files->getRequire($full)); } return $output; }, []); } /** * Load a locale from the given JSON file path. * * @param string $locale * @return array * * @throws \RuntimeException */ protected function loadJsonPaths($locale) { return collect(array_merge($this->jsonPaths, $this->paths)) ->reduce(function ($output, $path) use ($locale) { if ($this->files->exists($full = "{$path}/{$locale}.json")) { $decoded = json_decode($this->files->get($full), true); if (is_null($decoded) || json_last_error() !== JSON_ERROR_NONE) { throw new RuntimeException("Translation file [{$full}] contains an invalid JSON structure."); } $output = array_merge($output, $decoded); } return $output; }, []); } /** * Add a new namespace to the loader. * * @param string $namespace * @param string $hint * @return void */ public function addNamespace($namespace, $hint) { $this->hints[$namespace] = $hint; } /** * Get an array of all the registered namespaces. * * @return array */ public function namespaces() { return $this->hints; } /** * Add a new JSON path to the loader. * * @param string $path * @return void */ public function addJsonPath($path) { $this->jsonPaths[] = $path; } /** * Get an array of all the registered paths to JSON translation files. * * @return array */ public function jsonPaths() { return $this->jsonPaths; } } src/Illuminate/Translation/MessageSelector.php 0000644 00000026627 15107327037 0015554 0 ustar 00 <?php namespace Illuminate\Translation; class MessageSelector { /** * Select a proper translation string based on the given number. * * @param string $line * @param int $number * @param string $locale * @return mixed */ public function choose($line, $number, $locale) { $segments = explode('|', $line); if (($value = $this->extract($segments, $number)) !== null) { return trim($value); } $segments = $this->stripConditions($segments); $pluralIndex = $this->getPluralIndex($locale, $number); if (count($segments) === 1 || ! isset($segments[$pluralIndex])) { return $segments[0]; } return $segments[$pluralIndex]; } /** * Extract a translation string using inline conditions. * * @param array $segments * @param int $number * @return mixed */ private function extract($segments, $number) { foreach ($segments as $part) { if (! is_null($line = $this->extractFromString($part, $number))) { return $line; } } } /** * Get the translation string if the condition matches. * * @param string $part * @param int $number * @return mixed */ private function extractFromString($part, $number) { preg_match('/^[\{\[]([^\[\]\{\}]*)[\}\]](.*)/s', $part, $matches); if (count($matches) !== 3) { return null; } $condition = $matches[1]; $value = $matches[2]; if (str_contains($condition, ',')) { [$from, $to] = explode(',', $condition, 2); if ($to === '*' && $number >= $from) { return $value; } elseif ($from === '*' && $number <= $to) { return $value; } elseif ($number >= $from && $number <= $to) { return $value; } } return $condition == $number ? $value : null; } /** * Strip the inline conditions from each segment, just leaving the text. * * @param array $segments * @return array */ private function stripConditions($segments) { return collect($segments) ->map(fn ($part) => preg_replace('/^[\{\[]([^\[\]\{\}]*)[\}\]]/', '', $part)) ->all(); } /** * Get the index to use for pluralization. * * The plural rules are derived from code of the Zend Framework (2010-09-25), which * is subject to the new BSD license (https://framework.zend.com/license) * Copyright (c) 2005-2010 - Zend Technologies USA Inc. (http://www.zend.com) * * @param string $locale * @param int $number * @return int */ public function getPluralIndex($locale, $number) { switch ($locale) { case 'az': case 'az_AZ': case 'bo': case 'bo_CN': case 'bo_IN': case 'dz': case 'dz_BT': case 'id': case 'id_ID': case 'ja': case 'ja_JP': case 'jv': case 'ka': case 'ka_GE': case 'km': case 'km_KH': case 'kn': case 'kn_IN': case 'ko': case 'ko_KR': case 'ms': case 'ms_MY': case 'th': case 'th_TH': case 'tr': case 'tr_CY': case 'tr_TR': case 'vi': case 'vi_VN': case 'zh': case 'zh_CN': case 'zh_HK': case 'zh_SG': case 'zh_TW': return 0; case 'af': case 'af_ZA': case 'bn': case 'bn_BD': case 'bn_IN': case 'bg': case 'bg_BG': case 'ca': case 'ca_AD': case 'ca_ES': case 'ca_FR': case 'ca_IT': case 'da': case 'da_DK': case 'de': case 'de_AT': case 'de_BE': case 'de_CH': case 'de_DE': case 'de_LI': case 'de_LU': case 'el': case 'el_CY': case 'el_GR': case 'en': case 'en_AG': case 'en_AU': case 'en_BW': case 'en_CA': case 'en_DK': case 'en_GB': case 'en_HK': case 'en_IE': case 'en_IN': case 'en_NG': case 'en_NZ': case 'en_PH': case 'en_SG': case 'en_US': case 'en_ZA': case 'en_ZM': case 'en_ZW': case 'eo': case 'eo_US': case 'es': case 'es_AR': case 'es_BO': case 'es_CL': case 'es_CO': case 'es_CR': case 'es_CU': case 'es_DO': case 'es_EC': case 'es_ES': case 'es_GT': case 'es_HN': case 'es_MX': case 'es_NI': case 'es_PA': case 'es_PE': case 'es_PR': case 'es_PY': case 'es_SV': case 'es_US': case 'es_UY': case 'es_VE': case 'et': case 'et_EE': case 'eu': case 'eu_ES': case 'eu_FR': case 'fa': case 'fa_IR': case 'fi': case 'fi_FI': case 'fo': case 'fo_FO': case 'fur': case 'fur_IT': case 'fy': case 'fy_DE': case 'fy_NL': case 'gl': case 'gl_ES': case 'gu': case 'gu_IN': case 'ha': case 'ha_NG': case 'he': case 'he_IL': case 'hu': case 'hu_HU': case 'is': case 'is_IS': case 'it': case 'it_CH': case 'it_IT': case 'ku': case 'ku_TR': case 'lb': case 'lb_LU': case 'ml': case 'ml_IN': case 'mn': case 'mn_MN': case 'mr': case 'mr_IN': case 'nah': case 'nb': case 'nb_NO': case 'ne': case 'ne_NP': case 'nl': case 'nl_AW': case 'nl_BE': case 'nl_NL': case 'nn': case 'nn_NO': case 'no': case 'om': case 'om_ET': case 'om_KE': case 'or': case 'or_IN': case 'pa': case 'pa_IN': case 'pa_PK': case 'pap': case 'pap_AN': case 'pap_AW': case 'pap_CW': case 'ps': case 'ps_AF': case 'pt': case 'pt_BR': case 'pt_PT': case 'so': case 'so_DJ': case 'so_ET': case 'so_KE': case 'so_SO': case 'sq': case 'sq_AL': case 'sq_MK': case 'sv': case 'sv_FI': case 'sv_SE': case 'sw': case 'sw_KE': case 'sw_TZ': case 'ta': case 'ta_IN': case 'ta_LK': case 'te': case 'te_IN': case 'tk': case 'tk_TM': case 'ur': case 'ur_IN': case 'ur_PK': case 'zu': case 'zu_ZA': return ($number == 1) ? 0 : 1; case 'am': case 'am_ET': case 'bh': case 'fil': case 'fil_PH': case 'fr': case 'fr_BE': case 'fr_CA': case 'fr_CH': case 'fr_FR': case 'fr_LU': case 'gun': case 'hi': case 'hi_IN': case 'hy': case 'hy_AM': case 'ln': case 'ln_CD': case 'mg': case 'mg_MG': case 'nso': case 'nso_ZA': case 'ti': case 'ti_ER': case 'ti_ET': case 'wa': case 'wa_BE': case 'xbr': return (($number == 0) || ($number == 1)) ? 0 : 1; case 'be': case 'be_BY': case 'bs': case 'bs_BA': case 'hr': case 'hr_HR': case 'ru': case 'ru_RU': case 'ru_UA': case 'sr': case 'sr_ME': case 'sr_RS': case 'uk': case 'uk_UA': return (($number % 10 == 1) && ($number % 100 != 11)) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2); case 'cs': case 'cs_CZ': case 'sk': case 'sk_SK': return ($number == 1) ? 0 : ((($number >= 2) && ($number <= 4)) ? 1 : 2); case 'ga': case 'ga_IE': return ($number == 1) ? 0 : (($number == 2) ? 1 : 2); case 'lt': case 'lt_LT': return (($number % 10 == 1) && ($number % 100 != 11)) ? 0 : ((($number % 10 >= 2) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2); case 'sl': case 'sl_SI': return ($number % 100 == 1) ? 0 : (($number % 100 == 2) ? 1 : ((($number % 100 == 3) || ($number % 100 == 4)) ? 2 : 3)); case 'mk': case 'mk_MK': return ($number % 10 == 1) ? 0 : 1; case 'mt': case 'mt_MT': return ($number == 1) ? 0 : ((($number == 0) || (($number % 100 > 1) && ($number % 100 < 11))) ? 1 : ((($number % 100 > 10) && ($number % 100 < 20)) ? 2 : 3)); case 'lv': case 'lv_LV': return ($number == 0) ? 0 : ((($number % 10 == 1) && ($number % 100 != 11)) ? 1 : 2); case 'pl': case 'pl_PL': return ($number == 1) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 12) || ($number % 100 > 14))) ? 1 : 2); case 'cy': case 'cy_GB': return ($number == 1) ? 0 : (($number == 2) ? 1 : ((($number == 8) || ($number == 11)) ? 2 : 3)); case 'ro': case 'ro_RO': return ($number == 1) ? 0 : ((($number == 0) || (($number % 100 > 0) && ($number % 100 < 20))) ? 1 : 2); case 'ar': case 'ar_AE': case 'ar_BH': case 'ar_DZ': case 'ar_EG': case 'ar_IN': case 'ar_IQ': case 'ar_JO': case 'ar_KW': case 'ar_LB': case 'ar_LY': case 'ar_MA': case 'ar_OM': case 'ar_QA': case 'ar_SA': case 'ar_SD': case 'ar_SS': case 'ar_SY': case 'ar_TN': case 'ar_YE': return ($number == 0) ? 0 : (($number == 1) ? 1 : (($number == 2) ? 2 : ((($number % 100 >= 3) && ($number % 100 <= 10)) ? 3 : ((($number % 100 >= 11) && ($number % 100 <= 99)) ? 4 : 5)))); default: return 0; } } } src/Illuminate/Translation/ArrayLoader.php 0000644 00000003117 15107327037 0014661 0 ustar 00 <?php namespace Illuminate\Translation; use Illuminate\Contracts\Translation\Loader; class ArrayLoader implements Loader { /** * All of the translation messages. * * @var array */ protected $messages = []; /** * Load the messages for the given locale. * * @param string $locale * @param string $group * @param string|null $namespace * @return array */ public function load($locale, $group, $namespace = null) { $namespace = $namespace ?: '*'; return $this->messages[$namespace][$locale][$group] ?? []; } /** * Add a new namespace to the loader. * * @param string $namespace * @param string $hint * @return void */ public function addNamespace($namespace, $hint) { // } /** * Add a new JSON path to the loader. * * @param string $path * @return void */ public function addJsonPath($path) { // } /** * Add messages to the loader. * * @param string $locale * @param string $group * @param array $messages * @param string|null $namespace * @return $this */ public function addMessages($locale, $group, array $messages, $namespace = null) { $namespace = $namespace ?: '*'; $this->messages[$namespace][$locale][$group] = $messages; return $this; } /** * Get an array of all the registered namespaces. * * @return array */ public function namespaces() { return []; } } src/Illuminate/Translation/lang/en/pagination.php 0000644 00000001026 15107327037 0016125 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | Pagination Language Lines |-------------------------------------------------------------------------- | | The following language lines are used by the paginator library to build | the simple pagination links. You are free to change them to anything | you want to customize your views to better match your application. | */ 'previous' => '« Previous', 'next' => 'Next »', ]; src/Illuminate/Translation/lang/en/auth.php 0000644 00000001242 15107327037 0014735 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | Authentication Language Lines |-------------------------------------------------------------------------- | | The following language lines are used during authentication for various | messages that we need to display to the user. You are free to modify | these language lines according to your application's requirements. | */ 'failed' => 'These credentials do not match our records.', 'password' => 'The provided password is incorrect.', 'throttle' => 'Too many login attempts. Please try again in :seconds seconds.', ]; src/Illuminate/Translation/lang/en/passwords.php 0000644 00000001350 15107327037 0016021 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | Password Reset Language Lines |-------------------------------------------------------------------------- | | The following language lines are the default lines which match reasons | that are given by the password broker for a password update attempt | has failed, such as for an invalid token or invalid new password. | */ 'reset' => 'Your password has been reset.', 'sent' => 'We have emailed your password reset link.', 'throttled' => 'Please wait before retrying.', 'token' => 'This password reset token is invalid.', 'user' => "We can't find a user with that email address.", ]; src/Illuminate/Translation/lang/en/validation.php 0000644 00000024724 15107327037 0016140 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | Validation Language Lines |-------------------------------------------------------------------------- | | The following language lines contain the default error messages used by | the validator class. Some of these rules have multiple versions such | as the size rules. Feel free to tweak each of these messages here. | */ 'accepted' => 'The :attribute field must be accepted.', 'accepted_if' => 'The :attribute field must be accepted when :other is :value.', 'active_url' => 'The :attribute field must be a valid URL.', 'after' => 'The :attribute field must be a date after :date.', 'after_or_equal' => 'The :attribute field must be a date after or equal to :date.', 'alpha' => 'The :attribute field must only contain letters.', 'alpha_dash' => 'The :attribute field must only contain letters, numbers, dashes, and underscores.', 'alpha_num' => 'The :attribute field must only contain letters and numbers.', 'array' => 'The :attribute field must be an array.', 'ascii' => 'The :attribute field must only contain single-byte alphanumeric characters and symbols.', 'before' => 'The :attribute field must be a date before :date.', 'before_or_equal' => 'The :attribute field must be a date before or equal to :date.', 'between' => [ 'array' => 'The :attribute field must have between :min and :max items.', 'file' => 'The :attribute field must be between :min and :max kilobytes.', 'numeric' => 'The :attribute field must be between :min and :max.', 'string' => 'The :attribute field must be between :min and :max characters.', ], 'boolean' => 'The :attribute field must be true or false.', 'can' => 'The :attribute field contains an unauthorized value.', 'confirmed' => 'The :attribute field confirmation does not match.', 'current_password' => 'The password is incorrect.', 'date' => 'The :attribute field must be a valid date.', 'date_equals' => 'The :attribute field must be a date equal to :date.', 'date_format' => 'The :attribute field must match the format :format.', 'decimal' => 'The :attribute field must have :decimal decimal places.', 'declined' => 'The :attribute field must be declined.', 'declined_if' => 'The :attribute field must be declined when :other is :value.', 'different' => 'The :attribute field and :other must be different.', 'digits' => 'The :attribute field must be :digits digits.', 'digits_between' => 'The :attribute field must be between :min and :max digits.', 'dimensions' => 'The :attribute field has invalid image dimensions.', 'distinct' => 'The :attribute field has a duplicate value.', 'doesnt_end_with' => 'The :attribute field must not end with one of the following: :values.', 'doesnt_start_with' => 'The :attribute field must not start with one of the following: :values.', 'email' => 'The :attribute field must be a valid email address.', 'ends_with' => 'The :attribute field must end with one of the following: :values.', 'enum' => 'The selected :attribute is invalid.', 'exists' => 'The selected :attribute is invalid.', 'file' => 'The :attribute field must be a file.', 'filled' => 'The :attribute field must have a value.', 'gt' => [ 'array' => 'The :attribute field must have more than :value items.', 'file' => 'The :attribute field must be greater than :value kilobytes.', 'numeric' => 'The :attribute field must be greater than :value.', 'string' => 'The :attribute field must be greater than :value characters.', ], 'gte' => [ 'array' => 'The :attribute field must have :value items or more.', 'file' => 'The :attribute field must be greater than or equal to :value kilobytes.', 'numeric' => 'The :attribute field must be greater than or equal to :value.', 'string' => 'The :attribute field must be greater than or equal to :value characters.', ], 'image' => 'The :attribute field must be an image.', 'in' => 'The selected :attribute is invalid.', 'in_array' => 'The :attribute field must exist in :other.', 'integer' => 'The :attribute field must be an integer.', 'ip' => 'The :attribute field must be a valid IP address.', 'ipv4' => 'The :attribute field must be a valid IPv4 address.', 'ipv6' => 'The :attribute field must be a valid IPv6 address.', 'json' => 'The :attribute field must be a valid JSON string.', 'lowercase' => 'The :attribute field must be lowercase.', 'lt' => [ 'array' => 'The :attribute field must have less than :value items.', 'file' => 'The :attribute field must be less than :value kilobytes.', 'numeric' => 'The :attribute field must be less than :value.', 'string' => 'The :attribute field must be less than :value characters.', ], 'lte' => [ 'array' => 'The :attribute field must not have more than :value items.', 'file' => 'The :attribute field must be less than or equal to :value kilobytes.', 'numeric' => 'The :attribute field must be less than or equal to :value.', 'string' => 'The :attribute field must be less than or equal to :value characters.', ], 'mac_address' => 'The :attribute field must be a valid MAC address.', 'max' => [ 'array' => 'The :attribute field must not have more than :max items.', 'file' => 'The :attribute field must not be greater than :max kilobytes.', 'numeric' => 'The :attribute field must not be greater than :max.', 'string' => 'The :attribute field must not be greater than :max characters.', ], 'max_digits' => 'The :attribute field must not have more than :max digits.', 'mimes' => 'The :attribute field must be a file of type: :values.', 'mimetypes' => 'The :attribute field must be a file of type: :values.', 'min' => [ 'array' => 'The :attribute field must have at least :min items.', 'file' => 'The :attribute field must be at least :min kilobytes.', 'numeric' => 'The :attribute field must be at least :min.', 'string' => 'The :attribute field must be at least :min characters.', ], 'min_digits' => 'The :attribute field must have at least :min digits.', 'missing' => 'The :attribute field must be missing.', 'missing_if' => 'The :attribute field must be missing when :other is :value.', 'missing_unless' => 'The :attribute field must be missing unless :other is :value.', 'missing_with' => 'The :attribute field must be missing when :values is present.', 'missing_with_all' => 'The :attribute field must be missing when :values are present.', 'multiple_of' => 'The :attribute field must be a multiple of :value.', 'not_in' => 'The selected :attribute is invalid.', 'not_regex' => 'The :attribute field format is invalid.', 'numeric' => 'The :attribute field must be a number.', 'password' => [ 'letters' => 'The :attribute field must contain at least one letter.', 'mixed' => 'The :attribute field must contain at least one uppercase and one lowercase letter.', 'numbers' => 'The :attribute field must contain at least one number.', 'symbols' => 'The :attribute field must contain at least one symbol.', 'uncompromised' => 'The given :attribute has appeared in a data leak. Please choose a different :attribute.', ], 'present' => 'The :attribute field must be present.', 'prohibited' => 'The :attribute field is prohibited.', 'prohibited_if' => 'The :attribute field is prohibited when :other is :value.', 'prohibited_unless' => 'The :attribute field is prohibited unless :other is in :values.', 'prohibits' => 'The :attribute field prohibits :other from being present.', 'regex' => 'The :attribute field format is invalid.', 'required' => 'The :attribute field is required.', 'required_array_keys' => 'The :attribute field must contain entries for: :values.', 'required_if' => 'The :attribute field is required when :other is :value.', 'required_if_accepted' => 'The :attribute field is required when :other is accepted.', 'required_unless' => 'The :attribute field is required unless :other is in :values.', 'required_with' => 'The :attribute field is required when :values is present.', 'required_with_all' => 'The :attribute field is required when :values are present.', 'required_without' => 'The :attribute field is required when :values is not present.', 'required_without_all' => 'The :attribute field is required when none of :values are present.', 'same' => 'The :attribute field must match :other.', 'size' => [ 'array' => 'The :attribute field must contain :size items.', 'file' => 'The :attribute field must be :size kilobytes.', 'numeric' => 'The :attribute field must be :size.', 'string' => 'The :attribute field must be :size characters.', ], 'starts_with' => 'The :attribute field must start with one of the following: :values.', 'string' => 'The :attribute field must be a string.', 'timezone' => 'The :attribute field must be a valid timezone.', 'unique' => 'The :attribute has already been taken.', 'uploaded' => 'The :attribute failed to upload.', 'uppercase' => 'The :attribute field must be uppercase.', 'url' => 'The :attribute field must be a valid URL.', 'ulid' => 'The :attribute field must be a valid ULID.', 'uuid' => 'The :attribute field must be a valid UUID.', /* |-------------------------------------------------------------------------- | Custom Validation Language Lines |-------------------------------------------------------------------------- | | Here you may specify custom validation messages for attributes using the | convention "attribute.rule" to name the lines. This makes it quick to | specify a specific custom language line for a given attribute rule. | */ 'custom' => [ 'attribute-name' => [ 'rule-name' => 'custom-message', ], ], /* |-------------------------------------------------------------------------- | Custom Validation Attributes |-------------------------------------------------------------------------- | | The following language lines are used to swap our attribute placeholder | with something more reader friendly such as "E-Mail Address" instead | of "email". This simply helps us make our message more expressive. | */ 'attributes' => [], ]; src/Illuminate/Translation/composer.json 0000644 00000001677 15107327037 0014476 0 ustar 00 { "name": "illuminate/translation", "description": "The Illuminate Translation package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/collections": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0", "illuminate/filesystem": "^10.0", "illuminate/support": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Translation\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Translation/LICENSE.md 0000644 00000002063 15107327037 0013346 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Translation/TranslationServiceProvider.php 0000644 00000002656 15107327037 0020015 0 ustar 00 <?php namespace Illuminate\Translation; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; class TranslationServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Register the service provider. * * @return void */ public function register() { $this->registerLoader(); $this->app->singleton('translator', function ($app) { $loader = $app['translation.loader']; // When registering the translator component, we'll need to set the default // locale as well as the fallback locale. So, we'll grab the application // configuration so we can easily get both of these values from there. $locale = $app->getLocale(); $trans = new Translator($loader, $locale); $trans->setFallback($app->getFallbackLocale()); return $trans; }); } /** * Register the translation line loader. * * @return void */ protected function registerLoader() { $this->app->singleton('translation.loader', function ($app) { return new FileLoader($app['files'], [__DIR__.'/lang', $app['path.lang']]); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return ['translator', 'translation.loader']; } } src/Illuminate/Translation/CreatesPotentiallyTranslatedStrings.php 0000644 00000003150 15107327037 0021660 0 ustar 00 <?php namespace Illuminate\Translation; trait CreatesPotentiallyTranslatedStrings { /** * Create a pending potentially translated string. * * @param string $attribute * @param string|null $message * @return \Illuminate\Translation\PotentiallyTranslatedString */ protected function pendingPotentiallyTranslatedString($attribute, $message) { $destructor = $message === null ? fn ($message) => $this->messages[] = $message : fn ($message) => $this->messages[$attribute] = $message; return new class($message ?? $attribute, $this->validator->getTranslator(), $destructor) extends PotentiallyTranslatedString { /** * The callback to call when the object destructs. * * @var \Closure */ protected $destructor; /** * Create a new pending potentially translated string. * * @param string $message * @param \Illuminate\Contracts\Translation\Translator $translator * @param \Closure $destructor */ public function __construct($message, $translator, $destructor) { parent::__construct($message, $translator); $this->destructor = $destructor; } /** * Handle the object's destruction. * * @return void */ public function __destruct() { ($this->destructor)($this->toString()); } }; } } src/Illuminate/Translation/Translator.php 0000644 00000031754 15107327037 0014615 0 ustar 00 <?php namespace Illuminate\Translation; use Closure; use Illuminate\Contracts\Translation\Loader; use Illuminate\Contracts\Translation\Translator as TranslatorContract; use Illuminate\Support\Arr; use Illuminate\Support\NamespacedItemResolver; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use Illuminate\Support\Traits\ReflectsClosures; use InvalidArgumentException; class Translator extends NamespacedItemResolver implements TranslatorContract { use Macroable, ReflectsClosures; /** * The loader implementation. * * @var \Illuminate\Contracts\Translation\Loader */ protected $loader; /** * The default locale being used by the translator. * * @var string */ protected $locale; /** * The fallback locale used by the translator. * * @var string */ protected $fallback; /** * The array of loaded translation groups. * * @var array */ protected $loaded = []; /** * The message selector. * * @var \Illuminate\Translation\MessageSelector */ protected $selector; /** * The callable that should be invoked to determine applicable locales. * * @var callable */ protected $determineLocalesUsing; /** * The custom rendering callbacks for stringable objects. * * @var array */ protected $stringableHandlers = []; /** * Create a new translator instance. * * @param \Illuminate\Contracts\Translation\Loader $loader * @param string $locale * @return void */ public function __construct(Loader $loader, $locale) { $this->loader = $loader; $this->setLocale($locale); } /** * Determine if a translation exists for a given locale. * * @param string $key * @param string|null $locale * @return bool */ public function hasForLocale($key, $locale = null) { return $this->has($key, $locale, false); } /** * Determine if a translation exists. * * @param string $key * @param string|null $locale * @param bool $fallback * @return bool */ public function has($key, $locale = null, $fallback = true) { $locale = $locale ?: $this->locale; $line = $this->get($key, [], $locale, $fallback); // For JSON translations, the loaded files will contain the correct line. // Otherwise, we must assume we are handling typical translation file // and check if the returned line is not the same as the given key. if (! is_null($this->loaded['*']['*'][$locale][$key] ?? null)) { return true; } return $line !== $key; } /** * Get the translation for the given key. * * @param string $key * @param array $replace * @param string|null $locale * @param bool $fallback * @return string|array */ public function get($key, array $replace = [], $locale = null, $fallback = true) { $locale = $locale ?: $this->locale; // For JSON translations, there is only one file per locale, so we will simply load // that file and then we will be ready to check the array for the key. These are // only one level deep so we do not need to do any fancy searching through it. $this->load('*', '*', $locale); $line = $this->loaded['*']['*'][$locale][$key] ?? null; // If we can't find a translation for the JSON key, we will attempt to translate it // using the typical translation file. This way developers can always just use a // helper such as __ instead of having to pick between trans or __ with views. if (! isset($line)) { [$namespace, $group, $item] = $this->parseKey($key); // Here we will get the locale that should be used for the language line. If one // was not passed, we will use the default locales which was given to us when // the translator was instantiated. Then, we can load the lines and return. $locales = $fallback ? $this->localeArray($locale) : [$locale]; foreach ($locales as $locale) { if (! is_null($line = $this->getLine( $namespace, $group, $locale, $item, $replace ))) { return $line; } } } // If the line doesn't exist, we will return back the key which was requested as // that will be quick to spot in the UI if language keys are wrong or missing // from the application's language files. Otherwise we can return the line. return $this->makeReplacements($line ?: $key, $replace); } /** * Get a translation according to an integer value. * * @param string $key * @param \Countable|int|array $number * @param array $replace * @param string|null $locale * @return string */ public function choice($key, $number, array $replace = [], $locale = null) { $line = $this->get( $key, $replace, $locale = $this->localeForChoice($locale) ); // If the given "number" is actually an array or countable we will simply count the // number of elements in an instance. This allows developers to pass an array of // items without having to count it on their end first which gives bad syntax. if (is_countable($number)) { $number = count($number); } $replace['count'] = $number; return $this->makeReplacements( $this->getSelector()->choose($line, $number, $locale), $replace ); } /** * Get the proper locale for a choice operation. * * @param string|null $locale * @return string */ protected function localeForChoice($locale) { return $locale ?: $this->locale ?: $this->fallback; } /** * Retrieve a language line out the loaded array. * * @param string $namespace * @param string $group * @param string $locale * @param string $item * @param array $replace * @return string|array|null */ protected function getLine($namespace, $group, $locale, $item, array $replace) { $this->load($namespace, $group, $locale); $line = Arr::get($this->loaded[$namespace][$group][$locale], $item); if (is_string($line)) { return $this->makeReplacements($line, $replace); } elseif (is_array($line) && count($line) > 0) { array_walk_recursive($line, function (&$value, $key) use ($replace) { $value = $this->makeReplacements($value, $replace); }); return $line; } } /** * Make the place-holder replacements on a line. * * @param string $line * @param array $replace * @return string */ protected function makeReplacements($line, array $replace) { if (empty($replace)) { return $line; } $shouldReplace = []; foreach ($replace as $key => $value) { if (is_object($value) && isset($this->stringableHandlers[get_class($value)])) { $value = call_user_func($this->stringableHandlers[get_class($value)], $value); } $shouldReplace[':'.Str::ucfirst($key ?? '')] = Str::ucfirst($value ?? ''); $shouldReplace[':'.Str::upper($key ?? '')] = Str::upper($value ?? ''); $shouldReplace[':'.$key] = $value; } return strtr($line, $shouldReplace); } /** * Add translation lines to the given locale. * * @param array $lines * @param string $locale * @param string $namespace * @return void */ public function addLines(array $lines, $locale, $namespace = '*') { foreach ($lines as $key => $value) { [$group, $item] = explode('.', $key, 2); Arr::set($this->loaded, "$namespace.$group.$locale.$item", $value); } } /** * Load the specified language group. * * @param string $namespace * @param string $group * @param string $locale * @return void */ public function load($namespace, $group, $locale) { if ($this->isLoaded($namespace, $group, $locale)) { return; } // The loader is responsible for returning the array of language lines for the // given namespace, group, and locale. We'll set the lines in this array of // lines that have already been loaded so that we can easily access them. $lines = $this->loader->load($locale, $group, $namespace); $this->loaded[$namespace][$group][$locale] = $lines; } /** * Determine if the given group has been loaded. * * @param string $namespace * @param string $group * @param string $locale * @return bool */ protected function isLoaded($namespace, $group, $locale) { return isset($this->loaded[$namespace][$group][$locale]); } /** * Add a new namespace to the loader. * * @param string $namespace * @param string $hint * @return void */ public function addNamespace($namespace, $hint) { $this->loader->addNamespace($namespace, $hint); } /** * Add a new JSON path to the loader. * * @param string $path * @return void */ public function addJsonPath($path) { $this->loader->addJsonPath($path); } /** * Parse a key into namespace, group, and item. * * @param string $key * @return array */ public function parseKey($key) { $segments = parent::parseKey($key); if (is_null($segments[0])) { $segments[0] = '*'; } return $segments; } /** * Get the array of locales to be checked. * * @param string|null $locale * @return array */ protected function localeArray($locale) { $locales = array_filter([$locale ?: $this->locale, $this->fallback]); return call_user_func($this->determineLocalesUsing ?: fn () => $locales, $locales); } /** * Specify a callback that should be invoked to determined the applicable locale array. * * @param callable $callback * @return void */ public function determineLocalesUsing($callback) { $this->determineLocalesUsing = $callback; } /** * Get the message selector instance. * * @return \Illuminate\Translation\MessageSelector */ public function getSelector() { if (! isset($this->selector)) { $this->selector = new MessageSelector; } return $this->selector; } /** * Set the message selector instance. * * @param \Illuminate\Translation\MessageSelector $selector * @return void */ public function setSelector(MessageSelector $selector) { $this->selector = $selector; } /** * Get the language line loader implementation. * * @return \Illuminate\Contracts\Translation\Loader */ public function getLoader() { return $this->loader; } /** * Get the default locale being used. * * @return string */ public function locale() { return $this->getLocale(); } /** * Get the default locale being used. * * @return string */ public function getLocale() { return $this->locale; } /** * Set the default locale. * * @param string $locale * @return void * * @throws \InvalidArgumentException */ public function setLocale($locale) { if (Str::contains($locale, ['/', '\\'])) { throw new InvalidArgumentException('Invalid characters present in locale.'); } $this->locale = $locale; } /** * Get the fallback locale being used. * * @return string */ public function getFallback() { return $this->fallback; } /** * Set the fallback locale being used. * * @param string $fallback * @return void */ public function setFallback($fallback) { $this->fallback = $fallback; } /** * Set the loaded translation groups. * * @param array $loaded * @return void */ public function setLoaded(array $loaded) { $this->loaded = $loaded; } /** * Add a handler to be executed in order to format a given class to a string during translation replacements. * * @param callable|string $class * @param callable|null $handler * @return void */ public function stringable($class, $handler = null) { if ($class instanceof Closure) { [$class, $handler] = [ $this->firstClosureParameterType($class), $class, ]; } $this->stringableHandlers[$class] = $handler; } } src/Illuminate/Translation/PotentiallyTranslatedString.php 0000644 00000004021 15107327037 0020164 0 ustar 00 <?php namespace Illuminate\Translation; use Stringable; class PotentiallyTranslatedString implements Stringable { /** * The string that may be translated. * * @var string */ protected $string; /** * The translated string. * * @var string|null */ protected $translation; /** * The validator that may perform the translation. * * @var \Illuminate\Contracts\Translation\Translator */ protected $translator; /** * Create a new potentially translated string. * * @param string $string * @param \Illuminate\Contracts\Translation\Translator $translator */ public function __construct($string, $translator) { $this->string = $string; $this->translator = $translator; } /** * Translate the string. * * @param array $replace * @param string|null $locale * @return $this */ public function translate($replace = [], $locale = null) { $this->translation = $this->translator->get($this->string, $replace, $locale); return $this; } /** * Translates the string based on a count. * * @param \Countable|int|array $number * @param array $replace * @param string|null $locale * @return $this */ public function translateChoice($number, array $replace = [], $locale = null) { $this->translation = $this->translator->choice($this->string, $number, $replace, $locale); return $this; } /** * Get the original string. * * @return string */ public function original() { return $this->string; } /** * Get the potentially translated string. * * @return string */ public function __toString() { return $this->translation ?? $this->string; } /** * Get the potentially translated string. * * @return string */ public function toString() { return (string) $this; } } src/Illuminate/Conditionable/HigherOrderWhenProxy.php 0000644 00000004311 15107327037 0017013 0 ustar 00 <?php namespace Illuminate\Support; class HigherOrderWhenProxy { /** * The target being conditionally operated on. * * @var mixed */ protected $target; /** * The condition for proxying. * * @var bool */ protected $condition; /** * Indicates whether the proxy has a condition. * * @var bool */ protected $hasCondition = false; /** * Determine whether the condition should be negated. * * @var bool */ protected $negateConditionOnCapture; /** * Create a new proxy instance. * * @param mixed $target * @return void */ public function __construct($target) { $this->target = $target; } /** * Set the condition on the proxy. * * @param bool $condition * @return $this */ public function condition($condition) { [$this->condition, $this->hasCondition] = [$condition, true]; return $this; } /** * Indicate that the condition should be negated. * * @return $this */ public function negateConditionOnCapture() { $this->negateConditionOnCapture = true; return $this; } /** * Proxy accessing an attribute onto the target. * * @param string $key * @return mixed */ public function __get($key) { if (! $this->hasCondition) { $condition = $this->target->{$key}; return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition); } return $this->condition ? $this->target->{$key} : $this->target; } /** * Proxy a method call on the target. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (! $this->hasCondition) { $condition = $this->target->{$method}(...$parameters); return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition); } return $this->condition ? $this->target->{$method}(...$parameters) : $this->target; } } src/Illuminate/Conditionable/composer.json 0000644 00000001363 15107327037 0014742 0 ustar 00 { "name": "illuminate/conditionable", "description": "The Illuminate Conditionable package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.0.2" }, "autoload": { "psr-4": { "Illuminate\\Support\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Conditionable/Traits/Conditionable.php 0000644 00000004314 15107327037 0016750 0 ustar 00 <?php namespace Illuminate\Support\Traits; use Closure; use Illuminate\Support\HigherOrderWhenProxy; trait Conditionable { /** * Apply the callback if the given "value" is (or resolves to) truthy. * * @template TWhenParameter * @template TWhenReturnType * * @param (\Closure($this): TWhenParameter)|TWhenParameter|null $value * @param (callable($this, TWhenParameter): TWhenReturnType)|null $callback * @param (callable($this, TWhenParameter): TWhenReturnType)|null $default * @return $this|TWhenReturnType */ public function when($value = null, callable $callback = null, callable $default = null) { $value = $value instanceof Closure ? $value($this) : $value; if (func_num_args() === 0) { return new HigherOrderWhenProxy($this); } if (func_num_args() === 1) { return (new HigherOrderWhenProxy($this))->condition($value); } if ($value) { return $callback($this, $value) ?? $this; } elseif ($default) { return $default($this, $value) ?? $this; } return $this; } /** * Apply the callback if the given "value" is (or resolves to) falsy. * * @template TUnlessParameter * @template TUnlessReturnType * * @param (\Closure($this): TUnlessParameter)|TUnlessParameter|null $value * @param (callable($this, TUnlessParameter): TUnlessReturnType)|null $callback * @param (callable($this, TUnlessParameter): TUnlessReturnType)|null $default * @return $this|TUnlessReturnType */ public function unless($value = null, callable $callback = null, callable $default = null) { $value = $value instanceof Closure ? $value($this) : $value; if (func_num_args() === 0) { return (new HigherOrderWhenProxy($this))->negateConditionOnCapture(); } if (func_num_args() === 1) { return (new HigherOrderWhenProxy($this))->condition(! $value); } if (! $value) { return $callback($this, $value) ?? $this; } elseif ($default) { return $default($this, $value) ?? $this; } return $this; } } src/Illuminate/Conditionable/LICENSE.md 0000644 00000002063 15107327037 0013622 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Database/MultipleColumnsSelectedException.php 0000644 00000000211 15107327037 0020336 0 ustar 00 <?php namespace Illuminate\Database; use RuntimeException; class MultipleColumnsSelectedException extends RuntimeException { // } src/Illuminate/Database/ConnectionResolverInterface.php 0000644 00000001077 15107327037 0017327 0 ustar 00 <?php namespace Illuminate\Database; interface ConnectionResolverInterface { /** * Get a database connection instance. * * @param string|null $name * @return \Illuminate\Database\ConnectionInterface */ public function connection($name = null); /** * Get the default connection name. * * @return string */ public function getDefaultConnection(); /** * Set the default connection name. * * @param string $name * @return void */ public function setDefaultConnection($name); } src/Illuminate/Database/LostConnectionException.php 0000644 00000000174 15107327037 0016502 0 ustar 00 <?php namespace Illuminate\Database; use LogicException; class LostConnectionException extends LogicException { // } src/Illuminate/Database/Console/WipeCommand.php 0000644 00000005323 15107327037 0015470 0 ustar 00 <?php namespace Illuminate\Database\Console; use Illuminate\Console\Command; use Illuminate\Console\ConfirmableTrait; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'db:wipe')] class WipeCommand extends Command { use ConfirmableTrait; /** * The console command name. * * @var string */ protected $name = 'db:wipe'; /** * The console command description. * * @var string */ protected $description = 'Drop all tables, views, and types'; /** * Execute the console command. * * @return int */ public function handle() { if (! $this->confirmToProceed()) { return 1; } $database = $this->input->getOption('database'); if ($this->option('drop-views')) { $this->dropAllViews($database); $this->components->info('Dropped all views successfully.'); } $this->dropAllTables($database); $this->components->info('Dropped all tables successfully.'); if ($this->option('drop-types')) { $this->dropAllTypes($database); $this->components->info('Dropped all types successfully.'); } return 0; } /** * Drop all of the database tables. * * @param string $database * @return void */ protected function dropAllTables($database) { $this->laravel['db']->connection($database) ->getSchemaBuilder() ->dropAllTables(); } /** * Drop all of the database views. * * @param string $database * @return void */ protected function dropAllViews($database) { $this->laravel['db']->connection($database) ->getSchemaBuilder() ->dropAllViews(); } /** * Drop all of the database types. * * @param string $database * @return void */ protected function dropAllTypes($database) { $this->laravel['db']->connection($database) ->getSchemaBuilder() ->dropAllTypes(); } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'], ['drop-views', null, InputOption::VALUE_NONE, 'Drop all tables and views'], ['drop-types', null, InputOption::VALUE_NONE, 'Drop all tables and types (Postgres only)'], ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'], ]; } } src/Illuminate/Database/Console/DbCommand.php 0000644 00000015026 15107327037 0015112 0 ustar 00 <?php namespace Illuminate\Database\Console; use Illuminate\Console\Command; use Illuminate\Support\ConfigurationUrlParser; use Symfony\Component\Process\Process; use UnexpectedValueException; class DbCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'db {connection? : The database connection that should be used} {--read : Connect to the read connection} {--write : Connect to the write connection}'; /** * The console command description. * * @var string */ protected $description = 'Start a new database CLI session'; /** * Execute the console command. * * @return int */ public function handle() { $connection = $this->getConnection(); if (! isset($connection['host']) && $connection['driver'] !== 'sqlite') { $this->components->error('No host specified for this database connection.'); $this->line(' Use the <options=bold>[--read]</> and <options=bold>[--write]</> options to specify a read or write connection.'); $this->newLine(); return Command::FAILURE; } (new Process( array_merge([$this->getCommand($connection)], $this->commandArguments($connection)), null, $this->commandEnvironment($connection) ))->setTimeout(null)->setTty(true)->mustRun(function ($type, $buffer) { $this->output->write($buffer); }); return 0; } /** * Get the database connection configuration. * * @return array * * @throws \UnexpectedValueException */ public function getConnection() { $connection = $this->laravel['config']['database.connections.'. (($db = $this->argument('connection')) ?? $this->laravel['config']['database.default']) ]; if (empty($connection)) { throw new UnexpectedValueException("Invalid database connection [{$db}]."); } if (! empty($connection['url'])) { $connection = (new ConfigurationUrlParser)->parseConfiguration($connection); } if ($this->option('read')) { if (is_array($connection['read']['host'])) { $connection['read']['host'] = $connection['read']['host'][0]; } $connection = array_merge($connection, $connection['read']); } elseif ($this->option('write')) { if (is_array($connection['write']['host'])) { $connection['write']['host'] = $connection['write']['host'][0]; } $connection = array_merge($connection, $connection['write']); } return $connection; } /** * Get the arguments for the database client command. * * @param array $connection * @return array */ public function commandArguments(array $connection) { $driver = ucfirst($connection['driver']); return $this->{"get{$driver}Arguments"}($connection); } /** * Get the environment variables for the database client command. * * @param array $connection * @return array|null */ public function commandEnvironment(array $connection) { $driver = ucfirst($connection['driver']); if (method_exists($this, "get{$driver}Environment")) { return $this->{"get{$driver}Environment"}($connection); } return null; } /** * Get the database client command to run. * * @param array $connection * @return string */ public function getCommand(array $connection) { return [ 'mysql' => 'mysql', 'pgsql' => 'psql', 'sqlite' => 'sqlite3', 'sqlsrv' => 'sqlcmd', ][$connection['driver']]; } /** * Get the arguments for the MySQL CLI. * * @param array $connection * @return array */ protected function getMysqlArguments(array $connection) { return array_merge([ '--host='.$connection['host'], '--port='.$connection['port'], '--user='.$connection['username'], ], $this->getOptionalArguments([ 'password' => '--password='.$connection['password'], 'unix_socket' => '--socket='.($connection['unix_socket'] ?? ''), 'charset' => '--default-character-set='.($connection['charset'] ?? ''), ], $connection), [$connection['database']]); } /** * Get the arguments for the Postgres CLI. * * @param array $connection * @return array */ protected function getPgsqlArguments(array $connection) { return [$connection['database']]; } /** * Get the arguments for the SQLite CLI. * * @param array $connection * @return array */ protected function getSqliteArguments(array $connection) { return [$connection['database']]; } /** * Get the arguments for the SQL Server CLI. * * @param array $connection * @return array */ protected function getSqlsrvArguments(array $connection) { return array_merge(...$this->getOptionalArguments([ 'database' => ['-d', $connection['database']], 'username' => ['-U', $connection['username']], 'password' => ['-P', $connection['password']], 'host' => ['-S', 'tcp:'.$connection['host'] .($connection['port'] ? ','.$connection['port'] : ''), ], ], $connection)); } /** * Get the environment variables for the Postgres CLI. * * @param array $connection * @return array|null */ protected function getPgsqlEnvironment(array $connection) { return array_merge(...$this->getOptionalArguments([ 'username' => ['PGUSER' => $connection['username']], 'host' => ['PGHOST' => $connection['host']], 'port' => ['PGPORT' => $connection['port']], 'password' => ['PGPASSWORD' => $connection['password']], ], $connection)); } /** * Get the optional arguments based on the connection configuration. * * @param array $args * @param array $connection * @return array */ protected function getOptionalArguments(array $args, array $connection) { return array_values(array_filter($args, function ($key) use ($connection) { return ! empty($connection[$key]); }, ARRAY_FILTER_USE_KEY)); } } src/Illuminate/Database/Console/PruneCommand.php 0000644 00000013045 15107327037 0015655 0 ustar 00 <?php namespace Illuminate\Database\Console; use Illuminate\Console\Command; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Eloquent\MassPrunable; use Illuminate\Database\Eloquent\Prunable; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Events\ModelPruningFinished; use Illuminate\Database\Events\ModelPruningStarting; use Illuminate\Database\Events\ModelsPruned; use Illuminate\Support\Str; use InvalidArgumentException; use Symfony\Component\Finder\Finder; class PruneCommand extends Command { /** * The console command name. * * @var string */ protected $signature = 'model:prune {--model=* : Class names of the models to be pruned} {--except=* : Class names of the models to be excluded from pruning} {--chunk=1000 : The number of models to retrieve per chunk of models to be deleted} {--pretend : Display the number of prunable records found instead of deleting them}'; /** * The console command description. * * @var string */ protected $description = 'Prune models that are no longer needed'; /** * Execute the console command. * * @param \Illuminate\Contracts\Events\Dispatcher $events * @return void */ public function handle(Dispatcher $events) { $models = $this->models(); if ($models->isEmpty()) { $this->components->info('No prunable models found.'); return; } if ($this->option('pretend')) { $models->each(function ($model) { $this->pretendToPrune($model); }); return; } $pruning = []; $events->listen(ModelsPruned::class, function ($event) use (&$pruning) { if (! in_array($event->model, $pruning)) { $pruning[] = $event->model; $this->newLine(); $this->components->info(sprintf('Pruning [%s] records.', $event->model)); } $this->components->twoColumnDetail($event->model, "{$event->count} records"); }); $events->dispatch(new ModelPruningStarting($models->all())); $models->each(function ($model) { $this->pruneModel($model); }); $events->dispatch(new ModelPruningFinished($models->all())); $events->forget(ModelsPruned::class); } /** * Prune the given model. * * @param string $model * @return void */ protected function pruneModel(string $model) { $instance = new $model; $chunkSize = property_exists($instance, 'prunableChunkSize') ? $instance->prunableChunkSize : $this->option('chunk'); $total = $this->isPrunable($model) ? $instance->pruneAll($chunkSize) : 0; if ($total == 0) { $this->components->info("No prunable [$model] records found."); } } /** * Determine the models that should be pruned. * * @return \Illuminate\Support\Collection */ protected function models() { if (! empty($models = $this->option('model'))) { return collect($models)->filter(function ($model) { return class_exists($model); })->values(); } $except = $this->option('except'); if (! empty($models) && ! empty($except)) { throw new InvalidArgumentException('The --models and --except options cannot be combined.'); } return collect((new Finder)->in($this->getDefaultPath())->files()->name('*.php')) ->map(function ($model) { $namespace = $this->laravel->getNamespace(); return $namespace.str_replace( ['/', '.php'], ['\\', ''], Str::after($model->getRealPath(), realpath(app_path()).DIRECTORY_SEPARATOR) ); })->when(! empty($except), function ($models) use ($except) { return $models->reject(function ($model) use ($except) { return in_array($model, $except); }); })->filter(function ($model) { return class_exists($model); })->filter(function ($model) { return $this->isPrunable($model); })->values(); } /** * Get the default path where models are located. * * @return string|string[] */ protected function getDefaultPath() { return app_path('Models'); } /** * Determine if the given model class is prunable. * * @param string $model * @return bool */ protected function isPrunable($model) { $uses = class_uses_recursive($model); return in_array(Prunable::class, $uses) || in_array(MassPrunable::class, $uses); } /** * Display how many models will be pruned. * * @param string $model * @return void */ protected function pretendToPrune($model) { $instance = new $model; $count = $instance->prunable() ->when(in_array(SoftDeletes::class, class_uses_recursive(get_class($instance))), function ($query) { $query->withTrashed(); })->count(); if ($count === 0) { $this->components->info("No prunable [$model] records found."); } else { $this->components->info("{$count} [{$model}] records will be pruned."); } } } src/Illuminate/Database/Console/TableCommand.php 0000644 00000017620 15107327037 0015616 0 ustar 00 <?php namespace Illuminate\Database\Console; use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Table; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; use function Laravel\Prompts\select; #[AsCommand(name: 'db:table')] class TableCommand extends DatabaseInspectionCommand { /** * The name and signature of the console command. * * @var string */ protected $signature = 'db:table {table? : The name of the table} {--database= : The database connection} {--json : Output the table information as JSON}'; /** * The console command description. * * @var string */ protected $description = 'Display information about the given database table'; /** * Execute the console command. * * @return int */ public function handle(ConnectionResolverInterface $connections) { if (! $this->ensureDependenciesExist()) { return 1; } $connection = $connections->connection($this->input->getOption('database')); $schema = $connection->getDoctrineSchemaManager(); $this->registerTypeMappings($connection->getDoctrineConnection()->getDatabasePlatform()); $table = $this->argument('table') ?: select( 'Which table would you like to inspect?', collect($schema->listTables())->flatMap(fn (Table $table) => [$table->getName()])->toArray() ); if (! $schema->tablesExist([$table])) { return $this->components->warn("Table [{$table}] doesn't exist."); } $table = $schema->introspectTable($table); $columns = $this->columns($table); $indexes = $this->indexes($table); $foreignKeys = $this->foreignKeys($table); $data = [ 'table' => [ 'name' => $table->getName(), 'columns' => $columns->count(), 'size' => $this->getTableSize($connection, $table->getName()), ], 'columns' => $columns, 'indexes' => $indexes, 'foreign_keys' => $foreignKeys, ]; $this->display($data); return 0; } /** * Get the information regarding the table's columns. * * @param \Doctrine\DBAL\Schema\Table $table * @return \Illuminate\Support\Collection */ protected function columns(Table $table) { return collect($table->getColumns())->map(fn (Column $column) => [ 'column' => $column->getName(), 'attributes' => $this->getAttributesForColumn($column), 'default' => $column->getDefault(), 'type' => $column->getType()->getName(), ]); } /** * Get the attributes for a table column. * * @param \Doctrine\DBAL\Schema\Column $column * @return \Illuminate\Support\Collection */ protected function getAttributesForColumn(Column $column) { return collect([ $column->getAutoincrement() ? 'autoincrement' : null, 'type' => $column->getType()->getName(), $column->getUnsigned() ? 'unsigned' : null, ! $column->getNotNull() ? 'nullable' : null, ])->filter(); } /** * Get the information regarding the table's indexes. * * @param \Doctrine\DBAL\Schema\Table $table * @return \Illuminate\Support\Collection */ protected function indexes(Table $table) { return collect($table->getIndexes())->map(fn (Index $index) => [ 'name' => $index->getName(), 'columns' => collect($index->getColumns()), 'attributes' => $this->getAttributesForIndex($index), ]); } /** * Get the attributes for a table index. * * @param \Doctrine\DBAL\Schema\Index $index * @return \Illuminate\Support\Collection */ protected function getAttributesForIndex(Index $index) { return collect([ 'compound' => count($index->getColumns()) > 1, 'unique' => $index->isUnique(), 'primary' => $index->isPrimary(), ])->filter()->keys()->map(fn ($attribute) => Str::lower($attribute)); } /** * Get the information regarding the table's foreign keys. * * @param \Doctrine\DBAL\Schema\Table $table * @return \Illuminate\Support\Collection */ protected function foreignKeys(Table $table) { return collect($table->getForeignKeys())->map(fn (ForeignKeyConstraint $foreignKey) => [ 'name' => $foreignKey->getName(), 'local_table' => $table->getName(), 'local_columns' => collect($foreignKey->getLocalColumns()), 'foreign_table' => $foreignKey->getForeignTableName(), 'foreign_columns' => collect($foreignKey->getForeignColumns()), 'on_update' => Str::lower(rescue(fn () => $foreignKey->getOption('onUpdate'), 'N/A')), 'on_delete' => Str::lower(rescue(fn () => $foreignKey->getOption('onDelete'), 'N/A')), ]); } /** * Render the table information. * * @param array $data * @return void */ protected function display(array $data) { $this->option('json') ? $this->displayJson($data) : $this->displayForCli($data); } /** * Render the table information as JSON. * * @param array $data * @return void */ protected function displayJson(array $data) { $this->output->writeln(json_encode($data)); } /** * Render the table information formatted for the CLI. * * @param array $data * @return void */ protected function displayForCli(array $data) { [$table, $columns, $indexes, $foreignKeys] = [ $data['table'], $data['columns'], $data['indexes'], $data['foreign_keys'], ]; $this->newLine(); $this->components->twoColumnDetail('<fg=green;options=bold>'.$table['name'].'</>'); $this->components->twoColumnDetail('Columns', $table['columns']); if ($size = $table['size']) { $this->components->twoColumnDetail('Size', number_format($size / 1024 / 1024, 2).'MiB'); } $this->newLine(); if ($columns->isNotEmpty()) { $this->components->twoColumnDetail('<fg=green;options=bold>Column</>', 'Type'); $columns->each(function ($column) { $this->components->twoColumnDetail( $column['column'].' <fg=gray>'.$column['attributes']->implode(', ').'</>', ($column['default'] ? '<fg=gray>'.$column['default'].'</> ' : '').''.$column['type'].'' ); }); $this->newLine(); } if ($indexes->isNotEmpty()) { $this->components->twoColumnDetail('<fg=green;options=bold>Index</>'); $indexes->each(function ($index) { $this->components->twoColumnDetail( $index['name'].' <fg=gray>'.$index['columns']->implode(', ').'</>', $index['attributes']->implode(', ') ); }); $this->newLine(); } if ($foreignKeys->isNotEmpty()) { $this->components->twoColumnDetail('<fg=green;options=bold>Foreign Key</>', 'On Update / On Delete'); $foreignKeys->each(function ($foreignKey) { $this->components->twoColumnDetail( $foreignKey['name'].' <fg=gray;options=bold>'.$foreignKey['local_columns']->implode(', ').' references '.$foreignKey['foreign_columns']->implode(', ').' on '.$foreignKey['foreign_table'].'</>', $foreignKey['on_update'].' / '.$foreignKey['on_delete'], ); }); $this->newLine(); } } } src/Illuminate/Database/Console/DumpCommand.php 0000644 00000005364 15107327037 0015476 0 ustar 00 <?php namespace Illuminate\Database\Console; use Illuminate\Console\Command; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Connection; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\Events\SchemaDumped; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\Config; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'schema:dump')] class DumpCommand extends Command { /** * The console command name. * * @var string */ protected $signature = 'schema:dump {--database= : The database connection to use} {--path= : The path where the schema dump file should be stored} {--prune : Delete all existing migration files}'; /** * The console command description. * * @var string */ protected $description = 'Dump the given database schema'; /** * Execute the console command. * * @param \Illuminate\Database\ConnectionResolverInterface $connections * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher * @return void */ public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher) { $connection = $connections->connection($database = $this->input->getOption('database')); $this->schemaState($connection)->dump( $connection, $path = $this->path($connection) ); $dispatcher->dispatch(new SchemaDumped($connection, $path)); $info = 'Database schema dumped'; if ($this->option('prune')) { (new Filesystem)->deleteDirectory( database_path('migrations'), $preserve = false ); $info .= ' and pruned'; } $this->components->info($info.' successfully.'); } /** * Create a schema state instance for the given connection. * * @param \Illuminate\Database\Connection $connection * @return mixed */ protected function schemaState(Connection $connection) { return $connection->getSchemaState() ->withMigrationTable($connection->getTablePrefix().Config::get('database.migrations', 'migrations')) ->handleOutputUsing(function ($type, $buffer) { $this->output->write($buffer); }); } /** * Get the path that the dump should be written to. * * @param \Illuminate\Database\Connection $connection */ protected function path(Connection $connection) { return tap($this->option('path') ?: database_path('schema/'.$connection->getName().'-schema.sql'), function ($path) { (new Filesystem)->ensureDirectoryExists(dirname($path)); }); } } src/Illuminate/Database/Console/DatabaseInspectionCommand.php 0000644 00000017343 15107327037 0020331 0 ustar 00 <?php namespace Illuminate\Database\Console; use Doctrine\DBAL\Platforms\AbstractPlatform; use Illuminate\Console\Command; use Illuminate\Database\ConnectionInterface; use Illuminate\Database\MySqlConnection; use Illuminate\Database\PostgresConnection; use Illuminate\Database\QueryException; use Illuminate\Database\SQLiteConnection; use Illuminate\Database\SqlServerConnection; use Illuminate\Support\Arr; use Illuminate\Support\Composer; use Symfony\Component\Process\Exception\ProcessSignaledException; use Symfony\Component\Process\Exception\RuntimeException; use Symfony\Component\Process\Process; use function Laravel\Prompts\confirm; abstract class DatabaseInspectionCommand extends Command { /** * A map of database column types. * * @var array */ protected $typeMappings = [ 'bit' => 'string', 'citext' => 'string', 'enum' => 'string', 'geometry' => 'string', 'geomcollection' => 'string', 'linestring' => 'string', 'ltree' => 'string', 'multilinestring' => 'string', 'multipoint' => 'string', 'multipolygon' => 'string', 'point' => 'string', 'polygon' => 'string', 'sysname' => 'string', ]; /** * The Composer instance. * * @var \Illuminate\Support\Composer */ protected $composer; /** * Create a new command instance. * * @param \Illuminate\Support\Composer|null $composer * @return void */ public function __construct(Composer $composer = null) { parent::__construct(); $this->composer = $composer ?? $this->laravel->make(Composer::class); } /** * Register the custom Doctrine type mappings for inspection commands. * * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform * @return void */ protected function registerTypeMappings(AbstractPlatform $platform) { foreach ($this->typeMappings as $type => $value) { $platform->registerDoctrineTypeMapping($type, $value); } } /** * Get a human-readable platform name for the given platform. * * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform * @param string $database * @return string */ protected function getPlatformName(AbstractPlatform $platform, $database) { return match (class_basename($platform)) { 'MySQLPlatform' => 'MySQL <= 5', 'MySQL57Platform' => 'MySQL 5.7', 'MySQL80Platform' => 'MySQL 8', 'PostgreSQL100Platform', 'PostgreSQLPlatform' => 'Postgres', 'SqlitePlatform' => 'SQLite', 'SQLServerPlatform' => 'SQL Server', 'SQLServer2012Platform' => 'SQL Server 2012', default => $database, }; } /** * Get the size of a table in bytes. * * @param \Illuminate\Database\ConnectionInterface $connection * @param string $table * @return int|null */ protected function getTableSize(ConnectionInterface $connection, string $table) { return match (true) { $connection instanceof MySqlConnection => $this->getMySQLTableSize($connection, $table), $connection instanceof PostgresConnection => $this->getPostgresTableSize($connection, $table), $connection instanceof SQLiteConnection => $this->getSqliteTableSize($connection, $table), default => null, }; } /** * Get the size of a MySQL table in bytes. * * @param \Illuminate\Database\ConnectionInterface $connection * @param string $table * @return mixed */ protected function getMySQLTableSize(ConnectionInterface $connection, string $table) { $result = $connection->selectOne('SELECT (data_length + index_length) AS size FROM information_schema.TABLES WHERE table_schema = ? AND table_name = ?', [ $connection->getDatabaseName(), $table, ]); return Arr::wrap((array) $result)['size']; } /** * Get the size of a Postgres table in bytes. * * @param \Illuminate\Database\ConnectionInterface $connection * @param string $table * @return mixed */ protected function getPostgresTableSize(ConnectionInterface $connection, string $table) { $result = $connection->selectOne('SELECT pg_total_relation_size(?) AS size;', [ $table, ]); return Arr::wrap((array) $result)['size']; } /** * Get the size of a SQLite table in bytes. * * @param \Illuminate\Database\ConnectionInterface $connection * @param string $table * @return mixed */ protected function getSqliteTableSize(ConnectionInterface $connection, string $table) { try { $result = $connection->selectOne('SELECT SUM(pgsize) AS size FROM dbstat WHERE name=?', [ $table, ]); return Arr::wrap((array) $result)['size']; } catch (QueryException) { return null; } } /** * Get the number of open connections for a database. * * @param \Illuminate\Database\ConnectionInterface $connection * @return int|null */ protected function getConnectionCount(ConnectionInterface $connection) { $result = match (true) { $connection instanceof MySqlConnection => $connection->selectOne('show status where variable_name = "threads_connected"'), $connection instanceof PostgresConnection => $connection->selectOne('select count(*) AS "Value" from pg_stat_activity'), $connection instanceof SqlServerConnection => $connection->selectOne('SELECT COUNT(*) Value FROM sys.dm_exec_sessions WHERE status = ?', ['running']), default => null, }; if (! $result) { return null; } return Arr::wrap((array) $result)['Value']; } /** * Get the connection configuration details for the given connection. * * @param string $database * @return array */ protected function getConfigFromDatabase($database) { $database ??= config('database.default'); return Arr::except(config('database.connections.'.$database), ['password']); } /** * Ensure the dependencies for the database commands are available. * * @return bool */ protected function ensureDependenciesExist() { return tap(interface_exists('Doctrine\DBAL\Driver'), function ($dependenciesExist) { if (! $dependenciesExist && confirm('Inspecting database information requires the Doctrine DBAL (doctrine/dbal) package. Would you like to install it?', default: false)) { $this->installDependencies(); } }); } /** * Install the command's dependencies. * * @return void * * @throws \Symfony\Component\Process\Exception\ProcessSignaledException */ protected function installDependencies() { $command = collect($this->composer->findComposer()) ->push('require doctrine/dbal:^3.5.1') ->implode(' '); $process = Process::fromShellCommandline($command, null, null, null, null); if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) { try { $process->setTty(true); } catch (RuntimeException $e) { $this->components->warn($e->getMessage()); } } try { $process->run(fn ($type, $line) => $this->output->write($line)); } catch (ProcessSignaledException $e) { if (extension_loaded('pcntl') && $e->getSignal() !== SIGINT) { throw $e; } } } } src/Illuminate/Database/Console/ShowModelCommand.php 0000644 00000041516 15107327037 0016471 0 ustar 00 <?php namespace Illuminate\Database\Console; use BackedEnum; use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Types\DecimalType; use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Str; use ReflectionClass; use ReflectionMethod; use SplFileObject; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Output\OutputInterface; use UnitEnum; #[AsCommand(name: 'model:show')] class ShowModelCommand extends DatabaseInspectionCommand { /** * The console command name. * * @var string */ protected $name = 'model:show {model}'; /** * The console command description. * * @var string */ protected $description = 'Show information about an Eloquent model'; /** * The console command signature. * * @var string */ protected $signature = 'model:show {model : The model to show} {--database= : The database connection to use} {--json : Output the model as JSON}'; /** * The methods that can be called in a model to indicate a relation. * * @var array */ protected $relationMethods = [ 'hasMany', 'hasManyThrough', 'hasOneThrough', 'belongsToMany', 'hasOne', 'belongsTo', 'morphOne', 'morphTo', 'morphMany', 'morphToMany', 'morphedByMany', ]; /** * Execute the console command. * * @return int */ public function handle() { if (! $this->ensureDependenciesExist()) { return 1; } $class = $this->qualifyModel($this->argument('model')); try { $model = $this->laravel->make($class); $class = get_class($model); } catch (BindingResolutionException $e) { return $this->components->error($e->getMessage()); } if ($this->option('database')) { $model->setConnection($this->option('database')); } $this->display( $class, $model->getConnection()->getName(), $model->getConnection()->getTablePrefix().$model->getTable(), $this->getPolicy($model), $this->getAttributes($model), $this->getRelations($model), $this->getObservers($model), ); } /** * Get the first policy associated with this model. * * @param \Illuminate\Database\Eloquent\Model $model * @return string */ protected function getPolicy($model) { $policy = Gate::getPolicyFor($model::class); return $policy ? $policy::class : null; } /** * Get the column attributes for the given model. * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Support\Collection */ protected function getAttributes($model) { $connection = $model->getConnection(); $schema = $connection->getDoctrineSchemaManager(); $this->registerTypeMappings($connection->getDoctrineConnection()->getDatabasePlatform()); $table = $model->getConnection()->getTablePrefix().$model->getTable(); $columns = $schema->listTableColumns($table); $indexes = $schema->listTableIndexes($table); return collect($columns) ->values() ->map(fn (Column $column) => [ 'name' => $column->getName(), 'type' => $this->getColumnType($column), 'increments' => $column->getAutoincrement(), 'nullable' => ! $column->getNotnull(), 'default' => $this->getColumnDefault($column, $model), 'unique' => $this->columnIsUnique($column->getName(), $indexes), 'fillable' => $model->isFillable($column->getName()), 'hidden' => $this->attributeIsHidden($column->getName(), $model), 'appended' => null, 'cast' => $this->getCastType($column->getName(), $model), ]) ->merge($this->getVirtualAttributes($model, $columns)); } /** * Get the virtual (non-column) attributes for the given model. * * @param \Illuminate\Database\Eloquent\Model $model * @param \Doctrine\DBAL\Schema\Column[] $columns * @return \Illuminate\Support\Collection */ protected function getVirtualAttributes($model, $columns) { $class = new ReflectionClass($model); return collect($class->getMethods()) ->reject( fn (ReflectionMethod $method) => $method->isStatic() || $method->isAbstract() || $method->getDeclaringClass()->getName() === Model::class ) ->mapWithKeys(function (ReflectionMethod $method) use ($model) { if (preg_match('/^get(.+)Attribute$/', $method->getName(), $matches) === 1) { return [Str::snake($matches[1]) => 'accessor']; } elseif ($model->hasAttributeMutator($method->getName())) { return [Str::snake($method->getName()) => 'attribute']; } else { return []; } }) ->reject(fn ($cast, $name) => collect($columns)->has($name)) ->map(fn ($cast, $name) => [ 'name' => $name, 'type' => null, 'increments' => false, 'nullable' => null, 'default' => null, 'unique' => null, 'fillable' => $model->isFillable($name), 'hidden' => $this->attributeIsHidden($name, $model), 'appended' => $model->hasAppended($name), 'cast' => $cast, ]) ->values(); } /** * Get the relations from the given model. * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Support\Collection */ protected function getRelations($model) { return collect(get_class_methods($model)) ->map(fn ($method) => new ReflectionMethod($model, $method)) ->reject( fn (ReflectionMethod $method) => $method->isStatic() || $method->isAbstract() || $method->getDeclaringClass()->getName() === Model::class ) ->filter(function (ReflectionMethod $method) { $file = new SplFileObject($method->getFileName()); $file->seek($method->getStartLine() - 1); $code = ''; while ($file->key() < $method->getEndLine()) { $code .= trim($file->current()); $file->next(); } return collect($this->relationMethods) ->contains(fn ($relationMethod) => str_contains($code, '$this->'.$relationMethod.'(')); }) ->map(function (ReflectionMethod $method) use ($model) { $relation = $method->invoke($model); if (! $relation instanceof Relation) { return null; } return [ 'name' => $method->getName(), 'type' => Str::afterLast(get_class($relation), '\\'), 'related' => get_class($relation->getRelated()), ]; }) ->filter() ->values(); } /** * Get the Observers watching this model. * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Support\Collection */ protected function getObservers($model) { $listeners = $this->getLaravel()->make('events')->getRawListeners(); // Get the Eloquent observers for this model... $listeners = array_filter($listeners, function ($v, $key) use ($model) { return Str::startsWith($key, 'eloquent.') && Str::endsWith($key, $model::class); }, ARRAY_FILTER_USE_BOTH); // Format listeners Eloquent verb => Observer methods... $extractVerb = function ($key) { preg_match('/eloquent.([a-zA-Z]+)\: /', $key, $matches); return $matches[1] ?? '?'; }; $formatted = []; foreach ($listeners as $key => $observerMethods) { $formatted[] = [ 'event' => $extractVerb($key), 'observer' => array_map(fn ($obs) => is_string($obs) ? $obs : 'Closure', $observerMethods), ]; } return collect($formatted); } /** * Render the model information. * * @param string $class * @param string $database * @param string $table * @param string $policy * @param \Illuminate\Support\Collection $attributes * @param \Illuminate\Support\Collection $relations * @param \Illuminate\Support\Collection $observers * @return void */ protected function display($class, $database, $table, $policy, $attributes, $relations, $observers) { $this->option('json') ? $this->displayJson($class, $database, $table, $policy, $attributes, $relations, $observers) : $this->displayCli($class, $database, $table, $policy, $attributes, $relations, $observers); } /** * Render the model information as JSON. * * @param string $class * @param string $database * @param string $table * @param string $policy * @param \Illuminate\Support\Collection $attributes * @param \Illuminate\Support\Collection $relations * @param \Illuminate\Support\Collection $observers * @return void */ protected function displayJson($class, $database, $table, $policy, $attributes, $relations, $observers) { $this->output->writeln( collect([ 'class' => $class, 'database' => $database, 'table' => $table, 'policy' => $policy, 'attributes' => $attributes, 'relations' => $relations, 'observers' => $observers, ])->toJson() ); } /** * Render the model information for the CLI. * * @param string $class * @param string $database * @param string $table * @param string $policy * @param \Illuminate\Support\Collection $attributes * @param \Illuminate\Support\Collection $relations * @param \Illuminate\Support\Collection $observers * @return void */ protected function displayCli($class, $database, $table, $policy, $attributes, $relations, $observers) { $this->newLine(); $this->components->twoColumnDetail('<fg=green;options=bold>'.$class.'</>'); $this->components->twoColumnDetail('Database', $database); $this->components->twoColumnDetail('Table', $table); if ($policy) { $this->components->twoColumnDetail('Policy', $policy); } $this->newLine(); $this->components->twoColumnDetail( '<fg=green;options=bold>Attributes</>', 'type <fg=gray>/</> <fg=yellow;options=bold>cast</>', ); foreach ($attributes as $attribute) { $first = trim(sprintf( '%s %s', $attribute['name'], collect(['increments', 'unique', 'nullable', 'fillable', 'hidden', 'appended']) ->filter(fn ($property) => $attribute[$property]) ->map(fn ($property) => sprintf('<fg=gray>%s</>', $property)) ->implode('<fg=gray>,</> ') )); $second = collect([ $attribute['type'], $attribute['cast'] ? '<fg=yellow;options=bold>'.$attribute['cast'].'</>' : null, ])->filter()->implode(' <fg=gray>/</> '); $this->components->twoColumnDetail($first, $second); if ($attribute['default'] !== null) { $this->components->bulletList( [sprintf('default: %s', $attribute['default'])], OutputInterface::VERBOSITY_VERBOSE ); } } $this->newLine(); $this->components->twoColumnDetail('<fg=green;options=bold>Relations</>'); foreach ($relations as $relation) { $this->components->twoColumnDetail( sprintf('%s <fg=gray>%s</>', $relation['name'], $relation['type']), $relation['related'] ); } $this->newLine(); $this->components->twoColumnDetail('<fg=green;options=bold>Observers</>'); if ($observers->count()) { foreach ($observers as $observer) { $this->components->twoColumnDetail( sprintf('%s', $observer['event']), implode(', ', $observer['observer']) ); } } $this->newLine(); } /** * Get the cast type for the given column. * * @param string $column * @param \Illuminate\Database\Eloquent\Model $model * @return string|null */ protected function getCastType($column, $model) { if ($model->hasGetMutator($column) || $model->hasSetMutator($column)) { return 'accessor'; } if ($model->hasAttributeMutator($column)) { return 'attribute'; } return $this->getCastsWithDates($model)->get($column) ?? null; } /** * Get the model casts, including any date casts. * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Support\Collection */ protected function getCastsWithDates($model) { return collect($model->getDates()) ->filter() ->flip() ->map(fn () => 'datetime') ->merge($model->getCasts()); } /** * Get the type of the given column. * * @param \Doctrine\DBAL\Schema\Column $column * @return string */ protected function getColumnType($column) { $name = $column->getType()->getName(); $unsigned = $column->getUnsigned() ? ' unsigned' : ''; $details = match (get_class($column->getType())) { DecimalType::class => $column->getPrecision().','.$column->getScale(), default => $column->getLength(), }; if ($details) { return sprintf('%s(%s)%s', $name, $details, $unsigned); } return sprintf('%s%s', $name, $unsigned); } /** * Get the default value for the given column. * * @param \Doctrine\DBAL\Schema\Column $column * @param \Illuminate\Database\Eloquent\Model $model * @return mixed|null */ protected function getColumnDefault($column, $model) { $attributeDefault = $model->getAttributes()[$column->getName()] ?? null; return match (true) { $attributeDefault instanceof BackedEnum => $attributeDefault->value, $attributeDefault instanceof UnitEnum => $attributeDefault->name, default => $attributeDefault ?? $column->getDefault(), }; } /** * Determine if the given attribute is hidden. * * @param string $attribute * @param \Illuminate\Database\Eloquent\Model $model * @return bool */ protected function attributeIsHidden($attribute, $model) { if (count($model->getHidden()) > 0) { return in_array($attribute, $model->getHidden()); } if (count($model->getVisible()) > 0) { return ! in_array($attribute, $model->getVisible()); } return false; } /** * Determine if the given attribute is unique. * * @param string $column * @param \Doctrine\DBAL\Schema\Index[] $indexes * @return bool */ protected function columnIsUnique($column, $indexes) { return collect($indexes) ->filter(fn (Index $index) => count($index->getColumns()) === 1 && $index->getColumns()[0] === $column) ->contains(fn (Index $index) => $index->isUnique()); } /** * Qualify the given model class base name. * * @param string $model * @return string * * @see \Illuminate\Console\GeneratorCommand */ protected function qualifyModel(string $model) { if (str_contains($model, '\\') && class_exists($model)) { return $model; } $model = ltrim($model, '\\/'); $model = str_replace('/', '\\', $model); $rootNamespace = $this->laravel->getNamespace(); if (Str::startsWith($model, $rootNamespace)) { return $model; } return is_dir(app_path('Models')) ? $rootNamespace.'Models\\'.$model : $rootNamespace.$model; } } src/Illuminate/Database/Console/Seeds/SeedCommand.php 0000644 00000006647 15107327037 0016521 0 ustar 00 <?php namespace Illuminate\Database\Console\Seeds; use Illuminate\Console\Command; use Illuminate\Console\ConfirmableTrait; use Illuminate\Database\ConnectionResolverInterface as Resolver; use Illuminate\Database\Eloquent\Model; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'db:seed')] class SeedCommand extends Command { use ConfirmableTrait; /** * The console command name. * * @var string */ protected $name = 'db:seed'; /** * The console command description. * * @var string */ protected $description = 'Seed the database with records'; /** * The connection resolver instance. * * @var \Illuminate\Database\ConnectionResolverInterface */ protected $resolver; /** * Create a new database seed command instance. * * @param \Illuminate\Database\ConnectionResolverInterface $resolver * @return void */ public function __construct(Resolver $resolver) { parent::__construct(); $this->resolver = $resolver; } /** * Execute the console command. * * @return int */ public function handle() { if (! $this->confirmToProceed()) { return 1; } $this->components->info('Seeding database.'); $previousConnection = $this->resolver->getDefaultConnection(); $this->resolver->setDefaultConnection($this->getDatabase()); Model::unguarded(function () { $this->getSeeder()->__invoke(); }); if ($previousConnection) { $this->resolver->setDefaultConnection($previousConnection); } return 0; } /** * Get a seeder instance from the container. * * @return \Illuminate\Database\Seeder */ protected function getSeeder() { $class = $this->input->getArgument('class') ?? $this->input->getOption('class'); if (! str_contains($class, '\\')) { $class = 'Database\\Seeders\\'.$class; } if ($class === 'Database\\Seeders\\DatabaseSeeder' && ! class_exists($class)) { $class = 'DatabaseSeeder'; } return $this->laravel->make($class) ->setContainer($this->laravel) ->setCommand($this); } /** * Get the name of the database connection to use. * * @return string */ protected function getDatabase() { $database = $this->input->getOption('database'); return $database ?: $this->laravel['config']['database.default']; } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return [ ['class', InputArgument::OPTIONAL, 'The class name of the root seeder', null], ]; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['class', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder', 'Database\\Seeders\\DatabaseSeeder'], ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to seed'], ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'], ]; } } src/Illuminate/Database/Console/Seeds/SeederMakeCommand.php 0000644 00000003642 15107327037 0017636 0 ustar 00 <?php namespace Illuminate\Database\Console\Seeds; use Illuminate\Console\GeneratorCommand; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'make:seeder')] class SeederMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:seeder'; /** * The console command description. * * @var string */ protected $description = 'Create a new seeder class'; /** * The type of class being generated. * * @var string */ protected $type = 'Seeder'; /** * Execute the console command. * * @return void */ public function handle() { parent::handle(); } /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->resolveStubPath('/stubs/seeder.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return is_file($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Get the destination class path. * * @param string $name * @return string */ protected function getPath($name) { $name = str_replace('\\', '/', Str::replaceFirst($this->rootNamespace(), '', $name)); if (is_dir($this->laravel->databasePath().'/seeds')) { return $this->laravel->databasePath().'/seeds/'.$name.'.php'; } return $this->laravel->databasePath().'/seeders/'.$name.'.php'; } /** * Get the root namespace for the class. * * @return string */ protected function rootNamespace() { return 'Database\Seeders\\'; } } src/Illuminate/Database/Console/Seeds/WithoutModelEvents.php 0000644 00000000626 15107327037 0020142 0 ustar 00 <?php namespace Illuminate\Database\Console\Seeds; use Illuminate\Database\Eloquent\Model; trait WithoutModelEvents { /** * Prevent model events from being dispatched by the given callback. * * @param callable $callback * @return callable */ public function withoutModelEvents(callable $callback) { return fn () => Model::withoutEvents($callback); } } src/Illuminate/Database/Console/Seeds/stubs/seeder.stub 0000644 00000000411 15107327037 0017116 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; class {{ class }} extends Seeder { /** * Run the database seeds. */ public function run(): void { // } } src/Illuminate/Database/Console/Migrations/BaseCommand.php 0000644 00000002652 15107327037 0017554 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Console\Command; class BaseCommand extends Command { /** * Get all of the migration paths. * * @return array */ protected function getMigrationPaths() { // Here, we will check to see if a path option has been defined. If it has we will // use the path relative to the root of the installation folder so our database // migrations may be run for any customized path from within the application. if ($this->input->hasOption('path') && $this->option('path')) { return collect($this->option('path'))->map(function ($path) { return ! $this->usingRealPath() ? $this->laravel->basePath().'/'.$path : $path; })->all(); } return array_merge( $this->migrator->paths(), [$this->getMigrationPath()] ); } /** * Determine if the given path(s) are pre-resolved "real" paths. * * @return bool */ protected function usingRealPath() { return $this->input->hasOption('realpath') && $this->option('realpath'); } /** * Get the path to the migration directory. * * @return string */ protected function getMigrationPath() { return $this->laravel->databasePath().DIRECTORY_SEPARATOR.'migrations'; } } src/Illuminate/Database/Console/Migrations/TableGuesser.php 0000644 00000001613 15107327037 0017764 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; class TableGuesser { const CREATE_PATTERNS = [ '/^create_(\w+)_table$/', '/^create_(\w+)$/', ]; const CHANGE_PATTERNS = [ '/.+_(to|from|in)_(\w+)_table$/', '/.+_(to|from|in)_(\w+)$/', ]; /** * Attempt to guess the table name and "creation" status of the given migration. * * @param string $migration * @return array */ public static function guess($migration) { foreach (self::CREATE_PATTERNS as $pattern) { if (preg_match($pattern, $migration, $matches)) { return [$matches[1], $create = true]; } } foreach (self::CHANGE_PATTERNS as $pattern) { if (preg_match($pattern, $migration, $matches)) { return [$matches[2], $create = false]; } } } } src/Illuminate/Database/Console/Migrations/FreshCommand.php 0000644 00000007314 15107327037 0017751 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Console\Command; use Illuminate\Console\ConfirmableTrait; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Events\DatabaseRefreshed; use Symfony\Component\Console\Input\InputOption; class FreshCommand extends Command { use ConfirmableTrait; /** * The console command name. * * @var string */ protected $name = 'migrate:fresh'; /** * The console command description. * * @var string */ protected $description = 'Drop all tables and re-run all migrations'; /** * Execute the console command. * * @return int */ public function handle() { if (! $this->confirmToProceed()) { return 1; } $database = $this->input->getOption('database'); $this->newLine(); $this->components->task('Dropping all tables', fn () => $this->callSilent('db:wipe', array_filter([ '--database' => $database, '--drop-views' => $this->option('drop-views'), '--drop-types' => $this->option('drop-types'), '--force' => true, ])) == 0); $this->newLine(); $this->call('migrate', array_filter([ '--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'), ])); if ($this->laravel->bound(Dispatcher::class)) { $this->laravel[Dispatcher::class]->dispatch( new DatabaseRefreshed($database, $this->needsSeeding()) ); } if ($this->needsSeeding()) { $this->runSeeder($database); } return 0; } /** * Determine if the developer has requested database seeding. * * @return bool */ protected function needsSeeding() { return $this->option('seed') || $this->option('seeder'); } /** * Run the database seeder command. * * @param string $database * @return void */ protected function runSeeder($database) { $this->call('db:seed', array_filter([ '--database' => $database, '--class' => $this->option('seeder') ?: 'Database\\Seeders\\DatabaseSeeder', '--force' => true, ])); } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'], ['drop-views', null, InputOption::VALUE_NONE, 'Drop all tables and views'], ['drop-types', null, InputOption::VALUE_NONE, 'Drop all tables and types (Postgres only)'], ['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'], ]; } } src/Illuminate/Database/Console/Migrations/InstallCommand.php 0000644 00000003064 15107327037 0020306 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Console\Command; use Illuminate\Database\Migrations\MigrationRepositoryInterface; use Symfony\Component\Console\Input\InputOption; class InstallCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'migrate:install'; /** * The console command description. * * @var string */ protected $description = 'Create the migration repository'; /** * The repository instance. * * @var \Illuminate\Database\Migrations\MigrationRepositoryInterface */ protected $repository; /** * Create a new migration install command instance. * * @param \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository * @return void */ public function __construct(MigrationRepositoryInterface $repository) { parent::__construct(); $this->repository = $repository; } /** * Execute the console command. * * @return void */ public function handle() { $this->repository->setSource($this->input->getOption('database')); $this->repository->createRepository(); $this->components->info('Migration table created successfully.'); } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'], ]; } } src/Illuminate/Database/Console/Migrations/ResetCommand.php 0000644 00000004764 15107327037 0017772 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Console\ConfirmableTrait; use Illuminate\Database\Migrations\Migrator; use Symfony\Component\Console\Input\InputOption; class ResetCommand extends BaseCommand { use ConfirmableTrait; /** * The console command name. * * @var string */ protected $name = 'migrate:reset'; /** * The console command description. * * @var string */ protected $description = 'Rollback all database migrations'; /** * The migrator instance. * * @var \Illuminate\Database\Migrations\Migrator */ protected $migrator; /** * Create a new migration rollback command instance. * * @param \Illuminate\Database\Migrations\Migrator $migrator * @return void */ public function __construct(Migrator $migrator) { parent::__construct(); $this->migrator = $migrator; } /** * Execute the console command. * * @return int */ public function handle() { if (! $this->confirmToProceed()) { return 1; } return $this->migrator->usingConnection($this->option('database'), function () { // First, we'll make sure that the migration table actually exists before we // start trying to rollback and re-run all of the migrations. If it's not // present we'll just bail out with an info message for the developers. if (! $this->migrator->repositoryExists()) { return $this->components->warn('Migration table not found.'); } $this->migrator->setOutput($this->output)->reset( $this->getMigrationPaths(), $this->option('pretend') ); }); } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'], ['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'], ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run'], ]; } } src/Illuminate/Database/Console/Migrations/RollbackCommand.php 0000644 00000005011 15107327037 0020423 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Console\ConfirmableTrait; use Illuminate\Database\Migrations\Migrator; use Symfony\Component\Console\Input\InputOption; class RollbackCommand extends BaseCommand { use ConfirmableTrait; /** * The console command name. * * @var string */ protected $name = 'migrate:rollback'; /** * The console command description. * * @var string */ protected $description = 'Rollback the last database migration'; /** * The migrator instance. * * @var \Illuminate\Database\Migrations\Migrator */ protected $migrator; /** * Create a new migration rollback command instance. * * @param \Illuminate\Database\Migrations\Migrator $migrator * @return void */ public function __construct(Migrator $migrator) { parent::__construct(); $this->migrator = $migrator; } /** * Execute the console command. * * @return int */ public function handle() { if (! $this->confirmToProceed()) { return 1; } $this->migrator->usingConnection($this->option('database'), function () { $this->migrator->setOutput($this->output)->rollback( $this->getMigrationPaths(), [ 'pretend' => $this->option('pretend'), 'step' => (int) $this->option('step'), 'batch' => (int) $this->option('batch'), ] ); }); return 0; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'], ['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'], ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run'], ['step', null, InputOption::VALUE_OPTIONAL, 'The number of migrations to be reverted'], ['batch', null, InputOption::VALUE_REQUIRED, 'The batch of migrations (identified by their batch number) to be reverted'], ]; } } src/Illuminate/Database/Console/Migrations/MigrateMakeCommand.php 0000644 00000010575 15107327037 0021073 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Contracts\Console\PromptsForMissingInput; use Illuminate\Database\Migrations\MigrationCreator; use Illuminate\Support\Composer; use Illuminate\Support\Str; class MigrateMakeCommand extends BaseCommand implements PromptsForMissingInput { /** * The console command signature. * * @var string */ protected $signature = 'make:migration {name : The name of the migration} {--create= : The table to be created} {--table= : The table to migrate} {--path= : The location where the migration file should be created} {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths} {--fullpath : Output the full path of the migration (Deprecated)}'; /** * The console command description. * * @var string */ protected $description = 'Create a new migration file'; /** * The migration creator instance. * * @var \Illuminate\Database\Migrations\MigrationCreator */ protected $creator; /** * The Composer instance. * * @var \Illuminate\Support\Composer * * @deprecated Will be removed in a future Laravel version. */ protected $composer; /** * Create a new migration install command instance. * * @param \Illuminate\Database\Migrations\MigrationCreator $creator * @param \Illuminate\Support\Composer $composer * @return void */ public function __construct(MigrationCreator $creator, Composer $composer) { parent::__construct(); $this->creator = $creator; $this->composer = $composer; } /** * Execute the console command. * * @return void */ public function handle() { // It's possible for the developer to specify the tables to modify in this // schema operation. The developer may also specify if this table needs // to be freshly created so we can create the appropriate migrations. $name = Str::snake(trim($this->input->getArgument('name'))); $table = $this->input->getOption('table'); $create = $this->input->getOption('create') ?: false; // If no table was given as an option but a create option is given then we // will use the "create" option as the table name. This allows the devs // to pass a table name into this option as a short-cut for creating. if (! $table && is_string($create)) { $table = $create; $create = true; } // Next, we will attempt to guess the table name if this the migration has // "create" in the name. This will allow us to provide a convenient way // of creating migrations that create new tables for the application. if (! $table) { [$table, $create] = TableGuesser::guess($name); } // Now we are ready to write the migration out to disk. Once we've written // the migration out, we will dump-autoload for the entire framework to // make sure that the migrations are registered by the class loaders. $this->writeMigration($name, $table, $create); } /** * Write the migration file to disk. * * @param string $name * @param string $table * @param bool $create * @return void */ protected function writeMigration($name, $table, $create) { $file = $this->creator->create( $name, $this->getMigrationPath(), $table, $create ); $this->components->info(sprintf('Migration [%s] created successfully.', $file)); } /** * Get migration path (either specified by '--path' option or default location). * * @return string */ protected function getMigrationPath() { if (! is_null($targetPath = $this->input->getOption('path'))) { return ! $this->usingRealPath() ? $this->laravel->basePath().'/'.$targetPath : $targetPath; } return parent::getMigrationPath(); } /** * Prompt for missing input arguments using the returned questions. * * @return array */ protected function promptForMissingArgumentsUsing() { return [ 'name' => ['What should the migration be named?', 'E.g. create_flights_table'], ]; } } src/Illuminate/Database/Console/Migrations/RefreshCommand.php 0000644 00000011460 15107327037 0020275 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Console\Command; use Illuminate\Console\ConfirmableTrait; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Events\DatabaseRefreshed; use Symfony\Component\Console\Input\InputOption; class RefreshCommand extends Command { use ConfirmableTrait; /** * The console command name. * * @var string */ protected $name = 'migrate:refresh'; /** * The console command description. * * @var string */ protected $description = 'Reset and re-run all migrations'; /** * Execute the console command. * * @return int */ public function handle() { if (! $this->confirmToProceed()) { return 1; } // Next we'll gather some of the options so that we can have the right options // to pass to the commands. This includes options such as which database to // use and the path to use for the migration. Then we'll run the command. $database = $this->input->getOption('database'); $path = $this->input->getOption('path'); // If the "step" option is specified it means we only want to rollback a small // number of migrations before migrating again. For example, the user might // only rollback and remigrate the latest four migrations instead of all. $step = $this->input->getOption('step') ?: 0; if ($step > 0) { $this->runRollback($database, $path, $step); } else { $this->runReset($database, $path); } // The refresh command is essentially just a brief aggregate of a few other of // the migration commands and just provides a convenient wrapper to execute // them in succession. We'll also see if we need to re-seed the database. $this->call('migrate', array_filter([ '--database' => $database, '--path' => $path, '--realpath' => $this->input->getOption('realpath'), '--force' => true, ])); if ($this->laravel->bound(Dispatcher::class)) { $this->laravel[Dispatcher::class]->dispatch( new DatabaseRefreshed($database, $this->needsSeeding()) ); } if ($this->needsSeeding()) { $this->runSeeder($database); } return 0; } /** * Run the rollback command. * * @param string $database * @param string $path * @param int $step * @return void */ protected function runRollback($database, $path, $step) { $this->call('migrate:rollback', array_filter([ '--database' => $database, '--path' => $path, '--realpath' => $this->input->getOption('realpath'), '--step' => $step, '--force' => true, ])); } /** * Run the reset command. * * @param string $database * @param string $path * @return void */ protected function runReset($database, $path) { $this->call('migrate:reset', array_filter([ '--database' => $database, '--path' => $path, '--realpath' => $this->input->getOption('realpath'), '--force' => true, ])); } /** * Determine if the developer has requested database seeding. * * @return bool */ protected function needsSeeding() { return $this->option('seed') || $this->option('seeder'); } /** * Run the database seeder command. * * @param string $database * @return void */ protected function runSeeder($database) { $this->call('db:seed', array_filter([ '--database' => $database, '--class' => $this->option('seeder') ?: 'Database\\Seeders\\DatabaseSeeder', '--force' => true, ])); } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'], ['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'], ['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_OPTIONAL, 'The number of migrations to be reverted & re-run'], ]; } } src/Illuminate/Database/Console/Migrations/StatusCommand.php 0000644 00000010047 15107327037 0020162 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Database\Migrations\Migrator; use Illuminate\Support\Collection; use Symfony\Component\Console\Input\InputOption; class StatusCommand extends BaseCommand { /** * The console command name. * * @var string */ protected $name = 'migrate:status'; /** * The console command description. * * @var string */ protected $description = 'Show the status of each migration'; /** * The migrator instance. * * @var \Illuminate\Database\Migrations\Migrator */ protected $migrator; /** * Create a new migration rollback command instance. * * @param \Illuminate\Database\Migrations\Migrator $migrator * @return void */ public function __construct(Migrator $migrator) { parent::__construct(); $this->migrator = $migrator; } /** * Execute the console command. * * @return int|null */ public function handle() { return $this->migrator->usingConnection($this->option('database'), function () { if (! $this->migrator->repositoryExists()) { $this->components->error('Migration table not found.'); return 1; } $ran = $this->migrator->getRepository()->getRan(); $batches = $this->migrator->getRepository()->getMigrationBatches(); $migrations = $this->getStatusFor($ran, $batches) ->when($this->option('pending'), fn ($collection) => $collection->filter(function ($migration) { return str($migration[1])->contains('Pending'); })); if (count($migrations) > 0) { $this->newLine(); $this->components->twoColumnDetail('<fg=gray>Migration name</>', '<fg=gray>Batch / Status</>'); $migrations ->each( fn ($migration) => $this->components->twoColumnDetail($migration[0], $migration[1]) ); $this->newLine(); } elseif ($this->option('pending')) { $this->components->info('No pending migrations'); } else { $this->components->info('No migrations found'); } }); } /** * Get the status for the given run migrations. * * @param array $ran * @param array $batches * @return \Illuminate\Support\Collection */ protected function getStatusFor(array $ran, array $batches) { return Collection::make($this->getAllMigrationFiles()) ->map(function ($migration) use ($ran, $batches) { $migrationName = $this->migrator->getMigrationName($migration); $status = in_array($migrationName, $ran) ? '<fg=green;options=bold>Ran</>' : '<fg=yellow;options=bold>Pending</>'; if (in_array($migrationName, $ran)) { $status = '['.$batches[$migrationName].'] '.$status; } return [$migrationName, $status]; }); } /** * Get an array of all of the migration files. * * @return array */ protected function getAllMigrationFiles() { return $this->migrator->getMigrationFiles($this->getMigrationPaths()); } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'], ['pending', null, InputOption::VALUE_NONE, 'Only list pending migrations'], ['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to use'], ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'], ]; } } src/Illuminate/Database/Console/Migrations/MigrateCommand.php 0000644 00000022421 15107327037 0020266 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Console\ConfirmableTrait; use Illuminate\Contracts\Console\Isolatable; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Events\SchemaLoaded; use Illuminate\Database\Migrations\Migrator; use Illuminate\Database\SQLiteDatabaseDoesNotExistException; use Illuminate\Database\SqlServerConnection; use PDOException; use Throwable; use function Laravel\Prompts\confirm; class MigrateCommand extends BaseCommand implements Isolatable { use ConfirmableTrait; /** * The name and signature of the console command. * * @var string */ protected $signature = 'migrate {--database= : The database connection to use} {--force : Force the operation to run when in production} {--path=* : The path(s) to the migrations files to be executed} {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths} {--schema-path= : The path to a schema dump file} {--pretend : Dump the SQL queries that would be run} {--seed : Indicates if the seed task should be re-run} {--seeder= : The class name of the root seeder} {--step : Force the migrations to be run so they can be rolled back individually}'; /** * The console command description. * * @var string */ protected $description = 'Run the database migrations'; /** * The migrator instance. * * @var \Illuminate\Database\Migrations\Migrator */ protected $migrator; /** * The event dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $dispatcher; /** * Create a new migration command instance. * * @param \Illuminate\Database\Migrations\Migrator $migrator * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher * @return void */ public function __construct(Migrator $migrator, Dispatcher $dispatcher) { parent::__construct(); $this->migrator = $migrator; $this->dispatcher = $dispatcher; } /** * Execute the console command. * * @return int */ public function handle() { if (! $this->confirmToProceed()) { return 1; } $this->migrator->usingConnection($this->option('database'), function () { $this->prepareDatabase(); // Next, we will check to see if a path option has been defined. If it has // we will use the path relative to the root of this installation folder // so that migrations may be run for any path within the applications. $this->migrator->setOutput($this->output) ->run($this->getMigrationPaths(), [ 'pretend' => $this->option('pretend'), 'step' => $this->option('step'), ]); // Finally, if the "seed" option has been given, we will re-run the database // seed task to re-populate the database, which is convenient when adding // a migration and a seed at the same time, as it is only this command. if ($this->option('seed') && ! $this->option('pretend')) { $this->call('db:seed', [ '--class' => $this->option('seeder') ?: 'Database\\Seeders\\DatabaseSeeder', '--force' => true, ]); } }); return 0; } /** * Prepare the migration database for running. * * @return void */ protected function prepareDatabase() { if (! $this->repositoryExists()) { $this->components->info('Preparing database.'); $this->components->task('Creating migration table', function () { return $this->callSilent('migrate:install', array_filter([ '--database' => $this->option('database'), ])) == 0; }); $this->newLine(); } if (! $this->migrator->hasRunAnyMigrations() && ! $this->option('pretend')) { $this->loadSchemaState(); } } /** * Determine if the migrator repository exists. * * @return bool */ protected function repositoryExists() { return retry(2, fn () => $this->migrator->repositoryExists(), 0, function ($e) { try { if ($e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) { return $this->createMissingSqliteDatabase($e->getPrevious()->path); } $connection = $this->migrator->resolveConnection($this->option('database')); if ( $e->getPrevious() instanceof PDOException && $e->getPrevious()->getCode() === 1049 && $connection->getDriverName() === 'mysql') { return $this->createMissingMysqlDatabase($connection); } return false; } catch (Throwable) { return false; } }); } /** * Create a missing SQLite database. * * @param string $path * @return bool */ protected function createMissingSqliteDatabase($path) { if ($this->option('force')) { return touch($path); } if ($this->option('no-interaction')) { return false; } $this->components->warn('The SQLite database does not exist: '.$path); if (! confirm('Would you like to create it?', default: false)) { return false; } return touch($path); } /** * Create a missing MySQL database. * * @return bool */ protected function createMissingMysqlDatabase($connection) { if ($this->laravel['config']->get("database.connections.{$connection->getName()}.database") !== $connection->getDatabaseName()) { return false; } if (! $this->option('force') && $this->option('no-interaction')) { return false; } if (! $this->option('force') && ! $this->option('no-interaction')) { $this->components->warn("The database '{$connection->getDatabaseName()}' does not exist on the '{$connection->getName()}' connection."); if (! confirm('Would you like to create it?', default: false)) { return false; } } try { $this->laravel['config']->set("database.connections.{$connection->getName()}.database", null); $this->laravel['db']->purge(); $freshConnection = $this->migrator->resolveConnection($this->option('database')); return tap($freshConnection->unprepared("CREATE DATABASE IF NOT EXISTS `{$connection->getDatabaseName()}`"), function () { $this->laravel['db']->purge(); }); } finally { $this->laravel['config']->set("database.connections.{$connection->getName()}.database", $connection->getDatabaseName()); } } /** * Load the schema state to seed the initial database schema structure. * * @return void */ protected function loadSchemaState() { $connection = $this->migrator->resolveConnection($this->option('database')); // 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 SqlServerConnection || ! is_file($path = $this->schemaPath($connection))) { return; } $this->components->info('Loading stored database schemas.'); $this->components->task($path, function () use ($connection, $path) { // Since the schema file will create the "migrations" table and reload it to its // proper state, we need to delete it here so we don't get an error that this // table already exists when the stored database schema file gets executed. $this->migrator->deleteRepository(); $connection->getSchemaState()->handleOutputUsing(function ($type, $buffer) { $this->output->write($buffer); })->load($path); }); $this->newLine(); // Finally, we will fire an event that this schema has been loaded so developers // can perform any post schema load tasks that are necessary in listeners for // this event, which may seed the database tables with some necessary data. $this->dispatcher->dispatch( new SchemaLoaded($connection, $path) ); } /** * Get the path to the stored schema for the given connection. * * @param \Illuminate\Database\Connection $connection * @return string */ protected function schemaPath($connection) { if ($this->option('schema-path')) { return $this->option('schema-path'); } if (file_exists($path = database_path('schema/'.$connection->getName().'-schema.dump'))) { return $path; } return database_path('schema/'.$connection->getName().'-schema.sql'); } } src/Illuminate/Database/Console/MonitorCommand.php 0000644 00000007642 15107327037 0016221 0 ustar 00 <?php namespace Illuminate\Database\Console; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\Events\DatabaseBusy; use Illuminate\Support\Composer; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'db:monitor')] class MonitorCommand extends DatabaseInspectionCommand { /** * The name and signature of the console command. * * @var string */ protected $signature = 'db:monitor {--databases= : The database connections to monitor} {--max= : The maximum number of connections that can be open before an event is dispatched}'; /** * The console command description. * * @var string */ protected $description = 'Monitor the number of connections on the specified database'; /** * The connection resolver instance. * * @var \Illuminate\Database\ConnectionResolverInterface */ protected $connection; /** * The events dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * Create a new command instance. * * @param \Illuminate\Database\ConnectionResolverInterface $connection * @param \Illuminate\Contracts\Events\Dispatcher $events * @param \Illuminate\Support\Composer $composer */ public function __construct(ConnectionResolverInterface $connection, Dispatcher $events, Composer $composer) { parent::__construct($composer); $this->connection = $connection; $this->events = $events; } /** * Execute the console command. * * @return void */ public function handle() { $databases = $this->parseDatabases($this->option('databases')); $this->displayConnections($databases); if ($this->option('max')) { $this->dispatchEvents($databases); } } /** * Parse the database into an array of the connections. * * @param string $databases * @return \Illuminate\Support\Collection */ protected function parseDatabases($databases) { return collect(explode(',', $databases))->map(function ($database) { if (! $database) { $database = $this->laravel['config']['database.default']; } $maxConnections = $this->option('max'); return [ 'database' => $database, 'connections' => $connections = $this->getConnectionCount($this->connection->connection($database)), 'status' => $maxConnections && $connections >= $maxConnections ? '<fg=yellow;options=bold>ALERT</>' : '<fg=green;options=bold>OK</>', ]; }); } /** * Display the databases and their connection counts in the console. * * @param \Illuminate\Support\Collection $databases * @return void */ protected function displayConnections($databases) { $this->newLine(); $this->components->twoColumnDetail('<fg=gray>Database name</>', '<fg=gray>Connections</>'); $databases->each(function ($database) { $status = '['.$database['connections'].'] '.$database['status']; $this->components->twoColumnDetail($database['database'], $status); }); $this->newLine(); } /** * Dispatch the database monitoring events. * * @param \Illuminate\Support\Collection $databases * @return void */ protected function dispatchEvents($databases) { $databases->each(function ($database) { if ($database['status'] === '<fg=green;options=bold>OK</>') { return; } $this->events->dispatch( new DatabaseBusy( $database['database'], $database['connections'] ) ); }); } } src/Illuminate/Database/Console/ShowCommand.php 0000644 00000015542 15107327037 0015510 0 ustar 00 <?php namespace Illuminate\Database\Console; use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\View; use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Support\Arr; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'db:show')] class ShowCommand extends DatabaseInspectionCommand { /** * The name and signature of the console command. * * @var string */ protected $signature = 'db:show {--database= : The database connection} {--json : Output the database information as JSON} {--counts : Show the table row count <bg=red;options=bold> Note: This can be slow on large databases </>}; {--views : Show the database views <bg=red;options=bold> Note: This can be slow on large databases </>}'; /** * The console command description. * * @var string */ protected $description = 'Display information about the given database'; /** * Execute the console command. * * @param \Illuminate\Database\ConnectionResolverInterface $connections * @return int */ public function handle(ConnectionResolverInterface $connections) { if (! $this->ensureDependenciesExist()) { return 1; } $connection = $connections->connection($database = $this->input->getOption('database')); $doctrineConnection = $connection->getDoctrineConnection(); $schema = $connection->getDoctrineSchemaManager(); $this->registerTypeMappings($doctrineConnection->getDatabasePlatform()); $data = [ 'platform' => [ 'config' => $this->getConfigFromDatabase($database), 'name' => $this->getPlatformName($doctrineConnection->getDatabasePlatform(), $database), 'open_connections' => $this->getConnectionCount($connection), ], 'tables' => $this->tables($connection, $schema), ]; if ($this->option('views')) { $data['views'] = $this->collectViews($connection, $schema); } $this->display($data); return 0; } /** * Get information regarding the tables within the database. * * @param \Illuminate\Database\ConnectionInterface $connection * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema * @return \Illuminate\Support\Collection */ protected function tables(ConnectionInterface $connection, AbstractSchemaManager $schema) { return collect($schema->listTables())->map(fn (Table $table, $index) => [ 'table' => $table->getName(), 'size' => $this->getTableSize($connection, $table->getName()), 'rows' => $this->option('counts') ? $connection->table($table->getName())->count() : null, 'engine' => rescue(fn () => $table->getOption('engine'), null, false), 'comment' => $table->getComment(), ]); } /** * Get information regarding the views within the database. * * @param \Illuminate\Database\ConnectionInterface $connection * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema * @return \Illuminate\Support\Collection */ protected function collectViews(ConnectionInterface $connection, AbstractSchemaManager $schema) { return collect($schema->listViews()) ->reject(fn (View $view) => str($view->getName()) ->startsWith(['pg_catalog', 'information_schema', 'spt_'])) ->map(fn (View $view) => [ 'view' => $view->getName(), 'rows' => $connection->table($view->getName())->count(), ]); } /** * Render the database information. * * @param array $data * @return void */ protected function display(array $data) { $this->option('json') ? $this->displayJson($data) : $this->displayForCli($data); } /** * Render the database information as JSON. * * @param array $data * @return void */ protected function displayJson(array $data) { $this->output->writeln(json_encode($data)); } /** * Render the database information formatted for the CLI. * * @param array $data * @return void */ protected function displayForCli(array $data) { $platform = $data['platform']; $tables = $data['tables']; $views = $data['views'] ?? null; $this->newLine(); $this->components->twoColumnDetail('<fg=green;options=bold>'.$platform['name'].'</>'); $this->components->twoColumnDetail('Database', Arr::get($platform['config'], 'database')); $this->components->twoColumnDetail('Host', Arr::get($platform['config'], 'host')); $this->components->twoColumnDetail('Port', Arr::get($platform['config'], 'port')); $this->components->twoColumnDetail('Username', Arr::get($platform['config'], 'username')); $this->components->twoColumnDetail('URL', Arr::get($platform['config'], 'url')); $this->components->twoColumnDetail('Open Connections', $platform['open_connections']); $this->components->twoColumnDetail('Tables', $tables->count()); if ($tableSizeSum = $tables->sum('size')) { $this->components->twoColumnDetail('Total Size', number_format($tableSizeSum / 1024 / 1024, 2).'MiB'); } $this->newLine(); if ($tables->isNotEmpty()) { $this->components->twoColumnDetail('<fg=green;options=bold>Table</>', 'Size (MiB)'.($this->option('counts') ? ' <fg=gray;options=bold>/</> <fg=yellow;options=bold>Rows</>' : '')); $tables->each(function ($table) { if ($tableSize = $table['size']) { $tableSize = number_format($tableSize / 1024 / 1024, 2); } $this->components->twoColumnDetail( $table['table'].($this->output->isVerbose() ? ' <fg=gray>'.$table['engine'].'</>' : null), ($tableSize ? $tableSize : '—').($this->option('counts') ? ' <fg=gray;options=bold>/</> <fg=yellow;options=bold>'.number_format($table['rows']).'</>' : '') ); if ($this->output->isVerbose()) { if ($table['comment']) { $this->components->bulletList([ $table['comment'], ]); } } }); $this->newLine(); } if ($views && $views->isNotEmpty()) { $this->components->twoColumnDetail('<fg=green;options=bold>View</>', '<fg=green;options=bold>Rows</>'); $views->each(fn ($view) => $this->components->twoColumnDetail($view['view'], number_format($view['rows']))); $this->newLine(); } } } src/Illuminate/Database/Console/Factories/FactoryMakeCommand.php 0000644 00000007170 15107327037 0020712 0 ustar 00 <?php namespace Illuminate\Database\Console\Factories; use Illuminate\Console\GeneratorCommand; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'make:factory')] class FactoryMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:factory'; /** * The console command description. * * @var string */ protected $description = 'Create a new model factory'; /** * The type of class being generated. * * @var string */ protected $type = 'Factory'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->resolveStubPath('/stubs/factory.stub'); } /** * Resolve the fully-qualified path to the stub. * * @param string $stub * @return string */ protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) ? $customPath : __DIR__.$stub; } /** * Build the class with the given name. * * @param string $name * @return string */ protected function buildClass($name) { $factory = class_basename(Str::ucfirst(str_replace('Factory', '', $name))); $namespaceModel = $this->option('model') ? $this->qualifyModel($this->option('model')) : $this->qualifyModel($this->guessModelName($name)); $model = class_basename($namespaceModel); $namespace = $this->getNamespace( Str::replaceFirst($this->rootNamespace(), 'Database\\Factories\\', $this->qualifyClass($this->getNameInput())) ); $replace = [ '{{ factoryNamespace }}' => $namespace, 'NamespacedDummyModel' => $namespaceModel, '{{ namespacedModel }}' => $namespaceModel, '{{namespacedModel}}' => $namespaceModel, 'DummyModel' => $model, '{{ model }}' => $model, '{{model}}' => $model, '{{ factory }}' => $factory, '{{factory}}' => $factory, ]; return str_replace( array_keys($replace), array_values($replace), parent::buildClass($name) ); } /** * Get the destination class path. * * @param string $name * @return string */ protected function getPath($name) { $name = (string) Str::of($name)->replaceFirst($this->rootNamespace(), '')->finish('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_ends_with($name, 'Factory')) { $name = substr($name, 0, -7); } $modelName = $this->qualifyModel(Str::after($name, $this->rootNamespace())); if (class_exists($modelName)) { return $modelName; } if (is_dir(app_path('Models/'))) { return $this->rootNamespace().'Models\Model'; } return $this->rootNamespace().'Model'; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['model', 'm', InputOption::VALUE_OPTIONAL, 'The name of the model'], ]; } } src/Illuminate/Database/Console/Factories/stubs/factory.stub 0000644 00000000655 15107327037 0020204 0 ustar 00 <?php namespace {{ factoryNamespace }}; use Illuminate\Database\Eloquent\Factories\Factory; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\{{ namespacedModel }}> */ class {{ factory }}Factory extends Factory { /** * Define the model's default state. * * @return array<string, mixed> */ public function definition(): array { return [ // ]; } } src/Illuminate/Database/PDO/SqlServerDriver.php 0000644 00000001350 15107327037 0015403 0 ustar 00 <?php namespace Illuminate\Database\PDO; use Doctrine\DBAL\Driver\AbstractSQLServerDriver; class SqlServerDriver extends AbstractSQLServerDriver { /** * Create a new database connection. * * @param mixed[] $params * @param string|null $username * @param string|null $password * @param mixed[] $driverOptions * @return \Illuminate\Database\PDO\SqlServerConnection */ public function connect(array $params, $username = null, $password = null, array $driverOptions = []) { return new SqlServerConnection( new Connection($params['pdo']) ); } /** * {@inheritdoc} */ public function getName() { return 'pdo_sqlsrv'; } } src/Illuminate/Database/PDO/SqlServerConnection.php 0000644 00000006465 15107327037 0016263 0 ustar 00 <?php namespace Illuminate\Database\PDO; use Doctrine\DBAL\Driver\PDO\SQLSrv\Statement; use Doctrine\DBAL\Driver\Result; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Driver\Statement as StatementInterface; use Doctrine\DBAL\ParameterType; use PDO; class SqlServerConnection implements ServerInfoAwareConnection { /** * The underlying connection instance. * * @var \Illuminate\Database\PDO\Connection */ protected $connection; /** * Create a new SQL Server connection instance. * * @param \Illuminate\Database\PDO\Connection $connection * @return void */ public function __construct(Connection $connection) { $this->connection = $connection; } /** * Prepare a new SQL statement. * * @param string $sql * @return \Doctrine\DBAL\Driver\Statement */ public function prepare(string $sql): StatementInterface { return new Statement( $this->connection->prepare($sql) ); } /** * Execute a new query against the connection. * * @param string $sql * @return \Doctrine\DBAL\Driver\Result */ public function query(string $sql): Result { return $this->connection->query($sql); } /** * Execute an SQL statement. * * @param string $statement * @return int */ public function exec(string $statement): int { return $this->connection->exec($statement); } /** * Get the last insert ID. * * @param string|null $name * @return mixed */ public function lastInsertId($name = null) { if ($name === null) { return $this->connection->lastInsertId($name); } return $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?') ->execute([$name]) ->fetchOne(); } /** * Begin a new database transaction. * * @return bool */ public function beginTransaction() { return $this->connection->beginTransaction(); } /** * Commit a database transaction. * * @return bool */ public function commit() { return $this->connection->commit(); } /** * Rollback a database transaction. * * @return bool */ public function rollBack() { return $this->connection->rollBack(); } /** * Wrap quotes around the given input. * * @param string $value * @param int $type * @return string */ public function quote($value, $type = ParameterType::STRING) { $val = $this->connection->quote($value, $type); // Fix for a driver version terminating all values with null byte... if (\is_string($val) && str_contains($val, "\0")) { $val = \substr($val, 0, -1); } return $val; } /** * Get the server version for the connection. * * @return string */ public function getServerVersion() { return $this->connection->getServerVersion(); } /** * Get the wrapped PDO connection. * * @return \PDO */ public function getWrappedConnection(): PDO { return $this->connection->getWrappedConnection(); } } src/Illuminate/Database/PDO/MySqlDriver.php 0000644 00000000515 15107327037 0014524 0 ustar 00 <?php namespace Illuminate\Database\PDO; use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Illuminate\Database\PDO\Concerns\ConnectsToDatabase; class MySqlDriver extends AbstractMySQLDriver { use ConnectsToDatabase; /** * {@inheritdoc} */ public function getName() { return 'pdo_mysql'; } } src/Illuminate/Database/PDO/PostgresDriver.php 0000644 00000000532 15107327037 0015264 0 ustar 00 <?php namespace Illuminate\Database\PDO; use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver; use Illuminate\Database\PDO\Concerns\ConnectsToDatabase; class PostgresDriver extends AbstractPostgreSQLDriver { use ConnectsToDatabase; /** * {@inheritdoc} */ public function getName() { return 'pdo_pgsql'; } } src/Illuminate/Database/PDO/Concerns/ConnectsToDatabase.php 0000644 00000001511 15107327037 0017556 0 ustar 00 <?php namespace Illuminate\Database\PDO\Concerns; use Illuminate\Database\PDO\Connection; use InvalidArgumentException; use PDO; trait ConnectsToDatabase { /** * Create a new database connection. * * @param mixed[] $params * @param string|null $username * @param string|null $password * @param mixed[] $driverOptions * @return \Illuminate\Database\PDO\Connection * * @throws \InvalidArgumentException */ public function connect(array $params, $username = null, $password = null, array $driverOptions = []) { if (! isset($params['pdo']) || ! $params['pdo'] instanceof PDO) { throw new InvalidArgumentException('Laravel requires the "pdo" property to be set and be a PDO instance.'); } return new Connection($params['pdo']); } } src/Illuminate/Database/PDO/SQLiteDriver.php 0000644 00000000521 15107327037 0014615 0 ustar 00 <?php namespace Illuminate\Database\PDO; use Doctrine\DBAL\Driver\AbstractSQLiteDriver; use Illuminate\Database\PDO\Concerns\ConnectsToDatabase; class SQLiteDriver extends AbstractSQLiteDriver { use ConnectsToDatabase; /** * {@inheritdoc} */ public function getName() { return 'pdo_sqlite'; } } src/Illuminate/Database/PDO/Connection.php 0000644 00000010064 15107327037 0014402 0 ustar 00 <?php namespace Illuminate\Database\PDO; use Doctrine\DBAL\Driver\PDO\Exception; use Doctrine\DBAL\Driver\PDO\Result; use Doctrine\DBAL\Driver\PDO\Statement; use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Driver\Statement as StatementInterface; use Doctrine\DBAL\ParameterType; use PDO; use PDOException; use PDOStatement; class Connection implements ServerInfoAwareConnection { /** * The underlying PDO connection. * * @var \PDO */ protected $connection; /** * Create a new PDO connection instance. * * @param \PDO $connection * @return void */ public function __construct(PDO $connection) { $this->connection = $connection; } /** * Execute an SQL statement. * * @param string $statement * @return int */ public function exec(string $statement): int { try { $result = $this->connection->exec($statement); \assert($result !== false); return $result; } catch (PDOException $exception) { throw Exception::new($exception); } } /** * Prepare a new SQL statement. * * @param string $sql * @return \Doctrine\DBAL\Driver\Statement * * @throws \Doctrine\DBAL\Driver\PDO\Exception */ public function prepare(string $sql): StatementInterface { try { return $this->createStatement( $this->connection->prepare($sql) ); } catch (PDOException $exception) { throw Exception::new($exception); } } /** * Execute a new query against the connection. * * @param string $sql * @return \Doctrine\DBAL\Driver\Result */ public function query(string $sql): ResultInterface { try { $stmt = $this->connection->query($sql); \assert($stmt instanceof PDOStatement); return new Result($stmt); } catch (PDOException $exception) { throw Exception::new($exception); } } /** * Get the last insert ID. * * @param string|null $name * @return mixed * * @throws \Doctrine\DBAL\Driver\PDO\Exception */ public function lastInsertId($name = null) { try { if ($name === null) { return $this->connection->lastInsertId(); } return $this->connection->lastInsertId($name); } catch (PDOException $exception) { throw Exception::new($exception); } } /** * Create a new statement instance. * * @param \PDOStatement $stmt * @return \Doctrine\DBAL\Driver\PDO\Statement */ protected function createStatement(PDOStatement $stmt): Statement { return new Statement($stmt); } /** * Begin a new database transaction. * * @return bool */ public function beginTransaction() { return $this->connection->beginTransaction(); } /** * Commit a database transaction. * * @return bool */ public function commit() { return $this->connection->commit(); } /** * Rollback a database transaction. * * @return bool */ public function rollBack() { return $this->connection->rollBack(); } /** * Wrap quotes around the given input. * * @param string $input * @param string $type * @return string */ public function quote($input, $type = ParameterType::STRING) { return $this->connection->quote($input, $type); } /** * Get the server version for the connection. * * @return string */ public function getServerVersion() { return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION); } /** * Get the wrapped PDO connection. * * @return \PDO */ public function getWrappedConnection(): PDO { return $this->connection; } } src/Illuminate/Database/SqlServerConnection.php 0000644 00000010165 15107327037 0015631 0 ustar 00 <?php namespace Illuminate\Database; use Closure; use Exception; use Illuminate\Database\PDO\SqlServerDriver; use Illuminate\Database\Query\Grammars\SqlServerGrammar as QueryGrammar; use Illuminate\Database\Query\Processors\SqlServerProcessor; use Illuminate\Database\Schema\Grammars\SqlServerGrammar as SchemaGrammar; use Illuminate\Database\Schema\SqlServerBuilder; use Illuminate\Filesystem\Filesystem; use RuntimeException; use Throwable; class SqlServerConnection extends Connection { /** * Execute a Closure within a transaction. * * @param \Closure $callback * @param int $attempts * @return mixed * * @throws \Throwable */ public function transaction(Closure $callback, $attempts = 1) { for ($a = 1; $a <= $attempts; $a++) { if ($this->getDriverName() === 'sqlsrv') { return parent::transaction($callback, $attempts); } $this->getPdo()->exec('BEGIN TRAN'); // We'll simply execute the given callback within a try / catch block // and if we catch any exception we can rollback the transaction // so that none of the changes are persisted to the database. try { $result = $callback($this); $this->getPdo()->exec('COMMIT TRAN'); } // If we catch an exception, we will rollback so nothing gets messed // up in the database. Then we'll re-throw the exception so it can // be handled how the developer sees fit for their applications. catch (Throwable $e) { $this->getPdo()->exec('ROLLBACK TRAN'); throw $e; } return $result; } } /** * Escape a binary value for safe SQL embedding. * * @param string $value * @return string */ protected function escapeBinary($value) { $hex = bin2hex($value); return "0x{$hex}"; } /** * Determine if the given database exception was caused by a unique constraint violation. * * @param \Exception $exception * @return bool */ protected function isUniqueConstraintError(Exception $exception) { return boolval(preg_match('#Cannot insert duplicate key row in object#i', $exception->getMessage())); } /** * Get the default query grammar instance. * * @return \Illuminate\Database\Query\Grammars\SqlServerGrammar */ protected function getDefaultQueryGrammar() { ($grammar = new QueryGrammar)->setConnection($this); return $this->withTablePrefix($grammar); } /** * Get a schema builder instance for the connection. * * @return \Illuminate\Database\Schema\SqlServerBuilder */ public function getSchemaBuilder() { if (is_null($this->schemaGrammar)) { $this->useDefaultSchemaGrammar(); } return new SqlServerBuilder($this); } /** * Get the default schema grammar instance. * * @return \Illuminate\Database\Schema\Grammars\SqlServerGrammar */ protected function getDefaultSchemaGrammar() { ($grammar = new SchemaGrammar)->setConnection($this); return $this->withTablePrefix($grammar); } /** * Get the schema state for the connection. * * @param \Illuminate\Filesystem\Filesystem|null $files * @param callable|null $processFactory * * @throws \RuntimeException */ public function getSchemaState(Filesystem $files = null, callable $processFactory = null) { throw new RuntimeException('Schema dumping is not supported when using SQL Server.'); } /** * Get the default post processor instance. * * @return \Illuminate\Database\Query\Processors\SqlServerProcessor */ protected function getDefaultPostProcessor() { return new SqlServerProcessor; } /** * Get the Doctrine DBAL driver. * * @return \Illuminate\Database\PDO\SqlServerDriver */ protected function getDoctrineDriver() { return new SqlServerDriver; } } src/Illuminate/Database/MultipleRecordsFoundException.php 0000644 00000001351 15107327037 0017650 0 ustar 00 <?php namespace Illuminate\Database; use RuntimeException; class MultipleRecordsFoundException extends RuntimeException { /** * The number of records found. * * @var int */ public $count; /** * Create a new exception instance. * * @param int $count * @param int $code * @param \Throwable|null $previous * @return void */ public function __construct($count, $code = 0, $previous = null) { $this->count = $count; parent::__construct("$count records were found.", $code, $previous); } /** * Get the number of records found. * * @return int */ public function getCount() { return $this->count; } } src/Illuminate/Database/PostgresConnection.php 0000644 00000006033 15107327037 0015510 0 ustar 00 <?php namespace Illuminate\Database; use Exception; use Illuminate\Database\PDO\PostgresDriver; use Illuminate\Database\Query\Grammars\PostgresGrammar as QueryGrammar; use Illuminate\Database\Query\Processors\PostgresProcessor; use Illuminate\Database\Schema\Grammars\PostgresGrammar as SchemaGrammar; use Illuminate\Database\Schema\PostgresBuilder; use Illuminate\Database\Schema\PostgresSchemaState; use Illuminate\Filesystem\Filesystem; class PostgresConnection extends Connection { /** * Escape a binary value for safe SQL embedding. * * @param string $value * @return string */ protected function escapeBinary($value) { $hex = bin2hex($value); return "'\x{$hex}'::bytea"; } /** * Escape a bool value for safe SQL embedding. * * @param bool $value * @return string */ protected function escapeBool($value) { return $value ? 'true' : 'false'; } /** * Determine if the given database exception was caused by a unique constraint violation. * * @param \Exception $exception * @return bool */ protected function isUniqueConstraintError(Exception $exception) { return '23505' === $exception->getCode(); } /** * Get the default query grammar instance. * * @return \Illuminate\Database\Query\Grammars\PostgresGrammar */ protected function getDefaultQueryGrammar() { ($grammar = new QueryGrammar)->setConnection($this); return $this->withTablePrefix($grammar); } /** * Get a schema builder instance for the connection. * * @return \Illuminate\Database\Schema\PostgresBuilder */ public function getSchemaBuilder() { if (is_null($this->schemaGrammar)) { $this->useDefaultSchemaGrammar(); } return new PostgresBuilder($this); } /** * Get the default schema grammar instance. * * @return \Illuminate\Database\Schema\Grammars\PostgresGrammar */ protected function getDefaultSchemaGrammar() { ($grammar = new SchemaGrammar)->setConnection($this); return $this->withTablePrefix($grammar); } /** * Get the schema state for the connection. * * @param \Illuminate\Filesystem\Filesystem|null $files * @param callable|null $processFactory * @return \Illuminate\Database\Schema\PostgresSchemaState */ public function getSchemaState(Filesystem $files = null, callable $processFactory = null) { return new PostgresSchemaState($this, $files, $processFactory); } /** * Get the default post processor instance. * * @return \Illuminate\Database\Query\Processors\PostgresProcessor */ protected function getDefaultPostProcessor() { return new PostgresProcessor; } /** * Get the Doctrine DBAL driver. * * @return \Illuminate\Database\PDO\PostgresDriver */ protected function getDoctrineDriver() { return new PostgresDriver; } } src/Illuminate/Database/DatabaseTransactionsManager.php 0000644 00000005236 15107327037 0017256 0 ustar 00 <?php namespace Illuminate\Database; class DatabaseTransactionsManager { /** * All of the recorded transactions. * * @var \Illuminate\Support\Collection<int, \Illuminate\Database\DatabaseTransactionRecord> */ protected $transactions; /** * Create a new database transactions manager instance. * * @return void */ public function __construct() { $this->transactions = collect(); } /** * Start a new database transaction. * * @param string $connection * @param int $level * @return void */ public function begin($connection, $level) { $this->transactions->push( new DatabaseTransactionRecord($connection, $level) ); } /** * Rollback the active database transaction. * * @param string $connection * @param int $level * @return void */ public function rollback($connection, $level) { $this->transactions = $this->transactions->reject( fn ($transaction) => $transaction->connection == $connection && $transaction->level > $level )->values(); } /** * Commit the active database transaction. * * @param string $connection * @return void */ public function commit($connection) { [$forThisConnection, $forOtherConnections] = $this->transactions->partition( fn ($transaction) => $transaction->connection == $connection ); $this->transactions = $forOtherConnections->values(); $forThisConnection->map->executeCallbacks(); } /** * Register a transaction callback. * * @param callable $callback * @return void */ public function addCallback($callback) { if ($current = $this->callbackApplicableTransactions()->last()) { return $current->addCallback($callback); } $callback(); } /** * Get the transactions that are applicable to callbacks. * * @return \Illuminate\Support\Collection<int, \Illuminate\Database\DatabaseTransactionRecord> */ public function callbackApplicableTransactions() { return $this->transactions; } /** * Determine if after commit callbacks should be executed for the given transaction level. * * @param int $level * @return bool */ public function afterCommitCallbacksShouldBeExecuted($level) { return $level === 0; } /** * Get all the transactions. * * @return \Illuminate\Support\Collection */ public function getTransactions() { return $this->transactions; } } src/Illuminate/Database/README.md 0000644 00000004240 15107327037 0012426 0 ustar 00 ## Illuminate Database The Illuminate Database component is a full database toolkit for PHP, providing an expressive query builder, ActiveRecord style ORM, and schema builder. It currently supports MySQL, Postgres, SQL Server, and SQLite. It also serves as the database layer of the Laravel PHP framework. ### Usage Instructions First, create a new "Capsule" manager instance. Capsule aims to make configuring the library for usage outside of the Laravel framework as easy as possible. ```PHP use Illuminate\Database\Capsule\Manager as Capsule; $capsule = new Capsule; $capsule->addConnection([ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'database', 'username' => 'root', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ]); // Set the event dispatcher used by Eloquent models... (optional) use Illuminate\Events\Dispatcher; use Illuminate\Container\Container; $capsule->setEventDispatcher(new Dispatcher(new Container)); // Make this Capsule instance available globally via static methods... (optional) $capsule->setAsGlobal(); // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher()) $capsule->bootEloquent(); ``` > `composer require "illuminate/events"` required when you need to use observers with Eloquent. Once the Capsule instance has been registered. You may use it like so: **Using The Query Builder** ```PHP $users = Capsule::table('users')->where('votes', '>', 100)->get(); ``` Other core methods may be accessed directly from the Capsule in the same manner as from the DB facade: ```PHP $results = Capsule::select('select * from users where id = ?', [1]); ``` **Using The Schema Builder** ```PHP Capsule::schema()->create('users', function ($table) { $table->increments('id'); $table->string('email')->unique(); $table->timestamps(); }); ``` **Using The Eloquent ORM** ```PHP class User extends Illuminate\Database\Eloquent\Model {} $users = User::where('votes', '>', 1)->get(); ``` For further documentation on using the various database facilities this library provides, consult the [Laravel framework documentation](https://laravel.com/docs). src/Illuminate/Database/ConfigurationUrlParser.php 0000644 00000000300 15107327037 0016320 0 ustar 00 <?php namespace Illuminate\Database; use Illuminate\Support\ConfigurationUrlParser as BaseConfigurationUrlParser; class ConfigurationUrlParser extends BaseConfigurationUrlParser { // } src/Illuminate/Database/DBAL/TimestampType.php 0000644 00000006070 15107327037 0015172 0 ustar 00 <?php namespace Illuminate\Database\DBAL; use Doctrine\DBAL\Exception as DBALException; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\MariaDb1027Platform; use Doctrine\DBAL\Platforms\MariaDb1052Platform; use Doctrine\DBAL\Platforms\MariaDBPlatform; use Doctrine\DBAL\Platforms\MySQL57Platform; use Doctrine\DBAL\Platforms\MySQL80Platform; use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Platforms\PostgreSQL100Platform; use Doctrine\DBAL\Platforms\PostgreSQL94Platform; use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Platforms\SqlitePlatform; use Doctrine\DBAL\Platforms\SQLServer2012Platform; use Doctrine\DBAL\Platforms\SQLServerPlatform; use Doctrine\DBAL\Types\PhpDateTimeMappingType; use Doctrine\DBAL\Types\Type; class TimestampType extends Type implements PhpDateTimeMappingType { /** * {@inheritdoc} * * @throws DBALException */ public function getSQLDeclaration(array $column, AbstractPlatform $platform): string { return match (get_class($platform)) { MySQLPlatform::class, MySQL57Platform::class, MySQL80Platform::class, MariaDBPlatform::class, MariaDb1027Platform::class, MariaDb1052Platform::class, => $this->getMySqlPlatformSQLDeclaration($column), PostgreSQLPlatform::class, PostgreSQL94Platform::class, PostgreSQL100Platform::class => $this->getPostgresPlatformSQLDeclaration($column), SQLServerPlatform::class, SQLServer2012Platform::class => $this->getSqlServerPlatformSQLDeclaration($column), SqlitePlatform::class => 'DATETIME', default => throw new DBALException('Invalid platform: '.substr(strrchr(get_class($platform), '\\'), 1)), }; } /** * Get the SQL declaration for MySQL. * * @param array $column * @return string */ protected function getMySqlPlatformSQLDeclaration(array $column): string { $columnType = 'TIMESTAMP'; if ($column['precision']) { $columnType = 'TIMESTAMP('.min((int) $column['precision'], 6).')'; } $notNull = $column['notnull'] ?? false; if (! $notNull) { return $columnType.' NULL'; } return $columnType; } /** * Get the SQL declaration for PostgreSQL. * * @param array $column * @return string */ protected function getPostgresPlatformSQLDeclaration(array $column): string { return 'TIMESTAMP('.min((int) $column['precision'], 6).')'; } /** * Get the SQL declaration for SQL Server. * * @param array $column * @return string */ protected function getSqlServerPlatformSQLDeclaration(array $column): string { return $column['precision'] ?? false ? 'DATETIME2('.min((int) $column['precision'], 7).')' : 'DATETIME'; } /** * {@inheritdoc} * * @return string */ public function getName() { return 'timestamp'; } } src/Illuminate/Database/DatabaseServiceProvider.php 0000644 00000006303 15107327037 0016422 0 ustar 00 <?php namespace Illuminate\Database; use Faker\Factory as FakerFactory; use Faker\Generator as FakerGenerator; use Illuminate\Contracts\Queue\EntityResolver; use Illuminate\Database\Connectors\ConnectionFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\QueueEntityResolver; use Illuminate\Support\ServiceProvider; class DatabaseServiceProvider extends ServiceProvider { /** * The array of resolved Faker instances. * * @var array */ protected static $fakers = []; /** * Bootstrap the application events. * * @return void */ public function boot() { Model::setConnectionResolver($this->app['db']); Model::setEventDispatcher($this->app['events']); } /** * Register the service provider. * * @return void */ public function register() { Model::clearBootedModels(); $this->registerConnectionServices(); $this->registerEloquentFactory(); $this->registerQueueableEntityResolver(); } /** * Register the primary database bindings. * * @return void */ protected function registerConnectionServices() { // The connection factory is used to create the actual connection instances on // the database. We will inject the factory into the manager so that it may // make the connections while they are actually needed and not of before. $this->app->singleton('db.factory', function ($app) { return new ConnectionFactory($app); }); // The database manager is used to resolve various connections, since multiple // connections might be managed. It also implements the connection resolver // interface which may be used by other components requiring connections. $this->app->singleton('db', function ($app) { return new DatabaseManager($app, $app['db.factory']); }); $this->app->bind('db.connection', function ($app) { return $app['db']->connection(); }); $this->app->bind('db.schema', function ($app) { return $app['db']->connection()->getSchemaBuilder(); }); $this->app->singleton('db.transactions', function ($app) { return new DatabaseTransactionsManager; }); } /** * Register the Eloquent factory instance in the container. * * @return void */ protected function registerEloquentFactory() { $this->app->singleton(FakerGenerator::class, function ($app, $parameters) { $locale = $parameters['locale'] ?? $app['config']->get('app.faker_locale', 'en_US'); if (! isset(static::$fakers[$locale])) { static::$fakers[$locale] = FakerFactory::create($locale); } static::$fakers[$locale]->unique(true); return static::$fakers[$locale]; }); } /** * Register the queueable entity resolver implementation. * * @return void */ protected function registerQueueableEntityResolver() { $this->app->singleton(EntityResolver::class, function () { return new QueueEntityResolver; }); } } src/Illuminate/Database/RecordsNotFoundException.php 0000644 00000000201 15107327037 0016606 0 ustar 00 <?php namespace Illuminate\Database; use RuntimeException; class RecordsNotFoundException extends RuntimeException { // } src/Illuminate/Database/LazyLoadingViolationException.php 0000644 00000001403 15107327037 0017637 0 ustar 00 <?php namespace Illuminate\Database; use RuntimeException; class LazyLoadingViolationException extends RuntimeException { /** * The name of the affected Eloquent model. * * @var string */ public $model; /** * The name of the relation. * * @var string */ public $relation; /** * Create a new exception instance. * * @param object $model * @param string $relation * @return static */ public function __construct($model, $relation) { $class = get_class($model); parent::__construct("Attempted to lazy load [{$relation}] on model [{$class}] but lazy loading is disabled."); $this->model = $class; $this->relation = $relation; } } src/Illuminate/Database/MigrationServiceProvider.php 0000644 00000013630 15107327037 0016650 0 ustar 00 <?php namespace Illuminate\Database; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Database\Console\Migrations\FreshCommand; use Illuminate\Database\Console\Migrations\InstallCommand; use Illuminate\Database\Console\Migrations\MigrateCommand; use Illuminate\Database\Console\Migrations\MigrateMakeCommand; use Illuminate\Database\Console\Migrations\RefreshCommand; use Illuminate\Database\Console\Migrations\ResetCommand; use Illuminate\Database\Console\Migrations\RollbackCommand; use Illuminate\Database\Console\Migrations\StatusCommand; use Illuminate\Database\Migrations\DatabaseMigrationRepository; use Illuminate\Database\Migrations\MigrationCreator; use Illuminate\Database\Migrations\Migrator; use Illuminate\Support\ServiceProvider; class MigrationServiceProvider extends ServiceProvider implements DeferrableProvider { /** * The commands to be registered. * * @var array */ protected $commands = [ 'Migrate' => MigrateCommand::class, 'MigrateFresh' => FreshCommand::class, 'MigrateInstall' => InstallCommand::class, 'MigrateRefresh' => RefreshCommand::class, 'MigrateReset' => ResetCommand::class, 'MigrateRollback' => RollbackCommand::class, 'MigrateStatus' => StatusCommand::class, 'MigrateMake' => MigrateMakeCommand::class, ]; /** * Register the service provider. * * @return void */ public function register() { $this->registerRepository(); $this->registerMigrator(); $this->registerCreator(); $this->registerCommands($this->commands); } /** * Register the migration repository service. * * @return void */ protected function registerRepository() { $this->app->singleton('migration.repository', function ($app) { $table = $app['config']['database.migrations']; return new DatabaseMigrationRepository($app['db'], $table); }); } /** * Register the migrator service. * * @return void */ protected function registerMigrator() { // The migrator is responsible for actually running and rollback the migration // files in the application. We'll pass in our database connection resolver // so the migrator can resolve any of these connections when it needs to. $this->app->singleton('migrator', function ($app) { $repository = $app['migration.repository']; return new Migrator($repository, $app['db'], $app['files'], $app['events']); }); } /** * Register the migration creator. * * @return void */ protected function registerCreator() { $this->app->singleton('migration.creator', function ($app) { return new MigrationCreator($app['files'], $app->basePath('stubs')); }); } /** * Register the given commands. * * @param array $commands * @return void */ protected function registerCommands(array $commands) { foreach (array_keys($commands) as $command) { $this->{"register{$command}Command"}(); } $this->commands(array_values($commands)); } /** * Register the command. * * @return void */ protected function registerMigrateCommand() { $this->app->singleton(MigrateCommand::class, function ($app) { return new MigrateCommand($app['migrator'], $app[Dispatcher::class]); }); } /** * Register the command. * * @return void */ protected function registerMigrateFreshCommand() { $this->app->singleton(FreshCommand::class); } /** * Register the command. * * @return void */ protected function registerMigrateInstallCommand() { $this->app->singleton(InstallCommand::class, function ($app) { return new InstallCommand($app['migration.repository']); }); } /** * Register the command. * * @return void */ protected function registerMigrateMakeCommand() { $this->app->singleton(MigrateMakeCommand::class, function ($app) { // Once we have the migration creator registered, we will create the command // and inject the creator. The creator is responsible for the actual file // creation of the migrations, and may be extended by these developers. $creator = $app['migration.creator']; $composer = $app['composer']; return new MigrateMakeCommand($creator, $composer); }); } /** * Register the command. * * @return void */ protected function registerMigrateRefreshCommand() { $this->app->singleton(RefreshCommand::class); } /** * Register the command. * * @return void */ protected function registerMigrateResetCommand() { $this->app->singleton(ResetCommand::class, function ($app) { return new ResetCommand($app['migrator']); }); } /** * Register the command. * * @return void */ protected function registerMigrateRollbackCommand() { $this->app->singleton(RollbackCommand::class, function ($app) { return new RollbackCommand($app['migrator']); }); } /** * Register the command. * * @return void */ protected function registerMigrateStatusCommand() { $this->app->singleton(StatusCommand::class, function ($app) { return new StatusCommand($app['migrator']); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return array_merge([ 'migrator', 'migration.repository', 'migration.creator', ], array_values($this->commands)); } } src/Illuminate/Database/Concerns/ParsesSearchPath.php 0000644 00000001102 15107327037 0016624 0 ustar 00 <?php namespace Illuminate\Database\Concerns; trait ParsesSearchPath { /** * Parse the Postgres "search_path" configuration value into an array. * * @param string|array|null $searchPath * @return array */ protected function parseSearchPath($searchPath) { if (is_string($searchPath)) { preg_match_all('/[^\s,"\']+/', $searchPath, $matches); $searchPath = $matches[0]; } return array_map(function ($schema) { return trim($schema, '\'"'); }, $searchPath ?? []); } } src/Illuminate/Database/Concerns/CompilesJsonPaths.php 0000644 00000003007 15107327037 0017037 0 ustar 00 <?php namespace Illuminate\Database\Concerns; use Illuminate\Support\Str; trait CompilesJsonPaths { /** * Split the given JSON selector into the field and the optional path and wrap them separately. * * @param string $column * @return array */ protected function wrapJsonFieldAndPath($column) { $parts = explode('->', $column, 2); $field = $this->wrap($parts[0]); $path = count($parts) > 1 ? ', '.$this->wrapJsonPath($parts[1], '->') : ''; return [$field, $path]; } /** * Wrap the given JSON path. * * @param string $value * @param string $delimiter * @return string */ protected function wrapJsonPath($value, $delimiter = '->') { $value = preg_replace("/([\\\\]+)?\\'/", "''", $value); $jsonPath = collect(explode($delimiter, $value)) ->map(fn ($segment) => $this->wrapJsonPathSegment($segment)) ->join('.'); return "'$".(str_starts_with($jsonPath, '[') ? '' : '.').$jsonPath."'"; } /** * Wrap the given JSON path segment. * * @param string $segment * @return string */ protected function wrapJsonPathSegment($segment) { if (preg_match('/(\[[^\]]+\])+$/', $segment, $parts)) { $key = Str::beforeLast($segment, $parts[0]); if (! empty($key)) { return '"'.$key.'"'.$parts[0]; } return $parts[0]; } return '"'.$segment.'"'; } } src/Illuminate/Database/Concerns/BuildsQueries.php 0000644 00000043161 15107327037 0016217 0 ustar 00 <?php namespace Illuminate\Database\Concerns; use Illuminate\Container\Container; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\MultipleRecordsFoundException; use Illuminate\Database\Query\Expression; use Illuminate\Database\RecordsNotFoundException; use Illuminate\Pagination\Cursor; use Illuminate\Pagination\CursorPaginator; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; use Illuminate\Support\Collection; use Illuminate\Support\LazyCollection; use Illuminate\Support\Str; use Illuminate\Support\Traits\Conditionable; use InvalidArgumentException; use RuntimeException; trait BuildsQueries { use Conditionable; /** * Chunk the results of the query. * * @param int $count * @param callable $callback * @return bool */ public function chunk($count, callable $callback) { $this->enforceOrderBy(); $page = 1; do { // We'll execute the query for the given page and get the results. If there are // no results we can just break and return from here. When there are results // we will call the callback with the current chunk of these results here. $results = $this->forPage($page, $count)->get(); $countResults = $results->count(); if ($countResults == 0) { break; } // On each chunk result set, we will pass them to the callback and then let the // developer take care of everything within the callback, which allows us to // keep the memory low for spinning through large result sets for working. if ($callback($results, $page) === false) { return false; } unset($results); $page++; } while ($countResults == $count); return true; } /** * Run a map over each item while chunking. * * @param callable $callback * @param int $count * @return \Illuminate\Support\Collection */ public function chunkMap(callable $callback, $count = 1000) { $collection = Collection::make(); $this->chunk($count, function ($items) use ($collection, $callback) { $items->each(function ($item) use ($collection, $callback) { $collection->push($callback($item)); }); }); return $collection; } /** * Execute a callback over each item while chunking. * * @param callable $callback * @param int $count * @return bool * * @throws \RuntimeException */ public function each(callable $callback, $count = 1000) { return $this->chunk($count, function ($results) use ($callback) { foreach ($results as $key => $value) { if ($callback($value, $key) === false) { return false; } } }); } /** * Chunk the results of a query by comparing IDs. * * @param int $count * @param callable $callback * @param string|null $column * @param string|null $alias * @return bool */ public function chunkById($count, callable $callback, $column = null, $alias = null) { return $this->orderedChunkById($count, $callback, $column, $alias); } /** * Chunk the results of a query by comparing IDs in descending order. * * @param int $count * @param callable $callback * @param string|null $column * @param string|null $alias * @return bool */ public function chunkByIdDesc($count, callable $callback, $column = null, $alias = null) { return $this->orderedChunkById($count, $callback, $column, $alias, descending: true); } /** * Chunk the results of a query by comparing IDs in a given order. * * @param int $count * @param callable $callback * @param string|null $column * @param string|null $alias * @param bool $descending * @return bool */ public function orderedChunkById($count, callable $callback, $column = null, $alias = null, $descending = false) { $column ??= $this->defaultKeyName(); $alias ??= $column; $lastId = null; $page = 1; do { $clone = clone $this; // We'll execute the query for the given page and get the results. If there are // no results we can just break and return from here. When there are results // we will call the callback with the current chunk of these results here. if ($descending) { $results = $clone->forPageBeforeId($count, $lastId, $column)->get(); } else { $results = $clone->forPageAfterId($count, $lastId, $column)->get(); } $countResults = $results->count(); if ($countResults == 0) { break; } // On each chunk result set, we will pass them to the callback and then let the // developer take care of everything within the callback, which allows us to // keep the memory low for spinning through large result sets for working. if ($callback($results, $page) === false) { return false; } $lastId = data_get($results->last(), $alias); if ($lastId === null) { throw new RuntimeException("The chunkById operation was aborted because the [{$alias}] column is not present in the query result."); } unset($results); $page++; } while ($countResults == $count); return true; } /** * Execute a callback over each item while chunking by ID. * * @param callable $callback * @param int $count * @param string|null $column * @param string|null $alias * @return bool */ public function eachById(callable $callback, $count = 1000, $column = null, $alias = null) { return $this->chunkById($count, function ($results, $page) use ($callback, $count) { foreach ($results as $key => $value) { if ($callback($value, (($page - 1) * $count) + $key) === false) { return false; } } }, $column, $alias); } /** * Query lazily, by chunks of the given size. * * @param int $chunkSize * @return \Illuminate\Support\LazyCollection * * @throws \InvalidArgumentException */ public function lazy($chunkSize = 1000) { if ($chunkSize < 1) { throw new InvalidArgumentException('The chunk size should be at least 1'); } $this->enforceOrderBy(); return LazyCollection::make(function () use ($chunkSize) { $page = 1; while (true) { $results = $this->forPage($page++, $chunkSize)->get(); foreach ($results as $result) { yield $result; } if ($results->count() < $chunkSize) { return; } } }); } /** * Query lazily, by chunking the results of a query by comparing IDs. * * @param int $chunkSize * @param string|null $column * @param string|null $alias * @return \Illuminate\Support\LazyCollection * * @throws \InvalidArgumentException */ public function lazyById($chunkSize = 1000, $column = null, $alias = null) { return $this->orderedLazyById($chunkSize, $column, $alias); } /** * Query lazily, by chunking the results of a query by comparing IDs in descending order. * * @param int $chunkSize * @param string|null $column * @param string|null $alias * @return \Illuminate\Support\LazyCollection * * @throws \InvalidArgumentException */ public function lazyByIdDesc($chunkSize = 1000, $column = null, $alias = null) { return $this->orderedLazyById($chunkSize, $column, $alias, true); } /** * Query lazily, by chunking the results of a query by comparing IDs in a given order. * * @param int $chunkSize * @param string|null $column * @param string|null $alias * @param bool $descending * @return \Illuminate\Support\LazyCollection * * @throws \InvalidArgumentException */ protected function orderedLazyById($chunkSize = 1000, $column = null, $alias = null, $descending = false) { if ($chunkSize < 1) { throw new InvalidArgumentException('The chunk size should be at least 1'); } $column ??= $this->defaultKeyName(); $alias ??= $column; return LazyCollection::make(function () use ($chunkSize, $column, $alias, $descending) { $lastId = null; while (true) { $clone = clone $this; if ($descending) { $results = $clone->forPageBeforeId($chunkSize, $lastId, $column)->get(); } else { $results = $clone->forPageAfterId($chunkSize, $lastId, $column)->get(); } foreach ($results as $result) { yield $result; } if ($results->count() < $chunkSize) { return; } $lastId = $results->last()->{$alias}; if ($lastId === null) { throw new RuntimeException("The lazyById operation was aborted because the [{$alias}] column is not present in the query result."); } } }); } /** * Execute the query and get the first result. * * @param array|string $columns * @return \Illuminate\Database\Eloquent\Model|object|static|null */ public function first($columns = ['*']) { return $this->take(1)->get($columns)->first(); } /** * Execute the query and get the first result if it's the sole matching record. * * @param array|string $columns * @return \Illuminate\Database\Eloquent\Model|object|static|null * * @throws \Illuminate\Database\RecordsNotFoundException * @throws \Illuminate\Database\MultipleRecordsFoundException */ public function sole($columns = ['*']) { $result = $this->take(2)->get($columns); $count = $result->count(); if ($count === 0) { throw new RecordsNotFoundException; } if ($count > 1) { throw new MultipleRecordsFoundException($count); } return $result->first(); } /** * Paginate the given query using a cursor paginator. * * @param int $perPage * @param array|string $columns * @param string $cursorName * @param \Illuminate\Pagination\Cursor|string|null $cursor * @return \Illuminate\Contracts\Pagination\CursorPaginator */ protected function paginateUsingCursor($perPage, $columns = ['*'], $cursorName = 'cursor', $cursor = null) { if (! $cursor instanceof Cursor) { $cursor = is_string($cursor) ? Cursor::fromEncoded($cursor) : CursorPaginator::resolveCurrentCursor($cursorName, $cursor); } $orders = $this->ensureOrderForCursorPagination(! is_null($cursor) && $cursor->pointsToPreviousItems()); if (! is_null($cursor)) { $addCursorConditions = function (self $builder, $previousColumn, $i) use (&$addCursorConditions, $cursor, $orders) { $unionBuilders = isset($builder->unions) ? collect($builder->unions)->pluck('query') : collect(); if (! is_null($previousColumn)) { $originalColumn = $this->getOriginalColumnNameForCursorPagination($this, $previousColumn); $builder->where( Str::contains($originalColumn, ['(', ')']) ? new Expression($originalColumn) : $originalColumn, '=', $cursor->parameter($previousColumn) ); $unionBuilders->each(function ($unionBuilder) use ($previousColumn, $cursor) { $unionBuilder->where( $this->getOriginalColumnNameForCursorPagination($this, $previousColumn), '=', $cursor->parameter($previousColumn) ); $this->addBinding($unionBuilder->getRawBindings()['where'], 'union'); }); } $builder->where(function (self $builder) use ($addCursorConditions, $cursor, $orders, $i, $unionBuilders) { ['column' => $column, 'direction' => $direction] = $orders[$i]; $originalColumn = $this->getOriginalColumnNameForCursorPagination($this, $column); $builder->where( Str::contains($originalColumn, ['(', ')']) ? new Expression($originalColumn) : $originalColumn, $direction === 'asc' ? '>' : '<', $cursor->parameter($column) ); if ($i < $orders->count() - 1) { $builder->orWhere(function (self $builder) use ($addCursorConditions, $column, $i) { $addCursorConditions($builder, $column, $i + 1); }); } $unionBuilders->each(function ($unionBuilder) use ($column, $direction, $cursor, $i, $orders, $addCursorConditions) { $unionBuilder->where(function ($unionBuilder) use ($column, $direction, $cursor, $i, $orders, $addCursorConditions) { $unionBuilder->where( $this->getOriginalColumnNameForCursorPagination($this, $column), $direction === 'asc' ? '>' : '<', $cursor->parameter($column) ); if ($i < $orders->count() - 1) { $unionBuilder->orWhere(function (self $builder) use ($addCursorConditions, $column, $i) { $addCursorConditions($builder, $column, $i + 1); }); } $this->addBinding($unionBuilder->getRawBindings()['where'], 'union'); }); }); }); }; $addCursorConditions($this, null, 0); } $this->limit($perPage + 1); return $this->cursorPaginator($this->get($columns), $perPage, $cursor, [ 'path' => Paginator::resolveCurrentPath(), 'cursorName' => $cursorName, 'parameters' => $orders->pluck('column')->toArray(), ]); } /** * Get the original column name of the given column, without any aliasing. * * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $builder * @param string $parameter * @return string */ protected function getOriginalColumnNameForCursorPagination($builder, string $parameter) { $columns = $builder instanceof Builder ? $builder->getQuery()->getColumns() : $builder->getColumns(); if (! is_null($columns)) { foreach ($columns as $column) { if (($position = strripos($column, ' as ')) !== false) { $original = substr($column, 0, $position); $alias = substr($column, $position + 4); if ($parameter === $alias || $builder->getGrammar()->wrap($parameter) === $alias) { return $original; } } } } return $parameter; } /** * Create a new length-aware paginator instance. * * @param \Illuminate\Support\Collection $items * @param int $total * @param int $perPage * @param int $currentPage * @param array $options * @return \Illuminate\Pagination\LengthAwarePaginator */ protected function paginator($items, $total, $perPage, $currentPage, $options) { return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact( 'items', 'total', 'perPage', 'currentPage', 'options' )); } /** * Create a new simple paginator instance. * * @param \Illuminate\Support\Collection $items * @param int $perPage * @param int $currentPage * @param array $options * @return \Illuminate\Pagination\Paginator */ protected function simplePaginator($items, $perPage, $currentPage, $options) { return Container::getInstance()->makeWith(Paginator::class, compact( 'items', 'perPage', 'currentPage', 'options' )); } /** * Create a new cursor paginator instance. * * @param \Illuminate\Support\Collection $items * @param int $perPage * @param \Illuminate\Pagination\Cursor $cursor * @param array $options * @return \Illuminate\Pagination\CursorPaginator */ protected function cursorPaginator($items, $perPage, $cursor, $options) { return Container::getInstance()->makeWith(CursorPaginator::class, compact( 'items', 'perPage', 'cursor', 'options' )); } /** * Pass the query to a given callback. * * @param callable $callback * @return $this */ public function tap($callback) { $callback($this); return $this; } } src/Illuminate/Database/Concerns/ExplainsQueries.php 0000644 00000000704 15107327037 0016554 0 ustar 00 <?php namespace Illuminate\Database\Concerns; use Illuminate\Support\Collection; trait ExplainsQueries { /** * Explains the query. * * @return \Illuminate\Support\Collection */ public function explain() { $sql = $this->toSql(); $bindings = $this->getBindings(); $explanation = $this->getConnection()->select('EXPLAIN '.$sql, $bindings); return new Collection($explanation); } } src/Illuminate/Database/Concerns/ManagesTransactions.php 0000644 00000022751 15107327037 0017405 0 ustar 00 <?php namespace Illuminate\Database\Concerns; use Closure; use Illuminate\Database\DeadlockException; use RuntimeException; use Throwable; trait ManagesTransactions { /** * Execute a Closure within a transaction. * * @param \Closure $callback * @param int $attempts * @return mixed * * @throws \Throwable */ public function transaction(Closure $callback, $attempts = 1) { for ($currentAttempt = 1; $currentAttempt <= $attempts; $currentAttempt++) { $this->beginTransaction(); // We'll simply execute the given callback within a try / catch block and if we // catch any exception we can rollback this transaction so that none of this // gets actually persisted to a database or stored in a permanent fashion. try { $callbackResult = $callback($this); } // If we catch an exception we'll rollback this transaction and try again if we // are not out of attempts. If we are out of attempts we will just throw the // exception back out, and let the developer handle an uncaught exception. catch (Throwable $e) { $this->handleTransactionException( $e, $currentAttempt, $attempts ); continue; } try { if ($this->transactions == 1) { $this->fireConnectionEvent('committing'); $this->getPdo()->commit(); } $this->transactions = max(0, $this->transactions - 1); if ($this->afterCommitCallbacksShouldBeExecuted()) { $this->transactionsManager?->commit($this->getName()); } } catch (Throwable $e) { $this->handleCommitTransactionException( $e, $currentAttempt, $attempts ); continue; } $this->fireConnectionEvent('committed'); return $callbackResult; } } /** * Handle an exception encountered when running a transacted statement. * * @param \Throwable $e * @param int $currentAttempt * @param int $maxAttempts * @return void * * @throws \Throwable */ protected function handleTransactionException(Throwable $e, $currentAttempt, $maxAttempts) { // On a deadlock, MySQL rolls back the entire transaction so we can't just // retry the query. We have to throw this exception all the way out and // let the developer handle it in another way. We will decrement too. if ($this->causedByConcurrencyError($e) && $this->transactions > 1) { $this->transactions--; $this->transactionsManager?->rollback( $this->getName(), $this->transactions ); throw new DeadlockException($e->getMessage(), is_int($e->getCode()) ? $e->getCode() : 0, $e); } // If there was an exception we will rollback this transaction and then we // can check if we have exceeded the maximum attempt count for this and // if we haven't we will return and try this query again in our loop. $this->rollBack(); if ($this->causedByConcurrencyError($e) && $currentAttempt < $maxAttempts) { return; } throw $e; } /** * Start a new database transaction. * * @return void * * @throws \Throwable */ public function beginTransaction() { $this->createTransaction(); $this->transactions++; $this->transactionsManager?->begin( $this->getName(), $this->transactions ); $this->fireConnectionEvent('beganTransaction'); } /** * Create a transaction within the database. * * @return void * * @throws \Throwable */ protected function createTransaction() { if ($this->transactions == 0) { $this->reconnectIfMissingConnection(); try { $this->getPdo()->beginTransaction(); } catch (Throwable $e) { $this->handleBeginTransactionException($e); } } elseif ($this->transactions >= 1 && $this->queryGrammar->supportsSavepoints()) { $this->createSavepoint(); } } /** * Create a save point within the database. * * @return void * * @throws \Throwable */ protected function createSavepoint() { $this->getPdo()->exec( $this->queryGrammar->compileSavepoint('trans'.($this->transactions + 1)) ); } /** * Handle an exception from a transaction beginning. * * @param \Throwable $e * @return void * * @throws \Throwable */ protected function handleBeginTransactionException(Throwable $e) { if ($this->causedByLostConnection($e)) { $this->reconnect(); $this->getPdo()->beginTransaction(); } else { throw $e; } } /** * Commit the active database transaction. * * @return void * * @throws \Throwable */ public function commit() { if ($this->transactionLevel() == 1) { $this->fireConnectionEvent('committing'); $this->getPdo()->commit(); } $this->transactions = max(0, $this->transactions - 1); if ($this->afterCommitCallbacksShouldBeExecuted()) { $this->transactionsManager?->commit($this->getName()); } $this->fireConnectionEvent('committed'); } /** * Determine if after commit callbacks should be executed. * * @return bool */ protected function afterCommitCallbacksShouldBeExecuted() { return $this->transactionsManager?->afterCommitCallbacksShouldBeExecuted($this->transactions) || $this->transactions == 0; } /** * Handle an exception encountered when committing a transaction. * * @param \Throwable $e * @param int $currentAttempt * @param int $maxAttempts * @return void * * @throws \Throwable */ protected function handleCommitTransactionException(Throwable $e, $currentAttempt, $maxAttempts) { $this->transactions = max(0, $this->transactions - 1); if ($this->causedByConcurrencyError($e) && $currentAttempt < $maxAttempts) { return; } if ($this->causedByLostConnection($e)) { $this->transactions = 0; } throw $e; } /** * Rollback the active database transaction. * * @param int|null $toLevel * @return void * * @throws \Throwable */ public function rollBack($toLevel = null) { // We allow developers to rollback to a certain transaction level. We will verify // that this given transaction level is valid before attempting to rollback to // that level. If it's not we will just return out and not attempt anything. $toLevel = is_null($toLevel) ? $this->transactions - 1 : $toLevel; if ($toLevel < 0 || $toLevel >= $this->transactions) { return; } // Next, we will actually perform this rollback within this database and fire the // rollback event. We will also set the current transaction level to the given // level that was passed into this method so it will be right from here out. try { $this->performRollBack($toLevel); } catch (Throwable $e) { $this->handleRollBackException($e); } $this->transactions = $toLevel; $this->transactionsManager?->rollback( $this->getName(), $this->transactions ); $this->fireConnectionEvent('rollingBack'); } /** * Perform a rollback within the database. * * @param int $toLevel * @return void * * @throws \Throwable */ protected function performRollBack($toLevel) { if ($toLevel == 0) { $pdo = $this->getPdo(); if ($pdo->inTransaction()) { $pdo->rollBack(); } } elseif ($this->queryGrammar->supportsSavepoints()) { $this->getPdo()->exec( $this->queryGrammar->compileSavepointRollBack('trans'.($toLevel + 1)) ); } } /** * Handle an exception from a rollback. * * @param \Throwable $e * @return void * * @throws \Throwable */ protected function handleRollBackException(Throwable $e) { if ($this->causedByLostConnection($e)) { $this->transactions = 0; $this->transactionsManager?->rollback( $this->getName(), $this->transactions ); } throw $e; } /** * Get the number of active transactions. * * @return int */ public function transactionLevel() { return $this->transactions; } /** * Execute the callback after a transaction commits. * * @param callable $callback * @return void * * @throws \RuntimeException */ public function afterCommit($callback) { if ($this->transactionsManager) { return $this->transactionsManager->addCallback($callback); } throw new RuntimeException('Transactions Manager has not been set.'); } } src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php 0000644 00000072414 15107327037 0020076 0 ustar 00 <?php namespace Illuminate\Database\Schema\Grammars; use Illuminate\Database\Connection; use Illuminate\Database\Query\Expression; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Fluent; class SqlServerGrammar extends Grammar { /** * If this Grammar supports schema changes wrapped in a transaction. * * @var bool */ protected $transactions = true; /** * The possible column modifiers. * * @var string[] */ protected $modifiers = ['Collate', 'Nullable', 'Default', 'Persisted', 'Increment']; /** * The columns available as serials. * * @var string[] */ protected $serials = ['tinyInteger', 'smallInteger', 'mediumInteger', 'integer', 'bigInteger']; /** * The commands to be executed outside of create or alter command. * * @var string[] */ protected $fluentCommands = ['Default']; /** * Compile a create database command. * * @param string $name * @param \Illuminate\Database\Connection $connection * @return string */ public function compileCreateDatabase($name, $connection) { return sprintf( 'create database %s', $this->wrapValue($name), ); } /** * Compile a drop database if exists command. * * @param string $name * @return string */ public function compileDropDatabaseIfExists($name) { return sprintf( 'drop database if exists %s', $this->wrapValue($name) ); } /** * Compile the query to determine if a table exists. * * @return string */ public function compileTableExists() { return "select * from sys.sysobjects where id = object_id(?) and xtype in ('U', 'V')"; } /** * Compile the query to determine the list of columns. * * @deprecated Will be removed in a future Laravel version. * * @param string $table * @return string */ public function compileColumnListing($table) { return "select name from sys.columns where object_id = object_id('$table')"; } /** * Compile the query to determine the columns. * * @param string $table * @return string */ public function compileColumns($table) { return sprintf( 'select col.name, type.name as type_name, ' .'col.max_length as length, col.precision as precision, col.scale as places, ' .'col.is_nullable as nullable, def.definition as [default], ' .'col.is_identity as autoincrement, col.collation_name as collation, ' .'cast(prop.value as nvarchar(max)) as comment ' .'from sys.columns as col ' .'join sys.types as type on col.user_type_id = type.user_type_id ' .'join sys.objects as obj on col.object_id = obj.object_id ' .'join sys.schemas as scm on obj.schema_id = scm.schema_id ' .'left join sys.default_constraints def on col.default_object_id = def.object_id and col.object_id = def.parent_object_id ' ."left join sys.extended_properties as prop on obj.object_id = prop.major_id and col.column_id = prop.minor_id and prop.name = 'MS_Description' " ."where obj.type = 'U' and obj.name = %s and scm.name = SCHEMA_NAME()", $this->quoteString($table), ); } /** * Compile a create table command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileCreate(Blueprint $blueprint, Fluent $command) { $columns = implode(', ', $this->getColumns($blueprint)); return 'create table '.$this->wrapTable($blueprint)." ($columns)"; } /** * Compile a column addition table command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileAdd(Blueprint $blueprint, Fluent $command) { return sprintf('alter table %s add %s', $this->wrapTable($blueprint), implode(', ', $this->getColumns($blueprint)) ); } /** * Compile a rename column command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return array|string */ public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection) { return $connection->usingNativeSchemaOperations() ? sprintf("sp_rename '%s', %s, 'COLUMN'", $this->wrap($blueprint->getTable().'.'.$command->from), $this->wrap($command->to) ) : parent::compileRenameColumn($blueprint, $command, $connection); } /** * Compile a change column command into a series of SQL statements. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return array|string * * @throws \RuntimeException */ public function compileChange(Blueprint $blueprint, Fluent $command, Connection $connection) { if (! $connection->usingNativeSchemaOperations()) { return parent::compileChange($blueprint, $command, $connection); } $changes = [$this->compileDropDefaultConstraint($blueprint, $command)]; foreach ($blueprint->getChangedColumns() as $column) { $sql = sprintf('alter table %s alter column %s %s', $this->wrapTable($blueprint), $this->wrap($column), $this->getType($column) ); foreach ($this->modifiers as $modifier) { if (method_exists($this, $method = "modify{$modifier}")) { $sql .= $this->{$method}($blueprint, $column); } } $changes[] = $sql; } return $changes; } /** * Compile a primary key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compilePrimary(Blueprint $blueprint, Fluent $command) { return sprintf('alter table %s add constraint %s primary key (%s)', $this->wrapTable($blueprint), $this->wrap($command->index), $this->columnize($command->columns) ); } /** * Compile a unique key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileUnique(Blueprint $blueprint, Fluent $command) { return sprintf('create unique index %s on %s (%s)', $this->wrap($command->index), $this->wrapTable($blueprint), $this->columnize($command->columns) ); } /** * Compile a plain index key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileIndex(Blueprint $blueprint, Fluent $command) { return sprintf('create index %s on %s (%s)', $this->wrap($command->index), $this->wrapTable($blueprint), $this->columnize($command->columns) ); } /** * Compile a spatial index key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileSpatialIndex(Blueprint $blueprint, Fluent $command) { return sprintf('create spatial index %s on %s (%s)', $this->wrap($command->index), $this->wrapTable($blueprint), $this->columnize($command->columns) ); } /** * Compile a default command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string|null */ public function compileDefault(Blueprint $blueprint, Fluent $command) { if ($command->column->change && ! is_null($command->column->default)) { return sprintf('alter table %s add default %s for %s', $this->wrapTable($blueprint), $this->getDefaultValue($command->column->default), $this->wrap($command->column) ); } } /** * Compile a drop table command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDrop(Blueprint $blueprint, Fluent $command) { return 'drop table '.$this->wrapTable($blueprint); } /** * Compile a drop table (if exists) command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropIfExists(Blueprint $blueprint, Fluent $command) { return sprintf('if exists (select * from sys.sysobjects where id = object_id(%s, \'U\')) drop table %s', "'".str_replace("'", "''", $this->getTablePrefix().$blueprint->getTable())."'", $this->wrapTable($blueprint) ); } /** * Compile the SQL needed to drop all tables. * * @return string */ public function compileDropAllTables() { return "EXEC sp_msforeachtable 'DROP TABLE ?'"; } /** * Compile a drop column command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropColumn(Blueprint $blueprint, Fluent $command) { $columns = $this->wrapArray($command->columns); $dropExistingConstraintsSql = $this->compileDropDefaultConstraint($blueprint, $command).';'; return $dropExistingConstraintsSql.'alter table '.$this->wrapTable($blueprint).' drop column '.implode(', ', $columns); } /** * Compile a drop default constraint command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropDefaultConstraint(Blueprint $blueprint, Fluent $command) { $columns = $command->name === 'change' ? "'".collect($blueprint->getChangedColumns())->pluck('name')->implode("','")."'" : "'".implode("','", $command->columns)."'"; $tableName = $this->getTablePrefix().$blueprint->getTable(); $sql = "DECLARE @sql NVARCHAR(MAX) = '';"; $sql .= "SELECT @sql += 'ALTER TABLE [dbo].[{$tableName}] DROP CONSTRAINT ' + OBJECT_NAME([default_object_id]) + ';' "; $sql .= 'FROM sys.columns '; $sql .= "WHERE [object_id] = OBJECT_ID('[dbo].[{$tableName}]') AND [name] in ({$columns}) AND [default_object_id] <> 0;"; $sql .= 'EXEC(@sql)'; return $sql; } /** * Compile a drop primary key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropPrimary(Blueprint $blueprint, Fluent $command) { $index = $this->wrap($command->index); return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}"; } /** * Compile a drop unique key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropUnique(Blueprint $blueprint, Fluent $command) { $index = $this->wrap($command->index); return "drop index {$index} on {$this->wrapTable($blueprint)}"; } /** * Compile a drop index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropIndex(Blueprint $blueprint, Fluent $command) { $index = $this->wrap($command->index); return "drop index {$index} on {$this->wrapTable($blueprint)}"; } /** * Compile a drop spatial index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropSpatialIndex(Blueprint $blueprint, Fluent $command) { return $this->compileDropIndex($blueprint, $command); } /** * Compile a drop foreign key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropForeign(Blueprint $blueprint, Fluent $command) { $index = $this->wrap($command->index); return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}"; } /** * Compile a rename table command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileRename(Blueprint $blueprint, Fluent $command) { $from = $this->wrapTable($blueprint); return "sp_rename {$from}, ".$this->wrapTable($command->to); } /** * Compile a rename index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileRenameIndex(Blueprint $blueprint, Fluent $command) { return sprintf("sp_rename N'%s', %s, N'INDEX'", $this->wrap($blueprint->getTable().'.'.$command->from), $this->wrap($command->to) ); } /** * Compile the command to enable foreign key constraints. * * @return string */ public function compileEnableForeignKeyConstraints() { return 'EXEC sp_msforeachtable @command1="print \'?\'", @command2="ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all";'; } /** * Compile the command to disable foreign key constraints. * * @return string */ public function compileDisableForeignKeyConstraints() { return 'EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all";'; } /** * Compile the command to drop all foreign keys. * * @return string */ public function compileDropAllForeignKeys() { return "DECLARE @sql NVARCHAR(MAX) = N''; SELECT @sql += 'ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id)) + '.' + + QUOTENAME(OBJECT_NAME(parent_object_id)) + ' DROP CONSTRAINT ' + QUOTENAME(name) + ';' FROM sys.foreign_keys; EXEC sp_executesql @sql;"; } /** * Compile the command to drop all views. * * @return string */ public function compileDropAllViews() { return "DECLARE @sql NVARCHAR(MAX) = N''; SELECT @sql += 'DROP VIEW ' + QUOTENAME(OBJECT_SCHEMA_NAME(object_id)) + '.' + QUOTENAME(name) + ';' FROM sys.views; EXEC sp_executesql @sql;"; } /** * Compile the SQL needed to retrieve all table names. * * @return string */ public function compileGetAllTables() { return "select name, type from sys.tables where type = 'U'"; } /** * Compile the SQL needed to retrieve all view names. * * @return string */ public function compileGetAllViews() { return "select name, type from sys.objects where type = 'V'"; } /** * Create the column definition for a char type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeChar(Fluent $column) { return "nchar({$column->length})"; } /** * Create the column definition for a string type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeString(Fluent $column) { return "nvarchar({$column->length})"; } /** * Create the column definition for a tiny text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTinyText(Fluent $column) { return 'nvarchar(255)'; } /** * Create the column definition for a text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeText(Fluent $column) { return 'nvarchar(max)'; } /** * Create the column definition for a medium text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMediumText(Fluent $column) { return 'nvarchar(max)'; } /** * Create the column definition for a long text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeLongText(Fluent $column) { return 'nvarchar(max)'; } /** * Create the column definition for an integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeInteger(Fluent $column) { return 'int'; } /** * Create the column definition for a big integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeBigInteger(Fluent $column) { return 'bigint'; } /** * Create the column definition for a medium integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMediumInteger(Fluent $column) { return 'int'; } /** * Create the column definition for a tiny integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTinyInteger(Fluent $column) { return 'tinyint'; } /** * Create the column definition for a small integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeSmallInteger(Fluent $column) { return 'smallint'; } /** * Create the column definition for a float type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeFloat(Fluent $column) { return 'float'; } /** * Create the column definition for a double type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDouble(Fluent $column) { return 'float'; } /** * Create the column definition for a decimal type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDecimal(Fluent $column) { return "decimal({$column->total}, {$column->places})"; } /** * Create the column definition for a boolean type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeBoolean(Fluent $column) { return 'bit'; } /** * Create the column definition for an enumeration type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeEnum(Fluent $column) { return sprintf( 'nvarchar(255) check ("%s" in (%s))', $column->name, $this->quoteString($column->allowed) ); } /** * Create the column definition for a json type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeJson(Fluent $column) { return 'nvarchar(max)'; } /** * Create the column definition for a jsonb type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeJsonb(Fluent $column) { return 'nvarchar(max)'; } /** * Create the column definition for a date type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDate(Fluent $column) { return 'date'; } /** * Create the column definition for a date-time type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDateTime(Fluent $column) { return $this->typeTimestamp($column); } /** * Create the column definition for a date-time (with time zone) type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDateTimeTz(Fluent $column) { return $this->typeTimestampTz($column); } /** * Create the column definition for a time type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTime(Fluent $column) { return $column->precision ? "time($column->precision)" : 'time'; } /** * Create the column definition for a time (with time zone) type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTimeTz(Fluent $column) { return $this->typeTime($column); } /** * Create the column definition for a timestamp type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTimestamp(Fluent $column) { if ($column->useCurrent) { $column->default(new Expression('CURRENT_TIMESTAMP')); } return $column->precision ? "datetime2($column->precision)" : 'datetime'; } /** * Create the column definition for a timestamp (with time zone) type. * * @link https://docs.microsoft.com/en-us/sql/t-sql/data-types/datetimeoffset-transact-sql?view=sql-server-ver15 * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTimestampTz(Fluent $column) { if ($column->useCurrent) { $column->default(new Expression('CURRENT_TIMESTAMP')); } return $column->precision ? "datetimeoffset($column->precision)" : 'datetimeoffset'; } /** * Create the column definition for a year type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeYear(Fluent $column) { return $this->typeInteger($column); } /** * Create the column definition for a binary type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeBinary(Fluent $column) { return 'varbinary(max)'; } /** * Create the column definition for a uuid type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeUuid(Fluent $column) { return 'uniqueidentifier'; } /** * Create the column definition for an IP address type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeIpAddress(Fluent $column) { return 'nvarchar(45)'; } /** * Create the column definition for a MAC address type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMacAddress(Fluent $column) { return 'nvarchar(17)'; } /** * Create the column definition for a spatial Geometry type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeGeometry(Fluent $column) { return 'geography'; } /** * Create the column definition for a spatial Point type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typePoint(Fluent $column) { return 'geography'; } /** * Create the column definition for a spatial LineString type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeLineString(Fluent $column) { return 'geography'; } /** * Create the column definition for a spatial Polygon type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typePolygon(Fluent $column) { return 'geography'; } /** * Create the column definition for a spatial GeometryCollection type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeGeometryCollection(Fluent $column) { return 'geography'; } /** * Create the column definition for a spatial MultiPoint type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeMultiPoint(Fluent $column) { return 'geography'; } /** * Create the column definition for a spatial MultiLineString type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeMultiLineString(Fluent $column) { return 'geography'; } /** * Create the column definition for a spatial MultiPolygon type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeMultiPolygon(Fluent $column) { return 'geography'; } /** * Create the column definition for a generated, computed column type. * * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function typeComputed(Fluent $column) { return "as ({$column->expression})"; } /** * Get the SQL for a collation column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyCollate(Blueprint $blueprint, Fluent $column) { if (! is_null($column->collation)) { return ' collate '.$column->collation; } } /** * Get the SQL for a nullable column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyNullable(Blueprint $blueprint, Fluent $column) { if ($column->type !== 'computed') { return $column->nullable ? ' null' : ' not null'; } } /** * Get the SQL for a default column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyDefault(Blueprint $blueprint, Fluent $column) { if (! $column->change && ! is_null($column->default)) { return ' default '.$this->getDefaultValue($column->default); } } /** * Get the SQL for an auto-increment column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyIncrement(Blueprint $blueprint, Fluent $column) { if (! $column->change && in_array($column->type, $this->serials) && $column->autoIncrement) { return ' identity primary key'; } } /** * Get the SQL for a generated stored column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyPersisted(Blueprint $blueprint, Fluent $column) { if ($column->change) { if ($column->type === 'computed') { return $column->persisted ? ' add persisted' : ' drop persisted'; } return null; } if ($column->persisted) { return ' persisted'; } } /** * Wrap a table in keyword identifiers. * * @param \Illuminate\Database\Schema\Blueprint|\Illuminate\Contracts\Database\Query\Expression|string $table * @return string */ public function wrapTable($table) { if ($table instanceof Blueprint && $table->temporary) { $this->setTablePrefix('#'); } return parent::wrapTable($table); } /** * Quote the given string literal. * * @param string|array $value * @return string */ public function quoteString($value) { if (is_array($value)) { return implode(', ', array_map([$this, __FUNCTION__], $value)); } return "N'$value'"; } } src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php 0000644 00000106327 15107327037 0017757 0 ustar 00 <?php namespace Illuminate\Database\Schema\Grammars; use Illuminate\Database\Connection; use Illuminate\Database\Query\Expression; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Fluent; use LogicException; class PostgresGrammar extends Grammar { /** * If this Grammar supports schema changes wrapped in a transaction. * * @var bool */ protected $transactions = true; /** * The possible column modifiers. * * @var string[] */ protected $modifiers = ['Collate', 'Nullable', 'Default', 'VirtualAs', 'StoredAs', 'GeneratedAs', 'Increment']; /** * The columns available as serials. * * @var string[] */ protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger']; /** * The commands to be executed outside of create or alter command. * * @var string[] */ protected $fluentCommands = ['AutoIncrementStartingValues', 'Comment']; /** * Compile a create database command. * * @param string $name * @param \Illuminate\Database\Connection $connection * @return string */ public function compileCreateDatabase($name, $connection) { return sprintf( 'create database %s encoding %s', $this->wrapValue($name), $this->wrapValue($connection->getConfig('charset')), ); } /** * Compile a drop database if exists command. * * @param string $name * @return string */ public function compileDropDatabaseIfExists($name) { return sprintf( 'drop database if exists %s', $this->wrapValue($name) ); } /** * Compile the query to determine if a table exists. * * @return string */ public function compileTableExists() { return "select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'"; } /** * Compile the query to determine the list of columns. * * @deprecated Will be removed in a future Laravel version. * * @return string */ public function compileColumnListing() { return 'select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?'; } /** * Compile the query to determine the columns. * * @param string $database * @param string $schema * @param string $table * @return string */ public function compileColumns($database, $schema, $table) { return sprintf( 'select quote_ident(a.attname) as name, t.typname as type_name, format_type(a.atttypid, a.atttypmod) as type, ' .'(select tc.collcollate from pg_catalog.pg_collation tc where tc.oid = a.attcollation) as collation, ' .'not a.attnotnull as nullable, ' .'(select pg_get_expr(adbin, adrelid) from pg_attrdef where c.oid = pg_attrdef.adrelid and pg_attrdef.adnum = a.attnum) as default, ' .'(select pg_description.description from pg_description where pg_description.objoid = c.oid and a.attnum = pg_description.objsubid) as comment ' .'from pg_attribute a, pg_class c, pg_type t, pg_namespace n ' .'where c.relname = %s and n.nspname = %s and a.attnum > 0 and a.attrelid = c.oid and a.atttypid = t.oid and n.oid = c.relnamespace ' .'order by a.attnum', $this->quoteString($table), $this->quoteString($schema) ); } /** * Compile a create table command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileCreate(Blueprint $blueprint, Fluent $command) { return sprintf('%s table %s (%s)', $blueprint->temporary ? 'create temporary' : 'create', $this->wrapTable($blueprint), implode(', ', $this->getColumns($blueprint)) ); } /** * Compile a column addition command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileAdd(Blueprint $blueprint, Fluent $command) { return sprintf('alter table %s %s', $this->wrapTable($blueprint), implode(', ', $this->prefixArray('add column', $this->getColumns($blueprint))) ); } /** * Compile the auto-incrementing column starting values. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileAutoIncrementStartingValues(Blueprint $blueprint, Fluent $command) { if ($command->column->autoIncrement && $value = $command->column->get('startingValue', $command->column->get('from'))) { return 'alter sequence '.$blueprint->getTable().'_'.$command->column->name.'_seq restart with '.$value; } } /** * Compile a rename column command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return array|string */ public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection) { return $connection->usingNativeSchemaOperations() ? sprintf('alter table %s rename column %s to %s', $this->wrapTable($blueprint), $this->wrap($command->from), $this->wrap($command->to) ) : parent::compileRenameColumn($blueprint, $command, $connection); } /** * Compile a change column command into a series of SQL statements. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return array|string * * @throws \RuntimeException */ public function compileChange(Blueprint $blueprint, Fluent $command, Connection $connection) { if (! $connection->usingNativeSchemaOperations()) { return parent::compileChange($blueprint, $command, $connection); } $columns = []; foreach ($blueprint->getChangedColumns() as $column) { $changes = ['type '.$this->getType($column).$this->modifyCollate($blueprint, $column)]; foreach ($this->modifiers as $modifier) { if ($modifier === 'Collate') { continue; } if (method_exists($this, $method = "modify{$modifier}")) { $constraints = (array) $this->{$method}($blueprint, $column); foreach ($constraints as $constraint) { $changes[] = $constraint; } } } $columns[] = implode(', ', $this->prefixArray('alter column '.$this->wrap($column), $changes)); } return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns); } /** * Compile a primary key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compilePrimary(Blueprint $blueprint, Fluent $command) { $columns = $this->columnize($command->columns); return 'alter table '.$this->wrapTable($blueprint)." add primary key ({$columns})"; } /** * Compile a unique key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileUnique(Blueprint $blueprint, Fluent $command) { $sql = sprintf('alter table %s add constraint %s unique (%s)', $this->wrapTable($blueprint), $this->wrap($command->index), $this->columnize($command->columns) ); if (! is_null($command->deferrable)) { $sql .= $command->deferrable ? ' deferrable' : ' not deferrable'; } if ($command->deferrable && ! is_null($command->initiallyImmediate)) { $sql .= $command->initiallyImmediate ? ' initially immediate' : ' initially deferred'; } return $sql; } /** * Compile a plain index key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileIndex(Blueprint $blueprint, Fluent $command) { return sprintf('create index %s on %s%s (%s)', $this->wrap($command->index), $this->wrapTable($blueprint), $command->algorithm ? ' using '.$command->algorithm : '', $this->columnize($command->columns) ); } /** * Compile a fulltext index key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string * * @throws \RuntimeException */ public function compileFulltext(Blueprint $blueprint, Fluent $command) { $language = $command->language ?: 'english'; $columns = array_map(function ($column) use ($language) { return "to_tsvector({$this->quoteString($language)}, {$this->wrap($column)})"; }, $command->columns); return sprintf('create index %s on %s using gin ((%s))', $this->wrap($command->index), $this->wrapTable($blueprint), implode(' || ', $columns) ); } /** * Compile a spatial index key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileSpatialIndex(Blueprint $blueprint, Fluent $command) { $command->algorithm = 'gist'; return $this->compileIndex($blueprint, $command); } /** * Compile a foreign key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileForeign(Blueprint $blueprint, Fluent $command) { $sql = parent::compileForeign($blueprint, $command); if (! is_null($command->deferrable)) { $sql .= $command->deferrable ? ' deferrable' : ' not deferrable'; } if ($command->deferrable && ! is_null($command->initiallyImmediate)) { $sql .= $command->initiallyImmediate ? ' initially immediate' : ' initially deferred'; } if (! is_null($command->notValid)) { $sql .= ' not valid'; } return $sql; } /** * Compile a drop table command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDrop(Blueprint $blueprint, Fluent $command) { return 'drop table '.$this->wrapTable($blueprint); } /** * Compile a drop table (if exists) command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropIfExists(Blueprint $blueprint, Fluent $command) { return 'drop table if exists '.$this->wrapTable($blueprint); } /** * Compile the SQL needed to drop all tables. * * @param array $tables * @return string */ public function compileDropAllTables($tables) { return 'drop table '.implode(',', $this->escapeNames($tables)).' cascade'; } /** * Compile the SQL needed to drop all views. * * @param array $views * @return string */ public function compileDropAllViews($views) { return 'drop view '.implode(',', $this->escapeNames($views)).' cascade'; } /** * Compile the SQL needed to drop all types. * * @param array $types * @return string */ public function compileDropAllTypes($types) { return 'drop type '.implode(',', $this->escapeNames($types)).' cascade'; } /** * Compile the SQL needed to retrieve all table names. * * @param string|array $searchPath * @return string */ public function compileGetAllTables($searchPath) { return "select tablename, concat('\"', schemaname, '\".\"', tablename, '\"') as qualifiedname from pg_catalog.pg_tables where schemaname in ('".implode("','", (array) $searchPath)."')"; } /** * Compile the SQL needed to retrieve all view names. * * @param string|array $searchPath * @return string */ public function compileGetAllViews($searchPath) { return "select viewname, concat('\"', schemaname, '\".\"', viewname, '\"') as qualifiedname from pg_catalog.pg_views where schemaname in ('".implode("','", (array) $searchPath)."')"; } /** * Compile the SQL needed to retrieve all type names. * * @return string */ public function compileGetAllTypes() { return 'select distinct pg_type.typname from pg_type inner join pg_enum on pg_enum.enumtypid = pg_type.oid'; } /** * Compile a drop column command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropColumn(Blueprint $blueprint, Fluent $command) { $columns = $this->prefixArray('drop column', $this->wrapArray($command->columns)); return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns); } /** * Compile a drop primary key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropPrimary(Blueprint $blueprint, Fluent $command) { $index = $this->wrap("{$blueprint->getPrefix()}{$blueprint->getTable()}_pkey"); return 'alter table '.$this->wrapTable($blueprint)." drop constraint {$index}"; } /** * Compile a drop unique key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropUnique(Blueprint $blueprint, Fluent $command) { $index = $this->wrap($command->index); return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}"; } /** * Compile a drop index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropIndex(Blueprint $blueprint, Fluent $command) { return "drop index {$this->wrap($command->index)}"; } /** * Compile a drop fulltext index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropFullText(Blueprint $blueprint, Fluent $command) { return $this->compileDropIndex($blueprint, $command); } /** * Compile a drop spatial index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropSpatialIndex(Blueprint $blueprint, Fluent $command) { return $this->compileDropIndex($blueprint, $command); } /** * Compile a drop foreign key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropForeign(Blueprint $blueprint, Fluent $command) { $index = $this->wrap($command->index); return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}"; } /** * Compile a rename table command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileRename(Blueprint $blueprint, Fluent $command) { $from = $this->wrapTable($blueprint); return "alter table {$from} rename to ".$this->wrapTable($command->to); } /** * Compile a rename index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileRenameIndex(Blueprint $blueprint, Fluent $command) { return sprintf('alter index %s rename to %s', $this->wrap($command->from), $this->wrap($command->to) ); } /** * Compile the command to enable foreign key constraints. * * @return string */ public function compileEnableForeignKeyConstraints() { return 'SET CONSTRAINTS ALL IMMEDIATE;'; } /** * Compile the command to disable foreign key constraints. * * @return string */ public function compileDisableForeignKeyConstraints() { return 'SET CONSTRAINTS ALL DEFERRED;'; } /** * Compile a comment command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileComment(Blueprint $blueprint, Fluent $command) { if (! is_null($comment = $command->column->comment) || $command->column->change) { return sprintf('comment on column %s.%s is %s', $this->wrapTable($blueprint), $this->wrap($command->column->name), is_null($comment) ? 'NULL' : "'".str_replace("'", "''", $comment)."'" ); } } /** * Compile a table comment command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileTableComment(Blueprint $blueprint, Fluent $command) { return sprintf('comment on table %s is %s', $this->wrapTable($blueprint), "'".str_replace("'", "''", $command->comment)."'" ); } /** * Quote-escape the given tables, views, or types. * * @param array $names * @return array */ public function escapeNames($names) { return array_map(static function ($name) { return '"'.collect(explode('.', $name)) ->map(fn ($segment) => trim($segment, '\'"')) ->implode('"."').'"'; }, $names); } /** * Create the column definition for a char type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeChar(Fluent $column) { if ($column->length) { return "char({$column->length})"; } return 'char'; } /** * Create the column definition for a string type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeString(Fluent $column) { if ($column->length) { return "varchar({$column->length})"; } return 'varchar'; } /** * Create the column definition for a tiny text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTinyText(Fluent $column) { return 'varchar(255)'; } /** * Create the column definition for a text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeText(Fluent $column) { return 'text'; } /** * Create the column definition for a medium text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMediumText(Fluent $column) { return 'text'; } /** * Create the column definition for a long text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeLongText(Fluent $column) { return 'text'; } /** * Create the column definition for an integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeInteger(Fluent $column) { return $column->autoIncrement && is_null($column->generatedAs) ? 'serial' : 'integer'; } /** * Create the column definition for a big integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeBigInteger(Fluent $column) { return $column->autoIncrement && is_null($column->generatedAs) ? 'bigserial' : 'bigint'; } /** * Create the column definition for a medium integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMediumInteger(Fluent $column) { return $this->typeInteger($column); } /** * Create the column definition for a tiny integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTinyInteger(Fluent $column) { return $this->typeSmallInteger($column); } /** * Create the column definition for a small integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeSmallInteger(Fluent $column) { return $column->autoIncrement && is_null($column->generatedAs) ? 'smallserial' : 'smallint'; } /** * Create the column definition for a float type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeFloat(Fluent $column) { return $this->typeDouble($column); } /** * Create the column definition for a double type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDouble(Fluent $column) { return 'double precision'; } /** * Create the column definition for a real type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeReal(Fluent $column) { return 'real'; } /** * Create the column definition for a decimal type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDecimal(Fluent $column) { return "decimal({$column->total}, {$column->places})"; } /** * Create the column definition for a boolean type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeBoolean(Fluent $column) { return 'boolean'; } /** * Create the column definition for an enumeration type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeEnum(Fluent $column) { return sprintf( 'varchar(255) check ("%s" in (%s))', $column->name, $this->quoteString($column->allowed) ); } /** * Create the column definition for a json type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeJson(Fluent $column) { return 'json'; } /** * Create the column definition for a jsonb type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeJsonb(Fluent $column) { return 'jsonb'; } /** * Create the column definition for a date type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDate(Fluent $column) { return 'date'; } /** * Create the column definition for a date-time type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDateTime(Fluent $column) { return $this->typeTimestamp($column); } /** * Create the column definition for a date-time (with time zone) type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDateTimeTz(Fluent $column) { return $this->typeTimestampTz($column); } /** * Create the column definition for a time type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTime(Fluent $column) { return 'time'.(is_null($column->precision) ? '' : "($column->precision)").' without time zone'; } /** * Create the column definition for a time (with time zone) type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTimeTz(Fluent $column) { return 'time'.(is_null($column->precision) ? '' : "($column->precision)").' with time zone'; } /** * Create the column definition for a timestamp type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTimestamp(Fluent $column) { if ($column->useCurrent) { $column->default(new Expression('CURRENT_TIMESTAMP')); } return 'timestamp'.(is_null($column->precision) ? '' : "($column->precision)").' without time zone'; } /** * Create the column definition for a timestamp (with time zone) type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTimestampTz(Fluent $column) { if ($column->useCurrent) { $column->default(new Expression('CURRENT_TIMESTAMP')); } return 'timestamp'.(is_null($column->precision) ? '' : "($column->precision)").' with time zone'; } /** * Create the column definition for a year type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeYear(Fluent $column) { return $this->typeInteger($column); } /** * Create the column definition for a binary type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeBinary(Fluent $column) { return 'bytea'; } /** * Create the column definition for a uuid type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeUuid(Fluent $column) { return 'uuid'; } /** * Create the column definition for an IP address type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeIpAddress(Fluent $column) { return 'inet'; } /** * Create the column definition for a MAC address type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMacAddress(Fluent $column) { return 'macaddr'; } /** * Create the column definition for a spatial Geometry type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeGeometry(Fluent $column) { return $this->formatPostGisType('geometry', $column); } /** * Create the column definition for a spatial Point type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typePoint(Fluent $column) { return $this->formatPostGisType('point', $column); } /** * Create the column definition for a spatial LineString type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeLineString(Fluent $column) { return $this->formatPostGisType('linestring', $column); } /** * Create the column definition for a spatial Polygon type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typePolygon(Fluent $column) { return $this->formatPostGisType('polygon', $column); } /** * Create the column definition for a spatial GeometryCollection type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeGeometryCollection(Fluent $column) { return $this->formatPostGisType('geometrycollection', $column); } /** * Create the column definition for a spatial MultiPoint type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMultiPoint(Fluent $column) { return $this->formatPostGisType('multipoint', $column); } /** * Create the column definition for a spatial MultiLineString type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeMultiLineString(Fluent $column) { return $this->formatPostGisType('multilinestring', $column); } /** * Create the column definition for a spatial MultiPolygon type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMultiPolygon(Fluent $column) { return $this->formatPostGisType('multipolygon', $column); } /** * Create the column definition for a spatial MultiPolygonZ type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMultiPolygonZ(Fluent $column) { return $this->formatPostGisType('multipolygonz', $column); } /** * Format the column definition for a PostGIS spatial type. * * @param string $type * @param \Illuminate\Support\Fluent $column * @return string */ private function formatPostGisType($type, Fluent $column) { if ($column->isGeometry === null) { return sprintf('geography(%s, %s)', $type, $column->projection ?? '4326'); } if ($column->projection !== null) { return sprintf('geometry(%s, %s)', $type, $column->projection); } return "geometry({$type})"; } /** * Get the SQL for a collation column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyCollate(Blueprint $blueprint, Fluent $column) { if (! is_null($column->collation)) { return ' collate '.$this->wrapValue($column->collation); } } /** * Get the SQL for a nullable column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyNullable(Blueprint $blueprint, Fluent $column) { if ($column->change) { return $column->nullable ? 'drop not null' : 'set not null'; } return $column->nullable ? ' null' : ' not null'; } /** * Get the SQL for a default column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyDefault(Blueprint $blueprint, Fluent $column) { if ($column->change) { return is_null($column->default) ? 'drop default' : 'set default '.$this->getDefaultValue($column->default); } if (! is_null($column->default)) { return ' default '.$this->getDefaultValue($column->default); } } /** * Get the SQL for an auto-increment column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyIncrement(Blueprint $blueprint, Fluent $column) { if (! $column->change && (in_array($column->type, $this->serials) || ($column->generatedAs !== null)) && $column->autoIncrement) { return ' primary key'; } } /** * Get the SQL for a generated virtual column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column) { if ($column->change) { if (array_key_exists('virtualAs', $column->getAttributes())) { return is_null($column->virtualAs) ? 'drop expression if exists' : throw new LogicException('This database driver does not support modifying generated columns.'); } return null; } if (! is_null($column->virtualAs)) { return " generated always as ({$column->virtualAs})"; } } /** * Get the SQL for a generated stored column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyStoredAs(Blueprint $blueprint, Fluent $column) { if ($column->change) { if (array_key_exists('storedAs', $column->getAttributes())) { return is_null($column->storedAs) ? 'drop expression if exists' : throw new LogicException('This database driver does not support modifying generated columns.'); } return null; } if (! is_null($column->storedAs)) { return " generated always as ({$column->storedAs}) stored"; } } /** * Get the SQL for an identity column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|array|null */ protected function modifyGeneratedAs(Blueprint $blueprint, Fluent $column) { $sql = null; if (! is_null($column->generatedAs)) { $sql = sprintf( ' generated %s as identity%s', $column->always ? 'always' : 'by default', ! is_bool($column->generatedAs) && ! empty($column->generatedAs) ? " ({$column->generatedAs})" : '' ); } if ($column->change) { $changes = ['drop identity if exists']; if (! is_null($sql)) { $changes[] = 'add '.$sql; } return $changes; } return $sql; } } src/Illuminate/Database/Schema/Grammars/RenameColumn.php 0000644 00000005713 15107327037 0017224 0 ustar 00 <?php namespace Illuminate\Database\Schema\Grammars; use Doctrine\DBAL\Schema\AbstractSchemaManager as SchemaManager; use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\TableDiff; use Illuminate\Database\Connection; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Fluent; class RenameColumn { /** * Compile a rename column command. * * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return array */ public static function compile(Grammar $grammar, Blueprint $blueprint, Fluent $command, Connection $connection) { $schema = $connection->getDoctrineSchemaManager(); $databasePlatform = $connection->getDoctrineConnection()->getDatabasePlatform(); $databasePlatform->registerDoctrineTypeMapping('enum', 'string'); $column = $connection->getDoctrineColumn( $grammar->getTablePrefix().$blueprint->getTable(), $command->from ); return (array) $databasePlatform->getAlterTableSQL(static::getRenamedDiff( $grammar, $blueprint, $command, $column, $schema )); } /** * Get a new column instance with the new column name. * * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Doctrine\DBAL\Schema\Column $column * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema * @return \Doctrine\DBAL\Schema\TableDiff */ protected static function getRenamedDiff(Grammar $grammar, Blueprint $blueprint, Fluent $command, Column $column, SchemaManager $schema) { return static::setRenamedColumns( $grammar->getDoctrineTableDiff($blueprint, $schema), $command, $column ); } /** * Set the renamed columns on the table diff. * * @param \Doctrine\DBAL\Schema\TableDiff $tableDiff * @param \Illuminate\Support\Fluent $command * @param \Doctrine\DBAL\Schema\Column $column * @return \Doctrine\DBAL\Schema\TableDiff */ protected static function setRenamedColumns(TableDiff $tableDiff, Fluent $command, Column $column) { $tableDiff->renamedColumns = [ $command->from => new Column($command->to, $column->getType(), self::getWritableColumnOptions($column)), ]; return $tableDiff; } /** * Get the writable column options. * * @param \Doctrine\DBAL\Schema\Column $column * @return array */ private static function getWritableColumnOptions(Column $column) { return array_filter($column->toArray(), function (string $name) use ($column) { return method_exists($column, 'set'.$name); }, ARRAY_FILTER_USE_KEY); } } src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php 0000644 00000110214 15107327037 0017204 0 ustar 00 <?php namespace Illuminate\Database\Schema\Grammars; use Illuminate\Database\Connection; use Illuminate\Database\Query\Expression; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Fluent; use RuntimeException; class MySqlGrammar extends Grammar { /** * The possible column modifiers. * * @var string[] */ protected $modifiers = [ 'Unsigned', 'Charset', 'Collate', 'VirtualAs', 'StoredAs', 'Nullable', 'Srid', 'Default', 'OnUpdate', 'Invisible', 'Increment', 'Comment', 'After', 'First', ]; /** * The possible column serials. * * @var string[] */ protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger']; /** * The commands to be executed outside of create or alter command. * * @var string[] */ protected $fluentCommands = ['AutoIncrementStartingValues']; /** * Compile a create database command. * * @param string $name * @param \Illuminate\Database\Connection $connection * @return string */ public function compileCreateDatabase($name, $connection) { return sprintf( 'create database %s default character set %s default collate %s', $this->wrapValue($name), $this->wrapValue($connection->getConfig('charset')), $this->wrapValue($connection->getConfig('collation')), ); } /** * Compile a drop database if exists command. * * @param string $name * @return string */ public function compileDropDatabaseIfExists($name) { return sprintf( 'drop database if exists %s', $this->wrapValue($name) ); } /** * Compile the query to determine the list of tables. * * @return string */ public function compileTableExists() { return "select * from information_schema.tables where table_schema = ? and table_name = ? and table_type = 'BASE TABLE'"; } /** * Compile the query to determine the list of columns. * * @deprecated Will be removed in a future Laravel version. * * @return string */ public function compileColumnListing() { return 'select column_name as `column_name` from information_schema.columns where table_schema = ? and table_name = ?'; } /** * Compile the query to determine the columns. * * @param string $database * @param string $table * @return string */ public function compileColumns($database, $table) { return sprintf( 'select column_name as `name`, data_type as `type_name`, column_type as `type`, ' .'collation_name as `collation`, is_nullable as `nullable`, ' .'column_default as `default`, column_comment AS `comment`, extra as `extra` ' .'from information_schema.columns where table_schema = %s and table_name = %s ' .'order by ordinal_position asc', $this->quoteString($database), $this->quoteString($table) ); } /** * Compile a create table command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return string */ public function compileCreate(Blueprint $blueprint, Fluent $command, Connection $connection) { $sql = $this->compileCreateTable( $blueprint, $command, $connection ); // Once we have the primary SQL, we can add the encoding option to the SQL for // the table. Then, we can check if a storage engine has been supplied for // the table. If so, we will add the engine declaration to the SQL query. $sql = $this->compileCreateEncoding( $sql, $connection, $blueprint ); // Finally, we will append the engine configuration onto this SQL statement as // the final thing we do before returning this finished SQL. Once this gets // added the query will be ready to execute against the real connections. return $this->compileCreateEngine($sql, $connection, $blueprint); } /** * Create the main create table clause. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return string */ protected function compileCreateTable($blueprint, $command, $connection) { return sprintf('%s table %s (%s)', $blueprint->temporary ? 'create temporary' : 'create', $this->wrapTable($blueprint), implode(', ', $this->getColumns($blueprint)) ); } /** * Append the character set specifications to a command. * * @param string $sql * @param \Illuminate\Database\Connection $connection * @param \Illuminate\Database\Schema\Blueprint $blueprint * @return string */ protected function compileCreateEncoding($sql, Connection $connection, Blueprint $blueprint) { // First we will set the character set if one has been set on either the create // blueprint itself or on the root configuration for the connection that the // table is being created on. We will add these to the create table query. if (isset($blueprint->charset)) { $sql .= ' default character set '.$blueprint->charset; } elseif (! is_null($charset = $connection->getConfig('charset'))) { $sql .= ' default character set '.$charset; } // Next we will add the collation to the create table statement if one has been // added to either this create table blueprint or the configuration for this // connection that the query is targeting. We'll add it to this SQL query. if (isset($blueprint->collation)) { $sql .= " collate '{$blueprint->collation}'"; } elseif (! is_null($collation = $connection->getConfig('collation'))) { $sql .= " collate '{$collation}'"; } return $sql; } /** * Append the engine specifications to a command. * * @param string $sql * @param \Illuminate\Database\Connection $connection * @param \Illuminate\Database\Schema\Blueprint $blueprint * @return string */ protected function compileCreateEngine($sql, Connection $connection, Blueprint $blueprint) { if (isset($blueprint->engine)) { return $sql.' engine = '.$blueprint->engine; } elseif (! is_null($engine = $connection->getConfig('engine'))) { return $sql.' engine = '.$engine; } return $sql; } /** * Compile an add column command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileAdd(Blueprint $blueprint, Fluent $command) { $columns = $this->prefixArray('add', $this->getColumns($blueprint)); return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns); } /** * Compile the auto-incrementing column starting values. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileAutoIncrementStartingValues(Blueprint $blueprint, Fluent $command) { if ($command->column->autoIncrement && $value = $command->column->get('startingValue', $command->column->get('from'))) { return 'alter table '.$this->wrapTable($blueprint).' auto_increment = '.$value; } } /** * Compile a rename column command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return array|string */ public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection) { return $connection->usingNativeSchemaOperations() ? sprintf('alter table %s rename column %s to %s', $this->wrapTable($blueprint), $this->wrap($command->from), $this->wrap($command->to) ) : parent::compileRenameColumn($blueprint, $command, $connection); } /** * Compile a change column command into a series of SQL statements. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return array|string * * @throws \RuntimeException */ public function compileChange(Blueprint $blueprint, Fluent $command, Connection $connection) { if (! $connection->usingNativeSchemaOperations()) { return parent::compileChange($blueprint, $command, $connection); } $columns = []; foreach ($blueprint->getChangedColumns() as $column) { $sql = sprintf('%s %s%s %s', is_null($column->renameTo) ? 'modify' : 'change', $this->wrap($column), is_null($column->renameTo) ? '' : ' '.$this->wrap($column->renameTo), $this->getType($column) ); $columns[] = $this->addModifiers($sql, $blueprint, $column); } return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns); } /** * Compile a primary key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compilePrimary(Blueprint $blueprint, Fluent $command) { return sprintf('alter table %s add primary key %s(%s)', $this->wrapTable($blueprint), $command->algorithm ? 'using '.$command->algorithm : '', $this->columnize($command->columns) ); } /** * Compile a unique key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileUnique(Blueprint $blueprint, Fluent $command) { return $this->compileKey($blueprint, $command, 'unique'); } /** * Compile a plain index key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileIndex(Blueprint $blueprint, Fluent $command) { return $this->compileKey($blueprint, $command, 'index'); } /** * Compile a fulltext index key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileFullText(Blueprint $blueprint, Fluent $command) { return $this->compileKey($blueprint, $command, 'fulltext'); } /** * Compile a spatial index key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileSpatialIndex(Blueprint $blueprint, Fluent $command) { return $this->compileKey($blueprint, $command, 'spatial index'); } /** * Compile an index creation command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param string $type * @return string */ protected function compileKey(Blueprint $blueprint, Fluent $command, $type) { return sprintf('alter table %s add %s %s%s(%s)', $this->wrapTable($blueprint), $type, $this->wrap($command->index), $command->algorithm ? ' using '.$command->algorithm : '', $this->columnize($command->columns) ); } /** * Compile a drop table command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDrop(Blueprint $blueprint, Fluent $command) { return 'drop table '.$this->wrapTable($blueprint); } /** * Compile a drop table (if exists) command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropIfExists(Blueprint $blueprint, Fluent $command) { return 'drop table if exists '.$this->wrapTable($blueprint); } /** * Compile a drop column command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropColumn(Blueprint $blueprint, Fluent $command) { $columns = $this->prefixArray('drop', $this->wrapArray($command->columns)); return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns); } /** * Compile a drop primary key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropPrimary(Blueprint $blueprint, Fluent $command) { return 'alter table '.$this->wrapTable($blueprint).' drop primary key'; } /** * Compile a drop unique key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropUnique(Blueprint $blueprint, Fluent $command) { $index = $this->wrap($command->index); return "alter table {$this->wrapTable($blueprint)} drop index {$index}"; } /** * Compile a drop index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropIndex(Blueprint $blueprint, Fluent $command) { $index = $this->wrap($command->index); return "alter table {$this->wrapTable($blueprint)} drop index {$index}"; } /** * Compile a drop fulltext index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropFullText(Blueprint $blueprint, Fluent $command) { return $this->compileDropIndex($blueprint, $command); } /** * Compile a drop spatial index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropSpatialIndex(Blueprint $blueprint, Fluent $command) { return $this->compileDropIndex($blueprint, $command); } /** * Compile a drop foreign key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropForeign(Blueprint $blueprint, Fluent $command) { $index = $this->wrap($command->index); return "alter table {$this->wrapTable($blueprint)} drop foreign key {$index}"; } /** * Compile a rename table command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileRename(Blueprint $blueprint, Fluent $command) { $from = $this->wrapTable($blueprint); return "rename table {$from} to ".$this->wrapTable($command->to); } /** * Compile a rename index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileRenameIndex(Blueprint $blueprint, Fluent $command) { return sprintf('alter table %s rename index %s to %s', $this->wrapTable($blueprint), $this->wrap($command->from), $this->wrap($command->to) ); } /** * Compile the SQL needed to drop all tables. * * @param array $tables * @return string */ public function compileDropAllTables($tables) { return 'drop table '.implode(',', $this->wrapArray($tables)); } /** * Compile the SQL needed to drop all views. * * @param array $views * @return string */ public function compileDropAllViews($views) { return 'drop view '.implode(',', $this->wrapArray($views)); } /** * Compile the SQL needed to retrieve all table names. * * @return string */ public function compileGetAllTables() { return 'SHOW FULL TABLES WHERE table_type = \'BASE TABLE\''; } /** * Compile the SQL needed to retrieve all view names. * * @return string */ public function compileGetAllViews() { return 'SHOW FULL TABLES WHERE table_type = \'VIEW\''; } /** * Compile the command to enable foreign key constraints. * * @return string */ public function compileEnableForeignKeyConstraints() { return 'SET FOREIGN_KEY_CHECKS=1;'; } /** * Compile the command to disable foreign key constraints. * * @return string */ public function compileDisableForeignKeyConstraints() { return 'SET FOREIGN_KEY_CHECKS=0;'; } /** * Compile a table comment command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileTableComment(Blueprint $blueprint, Fluent $command) { return sprintf('alter table %s comment = %s', $this->wrapTable($blueprint), "'".str_replace("'", "''", $command->comment)."'" ); } /** * Create the column definition for a char type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeChar(Fluent $column) { return "char({$column->length})"; } /** * Create the column definition for a string type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeString(Fluent $column) { return "varchar({$column->length})"; } /** * Create the column definition for a tiny text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTinyText(Fluent $column) { return 'tinytext'; } /** * Create the column definition for a text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeText(Fluent $column) { return 'text'; } /** * Create the column definition for a medium text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMediumText(Fluent $column) { return 'mediumtext'; } /** * Create the column definition for a long text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeLongText(Fluent $column) { return 'longtext'; } /** * Create the column definition for a big integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeBigInteger(Fluent $column) { return 'bigint'; } /** * Create the column definition for an integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeInteger(Fluent $column) { return 'int'; } /** * Create the column definition for a medium integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMediumInteger(Fluent $column) { return 'mediumint'; } /** * Create the column definition for a tiny integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTinyInteger(Fluent $column) { return 'tinyint'; } /** * Create the column definition for a small integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeSmallInteger(Fluent $column) { return 'smallint'; } /** * Create the column definition for a float type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeFloat(Fluent $column) { return $this->typeDouble($column); } /** * Create the column definition for a double type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDouble(Fluent $column) { if ($column->total && $column->places) { return "double({$column->total}, {$column->places})"; } return 'double'; } /** * Create the column definition for a decimal type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDecimal(Fluent $column) { return "decimal({$column->total}, {$column->places})"; } /** * Create the column definition for a boolean type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeBoolean(Fluent $column) { return 'tinyint(1)'; } /** * Create the column definition for an enumeration type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeEnum(Fluent $column) { return sprintf('enum(%s)', $this->quoteString($column->allowed)); } /** * Create the column definition for a set enumeration type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeSet(Fluent $column) { return sprintf('set(%s)', $this->quoteString($column->allowed)); } /** * Create the column definition for a json type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeJson(Fluent $column) { return 'json'; } /** * Create the column definition for a jsonb type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeJsonb(Fluent $column) { return 'json'; } /** * Create the column definition for a date type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDate(Fluent $column) { return 'date'; } /** * Create the column definition for a date-time type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDateTime(Fluent $column) { $current = $column->precision ? "CURRENT_TIMESTAMP($column->precision)" : 'CURRENT_TIMESTAMP'; if ($column->useCurrent) { $column->default(new Expression($current)); } if ($column->useCurrentOnUpdate) { $column->onUpdate(new Expression($current)); } return $column->precision ? "datetime($column->precision)" : 'datetime'; } /** * Create the column definition for a date-time (with time zone) type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDateTimeTz(Fluent $column) { return $this->typeDateTime($column); } /** * Create the column definition for a time type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTime(Fluent $column) { return $column->precision ? "time($column->precision)" : 'time'; } /** * Create the column definition for a time (with time zone) type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTimeTz(Fluent $column) { return $this->typeTime($column); } /** * Create the column definition for a timestamp type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTimestamp(Fluent $column) { $current = $column->precision ? "CURRENT_TIMESTAMP($column->precision)" : 'CURRENT_TIMESTAMP'; if ($column->useCurrent) { $column->default(new Expression($current)); } if ($column->useCurrentOnUpdate) { $column->onUpdate(new Expression($current)); } return $column->precision ? "timestamp($column->precision)" : 'timestamp'; } /** * Create the column definition for a timestamp (with time zone) type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTimestampTz(Fluent $column) { return $this->typeTimestamp($column); } /** * Create the column definition for a year type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeYear(Fluent $column) { return 'year'; } /** * Create the column definition for a binary type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeBinary(Fluent $column) { return 'blob'; } /** * Create the column definition for a uuid type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeUuid(Fluent $column) { return 'char(36)'; } /** * Create the column definition for an IP address type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeIpAddress(Fluent $column) { return 'varchar(45)'; } /** * Create the column definition for a MAC address type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMacAddress(Fluent $column) { return 'varchar(17)'; } /** * Create the column definition for a spatial Geometry type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeGeometry(Fluent $column) { return 'geometry'; } /** * Create the column definition for a spatial Point type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typePoint(Fluent $column) { return 'point'; } /** * Create the column definition for a spatial LineString type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeLineString(Fluent $column) { return 'linestring'; } /** * Create the column definition for a spatial Polygon type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typePolygon(Fluent $column) { return 'polygon'; } /** * Create the column definition for a spatial GeometryCollection type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeGeometryCollection(Fluent $column) { return 'geometrycollection'; } /** * Create the column definition for a spatial MultiPoint type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeMultiPoint(Fluent $column) { return 'multipoint'; } /** * Create the column definition for a spatial MultiLineString type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeMultiLineString(Fluent $column) { return 'multilinestring'; } /** * Create the column definition for a spatial MultiPolygon type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeMultiPolygon(Fluent $column) { return 'multipolygon'; } /** * Create the column definition for a generated, computed column type. * * @param \Illuminate\Support\Fluent $column * @return void * * @throws \RuntimeException */ protected function typeComputed(Fluent $column) { throw new RuntimeException('This database driver requires a type, see the virtualAs / storedAs modifiers.'); } /** * Get the SQL for a generated virtual column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column) { if (! is_null($virtualAs = $column->virtualAsJson)) { if ($this->isJsonSelector($virtualAs)) { $virtualAs = $this->wrapJsonSelector($virtualAs); } return " as ({$virtualAs})"; } if (! is_null($virtualAs = $column->virtualAs)) { return " as ({$virtualAs})"; } } /** * Get the SQL for a generated stored column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyStoredAs(Blueprint $blueprint, Fluent $column) { if (! is_null($storedAs = $column->storedAsJson)) { if ($this->isJsonSelector($storedAs)) { $storedAs = $this->wrapJsonSelector($storedAs); } return " as ({$storedAs}) stored"; } if (! is_null($storedAs = $column->storedAs)) { return " as ({$storedAs}) stored"; } } /** * Get the SQL for an unsigned column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyUnsigned(Blueprint $blueprint, Fluent $column) { if ($column->unsigned) { return ' unsigned'; } } /** * Get the SQL for a character set column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyCharset(Blueprint $blueprint, Fluent $column) { if (! is_null($column->charset)) { return ' character set '.$column->charset; } } /** * Get the SQL for a collation column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyCollate(Blueprint $blueprint, Fluent $column) { if (! is_null($column->collation)) { return " collate '{$column->collation}'"; } } /** * Get the SQL for a nullable column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyNullable(Blueprint $blueprint, Fluent $column) { if (is_null($column->virtualAs) && is_null($column->virtualAsJson) && is_null($column->storedAs) && is_null($column->storedAsJson)) { return $column->nullable ? ' null' : ' not null'; } if ($column->nullable === false) { return ' not null'; } } /** * Get the SQL for an invisible column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyInvisible(Blueprint $blueprint, Fluent $column) { if (! is_null($column->invisible)) { return ' invisible'; } } /** * Get the SQL for a default column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyDefault(Blueprint $blueprint, Fluent $column) { if (! is_null($column->default)) { return ' default '.$this->getDefaultValue($column->default); } } /** * Get the SQL for an "on update" column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyOnUpdate(Blueprint $blueprint, Fluent $column) { if (! is_null($column->onUpdate)) { return ' on update '.$this->getValue($column->onUpdate); } } /** * Get the SQL for an auto-increment column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyIncrement(Blueprint $blueprint, Fluent $column) { if (in_array($column->type, $this->serials) && $column->autoIncrement) { return ' auto_increment primary key'; } } /** * Get the SQL for a "first" column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyFirst(Blueprint $blueprint, Fluent $column) { if (! is_null($column->first)) { return ' first'; } } /** * Get the SQL for an "after" column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyAfter(Blueprint $blueprint, Fluent $column) { if (! is_null($column->after)) { return ' after '.$this->wrap($column->after); } } /** * Get the SQL for a "comment" column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyComment(Blueprint $blueprint, Fluent $column) { if (! is_null($column->comment)) { return " comment '".addslashes($column->comment)."'"; } } /** * Get the SQL for a SRID column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifySrid(Blueprint $blueprint, Fluent $column) { if (is_int($column->srid) && $column->srid > 0) { return ' srid '.$column->srid; } } /** * Wrap a single string in keyword identifiers. * * @param string $value * @return string */ protected function wrapValue($value) { if ($value !== '*') { return '`'.str_replace('`', '``', $value).'`'; } return $value; } /** * Wrap the given JSON selector. * * @param string $value * @return string */ protected function wrapJsonSelector($value) { [$field, $path] = $this->wrapJsonFieldAndPath($value); return 'json_unquote(json_extract('.$field.$path.'))'; } } src/Illuminate/Database/Schema/Grammars/Grammar.php 0000644 00000024451 15107327037 0016225 0 ustar 00 <?php namespace Illuminate\Database\Schema\Grammars; use BackedEnum; use Doctrine\DBAL\Schema\AbstractSchemaManager as SchemaManager; use Doctrine\DBAL\Schema\TableDiff; use Illuminate\Contracts\Database\Query\Expression; use Illuminate\Database\Concerns\CompilesJsonPaths; use Illuminate\Database\Connection; use Illuminate\Database\Grammar as BaseGrammar; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Fluent; use LogicException; use RuntimeException; abstract class Grammar extends BaseGrammar { use CompilesJsonPaths; /** * The possible column modifiers. * * @var string[] */ protected $modifiers = []; /** * If this Grammar supports schema changes wrapped in a transaction. * * @var bool */ protected $transactions = false; /** * The commands to be executed outside of create or alter command. * * @var array */ protected $fluentCommands = []; /** * Compile a create database command. * * @param string $name * @param \Illuminate\Database\Connection $connection * @return void * * @throws \LogicException */ public function compileCreateDatabase($name, $connection) { throw new LogicException('This database driver does not support creating databases.'); } /** * Compile a drop database if exists command. * * @param string $name * @return void * * @throws \LogicException */ public function compileDropDatabaseIfExists($name) { throw new LogicException('This database driver does not support dropping databases.'); } /** * Compile a rename column command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return array|string */ public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection) { return RenameColumn::compile($this, $blueprint, $command, $connection); } /** * Compile a change column command into a series of SQL statements. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return array|string * * @throws \RuntimeException */ public function compileChange(Blueprint $blueprint, Fluent $command, Connection $connection) { return ChangeColumn::compile($this, $blueprint, $command, $connection); } /** * Compile a fulltext index key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string * * @throws \RuntimeException */ public function compileFulltext(Blueprint $blueprint, Fluent $command) { throw new RuntimeException('This database driver does not support fulltext index creation.'); } /** * Compile a drop fulltext index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string * * @throws \RuntimeException */ public function compileDropFullText(Blueprint $blueprint, Fluent $command) { throw new RuntimeException('This database driver does not support fulltext index removal.'); } /** * Compile a foreign key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileForeign(Blueprint $blueprint, Fluent $command) { // We need to prepare several of the elements of the foreign key definition // before we can create the SQL, such as wrapping the tables and convert // an array of columns to comma-delimited strings for the SQL queries. $sql = sprintf('alter table %s add constraint %s ', $this->wrapTable($blueprint), $this->wrap($command->index) ); // Once we have the initial portion of the SQL statement we will add on the // key name, table name, and referenced columns. These will complete the // main portion of the SQL statement and this SQL will almost be done. $sql .= sprintf('foreign key (%s) references %s (%s)', $this->columnize($command->columns), $this->wrapTable($command->on), $this->columnize((array) $command->references) ); // Once we have the basic foreign key creation statement constructed we can // build out the syntax for what should happen on an update or delete of // the affected columns, which will get something like "cascade", etc. if (! is_null($command->onDelete)) { $sql .= " on delete {$command->onDelete}"; } if (! is_null($command->onUpdate)) { $sql .= " on update {$command->onUpdate}"; } return $sql; } /** * Compile the blueprint's added column definitions. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @return array */ protected function getColumns(Blueprint $blueprint) { $columns = []; foreach ($blueprint->getAddedColumns() as $column) { // Each of the column types has their own compiler functions, which are tasked // with turning the column definition into its SQL format for this platform // used by the connection. The column's modifiers are compiled and added. $sql = $this->wrap($column).' '.$this->getType($column); $columns[] = $this->addModifiers($sql, $blueprint, $column); } return $columns; } /** * Get the SQL for the column data type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function getType(Fluent $column) { return $this->{'type'.ucfirst($column->type)}($column); } /** * Create the column definition for a generated, computed column type. * * @param \Illuminate\Support\Fluent $column * @return void * * @throws \RuntimeException */ protected function typeComputed(Fluent $column) { throw new RuntimeException('This database driver does not support the computed type.'); } /** * Add the column modifiers to the definition. * * @param string $sql * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string */ protected function addModifiers($sql, Blueprint $blueprint, Fluent $column) { foreach ($this->modifiers as $modifier) { if (method_exists($this, $method = "modify{$modifier}")) { $sql .= $this->{$method}($blueprint, $column); } } return $sql; } /** * Get the primary key command if it exists on the blueprint. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param string $name * @return \Illuminate\Support\Fluent|null */ protected function getCommandByName(Blueprint $blueprint, $name) { $commands = $this->getCommandsByName($blueprint, $name); if (count($commands) > 0) { return reset($commands); } } /** * Get all of the commands with a given name. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param string $name * @return array */ protected function getCommandsByName(Blueprint $blueprint, $name) { return array_filter($blueprint->getCommands(), function ($value) use ($name) { return $value->name == $name; }); } /** * Add a prefix to an array of values. * * @param string $prefix * @param array $values * @return array */ public function prefixArray($prefix, array $values) { return array_map(function ($value) use ($prefix) { return $prefix.' '.$value; }, $values); } /** * Wrap a table in keyword identifiers. * * @param mixed $table * @return string */ public function wrapTable($table) { return parent::wrapTable( $table instanceof Blueprint ? $table->getTable() : $table ); } /** * Wrap a value in keyword identifiers. * * @param \Illuminate\Support\Fluent|\Illuminate\Contracts\Database\Query\Expression|string $value * @param bool $prefixAlias * @return string */ public function wrap($value, $prefixAlias = false) { return parent::wrap( $value instanceof Fluent ? $value->name : $value, $prefixAlias ); } /** * Format a value so that it can be used in "default" clauses. * * @param mixed $value * @return string */ protected function getDefaultValue($value) { if ($value instanceof Expression) { return $this->getValue($value); } if ($value instanceof BackedEnum) { return "'{$value->value}'"; } return is_bool($value) ? "'".(int) $value."'" : "'".(string) $value."'"; } /** * Create an empty Doctrine DBAL TableDiff from the Blueprint. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema * @return \Doctrine\DBAL\Schema\TableDiff */ public function getDoctrineTableDiff(Blueprint $blueprint, SchemaManager $schema) { $tableName = $this->getTablePrefix().$blueprint->getTable(); $table = $schema->introspectTable($tableName); return new TableDiff(tableName: $tableName, fromTable: $table); } /** * Get the fluent commands for the grammar. * * @return array */ public function getFluentCommands() { return $this->fluentCommands; } /** * Check if this Grammar supports schema changes wrapped in a transaction. * * @return bool */ public function supportsSchemaTransactions() { return $this->transactions; } } src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php 0000644 00000066740 15107327037 0017316 0 ustar 00 <?php namespace Illuminate\Database\Schema\Grammars; use Doctrine\DBAL\Schema\Index; use Illuminate\Database\Connection; use Illuminate\Database\Query\Expression; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Arr; use Illuminate\Support\Fluent; use RuntimeException; class SQLiteGrammar extends Grammar { /** * The possible column modifiers. * * @var string[] */ protected $modifiers = ['Increment', 'Nullable', 'Default', 'VirtualAs', 'StoredAs']; /** * The columns available as serials. * * @var string[] */ protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger']; /** * Compile the query to determine if a table exists. * * @return string */ public function compileTableExists() { return "select * from sqlite_master where type = 'table' and name = ?"; } /** * Compile the query to determine the list of columns. * * @deprecated Will be removed in a future Laravel version. * * @param string $table * @return string */ public function compileColumnListing($table) { return 'pragma table_info('.$this->wrap(str_replace('.', '__', $table)).')'; } /** * Compile the query to determine the columns. * * @param string $table * @return string */ public function compileColumns($table) { return sprintf( "select name, type, not 'notnull' as 'nullable', dflt_value as 'default', pk as 'primary' " .'from pragma_table_info(%s) order by cid asc', $this->wrap(str_replace('.', '__', $table)) ); } /** * Compile a create table command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileCreate(Blueprint $blueprint, Fluent $command) { return sprintf('%s table %s (%s%s%s)', $blueprint->temporary ? 'create temporary' : 'create', $this->wrapTable($blueprint), implode(', ', $this->getColumns($blueprint)), (string) $this->addForeignKeys($blueprint), (string) $this->addPrimaryKeys($blueprint) ); } /** * Get the foreign key syntax for a table creation statement. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @return string|null */ protected function addForeignKeys(Blueprint $blueprint) { $foreigns = $this->getCommandsByName($blueprint, 'foreign'); return collect($foreigns)->reduce(function ($sql, $foreign) { // Once we have all the foreign key commands for the table creation statement // we'll loop through each of them and add them to the create table SQL we // are building, since SQLite needs foreign keys on the tables creation. $sql .= $this->getForeignKey($foreign); if (! is_null($foreign->onDelete)) { $sql .= " on delete {$foreign->onDelete}"; } // If this foreign key specifies the action to be taken on update we will add // that to the statement here. We'll append it to this SQL and then return // the SQL so we can keep adding any other foreign constraints onto this. if (! is_null($foreign->onUpdate)) { $sql .= " on update {$foreign->onUpdate}"; } return $sql; }, ''); } /** * Get the SQL for the foreign key. * * @param \Illuminate\Support\Fluent $foreign * @return string */ protected function getForeignKey($foreign) { // We need to columnize the columns that the foreign key is being defined for // so that it is a properly formatted list. Once we have done this, we can // return the foreign key SQL declaration to the calling method for use. return sprintf(', foreign key(%s) references %s(%s)', $this->columnize($foreign->columns), $this->wrapTable($foreign->on), $this->columnize((array) $foreign->references) ); } /** * Get the primary key syntax for a table creation statement. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @return string|null */ protected function addPrimaryKeys(Blueprint $blueprint) { if (! is_null($primary = $this->getCommandByName($blueprint, 'primary'))) { return ", primary key ({$this->columnize($primary->columns)})"; } } /** * Compile alter table commands for adding columns. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return array */ public function compileAdd(Blueprint $blueprint, Fluent $command) { $columns = $this->prefixArray('add column', $this->getColumns($blueprint)); return collect($columns)->reject(function ($column) { return preg_match('/as \(.*\) stored/', $column) > 0; })->map(function ($column) use ($blueprint) { return 'alter table '.$this->wrapTable($blueprint).' '.$column; })->all(); } /** * Compile a rename column command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return array|string */ public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection) { return $connection->usingNativeSchemaOperations() ? sprintf('alter table %s rename column %s to %s', $this->wrapTable($blueprint), $this->wrap($command->from), $this->wrap($command->to) ) : parent::compileRenameColumn($blueprint, $command, $connection); } /** * Compile a unique key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileUnique(Blueprint $blueprint, Fluent $command) { return sprintf('create unique index %s on %s (%s)', $this->wrap($command->index), $this->wrapTable($blueprint), $this->columnize($command->columns) ); } /** * Compile a plain index key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileIndex(Blueprint $blueprint, Fluent $command) { return sprintf('create index %s on %s (%s)', $this->wrap($command->index), $this->wrapTable($blueprint), $this->columnize($command->columns) ); } /** * Compile a spatial index key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return void * * @throws \RuntimeException */ public function compileSpatialIndex(Blueprint $blueprint, Fluent $command) { throw new RuntimeException('The database driver in use does not support spatial indexes.'); } /** * Compile a foreign key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string|null */ public function compileForeign(Blueprint $blueprint, Fluent $command) { // Handled on table creation... } /** * Compile a drop table command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDrop(Blueprint $blueprint, Fluent $command) { return 'drop table '.$this->wrapTable($blueprint); } /** * Compile a drop table (if exists) command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropIfExists(Blueprint $blueprint, Fluent $command) { return 'drop table if exists '.$this->wrapTable($blueprint); } /** * Compile the SQL needed to drop all tables. * * @return string */ public function compileDropAllTables() { return "delete from sqlite_master where type in ('table', 'index', 'trigger')"; } /** * Compile the SQL needed to drop all views. * * @return string */ public function compileDropAllViews() { return "delete from sqlite_master where type in ('view')"; } /** * Compile the SQL needed to retrieve all table names. * * @return string */ public function compileGetAllTables() { return 'select type, name from sqlite_master where type = \'table\' and name not like \'sqlite_%\''; } /** * Compile the SQL needed to retrieve all view names. * * @return string */ public function compileGetAllViews() { return 'select type, name from sqlite_master where type = \'view\''; } /** * Compile the SQL needed to rebuild the database. * * @return string */ public function compileRebuild() { return 'vacuum'; } /** * Compile a drop column command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return array */ public function compileDropColumn(Blueprint $blueprint, Fluent $command, Connection $connection) { if ($connection->usingNativeSchemaOperations()) { $table = $this->wrapTable($blueprint); $columns = $this->prefixArray('drop column', $this->wrapArray($command->columns)); return collect($columns)->map(fn ($column) => 'alter table '.$table.' '.$column )->all(); } else { $tableDiff = $this->getDoctrineTableDiff( $blueprint, $schema = $connection->getDoctrineSchemaManager() ); foreach ($command->columns as $name) { $tableDiff->removedColumns[$name] = $connection->getDoctrineColumn( $this->getTablePrefix().$blueprint->getTable(), $name ); } return (array) $schema->getDatabasePlatform()->getAlterTableSQL($tableDiff); } } /** * Compile a drop unique key command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropUnique(Blueprint $blueprint, Fluent $command) { $index = $this->wrap($command->index); return "drop index {$index}"; } /** * Compile a drop index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropIndex(Blueprint $blueprint, Fluent $command) { $index = $this->wrap($command->index); return "drop index {$index}"; } /** * Compile a drop spatial index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return void * * @throws \RuntimeException */ public function compileDropSpatialIndex(Blueprint $blueprint, Fluent $command) { throw new RuntimeException('The database driver in use does not support spatial indexes.'); } /** * Compile a rename table command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ public function compileRename(Blueprint $blueprint, Fluent $command) { $from = $this->wrapTable($blueprint); return "alter table {$from} rename to ".$this->wrapTable($command->to); } /** * Compile a rename index command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return array * * @throws \RuntimeException */ public function compileRenameIndex(Blueprint $blueprint, Fluent $command, Connection $connection) { $schemaManager = $connection->getDoctrineSchemaManager(); $indexes = $schemaManager->listTableIndexes($this->getTablePrefix().$blueprint->getTable()); $index = Arr::get($indexes, $command->from); if (! $index) { throw new RuntimeException("Index [{$command->from}] does not exist."); } $newIndex = new Index( $command->to, $index->getColumns(), $index->isUnique(), $index->isPrimary(), $index->getFlags(), $index->getOptions() ); $platform = $connection->getDoctrineConnection()->getDatabasePlatform(); return [ $platform->getDropIndexSQL($command->from, $this->getTablePrefix().$blueprint->getTable()), $platform->getCreateIndexSQL($newIndex, $this->getTablePrefix().$blueprint->getTable()), ]; } /** * Compile the command to enable foreign key constraints. * * @return string */ public function compileEnableForeignKeyConstraints() { return 'PRAGMA foreign_keys = ON;'; } /** * Compile the command to disable foreign key constraints. * * @return string */ public function compileDisableForeignKeyConstraints() { return 'PRAGMA foreign_keys = OFF;'; } /** * Compile the SQL needed to enable a writable schema. * * @return string */ public function compileEnableWriteableSchema() { return 'PRAGMA writable_schema = 1;'; } /** * Compile the SQL needed to disable a writable schema. * * @return string */ public function compileDisableWriteableSchema() { return 'PRAGMA writable_schema = 0;'; } /** * Create the column definition for a char type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeChar(Fluent $column) { return 'varchar'; } /** * Create the column definition for a string type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeString(Fluent $column) { return 'varchar'; } /** * Create the column definition for a tiny text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTinyText(Fluent $column) { return 'text'; } /** * Create the column definition for a text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeText(Fluent $column) { return 'text'; } /** * Create the column definition for a medium text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMediumText(Fluent $column) { return 'text'; } /** * Create the column definition for a long text type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeLongText(Fluent $column) { return 'text'; } /** * Create the column definition for an integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeInteger(Fluent $column) { return 'integer'; } /** * Create the column definition for a big integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeBigInteger(Fluent $column) { return 'integer'; } /** * Create the column definition for a medium integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMediumInteger(Fluent $column) { return 'integer'; } /** * Create the column definition for a tiny integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTinyInteger(Fluent $column) { return 'integer'; } /** * Create the column definition for a small integer type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeSmallInteger(Fluent $column) { return 'integer'; } /** * Create the column definition for a float type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeFloat(Fluent $column) { return 'float'; } /** * Create the column definition for a double type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDouble(Fluent $column) { return 'float'; } /** * Create the column definition for a decimal type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDecimal(Fluent $column) { return 'numeric'; } /** * Create the column definition for a boolean type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeBoolean(Fluent $column) { return 'tinyint(1)'; } /** * Create the column definition for an enumeration type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeEnum(Fluent $column) { return sprintf( 'varchar check ("%s" in (%s))', $column->name, $this->quoteString($column->allowed) ); } /** * Create the column definition for a json type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeJson(Fluent $column) { return 'text'; } /** * Create the column definition for a jsonb type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeJsonb(Fluent $column) { return 'text'; } /** * Create the column definition for a date type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDate(Fluent $column) { return 'date'; } /** * Create the column definition for a date-time type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDateTime(Fluent $column) { return $this->typeTimestamp($column); } /** * Create the column definition for a date-time (with time zone) type. * * Note: "SQLite does not have a storage class set aside for storing dates and/or times." * * @link https://www.sqlite.org/datatype3.html * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDateTimeTz(Fluent $column) { return $this->typeDateTime($column); } /** * Create the column definition for a time type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTime(Fluent $column) { return 'time'; } /** * Create the column definition for a time (with time zone) type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTimeTz(Fluent $column) { return $this->typeTime($column); } /** * Create the column definition for a timestamp type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTimestamp(Fluent $column) { if ($column->useCurrent) { $column->default(new Expression('CURRENT_TIMESTAMP')); } return 'datetime'; } /** * Create the column definition for a timestamp (with time zone) type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeTimestampTz(Fluent $column) { return $this->typeTimestamp($column); } /** * Create the column definition for a year type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeYear(Fluent $column) { return $this->typeInteger($column); } /** * Create the column definition for a binary type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeBinary(Fluent $column) { return 'blob'; } /** * Create the column definition for a uuid type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeUuid(Fluent $column) { return 'varchar'; } /** * Create the column definition for an IP address type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeIpAddress(Fluent $column) { return 'varchar'; } /** * Create the column definition for a MAC address type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeMacAddress(Fluent $column) { return 'varchar'; } /** * Create the column definition for a spatial Geometry type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeGeometry(Fluent $column) { return 'geometry'; } /** * Create the column definition for a spatial Point type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typePoint(Fluent $column) { return 'point'; } /** * Create the column definition for a spatial LineString type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeLineString(Fluent $column) { return 'linestring'; } /** * Create the column definition for a spatial Polygon type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typePolygon(Fluent $column) { return 'polygon'; } /** * Create the column definition for a spatial GeometryCollection type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeGeometryCollection(Fluent $column) { return 'geometrycollection'; } /** * Create the column definition for a spatial MultiPoint type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeMultiPoint(Fluent $column) { return 'multipoint'; } /** * Create the column definition for a spatial MultiLineString type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeMultiLineString(Fluent $column) { return 'multilinestring'; } /** * Create the column definition for a spatial MultiPolygon type. * * @param \Illuminate\Support\Fluent $column * @return string */ public function typeMultiPolygon(Fluent $column) { return 'multipolygon'; } /** * Create the column definition for a generated, computed column type. * * @param \Illuminate\Support\Fluent $column * @return void * * @throws \RuntimeException */ protected function typeComputed(Fluent $column) { throw new RuntimeException('This database driver requires a type, see the virtualAs / storedAs modifiers.'); } /** * Get the SQL for a generated virtual column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column) { if (! is_null($virtualAs = $column->virtualAsJson)) { if ($this->isJsonSelector($virtualAs)) { $virtualAs = $this->wrapJsonSelector($virtualAs); } return " as ({$virtualAs})"; } if (! is_null($virtualAs = $column->virtualAs)) { return " as ({$virtualAs})"; } } /** * Get the SQL for a generated stored column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyStoredAs(Blueprint $blueprint, Fluent $column) { if (! is_null($storedAs = $column->storedAsJson)) { if ($this->isJsonSelector($storedAs)) { $storedAs = $this->wrapJsonSelector($storedAs); } return " as ({$storedAs}) stored"; } if (! is_null($storedAs = $column->storedAs)) { return " as ({$column->storedAs}) stored"; } } /** * Get the SQL for a nullable column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyNullable(Blueprint $blueprint, Fluent $column) { if (is_null($column->virtualAs) && is_null($column->virtualAsJson) && is_null($column->storedAs) && is_null($column->storedAsJson)) { return $column->nullable ? '' : ' not null'; } if ($column->nullable === false) { return ' not null'; } } /** * Get the SQL for a default column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyDefault(Blueprint $blueprint, Fluent $column) { if (! is_null($column->default) && is_null($column->virtualAs) && is_null($column->virtualAsJson) && is_null($column->storedAs)) { return ' default '.$this->getDefaultValue($column->default); } } /** * Get the SQL for an auto-increment column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ protected function modifyIncrement(Blueprint $blueprint, Fluent $column) { if (in_array($column->type, $this->serials) && $column->autoIncrement) { return ' primary key autoincrement'; } } /** * Wrap the given JSON selector. * * @param string $value * @return string */ protected function wrapJsonSelector($value) { [$field, $path] = $this->wrapJsonFieldAndPath($value); return 'json_extract('.$field.$path.')'; } } src/Illuminate/Database/Schema/Grammars/ChangeColumn.php 0000644 00000016531 15107327037 0017202 0 ustar 00 <?php namespace Illuminate\Database\Schema\Grammars; use Doctrine\DBAL\Schema\AbstractSchemaManager as SchemaManager; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Types\Type; use Illuminate\Database\Connection; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Fluent; use RuntimeException; class ChangeColumn { /** * Compile a change column command into a series of SQL statements. * * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return array * * @throws \RuntimeException */ public static function compile($grammar, Blueprint $blueprint, Fluent $command, Connection $connection) { if (! $connection->isDoctrineAvailable()) { throw new RuntimeException(sprintf( 'Changing columns for table "%s" requires Doctrine DBAL. Please install the doctrine/dbal package.', $blueprint->getTable() )); } $schema = $connection->getDoctrineSchemaManager(); $databasePlatform = $connection->getDoctrineConnection()->getDatabasePlatform(); $databasePlatform->registerDoctrineTypeMapping('enum', 'string'); $tableDiff = static::getChangedDiff( $grammar, $blueprint, $schema ); if (! $tableDiff->isEmpty()) { return (array) $databasePlatform->getAlterTableSQL($tableDiff); } return []; } /** * Get the Doctrine table difference for the given changes. * * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema * @return \Doctrine\DBAL\Schema\TableDiff */ protected static function getChangedDiff($grammar, Blueprint $blueprint, SchemaManager $schema) { $current = $schema->introspectTable($grammar->getTablePrefix().$blueprint->getTable()); return $schema->createComparator()->compareTables( $current, static::getTableWithColumnChanges($blueprint, $current) ); } /** * Get a copy of the given Doctrine table after making the column changes. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Doctrine\DBAL\Schema\Table $table * @return \Doctrine\DBAL\Schema\Table */ protected static function getTableWithColumnChanges(Blueprint $blueprint, Table $table) { $table = clone $table; foreach ($blueprint->getChangedColumns() as $fluent) { $column = static::getDoctrineColumn($table, $fluent); // Here we will spin through each fluent column definition and map it to the proper // Doctrine column definitions - which is necessary because Laravel and Doctrine // use some different terminology for various column attributes on the tables. foreach ($fluent->getAttributes() as $key => $value) { if (! is_null($option = static::mapFluentOptionToDoctrine($key))) { if (method_exists($column, $method = 'set'.ucfirst($option))) { $column->{$method}(static::mapFluentValueToDoctrine($option, $value)); continue; } $column->setPlatformOption($option, static::mapFluentValueToDoctrine($option, $value)); } } } return $table; } /** * Get the Doctrine column instance for a column change. * * @param \Doctrine\DBAL\Schema\Table $table * @param \Illuminate\Support\Fluent $fluent * @return \Doctrine\DBAL\Schema\Column */ protected static function getDoctrineColumn(Table $table, Fluent $fluent) { return $table->modifyColumn( $fluent['name'], static::getDoctrineColumnChangeOptions($fluent) )->getColumn($fluent['name']); } /** * Get the Doctrine column change options. * * @param \Illuminate\Support\Fluent $fluent * @return array */ protected static function getDoctrineColumnChangeOptions(Fluent $fluent) { $options = ['type' => static::getDoctrineColumnType($fluent['type'])]; if (! in_array($fluent['type'], ['smallint', 'integer', 'bigint'])) { $options['autoincrement'] = false; } if (in_array($fluent['type'], ['tinyText', 'text', 'mediumText', 'longText'])) { $options['length'] = static::calculateDoctrineTextLength($fluent['type']); } if ($fluent['type'] === 'char') { $options['fixed'] = true; } if (static::doesntNeedCharacterOptions($fluent['type'])) { $options['customSchemaOptions'] = [ 'collation' => '', 'charset' => '', ]; } return $options; } /** * Get the doctrine column type. * * @param string $type * @return \Doctrine\DBAL\Types\Type */ protected static function getDoctrineColumnType($type) { $type = strtolower($type); return Type::getType(match ($type) { 'biginteger' => 'bigint', 'smallinteger' => 'smallint', 'tinytext', 'mediumtext', 'longtext' => 'text', 'binary' => 'blob', 'uuid' => 'guid', 'char' => 'string', 'double' => 'float', default => $type, }); } /** * Calculate the proper column length to force the Doctrine text type. * * @param string $type * @return int */ protected static function calculateDoctrineTextLength($type) { return match ($type) { 'tinyText' => 1, 'mediumText' => 65535 + 1, 'longText' => 16777215 + 1, default => 255 + 1, }; } /** * Determine if the given type does not need character / collation options. * * @param string $type * @return bool */ protected static function doesntNeedCharacterOptions($type) { return in_array($type, [ 'bigInteger', 'binary', 'boolean', 'date', 'dateTime', 'decimal', 'double', 'float', 'integer', 'json', 'mediumInteger', 'smallInteger', 'time', 'timestamp', 'tinyInteger', ]); } /** * Get the matching Doctrine option for a given Fluent attribute name. * * @param string $attribute * @return string|null */ protected static function mapFluentOptionToDoctrine($attribute) { return match ($attribute) { 'type', 'name' => null, 'nullable' => 'notnull', 'total' => 'precision', 'places' => 'scale', default => $attribute, }; } /** * Get the matching Doctrine value for a given Fluent attribute. * * @param string $option * @param mixed $value * @return mixed */ protected static function mapFluentValueToDoctrine($option, $value) { return $option === 'notnull' ? ! $value : $value; } } src/Illuminate/Database/Schema/SchemaState.php 0000644 00000005275 15107327037 0015272 0 ustar 00 <?php namespace Illuminate\Database\Schema; use Illuminate\Database\Connection; use Illuminate\Filesystem\Filesystem; use Symfony\Component\Process\Process; abstract class SchemaState { /** * The connection instance. * * @var \Illuminate\Database\Connection */ protected $connection; /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * The name of the application's migration table. * * @var string */ protected $migrationTable = 'migrations'; /** * The process factory callback. * * @var callable */ protected $processFactory; /** * The output callable instance. * * @var callable */ protected $output; /** * Create a new dumper instance. * * @param \Illuminate\Database\Connection $connection * @param \Illuminate\Filesystem\Filesystem|null $files * @param callable|null $processFactory * @return void */ public function __construct(Connection $connection, Filesystem $files = null, callable $processFactory = null) { $this->connection = $connection; $this->files = $files ?: new Filesystem; $this->processFactory = $processFactory ?: function (...$arguments) { return Process::fromShellCommandline(...$arguments)->setTimeout(null); }; $this->handleOutputUsing(function () { // }); } /** * Dump the database's schema into a file. * * @param \Illuminate\Database\Connection $connection * @param string $path * @return void */ abstract public function dump(Connection $connection, $path); /** * Load the given schema file into the database. * * @param string $path * @return void */ abstract public function load($path); /** * Create a new process instance. * * @param mixed ...$arguments * @return \Symfony\Component\Process\Process */ public function makeProcess(...$arguments) { return call_user_func($this->processFactory, ...$arguments); } /** * Specify the name of the application's migration table. * * @param string $table * @return $this */ public function withMigrationTable(string $table) { $this->migrationTable = $table; return $this; } /** * Specify the callback that should be used to handle process output. * * @param callable $output * @return $this */ public function handleOutputUsing(callable $output) { $this->output = $output; return $this; } } src/Illuminate/Database/Schema/PostgresSchemaState.php 0000644 00000005104 15107327037 0017010 0 ustar 00 <?php namespace Illuminate\Database\Schema; use Illuminate\Database\Connection; class PostgresSchemaState extends SchemaState { /** * Dump the database's schema into a file. * * @param \Illuminate\Database\Connection $connection * @param string $path * @return void */ public function dump(Connection $connection, $path) { $commands = collect([ $this->baseDumpCommand().' --schema-only > '.$path, $this->baseDumpCommand().' -t '.$this->migrationTable.' --data-only >> '.$path, ]); $commands->map(function ($command, $path) { $this->makeProcess($command)->mustRun($this->output, array_merge($this->baseVariables($this->connection->getConfig()), [ 'LARAVEL_LOAD_PATH' => $path, ])); }); } /** * Load the given schema file into the database. * * @param string $path * @return void */ public function load($path) { $command = 'pg_restore --no-owner --no-acl --clean --if-exists --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --username="${:LARAVEL_LOAD_USER}" --dbname="${:LARAVEL_LOAD_DATABASE}" "${:LARAVEL_LOAD_PATH}"'; if (str_ends_with($path, '.sql')) { $command = 'psql --file="${:LARAVEL_LOAD_PATH}" --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --username="${:LARAVEL_LOAD_USER}" --dbname="${:LARAVEL_LOAD_DATABASE}"'; } $process = $this->makeProcess($command); $process->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [ 'LARAVEL_LOAD_PATH' => $path, ])); } /** * Get the base dump command arguments for PostgreSQL as a string. * * @return string */ protected function baseDumpCommand() { return 'pg_dump --no-owner --no-acl --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --username="${:LARAVEL_LOAD_USER}" --dbname="${:LARAVEL_LOAD_DATABASE}"'; } /** * Get the base variables for a dump / load command. * * @param array $config * @return array */ protected function baseVariables(array $config) { $config['host'] ??= ''; return [ 'LARAVEL_LOAD_HOST' => is_array($config['host']) ? $config['host'][0] : $config['host'], 'LARAVEL_LOAD_PORT' => $config['port'] ?? '', 'LARAVEL_LOAD_USER' => $config['username'], 'PGPASSWORD' => $config['password'], 'LARAVEL_LOAD_DATABASE' => $config['database'], ]; } } src/Illuminate/Database/Schema/MySqlSchemaState.php 0000644 00000012461 15107327037 0016253 0 ustar 00 <?php namespace Illuminate\Database\Schema; use Exception; use Illuminate\Database\Connection; use Illuminate\Support\Str; use Symfony\Component\Process\Process; class MySqlSchemaState extends SchemaState { /** * Dump the database's schema into a file. * * @param \Illuminate\Database\Connection $connection * @param string $path * @return void */ public function dump(Connection $connection, $path) { $this->executeDumpProcess($this->makeProcess( $this->baseDumpCommand().' --routines --result-file="${:LARAVEL_LOAD_PATH}" --no-data' ), $this->output, array_merge($this->baseVariables($this->connection->getConfig()), [ 'LARAVEL_LOAD_PATH' => $path, ])); $this->removeAutoIncrementingState($path); $this->appendMigrationData($path); } /** * Remove the auto-incrementing state from the given schema dump. * * @param string $path * @return void */ protected function removeAutoIncrementingState(string $path) { $this->files->put($path, preg_replace( '/\s+AUTO_INCREMENT=[0-9]+/iu', '', $this->files->get($path) )); } /** * Append the migration data to the schema dump. * * @param string $path * @return void */ protected function appendMigrationData(string $path) { $process = $this->executeDumpProcess($this->makeProcess( $this->baseDumpCommand().' '.$this->migrationTable.' --no-create-info --skip-extended-insert --skip-routines --compact --complete-insert' ), null, array_merge($this->baseVariables($this->connection->getConfig()), [ // ])); $this->files->append($path, $process->getOutput()); } /** * Load the given schema file into the database. * * @param string $path * @return void */ public function load($path) { $command = 'mysql '.$this->connectionString().' --database="${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"'; $process = $this->makeProcess($command)->setTimeout(null); $process->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [ 'LARAVEL_LOAD_PATH' => $path, ])); } /** * Get the base dump command arguments for MySQL as a string. * * @return string */ protected function baseDumpCommand() { $command = 'mysqldump '.$this->connectionString().' --no-tablespaces --skip-add-locks --skip-comments --skip-set-charset --tz-utc --column-statistics=0'; if (! $this->connection->isMaria()) { $command .= ' --set-gtid-purged=OFF'; } return $command.' "${:LARAVEL_LOAD_DATABASE}"'; } /** * Generate a basic connection string (--socket, --host, --port, --user, --password) for the database. * * @return string */ protected function connectionString() { $value = ' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}"'; $config = $this->connection->getConfig(); $value .= $config['unix_socket'] ?? false ? ' --socket="${:LARAVEL_LOAD_SOCKET}"' : ' --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}"'; if (isset($config['options'][\PDO::MYSQL_ATTR_SSL_CA])) { $value .= ' --ssl-ca="${:LARAVEL_LOAD_SSL_CA}"'; } return $value; } /** * Get the base variables for a dump / load command. * * @param array $config * @return array */ protected function baseVariables(array $config) { $config['host'] ??= ''; return [ 'LARAVEL_LOAD_SOCKET' => $config['unix_socket'] ?? '', 'LARAVEL_LOAD_HOST' => is_array($config['host']) ? $config['host'][0] : $config['host'], 'LARAVEL_LOAD_PORT' => $config['port'] ?? '', 'LARAVEL_LOAD_USER' => $config['username'], 'LARAVEL_LOAD_PASSWORD' => $config['password'] ?? '', 'LARAVEL_LOAD_DATABASE' => $config['database'], 'LARAVEL_LOAD_SSL_CA' => $config['options'][\PDO::MYSQL_ATTR_SSL_CA] ?? '', ]; } /** * Execute the given dump process. * * @param \Symfony\Component\Process\Process $process * @param callable $output * @param array $variables * @return \Symfony\Component\Process\Process */ protected function executeDumpProcess(Process $process, $output, array $variables) { try { $process->setTimeout(null)->mustRun($output, $variables); } catch (Exception $e) { if (Str::contains($e->getMessage(), ['column-statistics', 'column_statistics'])) { return $this->executeDumpProcess(Process::fromShellCommandLine( str_replace(' --column-statistics=0', '', $process->getCommandLine()) ), $output, $variables); } if (str_contains($e->getMessage(), 'set-gtid-purged')) { return $this->executeDumpProcess(Process::fromShellCommandLine( str_replace(' --set-gtid-purged=OFF', '', $process->getCommandLine()) ), $output, $variables); } throw $e; } return $process; } } src/Illuminate/Database/Schema/ColumnDefinition.php 0000644 00000004547 15107327037 0016340 0 ustar 00 <?php namespace Illuminate\Database\Schema; use Illuminate\Support\Fluent; /** * @method $this after(string $column) Place the column "after" another column (MySQL) * @method $this always(bool $value = true) Used as a modifier for generatedAs() (PostgreSQL) * @method $this autoIncrement() Set INTEGER columns as auto-increment (primary key) * @method $this change() Change the column * @method $this charset(string $charset) Specify a character set for the column (MySQL) * @method $this collation(string $collation) Specify a collation for the column (MySQL/PostgreSQL/SQL Server) * @method $this comment(string $comment) Add a comment to the column (MySQL/PostgreSQL) * @method $this default(mixed $value) Specify a "default" value for the column * @method $this first() Place the column "first" in the table (MySQL) * @method $this from(int $startingValue) Set the starting value of an auto-incrementing field (MySQL / PostgreSQL) * @method $this generatedAs(string|Expression $expression = null) Create a SQL compliant identity column (PostgreSQL) * @method $this index(string $indexName = null) Add an index * @method $this invisible() Specify that the column should be invisible to "SELECT *" (MySQL) * @method $this nullable(bool $value = true) Allow NULL values to be inserted into the column * @method $this persisted() Mark the computed generated column as persistent (SQL Server) * @method $this primary() Add a primary index * @method $this fulltext(string $indexName = null) Add a fulltext index * @method $this spatialIndex(string $indexName = null) Add a spatial index * @method $this startingValue(int $startingValue) Set the starting value of an auto-incrementing field (MySQL/PostgreSQL) * @method $this storedAs(string $expression) Create a stored generated column (MySQL/PostgreSQL/SQLite) * @method $this type(string $type) Specify a type for the column * @method $this unique(string $indexName = null) Add a unique index * @method $this unsigned() Set the INTEGER column as UNSIGNED (MySQL) * @method $this useCurrent() Set the TIMESTAMP column to use CURRENT_TIMESTAMP as default value * @method $this useCurrentOnUpdate() Set the TIMESTAMP column to use CURRENT_TIMESTAMP when updating (MySQL) * @method $this virtualAs(string $expression) Create a virtual generated column (MySQL/PostgreSQL/SQLite) */ class ColumnDefinition extends Fluent { // } src/Illuminate/Database/Schema/IndexDefinition.php 0000644 00000001072 15107327037 0016140 0 ustar 00 <?php namespace Illuminate\Database\Schema; use Illuminate\Support\Fluent; /** * @method $this algorithm(string $algorithm) Specify an algorithm for the index (MySQL/PostgreSQL) * @method $this language(string $language) Specify a language for the full text index (PostgreSQL) * @method $this deferrable(bool $value = true) Specify that the unique index is deferrable (PostgreSQL) * @method $this initiallyImmediate(bool $value = true) Specify the default time to check the unique index constraint (PostgreSQL) */ class IndexDefinition extends Fluent { // } src/Illuminate/Database/Schema/SqliteSchemaState.php 0000644 00000005340 15107327037 0016445 0 ustar 00 <?php namespace Illuminate\Database\Schema; use Illuminate\Database\Connection; class SqliteSchemaState extends SchemaState { /** * Dump the database's schema into a file. * * @param \Illuminate\Database\Connection $connection * @param string $path * @return void */ public function dump(Connection $connection, $path) { with($process = $this->makeProcess( $this->baseCommand().' .schema' ))->setTimeout(null)->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [ // ])); $migrations = collect(preg_split("/\r\n|\n|\r/", $process->getOutput()))->filter(function ($line) { return stripos($line, 'sqlite_sequence') === false && strlen($line) > 0; })->all(); $this->files->put($path, implode(PHP_EOL, $migrations).PHP_EOL); $this->appendMigrationData($path); } /** * Append the migration data to the schema dump. * * @param string $path * @return void */ protected function appendMigrationData(string $path) { with($process = $this->makeProcess( $this->baseCommand().' ".dump \''.$this->migrationTable.'\'"' ))->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [ // ])); $migrations = collect(preg_split("/\r\n|\n|\r/", $process->getOutput()))->filter(function ($line) { return preg_match('/^\s*(--|INSERT\s)/iu', $line) === 1 && strlen($line) > 0; })->all(); $this->files->append($path, implode(PHP_EOL, $migrations).PHP_EOL); } /** * Load the given schema file into the database. * * @param string $path * @return void */ public function load($path) { if ($this->connection->getDatabaseName() === ':memory:') { $this->connection->getPdo()->exec($this->files->get($path)); return; } $process = $this->makeProcess($this->baseCommand().' < "${:LARAVEL_LOAD_PATH}"'); $process->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [ 'LARAVEL_LOAD_PATH' => $path, ])); } /** * Get the base sqlite command arguments as a string. * * @return string */ protected function baseCommand() { return 'sqlite3 "${:LARAVEL_LOAD_DATABASE}"'; } /** * Get the base variables for a dump / load command. * * @param array $config * @return array */ protected function baseVariables(array $config) { return [ 'LARAVEL_LOAD_DATABASE' => $config['database'], ]; } } src/Illuminate/Database/Schema/MySqlBuilder.php 0000644 00000006003 15107327037 0015433 0 ustar 00 <?php namespace Illuminate\Database\Schema; class MySqlBuilder extends Builder { /** * Create a database in the schema. * * @param string $name * @return bool */ public function createDatabase($name) { return $this->connection->statement( $this->grammar->compileCreateDatabase($name, $this->connection) ); } /** * Drop a database from the schema if the database exists. * * @param string $name * @return bool */ public function dropDatabaseIfExists($name) { return $this->connection->statement( $this->grammar->compileDropDatabaseIfExists($name) ); } /** * Determine if the given table exists. * * @param string $table * @return bool */ public function hasTable($table) { $table = $this->connection->getTablePrefix().$table; return count($this->connection->selectFromWriteConnection( $this->grammar->compileTableExists(), [$this->connection->getDatabaseName(), $table] )) > 0; } /** * Get the columns for a given table. * * @param string $table * @return array */ public function getColumns($table) { $table = $this->connection->getTablePrefix().$table; $results = $this->connection->selectFromWriteConnection( $this->grammar->compileColumns($this->connection->getDatabaseName(), $table) ); return $this->connection->getPostProcessor()->processColumns($results); } /** * Drop all tables from the database. * * @return void */ public function dropAllTables() { $tables = []; foreach ($this->getAllTables() as $row) { $row = (array) $row; $tables[] = reset($row); } if (empty($tables)) { return; } $this->disableForeignKeyConstraints(); $this->connection->statement( $this->grammar->compileDropAllTables($tables) ); $this->enableForeignKeyConstraints(); } /** * Drop all views from the database. * * @return void */ public function dropAllViews() { $views = []; foreach ($this->getAllViews() as $row) { $row = (array) $row; $views[] = reset($row); } if (empty($views)) { return; } $this->connection->statement( $this->grammar->compileDropAllViews($views) ); } /** * Get all of the table names for the database. * * @return array */ public function getAllTables() { return $this->connection->select( $this->grammar->compileGetAllTables() ); } /** * Get all of the view names for the database. * * @return array */ public function getAllViews() { return $this->connection->select( $this->grammar->compileGetAllViews() ); } } src/Illuminate/Database/Schema/PostgresBuilder.php 0000644 00000014315 15107327037 0016201 0 ustar 00 <?php namespace Illuminate\Database\Schema; use Illuminate\Database\Concerns\ParsesSearchPath; class PostgresBuilder extends Builder { use ParsesSearchPath { parseSearchPath as baseParseSearchPath; } /** * Create a database in the schema. * * @param string $name * @return bool */ public function createDatabase($name) { return $this->connection->statement( $this->grammar->compileCreateDatabase($name, $this->connection) ); } /** * Drop a database from the schema if the database exists. * * @param string $name * @return bool */ public function dropDatabaseIfExists($name) { return $this->connection->statement( $this->grammar->compileDropDatabaseIfExists($name) ); } /** * Determine if the given table exists. * * @param string $table * @return bool */ public function hasTable($table) { [$database, $schema, $table] = $this->parseSchemaAndTable($table); $table = $this->connection->getTablePrefix().$table; return count($this->connection->selectFromWriteConnection( $this->grammar->compileTableExists(), [$database, $schema, $table] )) > 0; } /** * Drop all tables from the database. * * @return void */ public function dropAllTables() { $tables = []; $excludedTables = $this->grammar->escapeNames( $this->connection->getConfig('dont_drop') ?? ['spatial_ref_sys'] ); foreach ($this->getAllTables() as $row) { $row = (array) $row; if (empty(array_intersect($this->grammar->escapeNames($row), $excludedTables))) { $tables[] = $row['qualifiedname'] ?? reset($row); } } if (empty($tables)) { return; } $this->connection->statement( $this->grammar->compileDropAllTables($tables) ); } /** * Drop all views from the database. * * @return void */ public function dropAllViews() { $views = []; foreach ($this->getAllViews() as $row) { $row = (array) $row; $views[] = $row['qualifiedname'] ?? reset($row); } if (empty($views)) { return; } $this->connection->statement( $this->grammar->compileDropAllViews($views) ); } /** * Drop all types from the database. * * @return void */ public function dropAllTypes() { $types = []; foreach ($this->getAllTypes() as $row) { $row = (array) $row; $types[] = reset($row); } if (empty($types)) { return; } $this->connection->statement( $this->grammar->compileDropAllTypes($types) ); } /** * Get all of the table names for the database. * * @return array */ public function getAllTables() { return $this->connection->select( $this->grammar->compileGetAllTables( $this->parseSearchPath( $this->connection->getConfig('search_path') ?: $this->connection->getConfig('schema') ) ) ); } /** * Get all of the view names for the database. * * @return array */ public function getAllViews() { return $this->connection->select( $this->grammar->compileGetAllViews( $this->parseSearchPath( $this->connection->getConfig('search_path') ?: $this->connection->getConfig('schema') ) ) ); } /** * Get all of the type names for the database. * * @return array */ public function getAllTypes() { return $this->connection->select( $this->grammar->compileGetAllTypes() ); } /** * Get the columns for a given table. * * @param string $table * @return array */ public function getColumns($table) { [$database, $schema, $table] = $this->parseSchemaAndTable($table); $table = $this->connection->getTablePrefix().$table; $results = $this->connection->selectFromWriteConnection( $this->grammar->compileColumns($database, $schema, $table) ); return $this->connection->getPostProcessor()->processColumns($results); } /** * Parse the database object reference and extract the database, schema, and table. * * @param string $reference * @return array */ protected function parseSchemaAndTable($reference) { $searchPath = $this->parseSearchPath( $this->connection->getConfig('search_path') ?: $this->connection->getConfig('schema') ?: 'public' ); $parts = explode('.', $reference); $database = $this->connection->getConfig('database'); // If the reference contains a database name, we will use that instead of the // default database name for the connection. This allows the database name // to be specified in the query instead of at the full connection level. if (count($parts) === 3) { $database = $parts[0]; array_shift($parts); } // We will use the default schema unless the schema has been specified in the // query. If the schema has been specified in the query then we can use it // instead of a default schema configured in the connection search path. $schema = $searchPath[0]; if (count($parts) === 2) { $schema = $parts[0]; array_shift($parts); } return [$database, $schema, $parts[0]]; } /** * Parse the "search_path" configuration value into an array. * * @param string|array|null $searchPath * @return array */ protected function parseSearchPath($searchPath) { return array_map(function ($schema) { return $schema === '$user' ? $this->connection->getConfig('username') : $schema; }, $this->baseParseSearchPath($searchPath)); } } src/Illuminate/Database/Schema/Builder.php 0000644 00000031072 15107327037 0014451 0 ustar 00 <?php namespace Illuminate\Database\Schema; use Closure; use Illuminate\Container\Container; use Illuminate\Database\Connection; use InvalidArgumentException; use LogicException; class Builder { /** * The database connection instance. * * @var \Illuminate\Database\Connection */ protected $connection; /** * The schema grammar instance. * * @var \Illuminate\Database\Schema\Grammars\Grammar */ protected $grammar; /** * The Blueprint resolver callback. * * @var \Closure */ protected $resolver; /** * The default string length for migrations. * * @var int|null */ public static $defaultStringLength = 255; /** * The default relationship morph key type. * * @var string */ public static $defaultMorphKeyType = 'int'; /** * Indicates whether Doctrine DBAL usage will be prevented if possible when dropping, renaming, and modifying columns. * * @var bool */ public static $alwaysUsesNativeSchemaOperationsIfPossible = false; /** * Create a new database Schema manager. * * @param \Illuminate\Database\Connection $connection * @return void */ public function __construct(Connection $connection) { $this->connection = $connection; $this->grammar = $connection->getSchemaGrammar(); } /** * Set the default string length for migrations. * * @param int $length * @return void */ public static function defaultStringLength($length) { static::$defaultStringLength = $length; } /** * Set the default morph key type for migrations. * * @param string $type * @return void * * @throws \InvalidArgumentException */ public static function defaultMorphKeyType(string $type) { if (! in_array($type, ['int', 'uuid', 'ulid'])) { throw new InvalidArgumentException("Morph key type must be 'int', 'uuid', or 'ulid'."); } static::$defaultMorphKeyType = $type; } /** * Set the default morph key type for migrations to UUIDs. * * @return void */ public static function morphUsingUuids() { return static::defaultMorphKeyType('uuid'); } /** * Set the default morph key type for migrations to ULIDs. * * @return void */ public static function morphUsingUlids() { return static::defaultMorphKeyType('ulid'); } /** * Attempt to use native schema operations for dropping, renaming, and modifying columns, even if Doctrine DBAL is installed. * * @param bool $value * @return void */ public static function useNativeSchemaOperationsIfPossible(bool $value = true) { static::$alwaysUsesNativeSchemaOperationsIfPossible = $value; } /** * Create a database in the schema. * * @param string $name * @return bool * * @throws \LogicException */ public function createDatabase($name) { throw new LogicException('This database driver does not support creating databases.'); } /** * Drop a database from the schema if the database exists. * * @param string $name * @return bool * * @throws \LogicException */ public function dropDatabaseIfExists($name) { throw new LogicException('This database driver does not support dropping databases.'); } /** * Determine if the given table exists. * * @param string $table * @return bool */ public function hasTable($table) { $table = $this->connection->getTablePrefix().$table; return count($this->connection->selectFromWriteConnection( $this->grammar->compileTableExists(), [$table] )) > 0; } /** * Determine if the given table has a given column. * * @param string $table * @param string $column * @return bool */ public function hasColumn($table, $column) { return in_array( strtolower($column), array_map('strtolower', $this->getColumnListing($table)) ); } /** * Determine if the given table has given columns. * * @param string $table * @param array $columns * @return bool */ public function hasColumns($table, array $columns) { $tableColumns = array_map('strtolower', $this->getColumnListing($table)); foreach ($columns as $column) { if (! in_array(strtolower($column), $tableColumns)) { return false; } } return true; } /** * Execute a table builder callback if the given table has a given column. * * @param string $table * @param string $column * @param \Closure $callback * @return void */ public function whenTableHasColumn(string $table, string $column, Closure $callback) { if ($this->hasColumn($table, $column)) { $this->table($table, fn (Blueprint $table) => $callback($table)); } } /** * Execute a table builder callback if the given table doesn't have a given column. * * @param string $table * @param string $column * @param \Closure $callback * @return void */ public function whenTableDoesntHaveColumn(string $table, string $column, Closure $callback) { if (! $this->hasColumn($table, $column)) { $this->table($table, fn (Blueprint $table) => $callback($table)); } } /** * Get the data type for the given column name. * * @param string $table * @param string $column * @param bool $fullDefinition * @return string */ public function getColumnType($table, $column, $fullDefinition = false) { if (! $this->connection->usingNativeSchemaOperations()) { $table = $this->connection->getTablePrefix().$table; return $this->connection->getDoctrineColumn($table, $column)->getType()->getName(); } $columns = $this->getColumns($table); foreach ($columns as $value) { if (strtolower($value['name']) === $column) { return $fullDefinition ? $value['type'] : $value['type_name']; } } throw new InvalidArgumentException("There is no column with name '$column' on table '$table'."); } /** * Get the column listing for a given table. * * @param string $table * @return array */ public function getColumnListing($table) { return array_column($this->getColumns($table), 'name'); } /** * Get the columns for a given table. * * @param string $table * @return array */ public function getColumns($table) { $table = $this->connection->getTablePrefix().$table; return $this->connection->getPostProcessor()->processColumns( $this->connection->selectFromWriteConnection($this->grammar->compileColumns($table)) ); } /** * Modify a table on the schema. * * @param string $table * @param \Closure $callback * @return void */ public function table($table, Closure $callback) { $this->build($this->createBlueprint($table, $callback)); } /** * Create a new table on the schema. * * @param string $table * @param \Closure $callback * @return void */ public function create($table, Closure $callback) { $this->build(tap($this->createBlueprint($table), function ($blueprint) use ($callback) { $blueprint->create(); $callback($blueprint); })); } /** * Drop a table from the schema. * * @param string $table * @return void */ public function drop($table) { $this->build(tap($this->createBlueprint($table), function ($blueprint) { $blueprint->drop(); })); } /** * Drop a table from the schema if it exists. * * @param string $table * @return void */ public function dropIfExists($table) { $this->build(tap($this->createBlueprint($table), function ($blueprint) { $blueprint->dropIfExists(); })); } /** * Drop columns from a table schema. * * @param string $table * @param string|array $columns * @return void */ public function dropColumns($table, $columns) { $this->table($table, function (Blueprint $blueprint) use ($columns) { $blueprint->dropColumn($columns); }); } /** * Drop all tables from the database. * * @return void * * @throws \LogicException */ public function dropAllTables() { throw new LogicException('This database driver does not support dropping all tables.'); } /** * Drop all views from the database. * * @return void * * @throws \LogicException */ public function dropAllViews() { throw new LogicException('This database driver does not support dropping all views.'); } /** * Drop all types from the database. * * @return void * * @throws \LogicException */ public function dropAllTypes() { throw new LogicException('This database driver does not support dropping all types.'); } /** * Get all of the table names for the database. * * @return array * * @throws \LogicException */ public function getAllTables() { throw new LogicException('This database driver does not support getting all tables.'); } /** * Rename a table on the schema. * * @param string $from * @param string $to * @return void */ public function rename($from, $to) { $this->build(tap($this->createBlueprint($from), function ($blueprint) use ($to) { $blueprint->rename($to); })); } /** * Enable foreign key constraints. * * @return bool */ public function enableForeignKeyConstraints() { return $this->connection->statement( $this->grammar->compileEnableForeignKeyConstraints() ); } /** * Disable foreign key constraints. * * @return bool */ public function disableForeignKeyConstraints() { return $this->connection->statement( $this->grammar->compileDisableForeignKeyConstraints() ); } /** * Disable foreign key constraints during the execution of a callback. * * @param \Closure $callback * @return mixed */ public function withoutForeignKeyConstraints(Closure $callback) { $this->disableForeignKeyConstraints(); try { return $callback(); } finally { $this->enableForeignKeyConstraints(); } } /** * Execute the blueprint to build / modify the table. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @return void */ protected function build(Blueprint $blueprint) { $blueprint->build($this->connection, $this->grammar); } /** * Create a new command set with a Closure. * * @param string $table * @param \Closure|null $callback * @return \Illuminate\Database\Schema\Blueprint */ protected function createBlueprint($table, Closure $callback = null) { $prefix = $this->connection->getConfig('prefix_indexes') ? $this->connection->getConfig('prefix') : ''; if (isset($this->resolver)) { return call_user_func($this->resolver, $table, $callback, $prefix); } return Container::getInstance()->make(Blueprint::class, compact('table', 'callback', 'prefix')); } /** * Get the database connection instance. * * @return \Illuminate\Database\Connection */ public function getConnection() { return $this->connection; } /** * Set the database connection instance. * * @param \Illuminate\Database\Connection $connection * @return $this */ public function setConnection(Connection $connection) { $this->connection = $connection; return $this; } /** * Set the Schema Blueprint resolver callback. * * @param \Closure $resolver * @return void */ public function blueprintResolver(Closure $resolver) { $this->resolver = $resolver; } } src/Illuminate/Database/Schema/ForeignIdColumnDefinition.php 0000644 00000003032 15107327037 0020113 0 ustar 00 <?php namespace Illuminate\Database\Schema; use Illuminate\Support\Str; class ForeignIdColumnDefinition extends ColumnDefinition { /** * The schema builder blueprint instance. * * @var \Illuminate\Database\Schema\Blueprint */ protected $blueprint; /** * Create a new foreign ID column definition. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param array $attributes * @return void */ public function __construct(Blueprint $blueprint, $attributes = []) { parent::__construct($attributes); $this->blueprint = $blueprint; } /** * Create a foreign key constraint on this column referencing the "id" column of the conventionally related table. * * @param string|null $table * @param string|null $column * @param string|null $indexName * @return \Illuminate\Database\Schema\ForeignKeyDefinition */ public function constrained($table = null, $column = 'id', $indexName = null) { return $this->references($column, $indexName)->on($table ?? Str::of($this->name)->beforeLast('_'.$column)->plural()); } /** * Specify which column this foreign ID references on another table. * * @param string $column * @param string $indexName * @return \Illuminate\Database\Schema\ForeignKeyDefinition */ public function references($column, $indexName = null) { return $this->blueprint->foreign($this->name, $indexName)->references($column); } } src/Illuminate/Database/Schema/Blueprint.php 0000644 00000142663 15107327037 0015040 0 ustar 00 <?php namespace Illuminate\Database\Schema; use BadMethodCallException; use Closure; use Illuminate\Database\Connection; use Illuminate\Database\Eloquent\Concerns\HasUlids; use Illuminate\Database\Query\Expression; use Illuminate\Database\Schema\Grammars\Grammar; use Illuminate\Database\SQLiteConnection; use Illuminate\Support\Fluent; use Illuminate\Support\Traits\Macroable; class Blueprint { use Macroable; /** * The table the blueprint describes. * * @var string */ protected $table; /** * The prefix of the table. * * @var string */ protected $prefix; /** * The columns that should be added to the table. * * @var \Illuminate\Database\Schema\ColumnDefinition[] */ protected $columns = []; /** * The commands that should be run for the table. * * @var \Illuminate\Support\Fluent[] */ protected $commands = []; /** * The storage engine that should be used for the table. * * @var string */ public $engine; /** * The default character set that should be used for the table. * * @var string */ public $charset; /** * The collation that should be used for the table. * * @var string */ public $collation; /** * Whether to make the table temporary. * * @var bool */ public $temporary = false; /** * The column to add new columns after. * * @var string */ public $after; /** * Create a new schema blueprint. * * @param string $table * @param \Closure|null $callback * @param string $prefix * @return void */ public function __construct($table, Closure $callback = null, $prefix = '') { $this->table = $table; $this->prefix = $prefix; if (! is_null($callback)) { $callback($this); } } /** * Execute the blueprint against the database. * * @param \Illuminate\Database\Connection $connection * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar * @return void */ public function build(Connection $connection, Grammar $grammar) { foreach ($this->toSql($connection, $grammar) as $statement) { $connection->statement($statement); } } /** * Get the raw SQL statements for the blueprint. * * @param \Illuminate\Database\Connection $connection * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar * @return array */ public function toSql(Connection $connection, Grammar $grammar) { $this->addImpliedCommands($connection, $grammar); $statements = []; // Each type of command has a corresponding compiler function on the schema // grammar which is used to build the necessary SQL statements to build // the blueprint element, so we'll just call that compilers function. $this->ensureCommandsAreValid($connection); foreach ($this->commands as $command) { $method = 'compile'.ucfirst($command->name); if (method_exists($grammar, $method) || $grammar::hasMacro($method)) { if (! is_null($sql = $grammar->$method($this, $command, $connection))) { $statements = array_merge($statements, (array) $sql); } } } return $statements; } /** * Ensure the commands on the blueprint are valid for the connection type. * * @param \Illuminate\Database\Connection $connection * @return void * * @throws \BadMethodCallException */ protected function ensureCommandsAreValid(Connection $connection) { if ($connection instanceof SQLiteConnection) { if ($this->commandsNamed(['dropColumn', 'renameColumn'])->count() > 1 && ! $connection->usingNativeSchemaOperations()) { throw new BadMethodCallException( "SQLite doesn't support multiple calls to dropColumn / renameColumn in a single modification." ); } if ($this->commandsNamed(['dropForeign'])->count() > 0) { throw new BadMethodCallException( "SQLite doesn't support dropping foreign keys (you would need to re-create the table)." ); } } } /** * Get all of the commands matching the given names. * * @param array $names * @return \Illuminate\Support\Collection */ protected function commandsNamed(array $names) { return collect($this->commands)->filter(function ($command) use ($names) { return in_array($command->name, $names); }); } /** * Add the commands that are implied by the blueprint's state. * * @param \Illuminate\Database\Connection $connection * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar * @return void */ protected function addImpliedCommands(Connection $connection, Grammar $grammar) { if (count($this->getAddedColumns()) > 0 && ! $this->creating()) { array_unshift($this->commands, $this->createCommand('add')); } if (count($this->getChangedColumns()) > 0 && ! $this->creating()) { array_unshift($this->commands, $this->createCommand('change')); } $this->addFluentIndexes(); $this->addFluentCommands($connection, $grammar); } /** * Add the index commands fluently specified on columns. * * @return void */ protected function addFluentIndexes() { foreach ($this->columns as $column) { foreach (['primary', 'unique', 'index', 'fulltext', 'fullText', 'spatialIndex'] as $index) { // If the index has been specified on the given column, but is simply equal // to "true" (boolean), no name has been specified for this index so the // index method can be called without a name and it will generate one. if ($column->{$index} === true) { $this->{$index}($column->name); $column->{$index} = null; continue 2; } // If the index has been specified on the given column, but it equals false // and the column is supposed to be changed, we will call the drop index // method with an array of column to drop it by its conventional name. elseif ($column->{$index} === false && $column->change) { $this->{'drop'.ucfirst($index)}([$column->name]); $column->{$index} = null; continue 2; } // If the index has been specified on the given column, and it has a string // value, we'll go ahead and call the index method and pass the name for // the index since the developer specified the explicit name for this. elseif (isset($column->{$index})) { $this->{$index}($column->name, $column->{$index}); $column->{$index} = null; continue 2; } } } } /** * Add the fluent commands specified on any columns. * * @param \Illuminate\Database\Connection $connection * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar * @return void */ public function addFluentCommands(Connection $connection, Grammar $grammar) { foreach ($this->columns as $column) { if ($column->change && ! $connection->usingNativeSchemaOperations()) { continue; } foreach ($grammar->getFluentCommands() as $commandName) { $this->addCommand($commandName, compact('column')); } } } /** * Determine if the blueprint has a create command. * * @return bool */ public function creating() { return collect($this->commands)->contains(function ($command) { return $command->name === 'create'; }); } /** * Indicate that the table needs to be created. * * @return \Illuminate\Support\Fluent */ public function create() { return $this->addCommand('create'); } /** * Indicate that the table needs to be temporary. * * @return void */ public function temporary() { $this->temporary = true; } /** * Indicate that the table should be dropped. * * @return \Illuminate\Support\Fluent */ public function drop() { return $this->addCommand('drop'); } /** * Indicate that the table should be dropped if it exists. * * @return \Illuminate\Support\Fluent */ public function dropIfExists() { return $this->addCommand('dropIfExists'); } /** * Indicate that the given columns should be dropped. * * @param array|mixed $columns * @return \Illuminate\Support\Fluent */ public function dropColumn($columns) { $columns = is_array($columns) ? $columns : func_get_args(); return $this->addCommand('dropColumn', compact('columns')); } /** * Indicate that the given columns should be renamed. * * @param string $from * @param string $to * @return \Illuminate\Support\Fluent */ public function renameColumn($from, $to) { return $this->addCommand('renameColumn', compact('from', 'to')); } /** * Indicate that the given primary key should be dropped. * * @param string|array|null $index * @return \Illuminate\Support\Fluent */ public function dropPrimary($index = null) { return $this->dropIndexCommand('dropPrimary', 'primary', $index); } /** * Indicate that the given unique key should be dropped. * * @param string|array $index * @return \Illuminate\Support\Fluent */ public function dropUnique($index) { return $this->dropIndexCommand('dropUnique', 'unique', $index); } /** * Indicate that the given index should be dropped. * * @param string|array $index * @return \Illuminate\Support\Fluent */ public function dropIndex($index) { return $this->dropIndexCommand('dropIndex', 'index', $index); } /** * Indicate that the given fulltext index should be dropped. * * @param string|array $index * @return \Illuminate\Support\Fluent */ public function dropFullText($index) { return $this->dropIndexCommand('dropFullText', 'fulltext', $index); } /** * Indicate that the given spatial index should be dropped. * * @param string|array $index * @return \Illuminate\Support\Fluent */ public function dropSpatialIndex($index) { return $this->dropIndexCommand('dropSpatialIndex', 'spatialIndex', $index); } /** * Indicate that the given foreign key should be dropped. * * @param string|array $index * @return \Illuminate\Support\Fluent */ public function dropForeign($index) { return $this->dropIndexCommand('dropForeign', 'foreign', $index); } /** * Indicate that the given column and foreign key should be dropped. * * @param string $column * @return \Illuminate\Support\Fluent */ public function dropConstrainedForeignId($column) { $this->dropForeign([$column]); return $this->dropColumn($column); } /** * Indicate that the given foreign key should be dropped. * * @param \Illuminate\Database\Eloquent\Model|string $model * @param string|null $column * @return \Illuminate\Support\Fluent */ public function dropForeignIdFor($model, $column = null) { if (is_string($model)) { $model = new $model; } return $this->dropForeign([$column ?: $model->getForeignKey()]); } /** * Indicate that the given foreign key should be dropped. * * @param \Illuminate\Database\Eloquent\Model|string $model * @param string|null $column * @return \Illuminate\Support\Fluent */ public function dropConstrainedForeignIdFor($model, $column = null) { if (is_string($model)) { $model = new $model; } return $this->dropConstrainedForeignId($column ?: $model->getForeignKey()); } /** * Indicate that the given indexes should be renamed. * * @param string $from * @param string $to * @return \Illuminate\Support\Fluent */ public function renameIndex($from, $to) { return $this->addCommand('renameIndex', compact('from', 'to')); } /** * Indicate that the timestamp columns should be dropped. * * @return void */ public function dropTimestamps() { $this->dropColumn('created_at', 'updated_at'); } /** * Indicate that the timestamp columns should be dropped. * * @return void */ public function dropTimestampsTz() { $this->dropTimestamps(); } /** * Indicate that the soft delete column should be dropped. * * @param string $column * @return void */ public function dropSoftDeletes($column = 'deleted_at') { $this->dropColumn($column); } /** * Indicate that the soft delete column should be dropped. * * @param string $column * @return void */ public function dropSoftDeletesTz($column = 'deleted_at') { $this->dropSoftDeletes($column); } /** * Indicate that the remember token column should be dropped. * * @return void */ public function dropRememberToken() { $this->dropColumn('remember_token'); } /** * Indicate that the polymorphic columns should be dropped. * * @param string $name * @param string|null $indexName * @return void */ public function dropMorphs($name, $indexName = null) { $this->dropIndex($indexName ?: $this->createIndexName('index', ["{$name}_type", "{$name}_id"])); $this->dropColumn("{$name}_type", "{$name}_id"); } /** * Rename the table to a given name. * * @param string $to * @return \Illuminate\Support\Fluent */ public function rename($to) { return $this->addCommand('rename', compact('to')); } /** * Specify the primary key(s) for the table. * * @param string|array $columns * @param string|null $name * @param string|null $algorithm * @return \Illuminate\Database\Schema\IndexDefinition */ public function primary($columns, $name = null, $algorithm = null) { return $this->indexCommand('primary', $columns, $name, $algorithm); } /** * Specify a unique index for the table. * * @param string|array $columns * @param string|null $name * @param string|null $algorithm * @return \Illuminate\Database\Schema\IndexDefinition */ public function unique($columns, $name = null, $algorithm = null) { return $this->indexCommand('unique', $columns, $name, $algorithm); } /** * Specify an index for the table. * * @param string|array $columns * @param string|null $name * @param string|null $algorithm * @return \Illuminate\Database\Schema\IndexDefinition */ public function index($columns, $name = null, $algorithm = null) { return $this->indexCommand('index', $columns, $name, $algorithm); } /** * Specify an fulltext for the table. * * @param string|array $columns * @param string|null $name * @param string|null $algorithm * @return \Illuminate\Database\Schema\IndexDefinition */ public function fullText($columns, $name = null, $algorithm = null) { return $this->indexCommand('fulltext', $columns, $name, $algorithm); } /** * Specify a spatial index for the table. * * @param string|array $columns * @param string|null $name * @return \Illuminate\Database\Schema\IndexDefinition */ public function spatialIndex($columns, $name = null) { return $this->indexCommand('spatialIndex', $columns, $name); } /** * Specify a raw index for the table. * * @param string $expression * @param string $name * @return \Illuminate\Database\Schema\IndexDefinition */ public function rawIndex($expression, $name) { return $this->index([new Expression($expression)], $name); } /** * Specify a foreign key for the table. * * @param string|array $columns * @param string|null $name * @return \Illuminate\Database\Schema\ForeignKeyDefinition */ public function foreign($columns, $name = null) { $command = new ForeignKeyDefinition( $this->indexCommand('foreign', $columns, $name)->getAttributes() ); $this->commands[count($this->commands) - 1] = $command; return $command; } /** * Create a new auto-incrementing big integer (8-byte) column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function id($column = 'id') { return $this->bigIncrements($column); } /** * Create a new auto-incrementing integer (4-byte) column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function increments($column) { return $this->unsignedInteger($column, true); } /** * Create a new auto-incrementing integer (4-byte) column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function integerIncrements($column) { return $this->unsignedInteger($column, true); } /** * Create a new auto-incrementing tiny integer (1-byte) column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function tinyIncrements($column) { return $this->unsignedTinyInteger($column, true); } /** * Create a new auto-incrementing small integer (2-byte) column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function smallIncrements($column) { return $this->unsignedSmallInteger($column, true); } /** * Create a new auto-incrementing medium integer (3-byte) column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function mediumIncrements($column) { return $this->unsignedMediumInteger($column, true); } /** * Create a new auto-incrementing big integer (8-byte) column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function bigIncrements($column) { return $this->unsignedBigInteger($column, true); } /** * Create a new char column on the table. * * @param string $column * @param int|null $length * @return \Illuminate\Database\Schema\ColumnDefinition */ public function char($column, $length = null) { $length = ! is_null($length) ? $length : Builder::$defaultStringLength; return $this->addColumn('char', $column, compact('length')); } /** * Create a new string column on the table. * * @param string $column * @param int|null $length * @return \Illuminate\Database\Schema\ColumnDefinition */ public function string($column, $length = null) { $length = $length ?: Builder::$defaultStringLength; return $this->addColumn('string', $column, compact('length')); } /** * Create a new tiny text column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function tinyText($column) { return $this->addColumn('tinyText', $column); } /** * Create a new text column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function text($column) { return $this->addColumn('text', $column); } /** * Create a new medium text column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function mediumText($column) { return $this->addColumn('mediumText', $column); } /** * Create a new long text column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function longText($column) { return $this->addColumn('longText', $column); } /** * Create a new integer (4-byte) column on the table. * * @param string $column * @param bool $autoIncrement * @param bool $unsigned * @return \Illuminate\Database\Schema\ColumnDefinition */ public function integer($column, $autoIncrement = false, $unsigned = false) { return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned')); } /** * Create a new tiny integer (1-byte) column on the table. * * @param string $column * @param bool $autoIncrement * @param bool $unsigned * @return \Illuminate\Database\Schema\ColumnDefinition */ public function tinyInteger($column, $autoIncrement = false, $unsigned = false) { return $this->addColumn('tinyInteger', $column, compact('autoIncrement', 'unsigned')); } /** * Create a new small integer (2-byte) column on the table. * * @param string $column * @param bool $autoIncrement * @param bool $unsigned * @return \Illuminate\Database\Schema\ColumnDefinition */ public function smallInteger($column, $autoIncrement = false, $unsigned = false) { return $this->addColumn('smallInteger', $column, compact('autoIncrement', 'unsigned')); } /** * Create a new medium integer (3-byte) column on the table. * * @param string $column * @param bool $autoIncrement * @param bool $unsigned * @return \Illuminate\Database\Schema\ColumnDefinition */ public function mediumInteger($column, $autoIncrement = false, $unsigned = false) { return $this->addColumn('mediumInteger', $column, compact('autoIncrement', 'unsigned')); } /** * Create a new big integer (8-byte) column on the table. * * @param string $column * @param bool $autoIncrement * @param bool $unsigned * @return \Illuminate\Database\Schema\ColumnDefinition */ public function bigInteger($column, $autoIncrement = false, $unsigned = false) { return $this->addColumn('bigInteger', $column, compact('autoIncrement', 'unsigned')); } /** * Create a new unsigned integer (4-byte) column on the table. * * @param string $column * @param bool $autoIncrement * @return \Illuminate\Database\Schema\ColumnDefinition */ public function unsignedInteger($column, $autoIncrement = false) { return $this->integer($column, $autoIncrement, true); } /** * Create a new unsigned tiny integer (1-byte) column on the table. * * @param string $column * @param bool $autoIncrement * @return \Illuminate\Database\Schema\ColumnDefinition */ public function unsignedTinyInteger($column, $autoIncrement = false) { return $this->tinyInteger($column, $autoIncrement, true); } /** * Create a new unsigned small integer (2-byte) column on the table. * * @param string $column * @param bool $autoIncrement * @return \Illuminate\Database\Schema\ColumnDefinition */ public function unsignedSmallInteger($column, $autoIncrement = false) { return $this->smallInteger($column, $autoIncrement, true); } /** * Create a new unsigned medium integer (3-byte) column on the table. * * @param string $column * @param bool $autoIncrement * @return \Illuminate\Database\Schema\ColumnDefinition */ public function unsignedMediumInteger($column, $autoIncrement = false) { return $this->mediumInteger($column, $autoIncrement, true); } /** * Create a new unsigned big integer (8-byte) column on the table. * * @param string $column * @param bool $autoIncrement * @return \Illuminate\Database\Schema\ColumnDefinition */ public function unsignedBigInteger($column, $autoIncrement = false) { return $this->bigInteger($column, $autoIncrement, true); } /** * Create a new unsigned big integer (8-byte) column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ForeignIdColumnDefinition */ public function foreignId($column) { return $this->addColumnDefinition(new ForeignIdColumnDefinition($this, [ 'type' => 'bigInteger', 'name' => $column, 'autoIncrement' => false, 'unsigned' => true, ])); } /** * Create a foreign ID column for the given model. * * @param \Illuminate\Database\Eloquent\Model|string $model * @param string|null $column * @return \Illuminate\Database\Schema\ForeignIdColumnDefinition */ public function foreignIdFor($model, $column = null) { if (is_string($model)) { $model = new $model; } $column = $column ?: $model->getForeignKey(); if ($model->getKeyType() === 'int' && $model->getIncrementing()) { return $this->foreignId($column); } $modelTraits = class_uses_recursive($model); if (in_array(HasUlids::class, $modelTraits, true)) { return $this->foreignUlid($column); } return $this->foreignUuid($column); } /** * Create a new float column on the table. * * @param string $column * @param int $total * @param int $places * @param bool $unsigned * @return \Illuminate\Database\Schema\ColumnDefinition */ public function float($column, $total = 8, $places = 2, $unsigned = false) { return $this->addColumn('float', $column, compact('total', 'places', 'unsigned')); } /** * Create a new double column on the table. * * @param string $column * @param int|null $total * @param int|null $places * @param bool $unsigned * @return \Illuminate\Database\Schema\ColumnDefinition */ public function double($column, $total = null, $places = null, $unsigned = false) { return $this->addColumn('double', $column, compact('total', 'places', 'unsigned')); } /** * Create a new decimal column on the table. * * @param string $column * @param int $total * @param int $places * @param bool $unsigned * @return \Illuminate\Database\Schema\ColumnDefinition */ public function decimal($column, $total = 8, $places = 2, $unsigned = false) { return $this->addColumn('decimal', $column, compact('total', 'places', 'unsigned')); } /** * Create a new unsigned float column on the table. * * @param string $column * @param int $total * @param int $places * @return \Illuminate\Database\Schema\ColumnDefinition */ public function unsignedFloat($column, $total = 8, $places = 2) { return $this->float($column, $total, $places, true); } /** * Create a new unsigned double column on the table. * * @param string $column * @param int $total * @param int $places * @return \Illuminate\Database\Schema\ColumnDefinition */ public function unsignedDouble($column, $total = null, $places = null) { return $this->double($column, $total, $places, true); } /** * Create a new unsigned decimal column on the table. * * @param string $column * @param int $total * @param int $places * @return \Illuminate\Database\Schema\ColumnDefinition */ public function unsignedDecimal($column, $total = 8, $places = 2) { return $this->decimal($column, $total, $places, true); } /** * Create a new boolean column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function boolean($column) { return $this->addColumn('boolean', $column); } /** * Create a new enum column on the table. * * @param string $column * @param array $allowed * @return \Illuminate\Database\Schema\ColumnDefinition */ public function enum($column, array $allowed) { return $this->addColumn('enum', $column, compact('allowed')); } /** * Create a new set column on the table. * * @param string $column * @param array $allowed * @return \Illuminate\Database\Schema\ColumnDefinition */ public function set($column, array $allowed) { return $this->addColumn('set', $column, compact('allowed')); } /** * Create a new json column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function json($column) { return $this->addColumn('json', $column); } /** * Create a new jsonb column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function jsonb($column) { return $this->addColumn('jsonb', $column); } /** * Create a new date column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function date($column) { return $this->addColumn('date', $column); } /** * Create a new date-time column on the table. * * @param string $column * @param int|null $precision * @return \Illuminate\Database\Schema\ColumnDefinition */ public function dateTime($column, $precision = 0) { return $this->addColumn('dateTime', $column, compact('precision')); } /** * Create a new date-time column (with time zone) on the table. * * @param string $column * @param int|null $precision * @return \Illuminate\Database\Schema\ColumnDefinition */ public function dateTimeTz($column, $precision = 0) { return $this->addColumn('dateTimeTz', $column, compact('precision')); } /** * Create a new time column on the table. * * @param string $column * @param int|null $precision * @return \Illuminate\Database\Schema\ColumnDefinition */ public function time($column, $precision = 0) { return $this->addColumn('time', $column, compact('precision')); } /** * Create a new time column (with time zone) on the table. * * @param string $column * @param int|null $precision * @return \Illuminate\Database\Schema\ColumnDefinition */ public function timeTz($column, $precision = 0) { return $this->addColumn('timeTz', $column, compact('precision')); } /** * Create a new timestamp column on the table. * * @param string $column * @param int|null $precision * @return \Illuminate\Database\Schema\ColumnDefinition */ public function timestamp($column, $precision = 0) { return $this->addColumn('timestamp', $column, compact('precision')); } /** * Create a new timestamp (with time zone) column on the table. * * @param string $column * @param int|null $precision * @return \Illuminate\Database\Schema\ColumnDefinition */ public function timestampTz($column, $precision = 0) { return $this->addColumn('timestampTz', $column, compact('precision')); } /** * Add nullable creation and update timestamps to the table. * * @param int|null $precision * @return void */ public function timestamps($precision = 0) { $this->timestamp('created_at', $precision)->nullable(); $this->timestamp('updated_at', $precision)->nullable(); } /** * Add nullable creation and update timestamps to the table. * * Alias for self::timestamps(). * * @param int|null $precision * @return void */ public function nullableTimestamps($precision = 0) { $this->timestamps($precision); } /** * Add creation and update timestampTz columns to the table. * * @param int|null $precision * @return void */ public function timestampsTz($precision = 0) { $this->timestampTz('created_at', $precision)->nullable(); $this->timestampTz('updated_at', $precision)->nullable(); } /** * Add creation and update datetime columns to the table. * * @param int|null $precision * @return void */ public function datetimes($precision = 0) { $this->datetime('created_at', $precision)->nullable(); $this->datetime('updated_at', $precision)->nullable(); } /** * Add a "deleted at" timestamp for the table. * * @param string $column * @param int|null $precision * @return \Illuminate\Database\Schema\ColumnDefinition */ public function softDeletes($column = 'deleted_at', $precision = 0) { return $this->timestamp($column, $precision)->nullable(); } /** * Add a "deleted at" timestampTz for the table. * * @param string $column * @param int|null $precision * @return \Illuminate\Database\Schema\ColumnDefinition */ public function softDeletesTz($column = 'deleted_at', $precision = 0) { return $this->timestampTz($column, $precision)->nullable(); } /** * Add a "deleted at" datetime column to the table. * * @param string $column * @param int|null $precision * @return \Illuminate\Database\Schema\ColumnDefinition */ public function softDeletesDatetime($column = 'deleted_at', $precision = 0) { return $this->datetime($column, $precision)->nullable(); } /** * Create a new year column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function year($column) { return $this->addColumn('year', $column); } /** * Create a new binary column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function binary($column) { return $this->addColumn('binary', $column); } /** * Create a new UUID column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function uuid($column = 'uuid') { return $this->addColumn('uuid', $column); } /** * Create a new UUID column on the table with a foreign key constraint. * * @param string $column * @return \Illuminate\Database\Schema\ForeignIdColumnDefinition */ public function foreignUuid($column) { return $this->addColumnDefinition(new ForeignIdColumnDefinition($this, [ 'type' => 'uuid', 'name' => $column, ])); } /** * Create a new ULID column on the table. * * @param string $column * @param int|null $length * @return \Illuminate\Database\Schema\ColumnDefinition */ public function ulid($column = 'ulid', $length = 26) { return $this->char($column, $length); } /** * Create a new ULID column on the table with a foreign key constraint. * * @param string $column * @param int|null $length * @return \Illuminate\Database\Schema\ForeignIdColumnDefinition */ public function foreignUlid($column, $length = 26) { return $this->addColumnDefinition(new ForeignIdColumnDefinition($this, [ 'type' => 'char', 'name' => $column, 'length' => $length, ])); } /** * Create a new IP address column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function ipAddress($column = 'ip_address') { return $this->addColumn('ipAddress', $column); } /** * Create a new MAC address column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function macAddress($column = 'mac_address') { return $this->addColumn('macAddress', $column); } /** * Create a new geometry column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function geometry($column) { return $this->addColumn('geometry', $column); } /** * Create a new point column on the table. * * @param string $column * @param int|null $srid * @return \Illuminate\Database\Schema\ColumnDefinition */ public function point($column, $srid = null) { return $this->addColumn('point', $column, compact('srid')); } /** * Create a new linestring column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function lineString($column) { return $this->addColumn('linestring', $column); } /** * Create a new polygon column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function polygon($column) { return $this->addColumn('polygon', $column); } /** * Create a new geometrycollection column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function geometryCollection($column) { return $this->addColumn('geometrycollection', $column); } /** * Create a new multipoint column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function multiPoint($column) { return $this->addColumn('multipoint', $column); } /** * Create a new multilinestring column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function multiLineString($column) { return $this->addColumn('multilinestring', $column); } /** * Create a new multipolygon column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function multiPolygon($column) { return $this->addColumn('multipolygon', $column); } /** * Create a new multipolygon column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function multiPolygonZ($column) { return $this->addColumn('multipolygonz', $column); } /** * Create a new generated, computed column on the table. * * @param string $column * @param string $expression * @return \Illuminate\Database\Schema\ColumnDefinition */ public function computed($column, $expression) { return $this->addColumn('computed', $column, compact('expression')); } /** * Add the proper columns for a polymorphic table. * * @param string $name * @param string|null $indexName * @return void */ public function morphs($name, $indexName = null) { if (Builder::$defaultMorphKeyType === 'uuid') { $this->uuidMorphs($name, $indexName); } elseif (Builder::$defaultMorphKeyType === 'ulid') { $this->ulidMorphs($name, $indexName); } else { $this->numericMorphs($name, $indexName); } } /** * Add nullable columns for a polymorphic table. * * @param string $name * @param string|null $indexName * @return void */ public function nullableMorphs($name, $indexName = null) { if (Builder::$defaultMorphKeyType === 'uuid') { $this->nullableUuidMorphs($name, $indexName); } elseif (Builder::$defaultMorphKeyType === 'ulid') { $this->nullableUlidMorphs($name, $indexName); } else { $this->nullableNumericMorphs($name, $indexName); } } /** * Add the proper columns for a polymorphic table using numeric IDs (incremental). * * @param string $name * @param string|null $indexName * @return void */ public function numericMorphs($name, $indexName = null) { $this->string("{$name}_type"); $this->unsignedBigInteger("{$name}_id"); $this->index(["{$name}_type", "{$name}_id"], $indexName); } /** * Add nullable columns for a polymorphic table using numeric IDs (incremental). * * @param string $name * @param string|null $indexName * @return void */ public function nullableNumericMorphs($name, $indexName = null) { $this->string("{$name}_type")->nullable(); $this->unsignedBigInteger("{$name}_id")->nullable(); $this->index(["{$name}_type", "{$name}_id"], $indexName); } /** * Add the proper columns for a polymorphic table using UUIDs. * * @param string $name * @param string|null $indexName * @return void */ public function uuidMorphs($name, $indexName = null) { $this->string("{$name}_type"); $this->uuid("{$name}_id"); $this->index(["{$name}_type", "{$name}_id"], $indexName); } /** * Add nullable columns for a polymorphic table using UUIDs. * * @param string $name * @param string|null $indexName * @return void */ public function nullableUuidMorphs($name, $indexName = null) { $this->string("{$name}_type")->nullable(); $this->uuid("{$name}_id")->nullable(); $this->index(["{$name}_type", "{$name}_id"], $indexName); } /** * Add the proper columns for a polymorphic table using ULIDs. * * @param string $name * @param string|null $indexName * @return void */ public function ulidMorphs($name, $indexName = null) { $this->string("{$name}_type"); $this->ulid("{$name}_id"); $this->index(["{$name}_type", "{$name}_id"], $indexName); } /** * Add nullable columns for a polymorphic table using ULIDs. * * @param string $name * @param string|null $indexName * @return void */ public function nullableUlidMorphs($name, $indexName = null) { $this->string("{$name}_type")->nullable(); $this->ulid("{$name}_id")->nullable(); $this->index(["{$name}_type", "{$name}_id"], $indexName); } /** * Adds the `remember_token` column to the table. * * @return \Illuminate\Database\Schema\ColumnDefinition */ public function rememberToken() { return $this->string('remember_token', 100)->nullable(); } /** * Add a comment to the table. * * @param string $comment * @return \Illuminate\Support\Fluent */ public function comment($comment) { return $this->addCommand('tableComment', compact('comment')); } /** * Add a new index command to the blueprint. * * @param string $type * @param string|array $columns * @param string $index * @param string|null $algorithm * @return \Illuminate\Support\Fluent */ protected function indexCommand($type, $columns, $index, $algorithm = null) { $columns = (array) $columns; // If no name was specified for this index, we will create one using a basic // convention of the table name, followed by the columns, followed by an // index type, such as primary or index, which makes the index unique. $index = $index ?: $this->createIndexName($type, $columns); return $this->addCommand( $type, compact('index', 'columns', 'algorithm') ); } /** * Create a new drop index command on the blueprint. * * @param string $command * @param string $type * @param string|array $index * @return \Illuminate\Support\Fluent */ protected function dropIndexCommand($command, $type, $index) { $columns = []; // If the given "index" is actually an array of columns, the developer means // to drop an index merely by specifying the columns involved without the // conventional name, so we will build the index name from the columns. if (is_array($index)) { $index = $this->createIndexName($type, $columns = $index); } return $this->indexCommand($command, $columns, $index); } /** * Create a default index name for the table. * * @param string $type * @param array $columns * @return string */ protected function createIndexName($type, array $columns) { $index = strtolower($this->prefix.$this->table.'_'.implode('_', $columns).'_'.$type); return str_replace(['-', '.'], '_', $index); } /** * Add a new column to the blueprint. * * @param string $type * @param string $name * @param array $parameters * @return \Illuminate\Database\Schema\ColumnDefinition */ public function addColumn($type, $name, array $parameters = []) { return $this->addColumnDefinition(new ColumnDefinition( array_merge(compact('type', 'name'), $parameters) )); } /** * Add a new column definition to the blueprint. * * @param \Illuminate\Database\Schema\ColumnDefinition $definition * @return \Illuminate\Database\Schema\ColumnDefinition */ protected function addColumnDefinition($definition) { $this->columns[] = $definition; if ($this->after) { $definition->after($this->after); $this->after = $definition->name; } return $definition; } /** * Add the columns from the callback after the given column. * * @param string $column * @param \Closure $callback * @return void */ public function after($column, Closure $callback) { $this->after = $column; $callback($this); $this->after = null; } /** * Remove a column from the schema blueprint. * * @param string $name * @return $this */ public function removeColumn($name) { $this->columns = array_values(array_filter($this->columns, function ($c) use ($name) { return $c['name'] != $name; })); return $this; } /** * Add a new command to the blueprint. * * @param string $name * @param array $parameters * @return \Illuminate\Support\Fluent */ protected function addCommand($name, array $parameters = []) { $this->commands[] = $command = $this->createCommand($name, $parameters); return $command; } /** * Create a new Fluent command. * * @param string $name * @param array $parameters * @return \Illuminate\Support\Fluent */ protected function createCommand($name, array $parameters = []) { return new Fluent(array_merge(compact('name'), $parameters)); } /** * Get the table the blueprint describes. * * @return string */ public function getTable() { return $this->table; } /** * Get the table prefix. * * @return string */ public function getPrefix() { return $this->prefix; } /** * Get the columns on the blueprint. * * @return \Illuminate\Database\Schema\ColumnDefinition[] */ public function getColumns() { return $this->columns; } /** * Get the commands on the blueprint. * * @return \Illuminate\Support\Fluent[] */ public function getCommands() { return $this->commands; } /** * Get the columns on the blueprint that should be added. * * @return \Illuminate\Database\Schema\ColumnDefinition[] */ public function getAddedColumns() { return array_filter($this->columns, function ($column) { return ! $column->change; }); } /** * Get the columns on the blueprint that should be changed. * * @return \Illuminate\Database\Schema\ColumnDefinition[] */ public function getChangedColumns() { return array_filter($this->columns, function ($column) { return (bool) $column->change; }); } } src/Illuminate/Database/Schema/ForeignKeyDefinition.php 0000644 00000003504 15107327037 0017135 0 ustar 00 <?php namespace Illuminate\Database\Schema; use Illuminate\Support\Fluent; /** * @method ForeignKeyDefinition deferrable(bool $value = true) Set the foreign key as deferrable (PostgreSQL) * @method ForeignKeyDefinition initiallyImmediate(bool $value = true) Set the default time to check the constraint (PostgreSQL) * @method ForeignKeyDefinition on(string $table) Specify the referenced table * @method ForeignKeyDefinition onDelete(string $action) Add an ON DELETE action * @method ForeignKeyDefinition onUpdate(string $action) Add an ON UPDATE action * @method ForeignKeyDefinition references(string|array $columns) Specify the referenced column(s) */ class ForeignKeyDefinition extends Fluent { /** * Indicate that updates should cascade. * * @return $this */ public function cascadeOnUpdate() { return $this->onUpdate('cascade'); } /** * Indicate that updates should be restricted. * * @return $this */ public function restrictOnUpdate() { return $this->onUpdate('restrict'); } /** * Indicate that deletes should cascade. * * @return $this */ public function cascadeOnDelete() { return $this->onDelete('cascade'); } /** * Indicate that deletes should be restricted. * * @return $this */ public function restrictOnDelete() { return $this->onDelete('restrict'); } /** * Indicate that deletes should set the foreign key value to null. * * @return $this */ public function nullOnDelete() { return $this->onDelete('set null'); } /** * Indicate that deletes should have "no action". * * @return $this */ public function noActionOnDelete() { return $this->onDelete('no action'); } } src/Illuminate/Database/Schema/SqlServerBuilder.php 0000644 00000003230 15107327037 0016313 0 ustar 00 <?php namespace Illuminate\Database\Schema; class SqlServerBuilder extends Builder { /** * Create a database in the schema. * * @param string $name * @return bool */ public function createDatabase($name) { return $this->connection->statement( $this->grammar->compileCreateDatabase($name, $this->connection) ); } /** * Drop a database from the schema if the database exists. * * @param string $name * @return bool */ public function dropDatabaseIfExists($name) { return $this->connection->statement( $this->grammar->compileDropDatabaseIfExists($name) ); } /** * Drop all tables from the database. * * @return void */ public function dropAllTables() { $this->connection->statement($this->grammar->compileDropAllForeignKeys()); $this->connection->statement($this->grammar->compileDropAllTables()); } /** * Drop all views from the database. * * @return void */ public function dropAllViews() { $this->connection->statement($this->grammar->compileDropAllViews()); } /** * Drop all tables from the database. * * @return array */ public function getAllTables() { return $this->connection->select( $this->grammar->compileGetAllTables() ); } /** * Get all of the view names for the database. * * @return array */ public function getAllViews() { return $this->connection->select( $this->grammar->compileGetAllViews() ); } } src/Illuminate/Database/Schema/SQLiteBuilder.php 0000644 00000004434 15107327037 0015535 0 ustar 00 <?php namespace Illuminate\Database\Schema; use Illuminate\Support\Facades\File; class SQLiteBuilder extends Builder { /** * Create a database in the schema. * * @param string $name * @return bool */ public function createDatabase($name) { return File::put($name, '') !== false; } /** * Drop a database from the schema if the database exists. * * @param string $name * @return bool */ public function dropDatabaseIfExists($name) { return File::exists($name) ? File::delete($name) : true; } /** * Drop all tables from the database. * * @return void */ public function dropAllTables() { if ($this->connection->getDatabaseName() !== ':memory:') { return $this->refreshDatabaseFile(); } $this->connection->select($this->grammar->compileEnableWriteableSchema()); $this->connection->select($this->grammar->compileDropAllTables()); $this->connection->select($this->grammar->compileDisableWriteableSchema()); $this->connection->select($this->grammar->compileRebuild()); } /** * Drop all views from the database. * * @return void */ public function dropAllViews() { $this->connection->select($this->grammar->compileEnableWriteableSchema()); $this->connection->select($this->grammar->compileDropAllViews()); $this->connection->select($this->grammar->compileDisableWriteableSchema()); $this->connection->select($this->grammar->compileRebuild()); } /** * Get all of the table names for the database. * * @return array */ public function getAllTables() { return $this->connection->select( $this->grammar->compileGetAllTables() ); } /** * Get all of the view names for the database. * * @return array */ public function getAllViews() { return $this->connection->select( $this->grammar->compileGetAllViews() ); } /** * Empty the database file. * * @return void */ public function refreshDatabaseFile() { file_put_contents($this->connection->getDatabaseName(), ''); } } src/Illuminate/Database/ConnectionInterface.php 0000644 00000007474 15107327037 0015614 0 ustar 00 <?php namespace Illuminate\Database; use Closure; interface ConnectionInterface { /** * Begin a fluent query against a database table. * * @param \Closure|\Illuminate\Database\Query\Builder|string $table * @param string|null $as * @return \Illuminate\Database\Query\Builder */ public function table($table, $as = null); /** * Get a new raw query expression. * * @param mixed $value * @return \Illuminate\Contracts\Database\Query\Expression */ public function raw($value); /** * Run a select statement and return a single result. * * @param string $query * @param array $bindings * @param bool $useReadPdo * @return mixed */ public function selectOne($query, $bindings = [], $useReadPdo = true); /** * Run a select statement against the database. * * @param string $query * @param array $bindings * @param bool $useReadPdo * @return array */ public function select($query, $bindings = [], $useReadPdo = true); /** * Run a select statement against the database and returns a generator. * * @param string $query * @param array $bindings * @param bool $useReadPdo * @return \Generator */ public function cursor($query, $bindings = [], $useReadPdo = true); /** * Run an insert statement against the database. * * @param string $query * @param array $bindings * @return bool */ public function insert($query, $bindings = []); /** * Run an update statement against the database. * * @param string $query * @param array $bindings * @return int */ public function update($query, $bindings = []); /** * Run a delete statement against the database. * * @param string $query * @param array $bindings * @return int */ public function delete($query, $bindings = []); /** * Execute an SQL statement and return the boolean result. * * @param string $query * @param array $bindings * @return bool */ public function statement($query, $bindings = []); /** * Run an SQL statement and get the number of rows affected. * * @param string $query * @param array $bindings * @return int */ public function affectingStatement($query, $bindings = []); /** * Run a raw, unprepared query against the PDO connection. * * @param string $query * @return bool */ public function unprepared($query); /** * Prepare the query bindings for execution. * * @param array $bindings * @return array */ public function prepareBindings(array $bindings); /** * Execute a Closure within a transaction. * * @param \Closure $callback * @param int $attempts * @return mixed * * @throws \Throwable */ public function transaction(Closure $callback, $attempts = 1); /** * Start a new database transaction. * * @return void */ public function beginTransaction(); /** * Commit the active database transaction. * * @return void */ public function commit(); /** * Rollback the active database transaction. * * @return void */ public function rollBack(); /** * Get the number of active transactions. * * @return int */ public function transactionLevel(); /** * Execute the given callback in "dry run" mode. * * @param \Closure $callback * @return array */ public function pretend(Closure $callback); /** * Get the name of the connected database. * * @return string */ public function getDatabaseName(); } src/Illuminate/Database/QueryException.php 0000644 00000004124 15107327037 0014645 0 ustar 00 <?php namespace Illuminate\Database; use Illuminate\Support\Str; use PDOException; use Throwable; class QueryException extends PDOException { /** * The database connection name. * * @var string */ public $connectionName; /** * The SQL for the query. * * @var string */ protected $sql; /** * The bindings for the query. * * @var array */ protected $bindings; /** * Create a new query exception instance. * * @param string $connectionName * @param string $sql * @param array $bindings * @param \Throwable $previous * @return void */ public function __construct($connectionName, $sql, array $bindings, Throwable $previous) { parent::__construct('', 0, $previous); $this->connectionName = $connectionName; $this->sql = $sql; $this->bindings = $bindings; $this->code = $previous->getCode(); $this->message = $this->formatMessage($connectionName, $sql, $bindings, $previous); if ($previous instanceof PDOException) { $this->errorInfo = $previous->errorInfo; } } /** * Format the SQL error message. * * @param string $connectionName * @param string $sql * @param array $bindings * @param \Throwable $previous * @return string */ protected function formatMessage($connectionName, $sql, $bindings, Throwable $previous) { return $previous->getMessage().' (Connection: '.$connectionName.', SQL: '.Str::replaceArray('?', $bindings, $sql).')'; } /** * Get the connection name for the query. * * @return string */ public function getConnectionName() { return $this->connectionName; } /** * Get the SQL for the query. * * @return string */ public function getSql() { return $this->sql; } /** * Get the bindings for the query. * * @return array */ public function getBindings() { return $this->bindings; } } src/Illuminate/Database/Seeder.php 0000644 00000011212 15107327037 0013064 0 ustar 00 <?php namespace Illuminate\Database; use Illuminate\Console\Command; use Illuminate\Console\View\Components\TwoColumnDetail; use Illuminate\Contracts\Container\Container; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Support\Arr; use InvalidArgumentException; abstract class Seeder { /** * The container instance. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * The console command instance. * * @var \Illuminate\Console\Command */ protected $command; /** * Seeders that have been called at least one time. * * @var array */ protected static $called = []; /** * Run the given seeder class. * * @param array|string $class * @param bool $silent * @param array $parameters * @return $this */ public function call($class, $silent = false, array $parameters = []) { $classes = Arr::wrap($class); foreach ($classes as $class) { $seeder = $this->resolve($class); $name = get_class($seeder); if ($silent === false && isset($this->command)) { with(new TwoColumnDetail($this->command->getOutput()))->render( $name, '<fg=yellow;options=bold>RUNNING</>' ); } $startTime = microtime(true); $seeder->__invoke($parameters); if ($silent === false && isset($this->command)) { $runTime = number_format((microtime(true) - $startTime) * 1000, 2); with(new TwoColumnDetail($this->command->getOutput()))->render( $name, "<fg=gray>$runTime ms</> <fg=green;options=bold>DONE</>" ); $this->command->getOutput()->writeln(''); } static::$called[] = $class; } return $this; } /** * Run the given seeder class. * * @param array|string $class * @param array $parameters * @return void */ public function callWith($class, array $parameters = []) { $this->call($class, false, $parameters); } /** * Silently run the given seeder class. * * @param array|string $class * @param array $parameters * @return void */ public function callSilent($class, array $parameters = []) { $this->call($class, true, $parameters); } /** * Run the given seeder class once. * * @param array|string $class * @param bool $silent * @return void */ public function callOnce($class, $silent = false, array $parameters = []) { if (in_array($class, static::$called)) { return; } $this->call($class, $silent, $parameters); } /** * Resolve an instance of the given seeder class. * * @param string $class * @return \Illuminate\Database\Seeder */ protected function resolve($class) { if (isset($this->container)) { $instance = $this->container->make($class); $instance->setContainer($this->container); } else { $instance = new $class; } if (isset($this->command)) { $instance->setCommand($this->command); } return $instance; } /** * Set the IoC container instance. * * @param \Illuminate\Contracts\Container\Container $container * @return $this */ public function setContainer(Container $container) { $this->container = $container; return $this; } /** * Set the console command instance. * * @param \Illuminate\Console\Command $command * @return $this */ public function setCommand(Command $command) { $this->command = $command; return $this; } /** * Run the database seeds. * * @param array $parameters * @return mixed * * @throws \InvalidArgumentException */ public function __invoke(array $parameters = []) { if (! method_exists($this, 'run')) { throw new InvalidArgumentException('Method [run] missing from '.get_class($this)); } $callback = fn () => isset($this->container) ? $this->container->call([$this, 'run'], $parameters) : $this->run(...$parameters); $uses = array_flip(class_uses_recursive(static::class)); if (isset($uses[WithoutModelEvents::class])) { $callback = $this->withoutModelEvents($callback); } return $callback(); } } src/Illuminate/Database/SQLiteDatabaseDoesNotExistException.php 0000644 00000001065 15107327037 0020640 0 ustar 00 <?php namespace Illuminate\Database; use InvalidArgumentException; class SQLiteDatabaseDoesNotExistException extends InvalidArgumentException { /** * The path to the database. * * @var string */ public $path; /** * Create a new exception instance. * * @param string $path * @return void */ public function __construct($path) { parent::__construct("Database file at path [{$path}] does not exist. Ensure this is an absolute path to the database."); $this->path = $path; } } src/Illuminate/Database/SQLiteConnection.php 0000644 00000007531 15107327037 0015047 0 ustar 00 <?php namespace Illuminate\Database; use Exception; use Illuminate\Database\PDO\SQLiteDriver; use Illuminate\Database\Query\Grammars\SQLiteGrammar as QueryGrammar; 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; class SQLiteConnection extends Connection { /** * Create a new database connection instance. * * @param \PDO|\Closure $pdo * @param string $database * @param string $tablePrefix * @param array $config * @return void */ public function __construct($pdo, $database = '', $tablePrefix = '', array $config = []) { parent::__construct($pdo, $database, $tablePrefix, $config); $enableForeignKeyConstraints = $this->getForeignKeyConstraintsConfigurationValue(); if ($enableForeignKeyConstraints === null) { return; } $enableForeignKeyConstraints ? $this->getSchemaBuilder()->enableForeignKeyConstraints() : $this->getSchemaBuilder()->disableForeignKeyConstraints(); } /** * Escape a binary value for safe SQL embedding. * * @param string $value * @return string */ protected function escapeBinary($value) { $hex = bin2hex($value); return "x'{$hex}'"; } /** * Determine if the given database exception was caused by a unique constraint violation. * * @param \Exception $exception * @return bool */ protected function isUniqueConstraintError(Exception $exception) { return boolval(preg_match('#(column(s)? .* (is|are) not unique|UNIQUE constraint failed: .*)#i', $exception->getMessage())); } /** * Get the default query grammar instance. * * @return \Illuminate\Database\Query\Grammars\SQLiteGrammar */ protected function getDefaultQueryGrammar() { ($grammar = new QueryGrammar)->setConnection($this); return $this->withTablePrefix($grammar); } /** * Get a schema builder instance for the connection. * * @return \Illuminate\Database\Schema\SQLiteBuilder */ public function getSchemaBuilder() { if (is_null($this->schemaGrammar)) { $this->useDefaultSchemaGrammar(); } return new SQLiteBuilder($this); } /** * Get the default schema grammar instance. * * @return \Illuminate\Database\Schema\Grammars\SQLiteGrammar */ protected function getDefaultSchemaGrammar() { ($grammar = new SchemaGrammar)->setConnection($this); return $this->withTablePrefix($grammar); } /** * Get the schema state for the connection. * * @param \Illuminate\Filesystem\Filesystem|null $files * @param callable|null $processFactory * * @throws \RuntimeException */ public function getSchemaState(Filesystem $files = null, callable $processFactory = null) { return new SqliteSchemaState($this, $files, $processFactory); } /** * Get the default post processor instance. * * @return \Illuminate\Database\Query\Processors\SQLiteProcessor */ protected function getDefaultPostProcessor() { return new SQLiteProcessor; } /** * Get the Doctrine DBAL driver. * * @return \Illuminate\Database\PDO\SQLiteDriver */ protected function getDoctrineDriver() { return new SQLiteDriver; } /** * Get the database connection foreign key constraints configuration option. * * @return bool|null */ protected function getForeignKeyConstraintsConfigurationValue() { return $this->getConfig('foreign_key_constraints'); } } src/Illuminate/Database/Grammar.php 0000644 00000016200 15107327037 0013245 0 ustar 00 <?php namespace Illuminate\Database; use Illuminate\Contracts\Database\Query\Expression; use Illuminate\Support\Traits\Macroable; use RuntimeException; abstract class Grammar { use Macroable; /** * The connection used for escaping values. * * @var \Illuminate\Database\Connection */ protected $connection; /** * The grammar table prefix. * * @var string */ protected $tablePrefix = ''; /** * Wrap an array of values. * * @param array $values * @return array */ public function wrapArray(array $values) { return array_map([$this, 'wrap'], $values); } /** * Wrap a table in keyword identifiers. * * @param \Illuminate\Contracts\Database\Query\Expression|string $table * @return string */ public function wrapTable($table) { if (! $this->isExpression($table)) { return $this->wrap($this->tablePrefix.$table, true); } return $this->getValue($table); } /** * Wrap a value in keyword identifiers. * * @param \Illuminate\Contracts\Database\Query\Expression|string $value * @param bool $prefixAlias * @return string */ public function wrap($value, $prefixAlias = false) { if ($this->isExpression($value)) { return $this->getValue($value); } // If the value being wrapped has a column alias we will need to separate out // the pieces so we can wrap each of the segments of the expression on its // own, and then join these both back together using the "as" connector. if (stripos($value, ' as ') !== false) { return $this->wrapAliasedValue($value, $prefixAlias); } // If the given value is a JSON selector we will wrap it differently than a // traditional value. We will need to split this path and wrap each part // wrapped, etc. Otherwise, we will simply wrap the value as a string. if ($this->isJsonSelector($value)) { return $this->wrapJsonSelector($value); } return $this->wrapSegments(explode('.', $value)); } /** * Wrap a value that has an alias. * * @param string $value * @param bool $prefixAlias * @return string */ protected function wrapAliasedValue($value, $prefixAlias = false) { $segments = preg_split('/\s+as\s+/i', $value); // If we are wrapping a table we need to prefix the alias with the table prefix // as well in order to generate proper syntax. If this is a column of course // no prefix is necessary. The condition will be true when from wrapTable. if ($prefixAlias) { $segments[1] = $this->tablePrefix.$segments[1]; } return $this->wrap($segments[0]).' as '.$this->wrapValue($segments[1]); } /** * Wrap the given value segments. * * @param array $segments * @return string */ protected function wrapSegments($segments) { return collect($segments)->map(function ($segment, $key) use ($segments) { return $key == 0 && count($segments) > 1 ? $this->wrapTable($segment) : $this->wrapValue($segment); })->implode('.'); } /** * Wrap a single string in keyword identifiers. * * @param string $value * @return string */ protected function wrapValue($value) { if ($value !== '*') { return '"'.str_replace('"', '""', $value).'"'; } return $value; } /** * Wrap the given JSON selector. * * @param string $value * @return string * * @throws \RuntimeException */ protected function wrapJsonSelector($value) { throw new RuntimeException('This database engine does not support JSON operations.'); } /** * Determine if the given string is a JSON selector. * * @param string $value * @return bool */ protected function isJsonSelector($value) { return str_contains($value, '->'); } /** * Convert an array of column names into a delimited string. * * @param array $columns * @return string */ public function columnize(array $columns) { return implode(', ', array_map([$this, 'wrap'], $columns)); } /** * Create query parameter place-holders for an array. * * @param array $values * @return string */ public function parameterize(array $values) { return implode(', ', array_map([$this, 'parameter'], $values)); } /** * Get the appropriate query parameter place-holder for a value. * * @param mixed $value * @return string */ public function parameter($value) { return $this->isExpression($value) ? $this->getValue($value) : '?'; } /** * Quote the given string literal. * * @param string|array $value * @return string */ public function quoteString($value) { if (is_array($value)) { return implode(', ', array_map([$this, __FUNCTION__], $value)); } return "'$value'"; } /** * Escapes a value for safe SQL embedding. * * @param string|float|int|bool|null $value * @param bool $binary * @return string */ public function escape($value, $binary = false) { if (is_null($this->connection)) { throw new RuntimeException("The database driver's grammar implementation does not support escaping values."); } return $this->connection->escape($value, $binary); } /** * Determine if the given value is a raw expression. * * @param mixed $value * @return bool */ public function isExpression($value) { return $value instanceof Expression; } /** * Transforms expressions to their scalar types. * * @param \Illuminate\Contracts\Database\Query\Expression|string|int|float $expression * @return string|int|float */ public function getValue($expression) { if ($this->isExpression($expression)) { return $this->getValue($expression->getValue($this)); } return $expression; } /** * Get the format for database stored dates. * * @return string */ public function getDateFormat() { return 'Y-m-d H:i:s'; } /** * Get the grammar's table prefix. * * @return string */ public function getTablePrefix() { return $this->tablePrefix; } /** * Set the grammar's table prefix. * * @param string $prefix * @return $this */ public function setTablePrefix($prefix) { $this->tablePrefix = $prefix; return $this; } /** * Set the grammar's database connection. * * @param \Illuminate\Database\Connection $connection * @return $this */ public function setConnection($connection) { $this->connection = $connection; return $this; } } src/Illuminate/Database/composer.json 0000644 00000003314 15107327037 0013672 0 ustar 00 { "name": "illuminate/database", "description": "The Illuminate Database package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "keywords": ["laravel", "database", "sql", "orm"], "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "ext-pdo": "*", "brick/math": "^0.9.3|^0.10.2|^0.11", "illuminate/collections": "^10.0", "illuminate/container": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0", "illuminate/support": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Database\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "ext-filter": "Required to use the Postgres database driver.", "doctrine/dbal": "Required to rename columns and drop SQLite columns (^3.5.1).", "fakerphp/faker": "Required to use the eloquent factory builder (^1.21).", "illuminate/console": "Required to use the database commands (^10.0).", "illuminate/events": "Required to use the observers with Eloquent (^10.0).", "illuminate/filesystem": "Required to use the migrations (^10.0).", "illuminate/pagination": "Required to paginate the result set (^10.0).", "symfony/finder": "Required to use Eloquent model factories (^6.2)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Database/Events/TransactionBeginning.php 0000644 00000000154 15107327037 0017232 0 ustar 00 <?php namespace Illuminate\Database\Events; class TransactionBeginning extends ConnectionEvent { // } src/Illuminate/Database/Events/MigrationsEnded.php 0000644 00000000147 15107327037 0016202 0 ustar 00 <?php namespace Illuminate\Database\Events; class MigrationsEnded extends MigrationsEvent { // } src/Illuminate/Database/Events/QueryExecuted.php 0000644 00000002162 15107327037 0015721 0 ustar 00 <?php namespace Illuminate\Database\Events; class QueryExecuted { /** * The SQL query that was executed. * * @var string */ public $sql; /** * The array of query bindings. * * @var array */ public $bindings; /** * The number of milliseconds it took to execute the query. * * @var float */ public $time; /** * The database connection instance. * * @var \Illuminate\Database\Connection */ public $connection; /** * The database connection name. * * @var string */ public $connectionName; /** * Create a new event instance. * * @param string $sql * @param array $bindings * @param float|null $time * @param \Illuminate\Database\Connection $connection * @return void */ public function __construct($sql, $bindings, $time, $connection) { $this->sql = $sql; $this->time = $time; $this->bindings = $bindings; $this->connection = $connection; $this->connectionName = $connection->getName(); } } src/Illuminate/Database/Events/ConnectionEvent.php 0000644 00000001145 15107327037 0016226 0 ustar 00 <?php namespace Illuminate\Database\Events; abstract class ConnectionEvent { /** * The name of the connection. * * @var string */ public $connectionName; /** * The database connection instance. * * @var \Illuminate\Database\Connection */ public $connection; /** * Create a new event instance. * * @param \Illuminate\Database\Connection $connection * @return void */ public function __construct($connection) { $this->connection = $connection; $this->connectionName = $connection->getName(); } } src/Illuminate/Database/Events/ModelPruningFinished.php 0000644 00000000641 15107327037 0017202 0 ustar 00 <?php namespace Illuminate\Database\Events; class ModelPruningFinished { /** * The class names of the models that were pruned. * * @var array<class-string> */ public $models; /** * Create a new event instance. * * @param array<class-string> $models * @return void */ public function __construct($models) { $this->models = $models; } } src/Illuminate/Database/Events/MigrationEnded.php 0000644 00000000145 15107327037 0016015 0 ustar 00 <?php namespace Illuminate\Database\Events; class MigrationEnded extends MigrationEvent { // } src/Illuminate/Database/Events/NoPendingMigrations.php 0000644 00000000574 15107327037 0017050 0 ustar 00 <?php namespace Illuminate\Database\Events; class NoPendingMigrations { /** * The migration method that was called. * * @var string */ public $method; /** * Create a new event instance. * * @param string $method * @return void */ public function __construct($method) { $this->method = $method; } } src/Illuminate/Database/Events/MigrationEvent.php 0000644 00000001450 15107327037 0016057 0 ustar 00 <?php namespace Illuminate\Database\Events; use Illuminate\Contracts\Database\Events\MigrationEvent as MigrationEventContract; use Illuminate\Database\Migrations\Migration; abstract class MigrationEvent implements MigrationEventContract { /** * A migration instance. * * @var \Illuminate\Database\Migrations\Migration */ public $migration; /** * The migration method that was called. * * @var string */ public $method; /** * Create a new event instance. * * @param \Illuminate\Database\Migrations\Migration $migration * @param string $method * @return void */ public function __construct(Migration $migration, $method) { $this->method = $method; $this->migration = $migration; } } src/Illuminate/Database/Events/TransactionCommitting.php 0000644 00000000155 15107327037 0017445 0 ustar 00 <?php namespace Illuminate\Database\Events; class TransactionCommitting extends ConnectionEvent { // } src/Illuminate/Database/Events/DatabaseRefreshed.php 0000644 00000000732 15107327037 0016462 0 ustar 00 <?php namespace Illuminate\Database\Events; use Illuminate\Contracts\Database\Events\MigrationEvent as MigrationEventContract; class DatabaseRefreshed implements MigrationEventContract { /** * Create a new event instance. * * @param string|null $database * @param bool seeding * @return void */ public function __construct( public ?string $database = null, public bool $seeding = false ) { // } } src/Illuminate/Database/Events/DatabaseBusy.php 0000644 00000001103 15107327037 0015466 0 ustar 00 <?php namespace Illuminate\Database\Events; class DatabaseBusy { /** * The database connection name. * * @var string */ public $connectionName; /** * The number of open connections. * * @var int */ public $connections; /** * Create a new event instance. * * @param string $connectionName * @param int $connections */ public function __construct($connectionName, $connections) { $this->connectionName = $connectionName; $this->connections = $connections; } } src/Illuminate/Database/Events/TransactionCommitted.php 0000644 00000000154 15107327037 0017257 0 ustar 00 <?php namespace Illuminate\Database\Events; class TransactionCommitted extends ConnectionEvent { // } src/Illuminate/Database/Events/ModelPruningStarting.php 0000644 00000000644 15107327037 0017247 0 ustar 00 <?php namespace Illuminate\Database\Events; class ModelPruningStarting { /** * The class names of the models that will be pruned. * * @var array<class-string> */ public $models; /** * Create a new event instance. * * @param array<class-string> $models * @return void */ public function __construct($models) { $this->models = $models; } } src/Illuminate/Database/Events/StatementPrepared.php 0000644 00000001173 15107327037 0016555 0 ustar 00 <?php namespace Illuminate\Database\Events; class StatementPrepared { /** * The database connection instance. * * @var \Illuminate\Database\Connection */ public $connection; /** * The PDO statement. * * @var \PDOStatement */ public $statement; /** * Create a new event instance. * * @param \Illuminate\Database\Connection $connection * @param \PDOStatement $statement * @return void */ public function __construct($connection, $statement) { $this->statement = $statement; $this->connection = $connection; } } src/Illuminate/Database/Events/TransactionRolledBack.php 0000644 00000000155 15107327037 0017335 0 ustar 00 <?php namespace Illuminate\Database\Events; class TransactionRolledBack extends ConnectionEvent { // } src/Illuminate/Database/Events/SchemaDumped.php 0000644 00000001375 15107327037 0015471 0 ustar 00 <?php namespace Illuminate\Database\Events; class SchemaDumped { /** * The database connection instance. * * @var \Illuminate\Database\Connection */ public $connection; /** * The database connection name. * * @var string */ public $connectionName; /** * The path to the schema dump. * * @var string */ public $path; /** * Create a new event instance. * * @param \Illuminate\Database\Connection $connection * @param string $path * @return void */ public function __construct($connection, $path) { $this->connection = $connection; $this->connectionName = $connection->getName(); $this->path = $path; } } src/Illuminate/Database/Events/ModelsPruned.php 0000644 00000001031 15107327037 0015520 0 ustar 00 <?php namespace Illuminate\Database\Events; class ModelsPruned { /** * The class name of the model that was pruned. * * @var string */ public $model; /** * The number of pruned records. * * @var int */ public $count; /** * Create a new event instance. * * @param string $model * @param int $count * @return void */ public function __construct($model, $count) { $this->model = $model; $this->count = $count; } } src/Illuminate/Database/Events/MigrationStarted.php 0000644 00000000147 15107327037 0016406 0 ustar 00 <?php namespace Illuminate\Database\Events; class MigrationStarted extends MigrationEvent { // } src/Illuminate/Database/Events/MigrationsEvent.php 0000644 00000000770 15107327037 0016246 0 ustar 00 <?php namespace Illuminate\Database\Events; use Illuminate\Contracts\Database\Events\MigrationEvent as MigrationEventContract; abstract class MigrationsEvent implements MigrationEventContract { /** * The migration method that was invoked. * * @var string */ public $method; /** * Create a new event instance. * * @param string $method * @return void */ public function __construct($method) { $this->method = $method; } } src/Illuminate/Database/Events/SchemaLoaded.php 0000644 00000001375 15107327037 0015443 0 ustar 00 <?php namespace Illuminate\Database\Events; class SchemaLoaded { /** * The database connection instance. * * @var \Illuminate\Database\Connection */ public $connection; /** * The database connection name. * * @var string */ public $connectionName; /** * The path to the schema dump. * * @var string */ public $path; /** * Create a new event instance. * * @param \Illuminate\Database\Connection $connection * @param string $path * @return void */ public function __construct($connection, $path) { $this->connection = $connection; $this->connectionName = $connection->getName(); $this->path = $path; } } src/Illuminate/Database/Events/ConnectionEstablished.php 0000644 00000000155 15107327037 0017374 0 ustar 00 <?php namespace Illuminate\Database\Events; class ConnectionEstablished extends ConnectionEvent { // } src/Illuminate/Database/Events/MigrationsStarted.php 0000644 00000000151 15107327037 0016564 0 ustar 00 <?php namespace Illuminate\Database\Events; class MigrationsStarted extends MigrationsEvent { // } src/Illuminate/Database/DatabaseTransactionRecord.php 0000644 00000002450 15107327037 0016732 0 ustar 00 <?php namespace Illuminate\Database; class DatabaseTransactionRecord { /** * The name of the database connection. * * @var string */ public $connection; /** * The transaction level. * * @var int */ public $level; /** * The callbacks that should be executed after committing. * * @var array */ protected $callbacks = []; /** * Create a new database transaction record instance. * * @param string $connection * @param int $level * @return void */ public function __construct($connection, $level) { $this->connection = $connection; $this->level = $level; } /** * Register a callback to be executed after committing. * * @param callable $callback * @return void */ public function addCallback($callback) { $this->callbacks[] = $callback; } /** * Execute all of the callbacks. * * @return void */ public function executeCallbacks() { foreach ($this->callbacks as $callback) { $callback(); } } /** * Get all of the callbacks. * * @return array */ public function getCallbacks() { return $this->callbacks; } } src/Illuminate/Database/DetectsConcurrencyErrors.php 0000644 00000002132 15107327037 0016661 0 ustar 00 <?php namespace Illuminate\Database; use Illuminate\Support\Str; use PDOException; use Throwable; trait DetectsConcurrencyErrors { /** * Determine if the given exception was caused by a concurrency error such as a deadlock or serialization failure. * * @param \Throwable $e * @return bool */ protected function causedByConcurrencyError(Throwable $e) { if ($e instanceof PDOException && ($e->getCode() === 40001 || $e->getCode() === '40001')) { return true; } $message = $e->getMessage(); return Str::contains($message, [ 'Deadlock found when trying to get lock', 'deadlock detected', 'The database file is locked', 'database is locked', 'database table is locked', 'A table in the database is locked', 'has been chosen as the deadlock victim', 'Lock wait timeout exceeded; try restarting transaction', 'WSREP detected deadlock/conflict and aborted the transaction. Try restarting the transaction', ]); } } src/Illuminate/Database/LICENSE.md 0000644 00000002063 15107327037 0012554 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Database/Connectors/ConnectionFactory.php 0000644 00000017252 15107327037 0017433 0 ustar 00 <?php namespace Illuminate\Database\Connectors; use Illuminate\Contracts\Container\Container; use Illuminate\Database\Connection; use Illuminate\Database\MySqlConnection; use Illuminate\Database\PostgresConnection; use Illuminate\Database\SQLiteConnection; use Illuminate\Database\SqlServerConnection; use Illuminate\Support\Arr; use InvalidArgumentException; use PDOException; class ConnectionFactory { /** * The IoC container instance. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * Create a new connection factory instance. * * @param \Illuminate\Contracts\Container\Container $container * @return void */ public function __construct(Container $container) { $this->container = $container; } /** * Establish a PDO connection based on the configuration. * * @param array $config * @param string|null $name * @return \Illuminate\Database\Connection */ public function make(array $config, $name = null) { $config = $this->parseConfig($config, $name); if (isset($config['read'])) { return $this->createReadWriteConnection($config); } return $this->createSingleConnection($config); } /** * Parse and prepare the database configuration. * * @param array $config * @param string $name * @return array */ protected function parseConfig(array $config, $name) { return Arr::add(Arr::add($config, 'prefix', ''), 'name', $name); } /** * Create a single database connection instance. * * @param array $config * @return \Illuminate\Database\Connection */ protected function createSingleConnection(array $config) { $pdo = $this->createPdoResolver($config); return $this->createConnection( $config['driver'], $pdo, $config['database'], $config['prefix'], $config ); } /** * Create a read / write database connection instance. * * @param array $config * @return \Illuminate\Database\Connection */ protected function createReadWriteConnection(array $config) { $connection = $this->createSingleConnection($this->getWriteConfig($config)); return $connection->setReadPdo($this->createReadPdo($config)); } /** * Create a new PDO instance for reading. * * @param array $config * @return \Closure */ protected function createReadPdo(array $config) { return $this->createPdoResolver($this->getReadConfig($config)); } /** * Get the read configuration for a read / write connection. * * @param array $config * @return array */ protected function getReadConfig(array $config) { return $this->mergeReadWriteConfig( $config, $this->getReadWriteConfig($config, 'read') ); } /** * Get the write configuration for a read / write connection. * * @param array $config * @return array */ protected function getWriteConfig(array $config) { return $this->mergeReadWriteConfig( $config, $this->getReadWriteConfig($config, 'write') ); } /** * Get a read / write level configuration. * * @param array $config * @param string $type * @return array */ protected function getReadWriteConfig(array $config, $type) { return isset($config[$type][0]) ? Arr::random($config[$type]) : $config[$type]; } /** * Merge a configuration for a read / write connection. * * @param array $config * @param array $merge * @return array */ protected function mergeReadWriteConfig(array $config, array $merge) { return Arr::except(array_merge($config, $merge), ['read', 'write']); } /** * Create a new Closure that resolves to a PDO instance. * * @param array $config * @return \Closure */ protected function createPdoResolver(array $config) { return array_key_exists('host', $config) ? $this->createPdoResolverWithHosts($config) : $this->createPdoResolverWithoutHosts($config); } /** * Create a new Closure that resolves to a PDO instance with a specific host or an array of hosts. * * @param array $config * @return \Closure * * @throws \PDOException */ protected function createPdoResolverWithHosts(array $config) { return function () use ($config) { foreach (Arr::shuffle($this->parseHosts($config)) as $host) { $config['host'] = $host; try { return $this->createConnector($config)->connect($config); } catch (PDOException $e) { continue; } } throw $e; }; } /** * Parse the hosts configuration item into an array. * * @param array $config * @return array * * @throws \InvalidArgumentException */ protected function parseHosts(array $config) { $hosts = Arr::wrap($config['host']); if (empty($hosts)) { throw new InvalidArgumentException('Database hosts array is empty.'); } return $hosts; } /** * Create a new Closure that resolves to a PDO instance where there is no configured host. * * @param array $config * @return \Closure */ protected function createPdoResolverWithoutHosts(array $config) { return fn () => $this->createConnector($config)->connect($config); } /** * Create a connector instance based on the configuration. * * @param array $config * @return \Illuminate\Database\Connectors\ConnectorInterface * * @throws \InvalidArgumentException */ public function createConnector(array $config) { if (! isset($config['driver'])) { throw new InvalidArgumentException('A driver must be specified.'); } if ($this->container->bound($key = "db.connector.{$config['driver']}")) { return $this->container->make($key); } return match ($config['driver']) { 'mysql' => new MySqlConnector, 'pgsql' => new PostgresConnector, 'sqlite' => new SQLiteConnector, 'sqlsrv' => new SqlServerConnector, default => throw new InvalidArgumentException("Unsupported driver [{$config['driver']}]."), }; } /** * Create a new connection instance. * * @param string $driver * @param \PDO|\Closure $connection * @param string $database * @param string $prefix * @param array $config * @return \Illuminate\Database\Connection * * @throws \InvalidArgumentException */ protected function createConnection($driver, $connection, $database, $prefix = '', array $config = []) { if ($resolver = Connection::getResolver($driver)) { return $resolver($connection, $database, $prefix, $config); } return match ($driver) { 'mysql' => new MySqlConnection($connection, $database, $prefix, $config), 'pgsql' => new PostgresConnection($connection, $database, $prefix, $config), 'sqlite' => new SQLiteConnection($connection, $database, $prefix, $config), 'sqlsrv' => new SqlServerConnection($connection, $database, $prefix, $config), default => throw new InvalidArgumentException("Unsupported driver [{$driver}]."), }; } } src/Illuminate/Database/Connectors/PostgresConnector.php 0000644 00000015032 15107327037 0017457 0 ustar 00 <?php namespace Illuminate\Database\Connectors; use Illuminate\Database\Concerns\ParsesSearchPath; use PDO; class PostgresConnector extends Connector implements ConnectorInterface { use ParsesSearchPath; /** * The default PDO connection options. * * @var array */ protected $options = [ PDO::ATTR_CASE => PDO::CASE_NATURAL, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_STRINGIFY_FETCHES => false, ]; /** * Establish a database connection. * * @param array $config * @return \PDO */ public function connect(array $config) { // First we'll create the basic DSN and connection instance connecting to the // using the configuration option specified by the developer. We will also // set the default character set on the connections to UTF-8 by default. $connection = $this->createConnection( $this->getDsn($config), $config, $this->getOptions($config) ); $this->configureIsolationLevel($connection, $config); $this->configureEncoding($connection, $config); // Next, we will check to see if a timezone has been specified in this config // and if it has we will issue a statement to modify the timezone with the // database. Setting this DB timezone is an optional configuration item. $this->configureTimezone($connection, $config); $this->configureSearchPath($connection, $config); // Postgres allows an application_name to be set by the user and this name is // used to when monitoring the application with pg_stat_activity. So we'll // determine if the option has been specified and run a statement if so. $this->configureApplicationName($connection, $config); $this->configureSynchronousCommit($connection, $config); return $connection; } /** * Set the connection transaction isolation level. * * @param \PDO $connection * @param array $config * @return void */ protected function configureIsolationLevel($connection, array $config) { if (isset($config['isolation_level'])) { $connection->prepare("set session characteristics as transaction isolation level {$config['isolation_level']}")->execute(); } } /** * Set the connection character set and collation. * * @param \PDO $connection * @param array $config * @return void */ protected function configureEncoding($connection, $config) { if (! isset($config['charset'])) { return; } $connection->prepare("set names '{$config['charset']}'")->execute(); } /** * Set the timezone on the connection. * * @param \PDO $connection * @param array $config * @return void */ protected function configureTimezone($connection, array $config) { if (isset($config['timezone'])) { $timezone = $config['timezone']; $connection->prepare("set time zone '{$timezone}'")->execute(); } } /** * Set the "search_path" on the database connection. * * @param \PDO $connection * @param array $config * @return void */ protected function configureSearchPath($connection, $config) { if (isset($config['search_path']) || isset($config['schema'])) { $searchPath = $this->quoteSearchPath( $this->parseSearchPath($config['search_path'] ?? $config['schema']) ); $connection->prepare("set search_path to {$searchPath}")->execute(); } } /** * Format the search path for the DSN. * * @param array $searchPath * @return string */ protected function quoteSearchPath($searchPath) { return count($searchPath) === 1 ? '"'.$searchPath[0].'"' : '"'.implode('", "', $searchPath).'"'; } /** * Set the application name on the connection. * * @param \PDO $connection * @param array $config * @return void */ protected function configureApplicationName($connection, $config) { if (isset($config['application_name'])) { $applicationName = $config['application_name']; $connection->prepare("set application_name to '$applicationName'")->execute(); } } /** * Create a DSN string from a configuration. * * @param array $config * @return string */ protected function getDsn(array $config) { // First we will create the basic DSN setup as well as the port if it is in // in the configuration options. This will give us the basic DSN we will // need to establish the PDO connections and return them back for use. extract($config, EXTR_SKIP); $host = isset($host) ? "host={$host};" : ''; // Sometimes - users may need to connect to a database that has a different // name than the database used for "information_schema" queries. This is // typically the case if using "pgbouncer" type software when pooling. $database = $connect_via_database ?? $database; $port = $connect_via_port ?? $port ?? null; $dsn = "pgsql:{$host}dbname='{$database}'"; // If a port was specified, we will add it to this Postgres DSN connections // format. Once we have done that we are ready to return this connection // string back out for usage, as this has been fully constructed here. if (! is_null($port)) { $dsn .= ";port={$port}"; } return $this->addSslOptions($dsn, $config); } /** * Add the SSL options to the DSN. * * @param string $dsn * @param array $config * @return string */ protected function addSslOptions($dsn, array $config) { foreach (['sslmode', 'sslcert', 'sslkey', 'sslrootcert'] as $option) { if (isset($config[$option])) { $dsn .= ";{$option}={$config[$option]}"; } } return $dsn; } /** * Configure the synchronous_commit setting. * * @param \PDO $connection * @param array $config * @return void */ protected function configureSynchronousCommit($connection, array $config) { if (! isset($config['synchronous_commit'])) { return; } $connection->prepare("set synchronous_commit to '{$config['synchronous_commit']}'")->execute(); } } src/Illuminate/Database/Connectors/SQLiteConnector.php 0000644 00000002533 15107327037 0017014 0 ustar 00 <?php namespace Illuminate\Database\Connectors; use Illuminate\Database\SQLiteDatabaseDoesNotExistException; class SQLiteConnector extends Connector implements ConnectorInterface { /** * Establish a database connection. * * @param array $config * @return \PDO * * @throws \Illuminate\Database\SQLiteDatabaseDoesNotExistException */ public function connect(array $config) { $options = $this->getOptions($config); // SQLite supports "in-memory" databases that only last as long as the owning // connection does. These are useful for tests or for short lifetime store // querying. In-memory databases may only have a single open connection. if ($config['database'] === ':memory:') { return $this->createConnection('sqlite::memory:', $config, $options); } $path = realpath($config['database']); // Here we'll verify that the SQLite database exists before going any further // as the developer probably wants to know if the database exists and this // SQLite driver will not throw any exception if it does not by default. if ($path === false) { throw new SQLiteDatabaseDoesNotExistException($config['database']); } return $this->createConnection("sqlite:{$path}", $config, $options); } } src/Illuminate/Database/Connectors/ConnectorInterface.php 0000644 00000000360 15107327037 0017547 0 ustar 00 <?php namespace Illuminate\Database\Connectors; interface ConnectorInterface { /** * Establish a database connection. * * @param array $config * @return \PDO */ public function connect(array $config); } src/Illuminate/Database/Connectors/MySqlConnector.php 0000644 00000013615 15107327037 0016723 0 ustar 00 <?php namespace Illuminate\Database\Connectors; use PDO; class MySqlConnector extends Connector implements ConnectorInterface { /** * Establish a database connection. * * @param array $config * @return \PDO */ public function connect(array $config) { $dsn = $this->getDsn($config); $options = $this->getOptions($config); // We need to grab the PDO options that should be used while making the brand // new connection instance. The PDO options control various aspects of the // connection's behavior, and some might be specified by the developers. $connection = $this->createConnection($dsn, $config, $options); if (! empty($config['database'])) { $connection->exec("use `{$config['database']}`;"); } $this->configureIsolationLevel($connection, $config); $this->configureEncoding($connection, $config); // Next, we will check to see if a timezone has been specified in this config // and if it has we will issue a statement to modify the timezone with the // database. Setting this DB timezone is an optional configuration item. $this->configureTimezone($connection, $config); $this->setModes($connection, $config); return $connection; } /** * Set the connection transaction isolation level. * * @param \PDO $connection * @param array $config * @return void */ protected function configureIsolationLevel($connection, array $config) { if (! isset($config['isolation_level'])) { return; } $connection->prepare( "SET SESSION TRANSACTION ISOLATION LEVEL {$config['isolation_level']}" )->execute(); } /** * Set the connection character set and collation. * * @param \PDO $connection * @param array $config * @return void|\PDO */ protected function configureEncoding($connection, array $config) { if (! isset($config['charset'])) { return $connection; } $connection->prepare( "set names '{$config['charset']}'".$this->getCollation($config) )->execute(); } /** * Get the collation for the configuration. * * @param array $config * @return string */ protected function getCollation(array $config) { return isset($config['collation']) ? " collate '{$config['collation']}'" : ''; } /** * Set the timezone on the connection. * * @param \PDO $connection * @param array $config * @return void */ protected function configureTimezone($connection, array $config) { if (isset($config['timezone'])) { $connection->prepare('set time_zone="'.$config['timezone'].'"')->execute(); } } /** * Create a DSN string from a configuration. * * Chooses socket or host/port based on the 'unix_socket' config value. * * @param array $config * @return string */ protected function getDsn(array $config) { return $this->hasSocket($config) ? $this->getSocketDsn($config) : $this->getHostDsn($config); } /** * Determine if the given configuration array has a UNIX socket value. * * @param array $config * @return bool */ protected function hasSocket(array $config) { return isset($config['unix_socket']) && ! empty($config['unix_socket']); } /** * Get the DSN string for a socket configuration. * * @param array $config * @return string */ protected function getSocketDsn(array $config) { return "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}"; } /** * Get the DSN string for a host / port configuration. * * @param array $config * @return string */ protected function getHostDsn(array $config) { extract($config, EXTR_SKIP); return isset($port) ? "mysql:host={$host};port={$port};dbname={$database}" : "mysql:host={$host};dbname={$database}"; } /** * Set the modes for the connection. * * @param \PDO $connection * @param array $config * @return void */ protected function setModes(PDO $connection, array $config) { if (isset($config['modes'])) { $this->setCustomModes($connection, $config); } elseif (isset($config['strict'])) { if ($config['strict']) { $connection->prepare($this->strictMode($connection, $config))->execute(); } else { $connection->prepare("set session sql_mode='NO_ENGINE_SUBSTITUTION'")->execute(); } } } /** * Set the custom modes on the connection. * * @param \PDO $connection * @param array $config * @return void */ protected function setCustomModes(PDO $connection, array $config) { $modes = implode(',', $config['modes']); $connection->prepare("set session sql_mode='{$modes}'")->execute(); } /** * Get the query to enable strict mode. * * @param \PDO $connection * @param array $config * @return string */ protected function strictMode(PDO $connection, $config) { $version = $config['version'] ?? $connection->getAttribute(PDO::ATTR_SERVER_VERSION); if (version_compare($version, '8.0.11') >= 0) { return "set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'"; } return "set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'"; } } src/Illuminate/Database/Connectors/Connector.php 0000644 00000005537 15107327037 0015741 0 ustar 00 <?php namespace Illuminate\Database\Connectors; use Exception; use Illuminate\Database\DetectsLostConnections; use PDO; use Throwable; class Connector { use DetectsLostConnections; /** * The default PDO connection options. * * @var array */ protected $options = [ PDO::ATTR_CASE => PDO::CASE_NATURAL, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_STRINGIFY_FETCHES => false, PDO::ATTR_EMULATE_PREPARES => false, ]; /** * Create a new PDO connection. * * @param string $dsn * @param array $config * @param array $options * @return \PDO * * @throws \Exception */ public function createConnection($dsn, array $config, array $options) { [$username, $password] = [ $config['username'] ?? null, $config['password'] ?? null, ]; try { return $this->createPdoConnection( $dsn, $username, $password, $options ); } catch (Exception $e) { return $this->tryAgainIfCausedByLostConnection( $e, $dsn, $username, $password, $options ); } } /** * Create a new PDO connection instance. * * @param string $dsn * @param string $username * @param string $password * @param array $options * @return \PDO */ protected function createPdoConnection($dsn, $username, $password, $options) { return new PDO($dsn, $username, $password, $options); } /** * Handle an exception that occurred during connect execution. * * @param \Throwable $e * @param string $dsn * @param string $username * @param string $password * @param array $options * @return \PDO * * @throws \Exception */ protected function tryAgainIfCausedByLostConnection(Throwable $e, $dsn, $username, $password, $options) { if ($this->causedByLostConnection($e)) { return $this->createPdoConnection($dsn, $username, $password, $options); } throw $e; } /** * Get the PDO options based on the configuration. * * @param array $config * @return array */ public function getOptions(array $config) { $options = $config['options'] ?? []; return array_diff_key($this->options, $options) + $options; } /** * Get the default PDO connection options. * * @return array */ public function getDefaultOptions() { return $this->options; } /** * Set the default PDO connection options. * * @param array $options * @return void */ public function setDefaultOptions(array $options) { $this->options = $options; } } src/Illuminate/Database/Connectors/SqlServerConnector.php 0000644 00000014711 15107327037 0017602 0 ustar 00 <?php namespace Illuminate\Database\Connectors; use Illuminate\Support\Arr; use PDO; class SqlServerConnector extends Connector implements ConnectorInterface { /** * The PDO connection options. * * @var array */ protected $options = [ PDO::ATTR_CASE => PDO::CASE_NATURAL, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_STRINGIFY_FETCHES => false, ]; /** * Establish a database connection. * * @param array $config * @return \PDO */ public function connect(array $config) { $options = $this->getOptions($config); $connection = $this->createConnection($this->getDsn($config), $config, $options); $this->configureIsolationLevel($connection, $config); return $connection; } /** * Set the connection transaction isolation level. * * https://learn.microsoft.com/en-us/sql/t-sql/statements/set-transaction-isolation-level-transact-sql * * @param \PDO $connection * @param array $config * @return void */ protected function configureIsolationLevel($connection, array $config) { if (! isset($config['isolation_level'])) { return; } $connection->prepare( "SET TRANSACTION ISOLATION LEVEL {$config['isolation_level']}" )->execute(); } /** * Create a DSN string from a configuration. * * @param array $config * @return string */ protected function getDsn(array $config) { // First we will create the basic DSN setup as well as the port if it is in // in the configuration options. This will give us the basic DSN we will // need to establish the PDO connections and return them back for use. if ($this->prefersOdbc($config)) { return $this->getOdbcDsn($config); } if (in_array('sqlsrv', $this->getAvailableDrivers())) { return $this->getSqlSrvDsn($config); } else { return $this->getDblibDsn($config); } } /** * Determine if the database configuration prefers ODBC. * * @param array $config * @return bool */ protected function prefersOdbc(array $config) { return in_array('odbc', $this->getAvailableDrivers()) && ($config['odbc'] ?? null) === true; } /** * Get the DSN string for a DbLib connection. * * @param array $config * @return string */ protected function getDblibDsn(array $config) { return $this->buildConnectString('dblib', array_merge([ 'host' => $this->buildHostString($config, ':'), 'dbname' => $config['database'], ], Arr::only($config, ['appname', 'charset', 'version']))); } /** * Get the DSN string for an ODBC connection. * * @param array $config * @return string */ protected function getOdbcDsn(array $config) { return isset($config['odbc_datasource_name']) ? 'odbc:'.$config['odbc_datasource_name'] : ''; } /** * Get the DSN string for a SqlSrv connection. * * @param array $config * @return string */ protected function getSqlSrvDsn(array $config) { $arguments = [ 'Server' => $this->buildHostString($config, ','), ]; if (isset($config['database'])) { $arguments['Database'] = $config['database']; } if (isset($config['readonly'])) { $arguments['ApplicationIntent'] = 'ReadOnly'; } if (isset($config['pooling']) && $config['pooling'] === false) { $arguments['ConnectionPooling'] = '0'; } if (isset($config['appname'])) { $arguments['APP'] = $config['appname']; } if (isset($config['encrypt'])) { $arguments['Encrypt'] = $config['encrypt']; } if (isset($config['trust_server_certificate'])) { $arguments['TrustServerCertificate'] = $config['trust_server_certificate']; } if (isset($config['multiple_active_result_sets']) && $config['multiple_active_result_sets'] === false) { $arguments['MultipleActiveResultSets'] = 'false'; } if (isset($config['transaction_isolation'])) { $arguments['TransactionIsolation'] = $config['transaction_isolation']; } if (isset($config['multi_subnet_failover'])) { $arguments['MultiSubnetFailover'] = $config['multi_subnet_failover']; } if (isset($config['column_encryption'])) { $arguments['ColumnEncryption'] = $config['column_encryption']; } if (isset($config['key_store_authentication'])) { $arguments['KeyStoreAuthentication'] = $config['key_store_authentication']; } if (isset($config['key_store_principal_id'])) { $arguments['KeyStorePrincipalId'] = $config['key_store_principal_id']; } if (isset($config['key_store_secret'])) { $arguments['KeyStoreSecret'] = $config['key_store_secret']; } if (isset($config['login_timeout'])) { $arguments['LoginTimeout'] = $config['login_timeout']; } if (isset($config['authentication'])) { $arguments['Authentication'] = $config['authentication']; } return $this->buildConnectString('sqlsrv', $arguments); } /** * Build a connection string from the given arguments. * * @param string $driver * @param array $arguments * @return string */ protected function buildConnectString($driver, array $arguments) { return $driver.':'.implode(';', array_map(function ($key) use ($arguments) { return sprintf('%s=%s', $key, $arguments[$key]); }, array_keys($arguments))); } /** * Build a host string from the given configuration. * * @param array $config * @param string $separator * @return string */ protected function buildHostString(array $config, $separator) { if (empty($config['port'])) { return $config['host']; } return $config['host'].$separator.$config['port']; } /** * Get the available PDO drivers. * * @return array */ protected function getAvailableDrivers() { return PDO::getAvailableDrivers(); } } src/Illuminate/Database/Eloquent/QueueEntityResolver.php 0000644 00000001245 15107327037 0017461 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use Illuminate\Contracts\Queue\EntityNotFoundException; use Illuminate\Contracts\Queue\EntityResolver as EntityResolverContract; class QueueEntityResolver implements EntityResolverContract { /** * Resolve the entity for the given ID. * * @param string $type * @param mixed $id * @return mixed * * @throws \Illuminate\Contracts\Queue\EntityNotFoundException */ public function resolve($type, $id) { $instance = (new $type)->find($id); if ($instance) { return $instance; } throw new EntityNotFoundException($type, $id); } } src/Illuminate/Database/Eloquent/InvalidCastException.php 0000644 00000001644 15107327037 0017541 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use RuntimeException; class InvalidCastException extends RuntimeException { /** * The name of the affected Eloquent model. * * @var string */ public $model; /** * The name of the column. * * @var string */ public $column; /** * The name of the cast type. * * @var string */ public $castType; /** * Create a new exception instance. * * @param object $model * @param string $column * @param string $castType * @return static */ public function __construct($model, $column, $castType) { $class = get_class($model); parent::__construct("Call to undefined cast [{$castType}] on column [{$column}] in model [{$class}]."); $this->model = $class; $this->column = $column; $this->castType = $castType; } } src/Illuminate/Database/Eloquent/JsonEncodingException.php 0000644 00000002555 15107327037 0017722 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use RuntimeException; class JsonEncodingException extends RuntimeException { /** * Create a new JSON encoding exception for the model. * * @param mixed $model * @param string $message * @return static */ public static function forModel($model, $message) { return new static('Error encoding model ['.get_class($model).'] with ID ['.$model->getKey().'] to JSON: '.$message); } /** * Create a new JSON encoding exception for the resource. * * @param \Illuminate\Http\Resources\Json\JsonResource $resource * @param string $message * @return static */ public static function forResource($resource, $message) { $model = $resource->resource; return new static('Error encoding resource ['.get_class($resource).'] with model ['.get_class($model).'] with ID ['.$model->getKey().'] to JSON: '.$message); } /** * Create a new JSON encoding exception for an attribute. * * @param mixed $model * @param mixed $key * @param string $message * @return static */ public static function forAttribute($model, $key, $message) { $class = get_class($model); return new static("Unable to encode attribute [{$key}] for model [{$class}] to JSON: {$message}."); } } src/Illuminate/Database/Eloquent/SoftDeletingScope.php 0000644 00000011237 15107327037 0017041 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; class SoftDeletingScope implements Scope { /** * All of the extensions to be added to the builder. * * @var string[] */ protected $extensions = ['Restore', 'RestoreOrCreate', 'CreateOrRestore', 'WithTrashed', 'WithoutTrashed', 'OnlyTrashed']; /** * Apply the scope to a given Eloquent query builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model $model * @return void */ public function apply(Builder $builder, Model $model) { $builder->whereNull($model->getQualifiedDeletedAtColumn()); } /** * Extend the query builder with the needed functions. * * @param \Illuminate\Database\Eloquent\Builder $builder * @return void */ public function extend(Builder $builder) { foreach ($this->extensions as $extension) { $this->{"add{$extension}"}($builder); } $builder->onDelete(function (Builder $builder) { $column = $this->getDeletedAtColumn($builder); return $builder->update([ $column => $builder->getModel()->freshTimestampString(), ]); }); } /** * Get the "deleted at" column for the builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @return string */ protected function getDeletedAtColumn(Builder $builder) { if (count((array) $builder->getQuery()->joins) > 0) { return $builder->getModel()->getQualifiedDeletedAtColumn(); } return $builder->getModel()->getDeletedAtColumn(); } /** * Add the restore extension to the builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @return void */ protected function addRestore(Builder $builder) { $builder->macro('restore', function (Builder $builder) { $builder->withTrashed(); return $builder->update([$builder->getModel()->getDeletedAtColumn() => null]); }); } /** * Add the restore-or-create extension to the builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @return void */ protected function addRestoreOrCreate(Builder $builder) { $builder->macro('restoreOrCreate', function (Builder $builder, array $attributes = [], array $values = []) { $builder->withTrashed(); return tap($builder->firstOrCreate($attributes, $values), function ($instance) { $instance->restore(); }); }); } /** * Add the create-or-restore extension to the builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @return void */ protected function addCreateOrRestore(Builder $builder) { $builder->macro('createOrRestore', function (Builder $builder, array $attributes = [], array $values = []) { $builder->withTrashed(); return tap($builder->createOrFirst($attributes, $values), function ($instance) { $instance->restore(); }); }); } /** * Add the with-trashed extension to the builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @return void */ protected function addWithTrashed(Builder $builder) { $builder->macro('withTrashed', function (Builder $builder, $withTrashed = true) { if (! $withTrashed) { return $builder->withoutTrashed(); } return $builder->withoutGlobalScope($this); }); } /** * Add the without-trashed extension to the builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @return void */ protected function addWithoutTrashed(Builder $builder) { $builder->macro('withoutTrashed', function (Builder $builder) { $model = $builder->getModel(); $builder->withoutGlobalScope($this)->whereNull( $model->getQualifiedDeletedAtColumn() ); return $builder; }); } /** * Add the only-trashed extension to the builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @return void */ protected function addOnlyTrashed(Builder $builder) { $builder->macro('onlyTrashed', function (Builder $builder) { $model = $builder->getModel(); $builder->withoutGlobalScope($this)->whereNotNull( $model->getQualifiedDeletedAtColumn() ); return $builder; }); } } src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php 0000644 00000076553 15107327037 0021430 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Concerns; use BadMethodCallException; use Closure; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\RelationNotFoundException; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Query\Expression; use Illuminate\Support\Str; use InvalidArgumentException; trait QueriesRelationships { /** * Add a relationship count / exists condition to the query. * * @param \Illuminate\Database\Eloquent\Relations\Relation|string $relation * @param string $operator * @param int $count * @param string $boolean * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Builder|static * * @throws \RuntimeException */ public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null) { if (is_string($relation)) { if (str_contains($relation, '.')) { return $this->hasNested($relation, $operator, $count, $boolean, $callback); } $relation = $this->getRelationWithoutConstraints($relation); } if ($relation instanceof MorphTo) { return $this->hasMorph($relation, ['*'], $operator, $count, $boolean, $callback); } // If we only need to check for the existence of the relation, then we can optimize // the subquery to only run a "where exists" clause instead of this full "count" // clause. This will make these queries run much faster compared with a count. $method = $this->canUseExistsForExistenceCheck($operator, $count) ? 'getRelationExistenceQuery' : 'getRelationExistenceCountQuery'; $hasQuery = $relation->{$method}( $relation->getRelated()->newQueryWithoutRelationships(), $this ); // Next we will call any given callback as an "anonymous" scope so they can get the // proper logical grouping of the where clauses if needed by this Eloquent query // builder. Then, we will be ready to finalize and return this query instance. if ($callback) { $hasQuery->callScope($callback); } return $this->addHasWhere( $hasQuery, $relation, $operator, $count, $boolean ); } /** * Add nested relationship count / exists conditions to the query. * * Sets up recursive call to whereHas until we finish the nested relation. * * @param string $relations * @param string $operator * @param int $count * @param string $boolean * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Builder|static */ protected function hasNested($relations, $operator = '>=', $count = 1, $boolean = 'and', $callback = null) { $relations = explode('.', $relations); $doesntHave = $operator === '<' && $count === 1; if ($doesntHave) { $operator = '>='; $count = 1; } $closure = function ($q) use (&$closure, &$relations, $operator, $count, $callback) { // In order to nest "has", we need to add count relation constraints on the // callback Closure. We'll do this by simply passing the Closure its own // reference to itself so it calls itself recursively on each segment. count($relations) > 1 ? $q->whereHas(array_shift($relations), $closure) : $q->has(array_shift($relations), $operator, $count, 'and', $callback); }; return $this->has(array_shift($relations), $doesntHave ? '<' : '>=', 1, $boolean, $closure); } /** * Add a relationship count / exists condition to the query with an "or". * * @param string $relation * @param string $operator * @param int $count * @return \Illuminate\Database\Eloquent\Builder|static */ public function orHas($relation, $operator = '>=', $count = 1) { return $this->has($relation, $operator, $count, 'or'); } /** * Add a relationship count / exists condition to the query. * * @param string $relation * @param string $boolean * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Builder|static */ public function doesntHave($relation, $boolean = 'and', Closure $callback = null) { return $this->has($relation, '<', 1, $boolean, $callback); } /** * Add a relationship count / exists condition to the query with an "or". * * @param string $relation * @return \Illuminate\Database\Eloquent\Builder|static */ public function orDoesntHave($relation) { return $this->doesntHave($relation, 'or'); } /** * Add a relationship count / exists condition to the query with where clauses. * * @param string $relation * @param \Closure|null $callback * @param string $operator * @param int $count * @return \Illuminate\Database\Eloquent\Builder|static */ public function whereHas($relation, Closure $callback = null, $operator = '>=', $count = 1) { return $this->has($relation, $operator, $count, 'and', $callback); } /** * Add a relationship count / exists condition to the query with where clauses. * * Also load the relationship with same condition. * * @param string $relation * @param \Closure|null $callback * @param string $operator * @param int $count * @return \Illuminate\Database\Eloquent\Builder|static */ public function withWhereHas($relation, Closure $callback = null, $operator = '>=', $count = 1) { return $this->whereHas(Str::before($relation, ':'), $callback, $operator, $count) ->with($callback ? [$relation => fn ($query) => $callback($query)] : $relation); } /** * Add a relationship count / exists condition to the query with where clauses and an "or". * * @param string $relation * @param \Closure|null $callback * @param string $operator * @param int $count * @return \Illuminate\Database\Eloquent\Builder|static */ public function orWhereHas($relation, Closure $callback = null, $operator = '>=', $count = 1) { return $this->has($relation, $operator, $count, 'or', $callback); } /** * Add a relationship count / exists condition to the query with where clauses. * * @param string $relation * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Builder|static */ public function whereDoesntHave($relation, Closure $callback = null) { return $this->doesntHave($relation, 'and', $callback); } /** * Add a relationship count / exists condition to the query with where clauses and an "or". * * @param string $relation * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Builder|static */ public function orWhereDoesntHave($relation, Closure $callback = null) { return $this->doesntHave($relation, 'or', $callback); } /** * Add a polymorphic relationship count / exists condition to the query. * * @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation * @param string|array $types * @param string $operator * @param int $count * @param string $boolean * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Builder|static */ public function hasMorph($relation, $types, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null) { if (is_string($relation)) { $relation = $this->getRelationWithoutConstraints($relation); } $types = (array) $types; if ($types === ['*']) { $types = $this->model->newModelQuery()->distinct()->pluck($relation->getMorphType())->filter()->all(); } foreach ($types as &$type) { $type = Relation::getMorphedModel($type) ?? $type; } return $this->where(function ($query) use ($relation, $callback, $operator, $count, $types) { foreach ($types as $type) { $query->orWhere(function ($query) use ($relation, $callback, $operator, $count, $type) { $belongsTo = $this->getBelongsToRelation($relation, $type); if ($callback) { $callback = function ($query) use ($callback, $type) { return $callback($query, $type); }; } $query->where($this->qualifyColumn($relation->getMorphType()), '=', (new $type)->getMorphClass()) ->whereHas($belongsTo, $callback, $operator, $count); }); } }, null, null, $boolean); } /** * Get the BelongsTo relationship for a single polymorphic type. * * @param \Illuminate\Database\Eloquent\Relations\MorphTo $relation * @param string $type * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ protected function getBelongsToRelation(MorphTo $relation, $type) { $belongsTo = Relation::noConstraints(function () use ($relation, $type) { return $this->model->belongsTo( $type, $relation->getForeignKeyName(), $relation->getOwnerKeyName() ); }); $belongsTo->getQuery()->mergeConstraintsFrom($relation->getQuery()); return $belongsTo; } /** * Add a polymorphic relationship count / exists condition to the query with an "or". * * @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation * @param string|array $types * @param string $operator * @param int $count * @return \Illuminate\Database\Eloquent\Builder|static */ public function orHasMorph($relation, $types, $operator = '>=', $count = 1) { return $this->hasMorph($relation, $types, $operator, $count, 'or'); } /** * Add a polymorphic relationship count / exists condition to the query. * * @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation * @param string|array $types * @param string $boolean * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Builder|static */ public function doesntHaveMorph($relation, $types, $boolean = 'and', Closure $callback = null) { return $this->hasMorph($relation, $types, '<', 1, $boolean, $callback); } /** * Add a polymorphic relationship count / exists condition to the query with an "or". * * @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation * @param string|array $types * @return \Illuminate\Database\Eloquent\Builder|static */ public function orDoesntHaveMorph($relation, $types) { return $this->doesntHaveMorph($relation, $types, 'or'); } /** * Add a polymorphic relationship count / exists condition to the query with where clauses. * * @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation * @param string|array $types * @param \Closure|null $callback * @param string $operator * @param int $count * @return \Illuminate\Database\Eloquent\Builder|static */ public function whereHasMorph($relation, $types, Closure $callback = null, $operator = '>=', $count = 1) { return $this->hasMorph($relation, $types, $operator, $count, 'and', $callback); } /** * Add a polymorphic relationship count / exists condition to the query with where clauses and an "or". * * @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation * @param string|array $types * @param \Closure|null $callback * @param string $operator * @param int $count * @return \Illuminate\Database\Eloquent\Builder|static */ public function orWhereHasMorph($relation, $types, Closure $callback = null, $operator = '>=', $count = 1) { return $this->hasMorph($relation, $types, $operator, $count, 'or', $callback); } /** * Add a polymorphic relationship count / exists condition to the query with where clauses. * * @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation * @param string|array $types * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Builder|static */ public function whereDoesntHaveMorph($relation, $types, Closure $callback = null) { return $this->doesntHaveMorph($relation, $types, 'and', $callback); } /** * Add a polymorphic relationship count / exists condition to the query with where clauses and an "or". * * @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation * @param string|array $types * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Builder|static */ public function orWhereDoesntHaveMorph($relation, $types, Closure $callback = null) { return $this->doesntHaveMorph($relation, $types, 'or', $callback); } /** * Add a basic where clause to a relationship query. * * @param string $relation * @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column * @param mixed $operator * @param mixed $value * @return \Illuminate\Database\Eloquent\Builder|static */ public function whereRelation($relation, $column, $operator = null, $value = null) { return $this->whereHas($relation, function ($query) use ($column, $operator, $value) { if ($column instanceof Closure) { $column($query); } else { $query->where($column, $operator, $value); } }); } /** * Add an "or where" clause to a relationship query. * * @param string $relation * @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column * @param mixed $operator * @param mixed $value * @return \Illuminate\Database\Eloquent\Builder|static */ public function orWhereRelation($relation, $column, $operator = null, $value = null) { return $this->orWhereHas($relation, function ($query) use ($column, $operator, $value) { if ($column instanceof Closure) { $column($query); } else { $query->where($column, $operator, $value); } }); } /** * Add a polymorphic relationship condition to the query with a where clause. * * @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation * @param string|array $types * @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column * @param mixed $operator * @param mixed $value * @return \Illuminate\Database\Eloquent\Builder|static */ public function whereMorphRelation($relation, $types, $column, $operator = null, $value = null) { return $this->whereHasMorph($relation, $types, function ($query) use ($column, $operator, $value) { $query->where($column, $operator, $value); }); } /** * Add a polymorphic relationship condition to the query with an "or where" clause. * * @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation * @param string|array $types * @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column * @param mixed $operator * @param mixed $value * @return \Illuminate\Database\Eloquent\Builder|static */ public function orWhereMorphRelation($relation, $types, $column, $operator = null, $value = null) { return $this->orWhereHasMorph($relation, $types, function ($query) use ($column, $operator, $value) { $query->where($column, $operator, $value); }); } /** * Add a morph-to relationship condition to the query. * * @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation * @param \Illuminate\Database\Eloquent\Model|string|null $model * @return \Illuminate\Database\Eloquent\Builder|static */ public function whereMorphedTo($relation, $model, $boolean = 'and') { if (is_string($relation)) { $relation = $this->getRelationWithoutConstraints($relation); } if (is_null($model)) { return $this->whereNull($relation->getMorphType(), $boolean); } if (is_string($model)) { $morphMap = Relation::morphMap(); if (! empty($morphMap) && in_array($model, $morphMap)) { $model = array_search($model, $morphMap, true); } return $this->where($relation->getMorphType(), $model, null, $boolean); } return $this->where(function ($query) use ($relation, $model) { $query->where($relation->getMorphType(), $model->getMorphClass()) ->where($relation->getForeignKeyName(), $model->getKey()); }, null, null, $boolean); } /** * Add a not morph-to relationship condition to the query. * * @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation * @param \Illuminate\Database\Eloquent\Model|string $model * @return \Illuminate\Database\Eloquent\Builder|static */ public function whereNotMorphedTo($relation, $model, $boolean = 'and') { if (is_string($relation)) { $relation = $this->getRelationWithoutConstraints($relation); } if (is_string($model)) { $morphMap = Relation::morphMap(); if (! empty($morphMap) && in_array($model, $morphMap)) { $model = array_search($model, $morphMap, true); } return $this->whereNot($relation->getMorphType(), '<=>', $model, $boolean); } return $this->whereNot(function ($query) use ($relation, $model) { $query->where($relation->getMorphType(), '<=>', $model->getMorphClass()) ->where($relation->getForeignKeyName(), '<=>', $model->getKey()); }, null, null, $boolean); } /** * Add a morph-to relationship condition to the query with an "or where" clause. * * @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation * @param \Illuminate\Database\Eloquent\Model|string|null $model * @return \Illuminate\Database\Eloquent\Builder|static */ public function orWhereMorphedTo($relation, $model) { return $this->whereMorphedTo($relation, $model, 'or'); } /** * Add a not morph-to relationship condition to the query with an "or where" clause. * * @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation * @param \Illuminate\Database\Eloquent\Model|string $model * @return \Illuminate\Database\Eloquent\Builder|static */ public function orWhereNotMorphedTo($relation, $model) { return $this->whereNotMorphedTo($relation, $model, 'or'); } /** * Add a "belongs to" relationship where clause to the query. * * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model> $related * @param string|null $relationshipName * @param string $boolean * @return $this * * @throws \Illuminate\Database\Eloquent\RelationNotFoundException */ public function whereBelongsTo($related, $relationshipName = null, $boolean = 'and') { if (! $related instanceof Collection) { $relatedCollection = $related->newCollection([$related]); } else { $relatedCollection = $related; $related = $relatedCollection->first(); } if ($relatedCollection->isEmpty()) { throw new InvalidArgumentException('Collection given to whereBelongsTo method may not be empty.'); } if ($relationshipName === null) { $relationshipName = Str::camel(class_basename($related)); } try { $relationship = $this->model->{$relationshipName}(); } catch (BadMethodCallException) { throw RelationNotFoundException::make($this->model, $relationshipName); } if (! $relationship instanceof BelongsTo) { throw RelationNotFoundException::make($this->model, $relationshipName, BelongsTo::class); } $this->whereIn( $relationship->getQualifiedForeignKeyName(), $relatedCollection->pluck($relationship->getOwnerKeyName())->toArray(), $boolean, ); return $this; } /** * Add an "BelongsTo" relationship with an "or where" clause to the query. * * @param \Illuminate\Database\Eloquent\Model $related * @param string|null $relationshipName * @return $this * * @throws \RuntimeException */ public function orWhereBelongsTo($related, $relationshipName = null) { return $this->whereBelongsTo($related, $relationshipName, 'or'); } /** * Add subselect queries to include an aggregate value for a relationship. * * @param mixed $relations * @param string $column * @param string $function * @return $this */ public function withAggregate($relations, $column, $function = null) { if (empty($relations)) { return $this; } if (is_null($this->query->columns)) { $this->query->select([$this->query->from.'.*']); } $relations = is_array($relations) ? $relations : [$relations]; foreach ($this->parseWithRelations($relations) as $name => $constraints) { // First we will determine if the name has been aliased using an "as" clause on the name // and if it has we will extract the actual relationship name and the desired name of // the resulting column. This allows multiple aggregates on the same relationships. $segments = explode(' ', $name); unset($alias); if (count($segments) === 3 && Str::lower($segments[1]) === 'as') { [$name, $alias] = [$segments[0], $segments[2]]; } $relation = $this->getRelationWithoutConstraints($name); if ($function) { $hashedColumn = $this->getRelationHashedColumn($column, $relation); $wrappedColumn = $this->getQuery()->getGrammar()->wrap( $column === '*' ? $column : $relation->getRelated()->qualifyColumn($hashedColumn) ); $expression = $function === 'exists' ? $wrappedColumn : sprintf('%s(%s)', $function, $wrappedColumn); } else { $expression = $column; } // Here, we will grab the relationship sub-query and prepare to add it to the main query // as a sub-select. First, we'll get the "has" query and use that to get the relation // sub-query. We'll format this relationship name and append this column if needed. $query = $relation->getRelationExistenceQuery( $relation->getRelated()->newQuery(), $this, new Expression($expression) )->setBindings([], 'select'); $query->callScope($constraints); $query = $query->mergeConstraintsFrom($relation->getQuery())->toBase(); // If the query contains certain elements like orderings / more than one column selected // then we will remove those elements from the query so that it will execute properly // when given to the database. Otherwise, we may receive SQL errors or poor syntax. $query->orders = null; $query->setBindings([], 'order'); if (count($query->columns) > 1) { $query->columns = [$query->columns[0]]; $query->bindings['select'] = []; } // Finally, we will make the proper column alias to the query and run this sub-select on // the query builder. Then, we will return the builder instance back to the developer // for further constraint chaining that needs to take place on the query as needed. $alias ??= Str::snake( preg_replace('/[^[:alnum:][:space:]_]/u', '', "$name $function $column") ); if ($function === 'exists') { $this->selectRaw( sprintf('exists(%s) as %s', $query->toSql(), $this->getQuery()->grammar->wrap($alias)), $query->getBindings() )->withCasts([$alias => 'bool']); } else { $this->selectSub( $function ? $query : $query->limit(1), $alias ); } } return $this; } /** * Get the relation hashed column name for the given column and relation. * * @param string $column * @param \Illuminate\Database\Eloquent\Relations\Relation $relation * @return string */ protected function getRelationHashedColumn($column, $relation) { if (str_contains($column, '.')) { return $column; } return $this->getQuery()->from === $relation->getQuery()->getQuery()->from ? "{$relation->getRelationCountHash(false)}.$column" : $column; } /** * Add subselect queries to count the relations. * * @param mixed $relations * @return $this */ public function withCount($relations) { return $this->withAggregate(is_array($relations) ? $relations : func_get_args(), '*', 'count'); } /** * Add subselect queries to include the max of the relation's column. * * @param string|array $relation * @param string $column * @return $this */ public function withMax($relation, $column) { return $this->withAggregate($relation, $column, 'max'); } /** * Add subselect queries to include the min of the relation's column. * * @param string|array $relation * @param string $column * @return $this */ public function withMin($relation, $column) { return $this->withAggregate($relation, $column, 'min'); } /** * Add subselect queries to include the sum of the relation's column. * * @param string|array $relation * @param string $column * @return $this */ public function withSum($relation, $column) { return $this->withAggregate($relation, $column, 'sum'); } /** * Add subselect queries to include the average of the relation's column. * * @param string|array $relation * @param string $column * @return $this */ public function withAvg($relation, $column) { return $this->withAggregate($relation, $column, 'avg'); } /** * Add subselect queries to include the existence of related models. * * @param string|array $relation * @return $this */ public function withExists($relation) { return $this->withAggregate($relation, '*', 'exists'); } /** * Add the "has" condition where clause to the query. * * @param \Illuminate\Database\Eloquent\Builder $hasQuery * @param \Illuminate\Database\Eloquent\Relations\Relation $relation * @param string $operator * @param int $count * @param string $boolean * @return \Illuminate\Database\Eloquent\Builder|static */ protected function addHasWhere(Builder $hasQuery, Relation $relation, $operator, $count, $boolean) { $hasQuery->mergeConstraintsFrom($relation->getQuery()); return $this->canUseExistsForExistenceCheck($operator, $count) ? $this->addWhereExistsQuery($hasQuery->toBase(), $boolean, $operator === '<' && $count === 1) : $this->addWhereCountQuery($hasQuery->toBase(), $operator, $count, $boolean); } /** * Merge the where constraints from another query to the current query. * * @param \Illuminate\Database\Eloquent\Builder $from * @return \Illuminate\Database\Eloquent\Builder|static */ public function mergeConstraintsFrom(Builder $from) { $whereBindings = $from->getQuery()->getRawBindings()['where'] ?? []; $wheres = $from->getQuery()->from !== $this->getQuery()->from ? $this->requalifyWhereTables( $from->getQuery()->wheres, $from->getQuery()->grammar->getValue($from->getQuery()->from), $this->getModel()->getTable() ) : $from->getQuery()->wheres; // Here we have some other query that we want to merge the where constraints from. We will // copy over any where constraints on the query as well as remove any global scopes the // query might have removed. Then we will return ourselves with the finished merging. return $this->withoutGlobalScopes( $from->removedScopes() )->mergeWheres( $wheres, $whereBindings ); } /** * Updates the table name for any columns with a new qualified name. * * @param array $wheres * @param string $from * @param string $to * @return array */ protected function requalifyWhereTables(array $wheres, string $from, string $to): array { return collect($wheres)->map(function ($where) use ($from, $to) { return collect($where)->map(function ($value) use ($from, $to) { return is_string($value) && str_starts_with($value, $from.'.') ? $to.'.'.Str::afterLast($value, '.') : $value; }); })->toArray(); } /** * Add a sub-query count clause to this query. * * @param \Illuminate\Database\Query\Builder $query * @param string $operator * @param int $count * @param string $boolean * @return $this */ protected function addWhereCountQuery(QueryBuilder $query, $operator = '>=', $count = 1, $boolean = 'and') { $this->query->addBinding($query->getBindings(), 'where'); return $this->where( new Expression('('.$query->toSql().')'), $operator, is_numeric($count) ? new Expression($count) : $count, $boolean ); } /** * Get the "has relation" base query instance. * * @param string $relation * @return \Illuminate\Database\Eloquent\Relations\Relation */ protected function getRelationWithoutConstraints($relation) { return Relation::noConstraints(function () use ($relation) { return $this->getModel()->{$relation}(); }); } /** * Check if we can run an "exists" query to optimize performance. * * @param string $operator * @param int $count * @return bool */ protected function canUseExistsForExistenceCheck($operator, $count) { return ($operator === '>=' || $operator === '<') && $count === 1; } } src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php 0000644 00000011736 15107327037 0020020 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Concerns; use Illuminate\Support\Facades\Date; trait HasTimestamps { /** * Indicates if the model should be timestamped. * * @var bool */ public $timestamps = true; /** * The list of models classes that have timestamps temporarily disabled. * * @var array */ protected static $ignoreTimestampsOn = []; /** * Update the model's update timestamp. * * @param string|null $attribute * @return bool */ public function touch($attribute = null) { if ($attribute) { $this->$attribute = $this->freshTimestamp(); return $this->save(); } if (! $this->usesTimestamps()) { return false; } $this->updateTimestamps(); return $this->save(); } /** * Update the model's update timestamp without raising any events. * * @param string|null $attribute * @return bool */ public function touchQuietly($attribute = null) { return static::withoutEvents(fn () => $this->touch($attribute)); } /** * Update the creation and update timestamps. * * @return $this */ public function updateTimestamps() { $time = $this->freshTimestamp(); $updatedAtColumn = $this->getUpdatedAtColumn(); if (! is_null($updatedAtColumn) && ! $this->isDirty($updatedAtColumn)) { $this->setUpdatedAt($time); } $createdAtColumn = $this->getCreatedAtColumn(); if (! $this->exists && ! is_null($createdAtColumn) && ! $this->isDirty($createdAtColumn)) { $this->setCreatedAt($time); } return $this; } /** * Set the value of the "created at" attribute. * * @param mixed $value * @return $this */ public function setCreatedAt($value) { $this->{$this->getCreatedAtColumn()} = $value; return $this; } /** * Set the value of the "updated at" attribute. * * @param mixed $value * @return $this */ public function setUpdatedAt($value) { $this->{$this->getUpdatedAtColumn()} = $value; return $this; } /** * Get a fresh timestamp for the model. * * @return \Illuminate\Support\Carbon */ public function freshTimestamp() { return Date::now(); } /** * Get a fresh timestamp for the model. * * @return string */ public function freshTimestampString() { return $this->fromDateTime($this->freshTimestamp()); } /** * Determine if the model uses timestamps. * * @return bool */ public function usesTimestamps() { return $this->timestamps && ! static::isIgnoringTimestamps($this::class); } /** * Get the name of the "created at" column. * * @return string|null */ public function getCreatedAtColumn() { return static::CREATED_AT; } /** * Get the name of the "updated at" column. * * @return string|null */ public function getUpdatedAtColumn() { return static::UPDATED_AT; } /** * Get the fully qualified "created at" column. * * @return string|null */ public function getQualifiedCreatedAtColumn() { return $this->qualifyColumn($this->getCreatedAtColumn()); } /** * Get the fully qualified "updated at" column. * * @return string|null */ public function getQualifiedUpdatedAtColumn() { return $this->qualifyColumn($this->getUpdatedAtColumn()); } /** * Disable timestamps for the current class during the given callback scope. * * @param callable $callback * @return mixed */ public static function withoutTimestamps(callable $callback) { return static::withoutTimestampsOn([static::class], $callback); } /** * Disable timestamps for the given model classes during the given callback scope. * * @param array $models * @param callable $callback * @return mixed */ public static function withoutTimestampsOn($models, $callback) { static::$ignoreTimestampsOn = array_values(array_merge(static::$ignoreTimestampsOn, $models)); try { return $callback(); } finally { static::$ignoreTimestampsOn = array_values(array_diff(static::$ignoreTimestampsOn, $models)); } } /** * Determine if the given model is ignoring timestamps / touches. * * @param string|null $class * @return bool */ public static function isIgnoringTimestamps($class = null) { $class ??= static::class; foreach (static::$ignoreTimestampsOn as $ignoredClass) { if ($class === $ignoredClass || is_subclass_of($class, $ignoredClass)) { return true; } } return false; } } src/Illuminate/Database/Eloquent/Concerns/HidesAttributes.php 0000644 00000005535 15107327037 0020341 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Concerns; trait HidesAttributes { /** * The attributes that should be hidden for serialization. * * @var array<string> */ protected $hidden = []; /** * The attributes that should be visible in serialization. * * @var array<string> */ protected $visible = []; /** * Get the hidden attributes for the model. * * @return array<string> */ public function getHidden() { return $this->hidden; } /** * Set the hidden attributes for the model. * * @param array<string> $hidden * @return $this */ public function setHidden(array $hidden) { $this->hidden = $hidden; return $this; } /** * Get the visible attributes for the model. * * @return array<string> */ public function getVisible() { return $this->visible; } /** * Set the visible attributes for the model. * * @param array<string> $visible * @return $this */ public function setVisible(array $visible) { $this->visible = $visible; return $this; } /** * Make the given, typically hidden, attributes visible. * * @param array<string>|string|null $attributes * @return $this */ public function makeVisible($attributes) { $attributes = is_array($attributes) ? $attributes : func_get_args(); $this->hidden = array_diff($this->hidden, $attributes); if (! empty($this->visible)) { $this->visible = array_values(array_unique(array_merge($this->visible, $attributes))); } return $this; } /** * Make the given, typically hidden, attributes visible if the given truth test passes. * * @param bool|\Closure $condition * @param array<string>|string|null $attributes * @return $this */ public function makeVisibleIf($condition, $attributes) { return value($condition, $this) ? $this->makeVisible($attributes) : $this; } /** * Make the given, typically visible, attributes hidden. * * @param array<string>|string|null $attributes * @return $this */ public function makeHidden($attributes) { $this->hidden = array_values(array_unique(array_merge( $this->hidden, is_array($attributes) ? $attributes : func_get_args() ))); return $this; } /** * Make the given, typically visible, attributes hidden if the given truth test passes. * * @param bool|\Closure $condition * @param array<string>|string|null $attributes * @return $this */ public function makeHiddenIf($condition, $attributes) { return value($condition, $this) ? $this->makeHidden($attributes) : $this; } } src/Illuminate/Database/Eloquent/Concerns/HasGlobalScopes.php 0000644 00000005016 15107327037 0020241 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Concerns; use Closure; use Illuminate\Database\Eloquent\Scope; use Illuminate\Support\Arr; use InvalidArgumentException; trait HasGlobalScopes { /** * Register a new global scope on the model. * * @param \Illuminate\Database\Eloquent\Scope|\Closure|string $scope * @param \Illuminate\Database\Eloquent\Scope|\Closure|null $implementation * @return mixed * * @throws \InvalidArgumentException */ public static function addGlobalScope($scope, $implementation = null) { if (is_string($scope) && ($implementation instanceof Closure || $implementation instanceof Scope)) { return static::$globalScopes[static::class][$scope] = $implementation; } elseif ($scope instanceof Closure) { return static::$globalScopes[static::class][spl_object_hash($scope)] = $scope; } elseif ($scope instanceof Scope) { return static::$globalScopes[static::class][get_class($scope)] = $scope; } throw new InvalidArgumentException('Global scope must be an instance of Closure or Scope.'); } /** * Determine if a model has a global scope. * * @param \Illuminate\Database\Eloquent\Scope|string $scope * @return bool */ public static function hasGlobalScope($scope) { return ! is_null(static::getGlobalScope($scope)); } /** * Get a global scope registered with the model. * * @param \Illuminate\Database\Eloquent\Scope|string $scope * @return \Illuminate\Database\Eloquent\Scope|\Closure|null */ public static function getGlobalScope($scope) { if (is_string($scope)) { return Arr::get(static::$globalScopes, static::class.'.'.$scope); } return Arr::get( static::$globalScopes, static::class.'.'.get_class($scope) ); } /** * Get all of the global scopes that are currently registered. * * @return array */ public static function getAllGlobalScopes() { return static::$globalScopes; } /** * Set the current global scopes. * * @param array $scopes * @return void */ public static function setAllGlobalScopes($scopes) { static::$globalScopes = $scopes; } /** * Get the global scopes for this class instance. * * @return array */ public function getGlobalScopes() { return Arr::get(static::$globalScopes, static::class, []); } } src/Illuminate/Database/Eloquent/Concerns/HasUlids.php 0000644 00000004217 15107327037 0016746 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Concerns; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Str; trait HasUlids { /** * Initialize the trait. * * @return void */ public function initializeHasUlids() { $this->usesUniqueIds = true; } /** * Get the columns that should receive a unique identifier. * * @return array */ public function uniqueIds() { return [$this->getKeyName()]; } /** * Generate a new ULID for the model. * * @return string */ public function newUniqueId() { return strtolower((string) Str::ulid()); } /** * Retrieve the model for a bound value. * * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation $query * @param mixed $value * @param string|null $field * @return \Illuminate\Database\Eloquent\Relations\Relation * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException */ public function resolveRouteBindingQuery($query, $value, $field = null) { if ($field && in_array($field, $this->uniqueIds()) && ! Str::isUlid($value)) { throw (new ModelNotFoundException)->setModel(get_class($this), $value); } if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! Str::isUlid($value)) { throw (new ModelNotFoundException)->setModel(get_class($this), $value); } return parent::resolveRouteBindingQuery($query, $value, $field); } /** * Get the auto-incrementing key type. * * @return string */ public function getKeyType() { if (in_array($this->getKeyName(), $this->uniqueIds())) { return 'string'; } return $this->keyType; } /** * Get the value indicating whether the IDs are incrementing. * * @return bool */ public function getIncrementing() { if (in_array($this->getKeyName(), $this->uniqueIds())) { return false; } return $this->incrementing; } } src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php 0000644 00000074047 15107327037 0020522 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Concerns; use Closure; use Illuminate\Database\ClassMorphViolationException; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\PendingHasThroughRelationship; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasOneThrough; use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Arr; use Illuminate\Support\Str; trait HasRelationships { /** * The loaded relationships for the model. * * @var array */ protected $relations = []; /** * The relationships that should be touched on save. * * @var array */ protected $touches = []; /** * The many to many relationship methods. * * @var string[] */ public static $manyMethods = [ 'belongsToMany', 'morphToMany', 'morphedByMany', ]; /** * The relation resolver callbacks. * * @var array */ protected static $relationResolvers = []; /** * Get the dynamic relation resolver if defined or inherited, or return null. * * @param string $class * @param string $key * @return mixed */ public function relationResolver($class, $key) { if ($resolver = static::$relationResolvers[$class][$key] ?? null) { return $resolver; } if ($parent = get_parent_class($class)) { return $this->relationResolver($parent, $key); } return null; } /** * Define a dynamic relation resolver. * * @param string $name * @param \Closure $callback * @return void */ public static function resolveRelationUsing($name, Closure $callback) { static::$relationResolvers = array_replace_recursive( static::$relationResolvers, [static::class => [$name => $callback]] ); } /** * Define a one-to-one relationship. * * @param string $related * @param string|null $foreignKey * @param string|null $localKey * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function hasOne($related, $foreignKey = null, $localKey = null) { $instance = $this->newRelatedInstance($related); $foreignKey = $foreignKey ?: $this->getForeignKey(); $localKey = $localKey ?: $this->getKeyName(); return $this->newHasOne($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey); } /** * Instantiate a new HasOne relationship. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $foreignKey * @param string $localKey * @return \Illuminate\Database\Eloquent\Relations\HasOne */ protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey) { return new HasOne($query, $parent, $foreignKey, $localKey); } /** * Define a has-one-through relationship. * * @param string $related * @param string $through * @param string|null $firstKey * @param string|null $secondKey * @param string|null $localKey * @param string|null $secondLocalKey * @return \Illuminate\Database\Eloquent\Relations\HasOneThrough */ public function hasOneThrough($related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null) { $through = $this->newRelatedThroughInstance($through); $firstKey = $firstKey ?: $this->getForeignKey(); $secondKey = $secondKey ?: $through->getForeignKey(); return $this->newHasOneThrough( $this->newRelatedInstance($related)->newQuery(), $this, $through, $firstKey, $secondKey, $localKey ?: $this->getKeyName(), $secondLocalKey ?: $through->getKeyName() ); } /** * Instantiate a new HasOneThrough relationship. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $farParent * @param \Illuminate\Database\Eloquent\Model $throughParent * @param string $firstKey * @param string $secondKey * @param string $localKey * @param string $secondLocalKey * @return \Illuminate\Database\Eloquent\Relations\HasOneThrough */ protected function newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) { return new HasOneThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); } /** * Define a polymorphic one-to-one relationship. * * @param string $related * @param string $name * @param string|null $type * @param string|null $id * @param string|null $localKey * @return \Illuminate\Database\Eloquent\Relations\MorphOne */ public function morphOne($related, $name, $type = null, $id = null, $localKey = null) { $instance = $this->newRelatedInstance($related); [$type, $id] = $this->getMorphs($name, $type, $id); $table = $instance->getTable(); $localKey = $localKey ?: $this->getKeyName(); return $this->newMorphOne($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey); } /** * Instantiate a new MorphOne relationship. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $type * @param string $id * @param string $localKey * @return \Illuminate\Database\Eloquent\Relations\MorphOne */ protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey) { return new MorphOne($query, $parent, $type, $id, $localKey); } /** * Define an inverse one-to-one or many relationship. * * @param string $related * @param string|null $foreignKey * @param string|null $ownerKey * @param string|null $relation * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null) { // If no relation name was given, we will use this debug backtrace to extract // the calling method's name and use that as the relationship name as most // of the time this will be what we desire to use for the relationships. if (is_null($relation)) { $relation = $this->guessBelongsToRelation(); } $instance = $this->newRelatedInstance($related); // If no foreign key was supplied, we can use a backtrace to guess the proper // foreign key name by using the name of the relationship function, which // when combined with an "_id" should conventionally match the columns. if (is_null($foreignKey)) { $foreignKey = Str::snake($relation).'_'.$instance->getKeyName(); } // Once we have the foreign key names we'll just create a new Eloquent query // for the related models and return the relationship instance which will // actually be responsible for retrieving and hydrating every relation. $ownerKey = $ownerKey ?: $instance->getKeyName(); return $this->newBelongsTo( $instance->newQuery(), $this, $foreignKey, $ownerKey, $relation ); } /** * Instantiate a new BelongsTo relationship. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $child * @param string $foreignKey * @param string $ownerKey * @param string $relation * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation) { return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation); } /** * Define a polymorphic, inverse one-to-one or many relationship. * * @param string|null $name * @param string|null $type * @param string|null $id * @param string|null $ownerKey * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null) { // If no name is provided, we will use the backtrace to get the function name // since that is most likely the name of the polymorphic interface. We can // use that to get both the class and foreign key that will be utilized. $name = $name ?: $this->guessBelongsToRelation(); [$type, $id] = $this->getMorphs( Str::snake($name), $type, $id ); // If the type value is null it is probably safe to assume we're eager loading // the relationship. In this case we'll just pass in a dummy query where we // need to remove any eager loads that may already be defined on a model. return is_null($class = $this->getAttributeFromArray($type)) || $class === '' ? $this->morphEagerTo($name, $type, $id, $ownerKey) : $this->morphInstanceTo($class, $name, $type, $id, $ownerKey); } /** * Define a polymorphic, inverse one-to-one or many relationship. * * @param string $name * @param string $type * @param string $id * @param string $ownerKey * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ protected function morphEagerTo($name, $type, $id, $ownerKey) { return $this->newMorphTo( $this->newQuery()->setEagerLoads([]), $this, $id, $ownerKey, $type, $name ); } /** * Define a polymorphic, inverse one-to-one or many relationship. * * @param string $target * @param string $name * @param string $type * @param string $id * @param string $ownerKey * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ protected function morphInstanceTo($target, $name, $type, $id, $ownerKey) { $instance = $this->newRelatedInstance( static::getActualClassNameForMorph($target) ); return $this->newMorphTo( $instance->newQuery(), $this, $id, $ownerKey ?? $instance->getKeyName(), $type, $name ); } /** * Instantiate a new MorphTo relationship. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $foreignKey * @param string $ownerKey * @param string $type * @param string $relation * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ protected function newMorphTo(Builder $query, Model $parent, $foreignKey, $ownerKey, $type, $relation) { return new MorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation); } /** * Retrieve the actual class name for a given morph class. * * @param string $class * @return string */ public static function getActualClassNameForMorph($class) { return Arr::get(Relation::morphMap() ?: [], $class, $class); } /** * Guess the "belongs to" relationship name. * * @return string */ protected function guessBelongsToRelation() { [$one, $two, $caller] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); return $caller['function']; } /** * Create a pending has-many-through or has-one-through relationship. * * @param string|\Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\HasOne $relationship * @return \Illuminate\Database\Eloquent\PendingHasThroughRelationship */ public function through($relationship) { if (is_string($relationship)) { $relationship = $this->{$relationship}(); } return new PendingHasThroughRelationship($this, $relationship); } /** * Define a one-to-many relationship. * * @param string $related * @param string|null $foreignKey * @param string|null $localKey * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function hasMany($related, $foreignKey = null, $localKey = null) { $instance = $this->newRelatedInstance($related); $foreignKey = $foreignKey ?: $this->getForeignKey(); $localKey = $localKey ?: $this->getKeyName(); return $this->newHasMany( $instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey ); } /** * Instantiate a new HasMany relationship. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $foreignKey * @param string $localKey * @return \Illuminate\Database\Eloquent\Relations\HasMany */ protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey) { return new HasMany($query, $parent, $foreignKey, $localKey); } /** * Define a has-many-through relationship. * * @param string $related * @param string $through * @param string|null $firstKey * @param string|null $secondKey * @param string|null $localKey * @param string|null $secondLocalKey * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough */ public function hasManyThrough($related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null) { $through = $this->newRelatedThroughInstance($through); $firstKey = $firstKey ?: $this->getForeignKey(); $secondKey = $secondKey ?: $through->getForeignKey(); return $this->newHasManyThrough( $this->newRelatedInstance($related)->newQuery(), $this, $through, $firstKey, $secondKey, $localKey ?: $this->getKeyName(), $secondLocalKey ?: $through->getKeyName() ); } /** * Instantiate a new HasManyThrough relationship. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $farParent * @param \Illuminate\Database\Eloquent\Model $throughParent * @param string $firstKey * @param string $secondKey * @param string $localKey * @param string $secondLocalKey * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough */ protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) { return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); } /** * Define a polymorphic one-to-many relationship. * * @param string $related * @param string $name * @param string|null $type * @param string|null $id * @param string|null $localKey * @return \Illuminate\Database\Eloquent\Relations\MorphMany */ public function morphMany($related, $name, $type = null, $id = null, $localKey = null) { $instance = $this->newRelatedInstance($related); // Here we will gather up the morph type and ID for the relationship so that we // can properly query the intermediate table of a relation. Finally, we will // get the table and create the relationship instances for the developers. [$type, $id] = $this->getMorphs($name, $type, $id); $table = $instance->getTable(); $localKey = $localKey ?: $this->getKeyName(); return $this->newMorphMany($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey); } /** * Instantiate a new MorphMany relationship. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $type * @param string $id * @param string $localKey * @return \Illuminate\Database\Eloquent\Relations\MorphMany */ protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey) { return new MorphMany($query, $parent, $type, $id, $localKey); } /** * Define a many-to-many relationship. * * @param string $related * @param string|class-string<\Illuminate\Database\Eloquent\Model>|null $table * @param string|null $foreignPivotKey * @param string|null $relatedPivotKey * @param string|null $parentKey * @param string|null $relatedKey * @param string|null $relation * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function belongsToMany($related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, $parentKey = null, $relatedKey = null, $relation = null) { // If no relationship name was passed, we will pull backtraces to get the // name of the calling function. We will use that function name as the // title of this relation since that is a great convention to apply. if (is_null($relation)) { $relation = $this->guessBelongsToManyRelation(); } // First, we'll need to determine the foreign key and "other key" for the // relationship. Once we have determined the keys we'll make the query // instances as well as the relationship instances we need for this. $instance = $this->newRelatedInstance($related); $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); // If no table name was provided, we can guess it by concatenating the two // models using underscores in alphabetical order. The two model names // are transformed to snake case from their default CamelCase also. if (is_null($table)) { $table = $this->joiningTable($related, $instance); } return $this->newBelongsToMany( $instance->newQuery(), $this, $table, $foreignPivotKey, $relatedPivotKey, $parentKey ?: $this->getKeyName(), $relatedKey ?: $instance->getKeyName(), $relation ); } /** * Instantiate a new BelongsToMany relationship. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string|class-string<\Illuminate\Database\Eloquent\Model> $table * @param string $foreignPivotKey * @param string $relatedPivotKey * @param string $parentKey * @param string $relatedKey * @param string|null $relationName * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null) { return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); } /** * Define a polymorphic many-to-many relationship. * * @param string $related * @param string $name * @param string|null $table * @param string|null $foreignPivotKey * @param string|null $relatedPivotKey * @param string|null $parentKey * @param string|null $relatedKey * @param string|null $relation * @param bool $inverse * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ public function morphToMany($related, $name, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, $parentKey = null, $relatedKey = null, $relation = null, $inverse = false) { $relation = $relation ?: $this->guessBelongsToManyRelation(); // First, we will need to determine the foreign key and "other key" for the // relationship. Once we have determined the keys we will make the query // instances, as well as the relationship instances we need for these. $instance = $this->newRelatedInstance($related); $foreignPivotKey = $foreignPivotKey ?: $name.'_id'; $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); // Now we're ready to create a new query builder for the related model and // the relationship instances for this relation. This relation will set // appropriate query constraints then entirely manage the hydrations. if (! $table) { $words = preg_split('/(_)/u', $name, -1, PREG_SPLIT_DELIM_CAPTURE); $lastWord = array_pop($words); $table = implode('', $words).Str::plural($lastWord); } return $this->newMorphToMany( $instance->newQuery(), $this, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey ?: $this->getKeyName(), $relatedKey ?: $instance->getKeyName(), $relation, $inverse ); } /** * Instantiate a new MorphToMany relationship. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $name * @param string $table * @param string $foreignPivotKey * @param string $relatedPivotKey * @param string $parentKey * @param string $relatedKey * @param string|null $relationName * @param bool $inverse * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ protected function newMorphToMany(Builder $query, Model $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null, $inverse = false) { return new MorphToMany($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName, $inverse); } /** * Define a polymorphic, inverse many-to-many relationship. * * @param string $related * @param string $name * @param string|null $table * @param string|null $foreignPivotKey * @param string|null $relatedPivotKey * @param string|null $parentKey * @param string|null $relatedKey * @param string|null $relation * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ public function morphedByMany($related, $name, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, $parentKey = null, $relatedKey = null, $relation = null) { $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); // For the inverse of the polymorphic many-to-many relations, we will change // the way we determine the foreign and other keys, as it is the opposite // of the morph-to-many method since we're figuring out these inverses. $relatedPivotKey = $relatedPivotKey ?: $name.'_id'; return $this->morphToMany( $related, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relation, true ); } /** * Get the relationship name of the belongsToMany relationship. * * @return string|null */ protected function guessBelongsToManyRelation() { $caller = Arr::first(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), function ($trace) { return ! in_array( $trace['function'], array_merge(static::$manyMethods, ['guessBelongsToManyRelation']) ); }); return ! is_null($caller) ? $caller['function'] : null; } /** * Get the joining table name for a many-to-many relation. * * @param string $related * @param \Illuminate\Database\Eloquent\Model|null $instance * @return string */ public function joiningTable($related, $instance = null) { // The joining table name, by convention, is simply the snake cased models // sorted alphabetically and concatenated with an underscore, so we can // just sort the models and join them together to get the table name. $segments = [ $instance ? $instance->joiningTableSegment() : Str::snake(class_basename($related)), $this->joiningTableSegment(), ]; // Now that we have the model names in an array we can just sort them and // use the implode function to join them together with an underscores, // which is typically used by convention within the database system. sort($segments); return strtolower(implode('_', $segments)); } /** * Get this model's half of the intermediate table name for belongsToMany relationships. * * @return string */ public function joiningTableSegment() { return Str::snake(class_basename($this)); } /** * Determine if the model touches a given relation. * * @param string $relation * @return bool */ public function touches($relation) { return in_array($relation, $this->getTouchedRelations()); } /** * Touch the owning relations of the model. * * @return void */ public function touchOwners() { foreach ($this->getTouchedRelations() as $relation) { $this->$relation()->touch(); if ($this->$relation instanceof self) { $this->$relation->fireModelEvent('saved', false); $this->$relation->touchOwners(); } elseif ($this->$relation instanceof Collection) { $this->$relation->each->touchOwners(); } } } /** * Get the polymorphic relationship columns. * * @param string $name * @param string $type * @param string $id * @return array */ protected function getMorphs($name, $type, $id) { return [$type ?: $name.'_type', $id ?: $name.'_id']; } /** * Get the class name for polymorphic relations. * * @return string */ public function getMorphClass() { $morphMap = Relation::morphMap(); if (! empty($morphMap) && in_array(static::class, $morphMap)) { return array_search(static::class, $morphMap, true); } if (static::class === Pivot::class) { return static::class; } if (Relation::requiresMorphMap()) { throw new ClassMorphViolationException($this); } return static::class; } /** * Create a new model instance for a related model. * * @param string $class * @return mixed */ protected function newRelatedInstance($class) { return tap(new $class, function ($instance) { if (! $instance->getConnectionName()) { $instance->setConnection($this->connection); } }); } /** * Create a new model instance for a related "through" model. * * @param string $class * @return mixed */ protected function newRelatedThroughInstance($class) { return new $class; } /** * Get all the loaded relations for the instance. * * @return array */ public function getRelations() { return $this->relations; } /** * Get a specified relationship. * * @param string $relation * @return mixed */ public function getRelation($relation) { return $this->relations[$relation]; } /** * Determine if the given relation is loaded. * * @param string $key * @return bool */ public function relationLoaded($key) { return array_key_exists($key, $this->relations); } /** * Set the given relationship on the model. * * @param string $relation * @param mixed $value * @return $this */ public function setRelation($relation, $value) { $this->relations[$relation] = $value; return $this; } /** * Unset a loaded relationship. * * @param string $relation * @return $this */ public function unsetRelation($relation) { unset($this->relations[$relation]); return $this; } /** * Set the entire relations array on the model. * * @param array $relations * @return $this */ public function setRelations(array $relations) { $this->relations = $relations; return $this; } /** * Duplicate the instance and unset all the loaded relations. * * @return $this */ public function withoutRelations() { $model = clone $this; return $model->unsetRelations(); } /** * Unset all the loaded relations for the instance. * * @return $this */ public function unsetRelations() { $this->relations = []; return $this; } /** * Get the relationships that are touched on save. * * @return array */ public function getTouchedRelations() { return $this->touches; } /** * Set the relationships that are touched on save. * * @param array $touches * @return $this */ public function setTouchedRelations(array $touches) { $this->touches = $touches; return $this; } } src/Illuminate/Database/Eloquent/Concerns/HasUuids.php 0000644 00000004212 15107327037 0016752 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Concerns; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Str; trait HasUuids { /** * Initialize the trait. * * @return void */ public function initializeHasUuids() { $this->usesUniqueIds = true; } /** * Get the columns that should receive a unique identifier. * * @return array */ public function uniqueIds() { return [$this->getKeyName()]; } /** * Generate a new UUID for the model. * * @return string */ public function newUniqueId() { return (string) Str::orderedUuid(); } /** * Retrieve the model for a bound value. * * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation $query * @param mixed $value * @param string|null $field * @return \Illuminate\Database\Eloquent\Relations\Relation * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException */ public function resolveRouteBindingQuery($query, $value, $field = null) { if ($field && in_array($field, $this->uniqueIds()) && ! Str::isUuid($value)) { throw (new ModelNotFoundException)->setModel(get_class($this), $value); } if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! Str::isUuid($value)) { throw (new ModelNotFoundException)->setModel(get_class($this), $value); } return parent::resolveRouteBindingQuery($query, $value, $field); } /** * Get the auto-incrementing key type. * * @return string */ public function getKeyType() { if (in_array($this->getKeyName(), $this->uniqueIds())) { return 'string'; } return $this->keyType; } /** * Get the value indicating whether the IDs are incrementing. * * @return bool */ public function getIncrementing() { if (in_array($this->getKeyName(), $this->uniqueIds())) { return false; } return $this->incrementing; } } src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php 0000644 00000013601 15107327037 0020523 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Concerns; trait GuardsAttributes { /** * The attributes that are mass assignable. * * @var array<string> */ protected $fillable = []; /** * The attributes that aren't mass assignable. * * @var array<string>|bool */ protected $guarded = ['*']; /** * Indicates if all mass assignment is enabled. * * @var bool */ protected static $unguarded = false; /** * The actual columns that exist on the database and can be guarded. * * @var array<string> */ protected static $guardableColumns = []; /** * Get the fillable attributes for the model. * * @return array<string> */ public function getFillable() { return $this->fillable; } /** * Set the fillable attributes for the model. * * @param array<string> $fillable * @return $this */ public function fillable(array $fillable) { $this->fillable = $fillable; return $this; } /** * Merge new fillable attributes with existing fillable attributes on the model. * * @param array<string> $fillable * @return $this */ public function mergeFillable(array $fillable) { $this->fillable = array_values(array_unique(array_merge($this->fillable, $fillable))); return $this; } /** * Get the guarded attributes for the model. * * @return array<string> */ public function getGuarded() { return $this->guarded === false ? [] : $this->guarded; } /** * Set the guarded attributes for the model. * * @param array<string> $guarded * @return $this */ public function guard(array $guarded) { $this->guarded = $guarded; return $this; } /** * Merge new guarded attributes with existing guarded attributes on the model. * * @param array<string> $guarded * @return $this */ public function mergeGuarded(array $guarded) { $this->guarded = array_values(array_unique(array_merge($this->guarded, $guarded))); return $this; } /** * Disable all mass assignable restrictions. * * @param bool $state * @return void */ public static function unguard($state = true) { static::$unguarded = $state; } /** * Enable the mass assignment restrictions. * * @return void */ public static function reguard() { static::$unguarded = false; } /** * Determine if the current state is "unguarded". * * @return bool */ public static function isUnguarded() { return static::$unguarded; } /** * Run the given callable while being unguarded. * * @param callable $callback * @return mixed */ public static function unguarded(callable $callback) { if (static::$unguarded) { return $callback(); } static::unguard(); try { return $callback(); } finally { static::reguard(); } } /** * Determine if the given attribute may be mass assigned. * * @param string $key * @return bool */ public function isFillable($key) { if (static::$unguarded) { return true; } // If the key is in the "fillable" array, we can of course assume that it's // a fillable attribute. Otherwise, we will check the guarded array when // we need to determine if the attribute is black-listed on the model. if (in_array($key, $this->getFillable())) { return true; } // If the attribute is explicitly listed in the "guarded" array then we can // return false immediately. This means this attribute is definitely not // fillable and there is no point in going any further in this method. if ($this->isGuarded($key)) { return false; } return empty($this->getFillable()) && ! str_contains($key, '.') && ! str_starts_with($key, '_'); } /** * Determine if the given key is guarded. * * @param string $key * @return bool */ public function isGuarded($key) { if (empty($this->getGuarded())) { return false; } return $this->getGuarded() == ['*'] || ! empty(preg_grep('/^'.preg_quote($key, '/').'$/i', $this->getGuarded())) || ! $this->isGuardableColumn($key); } /** * Determine if the given column is a valid, guardable column. * * @param string $key * @return bool */ protected function isGuardableColumn($key) { if (! isset(static::$guardableColumns[get_class($this)])) { $columns = $this->getConnection() ->getSchemaBuilder() ->getColumnListing($this->getTable()); if (empty($columns)) { return true; } static::$guardableColumns[get_class($this)] = $columns; } return in_array($key, static::$guardableColumns[get_class($this)]); } /** * Determine if the model is totally guarded. * * @return bool */ public function totallyGuarded() { return count($this->getFillable()) === 0 && $this->getGuarded() == ['*']; } /** * Get the fillable attributes of a given array. * * @param array $attributes * @return array */ protected function fillableFromArray(array $attributes) { if (count($this->getFillable()) > 0 && ! static::$unguarded) { return array_intersect_key($attributes, array_flip($this->getFillable())); } return $attributes; } } src/Illuminate/Database/Eloquent/Concerns/HasUniqueIds.php 0000644 00000001764 15107327037 0017600 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Concerns; trait HasUniqueIds { /** * Indicates if the model uses unique ids. * * @var bool */ public $usesUniqueIds = false; /** * Determine if the model uses unique ids. * * @return bool */ public function usesUniqueIds() { return $this->usesUniqueIds; } /** * Generate unique keys for the model. * * @return void */ public function setUniqueIds() { foreach ($this->uniqueIds() as $column) { if (empty($this->{$column})) { $this->{$column} = $this->newUniqueId(); } } } /** * Generate a new key for the model. * * @return string */ public function newUniqueId() { return null; } /** * Get the columns that should receive a unique identifier. * * @return array */ public function uniqueIds() { return []; } } src/Illuminate/Database/Eloquent/Concerns/HasEvents.php 0000644 00000024731 15107327037 0017135 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Concerns; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Events\NullDispatcher; use Illuminate\Support\Arr; use InvalidArgumentException; trait HasEvents { /** * The event map for the model. * * Allows for object-based events for native Eloquent events. * * @var array */ protected $dispatchesEvents = []; /** * User exposed observable events. * * These are extra user-defined events observers may subscribe to. * * @var array */ protected $observables = []; /** * Register observers with the model. * * @param object|array|string $classes * @return void * * @throws \RuntimeException */ public static function observe($classes) { $instance = new static; foreach (Arr::wrap($classes) as $class) { $instance->registerObserver($class); } } /** * Register a single observer with the model. * * @param object|string $class * @return void * * @throws \RuntimeException */ protected function registerObserver($class) { $className = $this->resolveObserverClassName($class); // When registering a model observer, we will spin through the possible events // and determine if this observer has that method. If it does, we will hook // it into the model's event system, making it convenient to watch these. foreach ($this->getObservableEvents() as $event) { if (method_exists($class, $event)) { static::registerModelEvent($event, $className.'@'.$event); } } } /** * Resolve the observer's class name from an object or string. * * @param object|string $class * @return string * * @throws \InvalidArgumentException */ private function resolveObserverClassName($class) { if (is_object($class)) { return get_class($class); } if (class_exists($class)) { return $class; } throw new InvalidArgumentException('Unable to find observer: '.$class); } /** * Get the observable event names. * * @return array */ public function getObservableEvents() { return array_merge( [ 'retrieved', 'creating', 'created', 'updating', 'updated', 'saving', 'saved', 'restoring', 'restored', 'replicating', 'deleting', 'deleted', 'forceDeleting', 'forceDeleted', ], $this->observables ); } /** * Set the observable event names. * * @param array $observables * @return $this */ public function setObservableEvents(array $observables) { $this->observables = $observables; return $this; } /** * Add an observable event name. * * @param array|mixed $observables * @return void */ public function addObservableEvents($observables) { $this->observables = array_unique(array_merge( $this->observables, is_array($observables) ? $observables : func_get_args() )); } /** * Remove an observable event name. * * @param array|mixed $observables * @return void */ public function removeObservableEvents($observables) { $this->observables = array_diff( $this->observables, is_array($observables) ? $observables : func_get_args() ); } /** * Register a model event with the dispatcher. * * @param string $event * @param \Illuminate\Events\QueuedClosure|\Closure|string|array $callback * @return void */ protected static function registerModelEvent($event, $callback) { if (isset(static::$dispatcher)) { $name = static::class; static::$dispatcher->listen("eloquent.{$event}: {$name}", $callback); } } /** * Fire the given event for the model. * * @param string $event * @param bool $halt * @return mixed */ protected function fireModelEvent($event, $halt = true) { if (! isset(static::$dispatcher)) { return true; } // First, we will get the proper method to call on the event dispatcher, and then we // will attempt to fire a custom, object based event for the given event. If that // returns a result we can return that result, or we'll call the string events. $method = $halt ? 'until' : 'dispatch'; $result = $this->filterModelEventResults( $this->fireCustomModelEvent($event, $method) ); if ($result === false) { return false; } return ! empty($result) ? $result : static::$dispatcher->{$method}( "eloquent.{$event}: ".static::class, $this ); } /** * Fire a custom model event for the given event. * * @param string $event * @param string $method * @return mixed|null */ protected function fireCustomModelEvent($event, $method) { if (! isset($this->dispatchesEvents[$event])) { return; } $result = static::$dispatcher->$method(new $this->dispatchesEvents[$event]($this)); if (! is_null($result)) { return $result; } } /** * Filter the model event results. * * @param mixed $result * @return mixed */ protected function filterModelEventResults($result) { if (is_array($result)) { $result = array_filter($result, function ($response) { return ! is_null($response); }); } return $result; } /** * Register a retrieved model event with the dispatcher. * * @param \Illuminate\Events\QueuedClosure|\Closure|string|array $callback * @return void */ public static function retrieved($callback) { static::registerModelEvent('retrieved', $callback); } /** * Register a saving model event with the dispatcher. * * @param \Illuminate\Events\QueuedClosure|\Closure|string|array $callback * @return void */ public static function saving($callback) { static::registerModelEvent('saving', $callback); } /** * Register a saved model event with the dispatcher. * * @param \Illuminate\Events\QueuedClosure|\Closure|string|array $callback * @return void */ public static function saved($callback) { static::registerModelEvent('saved', $callback); } /** * Register an updating model event with the dispatcher. * * @param \Illuminate\Events\QueuedClosure|\Closure|string|array $callback * @return void */ public static function updating($callback) { static::registerModelEvent('updating', $callback); } /** * Register an updated model event with the dispatcher. * * @param \Illuminate\Events\QueuedClosure|\Closure|string|array $callback * @return void */ public static function updated($callback) { static::registerModelEvent('updated', $callback); } /** * Register a creating model event with the dispatcher. * * @param \Illuminate\Events\QueuedClosure|\Closure|string|array $callback * @return void */ public static function creating($callback) { static::registerModelEvent('creating', $callback); } /** * Register a created model event with the dispatcher. * * @param \Illuminate\Events\QueuedClosure|\Closure|string|array $callback * @return void */ public static function created($callback) { static::registerModelEvent('created', $callback); } /** * Register a replicating model event with the dispatcher. * * @param \Illuminate\Events\QueuedClosure|\Closure|string|array $callback * @return void */ public static function replicating($callback) { static::registerModelEvent('replicating', $callback); } /** * Register a deleting model event with the dispatcher. * * @param \Illuminate\Events\QueuedClosure|\Closure|string|array $callback * @return void */ public static function deleting($callback) { static::registerModelEvent('deleting', $callback); } /** * Register a deleted model event with the dispatcher. * * @param \Illuminate\Events\QueuedClosure|\Closure|string|array $callback * @return void */ public static function deleted($callback) { static::registerModelEvent('deleted', $callback); } /** * Remove all the event listeners for the model. * * @return void */ public static function flushEventListeners() { if (! isset(static::$dispatcher)) { return; } $instance = new static; foreach ($instance->getObservableEvents() as $event) { static::$dispatcher->forget("eloquent.{$event}: ".static::class); } foreach (array_values($instance->dispatchesEvents) as $event) { static::$dispatcher->forget($event); } } /** * Get the event dispatcher instance. * * @return \Illuminate\Contracts\Events\Dispatcher */ public static function getEventDispatcher() { return static::$dispatcher; } /** * Set the event dispatcher instance. * * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher * @return void */ public static function setEventDispatcher(Dispatcher $dispatcher) { static::$dispatcher = $dispatcher; } /** * Unset the event dispatcher for models. * * @return void */ public static function unsetEventDispatcher() { static::$dispatcher = null; } /** * Execute a callback without firing any model events for any model type. * * @param callable $callback * @return mixed */ public static function withoutEvents(callable $callback) { $dispatcher = static::getEventDispatcher(); if ($dispatcher) { static::setEventDispatcher(new NullDispatcher($dispatcher)); } try { return $callback(); } finally { if ($dispatcher) { static::setEventDispatcher($dispatcher); } } } } src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php 0000644 00000201443 15107327037 0020014 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Concerns; use BackedEnum; use Brick\Math\BigDecimal; use Brick\Math\Exception\MathException as BrickMathException; use Brick\Math\RoundingMode; use Carbon\CarbonImmutable; use Carbon\CarbonInterface; use DateTimeImmutable; use DateTimeInterface; use Illuminate\Contracts\Database\Eloquent\Castable; use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Eloquent\Casts\AsArrayObject; use Illuminate\Database\Eloquent\Casts\AsCollection; use Illuminate\Database\Eloquent\Casts\AsEncryptedArrayObject; use Illuminate\Database\Eloquent\Casts\AsEncryptedCollection; use Illuminate\Database\Eloquent\Casts\AsEnumArrayObject; use Illuminate\Database\Eloquent\Casts\AsEnumCollection; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Json; use Illuminate\Database\Eloquent\InvalidCastException; use Illuminate\Database\Eloquent\JsonEncodingException; use Illuminate\Database\Eloquent\MissingAttributeException; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\LazyLoadingViolationException; use Illuminate\Support\Arr; use Illuminate\Support\Carbon; use Illuminate\Support\Collection as BaseCollection; use Illuminate\Support\Exceptions\MathException; use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Date; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; use InvalidArgumentException; use LogicException; use ReflectionClass; use ReflectionMethod; use ReflectionNamedType; use RuntimeException; trait HasAttributes { /** * The model's attributes. * * @var array */ protected $attributes = []; /** * The model attribute's original state. * * @var array */ protected $original = []; /** * The changed model attributes. * * @var array */ protected $changes = []; /** * The attributes that should be cast. * * @var array */ protected $casts = []; /** * The attributes that have been cast using custom classes. * * @var array */ protected $classCastCache = []; /** * The attributes that have been cast using "Attribute" return type mutators. * * @var array */ protected $attributeCastCache = []; /** * The built-in, primitive cast types supported by Eloquent. * * @var string[] */ protected static $primitiveCastTypes = [ 'array', 'bool', 'boolean', 'collection', 'custom_datetime', 'date', 'datetime', 'decimal', 'double', 'encrypted', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object', 'float', 'hashed', 'immutable_date', 'immutable_datetime', 'immutable_custom_datetime', 'int', 'integer', 'json', 'object', 'real', 'string', 'timestamp', ]; /** * The storage format of the model's date columns. * * @var string */ protected $dateFormat; /** * The accessors to append to the model's array form. * * @var array */ protected $appends = []; /** * Indicates whether attributes are snake cased on arrays. * * @var bool */ public static $snakeAttributes = true; /** * The cache of the mutated attributes for each class. * * @var array */ protected static $mutatorCache = []; /** * The cache of the "Attribute" return type marked mutated attributes for each class. * * @var array */ protected static $attributeMutatorCache = []; /** * The cache of the "Attribute" return type marked mutated, gettable attributes for each class. * * @var array */ protected static $getAttributeMutatorCache = []; /** * The cache of the "Attribute" return type marked mutated, settable attributes for each class. * * @var array */ protected static $setAttributeMutatorCache = []; /** * The cache of the converted cast types. * * @var array */ protected static $castTypeCache = []; /** * The encrypter instance that is used to encrypt attributes. * * @var \Illuminate\Contracts\Encryption\Encrypter|null */ public static $encrypter; /** * Convert the model's attributes to an array. * * @return array */ public function attributesToArray() { // If an attribute is a date, we will cast it to a string after converting it // to a DateTime / Carbon instance. This is so we will get some consistent // formatting while accessing attributes vs. arraying / JSONing a model. $attributes = $this->addDateAttributesToArray( $attributes = $this->getArrayableAttributes() ); $attributes = $this->addMutatedAttributesToArray( $attributes, $mutatedAttributes = $this->getMutatedAttributes() ); // Next we will handle any casts that have been setup for this model and cast // the values to their appropriate type. If the attribute has a mutator we // will not perform the cast on those attributes to avoid any confusion. $attributes = $this->addCastAttributesToArray( $attributes, $mutatedAttributes ); // Here we will grab all of the appended, calculated attributes to this model // as these attributes are not really in the attributes array, but are run // when we need to array or JSON the model for convenience to the coder. foreach ($this->getArrayableAppends() as $key) { $attributes[$key] = $this->mutateAttributeForArray($key, null); } return $attributes; } /** * Add the date attributes to the attributes array. * * @param array $attributes * @return array */ protected function addDateAttributesToArray(array $attributes) { foreach ($this->getDates() as $key) { if (! isset($attributes[$key])) { continue; } $attributes[$key] = $this->serializeDate( $this->asDateTime($attributes[$key]) ); } return $attributes; } /** * Add the mutated attributes to the attributes array. * * @param array $attributes * @param array $mutatedAttributes * @return array */ protected function addMutatedAttributesToArray(array $attributes, array $mutatedAttributes) { foreach ($mutatedAttributes as $key) { // We want to spin through all the mutated attributes for this model and call // the mutator for the attribute. We cache off every mutated attributes so // we don't have to constantly check on attributes that actually change. if (! array_key_exists($key, $attributes)) { continue; } // Next, we will call the mutator for this attribute so that we can get these // mutated attribute's actual values. After we finish mutating each of the // attributes we will return this final array of the mutated attributes. $attributes[$key] = $this->mutateAttributeForArray( $key, $attributes[$key] ); } return $attributes; } /** * Add the casted attributes to the attributes array. * * @param array $attributes * @param array $mutatedAttributes * @return array */ protected function addCastAttributesToArray(array $attributes, array $mutatedAttributes) { foreach ($this->getCasts() as $key => $value) { if (! array_key_exists($key, $attributes) || in_array($key, $mutatedAttributes)) { continue; } // Here we will cast the attribute. Then, if the cast is a date or datetime cast // then we will serialize the date for the array. This will convert the dates // to strings based on the date format specified for these Eloquent models. $attributes[$key] = $this->castAttribute( $key, $attributes[$key] ); // If the attribute cast was a date or a datetime, we will serialize the date as // a string. This allows the developers to customize how dates are serialized // into an array without affecting how they are persisted into the storage. if (isset($attributes[$key]) && in_array($value, ['date', 'datetime', 'immutable_date', 'immutable_datetime'])) { $attributes[$key] = $this->serializeDate($attributes[$key]); } if (isset($attributes[$key]) && ($this->isCustomDateTimeCast($value) || $this->isImmutableCustomDateTimeCast($value))) { $attributes[$key] = $attributes[$key]->format(explode(':', $value, 2)[1]); } if ($attributes[$key] instanceof DateTimeInterface && $this->isClassCastable($key)) { $attributes[$key] = $this->serializeDate($attributes[$key]); } if (isset($attributes[$key]) && $this->isClassSerializable($key)) { $attributes[$key] = $this->serializeClassCastableAttribute($key, $attributes[$key]); } if ($this->isEnumCastable($key) && (! ($attributes[$key] ?? null) instanceof Arrayable)) { $attributes[$key] = isset($attributes[$key]) ? $this->getStorableEnumValue($attributes[$key]) : null; } if ($attributes[$key] instanceof Arrayable) { $attributes[$key] = $attributes[$key]->toArray(); } } return $attributes; } /** * Get an attribute array of all arrayable attributes. * * @return array */ protected function getArrayableAttributes() { return $this->getArrayableItems($this->getAttributes()); } /** * Get all of the appendable values that are arrayable. * * @return array */ protected function getArrayableAppends() { if (! count($this->appends)) { return []; } return $this->getArrayableItems( array_combine($this->appends, $this->appends) ); } /** * Get the model's relationships in array form. * * @return array */ public function relationsToArray() { $attributes = []; foreach ($this->getArrayableRelations() as $key => $value) { // If the values implement the Arrayable interface we can just call this // toArray method on the instances which will convert both models and // collections to their proper array form and we'll set the values. if ($value instanceof Arrayable) { $relation = $value->toArray(); } // If the value is null, we'll still go ahead and set it in this list of // attributes, since null is used to represent empty relationships if // it has a has one or belongs to type relationships on the models. elseif (is_null($value)) { $relation = $value; } // If the relationships snake-casing is enabled, we will snake case this // key so that the relation attribute is snake cased in this returned // array to the developers, making this consistent with attributes. if (static::$snakeAttributes) { $key = Str::snake($key); } // If the relation value has been set, we will set it on this attributes // list for returning. If it was not arrayable or null, we'll not set // the value on the array because it is some type of invalid value. if (isset($relation) || is_null($value)) { $attributes[$key] = $relation; } unset($relation); } return $attributes; } /** * Get an attribute array of all arrayable relations. * * @return array */ protected function getArrayableRelations() { return $this->getArrayableItems($this->relations); } /** * Get an attribute array of all arrayable values. * * @param array $values * @return array */ protected function getArrayableItems(array $values) { if (count($this->getVisible()) > 0) { $values = array_intersect_key($values, array_flip($this->getVisible())); } if (count($this->getHidden()) > 0) { $values = array_diff_key($values, array_flip($this->getHidden())); } return $values; } /** * Get an attribute from the model. * * @param string $key * @return mixed */ public function getAttribute($key) { if (! $key) { return; } // If the attribute exists in the attribute array or has a "get" mutator we will // get the attribute's value. Otherwise, we will proceed as if the developers // are asking for a relationship's value. This covers both types of values. if (array_key_exists($key, $this->attributes) || array_key_exists($key, $this->casts) || $this->hasGetMutator($key) || $this->hasAttributeMutator($key) || $this->isClassCastable($key)) { return $this->getAttributeValue($key); } // Here we will determine if the model base class itself contains this given key // since we don't want to treat any of those methods as relationships because // they are all intended as helper methods and none of these are relations. if (method_exists(self::class, $key)) { return $this->throwMissingAttributeExceptionIfApplicable($key); } return $this->isRelation($key) || $this->relationLoaded($key) ? $this->getRelationValue($key) : $this->throwMissingAttributeExceptionIfApplicable($key); } /** * Either throw a missing attribute exception or return null depending on Eloquent's configuration. * * @param string $key * @return null * * @throws \Illuminate\Database\Eloquent\MissingAttributeException */ protected function throwMissingAttributeExceptionIfApplicable($key) { if ($this->exists && ! $this->wasRecentlyCreated && static::preventsAccessingMissingAttributes()) { if (isset(static::$missingAttributeViolationCallback)) { return call_user_func(static::$missingAttributeViolationCallback, $this, $key); } throw new MissingAttributeException($this, $key); } return null; } /** * Get a plain attribute (not a relationship). * * @param string $key * @return mixed */ public function getAttributeValue($key) { return $this->transformModelValue($key, $this->getAttributeFromArray($key)); } /** * Get an attribute from the $attributes array. * * @param string $key * @return mixed */ protected function getAttributeFromArray($key) { return $this->getAttributes()[$key] ?? null; } /** * Get a relationship. * * @param string $key * @return mixed */ public function getRelationValue($key) { // If the key already exists in the relationships array, it just means the // relationship has already been loaded, so we'll just return it out of // here because there is no need to query within the relations twice. if ($this->relationLoaded($key)) { return $this->relations[$key]; } if (! $this->isRelation($key)) { return; } if ($this->preventsLazyLoading) { $this->handleLazyLoadingViolation($key); } // If the "attribute" exists as a method on the model, we will just assume // it is a relationship and will load and return results from the query // and hydrate the relationship's value on the "relationships" array. return $this->getRelationshipFromMethod($key); } /** * Determine if the given key is a relationship method on the model. * * @param string $key * @return bool */ public function isRelation($key) { if ($this->hasAttributeMutator($key)) { return false; } return method_exists($this, $key) || $this->relationResolver(static::class, $key); } /** * Handle a lazy loading violation. * * @param string $key * @return mixed */ protected function handleLazyLoadingViolation($key) { if (isset(static::$lazyLoadingViolationCallback)) { return call_user_func(static::$lazyLoadingViolationCallback, $this, $key); } if (! $this->exists || $this->wasRecentlyCreated) { return; } throw new LazyLoadingViolationException($this, $key); } /** * Get a relationship value from a method. * * @param string $method * @return mixed * * @throws \LogicException */ protected function getRelationshipFromMethod($method) { $relation = $this->$method(); if (! $relation instanceof Relation) { if (is_null($relation)) { throw new LogicException(sprintf( '%s::%s must return a relationship instance, but "null" was returned. Was the "return" keyword used?', static::class, $method )); } throw new LogicException(sprintf( '%s::%s must return a relationship instance.', static::class, $method )); } return tap($relation->getResults(), function ($results) use ($method) { $this->setRelation($method, $results); }); } /** * Determine if a get mutator exists for an attribute. * * @param string $key * @return bool */ public function hasGetMutator($key) { return method_exists($this, 'get'.Str::studly($key).'Attribute'); } /** * Determine if a "Attribute" return type marked mutator exists for an attribute. * * @param string $key * @return bool */ public function hasAttributeMutator($key) { if (isset(static::$attributeMutatorCache[get_class($this)][$key])) { return static::$attributeMutatorCache[get_class($this)][$key]; } if (! method_exists($this, $method = Str::camel($key))) { return static::$attributeMutatorCache[get_class($this)][$key] = false; } $returnType = (new ReflectionMethod($this, $method))->getReturnType(); return static::$attributeMutatorCache[get_class($this)][$key] = $returnType instanceof ReflectionNamedType && $returnType->getName() === Attribute::class; } /** * Determine if a "Attribute" return type marked get mutator exists for an attribute. * * @param string $key * @return bool */ public function hasAttributeGetMutator($key) { if (isset(static::$getAttributeMutatorCache[get_class($this)][$key])) { return static::$getAttributeMutatorCache[get_class($this)][$key]; } if (! $this->hasAttributeMutator($key)) { return static::$getAttributeMutatorCache[get_class($this)][$key] = false; } return static::$getAttributeMutatorCache[get_class($this)][$key] = is_callable($this->{Str::camel($key)}()->get); } /** * Get the value of an attribute using its mutator. * * @param string $key * @param mixed $value * @return mixed */ protected function mutateAttribute($key, $value) { return $this->{'get'.Str::studly($key).'Attribute'}($value); } /** * Get the value of an "Attribute" return type marked attribute using its mutator. * * @param string $key * @param mixed $value * @return mixed */ protected function mutateAttributeMarkedAttribute($key, $value) { if (array_key_exists($key, $this->attributeCastCache)) { return $this->attributeCastCache[$key]; } $attribute = $this->{Str::camel($key)}(); $value = call_user_func($attribute->get ?: function ($value) { return $value; }, $value, $this->attributes); if ($attribute->withCaching || (is_object($value) && $attribute->withObjectCaching)) { $this->attributeCastCache[$key] = $value; } else { unset($this->attributeCastCache[$key]); } return $value; } /** * Get the value of an attribute using its mutator for array conversion. * * @param string $key * @param mixed $value * @return mixed */ protected function mutateAttributeForArray($key, $value) { if ($this->isClassCastable($key)) { $value = $this->getClassCastableAttributeValue($key, $value); } elseif (isset(static::$getAttributeMutatorCache[get_class($this)][$key]) && static::$getAttributeMutatorCache[get_class($this)][$key] === true) { $value = $this->mutateAttributeMarkedAttribute($key, $value); $value = $value instanceof DateTimeInterface ? $this->serializeDate($value) : $value; } else { $value = $this->mutateAttribute($key, $value); } return $value instanceof Arrayable ? $value->toArray() : $value; } /** * Merge new casts with existing casts on the model. * * @param array $casts * @return $this */ public function mergeCasts($casts) { $this->casts = array_merge($this->casts, $casts); return $this; } /** * Cast an attribute to a native PHP type. * * @param string $key * @param mixed $value * @return mixed */ protected function castAttribute($key, $value) { $castType = $this->getCastType($key); if (is_null($value) && in_array($castType, static::$primitiveCastTypes)) { return $value; } // If the key is one of the encrypted castable types, we'll first decrypt // the value and update the cast type so we may leverage the following // logic for casting this value to any additionally specified types. if ($this->isEncryptedCastable($key)) { $value = $this->fromEncryptedString($value); $castType = Str::after($castType, 'encrypted:'); } switch ($castType) { case 'int': case 'integer': return (int) $value; case 'real': case 'float': case 'double': return $this->fromFloat($value); case 'decimal': return $this->asDecimal($value, explode(':', $this->getCasts()[$key], 2)[1]); case 'string': return (string) $value; case 'bool': case 'boolean': return (bool) $value; case 'object': return $this->fromJson($value, true); case 'array': case 'json': return $this->fromJson($value); case 'collection': return new BaseCollection($this->fromJson($value)); case 'date': return $this->asDate($value); case 'datetime': case 'custom_datetime': return $this->asDateTime($value); case 'immutable_date': return $this->asDate($value)->toImmutable(); case 'immutable_custom_datetime': case 'immutable_datetime': return $this->asDateTime($value)->toImmutable(); case 'timestamp': return $this->asTimestamp($value); } if ($this->isEnumCastable($key)) { return $this->getEnumCastableAttributeValue($key, $value); } if ($this->isClassCastable($key)) { return $this->getClassCastableAttributeValue($key, $value); } return $value; } /** * Cast the given attribute using a custom cast class. * * @param string $key * @param mixed $value * @return mixed */ protected function getClassCastableAttributeValue($key, $value) { $caster = $this->resolveCasterClass($key); $objectCachingDisabled = $caster->withoutObjectCaching ?? false; if (isset($this->classCastCache[$key]) && ! $objectCachingDisabled) { return $this->classCastCache[$key]; } else { $value = $caster instanceof CastsInboundAttributes ? $value : $caster->get($this, $key, $value, $this->attributes); if ($caster instanceof CastsInboundAttributes || ! is_object($value) || $objectCachingDisabled) { unset($this->classCastCache[$key]); } else { $this->classCastCache[$key] = $value; } return $value; } } /** * Cast the given attribute to an enum. * * @param string $key * @param mixed $value * @return mixed */ protected function getEnumCastableAttributeValue($key, $value) { if (is_null($value)) { return; } $castType = $this->getCasts()[$key]; if ($value instanceof $castType) { return $value; } return $this->getEnumCaseFromValue($castType, $value); } /** * Get the type of cast for a model attribute. * * @param string $key * @return string */ protected function getCastType($key) { $castType = $this->getCasts()[$key]; if (isset(static::$castTypeCache[$castType])) { return static::$castTypeCache[$castType]; } if ($this->isCustomDateTimeCast($castType)) { $convertedCastType = 'custom_datetime'; } elseif ($this->isImmutableCustomDateTimeCast($castType)) { $convertedCastType = 'immutable_custom_datetime'; } elseif ($this->isDecimalCast($castType)) { $convertedCastType = 'decimal'; } elseif (class_exists($castType)) { $convertedCastType = $castType; } else { $convertedCastType = trim(strtolower($castType)); } return static::$castTypeCache[$castType] = $convertedCastType; } /** * Increment or decrement the given attribute using the custom cast class. * * @param string $method * @param string $key * @param mixed $value * @return mixed */ protected function deviateClassCastableAttribute($method, $key, $value) { return $this->resolveCasterClass($key)->{$method}( $this, $key, $value, $this->attributes ); } /** * Serialize the given attribute using the custom cast class. * * @param string $key * @param mixed $value * @return mixed */ protected function serializeClassCastableAttribute($key, $value) { return $this->resolveCasterClass($key)->serialize( $this, $key, $value, $this->attributes ); } /** * Determine if the cast type is a custom date time cast. * * @param string $cast * @return bool */ protected function isCustomDateTimeCast($cast) { return str_starts_with($cast, 'date:') || str_starts_with($cast, 'datetime:'); } /** * Determine if the cast type is an immutable custom date time cast. * * @param string $cast * @return bool */ protected function isImmutableCustomDateTimeCast($cast) { return str_starts_with($cast, 'immutable_date:') || str_starts_with($cast, 'immutable_datetime:'); } /** * Determine if the cast type is a decimal cast. * * @param string $cast * @return bool */ protected function isDecimalCast($cast) { return str_starts_with($cast, 'decimal:'); } /** * Set a given attribute on the model. * * @param string $key * @param mixed $value * @return mixed */ public function setAttribute($key, $value) { // First we will check for the presence of a mutator for the set operation // which simply lets the developers tweak the attribute as it is set on // this model, such as "json_encoding" a listing of data for storage. if ($this->hasSetMutator($key)) { return $this->setMutatedAttributeValue($key, $value); } elseif ($this->hasAttributeSetMutator($key)) { return $this->setAttributeMarkedMutatedAttributeValue($key, $value); } // If an attribute is listed as a "date", we'll convert it from a DateTime // instance into a form proper for storage on the database tables using // the connection grammar's date format. We will auto set the values. elseif (! is_null($value) && $this->isDateAttribute($key)) { $value = $this->fromDateTime($value); } if ($this->isEnumCastable($key)) { $this->setEnumCastableAttribute($key, $value); return $this; } if ($this->isClassCastable($key)) { $this->setClassCastableAttribute($key, $value); return $this; } if (! is_null($value) && $this->isJsonCastable($key)) { $value = $this->castAttributeAsJson($key, $value); } // If this attribute contains a JSON ->, we'll set the proper value in the // attribute's underlying array. This takes care of properly nesting an // attribute in the array's value in the case of deeply nested items. if (str_contains($key, '->')) { return $this->fillJsonAttribute($key, $value); } if (! is_null($value) && $this->isEncryptedCastable($key)) { $value = $this->castAttributeAsEncryptedString($key, $value); } if (! is_null($value) && $this->hasCast($key, 'hashed')) { $value = $this->castAttributeAsHashedString($key, $value); } $this->attributes[$key] = $value; return $this; } /** * Determine if a set mutator exists for an attribute. * * @param string $key * @return bool */ public function hasSetMutator($key) { return method_exists($this, 'set'.Str::studly($key).'Attribute'); } /** * Determine if an "Attribute" return type marked set mutator exists for an attribute. * * @param string $key * @return bool */ public function hasAttributeSetMutator($key) { $class = get_class($this); if (isset(static::$setAttributeMutatorCache[$class][$key])) { return static::$setAttributeMutatorCache[$class][$key]; } if (! method_exists($this, $method = Str::camel($key))) { return static::$setAttributeMutatorCache[$class][$key] = false; } $returnType = (new ReflectionMethod($this, $method))->getReturnType(); return static::$setAttributeMutatorCache[$class][$key] = $returnType instanceof ReflectionNamedType && $returnType->getName() === Attribute::class && is_callable($this->{$method}()->set); } /** * Set the value of an attribute using its mutator. * * @param string $key * @param mixed $value * @return mixed */ protected function setMutatedAttributeValue($key, $value) { return $this->{'set'.Str::studly($key).'Attribute'}($value); } /** * Set the value of a "Attribute" return type marked attribute using its mutator. * * @param string $key * @param mixed $value * @return mixed */ protected function setAttributeMarkedMutatedAttributeValue($key, $value) { $attribute = $this->{Str::camel($key)}(); $callback = $attribute->set ?: function ($value) use ($key) { $this->attributes[$key] = $value; }; $this->attributes = array_merge( $this->attributes, $this->normalizeCastClassResponse( $key, $callback($value, $this->attributes) ) ); if ($attribute->withCaching || (is_object($value) && $attribute->withObjectCaching)) { $this->attributeCastCache[$key] = $value; } else { unset($this->attributeCastCache[$key]); } return $this; } /** * Determine if the given attribute is a date or date castable. * * @param string $key * @return bool */ protected function isDateAttribute($key) { return in_array($key, $this->getDates(), true) || $this->isDateCastable($key); } /** * Set a given JSON attribute on the model. * * @param string $key * @param mixed $value * @return $this */ public function fillJsonAttribute($key, $value) { [$key, $path] = explode('->', $key, 2); $value = $this->asJson($this->getArrayAttributeWithValue( $path, $key, $value )); $this->attributes[$key] = $this->isEncryptedCastable($key) ? $this->castAttributeAsEncryptedString($key, $value) : $value; if ($this->isClassCastable($key)) { unset($this->classCastCache[$key]); } return $this; } /** * Set the value of a class castable attribute. * * @param string $key * @param mixed $value * @return void */ protected function setClassCastableAttribute($key, $value) { $caster = $this->resolveCasterClass($key); $this->attributes = array_replace( $this->attributes, $this->normalizeCastClassResponse($key, $caster->set( $this, $key, $value, $this->attributes )) ); if ($caster instanceof CastsInboundAttributes || ! is_object($value) || ($caster->withoutObjectCaching ?? false)) { unset($this->classCastCache[$key]); } else { $this->classCastCache[$key] = $value; } } /** * Set the value of an enum castable attribute. * * @param string $key * @param \UnitEnum|string|int $value * @return void */ protected function setEnumCastableAttribute($key, $value) { $enumClass = $this->getCasts()[$key]; if (! isset($value)) { $this->attributes[$key] = null; } elseif (is_object($value)) { $this->attributes[$key] = $this->getStorableEnumValue($value); } else { $this->attributes[$key] = $this->getStorableEnumValue( $this->getEnumCaseFromValue($enumClass, $value) ); } } /** * Get an enum case instance from a given class and value. * * @param string $enumClass * @param string|int $value * @return \UnitEnum|\BackedEnum */ protected function getEnumCaseFromValue($enumClass, $value) { return is_subclass_of($enumClass, BackedEnum::class) ? $enumClass::from($value) : constant($enumClass.'::'.$value); } /** * Get the storable value from the given enum. * * @param \UnitEnum|\BackedEnum $value * @return string|int */ protected function getStorableEnumValue($value) { return $value instanceof BackedEnum ? $value->value : $value->name; } /** * Get an array attribute with the given key and value set. * * @param string $path * @param string $key * @param mixed $value * @return $this */ protected function getArrayAttributeWithValue($path, $key, $value) { return tap($this->getArrayAttributeByKey($key), function (&$array) use ($path, $value) { Arr::set($array, str_replace('->', '.', $path), $value); }); } /** * Get an array attribute or return an empty array if it is not set. * * @param string $key * @return array */ protected function getArrayAttributeByKey($key) { if (! isset($this->attributes[$key])) { return []; } return $this->fromJson( $this->isEncryptedCastable($key) ? $this->fromEncryptedString($this->attributes[$key]) : $this->attributes[$key] ); } /** * Cast the given attribute to JSON. * * @param string $key * @param mixed $value * @return string */ protected function castAttributeAsJson($key, $value) { $value = $this->asJson($value); if ($value === false) { throw JsonEncodingException::forAttribute( $this, $key, json_last_error_msg() ); } return $value; } /** * Encode the given value as JSON. * * @param mixed $value * @return string */ protected function asJson($value) { return Json::encode($value); } /** * Decode the given JSON back into an array or object. * * @param string $value * @param bool $asObject * @return mixed */ public function fromJson($value, $asObject = false) { return Json::decode($value ?? '', ! $asObject); } /** * Decrypt the given encrypted string. * * @param string $value * @return mixed */ public function fromEncryptedString($value) { return (static::$encrypter ?? Crypt::getFacadeRoot())->decrypt($value, false); } /** * Cast the given attribute to an encrypted string. * * @param string $key * @param mixed $value * @return string */ protected function castAttributeAsEncryptedString($key, $value) { return (static::$encrypter ?? Crypt::getFacadeRoot())->encrypt($value, false); } /** * Set the encrypter instance that will be used to encrypt attributes. * * @param \Illuminate\Contracts\Encryption\Encrypter|null $encrypter * @return void */ public static function encryptUsing($encrypter) { static::$encrypter = $encrypter; } /** * Cast the given attribute to a hashed string. * * @param string $key * @param mixed $value * @return string */ protected function castAttributeAsHashedString($key, $value) { if ($value === null) { return null; } if (! Hash::isHashed($value)) { return Hash::make($value); } if (! Hash::verifyConfiguration($value)) { throw new RuntimeException("Could not verify the hashed value's configuration."); } return $value; } /** * Decode the given float. * * @param mixed $value * @return mixed */ public function fromFloat($value) { return match ((string) $value) { 'Infinity' => INF, '-Infinity' => -INF, 'NaN' => NAN, default => (float) $value, }; } /** * Return a decimal as string. * * @param float|string $value * @param int $decimals * @return string */ protected function asDecimal($value, $decimals) { try { return (string) BigDecimal::of($value)->toScale($decimals, RoundingMode::HALF_UP); } catch (BrickMathException $e) { throw new MathException('Unable to cast value to a decimal.', previous: $e); } } /** * Return a timestamp as DateTime object with time set to 00:00:00. * * @param mixed $value * @return \Illuminate\Support\Carbon */ protected function asDate($value) { return $this->asDateTime($value)->startOfDay(); } /** * Return a timestamp as DateTime object. * * @param mixed $value * @return \Illuminate\Support\Carbon */ protected function asDateTime($value) { // If this value is already a Carbon instance, we shall just return it as is. // This prevents us having to re-instantiate a Carbon instance when we know // it already is one, which wouldn't be fulfilled by the DateTime check. if ($value instanceof CarbonInterface) { return Date::instance($value); } // If the value is already a DateTime instance, we will just skip the rest of // these checks since they will be a waste of time, and hinder performance // when checking the field. We will just return the DateTime right away. if ($value instanceof DateTimeInterface) { return Date::parse( $value->format('Y-m-d H:i:s.u'), $value->getTimezone() ); } // If this value is an integer, we will assume it is a UNIX timestamp's value // and format a Carbon object from this timestamp. This allows flexibility // when defining your date fields as they might be UNIX timestamps here. if (is_numeric($value)) { return Date::createFromTimestamp($value); } // If the value is in simply year, month, day format, we will instantiate the // Carbon instances from that format. Again, this provides for simple date // fields on the database, while still supporting Carbonized conversion. if ($this->isStandardDateFormat($value)) { return Date::instance(Carbon::createFromFormat('Y-m-d', $value)->startOfDay()); } $format = $this->getDateFormat(); // 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. try { $date = Date::createFromFormat($format, $value); } catch (InvalidArgumentException) { $date = false; } return $date ?: Date::parse($value); } /** * Determine if the given value is a standard date format. * * @param string $value * @return bool */ protected function isStandardDateFormat($value) { return preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value); } /** * Convert a DateTime to a storable string. * * @param mixed $value * @return string|null */ public function fromDateTime($value) { return empty($value) ? $value : $this->asDateTime($value)->format( $this->getDateFormat() ); } /** * Return a timestamp as unix timestamp. * * @param mixed $value * @return int */ protected function asTimestamp($value) { return $this->asDateTime($value)->getTimestamp(); } /** * Prepare a date for array / JSON serialization. * * @param \DateTimeInterface $date * @return string */ protected function serializeDate(DateTimeInterface $date) { return $date instanceof DateTimeImmutable ? CarbonImmutable::instance($date)->toJSON() : Carbon::instance($date)->toJSON(); } /** * Get the attributes that should be converted to dates. * * @return array */ public function getDates() { return $this->usesTimestamps() ? [ $this->getCreatedAtColumn(), $this->getUpdatedAtColumn(), ] : []; } /** * Get the format for database stored dates. * * @return string */ public function getDateFormat() { return $this->dateFormat ?: $this->getConnection()->getQueryGrammar()->getDateFormat(); } /** * Set the date format used by the model. * * @param string $format * @return $this */ public function setDateFormat($format) { $this->dateFormat = $format; return $this; } /** * Determine whether an attribute should be cast to a native type. * * @param string $key * @param array|string|null $types * @return bool */ public function hasCast($key, $types = null) { if (array_key_exists($key, $this->getCasts())) { return $types ? in_array($this->getCastType($key), (array) $types, true) : true; } return false; } /** * Get the casts array. * * @return array */ public function getCasts() { if ($this->getIncrementing()) { return array_merge([$this->getKeyName() => $this->getKeyType()], $this->casts); } return $this->casts; } /** * Determine whether a value is Date / DateTime castable for inbound manipulation. * * @param string $key * @return bool */ protected function isDateCastable($key) { return $this->hasCast($key, ['date', 'datetime', 'immutable_date', 'immutable_datetime']); } /** * Determine whether a value is Date / DateTime custom-castable for inbound manipulation. * * @param string $key * @return bool */ protected function isDateCastableWithCustomFormat($key) { return $this->hasCast($key, ['custom_datetime', 'immutable_custom_datetime']); } /** * Determine whether a value is JSON castable for inbound manipulation. * * @param string $key * @return bool */ protected function isJsonCastable($key) { return $this->hasCast($key, ['array', 'json', 'object', 'collection', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object']); } /** * Determine whether a value is an encrypted castable for inbound manipulation. * * @param string $key * @return bool */ protected function isEncryptedCastable($key) { return $this->hasCast($key, ['encrypted', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object']); } /** * Determine if the given key is cast using a custom class. * * @param string $key * @return bool * * @throws \Illuminate\Database\Eloquent\InvalidCastException */ protected function isClassCastable($key) { $casts = $this->getCasts(); if (! array_key_exists($key, $casts)) { return false; } $castType = $this->parseCasterClass($casts[$key]); if (in_array($castType, static::$primitiveCastTypes)) { return false; } if (class_exists($castType)) { return true; } throw new InvalidCastException($this->getModel(), $key, $castType); } /** * Determine if the given key is cast using an enum. * * @param string $key * @return bool */ protected function isEnumCastable($key) { $casts = $this->getCasts(); if (! array_key_exists($key, $casts)) { return false; } $castType = $casts[$key]; if (in_array($castType, static::$primitiveCastTypes)) { return false; } return enum_exists($castType); } /** * Determine if the key is deviable using a custom class. * * @param string $key * @return bool * * @throws \Illuminate\Database\Eloquent\InvalidCastException */ protected function isClassDeviable($key) { if (! $this->isClassCastable($key)) { return false; } $castType = $this->resolveCasterClass($key); return method_exists($castType::class, 'increment') && method_exists($castType::class, 'decrement'); } /** * Determine if the key is serializable using a custom class. * * @param string $key * @return bool * * @throws \Illuminate\Database\Eloquent\InvalidCastException */ protected function isClassSerializable($key) { return ! $this->isEnumCastable($key) && $this->isClassCastable($key) && method_exists($this->resolveCasterClass($key), 'serialize'); } /** * Resolve the custom caster class for a given key. * * @param string $key * @return mixed */ protected function resolveCasterClass($key) { $castType = $this->getCasts()[$key]; $arguments = []; if (is_string($castType) && str_contains($castType, ':')) { $segments = explode(':', $castType, 2); $castType = $segments[0]; $arguments = explode(',', $segments[1]); } if (is_subclass_of($castType, Castable::class)) { $castType = $castType::castUsing($arguments); } if (is_object($castType)) { return $castType; } return new $castType(...$arguments); } /** * Parse the given caster class, removing any arguments. * * @param string $class * @return string */ protected function parseCasterClass($class) { return ! str_contains($class, ':') ? $class : explode(':', $class, 2)[0]; } /** * Merge the cast class and attribute cast attributes back into the model. * * @return void */ protected function mergeAttributesFromCachedCasts() { $this->mergeAttributesFromClassCasts(); $this->mergeAttributesFromAttributeCasts(); } /** * Merge the cast class attributes back into the model. * * @return void */ protected function mergeAttributesFromClassCasts() { foreach ($this->classCastCache as $key => $value) { $caster = $this->resolveCasterClass($key); $this->attributes = array_merge( $this->attributes, $caster instanceof CastsInboundAttributes ? [$key => $value] : $this->normalizeCastClassResponse($key, $caster->set($this, $key, $value, $this->attributes)) ); } } /** * Merge the cast class attributes back into the model. * * @return void */ protected function mergeAttributesFromAttributeCasts() { foreach ($this->attributeCastCache as $key => $value) { $attribute = $this->{Str::camel($key)}(); if ($attribute->get && ! $attribute->set) { continue; } $callback = $attribute->set ?: function ($value) use ($key) { $this->attributes[$key] = $value; }; $this->attributes = array_merge( $this->attributes, $this->normalizeCastClassResponse( $key, $callback($value, $this->attributes) ) ); } } /** * Normalize the response from a custom class caster. * * @param string $key * @param mixed $value * @return array */ protected function normalizeCastClassResponse($key, $value) { return is_array($value) ? $value : [$key => $value]; } /** * Get all of the current attributes on the model. * * @return array */ public function getAttributes() { $this->mergeAttributesFromCachedCasts(); return $this->attributes; } /** * Get all of the current attributes on the model for an insert operation. * * @return array */ protected function getAttributesForInsert() { return $this->getAttributes(); } /** * Set the array of model attributes. No checking is done. * * @param array $attributes * @param bool $sync * @return $this */ public function setRawAttributes(array $attributes, $sync = false) { $this->attributes = $attributes; if ($sync) { $this->syncOriginal(); } $this->classCastCache = []; $this->attributeCastCache = []; return $this; } /** * Get the model's original attribute values. * * @param string|null $key * @param mixed $default * @return mixed|array */ public function getOriginal($key = null, $default = null) { return (new static)->setRawAttributes( $this->original, $sync = true )->getOriginalWithoutRewindingModel($key, $default); } /** * Get the model's original attribute values. * * @param string|null $key * @param mixed $default * @return mixed|array */ protected function getOriginalWithoutRewindingModel($key = null, $default = null) { if ($key) { return $this->transformModelValue( $key, Arr::get($this->original, $key, $default) ); } return collect($this->original)->mapWithKeys(function ($value, $key) { return [$key => $this->transformModelValue($key, $value)]; })->all(); } /** * Get the model's raw original attribute values. * * @param string|null $key * @param mixed $default * @return mixed|array */ public function getRawOriginal($key = null, $default = null) { return Arr::get($this->original, $key, $default); } /** * Get a subset of the model's attributes. * * @param array|mixed $attributes * @return array */ public function only($attributes) { $results = []; foreach (is_array($attributes) ? $attributes : func_get_args() as $attribute) { $results[$attribute] = $this->getAttribute($attribute); } return $results; } /** * Sync the original attributes with the current. * * @return $this */ public function syncOriginal() { $this->original = $this->getAttributes(); return $this; } /** * Sync a single original attribute with its current value. * * @param string $attribute * @return $this */ public function syncOriginalAttribute($attribute) { return $this->syncOriginalAttributes($attribute); } /** * Sync multiple original attribute with their current values. * * @param array|string $attributes * @return $this */ public function syncOriginalAttributes($attributes) { $attributes = is_array($attributes) ? $attributes : func_get_args(); $modelAttributes = $this->getAttributes(); foreach ($attributes as $attribute) { $this->original[$attribute] = $modelAttributes[$attribute]; } return $this; } /** * Sync the changed attributes. * * @return $this */ public function syncChanges() { $this->changes = $this->getDirty(); return $this; } /** * Determine if the model or any of the given attribute(s) have been modified. * * @param array|string|null $attributes * @return bool */ public function isDirty($attributes = null) { return $this->hasChanges( $this->getDirty(), is_array($attributes) ? $attributes : func_get_args() ); } /** * Determine if the model or all the given attribute(s) have remained the same. * * @param array|string|null $attributes * @return bool */ public function isClean($attributes = null) { return ! $this->isDirty(...func_get_args()); } /** * Discard attribute changes and reset the attributes to their original state. * * @return $this */ public function discardChanges() { [$this->attributes, $this->changes] = [$this->original, []]; return $this; } /** * Determine if the model or any of the given attribute(s) were changed when the model was last saved. * * @param array|string|null $attributes * @return bool */ public function wasChanged($attributes = null) { return $this->hasChanges( $this->getChanges(), is_array($attributes) ? $attributes : func_get_args() ); } /** * Determine if any of the given attributes were changed when the model was last saved. * * @param array $changes * @param array|string|null $attributes * @return bool */ protected function hasChanges($changes, $attributes = null) { // If no specific attributes were provided, we will just see if the dirty array // already contains any attributes. If it does we will just return that this // count is greater than zero. Else, we need to check specific attributes. if (empty($attributes)) { return count($changes) > 0; } // Here we will spin through every attribute and see if this is in the array of // dirty attributes. If it is, we will return true and if we make it through // all of the attributes for the entire array we will return false at end. foreach (Arr::wrap($attributes) as $attribute) { if (array_key_exists($attribute, $changes)) { return true; } } return false; } /** * Get the attributes that have been changed since the last sync. * * @return array */ public function getDirty() { $dirty = []; foreach ($this->getAttributes() as $key => $value) { if (! $this->originalIsEquivalent($key)) { $dirty[$key] = $value; } } return $dirty; } /** * Get the attributes that were changed when the model was last saved. * * @return array */ public function getChanges() { return $this->changes; } /** * Determine if the new and old values for a given key are equivalent. * * @param string $key * @return bool */ public function originalIsEquivalent($key) { if (! array_key_exists($key, $this->original)) { return false; } $attribute = Arr::get($this->attributes, $key); $original = Arr::get($this->original, $key); if ($attribute === $original) { return true; } elseif (is_null($attribute)) { return false; } elseif ($this->isDateAttribute($key) || $this->isDateCastableWithCustomFormat($key)) { return $this->fromDateTime($attribute) === $this->fromDateTime($original); } elseif ($this->hasCast($key, ['object', 'collection'])) { return $this->fromJson($attribute) === $this->fromJson($original); } elseif ($this->hasCast($key, ['real', 'float', 'double'])) { if ($original === null) { return false; } return abs($this->castAttribute($key, $attribute) - $this->castAttribute($key, $original)) < PHP_FLOAT_EPSILON * 4; } elseif ($this->hasCast($key, static::$primitiveCastTypes)) { return $this->castAttribute($key, $attribute) === $this->castAttribute($key, $original); } elseif ($this->isClassCastable($key) && Str::startsWith($this->getCasts()[$key], [AsArrayObject::class, AsCollection::class])) { return $this->fromJson($attribute) === $this->fromJson($original); } elseif ($this->isClassCastable($key) && Str::startsWith($this->getCasts()[$key], [AsEnumArrayObject::class, AsEnumCollection::class])) { return $this->fromJson($attribute) === $this->fromJson($original); } elseif ($this->isClassCastable($key) && $original !== null && Str::startsWith($this->getCasts()[$key], [AsEncryptedArrayObject::class, AsEncryptedCollection::class])) { return $this->fromEncryptedString($attribute) === $this->fromEncryptedString($original); } return is_numeric($attribute) && is_numeric($original) && strcmp((string) $attribute, (string) $original) === 0; } /** * Transform a raw model value using mutators, casts, etc. * * @param string $key * @param mixed $value * @return mixed */ protected function transformModelValue($key, $value) { // If the attribute has a get mutator, we will call that then return what // it returns as the value, which is useful for transforming values on // retrieval from the model to a form that is more useful for usage. if ($this->hasGetMutator($key)) { return $this->mutateAttribute($key, $value); } elseif ($this->hasAttributeGetMutator($key)) { return $this->mutateAttributeMarkedAttribute($key, $value); } // If the attribute exists within the cast array, we will convert it to // an appropriate native PHP type dependent upon the associated value // given with the key in the pair. Dayle made this comment line up. if ($this->hasCast($key)) { return $this->castAttribute($key, $value); } // If the attribute is listed as a date, we will convert it to a DateTime // instance on retrieval, which makes it quite convenient to work with // date fields without having to create a mutator for each property. if ($value !== null && \in_array($key, $this->getDates(), false)) { return $this->asDateTime($value); } return $value; } /** * Append attributes to query when building a query. * * @param array|string $attributes * @return $this */ public function append($attributes) { $this->appends = array_values(array_unique( array_merge($this->appends, is_string($attributes) ? func_get_args() : $attributes) )); return $this; } /** * Get the accessors that are being appended to model arrays. * * @return array */ public function getAppends() { return $this->appends; } /** * Set the accessors to append to model arrays. * * @param array $appends * @return $this */ public function setAppends(array $appends) { $this->appends = $appends; return $this; } /** * Return whether the accessor attribute has been appended. * * @param string $attribute * @return bool */ public function hasAppended($attribute) { return in_array($attribute, $this->appends); } /** * Get the mutated attributes for a given instance. * * @return array */ public function getMutatedAttributes() { if (! isset(static::$mutatorCache[static::class])) { static::cacheMutatedAttributes($this); } return static::$mutatorCache[static::class]; } /** * Extract and cache all the mutated attributes of a class. * * @param object|string $classOrInstance * @return void */ public static function cacheMutatedAttributes($classOrInstance) { $reflection = new ReflectionClass($classOrInstance); $class = $reflection->getName(); static::$getAttributeMutatorCache[$class] = collect($attributeMutatorMethods = static::getAttributeMarkedMutatorMethods($classOrInstance)) ->mapWithKeys(function ($match) { return [lcfirst(static::$snakeAttributes ? Str::snake($match) : $match) => true]; })->all(); static::$mutatorCache[$class] = collect(static::getMutatorMethods($class)) ->merge($attributeMutatorMethods) ->map(function ($match) { return lcfirst(static::$snakeAttributes ? Str::snake($match) : $match); })->all(); } /** * Get all of the attribute mutator methods. * * @param mixed $class * @return array */ protected static function getMutatorMethods($class) { preg_match_all('/(?<=^|;)get([^;]+?)Attribute(;|$)/', implode(';', get_class_methods($class)), $matches); return $matches[1]; } /** * Get all of the "Attribute" return typed attribute mutator methods. * * @param mixed $class * @return array */ protected static function getAttributeMarkedMutatorMethods($class) { $instance = is_object($class) ? $class : new $class; return collect((new ReflectionClass($instance))->getMethods())->filter(function ($method) use ($instance) { $returnType = $method->getReturnType(); if ($returnType instanceof ReflectionNamedType && $returnType->getName() === Attribute::class) { if (is_callable($method->invoke($instance)->get)) { return true; } } return false; })->map->name->values()->all(); } } src/Illuminate/Database/Eloquent/Collection.php 0000644 00000053025 15107327037 0015554 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use Illuminate\Contracts\Queue\QueueableCollection; use Illuminate\Contracts\Queue\QueueableEntity; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; use Illuminate\Support\Arr; use Illuminate\Support\Collection as BaseCollection; use LogicException; /** * @template TKey of array-key * @template TModel of \Illuminate\Database\Eloquent\Model * * @extends \Illuminate\Support\Collection<TKey, TModel> */ class Collection extends BaseCollection implements QueueableCollection { use InteractsWithDictionary; /** * Find a model in the collection by key. * * @template TFindDefault * * @param mixed $key * @param TFindDefault $default * @return static<TKey, TModel>|TModel|TFindDefault */ public function find($key, $default = null) { if ($key instanceof Model) { $key = $key->getKey(); } if ($key instanceof Arrayable) { $key = $key->toArray(); } if (is_array($key)) { if ($this->isEmpty()) { return new static; } return $this->whereIn($this->first()->getKeyName(), $key); } return Arr::first($this->items, fn ($model) => $model->getKey() == $key, $default); } /** * Load a set of relationships onto the collection. * * @param array<array-key, (callable(\Illuminate\Database\Eloquent\Builder): mixed)|string>|string $relations * @return $this */ public function load($relations) { if ($this->isNotEmpty()) { if (is_string($relations)) { $relations = func_get_args(); } $query = $this->first()->newQueryWithoutRelationships()->with($relations); $this->items = $query->eagerLoadRelations($this->items); } return $this; } /** * Load a set of aggregations over relationship's column onto the collection. * * @param array<array-key, (callable(\Illuminate\Database\Eloquent\Builder): mixed)|string>|string $relations * @param string $column * @param string|null $function * @return $this */ public function loadAggregate($relations, $column, $function = null) { if ($this->isEmpty()) { return $this; } $models = $this->first()->newModelQuery() ->whereKey($this->modelKeys()) ->select($this->first()->getKeyName()) ->withAggregate($relations, $column, $function) ->get() ->keyBy($this->first()->getKeyName()); $attributes = Arr::except( array_keys($models->first()->getAttributes()), $models->first()->getKeyName() ); $this->each(function ($model) use ($models, $attributes) { $extraAttributes = Arr::only($models->get($model->getKey())->getAttributes(), $attributes); $model->forceFill($extraAttributes) ->syncOriginalAttributes($attributes) ->mergeCasts($models->get($model->getKey())->getCasts()); }); return $this; } /** * Load a set of relationship counts onto the collection. * * @param array<array-key, (callable(\Illuminate\Database\Eloquent\Builder): mixed)|string>|string $relations * @return $this */ public function loadCount($relations) { return $this->loadAggregate($relations, '*', 'count'); } /** * Load a set of relationship's max column values onto the collection. * * @param array<array-key, (callable(\Illuminate\Database\Eloquent\Builder): mixed)|string>|string $relations * @param string $column * @return $this */ public function loadMax($relations, $column) { return $this->loadAggregate($relations, $column, 'max'); } /** * Load a set of relationship's min column values onto the collection. * * @param array<array-key, (callable(\Illuminate\Database\Eloquent\Builder): mixed)|string>|string $relations * @param string $column * @return $this */ public function loadMin($relations, $column) { return $this->loadAggregate($relations, $column, 'min'); } /** * Load a set of relationship's column summations onto the collection. * * @param array<array-key, (callable(\Illuminate\Database\Eloquent\Builder): mixed)|string>|string $relations * @param string $column * @return $this */ public function loadSum($relations, $column) { return $this->loadAggregate($relations, $column, 'sum'); } /** * Load a set of relationship's average column values onto the collection. * * @param array<array-key, (callable(\Illuminate\Database\Eloquent\Builder): mixed)|string>|string $relations * @param string $column * @return $this */ public function loadAvg($relations, $column) { return $this->loadAggregate($relations, $column, 'avg'); } /** * Load a set of related existences onto the collection. * * @param array<array-key, (callable(\Illuminate\Database\Eloquent\Builder): mixed)|string>|string $relations * @return $this */ public function loadExists($relations) { return $this->loadAggregate($relations, '*', 'exists'); } /** * Load a set of relationships onto the collection if they are not already eager loaded. * * @param array<array-key, (callable(\Illuminate\Database\Eloquent\Builder): mixed)|string>|string $relations * @return $this */ public function loadMissing($relations) { if (is_string($relations)) { $relations = func_get_args(); } foreach ($relations as $key => $value) { if (is_numeric($key)) { $key = $value; } $segments = explode('.', explode(':', $key)[0]); if (str_contains($key, ':')) { $segments[count($segments) - 1] .= ':'.explode(':', $key)[1]; } $path = []; foreach ($segments as $segment) { $path[] = [$segment => $segment]; } if (is_callable($value)) { $path[count($segments) - 1][end($segments)] = $value; } $this->loadMissingRelation($this, $path); } return $this; } /** * Load a relationship path if it is not already eager loaded. * * @param \Illuminate\Database\Eloquent\Collection $models * @param array $path * @return void */ protected function loadMissingRelation(self $models, array $path) { $relation = array_shift($path); $name = explode(':', key($relation))[0]; if (is_string(reset($relation))) { $relation = reset($relation); } $models->filter(fn ($model) => ! is_null($model) && ! $model->relationLoaded($name))->load($relation); if (empty($path)) { return; } $models = $models->pluck($name)->whereNotNull(); if ($models->first() instanceof BaseCollection) { $models = $models->collapse(); } $this->loadMissingRelation(new static($models), $path); } /** * Load a set of relationships onto the mixed relationship collection. * * @param string $relation * @param array<array-key, (callable(\Illuminate\Database\Eloquent\Builder): mixed)|string> $relations * @return $this */ public function loadMorph($relation, $relations) { $this->pluck($relation) ->filter() ->groupBy(fn ($model) => get_class($model)) ->each(fn ($models, $className) => static::make($models)->load($relations[$className] ?? [])); return $this; } /** * Load a set of relationship counts onto the mixed relationship collection. * * @param string $relation * @param array<array-key, (callable(\Illuminate\Database\Eloquent\Builder): mixed)|string> $relations * @return $this */ public function loadMorphCount($relation, $relations) { $this->pluck($relation) ->filter() ->groupBy(fn ($model) => get_class($model)) ->each(fn ($models, $className) => static::make($models)->loadCount($relations[$className] ?? [])); return $this; } /** * Determine if a key exists in the collection. * * @param (callable(TModel, TKey): bool)|TModel|string|int $key * @param mixed $operator * @param mixed $value * @return bool */ public function contains($key, $operator = null, $value = null) { if (func_num_args() > 1 || $this->useAsCallable($key)) { return parent::contains(...func_get_args()); } if ($key instanceof Model) { return parent::contains(fn ($model) => $model->is($key)); } return parent::contains(fn ($model) => $model->getKey() == $key); } /** * Get the array of primary keys. * * @return array<int, array-key> */ public function modelKeys() { return array_map(fn ($model) => $model->getKey(), $this->items); } /** * Merge the collection with the given items. * * @param iterable<array-key, TModel> $items * @return static */ public function merge($items) { $dictionary = $this->getDictionary(); foreach ($items as $item) { $dictionary[$this->getDictionaryKey($item->getKey())] = $item; } return new static(array_values($dictionary)); } /** * Run a map over each of the items. * * @template TMapValue * * @param callable(TModel, TKey): TMapValue $callback * @return \Illuminate\Support\Collection<TKey, TMapValue>|static<TKey, TMapValue> */ public function map(callable $callback) { $result = parent::map($callback); return $result->contains(fn ($item) => ! $item instanceof Model) ? $result->toBase() : $result; } /** * Run an associative map over each of the items. * * The callback should return an associative array with a single key / value pair. * * @template TMapWithKeysKey of array-key * @template TMapWithKeysValue * * @param callable(TModel, TKey): array<TMapWithKeysKey, TMapWithKeysValue> $callback * @return \Illuminate\Support\Collection<TMapWithKeysKey, TMapWithKeysValue>|static<TMapWithKeysKey, TMapWithKeysValue> */ public function mapWithKeys(callable $callback) { $result = parent::mapWithKeys($callback); return $result->contains(fn ($item) => ! $item instanceof Model) ? $result->toBase() : $result; } /** * Reload a fresh model instance from the database for all the entities. * * @param array<array-key, string>|string $with * @return static */ public function fresh($with = []) { if ($this->isEmpty()) { return new static; } $model = $this->first(); $freshModels = $model->newQueryWithoutScopes() ->with(is_string($with) ? func_get_args() : $with) ->whereIn($model->getKeyName(), $this->modelKeys()) ->get() ->getDictionary(); return $this->filter(fn ($model) => $model->exists && isset($freshModels[$model->getKey()])) ->map(fn ($model) => $freshModels[$model->getKey()]); } /** * Diff the collection with the given items. * * @param iterable<array-key, TModel> $items * @return static */ public function diff($items) { $diff = new static; $dictionary = $this->getDictionary($items); foreach ($this->items as $item) { if (! isset($dictionary[$this->getDictionaryKey($item->getKey())])) { $diff->add($item); } } return $diff; } /** * Intersect the collection with the given items. * * @param iterable<array-key, TModel> $items * @return static */ public function intersect($items) { $intersect = new static; if (empty($items)) { return $intersect; } $dictionary = $this->getDictionary($items); foreach ($this->items as $item) { if (isset($dictionary[$this->getDictionaryKey($item->getKey())])) { $intersect->add($item); } } return $intersect; } /** * Return only unique items from the collection. * * @param (callable(TModel, TKey): mixed)|string|null $key * @param bool $strict * @return static<int, TModel> */ public function unique($key = null, $strict = false) { if (! is_null($key)) { return parent::unique($key, $strict); } return new static(array_values($this->getDictionary())); } /** * Returns only the models from the collection with the specified keys. * * @param array<array-key, mixed>|null $keys * @return static<int, TModel> */ public function only($keys) { if (is_null($keys)) { return new static($this->items); } $dictionary = Arr::only($this->getDictionary(), array_map($this->getDictionaryKey(...), (array) $keys)); return new static(array_values($dictionary)); } /** * Returns all models in the collection except the models with specified keys. * * @param array<array-key, mixed>|null $keys * @return static<int, TModel> */ public function except($keys) { if (is_null($keys)) { return new static($this->items); } $dictionary = Arr::except($this->getDictionary(), array_map($this->getDictionaryKey(...), (array) $keys)); return new static(array_values($dictionary)); } /** * Make the given, typically visible, attributes hidden across the entire collection. * * @param array<array-key, string>|string $attributes * @return $this */ public function makeHidden($attributes) { return $this->each->makeHidden($attributes); } /** * Make the given, typically hidden, attributes visible across the entire collection. * * @param array<array-key, string>|string $attributes * @return $this */ public function makeVisible($attributes) { return $this->each->makeVisible($attributes); } /** * Set the visible attributes across the entire collection. * * @param array<int, string> $visible * @return $this */ public function setVisible($visible) { return $this->each->setVisible($visible); } /** * Set the hidden attributes across the entire collection. * * @param array<int, string> $hidden * @return $this */ public function setHidden($hidden) { return $this->each->setHidden($hidden); } /** * Append an attribute across the entire collection. * * @param array<array-key, string>|string $attributes * @return $this */ public function append($attributes) { return $this->each->append($attributes); } /** * Get a dictionary keyed by primary keys. * * @param iterable<array-key, TModel>|null $items * @return array<array-key, TModel> */ public function getDictionary($items = null) { $items = is_null($items) ? $this->items : $items; $dictionary = []; foreach ($items as $value) { $dictionary[$this->getDictionaryKey($value->getKey())] = $value; } return $dictionary; } /** * The following methods are intercepted to always return base collections. */ /** * Count the number of items in the collection by a field or using a callback. * * @param (callable(TModel, TKey): array-key)|string|null $countBy * @return \Illuminate\Support\Collection<array-key, int> */ public function countBy($countBy = null) { return $this->toBase()->countBy($countBy); } /** * Collapse the collection of items into a single array. * * @return \Illuminate\Support\Collection<int, mixed> */ public function collapse() { return $this->toBase()->collapse(); } /** * Get a flattened array of the items in the collection. * * @param int $depth * @return \Illuminate\Support\Collection<int, mixed> */ public function flatten($depth = INF) { return $this->toBase()->flatten($depth); } /** * Flip the items in the collection. * * @return \Illuminate\Support\Collection<TModel, TKey> */ public function flip() { return $this->toBase()->flip(); } /** * Get the keys of the collection items. * * @return \Illuminate\Support\Collection<int, TKey> */ public function keys() { return $this->toBase()->keys(); } /** * Pad collection to the specified length with a value. * * @template TPadValue * * @param int $size * @param TPadValue $value * @return \Illuminate\Support\Collection<int, TModel|TPadValue> */ public function pad($size, $value) { return $this->toBase()->pad($size, $value); } /** * Get an array with the values of a given key. * * @param string|array<array-key, string> $value * @param string|null $key * @return \Illuminate\Support\Collection<array-key, mixed> */ public function pluck($value, $key = null) { return $this->toBase()->pluck($value, $key); } /** * Zip the collection together with one or more arrays. * * @template TZipValue * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TZipValue>|iterable<array-key, TZipValue> ...$items * @return \Illuminate\Support\Collection<int, \Illuminate\Support\Collection<int, TModel|TZipValue>> */ public function zip($items) { return $this->toBase()->zip(...func_get_args()); } /** * Get the comparison function to detect duplicates. * * @param bool $strict * @return callable(TModel, TModel): bool */ protected function duplicateComparator($strict) { return fn ($a, $b) => $a->is($b); } /** * Get the type of the entities being queued. * * @return string|null * * @throws \LogicException */ public function getQueueableClass() { if ($this->isEmpty()) { return; } $class = $this->getQueueableModelClass($this->first()); $this->each(function ($model) use ($class) { if ($this->getQueueableModelClass($model) !== $class) { throw new LogicException('Queueing collections with multiple model types is not supported.'); } }); return $class; } /** * Get the queueable class name for the given model. * * @param \Illuminate\Database\Eloquent\Model $model * @return string */ protected function getQueueableModelClass($model) { return method_exists($model, 'getQueueableClassName') ? $model->getQueueableClassName() : get_class($model); } /** * Get the identifiers for all of the entities. * * @return array<int, mixed> */ public function getQueueableIds() { if ($this->isEmpty()) { return []; } return $this->first() instanceof QueueableEntity ? $this->map->getQueueableId()->all() : $this->modelKeys(); } /** * Get the relationships of the entities being queued. * * @return array<int, string> */ public function getQueueableRelations() { if ($this->isEmpty()) { return []; } $relations = $this->map->getQueueableRelations()->all(); if (count($relations) === 0 || $relations === [[]]) { return []; } elseif (count($relations) === 1) { return reset($relations); } else { return array_intersect(...array_values($relations)); } } /** * Get the connection of the entities being queued. * * @return string|null * * @throws \LogicException */ public function getQueueableConnection() { if ($this->isEmpty()) { return; } $connection = $this->first()->getConnectionName(); $this->each(function ($model) use ($connection) { if ($model->getConnectionName() !== $connection) { throw new LogicException('Queueing collections with multiple model connections is not supported.'); } }); return $connection; } /** * Get the Eloquent query builder from the collection. * * @return \Illuminate\Database\Eloquent\Builder * * @throws \LogicException */ public function toQuery() { $model = $this->first(); if (! $model) { throw new LogicException('Unable to create query for empty collection.'); } $class = get_class($model); if ($this->filter(fn ($model) => ! $model instanceof $class)->isNotEmpty()) { throw new LogicException('Unable to create query for collection with mixed types.'); } return $model->newModelQuery()->whereKey($this->modelKeys()); } } src/Illuminate/Database/Eloquent/Prunable.php 0000644 00000002704 15107327037 0015227 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use Illuminate\Database\Events\ModelsPruned; use LogicException; trait Prunable { /** * Prune all prunable models in the database. * * @param int $chunkSize * @return int */ public function pruneAll(int $chunkSize = 1000) { $total = 0; $this->prunable() ->when(in_array(SoftDeletes::class, class_uses_recursive(get_class($this))), function ($query) { $query->withTrashed(); })->chunkById($chunkSize, function ($models) use (&$total) { $models->each->prune(); $total += $models->count(); event(new ModelsPruned(static::class, $total)); }); return $total; } /** * Get the prunable model query. * * @return \Illuminate\Database\Eloquent\Builder */ public function prunable() { throw new LogicException('Please implement the prunable method on your model.'); } /** * Prune the model in the database. * * @return bool|null */ public function prune() { $this->pruning(); return in_array(SoftDeletes::class, class_uses_recursive(get_class($this))) ? $this->forceDelete() : $this->delete(); } /** * Prepare the model for pruning. * * @return void */ protected function pruning() { // } } src/Illuminate/Database/Eloquent/Builder.php 0000644 00000162726 15107327037 0015060 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use BadMethodCallException; use Closure; use Exception; use Illuminate\Contracts\Database\Eloquent\Builder as BuilderContract; use Illuminate\Contracts\Database\Query\Expression; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Concerns\BuildsQueries; use Illuminate\Database\Eloquent\Concerns\QueriesRelationships; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\RecordsNotFoundException; use Illuminate\Database\UniqueConstraintViolationException; use Illuminate\Pagination\Paginator; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Support\Traits\ForwardsCalls; use ReflectionClass; use ReflectionMethod; /** * @property-read HigherOrderBuilderProxy $orWhere * @property-read HigherOrderBuilderProxy $whereNot * @property-read HigherOrderBuilderProxy $orWhereNot * * @mixin \Illuminate\Database\Query\Builder */ class Builder implements BuilderContract { use BuildsQueries, ForwardsCalls, QueriesRelationships { BuildsQueries::sole as baseSole; } /** * The base query builder instance. * * @var \Illuminate\Database\Query\Builder */ protected $query; /** * The model being queried. * * @var \Illuminate\Database\Eloquent\Model */ protected $model; /** * The relationships that should be eager loaded. * * @var array */ protected $eagerLoad = []; /** * All of the globally registered builder macros. * * @var array */ protected static $macros = []; /** * All of the locally registered builder macros. * * @var array */ protected $localMacros = []; /** * A replacement for the typical delete function. * * @var \Closure */ protected $onDelete; /** * The properties that should be returned from query builder. * * @var string[] */ protected $propertyPassthru = [ 'from', ]; /** * The methods that should be returned from query builder. * * @var string[] */ protected $passthru = [ 'aggregate', 'average', 'avg', 'count', 'dd', 'ddrawsql', 'doesntexist', 'doesntexistor', 'dump', 'dumprawsql', 'exists', 'existsor', 'explain', 'getbindings', 'getconnection', 'getgrammar', 'implode', 'insert', 'insertgetid', 'insertorignore', 'insertusing', 'max', 'min', 'raw', 'rawvalue', 'sum', 'tosql', 'torawsql', ]; /** * Applied global scopes. * * @var array */ protected $scopes = []; /** * Removed global scopes. * * @var array */ protected $removedScopes = []; /** * Create a new Eloquent query builder instance. * * @param \Illuminate\Database\Query\Builder $query * @return void */ public function __construct(QueryBuilder $query) { $this->query = $query; } /** * Create and return an un-saved model instance. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Model|static */ public function make(array $attributes = []) { return $this->newModelInstance($attributes); } /** * Register a new global scope. * * @param string $identifier * @param \Illuminate\Database\Eloquent\Scope|\Closure $scope * @return $this */ public function withGlobalScope($identifier, $scope) { $this->scopes[$identifier] = $scope; if (method_exists($scope, 'extend')) { $scope->extend($this); } return $this; } /** * Remove a registered global scope. * * @param \Illuminate\Database\Eloquent\Scope|string $scope * @return $this */ public function withoutGlobalScope($scope) { if (! is_string($scope)) { $scope = get_class($scope); } unset($this->scopes[$scope]); $this->removedScopes[] = $scope; return $this; } /** * Remove all or passed registered global scopes. * * @param array|null $scopes * @return $this */ public function withoutGlobalScopes(array $scopes = null) { if (! is_array($scopes)) { $scopes = array_keys($this->scopes); } foreach ($scopes as $scope) { $this->withoutGlobalScope($scope); } return $this; } /** * Get an array of global scopes that were removed from the query. * * @return array */ public function removedScopes() { return $this->removedScopes; } /** * Add a where clause on the primary key to the query. * * @param mixed $id * @return $this */ public function whereKey($id) { if ($id instanceof Model) { $id = $id->getKey(); } if (is_array($id) || $id instanceof Arrayable) { if (in_array($this->model->getKeyType(), ['int', 'integer'])) { $this->query->whereIntegerInRaw($this->model->getQualifiedKeyName(), $id); } else { $this->query->whereIn($this->model->getQualifiedKeyName(), $id); } return $this; } if ($id !== null && $this->model->getKeyType() === 'string') { $id = (string) $id; } return $this->where($this->model->getQualifiedKeyName(), '=', $id); } /** * Add a where clause on the primary key to the query. * * @param mixed $id * @return $this */ public function whereKeyNot($id) { if ($id instanceof Model) { $id = $id->getKey(); } if (is_array($id) || $id instanceof Arrayable) { if (in_array($this->model->getKeyType(), ['int', 'integer'])) { $this->query->whereIntegerNotInRaw($this->model->getQualifiedKeyName(), $id); } else { $this->query->whereNotIn($this->model->getQualifiedKeyName(), $id); } return $this; } if ($id !== null && $this->model->getKeyType() === 'string') { $id = (string) $id; } return $this->where($this->model->getQualifiedKeyName(), '!=', $id); } /** * Add a basic where clause to the query. * * @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column * @param mixed $operator * @param mixed $value * @param string $boolean * @return $this */ public function where($column, $operator = null, $value = null, $boolean = 'and') { if ($column instanceof Closure && is_null($operator)) { $column($query = $this->model->newQueryWithoutRelationships()); $this->query->addNestedWhereQuery($query->getQuery(), $boolean); } else { $this->query->where(...func_get_args()); } return $this; } /** * Add a basic where clause to the query, and return the first result. * * @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column * @param mixed $operator * @param mixed $value * @param string $boolean * @return \Illuminate\Database\Eloquent\Model|static|null */ public function firstWhere($column, $operator = null, $value = null, $boolean = 'and') { return $this->where(...func_get_args())->first(); } /** * Add an "or where" clause to the query. * * @param \Closure|array|string|\Illuminate\Contracts\Database\Query\Expression $column * @param mixed $operator * @param mixed $value * @return $this */ public function orWhere($column, $operator = null, $value = null) { [$value, $operator] = $this->query->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); return $this->where($column, $operator, $value, 'or'); } /** * Add a basic "where not" clause to the query. * * @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column * @param mixed $operator * @param mixed $value * @param string $boolean * @return $this */ public function whereNot($column, $operator = null, $value = null, $boolean = 'and') { return $this->where($column, $operator, $value, $boolean.' not'); } /** * Add an "or where not" clause to the query. * * @param \Closure|array|string|\Illuminate\Contracts\Database\Query\Expression $column * @param mixed $operator * @param mixed $value * @return $this */ public function orWhereNot($column, $operator = null, $value = null) { return $this->whereNot($column, $operator, $value, 'or'); } /** * Add an "order by" clause for a timestamp to the query. * * @param string|\Illuminate\Contracts\Database\Query\Expression $column * @return $this */ public function latest($column = null) { if (is_null($column)) { $column = $this->model->getCreatedAtColumn() ?? 'created_at'; } $this->query->latest($column); return $this; } /** * Add an "order by" clause for a timestamp to the query. * * @param string|\Illuminate\Contracts\Database\Query\Expression $column * @return $this */ public function oldest($column = null) { if (is_null($column)) { $column = $this->model->getCreatedAtColumn() ?? 'created_at'; } $this->query->oldest($column); return $this; } /** * Create a collection of models from plain arrays. * * @param array $items * @return \Illuminate\Database\Eloquent\Collection */ public function hydrate(array $items) { $instance = $this->newModelInstance(); return $instance->newCollection(array_map(function ($item) use ($items, $instance) { $model = $instance->newFromBuilder($item); if (count($items) > 1) { $model->preventsLazyLoading = Model::preventsLazyLoading(); } return $model; }, $items)); } /** * Create a collection of models from a raw query. * * @param string $query * @param array $bindings * @return \Illuminate\Database\Eloquent\Collection */ public function fromQuery($query, $bindings = []) { return $this->hydrate( $this->query->getConnection()->select($query, $bindings) ); } /** * Find a model by its primary key. * * @param mixed $id * @param array|string $columns * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static[]|static|null */ public function find($id, $columns = ['*']) { if (is_array($id) || $id instanceof Arrayable) { return $this->findMany($id, $columns); } return $this->whereKey($id)->first($columns); } /** * Find multiple models by their primary keys. * * @param \Illuminate\Contracts\Support\Arrayable|array $ids * @param array|string $columns * @return \Illuminate\Database\Eloquent\Collection */ public function findMany($ids, $columns = ['*']) { $ids = $ids instanceof Arrayable ? $ids->toArray() : $ids; if (empty($ids)) { return $this->model->newCollection(); } return $this->whereKey($ids)->get($columns); } /** * Find a model by its primary key or throw an exception. * * @param mixed $id * @param array|string $columns * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static|static[] * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public function findOrFail($id, $columns = ['*']) { $result = $this->find($id, $columns); $id = $id instanceof Arrayable ? $id->toArray() : $id; if (is_array($id)) { if (count($result) !== count(array_unique($id))) { throw (new ModelNotFoundException)->setModel( get_class($this->model), array_diff($id, $result->modelKeys()) ); } return $result; } if (is_null($result)) { throw (new ModelNotFoundException)->setModel( get_class($this->model), $id ); } return $result; } /** * Find a model by its primary key or return fresh model instance. * * @param mixed $id * @param array|string $columns * @return \Illuminate\Database\Eloquent\Model|static */ public function findOrNew($id, $columns = ['*']) { if (! is_null($model = $this->find($id, $columns))) { return $model; } return $this->newModelInstance(); } /** * Find a model by its primary key or call a callback. * * @param mixed $id * @param \Closure|array|string $columns * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static[]|static|mixed */ public function findOr($id, $columns = ['*'], Closure $callback = null) { if ($columns instanceof Closure) { $callback = $columns; $columns = ['*']; } if (! is_null($model = $this->find($id, $columns))) { return $model; } return $callback(); } /** * Get the first record matching the attributes or instantiate it. * * @param array $attributes * @param array $values * @return \Illuminate\Database\Eloquent\Model|static */ public function firstOrNew(array $attributes = [], array $values = []) { if (! is_null($instance = $this->where($attributes)->first())) { return $instance; } return $this->newModelInstance(array_merge($attributes, $values)); } /** * Get the first record matching the attributes. If the record is not found, create it. * * @param array $attributes * @param array $values * @return \Illuminate\Database\Eloquent\Model|static */ public function firstOrCreate(array $attributes = [], array $values = []) { if (! is_null($instance = (clone $this)->where($attributes)->first())) { return $instance; } return $this->createOrFirst($attributes, $values); } /** * Attempt to create the record. If a unique constraint violation occurs, attempt to find the matching record. * * @param array $attributes * @param array $values * @return \Illuminate\Database\Eloquent\Model|static */ public function createOrFirst(array $attributes = [], array $values = []) { try { return $this->withSavepointIfNeeded(fn () => $this->create(array_merge($attributes, $values))); } catch (UniqueConstraintViolationException $e) { return $this->useWritePdo()->where($attributes)->first() ?? throw $e; } } /** * Create or update a record matching the attributes, and fill it with values. * * @param array $attributes * @param array $values * @return \Illuminate\Database\Eloquent\Model|static */ public function updateOrCreate(array $attributes, array $values = []) { return tap($this->firstOrCreate($attributes, $values), function ($instance) use ($values) { if (! $instance->wasRecentlyCreated) { $instance->fill($values)->save(); } }); } /** * Execute the query and get the first result or throw an exception. * * @param array|string $columns * @return \Illuminate\Database\Eloquent\Model|static * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public function firstOrFail($columns = ['*']) { if (! is_null($model = $this->first($columns))) { return $model; } throw (new ModelNotFoundException)->setModel(get_class($this->model)); } /** * Execute the query and get the first result or call a callback. * * @param \Closure|array|string $columns * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Model|static|mixed */ public function firstOr($columns = ['*'], Closure $callback = null) { if ($columns instanceof Closure) { $callback = $columns; $columns = ['*']; } if (! is_null($model = $this->first($columns))) { return $model; } return $callback(); } /** * Execute the query and get the first result if it's the sole matching record. * * @param array|string $columns * @return \Illuminate\Database\Eloquent\Model * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> * @throws \Illuminate\Database\MultipleRecordsFoundException */ public function sole($columns = ['*']) { try { return $this->baseSole($columns); } catch (RecordsNotFoundException) { throw (new ModelNotFoundException)->setModel(get_class($this->model)); } } /** * Get a single column's value from the first result of a query. * * @param string|\Illuminate\Contracts\Database\Query\Expression $column * @return mixed */ public function value($column) { if ($result = $this->first([$column])) { $column = $column instanceof Expression ? $column->getValue($this->getGrammar()) : $column; return $result->{Str::afterLast($column, '.')}; } } /** * Get a single column's value from the first result of a query if it's the sole matching record. * * @param string|\Illuminate\Contracts\Database\Query\Expression $column * @return mixed * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> * @throws \Illuminate\Database\MultipleRecordsFoundException */ public function soleValue($column) { $column = $column instanceof Expression ? $column->getValue($this->getGrammar()) : $column; return $this->sole([$column])->{Str::afterLast($column, '.')}; } /** * Get a single column's value from the first result of the query or throw an exception. * * @param string|\Illuminate\Contracts\Database\Query\Expression $column * @return mixed * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public function valueOrFail($column) { $column = $column instanceof Expression ? $column->getValue($this->getGrammar()) : $column; return $this->firstOrFail([$column])->{Str::afterLast($column, '.')}; } /** * Execute the query as a "select" statement. * * @param array|string $columns * @return \Illuminate\Database\Eloquent\Collection|static[] */ public function get($columns = ['*']) { $builder = $this->applyScopes(); // If we actually found models we will also eager load any relationships that // have been specified as needing to be eager loaded, which will solve the // n+1 query issue for the developers to avoid running a lot of queries. if (count($models = $builder->getModels($columns)) > 0) { $models = $builder->eagerLoadRelations($models); } return $builder->getModel()->newCollection($models); } /** * Get the hydrated models without eager loading. * * @param array|string $columns * @return \Illuminate\Database\Eloquent\Model[]|static[] */ public function getModels($columns = ['*']) { return $this->model->hydrate( $this->query->get($columns)->all() )->all(); } /** * Eager load the relationships for the models. * * @param array $models * @return array */ public function eagerLoadRelations(array $models) { foreach ($this->eagerLoad as $name => $constraints) { // For nested eager loads we'll skip loading them here and they will be set as an // eager load on the query to retrieve the relation so that they will be eager // loaded on that query, because that is where they get hydrated as models. if (! str_contains($name, '.')) { $models = $this->eagerLoadRelation($models, $name, $constraints); } } return $models; } /** * Eagerly load the relationship on a set of models. * * @param array $models * @param string $name * @param \Closure $constraints * @return array */ protected function eagerLoadRelation(array $models, $name, Closure $constraints) { // First we will "back up" the existing where conditions on the query so we can // add our eager constraints. Then we will merge the wheres that were on the // query back to it in order that any where conditions might be specified. $relation = $this->getRelation($name); $relation->addEagerConstraints($models); $constraints($relation); // Once we have the results, we just match those back up to their parent models // using the relationship instance. Then we just return the finished arrays // of models which have been eagerly hydrated and are readied for return. return $relation->match( $relation->initRelation($models, $name), $relation->getEager(), $name ); } /** * Get the relation instance for the given relation name. * * @param string $name * @return \Illuminate\Database\Eloquent\Relations\Relation */ public function getRelation($name) { // We want to run a relationship query without any constrains so that we will // not have to remove these where clauses manually which gets really hacky // and error prone. We don't want constraints because we add eager ones. $relation = Relation::noConstraints(function () use ($name) { try { return $this->getModel()->newInstance()->$name(); } catch (BadMethodCallException) { throw RelationNotFoundException::make($this->getModel(), $name); } }); $nested = $this->relationsNestedUnder($name); // If there are nested relationships set on the query, we will put those onto // the query instances so that they can be handled after this relationship // is loaded. In this way they will all trickle down as they are loaded. if (count($nested) > 0) { $relation->getQuery()->with($nested); } return $relation; } /** * Get the deeply nested relations for a given top-level relation. * * @param string $relation * @return array */ protected function relationsNestedUnder($relation) { $nested = []; // We are basically looking for any relationships that are nested deeper than // the given top-level relationship. We will just check for any relations // that start with the given top relations and adds them to our arrays. foreach ($this->eagerLoad as $name => $constraints) { if ($this->isNestedUnder($relation, $name)) { $nested[substr($name, strlen($relation.'.'))] = $constraints; } } return $nested; } /** * Determine if the relationship is nested. * * @param string $relation * @param string $name * @return bool */ protected function isNestedUnder($relation, $name) { return str_contains($name, '.') && str_starts_with($name, $relation.'.'); } /** * Get a lazy collection for the given query. * * @return \Illuminate\Support\LazyCollection */ public function cursor() { return $this->applyScopes()->query->cursor()->map(function ($record) { return $this->newModelInstance()->newFromBuilder($record); }); } /** * Add a generic "order by" clause if the query doesn't already have one. * * @return void */ protected function enforceOrderBy() { if (empty($this->query->orders) && empty($this->query->unionOrders)) { $this->orderBy($this->model->getQualifiedKeyName(), 'asc'); } } /** * Get a collection with the values of a given column. * * @param string|\Illuminate\Contracts\Database\Query\Expression $column * @param string|null $key * @return \Illuminate\Support\Collection */ public function pluck($column, $key = null) { $results = $this->toBase()->pluck($column, $key); $column = $column instanceof Expression ? $column->getValue($this->getGrammar()) : $column; // If the model has a mutator for the requested column, we will spin through // the results and mutate the values so that the mutated version of these // columns are returned as you would expect from these Eloquent models. if (! $this->model->hasGetMutator($column) && ! $this->model->hasCast($column) && ! in_array($column, $this->model->getDates())) { return $results; } return $results->map(function ($value) use ($column) { return $this->model->newFromBuilder([$column => $value])->{$column}; }); } /** * Paginate the given query. * * @param int|null|\Closure $perPage * @param array|string $columns * @param string $pageName * @param int|null $page * @param \Closure|int|null $total * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator * * @throws \InvalidArgumentException */ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) { $page = $page ?: Paginator::resolveCurrentPage($pageName); $total = func_num_args() === 5 ? value(func_get_arg(4)) : $this->toBase()->getCountForPagination(); $perPage = ($perPage instanceof Closure ? $perPage($total) : $perPage ) ?: $this->model->getPerPage(); $results = $total ? $this->forPage($page, $perPage)->get($columns) : $this->model->newCollection(); return $this->paginator($results, $total, $perPage, $page, [ 'path' => Paginator::resolveCurrentPath(), 'pageName' => $pageName, ]); } /** * Paginate the given query into a simple paginator. * * @param int|null $perPage * @param array|string $columns * @param string $pageName * @param int|null $page * @return \Illuminate\Contracts\Pagination\Paginator */ public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) { $page = $page ?: Paginator::resolveCurrentPage($pageName); $perPage = $perPage ?: $this->model->getPerPage(); // Next we will set the limit and offset for this query so that when we get the // results we get the proper section of results. Then, we'll create the full // paginator instances for these results with the given page and per page. $this->skip(($page - 1) * $perPage)->take($perPage + 1); return $this->simplePaginator($this->get($columns), $perPage, $page, [ 'path' => Paginator::resolveCurrentPath(), 'pageName' => $pageName, ]); } /** * Paginate the given query into a cursor paginator. * * @param int|null $perPage * @param array|string $columns * @param string $cursorName * @param \Illuminate\Pagination\Cursor|string|null $cursor * @return \Illuminate\Contracts\Pagination\CursorPaginator */ public function cursorPaginate($perPage = null, $columns = ['*'], $cursorName = 'cursor', $cursor = null) { $perPage = $perPage ?: $this->model->getPerPage(); return $this->paginateUsingCursor($perPage, $columns, $cursorName, $cursor); } /** * Ensure the proper order by required for cursor pagination. * * @param bool $shouldReverse * @return \Illuminate\Support\Collection */ protected function ensureOrderForCursorPagination($shouldReverse = false) { if (empty($this->query->orders) && empty($this->query->unionOrders)) { $this->enforceOrderBy(); } $reverseDirection = function ($order) { if (! isset($order['direction'])) { return $order; } $order['direction'] = $order['direction'] === 'asc' ? 'desc' : 'asc'; return $order; }; if ($shouldReverse) { $this->query->orders = collect($this->query->orders)->map($reverseDirection)->toArray(); $this->query->unionOrders = collect($this->query->unionOrders)->map($reverseDirection)->toArray(); } $orders = ! empty($this->query->unionOrders) ? $this->query->unionOrders : $this->query->orders; return collect($orders) ->filter(fn ($order) => Arr::has($order, 'direction')) ->values(); } /** * Save a new model and return the instance. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Model|$this */ public function create(array $attributes = []) { return tap($this->newModelInstance($attributes), function ($instance) { $instance->save(); }); } /** * Save a new model and return the instance. Allow mass-assignment. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Model|$this */ public function forceCreate(array $attributes) { return $this->model->unguarded(function () use ($attributes) { return $this->newModelInstance()->create($attributes); }); } /** * Save a new model instance with mass assignment without raising model events. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Model|$this */ public function forceCreateQuietly(array $attributes = []) { return Model::withoutEvents(fn () => $this->forceCreate($attributes)); } /** * Update records in the database. * * @param array $values * @return int */ public function update(array $values) { return $this->toBase()->update($this->addUpdatedAtColumn($values)); } /** * Insert new records or update the existing ones. * * @param array $values * @param array|string $uniqueBy * @param array|null $update * @return int */ public function upsert(array $values, $uniqueBy, $update = null) { if (empty($values)) { return 0; } if (! is_array(reset($values))) { $values = [$values]; } if (is_null($update)) { $update = array_keys(reset($values)); } return $this->toBase()->upsert( $this->addTimestampsToUpsertValues($this->addUniqueIdsToUpsertValues($values)), $uniqueBy, $this->addUpdatedAtToUpsertColumns($update) ); } /** * Update the column's update timestamp. * * @param string|null $column * @return int|false */ public function touch($column = null) { $time = $this->model->freshTimestamp(); if ($column) { return $this->toBase()->update([$column => $time]); } $column = $this->model->getUpdatedAtColumn(); if (! $this->model->usesTimestamps() || is_null($column)) { return false; } return $this->toBase()->update([$column => $time]); } /** * Increment a column's value by a given amount. * * @param string|\Illuminate\Contracts\Database\Query\Expression $column * @param float|int $amount * @param array $extra * @return int */ public function increment($column, $amount = 1, array $extra = []) { return $this->toBase()->increment( $column, $amount, $this->addUpdatedAtColumn($extra) ); } /** * Decrement a column's value by a given amount. * * @param string|\Illuminate\Contracts\Database\Query\Expression $column * @param float|int $amount * @param array $extra * @return int */ public function decrement($column, $amount = 1, array $extra = []) { return $this->toBase()->decrement( $column, $amount, $this->addUpdatedAtColumn($extra) ); } /** * Add the "updated at" column to an array of values. * * @param array $values * @return array */ protected function addUpdatedAtColumn(array $values) { if (! $this->model->usesTimestamps() || is_null($this->model->getUpdatedAtColumn())) { return $values; } $column = $this->model->getUpdatedAtColumn(); if (! array_key_exists($column, $values)) { $timestamp = $this->model->freshTimestampString(); if ( $this->model->hasSetMutator($column) || $this->model->hasAttributeSetMutator($column) || $this->model->hasCast($column) ) { $timestamp = $this->model->newInstance() ->forceFill([$column => $timestamp]) ->getAttributes()[$column] ?? $timestamp; } $values = array_merge([$column => $timestamp], $values); } $segments = preg_split('/\s+as\s+/i', $this->query->from); $qualifiedColumn = end($segments).'.'.$column; $values[$qualifiedColumn] = Arr::get($values, $qualifiedColumn, $values[$column]); unset($values[$column]); return $values; } /** * Add unique IDs to the inserted values. * * @param array $values * @return array */ protected function addUniqueIdsToUpsertValues(array $values) { if (! $this->model->usesUniqueIds()) { return $values; } foreach ($this->model->uniqueIds() as $uniqueIdAttribute) { foreach ($values as &$row) { if (! array_key_exists($uniqueIdAttribute, $row)) { $row = array_merge([$uniqueIdAttribute => $this->model->newUniqueId()], $row); } } } return $values; } /** * Add timestamps to the inserted values. * * @param array $values * @return array */ protected function addTimestampsToUpsertValues(array $values) { if (! $this->model->usesTimestamps()) { return $values; } $timestamp = $this->model->freshTimestampString(); $columns = array_filter([ $this->model->getCreatedAtColumn(), $this->model->getUpdatedAtColumn(), ]); foreach ($columns as $column) { foreach ($values as &$row) { $row = array_merge([$column => $timestamp], $row); } } return $values; } /** * Add the "updated at" column to the updated columns. * * @param array $update * @return array */ protected function addUpdatedAtToUpsertColumns(array $update) { if (! $this->model->usesTimestamps()) { return $update; } $column = $this->model->getUpdatedAtColumn(); if (! is_null($column) && ! array_key_exists($column, $update) && ! in_array($column, $update)) { $update[] = $column; } return $update; } /** * Delete records from the database. * * @return mixed */ public function delete() { if (isset($this->onDelete)) { return call_user_func($this->onDelete, $this); } return $this->toBase()->delete(); } /** * Run the default delete function on the builder. * * Since we do not apply scopes here, the row will actually be deleted. * * @return mixed */ public function forceDelete() { return $this->query->delete(); } /** * Register a replacement for the default delete function. * * @param \Closure $callback * @return void */ public function onDelete(Closure $callback) { $this->onDelete = $callback; } /** * Determine if the given model has a scope. * * @param string $scope * @return bool */ public function hasNamedScope($scope) { return $this->model && $this->model->hasNamedScope($scope); } /** * Call the given local model scopes. * * @param array|string $scopes * @return static|mixed */ public function scopes($scopes) { $builder = $this; foreach (Arr::wrap($scopes) as $scope => $parameters) { // If the scope key is an integer, then the scope was passed as the value and // the parameter list is empty, so we will format the scope name and these // parameters here. Then, we'll be ready to call the scope on the model. if (is_int($scope)) { [$scope, $parameters] = [$parameters, []]; } // Next we'll pass the scope callback to the callScope method which will take // care of grouping the "wheres" properly so the logical order doesn't get // messed up when adding scopes. Then we'll return back out the builder. $builder = $builder->callNamedScope( $scope, Arr::wrap($parameters) ); } return $builder; } /** * Apply the scopes to the Eloquent builder instance and return it. * * @return static */ public function applyScopes() { if (! $this->scopes) { return $this; } $builder = clone $this; foreach ($this->scopes as $identifier => $scope) { if (! isset($builder->scopes[$identifier])) { continue; } $builder->callScope(function (self $builder) use ($scope) { // If the scope is a Closure we will just go ahead and call the scope with the // builder instance. The "callScope" method will properly group the clauses // that are added to this query so "where" clauses maintain proper logic. if ($scope instanceof Closure) { $scope($builder); } // If the scope is a scope object, we will call the apply method on this scope // passing in the builder and the model instance. After we run all of these // scopes we will return back the builder instance to the outside caller. if ($scope instanceof Scope) { $scope->apply($builder, $this->getModel()); } }); } return $builder; } /** * Apply the given scope on the current builder instance. * * @param callable $scope * @param array $parameters * @return mixed */ protected function callScope(callable $scope, array $parameters = []) { array_unshift($parameters, $this); $query = $this->getQuery(); // We will keep track of how many wheres are on the query before running the // scope so that we can properly group the added scope constraints in the // query as their own isolated nested where statement and avoid issues. $originalWhereCount = is_null($query->wheres) ? 0 : count($query->wheres); $result = $scope(...$parameters) ?? $this; if (count((array) $query->wheres) > $originalWhereCount) { $this->addNewWheresWithinGroup($query, $originalWhereCount); } return $result; } /** * Apply the given named scope on the current builder instance. * * @param string $scope * @param array $parameters * @return mixed */ protected function callNamedScope($scope, array $parameters = []) { return $this->callScope(function (...$parameters) use ($scope) { return $this->model->callNamedScope($scope, $parameters); }, $parameters); } /** * Nest where conditions by slicing them at the given where count. * * @param \Illuminate\Database\Query\Builder $query * @param int $originalWhereCount * @return void */ protected function addNewWheresWithinGroup(QueryBuilder $query, $originalWhereCount) { // Here, we totally remove all of the where clauses since we are going to // rebuild them as nested queries by slicing the groups of wheres into // their own sections. This is to prevent any confusing logic order. $allWheres = $query->wheres; $query->wheres = []; $this->groupWhereSliceForScope( $query, array_slice($allWheres, 0, $originalWhereCount) ); $this->groupWhereSliceForScope( $query, array_slice($allWheres, $originalWhereCount) ); } /** * Slice where conditions at the given offset and add them to the query as a nested condition. * * @param \Illuminate\Database\Query\Builder $query * @param array $whereSlice * @return void */ protected function groupWhereSliceForScope(QueryBuilder $query, $whereSlice) { $whereBooleans = collect($whereSlice)->pluck('boolean'); // Here we'll check if the given subset of where clauses contains any "or" // booleans and in this case create a nested where expression. That way // we don't add any unnecessary nesting thus keeping the query clean. if ($whereBooleans->contains('or')) { $query->wheres[] = $this->createNestedWhere( $whereSlice, $whereBooleans->first() ); } else { $query->wheres = array_merge($query->wheres, $whereSlice); } } /** * Create a where array with nested where conditions. * * @param array $whereSlice * @param string $boolean * @return array */ protected function createNestedWhere($whereSlice, $boolean = 'and') { $whereGroup = $this->getQuery()->forNestedWhere(); $whereGroup->wheres = $whereSlice; return ['type' => 'Nested', 'query' => $whereGroup, 'boolean' => $boolean]; } /** * Set the relationships that should be eager loaded. * * @param string|array $relations * @param string|\Closure|null $callback * @return $this */ public function with($relations, $callback = null) { if ($callback instanceof Closure) { $eagerLoad = $this->parseWithRelations([$relations => $callback]); } else { $eagerLoad = $this->parseWithRelations(is_string($relations) ? func_get_args() : $relations); } $this->eagerLoad = array_merge($this->eagerLoad, $eagerLoad); return $this; } /** * Prevent the specified relations from being eager loaded. * * @param mixed $relations * @return $this */ public function without($relations) { $this->eagerLoad = array_diff_key($this->eagerLoad, array_flip( is_string($relations) ? func_get_args() : $relations )); return $this; } /** * Set the relationships that should be eager loaded while removing any previously added eager loading specifications. * * @param mixed $relations * @return $this */ public function withOnly($relations) { $this->eagerLoad = []; return $this->with($relations); } /** * Create a new instance of the model being queried. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Model|static */ public function newModelInstance($attributes = []) { return $this->model->newInstance($attributes)->setConnection( $this->query->getConnection()->getName() ); } /** * Parse a list of relations into individuals. * * @param array $relations * @return array */ protected function parseWithRelations(array $relations) { if ($relations === []) { return []; } $results = []; foreach ($this->prepareNestedWithRelationships($relations) as $name => $constraints) { // We need to separate out any nested includes, which allows the developers // to load deep relationships using "dots" without stating each level of // the relationship with its own key in the array of eager-load names. $results = $this->addNestedWiths($name, $results); $results[$name] = $constraints; } return $results; } /** * Prepare nested with relationships. * * @param array $relations * @param string $prefix * @return array */ protected function prepareNestedWithRelationships($relations, $prefix = '') { $preparedRelationships = []; if ($prefix !== '') { $prefix .= '.'; } // If any of the relationships are formatted with the [$attribute => array()] // syntax, we shall loop over the nested relations and prepend each key of // this array while flattening into the traditional dot notation format. foreach ($relations as $key => $value) { if (! is_string($key) || ! is_array($value)) { continue; } [$attribute, $attributeSelectConstraint] = $this->parseNameAndAttributeSelectionConstraint($key); $preparedRelationships = array_merge( $preparedRelationships, ["{$prefix}{$attribute}" => $attributeSelectConstraint], $this->prepareNestedWithRelationships($value, "{$prefix}{$attribute}"), ); unset($relations[$key]); } // We now know that the remaining relationships are in a dot notation format // and may be a string or Closure. We'll loop over them and ensure all of // the present Closures are merged + strings are made into constraints. foreach ($relations as $key => $value) { if (is_numeric($key) && is_string($value)) { [$key, $value] = $this->parseNameAndAttributeSelectionConstraint($value); } $preparedRelationships[$prefix.$key] = $this->combineConstraints([ $value, $preparedRelationships[$prefix.$key] ?? static function () { // }, ]); } return $preparedRelationships; } /** * Combine an array of constraints into a single constraint. * * @param array $constraints * @return \Closure */ protected function combineConstraints(array $constraints) { return function ($builder) use ($constraints) { foreach ($constraints as $constraint) { $builder = $constraint($builder) ?? $builder; } return $builder; }; } /** * Parse the attribute select constraints from the name. * * @param string $name * @return array */ protected function parseNameAndAttributeSelectionConstraint($name) { return str_contains($name, ':') ? $this->createSelectWithConstraint($name) : [$name, static function () { // }]; } /** * Create a constraint to select the given columns for the relation. * * @param string $name * @return array */ protected function createSelectWithConstraint($name) { return [explode(':', $name)[0], static function ($query) use ($name) { $query->select(array_map(static function ($column) use ($query) { if (str_contains($column, '.')) { return $column; } return $query instanceof BelongsToMany ? $query->getRelated()->getTable().'.'.$column : $column; }, explode(',', explode(':', $name)[1]))); }]; } /** * Parse the nested relationships in a relation. * * @param string $name * @param array $results * @return array */ protected function addNestedWiths($name, $results) { $progress = []; // If the relation has already been set on the result array, we will not set it // again, since that would override any constraints that were already placed // on the relationships. We will only set the ones that are not specified. foreach (explode('.', $name) as $segment) { $progress[] = $segment; if (! isset($results[$last = implode('.', $progress)])) { $results[$last] = static function () { // }; } } return $results; } /** * Apply query-time casts to the model instance. * * @param array $casts * @return $this */ public function withCasts($casts) { $this->model->mergeCasts($casts); return $this; } /** * Execute the given Closure within a transaction savepoint if needed. * * @template TModelValue * * @param \Closure(): TModelValue $scope * @return TModelValue */ public function withSavepointIfNeeded(Closure $scope): mixed { return $this->getQuery()->getConnection()->transactionLevel() > 0 ? $this->getQuery()->getConnection()->transaction($scope) : $scope(); } /** * Get the underlying query builder instance. * * @return \Illuminate\Database\Query\Builder */ public function getQuery() { return $this->query; } /** * Set the underlying query builder instance. * * @param \Illuminate\Database\Query\Builder $query * @return $this */ public function setQuery($query) { $this->query = $query; return $this; } /** * Get a base query builder instance. * * @return \Illuminate\Database\Query\Builder */ public function toBase() { return $this->applyScopes()->getQuery(); } /** * Get the relationships being eagerly loaded. * * @return array */ public function getEagerLoads() { return $this->eagerLoad; } /** * Set the relationships being eagerly loaded. * * @param array $eagerLoad * @return $this */ public function setEagerLoads(array $eagerLoad) { $this->eagerLoad = $eagerLoad; return $this; } /** * Indicate that the given relationships should not be eagerly loaded. * * @param array $relations * @return $this */ public function withoutEagerLoad(array $relations) { $relations = array_diff(array_keys($this->model->getRelations()), $relations); return $this->with($relations); } /** * Flush the relationships being eagerly loaded. * * @return $this */ public function withoutEagerLoads() { return $this->setEagerLoads([]); } /** * Get the default key name of the table. * * @return string */ protected function defaultKeyName() { return $this->getModel()->getKeyName(); } /** * Get the model instance being queried. * * @return \Illuminate\Database\Eloquent\Model|static */ public function getModel() { return $this->model; } /** * Set a model instance for the model being queried. * * @param \Illuminate\Database\Eloquent\Model $model * @return $this */ public function setModel(Model $model) { $this->model = $model; $this->query->from($model->getTable()); return $this; } /** * Qualify the given column name by the model's table. * * @param string|\Illuminate\Contracts\Database\Query\Expression $column * @return string */ public function qualifyColumn($column) { $column = $column instanceof Expression ? $column->getValue($this->getGrammar()) : $column; return $this->model->qualifyColumn($column); } /** * Qualify the given columns with the model's table. * * @param array|\Illuminate\Contracts\Database\Query\Expression $columns * @return array */ public function qualifyColumns($columns) { return $this->model->qualifyColumns($columns); } /** * Get the given macro by name. * * @param string $name * @return \Closure */ public function getMacro($name) { return Arr::get($this->localMacros, $name); } /** * Checks if a macro is registered. * * @param string $name * @return bool */ public function hasMacro($name) { return isset($this->localMacros[$name]); } /** * Get the given global macro by name. * * @param string $name * @return \Closure */ public static function getGlobalMacro($name) { return Arr::get(static::$macros, $name); } /** * Checks if a global macro is registered. * * @param string $name * @return bool */ public static function hasGlobalMacro($name) { return isset(static::$macros[$name]); } /** * Dynamically access builder proxies. * * @param string $key * @return mixed * * @throws \Exception */ public function __get($key) { if (in_array($key, ['orWhere', 'whereNot', 'orWhereNot'])) { return new HigherOrderBuilderProxy($this, $key); } if (in_array($key, $this->propertyPassthru)) { return $this->toBase()->{$key}; } throw new Exception("Property [{$key}] does not exist on the Eloquent builder instance."); } /** * Dynamically handle calls into the query instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if ($method === 'macro') { $this->localMacros[$parameters[0]] = $parameters[1]; return; } if ($this->hasMacro($method)) { array_unshift($parameters, $this); return $this->localMacros[$method](...$parameters); } if (static::hasGlobalMacro($method)) { $callable = static::$macros[$method]; if ($callable instanceof Closure) { $callable = $callable->bindTo($this, static::class); } return $callable(...$parameters); } if ($this->hasNamedScope($method)) { return $this->callNamedScope($method, $parameters); } if (in_array(strtolower($method), $this->passthru)) { return $this->toBase()->{$method}(...$parameters); } $this->forwardCallTo($this->query, $method, $parameters); return $this; } /** * Dynamically handle calls into the query instance. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public static function __callStatic($method, $parameters) { if ($method === 'macro') { static::$macros[$parameters[0]] = $parameters[1]; return; } if ($method === 'mixin') { return static::registerMixin($parameters[0], $parameters[1] ?? true); } if (! static::hasGlobalMacro($method)) { static::throwBadMethodCallException($method); } $callable = static::$macros[$method]; if ($callable instanceof Closure) { $callable = $callable->bindTo(null, static::class); } return $callable(...$parameters); } /** * Register the given mixin with the builder. * * @param string $mixin * @param bool $replace * @return void */ protected static function registerMixin($mixin, $replace) { $methods = (new ReflectionClass($mixin))->getMethods( ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED ); foreach ($methods as $method) { if ($replace || ! static::hasGlobalMacro($method->name)) { static::macro($method->name, $method->invoke($mixin)); } } } /** * Clone the Eloquent query builder. * * @return static */ public function clone() { return clone $this; } /** * Force a clone of the underlying query builder when cloning. * * @return void */ public function __clone() { $this->query = clone $this->query; } } src/Illuminate/Database/Eloquent/Scope.php 0000644 00000000536 15107327037 0014531 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; interface Scope { /** * Apply the scope to a given Eloquent query builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model $model * @return void */ public function apply(Builder $builder, Model $model); } src/Illuminate/Database/Eloquent/Relations/Relation.php 0000644 00000032457 15107327037 0017204 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use Closure; use Illuminate\Contracts\Database\Eloquent\Builder as BuilderContract; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\MultipleRecordsFoundException; use Illuminate\Database\Query\Expression; use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\Macroable; abstract class Relation implements BuilderContract { use ForwardsCalls, Macroable { Macroable::__call as macroCall; } /** * The Eloquent query builder instance. * * @var \Illuminate\Database\Eloquent\Builder */ protected $query; /** * The parent model instance. * * @var \Illuminate\Database\Eloquent\Model */ protected $parent; /** * The related model instance. * * @var \Illuminate\Database\Eloquent\Model */ protected $related; /** * Indicates whether the eagerly loaded relation should implicitly return an empty collection. * * @var bool */ protected $eagerKeysWereEmpty = false; /** * Indicates if the relation is adding constraints. * * @var bool */ protected static $constraints = true; /** * An array to map class names to their morph names in the database. * * @var array */ public static $morphMap = []; /** * Prevents morph relationships without a morph map. * * @var bool */ protected static $requireMorphMap = false; /** * The count of self joins. * * @var int */ protected static $selfJoinCount = 0; /** * Create a new relation instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @return void */ public function __construct(Builder $query, Model $parent) { $this->query = $query; $this->parent = $parent; $this->related = $query->getModel(); $this->addConstraints(); } /** * Run a callback with constraints disabled on the relation. * * @param \Closure $callback * @return mixed */ public static function noConstraints(Closure $callback) { $previous = static::$constraints; static::$constraints = false; // When resetting the relation where clause, we want to shift the first element // off of the bindings, leaving only the constraints that the developers put // as "extra" on the relationships, and not original relation constraints. try { return $callback(); } finally { static::$constraints = $previous; } } /** * Set the base constraints on the relation query. * * @return void */ abstract public function addConstraints(); /** * Set the constraints for an eager load of the relation. * * @param array $models * @return void */ abstract public function addEagerConstraints(array $models); /** * Initialize the relation on a set of models. * * @param array $models * @param string $relation * @return array */ abstract public function initRelation(array $models, $relation); /** * Match the eagerly loaded results to their parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array */ abstract public function match(array $models, Collection $results, $relation); /** * Get the results of the relationship. * * @return mixed */ abstract public function getResults(); /** * Get the relationship for eager loading. * * @return \Illuminate\Database\Eloquent\Collection */ public function getEager() { return $this->eagerKeysWereEmpty ? $this->query->getModel()->newCollection() : $this->get(); } /** * Execute the query and get the first result if it's the sole matching record. * * @param array|string $columns * @return \Illuminate\Database\Eloquent\Model * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> * @throws \Illuminate\Database\MultipleRecordsFoundException */ public function sole($columns = ['*']) { $result = $this->take(2)->get($columns); $count = $result->count(); if ($count === 0) { throw (new ModelNotFoundException)->setModel(get_class($this->related)); } if ($count > 1) { throw new MultipleRecordsFoundException($count); } return $result->first(); } /** * Execute the query as a "select" statement. * * @param array $columns * @return \Illuminate\Database\Eloquent\Collection */ public function get($columns = ['*']) { return $this->query->get($columns); } /** * Touch all of the related models for the relationship. * * @return void */ public function touch() { $model = $this->getRelated(); if (! $model::isIgnoringTouch()) { $this->rawUpdate([ $model->getUpdatedAtColumn() => $model->freshTimestampString(), ]); } } /** * Run a raw update against the base query. * * @param array $attributes * @return int */ public function rawUpdate(array $attributes = []) { return $this->query->withoutGlobalScopes()->update($attributes); } /** * Add the constraints for a relationship count query. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceCountQuery(Builder $query, Builder $parentQuery) { return $this->getRelationExistenceQuery( $query, $parentQuery, new Expression('count(*)') )->setBindings([], 'select'); } /** * Add the constraints for an internal relationship existence query. * * Essentially, these queries compare on column names like whereColumn. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) { return $query->select($columns)->whereColumn( $this->getQualifiedParentKeyName(), '=', $this->getExistenceCompareKey() ); } /** * Get a relationship join table hash. * * @param bool $incrementJoinCount * @return string */ public function getRelationCountHash($incrementJoinCount = true) { return 'laravel_reserved_'.($incrementJoinCount ? static::$selfJoinCount++ : static::$selfJoinCount); } /** * Get all of the primary keys for an array of models. * * @param array $models * @param string|null $key * @return array */ protected function getKeys(array $models, $key = null) { return collect($models)->map(function ($value) use ($key) { return $key ? $value->getAttribute($key) : $value->getKey(); })->values()->unique(null, true)->sort()->all(); } /** * Get the query builder that will contain the relationship constraints. * * @return \Illuminate\Database\Eloquent\Builder */ protected function getRelationQuery() { return $this->query; } /** * Get the underlying query for the relation. * * @return \Illuminate\Database\Eloquent\Builder */ public function getQuery() { return $this->query; } /** * Get the base query builder driving the Eloquent builder. * * @return \Illuminate\Database\Query\Builder */ public function getBaseQuery() { return $this->query->getQuery(); } /** * Get a base query builder instance. * * @return \Illuminate\Database\Query\Builder */ public function toBase() { return $this->query->toBase(); } /** * Get the parent model of the relation. * * @return \Illuminate\Database\Eloquent\Model */ public function getParent() { return $this->parent; } /** * Get the fully qualified parent key name. * * @return string */ public function getQualifiedParentKeyName() { return $this->parent->getQualifiedKeyName(); } /** * Get the related model of the relation. * * @return \Illuminate\Database\Eloquent\Model */ public function getRelated() { return $this->related; } /** * Get the name of the "created at" column. * * @return string */ public function createdAt() { return $this->parent->getCreatedAtColumn(); } /** * Get the name of the "updated at" column. * * @return string */ public function updatedAt() { return $this->parent->getUpdatedAtColumn(); } /** * Get the name of the related model's "updated at" column. * * @return string */ public function relatedUpdatedAt() { return $this->related->getUpdatedAtColumn(); } /** * Add a whereIn eager constraint for the given set of model keys to be loaded. * * @param string $whereIn * @param string $key * @param array $modelKeys * @param \Illuminate\Database\Eloquent\Builder $query * @return void */ protected function whereInEager(string $whereIn, string $key, array $modelKeys, $query = null) { ($query ?? $this->query)->{$whereIn}($key, $modelKeys); if ($modelKeys === []) { $this->eagerKeysWereEmpty = true; } } /** * Get the name of the "where in" method for eager loading. * * @param \Illuminate\Database\Eloquent\Model $model * @param string $key * @return string */ protected function whereInMethod(Model $model, $key) { return $model->getKeyName() === last(explode('.', $key)) && in_array($model->getKeyType(), ['int', 'integer']) ? 'whereIntegerInRaw' : 'whereIn'; } /** * Prevent polymorphic relationships from being used without model mappings. * * @param bool $requireMorphMap * @return void */ public static function requireMorphMap($requireMorphMap = true) { static::$requireMorphMap = $requireMorphMap; } /** * Determine if polymorphic relationships require explicit model mapping. * * @return bool */ public static function requiresMorphMap() { return static::$requireMorphMap; } /** * Define the morph map for polymorphic relations and require all morphed models to be explicitly mapped. * * @param array $map * @param bool $merge * @return array */ public static function enforceMorphMap(array $map, $merge = true) { static::requireMorphMap(); return static::morphMap($map, $merge); } /** * Set or get the morph map for polymorphic relations. * * @param array|null $map * @param bool $merge * @return array */ public static function morphMap(array $map = null, $merge = true) { $map = static::buildMorphMapFromModels($map); if (is_array($map)) { static::$morphMap = $merge && static::$morphMap ? $map + static::$morphMap : $map; } return static::$morphMap; } /** * Builds a table-keyed array from model class names. * * @param string[]|null $models * @return array|null */ protected static function buildMorphMapFromModels(array $models = null) { if (is_null($models) || ! array_is_list($models)) { return $models; } return array_combine(array_map(function ($model) { return (new $model)->getTable(); }, $models), $models); } /** * Get the model associated with a custom polymorphic type. * * @param string $alias * @return string|null */ public static function getMorphedModel($alias) { return static::$morphMap[$alias] ?? null; } /** * Handle dynamic method calls to the relationship. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } return $this->forwardDecoratedCallTo($this->query, $method, $parameters); } /** * Force a clone of the underlying query builder when cloning. * * @return void */ public function __clone() { $this->query = clone $this->query; } } src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php 0000644 00000004316 15107327037 0020136 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; class HasOneThrough extends HasManyThrough { use InteractsWithDictionary, SupportsDefaultModels; /** * Get the results of the relationship. * * @return mixed */ public function getResults() { return $this->first() ?: $this->getDefaultFor($this->farParent); } /** * Initialize the relation on a set of models. * * @param array $models * @param string $relation * @return array */ public function initRelation(array $models, $relation) { foreach ($models as $model) { $model->setRelation($relation, $this->getDefaultFor($model)); } return $models; } /** * Match the eagerly loaded results to their parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array */ public function match(array $models, Collection $results, $relation) { $dictionary = $this->buildDictionary($results); // Once we have the dictionary we can simply spin through the parent models to // link them up with their children using the keyed dictionary to make the // matching very convenient and easy work. Then we'll just return them. foreach ($models as $model) { if (isset($dictionary[$key = $this->getDictionaryKey($model->getAttribute($this->localKey))])) { $value = $dictionary[$key]; $model->setRelation( $relation, reset($value) ); } } return $models; } /** * Make a new related instance for the given model. * * @param \Illuminate\Database\Eloquent\Model $parent * @return \Illuminate\Database\Eloquent\Model */ public function newRelatedInstanceFor(Model $parent) { return $this->related->newInstance(); } } src/Illuminate/Database/Eloquent/Relations/MorphOne.php 0000644 00000010254 15107327037 0017145 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Contracts\Database\Eloquent\SupportsPartialRelations; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Concerns\CanBeOneOfMany; use Illuminate\Database\Eloquent\Relations\Concerns\ComparesRelatedModels; use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; use Illuminate\Database\Query\JoinClause; class MorphOne extends MorphOneOrMany implements SupportsPartialRelations { use CanBeOneOfMany, ComparesRelatedModels, SupportsDefaultModels; /** * Get the results of the relationship. * * @return mixed */ public function getResults() { if (is_null($this->getParentKey())) { return $this->getDefaultFor($this->parent); } return $this->query->first() ?: $this->getDefaultFor($this->parent); } /** * Initialize the relation on a set of models. * * @param array $models * @param string $relation * @return array */ public function initRelation(array $models, $relation) { foreach ($models as $model) { $model->setRelation($relation, $this->getDefaultFor($model)); } return $models; } /** * Match the eagerly loaded results to their parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array */ public function match(array $models, Collection $results, $relation) { return $this->matchOne($models, $results, $relation); } /** * Get the relationship query. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) { if ($this->isOneOfMany()) { $this->mergeOneOfManyJoinsTo($query); } return parent::getRelationExistenceQuery($query, $parentQuery, $columns); } /** * Add constraints for inner join subselect for one of many relationships. * * @param \Illuminate\Database\Eloquent\Builder $query * @param string|null $column * @param string|null $aggregate * @return void */ public function addOneOfManySubQueryConstraints(Builder $query, $column = null, $aggregate = null) { $query->addSelect($this->foreignKey, $this->morphType); } /** * Get the columns that should be selected by the one of many subquery. * * @return array|string */ public function getOneOfManySubQuerySelectColumns() { return [$this->foreignKey, $this->morphType]; } /** * Add join query constraints for one of many relationships. * * @param \Illuminate\Database\Query\JoinClause $join * @return void */ public function addOneOfManyJoinSubQueryConstraints(JoinClause $join) { $join ->on($this->qualifySubSelectColumn($this->morphType), '=', $this->qualifyRelatedColumn($this->morphType)) ->on($this->qualifySubSelectColumn($this->foreignKey), '=', $this->qualifyRelatedColumn($this->foreignKey)); } /** * Make a new related instance for the given model. * * @param \Illuminate\Database\Eloquent\Model $parent * @return \Illuminate\Database\Eloquent\Model */ public function newRelatedInstanceFor(Model $parent) { return $this->related->newInstance() ->setAttribute($this->getForeignKeyName(), $parent->{$this->localKey}) ->setAttribute($this->getMorphType(), $this->morphClass); } /** * Get the value of the model's foreign key. * * @param \Illuminate\Database\Eloquent\Model $model * @return mixed */ protected function getRelatedKeyFrom(Model $model) { return $model->getAttribute($this->getForeignKeyName()); } } src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php 0000644 00000123463 15107327037 0020146 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use Closure; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithPivotTable; use Illuminate\Database\UniqueConstraintViolationException; use Illuminate\Support\Str; use InvalidArgumentException; class BelongsToMany extends Relation { use InteractsWithDictionary, InteractsWithPivotTable; /** * The intermediate table for the relation. * * @var string */ protected $table; /** * The foreign key of the parent model. * * @var string */ protected $foreignPivotKey; /** * The associated key of the relation. * * @var string */ protected $relatedPivotKey; /** * The key name of the parent model. * * @var string */ protected $parentKey; /** * The key name of the related model. * * @var string */ protected $relatedKey; /** * The "name" of the relationship. * * @var string */ protected $relationName; /** * The pivot table columns to retrieve. * * @var array */ protected $pivotColumns = []; /** * Any pivot table restrictions for where clauses. * * @var array */ protected $pivotWheres = []; /** * Any pivot table restrictions for whereIn clauses. * * @var array */ protected $pivotWhereIns = []; /** * Any pivot table restrictions for whereNull clauses. * * @var array */ protected $pivotWhereNulls = []; /** * The default values for the pivot columns. * * @var array */ protected $pivotValues = []; /** * Indicates if timestamps are available on the pivot table. * * @var bool */ public $withTimestamps = false; /** * The custom pivot table column for the created_at timestamp. * * @var string */ protected $pivotCreatedAt; /** * The custom pivot table column for the updated_at timestamp. * * @var string */ protected $pivotUpdatedAt; /** * The class name of the custom pivot model to use for the relationship. * * @var string */ protected $using; /** * The name of the accessor to use for the "pivot" relationship. * * @var string */ protected $accessor = 'pivot'; /** * Create a new belongs to many relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string|class-string<\Illuminate\Database\Eloquent\Model> $table * @param string $foreignPivotKey * @param string $relatedPivotKey * @param string $parentKey * @param string $relatedKey * @param string|null $relationName * @return void */ public function __construct(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null) { $this->parentKey = $parentKey; $this->relatedKey = $relatedKey; $this->relationName = $relationName; $this->relatedPivotKey = $relatedPivotKey; $this->foreignPivotKey = $foreignPivotKey; $this->table = $this->resolveTableName($table); parent::__construct($query, $parent); } /** * Attempt to resolve the intermediate table name from the given string. * * @param string $table * @return string */ protected function resolveTableName($table) { if (! str_contains($table, '\\') || ! class_exists($table)) { return $table; } $model = new $table; if (! $model instanceof Model) { return $table; } if (in_array(AsPivot::class, class_uses_recursive($model))) { $this->using($table); } return $model->getTable(); } /** * Set the base constraints on the relation query. * * @return void */ public function addConstraints() { $this->performJoin(); if (static::$constraints) { $this->addWhereConstraints(); } } /** * Set the join clause for the relation query. * * @param \Illuminate\Database\Eloquent\Builder|null $query * @return $this */ protected function performJoin($query = null) { $query = $query ?: $this->query; // We need to join to the intermediate table on the related model's primary // key column with the intermediate table's foreign key for the related // model instance. Then we can set the "where" for the parent models. $query->join( $this->table, $this->getQualifiedRelatedKeyName(), '=', $this->getQualifiedRelatedPivotKeyName() ); return $this; } /** * Set the where clause for the relation query. * * @return $this */ protected function addWhereConstraints() { $this->query->where( $this->getQualifiedForeignPivotKeyName(), '=', $this->parent->{$this->parentKey} ); return $this; } /** * Set the constraints for an eager load of the relation. * * @param array $models * @return void */ public function addEagerConstraints(array $models) { $whereIn = $this->whereInMethod($this->parent, $this->parentKey); $this->whereInEager( $whereIn, $this->getQualifiedForeignPivotKeyName(), $this->getKeys($models, $this->parentKey) ); } /** * Initialize the relation on a set of models. * * @param array $models * @param string $relation * @return array */ public function initRelation(array $models, $relation) { foreach ($models as $model) { $model->setRelation($relation, $this->related->newCollection()); } return $models; } /** * Match the eagerly loaded results to their parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array */ public function match(array $models, Collection $results, $relation) { $dictionary = $this->buildDictionary($results); // Once we have an array dictionary of child objects we can easily match the // children back to their parent using the dictionary and the keys on the // parent models. Then we should return these hydrated models back out. foreach ($models as $model) { $key = $this->getDictionaryKey($model->{$this->parentKey}); if (isset($dictionary[$key])) { $model->setRelation( $relation, $this->related->newCollection($dictionary[$key]) ); } } return $models; } /** * Build model dictionary keyed by the relation's foreign key. * * @param \Illuminate\Database\Eloquent\Collection $results * @return array */ protected function buildDictionary(Collection $results) { // First we'll build a dictionary of child models keyed by the foreign key // of the relation so that we will easily and quickly match them to the // parents without having a possibly slow inner loop for every model. $dictionary = []; foreach ($results as $result) { $value = $this->getDictionaryKey($result->{$this->accessor}->{$this->foreignPivotKey}); $dictionary[$value][] = $result; } return $dictionary; } /** * Get the class being used for pivot models. * * @return string */ public function getPivotClass() { return $this->using ?? Pivot::class; } /** * Specify the custom pivot model to use for the relationship. * * @param string $class * @return $this */ public function using($class) { $this->using = $class; return $this; } /** * Specify the custom pivot accessor to use for the relationship. * * @param string $accessor * @return $this */ public function as($accessor) { $this->accessor = $accessor; return $this; } /** * Set a where clause for a pivot table column. * * @param string $column * @param mixed $operator * @param mixed $value * @param string $boolean * @return $this */ public function wherePivot($column, $operator = null, $value = null, $boolean = 'and') { $this->pivotWheres[] = func_get_args(); return $this->where($this->qualifyPivotColumn($column), $operator, $value, $boolean); } /** * Set a "where between" clause for a pivot table column. * * @param string $column * @param array $values * @param string $boolean * @param bool $not * @return $this */ public function wherePivotBetween($column, array $values, $boolean = 'and', $not = false) { return $this->whereBetween($this->qualifyPivotColumn($column), $values, $boolean, $not); } /** * Set a "or where between" clause for a pivot table column. * * @param string $column * @param array $values * @return $this */ public function orWherePivotBetween($column, array $values) { return $this->wherePivotBetween($column, $values, 'or'); } /** * Set a "where pivot not between" clause for a pivot table column. * * @param string $column * @param array $values * @param string $boolean * @return $this */ public function wherePivotNotBetween($column, array $values, $boolean = 'and') { return $this->wherePivotBetween($column, $values, $boolean, true); } /** * Set a "or where not between" clause for a pivot table column. * * @param string $column * @param array $values * @return $this */ public function orWherePivotNotBetween($column, array $values) { return $this->wherePivotBetween($column, $values, 'or', true); } /** * Set a "where in" clause for a pivot table column. * * @param string $column * @param mixed $values * @param string $boolean * @param bool $not * @return $this */ public function wherePivotIn($column, $values, $boolean = 'and', $not = false) { $this->pivotWhereIns[] = func_get_args(); return $this->whereIn($this->qualifyPivotColumn($column), $values, $boolean, $not); } /** * Set an "or where" clause for a pivot table column. * * @param string $column * @param mixed $operator * @param mixed $value * @return $this */ public function orWherePivot($column, $operator = null, $value = null) { return $this->wherePivot($column, $operator, $value, 'or'); } /** * Set a where clause for a pivot table column. * * In addition, new pivot records will receive this value. * * @param string|array $column * @param mixed $value * @return $this * * @throws \InvalidArgumentException */ public function withPivotValue($column, $value = null) { if (is_array($column)) { foreach ($column as $name => $value) { $this->withPivotValue($name, $value); } return $this; } if (is_null($value)) { throw new InvalidArgumentException('The provided value may not be null.'); } $this->pivotValues[] = compact('column', 'value'); return $this->wherePivot($column, '=', $value); } /** * Set an "or where in" clause for a pivot table column. * * @param string $column * @param mixed $values * @return $this */ public function orWherePivotIn($column, $values) { return $this->wherePivotIn($column, $values, 'or'); } /** * Set a "where not in" clause for a pivot table column. * * @param string $column * @param mixed $values * @param string $boolean * @return $this */ public function wherePivotNotIn($column, $values, $boolean = 'and') { return $this->wherePivotIn($column, $values, $boolean, true); } /** * Set an "or where not in" clause for a pivot table column. * * @param string $column * @param mixed $values * @return $this */ public function orWherePivotNotIn($column, $values) { return $this->wherePivotNotIn($column, $values, 'or'); } /** * Set a "where null" clause for a pivot table column. * * @param string $column * @param string $boolean * @param bool $not * @return $this */ public function wherePivotNull($column, $boolean = 'and', $not = false) { $this->pivotWhereNulls[] = func_get_args(); return $this->whereNull($this->qualifyPivotColumn($column), $boolean, $not); } /** * Set a "where not null" clause for a pivot table column. * * @param string $column * @param string $boolean * @return $this */ public function wherePivotNotNull($column, $boolean = 'and') { return $this->wherePivotNull($column, $boolean, true); } /** * Set a "or where null" clause for a pivot table column. * * @param string $column * @param bool $not * @return $this */ public function orWherePivotNull($column, $not = false) { return $this->wherePivotNull($column, 'or', $not); } /** * Set a "or where not null" clause for a pivot table column. * * @param string $column * @return $this */ public function orWherePivotNotNull($column) { return $this->orWherePivotNull($column, true); } /** * Add an "order by" clause for a pivot table column. * * @param string $column * @param string $direction * @return $this */ public function orderByPivot($column, $direction = 'asc') { return $this->orderBy($this->qualifyPivotColumn($column), $direction); } /** * Find a related model by its primary key or return a new instance of the related model. * * @param mixed $id * @param array $columns * @return \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model */ public function findOrNew($id, $columns = ['*']) { if (is_null($instance = $this->find($id, $columns))) { $instance = $this->related->newInstance(); } return $instance; } /** * Get the first related model record matching the attributes or instantiate it. * * @param array $attributes * @param array $values * @return \Illuminate\Database\Eloquent\Model */ public function firstOrNew(array $attributes = [], array $values = []) { if (is_null($instance = $this->related->where($attributes)->first())) { $instance = $this->related->newInstance(array_merge($attributes, $values)); } return $instance; } /** * Get the first record matching the attributes. If the record is not found, create it. * * @param array $attributes * @param array $values * @param array $joining * @param bool $touch * @return \Illuminate\Database\Eloquent\Model */ public function firstOrCreate(array $attributes = [], array $values = [], array $joining = [], $touch = true) { if (is_null($instance = (clone $this)->where($attributes)->first())) { if (is_null($instance = $this->related->where($attributes)->first())) { $instance = $this->createOrFirst($attributes, $values, $joining, $touch); } else { try { $this->getQuery()->withSavepointIfNeeded(fn () => $this->attach($instance, $joining, $touch)); } catch (UniqueConstraintViolationException) { // Nothing to do, the model was already attached... } } } return $instance; } /** * Attempt to create the record. If a unique constraint violation occurs, attempt to find the matching record. * * @param array $attributes * @param array $values * @param array $joining * @param bool $touch * @return \Illuminate\Database\Eloquent\Model */ public function createOrFirst(array $attributes = [], array $values = [], array $joining = [], $touch = true) { try { return $this->getQuery()->withSavePointIfNeeded(fn () => $this->create(array_merge($attributes, $values), $joining, $touch)); } catch (UniqueConstraintViolationException $e) { // ... } try { return tap($this->related->where($attributes)->first() ?? throw $e, function ($instance) use ($joining, $touch) { $this->getQuery()->withSavepointIfNeeded(fn () => $this->attach($instance, $joining, $touch)); }); } catch (UniqueConstraintViolationException $e) { return (clone $this)->useWritePdo()->where($attributes)->first() ?? throw $e; } } /** * Create or update a related record matching the attributes, and fill it with values. * * @param array $attributes * @param array $values * @param array $joining * @param bool $touch * @return \Illuminate\Database\Eloquent\Model */ public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true) { return tap($this->firstOrCreate($attributes, $values, $joining, $touch), function ($instance) use ($values) { if (! $instance->wasRecentlyCreated) { $instance->fill($values); $instance->save(['touch' => false]); } }); } /** * Find a related model by its primary key. * * @param mixed $id * @param array $columns * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|null */ public function find($id, $columns = ['*']) { if (! $id instanceof Model && (is_array($id) || $id instanceof Arrayable)) { return $this->findMany($id, $columns); } return $this->where( $this->getRelated()->getQualifiedKeyName(), '=', $this->parseId($id) )->first($columns); } /** * Find multiple related models by their primary keys. * * @param \Illuminate\Contracts\Support\Arrayable|array $ids * @param array $columns * @return \Illuminate\Database\Eloquent\Collection */ public function findMany($ids, $columns = ['*']) { $ids = $ids instanceof Arrayable ? $ids->toArray() : $ids; if (empty($ids)) { return $this->getRelated()->newCollection(); } return $this->whereKey( $this->parseIds($ids) )->get($columns); } /** * Find a related model by its primary key or throw an exception. * * @param mixed $id * @param array $columns * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public function findOrFail($id, $columns = ['*']) { $result = $this->find($id, $columns); $id = $id instanceof Arrayable ? $id->toArray() : $id; if (is_array($id)) { if (count($result) === count(array_unique($id))) { return $result; } } elseif (! is_null($result)) { return $result; } throw (new ModelNotFoundException)->setModel(get_class($this->related), $id); } /** * Find a related model by its primary key or call a callback. * * @param mixed $id * @param \Closure|array $columns * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|mixed */ public function findOr($id, $columns = ['*'], Closure $callback = null) { if ($columns instanceof Closure) { $callback = $columns; $columns = ['*']; } $result = $this->find($id, $columns); $id = $id instanceof Arrayable ? $id->toArray() : $id; if (is_array($id)) { if (count($result) === count(array_unique($id))) { return $result; } } elseif (! is_null($result)) { return $result; } return $callback(); } /** * Add a basic where clause to the query, and return the first result. * * @param \Closure|string|array $column * @param mixed $operator * @param mixed $value * @param string $boolean * @return \Illuminate\Database\Eloquent\Model|static */ public function firstWhere($column, $operator = null, $value = null, $boolean = 'and') { return $this->where($column, $operator, $value, $boolean)->first(); } /** * Execute the query and get the first result. * * @param array $columns * @return mixed */ public function first($columns = ['*']) { $results = $this->take(1)->get($columns); return count($results) > 0 ? $results->first() : null; } /** * Execute the query and get the first result or throw an exception. * * @param array $columns * @return \Illuminate\Database\Eloquent\Model|static * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public function firstOrFail($columns = ['*']) { if (! is_null($model = $this->first($columns))) { return $model; } throw (new ModelNotFoundException)->setModel(get_class($this->related)); } /** * Execute the query and get the first result or call a callback. * * @param \Closure|array $columns * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Model|static|mixed */ public function firstOr($columns = ['*'], Closure $callback = null) { if ($columns instanceof Closure) { $callback = $columns; $columns = ['*']; } if (! is_null($model = $this->first($columns))) { return $model; } return $callback(); } /** * Get the results of the relationship. * * @return mixed */ public function getResults() { return ! is_null($this->parent->{$this->parentKey}) ? $this->get() : $this->related->newCollection(); } /** * Execute the query as a "select" statement. * * @param array $columns * @return \Illuminate\Database\Eloquent\Collection */ public function get($columns = ['*']) { // First we'll add the proper select columns onto the query so it is run with // the proper columns. Then, we will get the results and hydrate our pivot // models with the result of those columns as a separate model relation. $builder = $this->query->applyScopes(); $columns = $builder->getQuery()->columns ? [] : $columns; $models = $builder->addSelect( $this->shouldSelect($columns) )->getModels(); $this->hydratePivotRelation($models); // If we actually found models we will also eager load any relationships that // have been specified as needing to be eager loaded. This will solve the // n + 1 query problem for the developer and also increase performance. if (count($models) > 0) { $models = $builder->eagerLoadRelations($models); } return $this->related->newCollection($models); } /** * Get the select columns for the relation query. * * @param array $columns * @return array */ protected function shouldSelect(array $columns = ['*']) { if ($columns == ['*']) { $columns = [$this->related->getTable().'.*']; } return array_merge($columns, $this->aliasedPivotColumns()); } /** * Get the pivot columns for the relation. * * "pivot_" is prefixed at each column for easy removal later. * * @return array */ protected function aliasedPivotColumns() { $defaults = [$this->foreignPivotKey, $this->relatedPivotKey]; return collect(array_merge($defaults, $this->pivotColumns))->map(function ($column) { return $this->qualifyPivotColumn($column).' as pivot_'.$column; })->unique()->all(); } /** * Get a paginator for the "select" statement. * * @param int|null $perPage * @param array $columns * @param string $pageName * @param int|null $page * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) { $this->query->addSelect($this->shouldSelect($columns)); return tap($this->query->paginate($perPage, $columns, $pageName, $page), function ($paginator) { $this->hydratePivotRelation($paginator->items()); }); } /** * Paginate the given query into a simple paginator. * * @param int|null $perPage * @param array $columns * @param string $pageName * @param int|null $page * @return \Illuminate\Contracts\Pagination\Paginator */ public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) { $this->query->addSelect($this->shouldSelect($columns)); return tap($this->query->simplePaginate($perPage, $columns, $pageName, $page), function ($paginator) { $this->hydratePivotRelation($paginator->items()); }); } /** * Paginate the given query into a cursor paginator. * * @param int|null $perPage * @param array $columns * @param string $cursorName * @param string|null $cursor * @return \Illuminate\Contracts\Pagination\CursorPaginator */ public function cursorPaginate($perPage = null, $columns = ['*'], $cursorName = 'cursor', $cursor = null) { $this->query->addSelect($this->shouldSelect($columns)); return tap($this->query->cursorPaginate($perPage, $columns, $cursorName, $cursor), function ($paginator) { $this->hydratePivotRelation($paginator->items()); }); } /** * Chunk the results of the query. * * @param int $count * @param callable $callback * @return bool */ public function chunk($count, callable $callback) { return $this->prepareQueryBuilder()->chunk($count, function ($results, $page) use ($callback) { $this->hydratePivotRelation($results->all()); return $callback($results, $page); }); } /** * Chunk the results of a query by comparing numeric IDs. * * @param int $count * @param callable $callback * @param string|null $column * @param string|null $alias * @return bool */ public function chunkById($count, callable $callback, $column = null, $alias = null) { $this->prepareQueryBuilder(); $column ??= $this->getRelated()->qualifyColumn( $this->getRelatedKeyName() ); $alias ??= $this->getRelatedKeyName(); return $this->query->chunkById($count, function ($results) use ($callback) { $this->hydratePivotRelation($results->all()); return $callback($results); }, $column, $alias); } /** * Execute a callback over each item while chunking. * * @param callable $callback * @param int $count * @return bool */ public function each(callable $callback, $count = 1000) { return $this->chunk($count, function ($results) use ($callback) { foreach ($results as $key => $value) { if ($callback($value, $key) === false) { return false; } } }); } /** * Query lazily, by chunks of the given size. * * @param int $chunkSize * @return \Illuminate\Support\LazyCollection */ public function lazy($chunkSize = 1000) { return $this->prepareQueryBuilder()->lazy($chunkSize)->map(function ($model) { $this->hydratePivotRelation([$model]); return $model; }); } /** * Query lazily, by chunking the results of a query by comparing IDs. * * @param int $chunkSize * @param string|null $column * @param string|null $alias * @return \Illuminate\Support\LazyCollection */ public function lazyById($chunkSize = 1000, $column = null, $alias = null) { $column ??= $this->getRelated()->qualifyColumn( $this->getRelatedKeyName() ); $alias ??= $this->getRelatedKeyName(); return $this->prepareQueryBuilder()->lazyById($chunkSize, $column, $alias)->map(function ($model) { $this->hydratePivotRelation([$model]); return $model; }); } /** * Get a lazy collection for the given query. * * @return \Illuminate\Support\LazyCollection */ public function cursor() { return $this->prepareQueryBuilder()->cursor()->map(function ($model) { $this->hydratePivotRelation([$model]); return $model; }); } /** * Prepare the query builder for query execution. * * @return \Illuminate\Database\Eloquent\Builder */ protected function prepareQueryBuilder() { return $this->query->addSelect($this->shouldSelect()); } /** * Hydrate the pivot table relationship on the models. * * @param array $models * @return void */ protected function hydratePivotRelation(array $models) { // To hydrate the pivot relationship, we will just gather the pivot attributes // and create a new Pivot model, which is basically a dynamic model that we // will set the attributes, table, and connections on it so it will work. foreach ($models as $model) { $model->setRelation($this->accessor, $this->newExistingPivot( $this->migratePivotAttributes($model) )); } } /** * Get the pivot attributes from a model. * * @param \Illuminate\Database\Eloquent\Model $model * @return array */ protected function migratePivotAttributes(Model $model) { $values = []; foreach ($model->getAttributes() as $key => $value) { // To get the pivots attributes we will just take any of the attributes which // begin with "pivot_" and add those to this arrays, as well as unsetting // them from the parent's models since they exist in a different table. if (str_starts_with($key, 'pivot_')) { $values[substr($key, 6)] = $value; unset($model->$key); } } return $values; } /** * If we're touching the parent model, touch. * * @return void */ public function touchIfTouching() { if ($this->touchingParent()) { $this->getParent()->touch(); } if ($this->getParent()->touches($this->relationName)) { $this->touch(); } } /** * Determine if we should touch the parent on sync. * * @return bool */ protected function touchingParent() { return $this->getRelated()->touches($this->guessInverseRelation()); } /** * Attempt to guess the name of the inverse of the relation. * * @return string */ protected function guessInverseRelation() { return Str::camel(Str::pluralStudly(class_basename($this->getParent()))); } /** * Touch all of the related models for the relationship. * * E.g.: Touch all roles associated with this user. * * @return void */ public function touch() { $columns = [ $this->related->getUpdatedAtColumn() => $this->related->freshTimestampString(), ]; // If we actually have IDs for the relation, we will run the query to update all // the related model's timestamps, to make sure these all reflect the changes // to the parent models. This will help us keep any caching synced up here. if (count($ids = $this->allRelatedIds()) > 0) { $this->getRelated()->newQueryWithoutRelationships()->whereKey($ids)->update($columns); } } /** * Get all of the IDs for the related models. * * @return \Illuminate\Support\Collection */ public function allRelatedIds() { return $this->newPivotQuery()->pluck($this->relatedPivotKey); } /** * Save a new model and attach it to the parent model. * * @param \Illuminate\Database\Eloquent\Model $model * @param array $pivotAttributes * @param bool $touch * @return \Illuminate\Database\Eloquent\Model */ public function save(Model $model, array $pivotAttributes = [], $touch = true) { $model->save(['touch' => false]); $this->attach($model, $pivotAttributes, $touch); return $model; } /** * Save a new model without raising any events and attach it to the parent model. * * @param \Illuminate\Database\Eloquent\Model $model * @param array $pivotAttributes * @param bool $touch * @return \Illuminate\Database\Eloquent\Model */ public function saveQuietly(Model $model, array $pivotAttributes = [], $touch = true) { return Model::withoutEvents(function () use ($model, $pivotAttributes, $touch) { return $this->save($model, $pivotAttributes, $touch); }); } /** * Save an array of new models and attach them to the parent model. * * @param \Illuminate\Support\Collection|array $models * @param array $pivotAttributes * @return array */ public function saveMany($models, array $pivotAttributes = []) { foreach ($models as $key => $model) { $this->save($model, (array) ($pivotAttributes[$key] ?? []), false); } $this->touchIfTouching(); return $models; } /** * Save an array of new models without raising any events and attach them to the parent model. * * @param \Illuminate\Support\Collection|array $models * @param array $pivotAttributes * @return array */ public function saveManyQuietly($models, array $pivotAttributes = []) { return Model::withoutEvents(function () use ($models, $pivotAttributes) { return $this->saveMany($models, $pivotAttributes); }); } /** * Create a new instance of the related model. * * @param array $attributes * @param array $joining * @param bool $touch * @return \Illuminate\Database\Eloquent\Model */ public function create(array $attributes = [], array $joining = [], $touch = true) { $instance = $this->related->newInstance($attributes); // Once we save the related model, we need to attach it to the base model via // through intermediate table so we'll use the existing "attach" method to // accomplish this which will insert the record and any more attributes. $instance->save(['touch' => false]); $this->attach($instance, $joining, $touch); return $instance; } /** * Create an array of new instances of the related models. * * @param iterable $records * @param array $joinings * @return array */ public function createMany(iterable $records, array $joinings = []) { $instances = []; foreach ($records as $key => $record) { $instances[] = $this->create($record, (array) ($joinings[$key] ?? []), false); } $this->touchIfTouching(); return $instances; } /** * Add the constraints for a relationship query. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) { if ($parentQuery->getQuery()->from == $query->getQuery()->from) { return $this->getRelationExistenceQueryForSelfJoin($query, $parentQuery, $columns); } $this->performJoin($query); return parent::getRelationExistenceQuery($query, $parentQuery, $columns); } /** * Add the constraints for a relationship query on the same table. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQueryForSelfJoin(Builder $query, Builder $parentQuery, $columns = ['*']) { $query->select($columns); $query->from($this->related->getTable().' as '.$hash = $this->getRelationCountHash()); $this->related->setTable($hash); $this->performJoin($query); return parent::getRelationExistenceQuery($query, $parentQuery, $columns); } /** * Get the key for comparing against the parent key in "has" query. * * @return string */ public function getExistenceCompareKey() { return $this->getQualifiedForeignPivotKeyName(); } /** * Specify that the pivot table has creation and update timestamps. * * @param mixed $createdAt * @param mixed $updatedAt * @return $this */ public function withTimestamps($createdAt = null, $updatedAt = null) { $this->withTimestamps = true; $this->pivotCreatedAt = $createdAt; $this->pivotUpdatedAt = $updatedAt; return $this->withPivot($this->createdAt(), $this->updatedAt()); } /** * Get the name of the "created at" column. * * @return string */ public function createdAt() { return $this->pivotCreatedAt ?: $this->parent->getCreatedAtColumn(); } /** * Get the name of the "updated at" column. * * @return string */ public function updatedAt() { return $this->pivotUpdatedAt ?: $this->parent->getUpdatedAtColumn(); } /** * Get the foreign key for the relation. * * @return string */ public function getForeignPivotKeyName() { return $this->foreignPivotKey; } /** * Get the fully qualified foreign key for the relation. * * @return string */ public function getQualifiedForeignPivotKeyName() { return $this->qualifyPivotColumn($this->foreignPivotKey); } /** * Get the "related key" for the relation. * * @return string */ public function getRelatedPivotKeyName() { return $this->relatedPivotKey; } /** * Get the fully qualified "related key" for the relation. * * @return string */ public function getQualifiedRelatedPivotKeyName() { return $this->qualifyPivotColumn($this->relatedPivotKey); } /** * Get the parent key for the relationship. * * @return string */ public function getParentKeyName() { return $this->parentKey; } /** * Get the fully qualified parent key name for the relation. * * @return string */ public function getQualifiedParentKeyName() { return $this->parent->qualifyColumn($this->parentKey); } /** * Get the related key for the relationship. * * @return string */ public function getRelatedKeyName() { return $this->relatedKey; } /** * Get the fully qualified related key name for the relation. * * @return string */ public function getQualifiedRelatedKeyName() { return $this->related->qualifyColumn($this->relatedKey); } /** * Get the intermediate table for the relationship. * * @return string */ public function getTable() { return $this->table; } /** * Get the relationship name for the relationship. * * @return string */ public function getRelationName() { return $this->relationName; } /** * Get the name of the pivot accessor for this relationship. * * @return string */ public function getPivotAccessor() { return $this->accessor; } /** * Get the pivot columns for this relationship. * * @return array */ public function getPivotColumns() { return $this->pivotColumns; } /** * Qualify the given column name by the pivot table. * * @param string $column * @return string */ public function qualifyPivotColumn($column) { return str_contains($column, '.') ? $column : $this->table.'.'.$column; } } src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php 0000644 00000006775 15107327037 0020310 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; abstract class MorphOneOrMany extends HasOneOrMany { /** * The foreign key type for the relationship. * * @var string */ protected $morphType; /** * The class name of the parent model. * * @var string */ protected $morphClass; /** * Create a new morph one or many relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $type * @param string $id * @param string $localKey * @return void */ public function __construct(Builder $query, Model $parent, $type, $id, $localKey) { $this->morphType = $type; $this->morphClass = $parent->getMorphClass(); parent::__construct($query, $parent, $id, $localKey); } /** * Set the base constraints on the relation query. * * @return void */ public function addConstraints() { if (static::$constraints) { $this->getRelationQuery()->where($this->morphType, $this->morphClass); parent::addConstraints(); } } /** * Set the constraints for an eager load of the relation. * * @param array $models * @return void */ public function addEagerConstraints(array $models) { parent::addEagerConstraints($models); $this->getRelationQuery()->where($this->morphType, $this->morphClass); } /** * Create a new instance of the related model. Allow mass-assignment. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Model */ public function forceCreate(array $attributes = []) { $attributes[$this->getForeignKeyName()] = $this->getParentKey(); $attributes[$this->getMorphType()] = $this->morphClass; return $this->related->forceCreate($attributes); } /** * Set the foreign ID and type for creating a related model. * * @param \Illuminate\Database\Eloquent\Model $model * @return void */ protected function setForeignAttributesForCreate(Model $model) { $model->{$this->getForeignKeyName()} = $this->getParentKey(); $model->{$this->getMorphType()} = $this->morphClass; } /** * Get the relationship query. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) { return parent::getRelationExistenceQuery($query, $parentQuery, $columns)->where( $query->qualifyColumn($this->getMorphType()), $this->morphClass ); } /** * Get the foreign key "type" name. * * @return string */ public function getQualifiedMorphType() { return $this->morphType; } /** * Get the plain morph type name without the table. * * @return string */ public function getMorphType() { return last(explode('.', $this->morphType)); } /** * Get the class name of the parent model. * * @return string */ public function getMorphClass() { return $this->morphClass; } } src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php 0000644 00000060365 15107327037 0020327 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use Closure; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\UniqueConstraintViolationException; class HasManyThrough extends Relation { use InteractsWithDictionary; /** * The "through" parent model instance. * * @var \Illuminate\Database\Eloquent\Model */ protected $throughParent; /** * The far parent model instance. * * @var \Illuminate\Database\Eloquent\Model */ protected $farParent; /** * The near key on the relationship. * * @var string */ protected $firstKey; /** * The far key on the relationship. * * @var string */ protected $secondKey; /** * The local key on the relationship. * * @var string */ protected $localKey; /** * The local key on the intermediary model. * * @var string */ protected $secondLocalKey; /** * Create a new has many through relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $farParent * @param \Illuminate\Database\Eloquent\Model $throughParent * @param string $firstKey * @param string $secondKey * @param string $localKey * @param string $secondLocalKey * @return void */ public function __construct(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) { $this->localKey = $localKey; $this->firstKey = $firstKey; $this->secondKey = $secondKey; $this->farParent = $farParent; $this->throughParent = $throughParent; $this->secondLocalKey = $secondLocalKey; parent::__construct($query, $throughParent); } /** * Convert the relationship to a "has one through" relationship. * * @return \Illuminate\Database\Eloquent\Relations\HasOneThrough */ public function one() { return HasOneThrough::noConstraints(fn () => new HasOneThrough( $this->getQuery(), $this->farParent, $this->throughParent, $this->getFirstKeyName(), $this->secondKey, $this->getLocalKeyName(), $this->getSecondLocalKeyName(), )); } /** * Set the base constraints on the relation query. * * @return void */ public function addConstraints() { $localValue = $this->farParent[$this->localKey]; $this->performJoin(); if (static::$constraints) { $this->query->where($this->getQualifiedFirstKeyName(), '=', $localValue); } } /** * Set the join clause on the query. * * @param \Illuminate\Database\Eloquent\Builder|null $query * @return void */ protected function performJoin(Builder $query = null) { $query = $query ?: $this->query; $farKey = $this->getQualifiedFarKeyName(); $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $farKey); if ($this->throughParentSoftDeletes()) { $query->withGlobalScope('SoftDeletableHasManyThrough', function ($query) { $query->whereNull($this->throughParent->getQualifiedDeletedAtColumn()); }); } } /** * Get the fully qualified parent key name. * * @return string */ public function getQualifiedParentKeyName() { return $this->parent->qualifyColumn($this->secondLocalKey); } /** * Determine whether "through" parent of the relation uses Soft Deletes. * * @return bool */ public function throughParentSoftDeletes() { return in_array(SoftDeletes::class, class_uses_recursive($this->throughParent)); } /** * Indicate that trashed "through" parents should be included in the query. * * @return $this */ public function withTrashedParents() { $this->query->withoutGlobalScope('SoftDeletableHasManyThrough'); return $this; } /** * Set the constraints for an eager load of the relation. * * @param array $models * @return void */ public function addEagerConstraints(array $models) { $whereIn = $this->whereInMethod($this->farParent, $this->localKey); $this->whereInEager( $whereIn, $this->getQualifiedFirstKeyName(), $this->getKeys($models, $this->localKey) ); } /** * Initialize the relation on a set of models. * * @param array $models * @param string $relation * @return array */ public function initRelation(array $models, $relation) { foreach ($models as $model) { $model->setRelation($relation, $this->related->newCollection()); } return $models; } /** * Match the eagerly loaded results to their parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array */ public function match(array $models, Collection $results, $relation) { $dictionary = $this->buildDictionary($results); // Once we have the dictionary we can simply spin through the parent models to // link them up with their children using the keyed dictionary to make the // matching very convenient and easy work. Then we'll just return them. foreach ($models as $model) { if (isset($dictionary[$key = $this->getDictionaryKey($model->getAttribute($this->localKey))])) { $model->setRelation( $relation, $this->related->newCollection($dictionary[$key]) ); } } return $models; } /** * Build model dictionary keyed by the relation's foreign key. * * @param \Illuminate\Database\Eloquent\Collection $results * @return array */ protected function buildDictionary(Collection $results) { $dictionary = []; // First we will create a dictionary of models keyed by the foreign key of the // relationship as this will allow us to quickly access all of the related // models without having to do nested looping which will be quite slow. foreach ($results as $result) { $dictionary[$result->laravel_through_key][] = $result; } return $dictionary; } /** * Get the first related model record matching the attributes or instantiate it. * * @param array $attributes * @param array $values * @return \Illuminate\Database\Eloquent\Model */ public function firstOrNew(array $attributes = [], array $values = []) { if (! is_null($instance = $this->where($attributes)->first())) { return $instance; } return $this->related->newInstance(array_merge($attributes, $values)); } /** * Get the first record matching the attributes. If the record is not found, create it. * * @param array $attributes * @param array $values * @return \Illuminate\Database\Eloquent\Model */ public function firstOrCreate(array $attributes = [], array $values = []) { if (! is_null($instance = (clone $this)->where($attributes)->first())) { return $instance; } return $this->createOrFirst(array_merge($attributes, $values)); } /** * Attempt to create the record. If a unique constraint violation occurs, attempt to find the matching record. * * @param array $attributes * @param array $values * @return \Illuminate\Database\Eloquent\Model */ public function createOrFirst(array $attributes = [], array $values = []) { try { return $this->getQuery()->withSavepointIfNeeded(fn () => $this->create(array_merge($attributes, $values))); } catch (UniqueConstraintViolationException $exception) { return $this->where($attributes)->first() ?? throw $exception; } } /** * Create or update a related record matching the attributes, and fill it with values. * * @param array $attributes * @param array $values * @return \Illuminate\Database\Eloquent\Model */ public function updateOrCreate(array $attributes, array $values = []) { return tap($this->firstOrCreate($attributes, $values), function ($instance) use ($values) { if (! $instance->wasRecentlyCreated) { $instance->fill($values)->save(); } }); } /** * Add a basic where clause to the query, and return the first result. * * @param \Closure|string|array $column * @param mixed $operator * @param mixed $value * @param string $boolean * @return \Illuminate\Database\Eloquent\Model|static */ public function firstWhere($column, $operator = null, $value = null, $boolean = 'and') { return $this->where($column, $operator, $value, $boolean)->first(); } /** * Execute the query and get the first related model. * * @param array $columns * @return mixed */ public function first($columns = ['*']) { $results = $this->take(1)->get($columns); return count($results) > 0 ? $results->first() : null; } /** * Execute the query and get the first result or throw an exception. * * @param array $columns * @return \Illuminate\Database\Eloquent\Model|static * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public function firstOrFail($columns = ['*']) { if (! is_null($model = $this->first($columns))) { return $model; } throw (new ModelNotFoundException)->setModel(get_class($this->related)); } /** * Execute the query and get the first result or call a callback. * * @param \Closure|array $columns * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Model|static|mixed */ public function firstOr($columns = ['*'], Closure $callback = null) { if ($columns instanceof Closure) { $callback = $columns; $columns = ['*']; } if (! is_null($model = $this->first($columns))) { return $model; } return $callback(); } /** * Find a related model by its primary key. * * @param mixed $id * @param array $columns * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|null */ public function find($id, $columns = ['*']) { if (is_array($id) || $id instanceof Arrayable) { return $this->findMany($id, $columns); } return $this->where( $this->getRelated()->getQualifiedKeyName(), '=', $id )->first($columns); } /** * Find multiple related models by their primary keys. * * @param \Illuminate\Contracts\Support\Arrayable|array $ids * @param array $columns * @return \Illuminate\Database\Eloquent\Collection */ public function findMany($ids, $columns = ['*']) { $ids = $ids instanceof Arrayable ? $ids->toArray() : $ids; if (empty($ids)) { return $this->getRelated()->newCollection(); } return $this->whereIn( $this->getRelated()->getQualifiedKeyName(), $ids )->get($columns); } /** * Find a related model by its primary key or throw an exception. * * @param mixed $id * @param array $columns * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model> */ public function findOrFail($id, $columns = ['*']) { $result = $this->find($id, $columns); $id = $id instanceof Arrayable ? $id->toArray() : $id; if (is_array($id)) { if (count($result) === count(array_unique($id))) { return $result; } } elseif (! is_null($result)) { return $result; } throw (new ModelNotFoundException)->setModel(get_class($this->related), $id); } /** * Find a related model by its primary key or call a callback. * * @param mixed $id * @param \Closure|array $columns * @param \Closure|null $callback * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|mixed */ public function findOr($id, $columns = ['*'], Closure $callback = null) { if ($columns instanceof Closure) { $callback = $columns; $columns = ['*']; } $result = $this->find($id, $columns); $id = $id instanceof Arrayable ? $id->toArray() : $id; if (is_array($id)) { if (count($result) === count(array_unique($id))) { return $result; } } elseif (! is_null($result)) { return $result; } return $callback(); } /** * Get the results of the relationship. * * @return mixed */ public function getResults() { return ! is_null($this->farParent->{$this->localKey}) ? $this->get() : $this->related->newCollection(); } /** * Execute the query as a "select" statement. * * @param array $columns * @return \Illuminate\Database\Eloquent\Collection */ public function get($columns = ['*']) { $builder = $this->prepareQueryBuilder($columns); $models = $builder->getModels(); // If we actually found models we will also eager load any relationships that // have been specified as needing to be eager loaded. This will solve the // n + 1 query problem for the developer and also increase performance. if (count($models) > 0) { $models = $builder->eagerLoadRelations($models); } return $this->related->newCollection($models); } /** * Get a paginator for the "select" statement. * * @param int|null $perPage * @param array $columns * @param string $pageName * @param int $page * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) { $this->query->addSelect($this->shouldSelect($columns)); return $this->query->paginate($perPage, $columns, $pageName, $page); } /** * Paginate the given query into a simple paginator. * * @param int|null $perPage * @param array $columns * @param string $pageName * @param int|null $page * @return \Illuminate\Contracts\Pagination\Paginator */ public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) { $this->query->addSelect($this->shouldSelect($columns)); return $this->query->simplePaginate($perPage, $columns, $pageName, $page); } /** * Paginate the given query into a cursor paginator. * * @param int|null $perPage * @param array $columns * @param string $cursorName * @param string|null $cursor * @return \Illuminate\Contracts\Pagination\CursorPaginator */ public function cursorPaginate($perPage = null, $columns = ['*'], $cursorName = 'cursor', $cursor = null) { $this->query->addSelect($this->shouldSelect($columns)); return $this->query->cursorPaginate($perPage, $columns, $cursorName, $cursor); } /** * Set the select clause for the relation query. * * @param array $columns * @return array */ protected function shouldSelect(array $columns = ['*']) { if ($columns == ['*']) { $columns = [$this->related->getTable().'.*']; } return array_merge($columns, [$this->getQualifiedFirstKeyName().' as laravel_through_key']); } /** * Chunk the results of the query. * * @param int $count * @param callable $callback * @return bool */ public function chunk($count, callable $callback) { return $this->prepareQueryBuilder()->chunk($count, $callback); } /** * Chunk the results of a query by comparing numeric IDs. * * @param int $count * @param callable $callback * @param string|null $column * @param string|null $alias * @return bool */ public function chunkById($count, callable $callback, $column = null, $alias = null) { $column ??= $this->getRelated()->getQualifiedKeyName(); $alias ??= $this->getRelated()->getKeyName(); return $this->prepareQueryBuilder()->chunkById($count, $callback, $column, $alias); } /** * Execute a callback over each item while chunking by ID. * * @param callable $callback * @param int $count * @param string|null $column * @param string|null $alias * @return bool */ public function eachById(callable $callback, $count = 1000, $column = null, $alias = null) { $column = $column ?? $this->getRelated()->getQualifiedKeyName(); $alias = $alias ?? $this->getRelated()->getKeyName(); return $this->prepareQueryBuilder()->eachById($callback, $count, $column, $alias); } /** * Get a generator for the given query. * * @return \Illuminate\Support\LazyCollection */ public function cursor() { return $this->prepareQueryBuilder()->cursor(); } /** * Execute a callback over each item while chunking. * * @param callable $callback * @param int $count * @return bool */ public function each(callable $callback, $count = 1000) { return $this->chunk($count, function ($results) use ($callback) { foreach ($results as $key => $value) { if ($callback($value, $key) === false) { return false; } } }); } /** * Query lazily, by chunks of the given size. * * @param int $chunkSize * @return \Illuminate\Support\LazyCollection */ public function lazy($chunkSize = 1000) { return $this->prepareQueryBuilder()->lazy($chunkSize); } /** * Query lazily, by chunking the results of a query by comparing IDs. * * @param int $chunkSize * @param string|null $column * @param string|null $alias * @return \Illuminate\Support\LazyCollection */ public function lazyById($chunkSize = 1000, $column = null, $alias = null) { $column ??= $this->getRelated()->getQualifiedKeyName(); $alias ??= $this->getRelated()->getKeyName(); return $this->prepareQueryBuilder()->lazyById($chunkSize, $column, $alias); } /** * Prepare the query builder for query execution. * * @param array $columns * @return \Illuminate\Database\Eloquent\Builder */ protected function prepareQueryBuilder($columns = ['*']) { $builder = $this->query->applyScopes(); return $builder->addSelect( $this->shouldSelect($builder->getQuery()->columns ? [] : $columns) ); } /** * Add the constraints for a relationship query. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) { if ($parentQuery->getQuery()->from === $query->getQuery()->from) { return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns); } if ($parentQuery->getQuery()->from === $this->throughParent->getTable()) { return $this->getRelationExistenceQueryForThroughSelfRelation($query, $parentQuery, $columns); } $this->performJoin($query); return $query->select($columns)->whereColumn( $this->getQualifiedLocalKeyName(), '=', $this->getQualifiedFirstKeyName() ); } /** * Add the constraints for a relationship query on the same table. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*']) { $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()); $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->secondKey); if ($this->throughParentSoftDeletes()) { $query->whereNull($this->throughParent->getQualifiedDeletedAtColumn()); } $query->getModel()->setTable($hash); return $query->select($columns)->whereColumn( $parentQuery->getQuery()->from.'.'.$this->localKey, '=', $this->getQualifiedFirstKeyName() ); } /** * Add the constraints for a relationship query on the same table as the through parent. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQueryForThroughSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*']) { $table = $this->throughParent->getTable().' as '.$hash = $this->getRelationCountHash(); $query->join($table, $hash.'.'.$this->secondLocalKey, '=', $this->getQualifiedFarKeyName()); if ($this->throughParentSoftDeletes()) { $query->whereNull($hash.'.'.$this->throughParent->getDeletedAtColumn()); } return $query->select($columns)->whereColumn( $parentQuery->getQuery()->from.'.'.$this->localKey, '=', $hash.'.'.$this->firstKey ); } /** * Get the qualified foreign key on the related model. * * @return string */ public function getQualifiedFarKeyName() { return $this->getQualifiedForeignKeyName(); } /** * Get the foreign key on the "through" model. * * @return string */ public function getFirstKeyName() { return $this->firstKey; } /** * Get the qualified foreign key on the "through" model. * * @return string */ public function getQualifiedFirstKeyName() { return $this->throughParent->qualifyColumn($this->firstKey); } /** * Get the foreign key on the related model. * * @return string */ public function getForeignKeyName() { return $this->secondKey; } /** * Get the qualified foreign key on the related model. * * @return string */ public function getQualifiedForeignKeyName() { return $this->related->qualifyColumn($this->secondKey); } /** * Get the local key on the far parent model. * * @return string */ public function getLocalKeyName() { return $this->localKey; } /** * Get the qualified local key on the far parent model. * * @return string */ public function getQualifiedLocalKeyName() { return $this->farParent->qualifyColumn($this->localKey); } /** * Get the local key on the intermediary model. * * @return string */ public function getSecondLocalKeyName() { return $this->secondLocalKey; } } src/Illuminate/Database/Eloquent/Relations/HasMany.php 0000644 00000003011 15107327037 0016747 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Database\Eloquent\Collection; class HasMany extends HasOneOrMany { /** * Convert the relationship to a "has one" relationship. * * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function one() { return HasOne::noConstraints(fn () => new HasOne( $this->getQuery(), $this->parent, $this->foreignKey, $this->localKey )); } /** * Get the results of the relationship. * * @return mixed */ public function getResults() { return ! is_null($this->getParentKey()) ? $this->query->get() : $this->related->newCollection(); } /** * Initialize the relation on a set of models. * * @param array $models * @param string $relation * @return array */ public function initRelation(array $models, $relation) { foreach ($models as $model) { $model->setRelation($relation, $this->related->newCollection()); } return $models; } /** * Match the eagerly loaded results to their parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array */ public function match(array $models, Collection $results, $relation) { return $this->matchMany($models, $results, $relation); } } src/Illuminate/Database/Eloquent/Relations/Pivot.php 0000644 00000000707 15107327037 0016521 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot; class Pivot extends Model { use AsPivot; /** * Indicates if the IDs are auto-incrementing. * * @var bool */ public $incrementing = false; /** * The attributes that aren't mass assignable. * * @var array */ protected $guarded = []; } src/Illuminate/Database/Eloquent/Relations/MorphMany.php 0000644 00000004471 15107327037 0017334 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; class MorphMany extends MorphOneOrMany { /** * Convert the relationship to a "morph one" relationship. * * @return \Illuminate\Database\Eloquent\Relations\MorphOne */ public function one() { return MorphOne::noConstraints(fn () => new MorphOne( $this->getQuery(), $this->getParent(), $this->morphType, $this->foreignKey, $this->localKey )); } /** * Get the results of the relationship. * * @return mixed */ public function getResults() { return ! is_null($this->getParentKey()) ? $this->query->get() : $this->related->newCollection(); } /** * Initialize the relation on a set of models. * * @param array $models * @param string $relation * @return array */ public function initRelation(array $models, $relation) { foreach ($models as $model) { $model->setRelation($relation, $this->related->newCollection()); } return $models; } /** * Match the eagerly loaded results to their parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array */ public function match(array $models, Collection $results, $relation) { return $this->matchMany($models, $results, $relation); } /** * Create a new instance of the related model. Allow mass-assignment. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Model */ public function forceCreate(array $attributes = []) { $attributes[$this->getMorphType()] = $this->morphClass; return parent::forceCreate($attributes); } /** * Create a new instance of the related model with mass assignment without raising model events. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Model */ public function forceCreateQuietly(array $attributes = []) { return Model::withoutEvents(fn () => $this->forceCreate($attributes)); } } src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php 0000644 00000050516 15107327037 0023757 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations\Concerns; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Support\Collection as BaseCollection; trait InteractsWithPivotTable { /** * Toggles a model (or models) from the parent. * * Each existing model is detached, and non existing ones are attached. * * @param mixed $ids * @param bool $touch * @return array */ public function toggle($ids, $touch = true) { $changes = [ 'attached' => [], 'detached' => [], ]; $records = $this->formatRecordsList($this->parseIds($ids)); // Next, we will determine which IDs should get removed from the join table by // checking which of the given ID/records is in the list of current records // and removing all of those rows from this "intermediate" joining table. $detach = array_values(array_intersect( $this->newPivotQuery()->pluck($this->relatedPivotKey)->all(), array_keys($records) )); if (count($detach) > 0) { $this->detach($detach, false); $changes['detached'] = $this->castKeys($detach); } // Finally, for all of the records which were not "detached", we'll attach the // records into the intermediate table. Then, we will add those attaches to // this change list and get ready to return these results to the callers. $attach = array_diff_key($records, array_flip($detach)); if (count($attach) > 0) { $this->attach($attach, [], false); $changes['attached'] = array_keys($attach); } // Once we have finished attaching or detaching the records, we will see if we // have done any attaching or detaching, and if we have we will touch these // relationships if they are configured to touch on any database updates. if ($touch && (count($changes['attached']) || count($changes['detached']))) { $this->touchIfTouching(); } return $changes; } /** * Sync the intermediate tables with a list of IDs without detaching. * * @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids * @return array */ public function syncWithoutDetaching($ids) { return $this->sync($ids, false); } /** * Sync the intermediate tables with a list of IDs or collection of models. * * @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids * @param bool $detaching * @return array */ public function sync($ids, $detaching = true) { $changes = [ 'attached' => [], 'detached' => [], 'updated' => [], ]; // First we need to attach any of the associated models that are not currently // in this joining table. We'll spin through the given IDs, checking to see // if they exist in the array of current ones, and if not we will insert. $current = $this->getCurrentlyAttachedPivots() ->pluck($this->relatedPivotKey)->all(); $records = $this->formatRecordsList($this->parseIds($ids)); // Next, we will take the differences of the currents and given IDs and detach // all of the entities that exist in the "current" array but are not in the // array of the new IDs given to the method which will complete the sync. if ($detaching) { $detach = array_diff($current, array_keys($records)); if (count($detach) > 0) { $this->detach($detach); $changes['detached'] = $this->castKeys($detach); } } // Now we are finally ready to attach the new records. Note that we'll disable // touching until after the entire operation is complete so we don't fire a // ton of touch operations until we are totally done syncing the records. $changes = array_merge( $changes, $this->attachNew($records, $current, false) ); // Once we have finished attaching or detaching the records, we will see if we // have done any attaching or detaching, and if we have we will touch these // relationships if they are configured to touch on any database updates. if (count($changes['attached']) || count($changes['updated']) || count($changes['detached'])) { $this->touchIfTouching(); } return $changes; } /** * Sync the intermediate tables with a list of IDs or collection of models with the given pivot values. * * @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids * @param array $values * @param bool $detaching * @return array */ public function syncWithPivotValues($ids, array $values, bool $detaching = true) { return $this->sync(collect($this->parseIds($ids))->mapWithKeys(function ($id) use ($values) { return [$id => $values]; }), $detaching); } /** * Format the sync / toggle record list so that it is keyed by ID. * * @param array $records * @return array */ protected function formatRecordsList(array $records) { return collect($records)->mapWithKeys(function ($attributes, $id) { if (! is_array($attributes)) { [$id, $attributes] = [$attributes, []]; } return [$id => $attributes]; })->all(); } /** * Attach all of the records that aren't in the given current records. * * @param array $records * @param array $current * @param bool $touch * @return array */ protected function attachNew(array $records, array $current, $touch = true) { $changes = ['attached' => [], 'updated' => []]; foreach ($records as $id => $attributes) { // If the ID is not in the list of existing pivot IDs, we will insert a new pivot // record, otherwise, we will just update this existing record on this joining // table, so that the developers will easily update these records pain free. if (! in_array($id, $current)) { $this->attach($id, $attributes, $touch); $changes['attached'][] = $this->castKey($id); } // Now we'll try to update an existing pivot record with the attributes that were // given to the method. If the model is actually updated we will add it to the // list of updated pivot records so we return them back out to the consumer. elseif (count($attributes) > 0 && $this->updateExistingPivot($id, $attributes, $touch)) { $changes['updated'][] = $this->castKey($id); } } return $changes; } /** * Update an existing pivot record on the table. * * @param mixed $id * @param array $attributes * @param bool $touch * @return int */ public function updateExistingPivot($id, array $attributes, $touch = true) { if ($this->using && empty($this->pivotWheres) && empty($this->pivotWhereIns) && empty($this->pivotWhereNulls)) { return $this->updateExistingPivotUsingCustomClass($id, $attributes, $touch); } if ($this->hasPivotColumn($this->updatedAt())) { $attributes = $this->addTimestampsToAttachment($attributes, true); } $updated = $this->newPivotStatementForId($this->parseId($id))->update( $this->castAttributes($attributes) ); if ($touch) { $this->touchIfTouching(); } return $updated; } /** * Update an existing pivot record on the table via a custom class. * * @param mixed $id * @param array $attributes * @param bool $touch * @return int */ protected function updateExistingPivotUsingCustomClass($id, array $attributes, $touch) { $pivot = $this->getCurrentlyAttachedPivots() ->where($this->foreignPivotKey, $this->parent->{$this->parentKey}) ->where($this->relatedPivotKey, $this->parseId($id)) ->first(); $updated = $pivot ? $pivot->fill($attributes)->isDirty() : false; if ($updated) { $pivot->save(); } if ($touch) { $this->touchIfTouching(); } return (int) $updated; } /** * Attach a model to the parent. * * @param mixed $id * @param array $attributes * @param bool $touch * @return void */ public function attach($id, array $attributes = [], $touch = true) { if ($this->using) { $this->attachUsingCustomClass($id, $attributes); } else { // Here we will insert the attachment records into the pivot table. Once we have // inserted the records, we will touch the relationships if necessary and the // function will return. We can parse the IDs before inserting the records. $this->newPivotStatement()->insert($this->formatAttachRecords( $this->parseIds($id), $attributes )); } if ($touch) { $this->touchIfTouching(); } } /** * Attach a model to the parent using a custom class. * * @param mixed $id * @param array $attributes * @return void */ protected function attachUsingCustomClass($id, array $attributes) { $records = $this->formatAttachRecords( $this->parseIds($id), $attributes ); foreach ($records as $record) { $this->newPivot($record, false)->save(); } } /** * Create an array of records to insert into the pivot table. * * @param array $ids * @param array $attributes * @return array */ protected function formatAttachRecords($ids, array $attributes) { $records = []; $hasTimestamps = ($this->hasPivotColumn($this->createdAt()) || $this->hasPivotColumn($this->updatedAt())); // To create the attachment records, we will simply spin through the IDs given // and create a new record to insert for each ID. Each ID may actually be a // key in the array, with extra attributes to be placed in other columns. foreach ($ids as $key => $value) { $records[] = $this->formatAttachRecord( $key, $value, $attributes, $hasTimestamps ); } return $records; } /** * Create a full attachment record payload. * * @param int $key * @param mixed $value * @param array $attributes * @param bool $hasTimestamps * @return array */ protected function formatAttachRecord($key, $value, $attributes, $hasTimestamps) { [$id, $attributes] = $this->extractAttachIdAndAttributes($key, $value, $attributes); return array_merge( $this->baseAttachRecord($id, $hasTimestamps), $this->castAttributes($attributes) ); } /** * Get the attach record ID and extra attributes. * * @param mixed $key * @param mixed $value * @param array $attributes * @return array */ protected function extractAttachIdAndAttributes($key, $value, array $attributes) { return is_array($value) ? [$key, array_merge($value, $attributes)] : [$value, $attributes]; } /** * Create a new pivot attachment record. * * @param int $id * @param bool $timed * @return array */ protected function baseAttachRecord($id, $timed) { $record[$this->relatedPivotKey] = $id; $record[$this->foreignPivotKey] = $this->parent->{$this->parentKey}; // If the record needs to have creation and update timestamps, we will make // them by calling the parent model's "freshTimestamp" method which will // provide us with a fresh timestamp in this model's preferred format. if ($timed) { $record = $this->addTimestampsToAttachment($record); } foreach ($this->pivotValues as $value) { $record[$value['column']] = $value['value']; } return $record; } /** * Set the creation and update timestamps on an attach record. * * @param array $record * @param bool $exists * @return array */ protected function addTimestampsToAttachment(array $record, $exists = false) { $fresh = $this->parent->freshTimestamp(); if ($this->using) { $pivotModel = new $this->using; $fresh = $pivotModel->fromDateTime($fresh); } if (! $exists && $this->hasPivotColumn($this->createdAt())) { $record[$this->createdAt()] = $fresh; } if ($this->hasPivotColumn($this->updatedAt())) { $record[$this->updatedAt()] = $fresh; } return $record; } /** * Determine whether the given column is defined as a pivot column. * * @param string $column * @return bool */ public function hasPivotColumn($column) { return in_array($column, $this->pivotColumns); } /** * Detach models from the relationship. * * @param mixed $ids * @param bool $touch * @return int */ public function detach($ids = null, $touch = true) { if ($this->using && ! empty($ids) && empty($this->pivotWheres) && empty($this->pivotWhereIns) && empty($this->pivotWhereNulls)) { $results = $this->detachUsingCustomClass($ids); } else { $query = $this->newPivotQuery(); // If associated IDs were passed to the method we will only delete those // associations, otherwise all of the association ties will be broken. // We'll return the numbers of affected rows when we do the deletes. if (! is_null($ids)) { $ids = $this->parseIds($ids); if (empty($ids)) { return 0; } $query->whereIn($this->getQualifiedRelatedPivotKeyName(), (array) $ids); } // Once we have all of the conditions set on the statement, we are ready // to run the delete on the pivot table. Then, if the touch parameter // is true, we will go ahead and touch all related models to sync. $results = $query->delete(); } if ($touch) { $this->touchIfTouching(); } return $results; } /** * Detach models from the relationship using a custom class. * * @param mixed $ids * @return int */ protected function detachUsingCustomClass($ids) { $results = 0; foreach ($this->parseIds($ids) as $id) { $results += $this->newPivot([ $this->foreignPivotKey => $this->parent->{$this->parentKey}, $this->relatedPivotKey => $id, ], true)->delete(); } return $results; } /** * Get the pivot models that are currently attached. * * @return \Illuminate\Support\Collection */ protected function getCurrentlyAttachedPivots() { return $this->newPivotQuery()->get()->map(function ($record) { $class = $this->using ?: Pivot::class; $pivot = $class::fromRawAttributes($this->parent, (array) $record, $this->getTable(), true); return $pivot->setPivotKeys($this->foreignPivotKey, $this->relatedPivotKey); }); } /** * Create a new pivot model instance. * * @param array $attributes * @param bool $exists * @return \Illuminate\Database\Eloquent\Relations\Pivot */ public function newPivot(array $attributes = [], $exists = false) { $attributes = array_merge(array_column($this->pivotValues, 'value', 'column'), $attributes); $pivot = $this->related->newPivot( $this->parent, $attributes, $this->table, $exists, $this->using ); return $pivot->setPivotKeys($this->foreignPivotKey, $this->relatedPivotKey); } /** * Create a new existing pivot model instance. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Relations\Pivot */ public function newExistingPivot(array $attributes = []) { return $this->newPivot($attributes, true); } /** * Get a new plain query builder for the pivot table. * * @return \Illuminate\Database\Query\Builder */ public function newPivotStatement() { return $this->query->getQuery()->newQuery()->from($this->table); } /** * Get a new pivot statement for a given "other" ID. * * @param mixed $id * @return \Illuminate\Database\Query\Builder */ public function newPivotStatementForId($id) { return $this->newPivotQuery()->whereIn($this->relatedPivotKey, $this->parseIds($id)); } /** * Create a new query builder for the pivot table. * * @return \Illuminate\Database\Query\Builder */ public function newPivotQuery() { $query = $this->newPivotStatement(); foreach ($this->pivotWheres as $arguments) { $query->where(...$arguments); } foreach ($this->pivotWhereIns as $arguments) { $query->whereIn(...$arguments); } foreach ($this->pivotWhereNulls as $arguments) { $query->whereNull(...$arguments); } return $query->where($this->getQualifiedForeignPivotKeyName(), $this->parent->{$this->parentKey}); } /** * Set the columns on the pivot table to retrieve. * * @param array|mixed $columns * @return $this */ public function withPivot($columns) { $this->pivotColumns = array_merge( $this->pivotColumns, is_array($columns) ? $columns : func_get_args() ); return $this; } /** * Get all of the IDs from the given mixed value. * * @param mixed $value * @return array */ protected function parseIds($value) { if ($value instanceof Model) { return [$value->{$this->relatedKey}]; } if ($value instanceof Collection) { return $value->pluck($this->relatedKey)->all(); } if ($value instanceof BaseCollection) { return $value->toArray(); } return (array) $value; } /** * Get the ID from the given mixed value. * * @param mixed $value * @return mixed */ protected function parseId($value) { return $value instanceof Model ? $value->{$this->relatedKey} : $value; } /** * Cast the given keys to integers if they are numeric and string otherwise. * * @param array $keys * @return array */ protected function castKeys(array $keys) { return array_map(function ($v) { return $this->castKey($v); }, $keys); } /** * Cast the given key to convert to primary key type. * * @param mixed $key * @return mixed */ protected function castKey($key) { return $this->getTypeSwapValue( $this->related->getKeyType(), $key ); } /** * Cast the given pivot attributes. * * @param array $attributes * @return array */ protected function castAttributes($attributes) { return $this->using ? $this->newPivot()->fill($attributes)->getAttributes() : $attributes; } /** * Converts a given value to a given type value. * * @param string $type * @param mixed $value * @return mixed */ protected function getTypeSwapValue($type, $value) { return match (strtolower($type)) { 'int', 'integer' => (int) $value, 'real', 'float', 'double' => (float) $value, 'string' => (string) $value, default => $value, }; } } src/Illuminate/Database/Eloquent/Relations/Concerns/AsPivot.php 0000644 00000020761 15107327037 0020561 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations\Concerns; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; trait AsPivot { /** * The parent model of the relationship. * * @var \Illuminate\Database\Eloquent\Model */ public $pivotParent; /** * The name of the foreign key column. * * @var string */ protected $foreignKey; /** * The name of the "other key" column. * * @var string */ protected $relatedKey; /** * Create a new pivot model instance. * * @param \Illuminate\Database\Eloquent\Model $parent * @param array $attributes * @param string $table * @param bool $exists * @return static */ public static function fromAttributes(Model $parent, $attributes, $table, $exists = false) { $instance = new static; $instance->timestamps = $instance->hasTimestampAttributes($attributes); // The pivot model is a "dynamic" model since we will set the tables dynamically // for the instance. This allows it work for any intermediate tables for the // many to many relationship that are defined by this developer's classes. $instance->setConnection($parent->getConnectionName()) ->setTable($table) ->forceFill($attributes) ->syncOriginal(); // We store off the parent instance so we will access the timestamp column names // for the model, since the pivot model timestamps aren't easily configurable // from the developer's point of view. We can use the parents to get these. $instance->pivotParent = $parent; $instance->exists = $exists; return $instance; } /** * Create a new pivot model from raw values returned from a query. * * @param \Illuminate\Database\Eloquent\Model $parent * @param array $attributes * @param string $table * @param bool $exists * @return static */ public static function fromRawAttributes(Model $parent, $attributes, $table, $exists = false) { $instance = static::fromAttributes($parent, [], $table, $exists); $instance->timestamps = $instance->hasTimestampAttributes($attributes); $instance->setRawAttributes( array_merge($instance->getRawOriginal(), $attributes), $exists ); return $instance; } /** * Set the keys for a select query. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ protected function setKeysForSelectQuery($query) { if (isset($this->attributes[$this->getKeyName()])) { return parent::setKeysForSelectQuery($query); } $query->where($this->foreignKey, $this->getOriginal( $this->foreignKey, $this->getAttribute($this->foreignKey) )); return $query->where($this->relatedKey, $this->getOriginal( $this->relatedKey, $this->getAttribute($this->relatedKey) )); } /** * Set the keys for a save update query. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ protected function setKeysForSaveQuery($query) { return $this->setKeysForSelectQuery($query); } /** * Delete the pivot model record from the database. * * @return int */ public function delete() { if (isset($this->attributes[$this->getKeyName()])) { return (int) parent::delete(); } if ($this->fireModelEvent('deleting') === false) { return 0; } $this->touchOwners(); return tap($this->getDeleteQuery()->delete(), function () { $this->exists = false; $this->fireModelEvent('deleted', false); }); } /** * Get the query builder for a delete operation on the pivot. * * @return \Illuminate\Database\Eloquent\Builder */ protected function getDeleteQuery() { return $this->newQueryWithoutRelationships()->where([ $this->foreignKey => $this->getOriginal($this->foreignKey, $this->getAttribute($this->foreignKey)), $this->relatedKey => $this->getOriginal($this->relatedKey, $this->getAttribute($this->relatedKey)), ]); } /** * Get the table associated with the model. * * @return string */ public function getTable() { if (! isset($this->table)) { $this->setTable(str_replace( '\\', '', Str::snake(Str::singular(class_basename($this))) )); } return $this->table; } /** * Get the foreign key column name. * * @return string */ public function getForeignKey() { return $this->foreignKey; } /** * Get the "related key" column name. * * @return string */ public function getRelatedKey() { return $this->relatedKey; } /** * Get the "related key" column name. * * @return string */ public function getOtherKey() { return $this->getRelatedKey(); } /** * Set the key names for the pivot model instance. * * @param string $foreignKey * @param string $relatedKey * @return $this */ public function setPivotKeys($foreignKey, $relatedKey) { $this->foreignKey = $foreignKey; $this->relatedKey = $relatedKey; return $this; } /** * Determine if the pivot model or given attributes has timestamp attributes. * * @param array|null $attributes * @return bool */ public function hasTimestampAttributes($attributes = null) { return array_key_exists($this->getCreatedAtColumn(), $attributes ?? $this->attributes); } /** * Get the name of the "created at" column. * * @return string */ public function getCreatedAtColumn() { return $this->pivotParent ? $this->pivotParent->getCreatedAtColumn() : parent::getCreatedAtColumn(); } /** * Get the name of the "updated at" column. * * @return string */ public function getUpdatedAtColumn() { return $this->pivotParent ? $this->pivotParent->getUpdatedAtColumn() : parent::getUpdatedAtColumn(); } /** * Get the queueable identity for the entity. * * @return mixed */ public function getQueueableId() { if (isset($this->attributes[$this->getKeyName()])) { return $this->getKey(); } return sprintf( '%s:%s:%s:%s', $this->foreignKey, $this->getAttribute($this->foreignKey), $this->relatedKey, $this->getAttribute($this->relatedKey) ); } /** * Get a new query to restore one or more models by their queueable IDs. * * @param int[]|string[]|string $ids * @return \Illuminate\Database\Eloquent\Builder */ public function newQueryForRestoration($ids) { if (is_array($ids)) { return $this->newQueryForCollectionRestoration($ids); } if (! str_contains($ids, ':')) { return parent::newQueryForRestoration($ids); } $segments = explode(':', $ids); return $this->newQueryWithoutScopes() ->where($segments[0], $segments[1]) ->where($segments[2], $segments[3]); } /** * Get a new query to restore multiple models by their queueable IDs. * * @param int[]|string[] $ids * @return \Illuminate\Database\Eloquent\Builder */ protected function newQueryForCollectionRestoration(array $ids) { $ids = array_values($ids); if (! str_contains($ids[0], ':')) { return parent::newQueryForRestoration($ids); } $query = $this->newQueryWithoutScopes(); foreach ($ids as $id) { $segments = explode(':', $id); $query->orWhere(function ($query) use ($segments) { return $query->where($segments[0], $segments[1]) ->where($segments[2], $segments[3]); }); } return $query; } /** * Unset all the loaded relations for the instance. * * @return $this */ public function unsetRelations() { $this->pivotParent = null; $this->relations = []; return $this; } } src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithDictionary.php 0000644 00000001720 15107327037 0024004 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations\Concerns; use BackedEnum; use Doctrine\Instantiator\Exception\InvalidArgumentException; use UnitEnum; trait InteractsWithDictionary { /** * Get a dictionary key attribute - casting it to a string if necessary. * * @param mixed $attribute * @return mixed * * @throws \Doctrine\Instantiator\Exception\InvalidArgumentException */ protected function getDictionaryKey($attribute) { if (is_object($attribute)) { if (method_exists($attribute, '__toString')) { return $attribute->__toString(); } if ($attribute instanceof UnitEnum) { return $attribute instanceof BackedEnum ? $attribute->value : $attribute->name; } throw new InvalidArgumentException('Model attribute value is an object but does not have a __toString method.'); } return $attribute; } } src/Illuminate/Database/Eloquent/Relations/Concerns/CanBeOneOfMany.php 0000644 00000023230 15107327037 0021712 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations\Concerns; use Closure; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Arr; use InvalidArgumentException; trait CanBeOneOfMany { /** * Determines whether the relationship is one-of-many. * * @var bool */ protected $isOneOfMany = false; /** * The name of the relationship. * * @var string */ protected $relationName; /** * The one of many inner join subselect query builder instance. * * @var \Illuminate\Database\Eloquent\Builder|null */ protected $oneOfManySubQuery; /** * Add constraints for inner join subselect for one of many relationships. * * @param \Illuminate\Database\Eloquent\Builder $query * @param string|null $column * @param string|null $aggregate * @return void */ abstract public function addOneOfManySubQueryConstraints(Builder $query, $column = null, $aggregate = null); /** * Get the columns the determine the relationship groups. * * @return array|string */ abstract public function getOneOfManySubQuerySelectColumns(); /** * Add join query constraints for one of many relationships. * * @param \Illuminate\Database\Query\JoinClause $join * @return void */ abstract public function addOneOfManyJoinSubQueryConstraints(JoinClause $join); /** * Indicate that the relation is a single result of a larger one-to-many relationship. * * @param string|array|null $column * @param string|\Closure|null $aggregate * @param string|null $relation * @return $this * * @throws \InvalidArgumentException */ public function ofMany($column = 'id', $aggregate = 'MAX', $relation = null) { $this->isOneOfMany = true; $this->relationName = $relation ?: $this->getDefaultOneOfManyJoinAlias( $this->guessRelationship() ); $keyName = $this->query->getModel()->getKeyName(); $columns = is_string($columns = $column) ? [ $column => $aggregate, $keyName => $aggregate, ] : $column; if (! array_key_exists($keyName, $columns)) { $columns[$keyName] = 'MAX'; } if ($aggregate instanceof Closure) { $closure = $aggregate; } foreach ($columns as $column => $aggregate) { if (! in_array(strtolower($aggregate), ['min', 'max'])) { throw new InvalidArgumentException("Invalid aggregate [{$aggregate}] used within ofMany relation. Available aggregates: MIN, MAX"); } $subQuery = $this->newOneOfManySubQuery( $this->getOneOfManySubQuerySelectColumns(), array_merge([$column], $previous['columns'] ?? []), $aggregate, ); if (isset($previous)) { $this->addOneOfManyJoinSubQuery( $subQuery, $previous['subQuery'], $previous['columns'], ); } if (isset($closure)) { $closure($subQuery); } if (! isset($previous)) { $this->oneOfManySubQuery = $subQuery; } if (array_key_last($columns) == $column) { $this->addOneOfManyJoinSubQuery( $this->query, $subQuery, array_merge([$column], $previous['columns'] ?? []), ); } $previous = [ 'subQuery' => $subQuery, 'columns' => array_merge([$column], $previous['columns'] ?? []), ]; } $this->addConstraints(); $columns = $this->query->getQuery()->columns; if (is_null($columns) || $columns === ['*']) { $this->select([$this->qualifyColumn('*')]); } return $this; } /** * Indicate that the relation is the latest single result of a larger one-to-many relationship. * * @param string|array|null $column * @param string|null $relation * @return $this */ public function latestOfMany($column = 'id', $relation = null) { return $this->ofMany(collect(Arr::wrap($column))->mapWithKeys(function ($column) { return [$column => 'MAX']; })->all(), 'MAX', $relation); } /** * Indicate that the relation is the oldest single result of a larger one-to-many relationship. * * @param string|array|null $column * @param string|null $relation * @return $this */ public function oldestOfMany($column = 'id', $relation = null) { return $this->ofMany(collect(Arr::wrap($column))->mapWithKeys(function ($column) { return [$column => 'MIN']; })->all(), 'MIN', $relation); } /** * Get the default alias for the one of many inner join clause. * * @param string $relation * @return string */ protected function getDefaultOneOfManyJoinAlias($relation) { return $relation == $this->query->getModel()->getTable() ? $relation.'_of_many' : $relation; } /** * Get a new query for the related model, grouping the query by the given column, often the foreign key of the relationship. * * @param string|array $groupBy * @param array<string>|null $columns * @param string|null $aggregate * @return \Illuminate\Database\Eloquent\Builder */ protected function newOneOfManySubQuery($groupBy, $columns = null, $aggregate = null) { $subQuery = $this->query->getModel() ->newQuery() ->withoutGlobalScopes($this->removedScopes()); foreach (Arr::wrap($groupBy) as $group) { $subQuery->groupBy($this->qualifyRelatedColumn($group)); } if (! is_null($columns)) { foreach ($columns as $key => $column) { $aggregatedColumn = $subQuery->getQuery()->grammar->wrap($subQuery->qualifyColumn($column)); if ($key === 0) { $aggregatedColumn = "{$aggregate}({$aggregatedColumn})"; } else { $aggregatedColumn = "min({$aggregatedColumn})"; } $subQuery->selectRaw($aggregatedColumn.' as '.$subQuery->getQuery()->grammar->wrap($column.'_aggregate')); } } $this->addOneOfManySubQueryConstraints($subQuery, $groupBy, $columns, $aggregate); return $subQuery; } /** * Add the join subquery to the given query on the given column and the relationship's foreign key. * * @param \Illuminate\Database\Eloquent\Builder $parent * @param \Illuminate\Database\Eloquent\Builder $subQuery * @param array<string> $on * @return void */ protected function addOneOfManyJoinSubQuery(Builder $parent, Builder $subQuery, $on) { $parent->beforeQuery(function ($parent) use ($subQuery, $on) { $subQuery->applyBeforeQueryCallbacks(); $parent->joinSub($subQuery, $this->relationName, function ($join) use ($on) { foreach ($on as $onColumn) { $join->on($this->qualifySubSelectColumn($onColumn.'_aggregate'), '=', $this->qualifyRelatedColumn($onColumn)); } $this->addOneOfManyJoinSubQueryConstraints($join, $on); }); }); } /** * Merge the relationship query joins to the given query builder. * * @param \Illuminate\Database\Eloquent\Builder $query * @return void */ protected function mergeOneOfManyJoinsTo(Builder $query) { $query->getQuery()->beforeQueryCallbacks = $this->query->getQuery()->beforeQueryCallbacks; $query->applyBeforeQueryCallbacks(); } /** * Get the query builder that will contain the relationship constraints. * * @return \Illuminate\Database\Eloquent\Builder */ protected function getRelationQuery() { return $this->isOneOfMany() ? $this->oneOfManySubQuery : $this->query; } /** * Get the one of many inner join subselect builder instance. * * @return \Illuminate\Database\Eloquent\Builder|void */ public function getOneOfManySubQuery() { return $this->oneOfManySubQuery; } /** * Get the qualified column name for the one-of-many relationship using the subselect join query's alias. * * @param string $column * @return string */ public function qualifySubSelectColumn($column) { return $this->getRelationName().'.'.last(explode('.', $column)); } /** * Qualify related column using the related table name if it is not already qualified. * * @param string $column * @return string */ protected function qualifyRelatedColumn($column) { return str_contains($column, '.') ? $column : $this->query->getModel()->getTable().'.'.$column; } /** * Guess the "hasOne" relationship's name via backtrace. * * @return string */ protected function guessRelationship() { return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2]['function']; } /** * Determine whether the relationship is a one-of-many relationship. * * @return bool */ public function isOneOfMany() { return $this->isOneOfMany; } /** * Get the name of the relationship. * * @return string */ public function getRelationName() { return $this->relationName; } } src/Illuminate/Database/Eloquent/Relations/Concerns/SupportsDefaultModels.php 0000644 00000003021 15107327037 0023472 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations\Concerns; use Illuminate\Database\Eloquent\Model; trait SupportsDefaultModels { /** * Indicates if a default model instance should be used. * * Alternatively, may be a Closure or array. * * @var \Closure|array|bool */ protected $withDefault; /** * Make a new related instance for the given model. * * @param \Illuminate\Database\Eloquent\Model $parent * @return \Illuminate\Database\Eloquent\Model */ abstract protected function newRelatedInstanceFor(Model $parent); /** * Return a new model instance in case the relationship does not exist. * * @param \Closure|array|bool $callback * @return $this */ public function withDefault($callback = true) { $this->withDefault = $callback; return $this; } /** * Get the default value for this relation. * * @param \Illuminate\Database\Eloquent\Model $parent * @return \Illuminate\Database\Eloquent\Model|null */ protected function getDefaultFor(Model $parent) { if (! $this->withDefault) { return; } $instance = $this->newRelatedInstanceFor($parent); if (is_callable($this->withDefault)) { return call_user_func($this->withDefault, $instance, $parent) ?: $instance; } if (is_array($this->withDefault)) { $instance->forceFill($this->withDefault); } return $instance; } } src/Illuminate/Database/Eloquent/Relations/Concerns/ComparesRelatedModels.php 0000644 00000004047 15107327037 0023411 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations\Concerns; use Illuminate\Contracts\Database\Eloquent\SupportsPartialRelations; use Illuminate\Database\Eloquent\Model; trait ComparesRelatedModels { /** * Determine if the model is the related instance of the relationship. * * @param \Illuminate\Database\Eloquent\Model|null $model * @return bool */ public function is($model) { $match = ! is_null($model) && $this->compareKeys($this->getParentKey(), $this->getRelatedKeyFrom($model)) && $this->related->getTable() === $model->getTable() && $this->related->getConnectionName() === $model->getConnectionName(); if ($match && $this instanceof SupportsPartialRelations && $this->isOneOfMany()) { return $this->query ->whereKey($model->getKey()) ->exists(); } return $match; } /** * Determine if the model is not the related instance of the relationship. * * @param \Illuminate\Database\Eloquent\Model|null $model * @return bool */ public function isNot($model) { return ! $this->is($model); } /** * Get the value of the parent model's key. * * @return mixed */ abstract public function getParentKey(); /** * Get the value of the model's related key. * * @param \Illuminate\Database\Eloquent\Model $model * @return mixed */ abstract protected function getRelatedKeyFrom(Model $model); /** * Compare the parent key with the related key. * * @param mixed $parentKey * @param mixed $relatedKey * @return bool */ protected function compareKeys($parentKey, $relatedKey) { if (empty($parentKey) || empty($relatedKey)) { return false; } if (is_int($parentKey) || is_int($relatedKey)) { return (int) $parentKey === (int) $relatedKey; } return $parentKey === $relatedKey; } } src/Illuminate/Database/Eloquent/Relations/MorphPivot.php 0000644 00000010676 15107327037 0017535 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; class MorphPivot extends Pivot { /** * The type of the polymorphic relation. * * Explicitly define this so it's not included in saved attributes. * * @var string */ protected $morphType; /** * The value of the polymorphic relation. * * Explicitly define this so it's not included in saved attributes. * * @var string */ protected $morphClass; /** * Set the keys for a save update query. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ protected function setKeysForSaveQuery($query) { $query->where($this->morphType, $this->morphClass); return parent::setKeysForSaveQuery($query); } /** * Set the keys for a select query. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ protected function setKeysForSelectQuery($query) { $query->where($this->morphType, $this->morphClass); return parent::setKeysForSelectQuery($query); } /** * Delete the pivot model record from the database. * * @return int */ public function delete() { if (isset($this->attributes[$this->getKeyName()])) { return (int) parent::delete(); } if ($this->fireModelEvent('deleting') === false) { return 0; } $query = $this->getDeleteQuery(); $query->where($this->morphType, $this->morphClass); return tap($query->delete(), function () { $this->fireModelEvent('deleted', false); }); } /** * Get the morph type for the pivot. * * @return string */ public function getMorphType() { return $this->morphType; } /** * Set the morph type for the pivot. * * @param string $morphType * @return $this */ public function setMorphType($morphType) { $this->morphType = $morphType; return $this; } /** * Set the morph class for the pivot. * * @param string $morphClass * @return \Illuminate\Database\Eloquent\Relations\MorphPivot */ public function setMorphClass($morphClass) { $this->morphClass = $morphClass; return $this; } /** * Get the queueable identity for the entity. * * @return mixed */ public function getQueueableId() { if (isset($this->attributes[$this->getKeyName()])) { return $this->getKey(); } return sprintf( '%s:%s:%s:%s:%s:%s', $this->foreignKey, $this->getAttribute($this->foreignKey), $this->relatedKey, $this->getAttribute($this->relatedKey), $this->morphType, $this->morphClass ); } /** * Get a new query to restore one or more models by their queueable IDs. * * @param array|int $ids * @return \Illuminate\Database\Eloquent\Builder */ public function newQueryForRestoration($ids) { if (is_array($ids)) { return $this->newQueryForCollectionRestoration($ids); } if (! str_contains($ids, ':')) { return parent::newQueryForRestoration($ids); } $segments = explode(':', $ids); return $this->newQueryWithoutScopes() ->where($segments[0], $segments[1]) ->where($segments[2], $segments[3]) ->where($segments[4], $segments[5]); } /** * Get a new query to restore multiple models by their queueable IDs. * * @param array $ids * @return \Illuminate\Database\Eloquent\Builder */ protected function newQueryForCollectionRestoration(array $ids) { $ids = array_values($ids); if (! str_contains($ids[0], ':')) { return parent::newQueryForRestoration($ids); } $query = $this->newQueryWithoutScopes(); foreach ($ids as $id) { $segments = explode(':', $id); $query->orWhere(function ($query) use ($segments) { return $query->where($segments[0], $segments[1]) ->where($segments[2], $segments[3]) ->where($segments[4], $segments[5]); }); } return $query; } } src/Illuminate/Database/Eloquent/Relations/HasOne.php 0000644 00000010056 15107327037 0016573 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Contracts\Database\Eloquent\SupportsPartialRelations; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Concerns\CanBeOneOfMany; use Illuminate\Database\Eloquent\Relations\Concerns\ComparesRelatedModels; use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; use Illuminate\Database\Query\JoinClause; class HasOne extends HasOneOrMany implements SupportsPartialRelations { use ComparesRelatedModels, CanBeOneOfMany, SupportsDefaultModels; /** * Get the results of the relationship. * * @return mixed */ public function getResults() { if (is_null($this->getParentKey())) { return $this->getDefaultFor($this->parent); } return $this->query->first() ?: $this->getDefaultFor($this->parent); } /** * Initialize the relation on a set of models. * * @param array $models * @param string $relation * @return array */ public function initRelation(array $models, $relation) { foreach ($models as $model) { $model->setRelation($relation, $this->getDefaultFor($model)); } return $models; } /** * Match the eagerly loaded results to their parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array */ public function match(array $models, Collection $results, $relation) { return $this->matchOne($models, $results, $relation); } /** * Add the constraints for an internal relationship existence query. * * Essentially, these queries compare on column names like "whereColumn". * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) { if ($this->isOneOfMany()) { $this->mergeOneOfManyJoinsTo($query); } return parent::getRelationExistenceQuery($query, $parentQuery, $columns); } /** * Add constraints for inner join subselect for one of many relationships. * * @param \Illuminate\Database\Eloquent\Builder $query * @param string|null $column * @param string|null $aggregate * @return void */ public function addOneOfManySubQueryConstraints(Builder $query, $column = null, $aggregate = null) { $query->addSelect($this->foreignKey); } /** * Get the columns that should be selected by the one of many subquery. * * @return array|string */ public function getOneOfManySubQuerySelectColumns() { return $this->foreignKey; } /** * Add join query constraints for one of many relationships. * * @param \Illuminate\Database\Query\JoinClause $join * @return void */ public function addOneOfManyJoinSubQueryConstraints(JoinClause $join) { $join->on($this->qualifySubSelectColumn($this->foreignKey), '=', $this->qualifyRelatedColumn($this->foreignKey)); } /** * Make a new related instance for the given model. * * @param \Illuminate\Database\Eloquent\Model $parent * @return \Illuminate\Database\Eloquent\Model */ public function newRelatedInstanceFor(Model $parent) { return $this->related->newInstance()->setAttribute( $this->getForeignKeyName(), $parent->{$this->localKey} ); } /** * Get the value of the model's foreign key. * * @param \Illuminate\Database\Eloquent\Model $model * @return mixed */ protected function getRelatedKeyFrom(Model $model) { return $model->getAttribute($this->getForeignKeyName()); } } src/Illuminate/Database/Eloquent/Relations/BelongsTo.php 0000644 00000025650 15107327037 0017320 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Concerns\ComparesRelatedModels; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; class BelongsTo extends Relation { use ComparesRelatedModels, InteractsWithDictionary, SupportsDefaultModels; /** * The child model instance of the relation. * * @var \Illuminate\Database\Eloquent\Model */ protected $child; /** * The foreign key of the parent model. * * @var string */ protected $foreignKey; /** * The associated key on the parent model. * * @var string */ protected $ownerKey; /** * The name of the relationship. * * @var string */ protected $relationName; /** * Create a new belongs to relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $child * @param string $foreignKey * @param string $ownerKey * @param string $relationName * @return void */ public function __construct(Builder $query, Model $child, $foreignKey, $ownerKey, $relationName) { $this->ownerKey = $ownerKey; $this->relationName = $relationName; $this->foreignKey = $foreignKey; // In the underlying base relationship class, this variable is referred to as // the "parent" since most relationships are not inversed. But, since this // one is we will create a "child" variable for much better readability. $this->child = $child; parent::__construct($query, $child); } /** * Get the results of the relationship. * * @return mixed */ public function getResults() { if (is_null($this->getForeignKeyFrom($this->child))) { return $this->getDefaultFor($this->parent); } return $this->query->first() ?: $this->getDefaultFor($this->parent); } /** * Set the base constraints on the relation query. * * @return void */ public function addConstraints() { if (static::$constraints) { // For belongs to relationships, which are essentially the inverse of has one // or has many relationships, we need to actually query on the primary key // of the related models matching on the foreign key that's on a parent. $table = $this->related->getTable(); $this->query->where($table.'.'.$this->ownerKey, '=', $this->getForeignKeyFrom($this->child)); } } /** * Set the constraints for an eager load of the relation. * * @param array $models * @return void */ public function addEagerConstraints(array $models) { // We'll grab the primary key name of the related models since it could be set to // a non-standard name and not "id". We will then construct the constraint for // our eagerly loading query so it returns the proper models from execution. $key = $this->related->getTable().'.'.$this->ownerKey; $whereIn = $this->whereInMethod($this->related, $this->ownerKey); $this->whereInEager($whereIn, $key, $this->getEagerModelKeys($models)); } /** * Gather the keys from an array of related models. * * @param array $models * @return array */ protected function getEagerModelKeys(array $models) { $keys = []; // First we need to gather all of the keys from the parent models so we know what // to query for via the eager loading query. We will add them to an array then // execute a "where in" statement to gather up all of those related records. foreach ($models as $model) { if (! is_null($value = $this->getForeignKeyFrom($model))) { $keys[] = $value; } } sort($keys); return array_values(array_unique($keys)); } /** * Initialize the relation on a set of models. * * @param array $models * @param string $relation * @return array */ public function initRelation(array $models, $relation) { foreach ($models as $model) { $model->setRelation($relation, $this->getDefaultFor($model)); } return $models; } /** * Match the eagerly loaded results to their parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array */ public function match(array $models, Collection $results, $relation) { // First we will get to build a dictionary of the child models by their primary // key of the relationship, then we can easily match the children back onto // the parents using that dictionary and the primary key of the children. $dictionary = []; foreach ($results as $result) { $attribute = $this->getDictionaryKey($this->getRelatedKeyFrom($result)); $dictionary[$attribute] = $result; } // Once we have the dictionary constructed, we can loop through all the parents // and match back onto their children using these keys of the dictionary and // the primary key of the children to map them onto the correct instances. foreach ($models as $model) { $attribute = $this->getDictionaryKey($this->getForeignKeyFrom($model)); if (isset($dictionary[$attribute])) { $model->setRelation($relation, $dictionary[$attribute]); } } return $models; } /** * Associate the model instance to the given parent. * * @param \Illuminate\Database\Eloquent\Model|int|string|null $model * @return \Illuminate\Database\Eloquent\Model */ public function associate($model) { $ownerKey = $model instanceof Model ? $model->getAttribute($this->ownerKey) : $model; $this->child->setAttribute($this->foreignKey, $ownerKey); if ($model instanceof Model) { $this->child->setRelation($this->relationName, $model); } else { $this->child->unsetRelation($this->relationName); } return $this->child; } /** * Dissociate previously associated model from the given parent. * * @return \Illuminate\Database\Eloquent\Model */ public function dissociate() { $this->child->setAttribute($this->foreignKey, null); return $this->child->setRelation($this->relationName, null); } /** * Alias of "dissociate" method. * * @return \Illuminate\Database\Eloquent\Model */ public function disassociate() { return $this->dissociate(); } /** * Add the constraints for a relationship query. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) { if ($parentQuery->getQuery()->from == $query->getQuery()->from) { return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns); } return $query->select($columns)->whereColumn( $this->getQualifiedForeignKeyName(), '=', $query->qualifyColumn($this->ownerKey) ); } /** * Add the constraints for a relationship query on the same table. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*']) { $query->select($columns)->from( $query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash() ); $query->getModel()->setTable($hash); return $query->whereColumn( $hash.'.'.$this->ownerKey, '=', $this->getQualifiedForeignKeyName() ); } /** * Determine if the related model has an auto-incrementing ID. * * @return bool */ protected function relationHasIncrementingId() { return $this->related->getIncrementing() && in_array($this->related->getKeyType(), ['int', 'integer']); } /** * Make a new related instance for the given model. * * @param \Illuminate\Database\Eloquent\Model $parent * @return \Illuminate\Database\Eloquent\Model */ protected function newRelatedInstanceFor(Model $parent) { return $this->related->newInstance(); } /** * Get the child of the relationship. * * @return \Illuminate\Database\Eloquent\Model */ public function getChild() { return $this->child; } /** * Get the foreign key of the relationship. * * @return string */ public function getForeignKeyName() { return $this->foreignKey; } /** * Get the fully qualified foreign key of the relationship. * * @return string */ public function getQualifiedForeignKeyName() { return $this->child->qualifyColumn($this->foreignKey); } /** * Get the key value of the child's foreign key. * * @return mixed */ public function getParentKey() { return $this->getForeignKeyFrom($this->child); } /** * Get the associated key of the relationship. * * @return string */ public function getOwnerKeyName() { return $this->ownerKey; } /** * Get the fully qualified associated key of the relationship. * * @return string */ public function getQualifiedOwnerKeyName() { return $this->related->qualifyColumn($this->ownerKey); } /** * Get the value of the model's associated key. * * @param \Illuminate\Database\Eloquent\Model $model * @return mixed */ protected function getRelatedKeyFrom(Model $model) { return $model->{$this->ownerKey}; } /** * Get the value of the model's foreign key. * * @param \Illuminate\Database\Eloquent\Model $model * @return mixed */ protected function getForeignKeyFrom(Model $model) { return $model->{$this->foreignKey}; } /** * Get the name of the relationship. * * @return string */ public function getRelationName() { return $this->relationName; } } src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php 0000644 00000035155 15107327037 0017730 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; use Illuminate\Database\UniqueConstraintViolationException; abstract class HasOneOrMany extends Relation { use InteractsWithDictionary; /** * The foreign key of the parent model. * * @var string */ protected $foreignKey; /** * The local key of the parent model. * * @var string */ protected $localKey; /** * Create a new has one or many relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $foreignKey * @param string $localKey * @return void */ public function __construct(Builder $query, Model $parent, $foreignKey, $localKey) { $this->localKey = $localKey; $this->foreignKey = $foreignKey; parent::__construct($query, $parent); } /** * Create and return an un-saved instance of the related model. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Model */ public function make(array $attributes = []) { return tap($this->related->newInstance($attributes), function ($instance) { $this->setForeignAttributesForCreate($instance); }); } /** * Create and return an un-saved instance of the related models. * * @param iterable $records * @return \Illuminate\Database\Eloquent\Collection */ public function makeMany($records) { $instances = $this->related->newCollection(); foreach ($records as $record) { $instances->push($this->make($record)); } return $instances; } /** * Set the base constraints on the relation query. * * @return void */ public function addConstraints() { if (static::$constraints) { $query = $this->getRelationQuery(); $query->where($this->foreignKey, '=', $this->getParentKey()); $query->whereNotNull($this->foreignKey); } } /** * Set the constraints for an eager load of the relation. * * @param array $models * @return void */ public function addEagerConstraints(array $models) { $whereIn = $this->whereInMethod($this->parent, $this->localKey); $this->whereInEager( $whereIn, $this->foreignKey, $this->getKeys($models, $this->localKey), $this->getRelationQuery() ); } /** * Match the eagerly loaded results to their single parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array */ public function matchOne(array $models, Collection $results, $relation) { return $this->matchOneOrMany($models, $results, $relation, 'one'); } /** * Match the eagerly loaded results to their many parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array */ public function matchMany(array $models, Collection $results, $relation) { return $this->matchOneOrMany($models, $results, $relation, 'many'); } /** * Match the eagerly loaded results to their many parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @param string $type * @return array */ protected function matchOneOrMany(array $models, Collection $results, $relation, $type) { $dictionary = $this->buildDictionary($results); // Once we have the dictionary we can simply spin through the parent models to // link them up with their children using the keyed dictionary to make the // matching very convenient and easy work. Then we'll just return them. foreach ($models as $model) { if (isset($dictionary[$key = $this->getDictionaryKey($model->getAttribute($this->localKey))])) { $model->setRelation( $relation, $this->getRelationValue($dictionary, $key, $type) ); } } return $models; } /** * Get the value of a relationship by one or many type. * * @param array $dictionary * @param string $key * @param string $type * @return mixed */ protected function getRelationValue(array $dictionary, $key, $type) { $value = $dictionary[$key]; return $type === 'one' ? reset($value) : $this->related->newCollection($value); } /** * Build model dictionary keyed by the relation's foreign key. * * @param \Illuminate\Database\Eloquent\Collection $results * @return array */ protected function buildDictionary(Collection $results) { $foreign = $this->getForeignKeyName(); return $results->mapToDictionary(function ($result) use ($foreign) { return [$this->getDictionaryKey($result->{$foreign}) => $result]; })->all(); } /** * Find a model by its primary key or return a new instance of the related model. * * @param mixed $id * @param array $columns * @return \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model */ public function findOrNew($id, $columns = ['*']) { if (is_null($instance = $this->find($id, $columns))) { $instance = $this->related->newInstance(); $this->setForeignAttributesForCreate($instance); } return $instance; } /** * Get the first related model record matching the attributes or instantiate it. * * @param array $attributes * @param array $values * @return \Illuminate\Database\Eloquent\Model */ public function firstOrNew(array $attributes = [], array $values = []) { if (is_null($instance = $this->where($attributes)->first())) { $instance = $this->related->newInstance(array_merge($attributes, $values)); $this->setForeignAttributesForCreate($instance); } return $instance; } /** * Get the first record matching the attributes. If the record is not found, create it. * * @param array $attributes * @param array $values * @return \Illuminate\Database\Eloquent\Model */ public function firstOrCreate(array $attributes = [], array $values = []) { if (is_null($instance = (clone $this)->where($attributes)->first())) { $instance = $this->createOrFirst($attributes, $values); } return $instance; } /** * Attempt to create the record. If a unique constraint violation occurs, attempt to find the matching record. * * @param array $attributes * @param array $values * @return \Illuminate\Database\Eloquent\Model */ public function createOrFirst(array $attributes = [], array $values = []) { try { return $this->getQuery()->withSavepointIfNeeded(fn () => $this->create(array_merge($attributes, $values))); } catch (UniqueConstraintViolationException $e) { return $this->useWritePdo()->where($attributes)->first() ?? throw $e; } } /** * Create or update a related record matching the attributes, and fill it with values. * * @param array $attributes * @param array $values * @return \Illuminate\Database\Eloquent\Model */ public function updateOrCreate(array $attributes, array $values = []) { return tap($this->firstOrCreate($attributes, $values), function ($instance) use ($values) { if (! $instance->wasRecentlyCreated) { $instance->fill($values)->save(); } }); } /** * Attach a model instance to the parent model. * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Database\Eloquent\Model|false */ public function save(Model $model) { $this->setForeignAttributesForCreate($model); return $model->save() ? $model : false; } /** * Attach a model instance without raising any events to the parent model. * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Database\Eloquent\Model|false */ public function saveQuietly(Model $model) { return Model::withoutEvents(function () use ($model) { return $this->save($model); }); } /** * Attach a collection of models to the parent instance. * * @param iterable $models * @return iterable */ public function saveMany($models) { foreach ($models as $model) { $this->save($model); } return $models; } /** * Attach a collection of models to the parent instance without raising any events to the parent model. * * @param iterable $models * @return iterable */ public function saveManyQuietly($models) { return Model::withoutEvents(function () use ($models) { return $this->saveMany($models); }); } /** * Create a new instance of the related model. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Model */ public function create(array $attributes = []) { return tap($this->related->newInstance($attributes), function ($instance) { $this->setForeignAttributesForCreate($instance); $instance->save(); }); } /** * Create a new instance of the related model without raising any events to the parent model. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Model */ public function createQuietly(array $attributes = []) { return Model::withoutEvents(fn () => $this->create($attributes)); } /** * Create a new instance of the related model. Allow mass-assignment. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Model */ public function forceCreate(array $attributes = []) { $attributes[$this->getForeignKeyName()] = $this->getParentKey(); return $this->related->forceCreate($attributes); } /** * Create a new instance of the related model with mass assignment without raising model events. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Model */ public function forceCreateQuietly(array $attributes = []) { return Model::withoutEvents(fn () => $this->forceCreate($attributes)); } /** * Create a Collection of new instances of the related model. * * @param iterable $records * @return \Illuminate\Database\Eloquent\Collection */ public function createMany(iterable $records) { $instances = $this->related->newCollection(); foreach ($records as $record) { $instances->push($this->create($record)); } return $instances; } /** * Create a Collection of new instances of the related model without raising any events to the parent model. * * @param iterable $records * @return \Illuminate\Database\Eloquent\Collection */ public function createManyQuietly(iterable $records) { return Model::withoutEvents(fn () => $this->createMany($records)); } /** * Set the foreign ID for creating a related model. * * @param \Illuminate\Database\Eloquent\Model $model * @return void */ protected function setForeignAttributesForCreate(Model $model) { $model->setAttribute($this->getForeignKeyName(), $this->getParentKey()); } /** * Add the constraints for a relationship query. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) { if ($query->getQuery()->from == $parentQuery->getQuery()->from) { return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns); } return parent::getRelationExistenceQuery($query, $parentQuery, $columns); } /** * Add the constraints for a relationship query on the same table. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*']) { $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()); $query->getModel()->setTable($hash); return $query->select($columns)->whereColumn( $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->getForeignKeyName() ); } /** * Get the key for comparing against the parent key in "has" query. * * @return string */ public function getExistenceCompareKey() { return $this->getQualifiedForeignKeyName(); } /** * Get the key value of the parent's local key. * * @return mixed */ public function getParentKey() { return $this->parent->getAttribute($this->localKey); } /** * Get the fully qualified parent key name. * * @return string */ public function getQualifiedParentKeyName() { return $this->parent->qualifyColumn($this->localKey); } /** * Get the plain foreign key. * * @return string */ public function getForeignKeyName() { $segments = explode('.', $this->getQualifiedForeignKeyName()); return end($segments); } /** * Get the foreign key for the relationship. * * @return string */ public function getQualifiedForeignKeyName() { return $this->foreignKey; } /** * Get the local key for the relationship. * * @return string */ public function getLocalKeyName() { return $this->localKey; } } src/Illuminate/Database/Eloquent/Relations/MorphToMany.php 0000644 00000013375 15107327037 0017642 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; class MorphToMany extends BelongsToMany { /** * The type of the polymorphic relation. * * @var string */ protected $morphType; /** * The class name of the morph type constraint. * * @var string */ protected $morphClass; /** * Indicates if we are connecting the inverse of the relation. * * This primarily affects the morphClass constraint. * * @var bool */ protected $inverse; /** * Create a new morph to many relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $name * @param string $table * @param string $foreignPivotKey * @param string $relatedPivotKey * @param string $parentKey * @param string $relatedKey * @param string|null $relationName * @param bool $inverse * @return void */ public function __construct(Builder $query, Model $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null, $inverse = false) { $this->inverse = $inverse; $this->morphType = $name.'_type'; $this->morphClass = $inverse ? $query->getModel()->getMorphClass() : $parent->getMorphClass(); parent::__construct( $query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName ); } /** * Set the where clause for the relation query. * * @return $this */ protected function addWhereConstraints() { parent::addWhereConstraints(); $this->query->where($this->qualifyPivotColumn($this->morphType), $this->morphClass); return $this; } /** * Set the constraints for an eager load of the relation. * * @param array $models * @return void */ public function addEagerConstraints(array $models) { parent::addEagerConstraints($models); $this->query->where($this->qualifyPivotColumn($this->morphType), $this->morphClass); } /** * Create a new pivot attachment record. * * @param int $id * @param bool $timed * @return array */ protected function baseAttachRecord($id, $timed) { return Arr::add( parent::baseAttachRecord($id, $timed), $this->morphType, $this->morphClass ); } /** * Add the constraints for a relationship count query. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) { return parent::getRelationExistenceQuery($query, $parentQuery, $columns)->where( $this->qualifyPivotColumn($this->morphType), $this->morphClass ); } /** * Get the pivot models that are currently attached. * * @return \Illuminate\Support\Collection */ protected function getCurrentlyAttachedPivots() { return parent::getCurrentlyAttachedPivots()->map(function ($record) { return $record instanceof MorphPivot ? $record->setMorphType($this->morphType) ->setMorphClass($this->morphClass) : $record; }); } /** * Create a new query builder for the pivot table. * * @return \Illuminate\Database\Query\Builder */ public function newPivotQuery() { return parent::newPivotQuery()->where($this->morphType, $this->morphClass); } /** * Create a new pivot model instance. * * @param array $attributes * @param bool $exists * @return \Illuminate\Database\Eloquent\Relations\Pivot */ public function newPivot(array $attributes = [], $exists = false) { $using = $this->using; $attributes = array_merge([$this->morphType => $this->morphClass], $attributes); $pivot = $using ? $using::fromRawAttributes($this->parent, $attributes, $this->table, $exists) : MorphPivot::fromAttributes($this->parent, $attributes, $this->table, $exists); $pivot->setPivotKeys($this->foreignPivotKey, $this->relatedPivotKey) ->setMorphType($this->morphType) ->setMorphClass($this->morphClass); return $pivot; } /** * Get the pivot columns for the relation. * * "pivot_" is prefixed at each column for easy removal later. * * @return array */ protected function aliasedPivotColumns() { $defaults = [$this->foreignPivotKey, $this->relatedPivotKey, $this->morphType]; return collect(array_merge($defaults, $this->pivotColumns))->map(function ($column) { return $this->qualifyPivotColumn($column).' as pivot_'.$column; })->unique()->all(); } /** * Get the foreign key "type" name. * * @return string */ public function getMorphType() { return $this->morphType; } /** * Get the class name of the parent model. * * @return string */ public function getMorphClass() { return $this->morphClass; } /** * Get the indicator for a reverse relationship. * * @return bool */ public function getInverse() { return $this->inverse; } } src/Illuminate/Database/Eloquent/Relations/MorphTo.php 0000644 00000030134 15107327037 0017005 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use BadMethodCallException; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary; class MorphTo extends BelongsTo { use InteractsWithDictionary; /** * The type of the polymorphic relation. * * @var string */ protected $morphType; /** * The models whose relations are being eager loaded. * * @var \Illuminate\Database\Eloquent\Collection */ protected $models; /** * All of the models keyed by ID. * * @var array */ protected $dictionary = []; /** * A buffer of dynamic calls to query macros. * * @var array */ protected $macroBuffer = []; /** * A map of relations to load for each individual morph type. * * @var array */ protected $morphableEagerLoads = []; /** * A map of relationship counts to load for each individual morph type. * * @var array */ protected $morphableEagerLoadCounts = []; /** * A map of constraints to apply for each individual morph type. * * @var array */ protected $morphableConstraints = []; /** * Create a new morph to relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $foreignKey * @param string $ownerKey * @param string $type * @param string $relation * @return void */ public function __construct(Builder $query, Model $parent, $foreignKey, $ownerKey, $type, $relation) { $this->morphType = $type; parent::__construct($query, $parent, $foreignKey, $ownerKey, $relation); } /** * Set the constraints for an eager load of the relation. * * @param array $models * @return void */ public function addEagerConstraints(array $models) { $this->buildDictionary($this->models = Collection::make($models)); } /** * Build a dictionary with the models. * * @param \Illuminate\Database\Eloquent\Collection $models * @return void */ protected function buildDictionary(Collection $models) { foreach ($models as $model) { if ($model->{$this->morphType}) { $morphTypeKey = $this->getDictionaryKey($model->{$this->morphType}); $foreignKeyKey = $this->getDictionaryKey($model->{$this->foreignKey}); $this->dictionary[$morphTypeKey][$foreignKeyKey][] = $model; } } } /** * Get the results of the relationship. * * Called via eager load method of Eloquent query builder. * * @return mixed */ public function getEager() { foreach (array_keys($this->dictionary) as $type) { $this->matchToMorphParents($type, $this->getResultsByType($type)); } return $this->models; } /** * Get all of the relation results for a type. * * @param string $type * @return \Illuminate\Database\Eloquent\Collection */ protected function getResultsByType($type) { $instance = $this->createModelByType($type); $ownerKey = $this->ownerKey ?? $instance->getKeyName(); $query = $this->replayMacros($instance->newQuery()) ->mergeConstraintsFrom($this->getQuery()) ->with(array_merge( $this->getQuery()->getEagerLoads(), (array) ($this->morphableEagerLoads[get_class($instance)] ?? []) )) ->withCount( (array) ($this->morphableEagerLoadCounts[get_class($instance)] ?? []) ); if ($callback = ($this->morphableConstraints[get_class($instance)] ?? null)) { $callback($query); } $whereIn = $this->whereInMethod($instance, $ownerKey); return $query->{$whereIn}( $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, $keyType) { return $keyType !== 'string' ? array_keys($this->dictionary[$type]) : array_map(function ($modelId) { return (string) $modelId; }, array_filter(array_keys($this->dictionary[$type]))); } /** * Create a new model instance by type. * * @param string $type * @return \Illuminate\Database\Eloquent\Model */ public function createModelByType($type) { $class = Model::getActualClassNameForMorph($type); return tap(new $class, function ($instance) { if (! $instance->getConnectionName()) { $instance->setConnection($this->getConnection()->getName()); } }); } /** * Match the eagerly loaded results to their parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array */ public function match(array $models, Collection $results, $relation) { return $models; } /** * Match the results for a given type to their parents. * * @param string $type * @param \Illuminate\Database\Eloquent\Collection $results * @return void */ protected function matchToMorphParents($type, Collection $results) { foreach ($results as $result) { $ownerKey = ! is_null($this->ownerKey) ? $this->getDictionaryKey($result->{$this->ownerKey}) : $result->getKey(); if (isset($this->dictionary[$type][$ownerKey])) { foreach ($this->dictionary[$type][$ownerKey] as $model) { $model->setRelation($this->relationName, $result); } } } } /** * Associate the model instance to the given parent. * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Database\Eloquent\Model */ public function associate($model) { if ($model instanceof Model) { $foreignKey = $this->ownerKey && $model->{$this->ownerKey} ? $this->ownerKey : $model->getKeyName(); } $this->parent->setAttribute( $this->foreignKey, $model instanceof Model ? $model->{$foreignKey} : null ); $this->parent->setAttribute( $this->morphType, $model instanceof Model ? $model->getMorphClass() : null ); return $this->parent->setRelation($this->relationName, $model); } /** * Dissociate previously associated model from the given parent. * * @return \Illuminate\Database\Eloquent\Model */ public function dissociate() { $this->parent->setAttribute($this->foreignKey, null); $this->parent->setAttribute($this->morphType, null); return $this->parent->setRelation($this->relationName, null); } /** * Touch all of the related models for the relationship. * * @return void */ public function touch() { if (! is_null($this->child->{$this->foreignKey})) { parent::touch(); } } /** * Make a new related instance for the given model. * * @param \Illuminate\Database\Eloquent\Model $parent * @return \Illuminate\Database\Eloquent\Model */ protected function newRelatedInstanceFor(Model $parent) { return $parent->{$this->getRelationName()}()->getRelated()->newInstance(); } /** * Get the foreign key "type" name. * * @return string */ public function getMorphType() { return $this->morphType; } /** * Get the dictionary used by the relationship. * * @return array */ public function getDictionary() { return $this->dictionary; } /** * Specify which relations to load for a given morph type. * * @param array $with * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function morphWith(array $with) { $this->morphableEagerLoads = array_merge( $this->morphableEagerLoads, $with ); return $this; } /** * Specify which relationship counts to load for a given morph type. * * @param array $withCount * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function morphWithCount(array $withCount) { $this->morphableEagerLoadCounts = array_merge( $this->morphableEagerLoadCounts, $withCount ); return $this; } /** * Specify constraints on the query for a given morph type. * * @param array $callbacks * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function constrain(array $callbacks) { $this->morphableConstraints = array_merge( $this->morphableConstraints, $callbacks ); return $this; } /** * Indicate that soft deleted models should be included in the results. * * @return $this */ public function withTrashed() { $callback = fn ($query) => $query->hasMacro('withTrashed') ? $query->withTrashed() : $query; $this->macroBuffer[] = [ 'method' => 'when', 'parameters' => [true, $callback], ]; return $this->when(true, $callback); } /** * Indicate that soft deleted models should not be included in the results. * * @return $this */ public function withoutTrashed() { $callback = fn ($query) => $query->hasMacro('withoutTrashed') ? $query->withoutTrashed() : $query; $this->macroBuffer[] = [ 'method' => 'when', 'parameters' => [true, $callback], ]; return $this->when(true, $callback); } /** * Indicate that only soft deleted models should be included in the results. * * @return $this */ public function onlyTrashed() { $callback = fn ($query) => $query->hasMacro('onlyTrashed') ? $query->onlyTrashed() : $query; $this->macroBuffer[] = [ 'method' => 'when', 'parameters' => [true, $callback], ]; return $this->when(true, $callback); } /** * Replay stored macro calls on the actual related instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ protected function replayMacros(Builder $query) { foreach ($this->macroBuffer as $macro) { $query->{$macro['method']}(...$macro['parameters']); } return $query; } /** * Handle dynamic method calls to the relationship. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { try { $result = parent::__call($method, $parameters); if (in_array($method, ['select', 'selectRaw', 'selectSub', 'addSelect', 'withoutGlobalScopes'])) { $this->macroBuffer[] = compact('method', 'parameters'); } return $result; } // If we tried to call a method that does not exist on the parent Builder instance, // we'll assume that we want to call a query macro (e.g. withTrashed) that only // exists on related models. We will just store the call and replay it later. catch (BadMethodCallException) { $this->macroBuffer[] = compact('method', 'parameters'); return $this; } } } src/Illuminate/Database/Eloquent/SoftDeletes.php 0000644 00000014427 15107327037 0015705 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; /** * @method static \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withTrashed(bool $withTrashed = true) * @method static \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder onlyTrashed() * @method static \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withoutTrashed() */ trait SoftDeletes { /** * Indicates if the model is currently force deleting. * * @var bool */ protected $forceDeleting = false; /** * Boot the soft deleting trait for a model. * * @return void */ public static function bootSoftDeletes() { static::addGlobalScope(new SoftDeletingScope); } /** * Initialize the soft deleting trait for an instance. * * @return void */ public function initializeSoftDeletes() { if (! isset($this->casts[$this->getDeletedAtColumn()])) { $this->casts[$this->getDeletedAtColumn()] = 'datetime'; } } /** * Force a hard delete on a soft deleted model. * * @return bool|null */ public function forceDelete() { if ($this->fireModelEvent('forceDeleting') === false) { return false; } $this->forceDeleting = true; return tap($this->delete(), function ($deleted) { $this->forceDeleting = false; if ($deleted) { $this->fireModelEvent('forceDeleted', false); } }); } /** * Force a hard delete on a soft deleted model without raising any events. * * @return bool|null */ public function forceDeleteQuietly() { return static::withoutEvents(fn () => $this->forceDelete()); } /** * Perform the actual delete query on this model instance. * * @return mixed */ protected function performDeleteOnModel() { if ($this->forceDeleting) { return tap($this->setKeysForSaveQuery($this->newModelQuery())->forceDelete(), function () { $this->exists = false; }); } return $this->runSoftDelete(); } /** * Perform the actual delete query on this model instance. * * @return void */ protected function runSoftDelete() { $query = $this->setKeysForSaveQuery($this->newModelQuery()); $time = $this->freshTimestamp(); $columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)]; $this->{$this->getDeletedAtColumn()} = $time; if ($this->usesTimestamps() && ! is_null($this->getUpdatedAtColumn())) { $this->{$this->getUpdatedAtColumn()} = $time; $columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time); } $query->update($columns); $this->syncOriginalAttributes(array_keys($columns)); $this->fireModelEvent('trashed', false); } /** * Restore a soft-deleted model instance. * * @return bool */ public function restore() { // If the restoring event does not return false, we will proceed with this // restore operation. Otherwise, we bail out so the developer will stop // the restore totally. We will clear the deleted timestamp and save. if ($this->fireModelEvent('restoring') === false) { return false; } $this->{$this->getDeletedAtColumn()} = null; // Once we have saved the model, we will fire the "restored" event so this // developer will do anything they need to after a restore operation is // totally finished. Then we will return the result of the save call. $this->exists = true; $result = $this->save(); $this->fireModelEvent('restored', false); return $result; } /** * Restore a soft-deleted model instance without raising any events. * * @return bool */ public function restoreQuietly() { return static::withoutEvents(fn () => $this->restore()); } /** * Determine if the model instance has been soft-deleted. * * @return bool */ public function trashed() { return ! is_null($this->{$this->getDeletedAtColumn()}); } /** * Register a "softDeleted" model event callback with the dispatcher. * * @param \Closure|string $callback * @return void */ public static function softDeleted($callback) { static::registerModelEvent('trashed', $callback); } /** * Register a "restoring" model event callback with the dispatcher. * * @param \Closure|string $callback * @return void */ public static function restoring($callback) { static::registerModelEvent('restoring', $callback); } /** * Register a "restored" model event callback with the dispatcher. * * @param \Closure|string $callback * @return void */ public static function restored($callback) { static::registerModelEvent('restored', $callback); } /** * Register a "forceDeleting" model event callback with the dispatcher. * * @param \Closure|string $callback * @return void */ public static function forceDeleting($callback) { static::registerModelEvent('forceDeleting', $callback); } /** * Register a "forceDeleted" model event callback with the dispatcher. * * @param \Closure|string $callback * @return void */ public static function forceDeleted($callback) { static::registerModelEvent('forceDeleted', $callback); } /** * Determine if the model is currently force deleting. * * @return bool */ public function isForceDeleting() { return $this->forceDeleting; } /** * Get the name of the "deleted at" column. * * @return string */ public function getDeletedAtColumn() { return defined(static::class.'::DELETED_AT') ? static::DELETED_AT : 'deleted_at'; } /** * Get the fully qualified "deleted at" column. * * @return string */ public function getQualifiedDeletedAtColumn() { return $this->qualifyColumn($this->getDeletedAtColumn()); } } src/Illuminate/Database/Eloquent/RelationNotFoundException.php 0000644 00000001751 15107327037 0020571 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use RuntimeException; class RelationNotFoundException extends RuntimeException { /** * The name of the affected Eloquent model. * * @var string */ public $model; /** * The name of the relation. * * @var string */ public $relation; /** * Create a new exception instance. * * @param object $model * @param string $relation * @param string|null $type * @return static */ public static function make($model, $relation, $type = null) { $class = get_class($model); $instance = new static( is_null($type) ? "Call to undefined relationship [{$relation}] on model [{$class}]." : "Call to undefined relationship [{$relation}] on model [{$class}] of type [{$type}].", ); $instance->model = $class; $instance->relation = $relation; return $instance; } } src/Illuminate/Database/Eloquent/MassAssignmentException.php 0000644 00000000211 15107327037 0020261 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use RuntimeException; class MassAssignmentException extends RuntimeException { // } src/Illuminate/Database/Eloquent/BroadcastableModelEventOccurred.php 0000644 00000006322 15107327037 0021657 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Queue\SerializesModels; class BroadcastableModelEventOccurred implements ShouldBroadcast { use InteractsWithSockets, SerializesModels; /** * The model instance corresponding to the event. * * @var \Illuminate\Database\Eloquent\Model */ public $model; /** * The event name (created, updated, etc.). * * @var string */ protected $event; /** * The channels that the event should be broadcast on. * * @var array */ protected $channels = []; /** * The queue connection that should be used to queue the broadcast job. * * @var string */ public $connection; /** * The queue that should be used to queue the broadcast job. * * @var string */ public $queue; /** * Indicates whether the job should be dispatched after all database transactions have committed. * * @var bool|null */ public $afterCommit; /** * Create a new event instance. * * @param \Illuminate\Database\Eloquent\Model $model * @param string $event * @return void */ public function __construct($model, $event) { $this->model = $model; $this->event = $event; } /** * The channels the event should broadcast on. * * @return array */ public function broadcastOn() { $channels = empty($this->channels) ? ($this->model->broadcastOn($this->event) ?: []) : $this->channels; return collect($channels)->map(function ($channel) { return $channel instanceof Model ? new PrivateChannel($channel) : $channel; })->all(); } /** * The name the event should broadcast as. * * @return string */ public function broadcastAs() { $default = class_basename($this->model).ucfirst($this->event); return method_exists($this->model, 'broadcastAs') ? ($this->model->broadcastAs($this->event) ?: $default) : $default; } /** * Get the data that should be sent with the broadcasted event. * * @return array|null */ public function broadcastWith() { return method_exists($this->model, 'broadcastWith') ? $this->model->broadcastWith($this->event) : null; } /** * Manually specify the channels the event should broadcast on. * * @param array $channels * @return $this */ public function onChannels(array $channels) { $this->channels = $channels; return $this; } /** * Determine if the event should be broadcast synchronously. * * @return bool */ public function shouldBroadcastNow() { return $this->event === 'deleted' && ! method_exists($this->model, 'bootSoftDeletes'); } /** * Get the event name. * * @return string */ public function event() { return $this->event; } } src/Illuminate/Database/Eloquent/MissingAttributeException.php 0000644 00000001064 15107327037 0020631 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use OutOfBoundsException; class MissingAttributeException extends OutOfBoundsException { /** * Create a new missing attribute exception instance. * * @param \Illuminate\Database\Eloquent\Model $model * @param string $key * @return void */ public function __construct($model, $key) { parent::__construct(sprintf( 'The attribute [%s] either does not exist or was not retrieved for model [%s].', $key, get_class($model) )); } } src/Illuminate/Database/Eloquent/ModelNotFoundException.php 0000644 00000002613 15107327037 0020052 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use Illuminate\Database\RecordsNotFoundException; use Illuminate\Support\Arr; /** * @template TModel of \Illuminate\Database\Eloquent\Model */ class ModelNotFoundException extends RecordsNotFoundException { /** * Name of the affected Eloquent model. * * @var class-string<TModel> */ protected $model; /** * The affected model IDs. * * @var array<int, int|string> */ protected $ids; /** * Set the affected Eloquent model and instance ids. * * @param class-string<TModel> $model * @param array<int, int|string>|int|string $ids * @return $this */ public function setModel($model, $ids = []) { $this->model = $model; $this->ids = Arr::wrap($ids); $this->message = "No query results for model [{$model}]"; if (count($this->ids) > 0) { $this->message .= ' '.implode(', ', $this->ids); } else { $this->message .= '.'; } return $this; } /** * Get the affected Eloquent model. * * @return class-string<TModel> */ public function getModel() { return $this->model; } /** * Get the affected Eloquent model IDs. * * @return array<int, int|string> */ public function getIds() { return $this->ids; } } src/Illuminate/Database/Eloquent/MassPrunable.php 0000644 00000002257 15107327037 0016056 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use Illuminate\Database\Events\ModelsPruned; use LogicException; trait MassPrunable { /** * Prune all prunable models in the database. * * @param int $chunkSize * @return int */ public function pruneAll(int $chunkSize = 1000) { $query = tap($this->prunable(), function ($query) use ($chunkSize) { $query->when(! $query->getQuery()->limit, function ($query) use ($chunkSize) { $query->limit($chunkSize); }); }); $total = 0; do { $total += $count = in_array(SoftDeletes::class, class_uses_recursive(get_class($this))) ? $query->forceDelete() : $query->delete(); if ($count > 0) { event(new ModelsPruned(static::class, $total)); } } while ($count > 0); return $total; } /** * Get the prunable model query. * * @return \Illuminate\Database\Eloquent\Builder */ public function prunable() { throw new LogicException('Please implement the prunable method on your model.'); } } src/Illuminate/Database/Eloquent/BroadcastsEventsAfterCommit.php 0000644 00000000534 15107327037 0021063 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; trait BroadcastsEventsAfterCommit { use BroadcastsEvents; /** * Determine if the model event broadcast queued job should be dispatched after all transactions are committed. * * @return bool */ public function broadcastAfterCommit() { return true; } } src/Illuminate/Database/Eloquent/BroadcastsEvents.php 0000644 00000013354 15107327037 0016734 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use Illuminate\Support\Arr; trait BroadcastsEvents { /** * Boot the event broadcasting trait. * * @return void */ public static function bootBroadcastsEvents() { static::created(function ($model) { $model->broadcastCreated(); }); static::updated(function ($model) { $model->broadcastUpdated(); }); if (method_exists(static::class, 'bootSoftDeletes')) { static::softDeleted(function ($model) { $model->broadcastTrashed(); }); static::restored(function ($model) { $model->broadcastRestored(); }); } static::deleted(function ($model) { $model->broadcastDeleted(); }); } /** * Broadcast that the model was created. * * @param \Illuminate\Broadcasting\Channel|\Illuminate\Contracts\Broadcasting\HasBroadcastChannel|array|null $channels * @return \Illuminate\Broadcasting\PendingBroadcast */ public function broadcastCreated($channels = null) { return $this->broadcastIfBroadcastChannelsExistForEvent( $this->newBroadcastableModelEvent('created'), 'created', $channels ); } /** * Broadcast that the model was updated. * * @param \Illuminate\Broadcasting\Channel|\Illuminate\Contracts\Broadcasting\HasBroadcastChannel|array|null $channels * @return \Illuminate\Broadcasting\PendingBroadcast */ public function broadcastUpdated($channels = null) { return $this->broadcastIfBroadcastChannelsExistForEvent( $this->newBroadcastableModelEvent('updated'), 'updated', $channels ); } /** * Broadcast that the model was trashed. * * @param \Illuminate\Broadcasting\Channel|\Illuminate\Contracts\Broadcasting\HasBroadcastChannel|array|null $channels * @return \Illuminate\Broadcasting\PendingBroadcast */ public function broadcastTrashed($channels = null) { return $this->broadcastIfBroadcastChannelsExistForEvent( $this->newBroadcastableModelEvent('trashed'), 'trashed', $channels ); } /** * Broadcast that the model was restored. * * @param \Illuminate\Broadcasting\Channel|\Illuminate\Contracts\Broadcasting\HasBroadcastChannel|array|null $channels * @return \Illuminate\Broadcasting\PendingBroadcast */ public function broadcastRestored($channels = null) { return $this->broadcastIfBroadcastChannelsExistForEvent( $this->newBroadcastableModelEvent('restored'), 'restored', $channels ); } /** * Broadcast that the model was deleted. * * @param \Illuminate\Broadcasting\Channel|\Illuminate\Contracts\Broadcasting\HasBroadcastChannel|array|null $channels * @return \Illuminate\Broadcasting\PendingBroadcast */ public function broadcastDeleted($channels = null) { return $this->broadcastIfBroadcastChannelsExistForEvent( $this->newBroadcastableModelEvent('deleted'), 'deleted', $channels ); } /** * Broadcast the given event instance if channels are configured for the model event. * * @param mixed $instance * @param string $event * @param mixed $channels * @return \Illuminate\Broadcasting\PendingBroadcast|null */ protected function broadcastIfBroadcastChannelsExistForEvent($instance, $event, $channels = null) { if (! static::$isBroadcasting) { return; } if (! empty($this->broadcastOn($event)) || ! empty($channels)) { return broadcast($instance->onChannels(Arr::wrap($channels))); } } /** * Create a new broadcastable model event event. * * @param string $event * @return mixed */ public function newBroadcastableModelEvent($event) { return tap($this->newBroadcastableEvent($event), function ($event) { $event->connection = property_exists($this, 'broadcastConnection') ? $this->broadcastConnection : $this->broadcastConnection(); $event->queue = property_exists($this, 'broadcastQueue') ? $this->broadcastQueue : $this->broadcastQueue(); $event->afterCommit = property_exists($this, 'broadcastAfterCommit') ? $this->broadcastAfterCommit : $this->broadcastAfterCommit(); }); } /** * Create a new broadcastable model event for the model. * * @param string $event * @return \Illuminate\Database\Eloquent\BroadcastableModelEventOccurred */ protected function newBroadcastableEvent(string $event) { return new BroadcastableModelEventOccurred($this, $event); } /** * Get the channels that model events should broadcast on. * * @param string $event * @return \Illuminate\Broadcasting\Channel|array */ public function broadcastOn($event) { return [$this]; } /** * Get the queue connection that should be used to broadcast model events. * * @return string|null */ public function broadcastConnection() { // } /** * Get the queue that should be used to broadcast model events. * * @return string|null */ public function broadcastQueue() { // } /** * Determine if the model event broadcast queued job should be dispatched after all transactions are committed. * * @return bool */ public function broadcastAfterCommit() { return false; } } src/Illuminate/Database/Eloquent/Casts/AsStringable.php 0000644 00000001667 15107327037 0017121 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Casts; use Illuminate\Contracts\Database\Eloquent\Castable; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use Illuminate\Support\Str; class AsStringable implements Castable { /** * Get the caster class to use when casting from / to this cast target. * * @param array $arguments * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Support\Stringable, string|\Stringable> */ public static function castUsing(array $arguments) { return new class implements CastsAttributes { public function get($model, $key, $value, $attributes) { return isset($value) ? Str::of($value) : null; } public function set($model, $key, $value, $attributes) { return isset($value) ? (string) $value : null; } }; } } src/Illuminate/Database/Eloquent/Casts/AsEnumArrayObject.php 0000644 00000005147 15107327037 0020056 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Casts; use BackedEnum; use Illuminate\Contracts\Database\Eloquent\Castable; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use Illuminate\Support\Collection; class AsEnumArrayObject implements Castable { /** * Get the caster class to use when casting from / to this cast target. * * @template TEnum * * @param array{class-string<TEnum>} $arguments * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Database\Eloquent\Casts\ArrayObject<array-key, TEnum>, iterable<TEnum>> */ public static function castUsing(array $arguments) { return new class($arguments) implements CastsAttributes { protected $arguments; public function __construct(array $arguments) { $this->arguments = $arguments; } public function get($model, $key, $value, $attributes) { if (! isset($attributes[$key]) || is_null($attributes[$key])) { return; } $data = Json::decode($attributes[$key]); if (! is_array($data)) { return; } $enumClass = $this->arguments[0]; return new ArrayObject((new Collection($data))->map(function ($value) use ($enumClass) { return is_subclass_of($enumClass, BackedEnum::class) ? $enumClass::from($value) : constant($enumClass.'::'.$value); })->toArray()); } public function set($model, $key, $value, $attributes) { if ($value === null) { return [$key => null]; } $storable = []; foreach ($value as $enum) { $storable[] = $this->getStorableEnumValue($enum); } return [$key => Json::encode($storable)]; } public function serialize($model, string $key, $value, array $attributes) { return (new Collection($value->getArrayCopy()))->map(function ($enum) { return $this->getStorableEnumValue($enum); })->toArray(); } protected function getStorableEnumValue($enum) { if (is_string($enum) || is_int($enum)) { return $enum; } return $enum instanceof BackedEnum ? $enum->value : $enum->name; } }; } } src/Illuminate/Database/Eloquent/Casts/Json.php 0000644 00000002204 15107327037 0015440 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Casts; class Json { /** * The custom JSON encoder. * * @var callable|null */ protected static $encoder; /** * The custom JSON decode. * * @var callable|null */ protected static $decoder; /** * Encode the given value. */ public static function encode(mixed $value): mixed { return isset(static::$encoder) ? (static::$encoder)($value) : json_encode($value); } /** * Decode the given value. */ public static function decode(mixed $value, ?bool $associative = true): mixed { return isset(static::$decoder) ? (static::$decoder)($value, $associative) : json_decode($value, $associative); } /** * Encode all values using the given callable. */ public static function encodeUsing(?callable $encoder): void { static::$encoder = $encoder; } /** * Decode all values using the given callable. */ public static function decodeUsing(?callable $decoder): void { static::$decoder = $decoder; } } src/Illuminate/Database/Eloquent/Casts/AsCollection.php 0000644 00000003017 15107327037 0017111 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Casts; use Illuminate\Contracts\Database\Eloquent\Castable; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use Illuminate\Support\Collection; use InvalidArgumentException; class AsCollection implements Castable { /** * Get the caster class to use when casting from / to this cast target. * * @param array $arguments * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Support\Collection<array-key, mixed>, iterable> */ public static function castUsing(array $arguments) { return new class($arguments) implements CastsAttributes { public function __construct(protected array $arguments) { } public function get($model, $key, $value, $attributes) { if (! isset($attributes[$key])) { return; } $data = Json::decode($attributes[$key]); $collectionClass = $this->arguments[0] ?? Collection::class; if (! is_a($collectionClass, Collection::class, true)) { throw new InvalidArgumentException('The provided class must extend ['.Collection::class.'].'); } return is_array($data) ? new $collectionClass($data) : null; } public function set($model, $key, $value, $attributes) { return [$key => Json::encode($value)]; } }; } } src/Illuminate/Database/Eloquent/Casts/AsEncryptedCollection.php 0000644 00000003221 15107327037 0020764 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Casts; use Illuminate\Contracts\Database\Eloquent\Castable; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Crypt; use InvalidArgumentException; class AsEncryptedCollection implements Castable { /** * Get the caster class to use when casting from / to this cast target. * * @param array $arguments * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Support\Collection<array-key, mixed>, iterable> */ public static function castUsing(array $arguments) { return new class($arguments) implements CastsAttributes { public function __construct(protected array $arguments) { } public function get($model, $key, $value, $attributes) { $collectionClass = $this->arguments[0] ?? Collection::class; if (! is_a($collectionClass, Collection::class, true)) { throw new InvalidArgumentException('The provided class must extend ['.Collection::class.'].'); } if (isset($attributes[$key])) { return new $collectionClass(Json::decode(Crypt::decryptString($attributes[$key]))); } return null; } public function set($model, $key, $value, $attributes) { if (! is_null($value)) { return [$key => Crypt::encryptString(Json::encode($value))]; } return null; } }; } } src/Illuminate/Database/Eloquent/Casts/ArrayObject.php 0000644 00000001616 15107327037 0016742 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Casts; use ArrayObject as BaseArrayObject; use Illuminate\Contracts\Support\Arrayable; use JsonSerializable; /** * @template TKey of array-key * @template TItem * * @extends \ArrayObject<TKey, TItem> */ class ArrayObject extends BaseArrayObject implements Arrayable, JsonSerializable { /** * Get a collection containing the underlying array. * * @return \Illuminate\Support\Collection */ public function collect() { return collect($this->getArrayCopy()); } /** * Get the instance as an array. * * @return array */ public function toArray() { return $this->getArrayCopy(); } /** * Get the array that should be JSON serialized. * * @return array */ public function jsonSerialize(): array { return $this->getArrayCopy(); } } src/Illuminate/Database/Eloquent/Casts/AsArrayObject.php 0000644 00000002363 15107327037 0017226 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Casts; use Illuminate\Contracts\Database\Eloquent\Castable; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; class AsArrayObject implements Castable { /** * Get the caster class to use when casting from / to this cast target. * * @param array $arguments * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Database\Eloquent\Casts\ArrayObject<array-key, mixed>, iterable> */ public static function castUsing(array $arguments) { return new class implements CastsAttributes { public function get($model, $key, $value, $attributes) { if (! isset($attributes[$key])) { return; } $data = Json::decode($attributes[$key]); return is_array($data) ? new ArrayObject($data) : null; } public function set($model, $key, $value, $attributes) { return [$key => Json::encode($value)]; } public function serialize($model, string $key, $value, array $attributes) { return $value->getArrayCopy(); } }; } } src/Illuminate/Database/Eloquent/Casts/Attribute.php 0000644 00000003664 15107327037 0016505 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Casts; class Attribute { /** * The attribute accessor. * * @var callable */ public $get; /** * The attribute mutator. * * @var callable */ public $set; /** * Indicates if caching is enabled for this attribute. * * @var bool */ public $withCaching = false; /** * Indicates if caching of objects is enabled for this attribute. * * @var bool */ public $withObjectCaching = true; /** * Create a new attribute accessor / mutator. * * @param callable|null $get * @param callable|null $set * @return void */ public function __construct(callable $get = null, callable $set = null) { $this->get = $get; $this->set = $set; } /** * Create a new attribute accessor / mutator. * * @param callable|null $get * @param callable|null $set * @return static */ public static function make(callable $get = null, callable $set = null): static { return new static($get, $set); } /** * Create a new attribute accessor. * * @param callable $get * @return static */ public static function get(callable $get) { return new static($get); } /** * Create a new attribute mutator. * * @param callable $set * @return static */ public static function set(callable $set) { return new static(null, $set); } /** * Disable object caching for the attribute. * * @return static */ public function withoutObjectCaching() { $this->withObjectCaching = false; return $this; } /** * Enable caching for the attribute. * * @return static */ public function shouldCache() { $this->withCaching = true; return $this; } } src/Illuminate/Database/Eloquent/Casts/AsEncryptedArrayObject.php 0000644 00000002620 15107327037 0021100 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Casts; use Illuminate\Contracts\Database\Eloquent\Castable; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use Illuminate\Support\Facades\Crypt; class AsEncryptedArrayObject implements Castable { /** * Get the caster class to use when casting from / to this cast target. * * @param array $arguments * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Database\Eloquent\Casts\ArrayObject<array-key, mixed>, iterable> */ public static function castUsing(array $arguments) { return new class implements CastsAttributes { public function get($model, $key, $value, $attributes) { if (isset($attributes[$key])) { return new ArrayObject(Json::decode(Crypt::decryptString($attributes[$key]))); } return null; } public function set($model, $key, $value, $attributes) { if (! is_null($value)) { return [$key => Crypt::encryptString(Json::encode($value))]; } return null; } public function serialize($model, string $key, $value, array $attributes) { return ! is_null($value) ? $value->getArrayCopy() : null; } }; } } src/Illuminate/Database/Eloquent/Casts/AsEnumCollection.php 0000644 00000005054 15107327037 0017741 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Casts; use BackedEnum; use Illuminate\Contracts\Database\Eloquent\Castable; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use Illuminate\Support\Collection; class AsEnumCollection implements Castable { /** * Get the caster class to use when casting from / to this cast target. * * @template TEnum of \UnitEnum|\BackedEnum * * @param array{class-string<TEnum>} $arguments * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Support\Collection<array-key, TEnum>, iterable<TEnum>> */ public static function castUsing(array $arguments) { return new class($arguments) implements CastsAttributes { protected $arguments; public function __construct(array $arguments) { $this->arguments = $arguments; } public function get($model, $key, $value, $attributes) { if (! isset($attributes[$key]) || is_null($attributes[$key])) { return; } $data = Json::decode($attributes[$key]); if (! is_array($data)) { return; } $enumClass = $this->arguments[0]; return (new Collection($data))->map(function ($value) use ($enumClass) { return is_subclass_of($enumClass, BackedEnum::class) ? $enumClass::from($value) : constant($enumClass.'::'.$value); }); } public function set($model, $key, $value, $attributes) { $value = $value !== null ? Json::encode((new Collection($value))->map(function ($enum) { return $this->getStorableEnumValue($enum); })->jsonSerialize()) : null; return [$key => $value]; } public function serialize($model, string $key, $value, array $attributes) { return (new Collection($value))->map(function ($enum) { return $this->getStorableEnumValue($enum); })->toArray(); } protected function getStorableEnumValue($enum) { if (is_string($enum) || is_int($enum)) { return $enum; } return $enum instanceof BackedEnum ? $enum->value : $enum->name; } }; } } src/Illuminate/Database/Eloquent/Model.php 0000644 00000173676 15107327037 0014540 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use ArrayAccess; use Illuminate\Contracts\Broadcasting\HasBroadcastChannel; use Illuminate\Contracts\Queue\QueueableCollection; use Illuminate\Contracts\Queue\QueueableEntity; use Illuminate\Contracts\Routing\UrlRoutable; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Database\ConnectionResolverInterface as Resolver; use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot; use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Support\Arr; use Illuminate\Support\Collection as BaseCollection; use Illuminate\Support\Str; use Illuminate\Support\Traits\ForwardsCalls; use JsonSerializable; use LogicException; abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToString, HasBroadcastChannel, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable { use Concerns\HasAttributes, Concerns\HasEvents, Concerns\HasGlobalScopes, Concerns\HasRelationships, Concerns\HasTimestamps, Concerns\HasUniqueIds, Concerns\HidesAttributes, Concerns\GuardsAttributes, ForwardsCalls; /** * The connection name for the model. * * @var string|null */ protected $connection; /** * The table associated with the model. * * @var string */ protected $table; /** * The primary key for the model. * * @var string */ protected $primaryKey = 'id'; /** * The "type" of the primary key ID. * * @var string */ protected $keyType = 'int'; /** * Indicates if the IDs are auto-incrementing. * * @var bool */ public $incrementing = true; /** * The relations to eager load on every query. * * @var array */ protected $with = []; /** * The relationship counts that should be eager loaded on every query. * * @var array */ protected $withCount = []; /** * Indicates whether lazy loading will be prevented on this model. * * @var bool */ public $preventsLazyLoading = false; /** * The number of models to return for pagination. * * @var int */ protected $perPage = 15; /** * Indicates if the model exists. * * @var bool */ public $exists = false; /** * Indicates if the model was inserted during the current request lifecycle. * * @var bool */ public $wasRecentlyCreated = false; /** * Indicates that the object's string representation should be escaped when __toString is invoked. * * @var bool */ protected $escapeWhenCastingToString = false; /** * The connection resolver instance. * * @var \Illuminate\Database\ConnectionResolverInterface */ protected static $resolver; /** * The event dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected static $dispatcher; /** * The array of booted models. * * @var array */ protected static $booted = []; /** * The array of trait initializers that will be called on each new instance. * * @var array */ protected static $traitInitializers = []; /** * The array of global scopes on the model. * * @var array */ protected static $globalScopes = []; /** * The list of models classes that should not be affected with touch. * * @var array */ protected static $ignoreOnTouch = []; /** * Indicates whether lazy loading should be restricted on all models. * * @var bool */ protected static $modelsShouldPreventLazyLoading = false; /** * The callback that is responsible for handling lazy loading violations. * * @var callable|null */ protected static $lazyLoadingViolationCallback; /** * Indicates if an exception should be thrown instead of silently discarding non-fillable attributes. * * @var bool */ protected static $modelsShouldPreventSilentlyDiscardingAttributes = false; /** * The callback that is responsible for handling discarded attribute violations. * * @var callable|null */ protected static $discardedAttributeViolationCallback; /** * Indicates if an exception should be thrown when trying to access a missing attribute on a retrieved model. * * @var bool */ protected static $modelsShouldPreventAccessingMissingAttributes = false; /** * The callback that is responsible for handling missing attribute violations. * * @var callable|null */ protected static $missingAttributeViolationCallback; /** * Indicates if broadcasting is currently enabled. * * @var bool */ protected static $isBroadcasting = true; /** * The name of the "created at" column. * * @var string|null */ const CREATED_AT = 'created_at'; /** * The name of the "updated at" column. * * @var string|null */ const UPDATED_AT = 'updated_at'; /** * Create a new Eloquent model instance. * * @param array $attributes * @return void */ public function __construct(array $attributes = []) { $this->bootIfNotBooted(); $this->initializeTraits(); $this->syncOriginal(); $this->fill($attributes); } /** * Check if the model needs to be booted and if so, do it. * * @return void */ protected function bootIfNotBooted() { if (! isset(static::$booted[static::class])) { static::$booted[static::class] = true; $this->fireModelEvent('booting', false); static::booting(); static::boot(); static::booted(); $this->fireModelEvent('booted', false); } } /** * Perform any actions required before the model boots. * * @return void */ protected static function booting() { // } /** * Bootstrap the model and its traits. * * @return void */ protected static function boot() { static::bootTraits(); } /** * Boot all of the bootable traits on the model. * * @return void */ protected static function bootTraits() { $class = static::class; $booted = []; static::$traitInitializers[$class] = []; foreach (class_uses_recursive($class) as $trait) { $method = 'boot'.class_basename($trait); if (method_exists($class, $method) && ! in_array($method, $booted)) { forward_static_call([$class, $method]); $booted[] = $method; } if (method_exists($class, $method = 'initialize'.class_basename($trait))) { static::$traitInitializers[$class][] = $method; static::$traitInitializers[$class] = array_unique( static::$traitInitializers[$class] ); } } } /** * Initialize any initializable traits on the model. * * @return void */ protected function initializeTraits() { foreach (static::$traitInitializers[static::class] as $method) { $this->{$method}(); } } /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { // } /** * Clear the list of booted models so they will be re-booted. * * @return void */ public static function clearBootedModels() { static::$booted = []; static::$globalScopes = []; } /** * Disables relationship model touching for the current class during given callback scope. * * @param callable $callback * @return void */ public static function withoutTouching(callable $callback) { static::withoutTouchingOn([static::class], $callback); } /** * Disables relationship model touching for the given model classes during given callback scope. * * @param array $models * @param callable $callback * @return void */ public static function withoutTouchingOn(array $models, callable $callback) { static::$ignoreOnTouch = array_values(array_merge(static::$ignoreOnTouch, $models)); try { $callback(); } finally { static::$ignoreOnTouch = array_values(array_diff(static::$ignoreOnTouch, $models)); } } /** * Determine if the given model is ignoring touches. * * @param string|null $class * @return bool */ public static function isIgnoringTouch($class = null) { $class = $class ?: static::class; if (! get_class_vars($class)['timestamps'] || ! $class::UPDATED_AT) { return true; } foreach (static::$ignoreOnTouch as $ignoredClass) { if ($class === $ignoredClass || is_subclass_of($class, $ignoredClass)) { return true; } } return false; } /** * Indicate that models should prevent lazy loading, silently discarding attributes, and accessing missing attributes. * * @param bool $shouldBeStrict * @return void */ public static function shouldBeStrict(bool $shouldBeStrict = true) { static::preventLazyLoading($shouldBeStrict); static::preventSilentlyDiscardingAttributes($shouldBeStrict); static::preventAccessingMissingAttributes($shouldBeStrict); } /** * Prevent model relationships from being lazy loaded. * * @param bool $value * @return void */ public static function preventLazyLoading($value = true) { static::$modelsShouldPreventLazyLoading = $value; } /** * Register a callback that is responsible for handling lazy loading violations. * * @param callable|null $callback * @return void */ public static function handleLazyLoadingViolationUsing(?callable $callback) { static::$lazyLoadingViolationCallback = $callback; } /** * Prevent non-fillable attributes from being silently discarded. * * @param bool $value * @return void */ public static function preventSilentlyDiscardingAttributes($value = true) { static::$modelsShouldPreventSilentlyDiscardingAttributes = $value; } /** * Register a callback that is responsible for handling discarded attribute violations. * * @param callable|null $callback * @return void */ public static function handleDiscardedAttributeViolationUsing(?callable $callback) { static::$discardedAttributeViolationCallback = $callback; } /** * Prevent accessing missing attributes on retrieved models. * * @param bool $value * @return void */ public static function preventAccessingMissingAttributes($value = true) { static::$modelsShouldPreventAccessingMissingAttributes = $value; } /** * Register a callback that is responsible for handling missing attribute violations. * * @param callable|null $callback * @return void */ public static function handleMissingAttributeViolationUsing(?callable $callback) { static::$missingAttributeViolationCallback = $callback; } /** * Execute a callback without broadcasting any model events for all model types. * * @param callable $callback * @return mixed */ public static function withoutBroadcasting(callable $callback) { $isBroadcasting = static::$isBroadcasting; static::$isBroadcasting = false; try { return $callback(); } finally { static::$isBroadcasting = $isBroadcasting; } } /** * Fill the model with an array of attributes. * * @param array $attributes * @return $this * * @throws \Illuminate\Database\Eloquent\MassAssignmentException */ public function fill(array $attributes) { $totallyGuarded = $this->totallyGuarded(); $fillable = $this->fillableFromArray($attributes); foreach ($fillable as $key => $value) { // The developers may choose to place some attributes in the "fillable" array // which means only those attributes may be set through mass assignment to // the model, and all others will just get ignored for security reasons. if ($this->isFillable($key)) { $this->setAttribute($key, $value); } elseif ($totallyGuarded || static::preventsSilentlyDiscardingAttributes()) { if (isset(static::$discardedAttributeViolationCallback)) { call_user_func(static::$discardedAttributeViolationCallback, $this, [$key]); } else { throw new MassAssignmentException(sprintf( 'Add [%s] to fillable property to allow mass assignment on [%s].', $key, get_class($this) )); } } } if (count($attributes) !== count($fillable) && static::preventsSilentlyDiscardingAttributes()) { $keys = array_diff(array_keys($attributes), array_keys($fillable)); if (isset(static::$discardedAttributeViolationCallback)) { call_user_func(static::$discardedAttributeViolationCallback, $this, $keys); } else { throw new MassAssignmentException(sprintf( 'Add fillable property [%s] to allow mass assignment on [%s].', implode(', ', $keys), get_class($this) )); } } return $this; } /** * Fill the model with an array of attributes. Force mass assignment. * * @param array $attributes * @return $this */ public function forceFill(array $attributes) { return static::unguarded(fn () => $this->fill($attributes)); } /** * Qualify the given column name by the model's table. * * @param string $column * @return string */ public function qualifyColumn($column) { if (str_contains($column, '.')) { return $column; } return $this->getTable().'.'.$column; } /** * Qualify the given columns with the model's table. * * @param array $columns * @return array */ public function qualifyColumns($columns) { return collect($columns)->map(function ($column) { return $this->qualifyColumn($column); })->all(); } /** * Create a new instance of the given model. * * @param array $attributes * @param bool $exists * @return static */ public function newInstance($attributes = [], $exists = false) { // This method just provides a convenient way for us to generate fresh model // instances of this current model. It is particularly useful during the // hydration of new objects via the Eloquent query builder instances. $model = new static; $model->exists = $exists; $model->setConnection( $this->getConnectionName() ); $model->setTable($this->getTable()); $model->mergeCasts($this->casts); $model->fill((array) $attributes); return $model; } /** * Create a new model instance that is existing. * * @param array $attributes * @param string|null $connection * @return static */ public function newFromBuilder($attributes = [], $connection = null) { $model = $this->newInstance([], true); $model->setRawAttributes((array) $attributes, true); $model->setConnection($connection ?: $this->getConnectionName()); $model->fireModelEvent('retrieved', false); return $model; } /** * Begin querying the model on a given connection. * * @param string|null $connection * @return \Illuminate\Database\Eloquent\Builder */ public static function on($connection = null) { // First we will just create a fresh instance of this model, and then we can set the // connection on the model so that it is used for the queries we execute, as well // as being set on every relation we retrieve without a custom connection name. $instance = new static; $instance->setConnection($connection); return $instance->newQuery(); } /** * Begin querying the model on the write connection. * * @return \Illuminate\Database\Eloquent\Builder */ public static function onWriteConnection() { return static::query()->useWritePdo(); } /** * Get all of the models from the database. * * @param array|string $columns * @return \Illuminate\Database\Eloquent\Collection<int, static> */ public static function all($columns = ['*']) { return static::query()->get( is_array($columns) ? $columns : func_get_args() ); } /** * Begin querying a model with eager loading. * * @param array|string $relations * @return \Illuminate\Database\Eloquent\Builder */ public static function with($relations) { return static::query()->with( is_string($relations) ? func_get_args() : $relations ); } /** * Eager load relations on the model. * * @param array|string $relations * @return $this */ public function load($relations) { $query = $this->newQueryWithoutRelationships()->with( is_string($relations) ? func_get_args() : $relations ); $query->eagerLoadRelations([$this]); return $this; } /** * Eager load relationships on the polymorphic relation of a model. * * @param string $relation * @param array $relations * @return $this */ public function loadMorph($relation, $relations) { if (! $this->{$relation}) { return $this; } $className = get_class($this->{$relation}); $this->{$relation}->load($relations[$className] ?? []); return $this; } /** * Eager load relations on the model if they are not already eager loaded. * * @param array|string $relations * @return $this */ public function loadMissing($relations) { $relations = is_string($relations) ? func_get_args() : $relations; $this->newCollection([$this])->loadMissing($relations); return $this; } /** * Eager load relation's column aggregations on the model. * * @param array|string $relations * @param string $column * @param string|null $function * @return $this */ public function loadAggregate($relations, $column, $function = null) { $this->newCollection([$this])->loadAggregate($relations, $column, $function); return $this; } /** * Eager load relation counts on the model. * * @param array|string $relations * @return $this */ public function loadCount($relations) { $relations = is_string($relations) ? func_get_args() : $relations; return $this->loadAggregate($relations, '*', 'count'); } /** * Eager load relation max column values on the model. * * @param array|string $relations * @param string $column * @return $this */ public function loadMax($relations, $column) { return $this->loadAggregate($relations, $column, 'max'); } /** * Eager load relation min column values on the model. * * @param array|string $relations * @param string $column * @return $this */ public function loadMin($relations, $column) { return $this->loadAggregate($relations, $column, 'min'); } /** * Eager load relation's column summations on the model. * * @param array|string $relations * @param string $column * @return $this */ public function loadSum($relations, $column) { return $this->loadAggregate($relations, $column, 'sum'); } /** * Eager load relation average column values on the model. * * @param array|string $relations * @param string $column * @return $this */ public function loadAvg($relations, $column) { return $this->loadAggregate($relations, $column, 'avg'); } /** * Eager load related model existence values on the model. * * @param array|string $relations * @return $this */ public function loadExists($relations) { return $this->loadAggregate($relations, '*', 'exists'); } /** * Eager load relationship column aggregation on the polymorphic relation of a model. * * @param string $relation * @param array $relations * @param string $column * @param string|null $function * @return $this */ public function loadMorphAggregate($relation, $relations, $column, $function = null) { if (! $this->{$relation}) { return $this; } $className = get_class($this->{$relation}); $this->{$relation}->loadAggregate($relations[$className] ?? [], $column, $function); return $this; } /** * Eager load relationship counts on the polymorphic relation of a model. * * @param string $relation * @param array $relations * @return $this */ public function loadMorphCount($relation, $relations) { return $this->loadMorphAggregate($relation, $relations, '*', 'count'); } /** * Eager load relationship max column values on the polymorphic relation of a model. * * @param string $relation * @param array $relations * @param string $column * @return $this */ public function loadMorphMax($relation, $relations, $column) { return $this->loadMorphAggregate($relation, $relations, $column, 'max'); } /** * Eager load relationship min column values on the polymorphic relation of a model. * * @param string $relation * @param array $relations * @param string $column * @return $this */ public function loadMorphMin($relation, $relations, $column) { return $this->loadMorphAggregate($relation, $relations, $column, 'min'); } /** * Eager load relationship column summations on the polymorphic relation of a model. * * @param string $relation * @param array $relations * @param string $column * @return $this */ public function loadMorphSum($relation, $relations, $column) { return $this->loadMorphAggregate($relation, $relations, $column, 'sum'); } /** * Eager load relationship average column values on the polymorphic relation of a model. * * @param string $relation * @param array $relations * @param string $column * @return $this */ public function loadMorphAvg($relation, $relations, $column) { return $this->loadMorphAggregate($relation, $relations, $column, 'avg'); } /** * Increment a column's value by a given amount. * * @param string $column * @param float|int $amount * @param array $extra * @return int */ protected function increment($column, $amount = 1, array $extra = []) { return $this->incrementOrDecrement($column, $amount, $extra, 'increment'); } /** * Decrement a column's value by a given amount. * * @param string $column * @param float|int $amount * @param array $extra * @return int */ protected function decrement($column, $amount = 1, array $extra = []) { return $this->incrementOrDecrement($column, $amount, $extra, 'decrement'); } /** * Run the increment or decrement method on the model. * * @param string $column * @param float|int $amount * @param array $extra * @param string $method * @return int */ protected function incrementOrDecrement($column, $amount, $extra, $method) { if (! $this->exists) { return $this->newQueryWithoutRelationships()->{$method}($column, $amount, $extra); } $this->{$column} = $this->isClassDeviable($column) ? $this->deviateClassCastableAttribute($method, $column, $amount) : $this->{$column} + ($method === 'increment' ? $amount : $amount * -1); $this->forceFill($extra); if ($this->fireModelEvent('updating') === false) { return false; } return tap($this->setKeysForSaveQuery($this->newQueryWithoutScopes())->{$method}($column, $amount, $extra), function () use ($column) { $this->syncChanges(); $this->fireModelEvent('updated', false); $this->syncOriginalAttribute($column); }); } /** * Update the model in the database. * * @param array $attributes * @param array $options * @return bool */ public function update(array $attributes = [], array $options = []) { if (! $this->exists) { return false; } return $this->fill($attributes)->save($options); } /** * Update the model in the database within a transaction. * * @param array $attributes * @param array $options * @return bool * * @throws \Throwable */ public function updateOrFail(array $attributes = [], array $options = []) { if (! $this->exists) { return false; } return $this->fill($attributes)->saveOrFail($options); } /** * Update the model in the database without raising any events. * * @param array $attributes * @param array $options * @return bool */ public function updateQuietly(array $attributes = [], array $options = []) { if (! $this->exists) { return false; } return $this->fill($attributes)->saveQuietly($options); } /** * Increment a column's value by a given amount without raising any events. * * @param string $column * @param float|int $amount * @param array $extra * @return int */ protected function incrementQuietly($column, $amount = 1, array $extra = []) { return static::withoutEvents(function () use ($column, $amount, $extra) { return $this->incrementOrDecrement($column, $amount, $extra, 'increment'); }); } /** * Decrement a column's value by a given amount without raising any events. * * @param string $column * @param float|int $amount * @param array $extra * @return int */ protected function decrementQuietly($column, $amount = 1, array $extra = []) { return static::withoutEvents(function () use ($column, $amount, $extra) { return $this->incrementOrDecrement($column, $amount, $extra, 'decrement'); }); } /** * Save the model and all of its relationships. * * @return bool */ public function push() { if (! $this->save()) { return false; } // To sync all of the relationships to the database, we will simply spin through // the relationships and save each model via this "push" method, which allows // us to recurse into all of these nested relations for the model instance. foreach ($this->relations as $models) { $models = $models instanceof Collection ? $models->all() : [$models]; foreach (array_filter($models) as $model) { if (! $model->push()) { return false; } } } return true; } /** * Save the model and all of its relationships without raising any events to the parent model. * * @return bool */ public function pushQuietly() { return static::withoutEvents(fn () => $this->push()); } /** * Save the model to the database without raising any events. * * @param array $options * @return bool */ public function saveQuietly(array $options = []) { return static::withoutEvents(fn () => $this->save($options)); } /** * Save the model to the database. * * @param array $options * @return bool */ public function save(array $options = []) { $this->mergeAttributesFromCachedCasts(); $query = $this->newModelQuery(); // If the "saving" event returns false we'll bail out of the save and return // false, indicating that the save failed. This provides a chance for any // listeners to cancel save operations if validations fail or whatever. if ($this->fireModelEvent('saving') === false) { return false; } // If the model already exists in the database we can just update our record // that is already in this database using the current IDs in this "where" // clause to only update this model. Otherwise, we'll just insert them. if ($this->exists) { $saved = $this->isDirty() ? $this->performUpdate($query) : true; } // If the model is brand new, we'll insert it into our database and set the // ID attribute on the model to the value of the newly inserted row's ID // which is typically an auto-increment value managed by the database. else { $saved = $this->performInsert($query); if (! $this->getConnectionName() && $connection = $query->getConnection()) { $this->setConnection($connection->getName()); } } // If the model is successfully saved, we need to do a few more things once // that is done. We will call the "saved" method here to run any actions // we need to happen after a model gets successfully saved right here. if ($saved) { $this->finishSave($options); } return $saved; } /** * Save the model to the database within a transaction. * * @param array $options * @return bool * * @throws \Throwable */ public function saveOrFail(array $options = []) { return $this->getConnection()->transaction(fn () => $this->save($options)); } /** * Perform any actions that are necessary after the model is saved. * * @param array $options * @return void */ protected function finishSave(array $options) { $this->fireModelEvent('saved', false); if ($this->isDirty() && ($options['touch'] ?? true)) { $this->touchOwners(); } $this->syncOriginal(); } /** * Perform a model update operation. * * @param \Illuminate\Database\Eloquent\Builder $query * @return bool */ protected function performUpdate(Builder $query) { // If the updating event returns false, we will cancel the update operation so // developers can hook Validation systems into their models and cancel this // operation if the model does not pass validation. Otherwise, we update. if ($this->fireModelEvent('updating') === false) { return false; } // First we need to create a fresh query instance and touch the creation and // update timestamp on the model which are maintained by us for developer // convenience. Then we will just continue saving the model instances. if ($this->usesTimestamps()) { $this->updateTimestamps(); } // Once we have run the update operation, we will fire the "updated" event for // this model instance. This will allow developers to hook into these after // models are updated, giving them a chance to do any special processing. $dirty = $this->getDirty(); if (count($dirty) > 0) { $this->setKeysForSaveQuery($query)->update($dirty); $this->syncChanges(); $this->fireModelEvent('updated', false); } return true; } /** * Set the keys for a select query. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ protected function setKeysForSelectQuery($query) { $query->where($this->getKeyName(), '=', $this->getKeyForSelectQuery()); return $query; } /** * Get the primary key value for a select query. * * @return mixed */ protected function getKeyForSelectQuery() { return $this->original[$this->getKeyName()] ?? $this->getKey(); } /** * Set the keys for a save update query. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ protected function setKeysForSaveQuery($query) { $query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery()); return $query; } /** * Get the primary key value for a save query. * * @return mixed */ protected function getKeyForSaveQuery() { return $this->original[$this->getKeyName()] ?? $this->getKey(); } /** * Perform a model insert operation. * * @param \Illuminate\Database\Eloquent\Builder $query * @return bool */ protected function performInsert(Builder $query) { if ($this->usesUniqueIds()) { $this->setUniqueIds(); } if ($this->fireModelEvent('creating') === false) { return false; } // First we'll need to create a fresh query instance and touch the creation and // update timestamps on this model, which are maintained by us for developer // convenience. After, we will just continue saving these model instances. if ($this->usesTimestamps()) { $this->updateTimestamps(); } // If the model has an incrementing key, we can use the "insertGetId" method on // the query builder, which will give us back the final inserted ID for this // table from the database. Not all tables have to be incrementing though. $attributes = $this->getAttributesForInsert(); if ($this->getIncrementing()) { $this->insertAndSetId($query, $attributes); } // If the table isn't incrementing we'll simply insert these attributes as they // are. These attribute arrays must contain an "id" column previously placed // there by the developer as the manually determined key for these models. else { if (empty($attributes)) { return true; } $query->insert($attributes); } // We will go ahead and set the exists property to true, so that it is set when // the created event is fired, just in case the developer tries to update it // during the event. This will allow them to do so and run an update here. $this->exists = true; $this->wasRecentlyCreated = true; $this->fireModelEvent('created', false); return true; } /** * Insert the given attributes and set the ID on the model. * * @param \Illuminate\Database\Eloquent\Builder $query * @param array $attributes * @return void */ protected function insertAndSetId(Builder $query, $attributes) { $id = $query->insertGetId($attributes, $keyName = $this->getKeyName()); $this->setAttribute($keyName, $id); } /** * Destroy the models for the given IDs. * * @param \Illuminate\Support\Collection|array|int|string $ids * @return int */ public static function destroy($ids) { if ($ids instanceof EloquentCollection) { $ids = $ids->modelKeys(); } if ($ids instanceof BaseCollection) { $ids = $ids->all(); } $ids = is_array($ids) ? $ids : func_get_args(); if (count($ids) === 0) { return 0; } // We will actually pull the models from the database table and call delete on // each of them individually so that their events get fired properly with a // correct set of attributes in case the developers wants to check these. $key = ($instance = new static)->getKeyName(); $count = 0; foreach ($instance->whereIn($key, $ids)->get() as $model) { if ($model->delete()) { $count++; } } return $count; } /** * Delete the model from the database. * * @return bool|null * * @throws \LogicException */ public function delete() { $this->mergeAttributesFromCachedCasts(); if (is_null($this->getKeyName())) { throw new LogicException('No primary key defined on model.'); } // If the model doesn't exist, there is nothing to delete so we'll just return // immediately and not do anything else. Otherwise, we will continue with a // deletion process on the model, firing the proper events, and so forth. if (! $this->exists) { return; } if ($this->fireModelEvent('deleting') === false) { return false; } // Here, we'll touch the owning models, verifying these timestamps get updated // for the models. This will allow any caching to get broken on the parents // by the timestamp. Then we will go ahead and delete the model instance. $this->touchOwners(); $this->performDeleteOnModel(); // Once the model has been deleted, we will fire off the deleted event so that // the developers may hook into post-delete operations. We will then return // a boolean true as the delete is presumably successful on the database. $this->fireModelEvent('deleted', false); return true; } /** * Delete the model from the database without raising any events. * * @return bool */ public function deleteQuietly() { return static::withoutEvents(fn () => $this->delete()); } /** * Delete the model from the database within a transaction. * * @return bool|null * * @throws \Throwable */ public function deleteOrFail() { if (! $this->exists) { return false; } return $this->getConnection()->transaction(fn () => $this->delete()); } /** * Force a hard delete on a soft deleted model. * * This method protects developers from running forceDelete when the trait is missing. * * @return bool|null */ public function forceDelete() { return $this->delete(); } /** * Perform the actual delete query on this model instance. * * @return void */ protected function performDeleteOnModel() { $this->setKeysForSaveQuery($this->newModelQuery())->delete(); $this->exists = false; } /** * Begin querying the model. * * @return \Illuminate\Database\Eloquent\Builder */ public static function query() { return (new static)->newQuery(); } /** * Get a new query builder for the model's table. * * @return \Illuminate\Database\Eloquent\Builder */ public function newQuery() { return $this->registerGlobalScopes($this->newQueryWithoutScopes()); } /** * Get a new query builder that doesn't have any global scopes or eager loading. * * @return \Illuminate\Database\Eloquent\Builder|static */ public function newModelQuery() { return $this->newEloquentBuilder( $this->newBaseQueryBuilder() )->setModel($this); } /** * Get a new query builder with no relationships loaded. * * @return \Illuminate\Database\Eloquent\Builder */ public function newQueryWithoutRelationships() { return $this->registerGlobalScopes($this->newModelQuery()); } /** * Register the global scopes for this builder instance. * * @param \Illuminate\Database\Eloquent\Builder $builder * @return \Illuminate\Database\Eloquent\Builder */ public function registerGlobalScopes($builder) { foreach ($this->getGlobalScopes() as $identifier => $scope) { $builder->withGlobalScope($identifier, $scope); } return $builder; } /** * Get a new query builder that doesn't have any global scopes. * * @return \Illuminate\Database\Eloquent\Builder|static */ public function newQueryWithoutScopes() { return $this->newModelQuery() ->with($this->with) ->withCount($this->withCount); } /** * Get a new query instance without a given scope. * * @param \Illuminate\Database\Eloquent\Scope|string $scope * @return \Illuminate\Database\Eloquent\Builder */ public function newQueryWithoutScope($scope) { return $this->newQuery()->withoutGlobalScope($scope); } /** * Get a new query to restore one or more models by their queueable IDs. * * @param array|int $ids * @return \Illuminate\Database\Eloquent\Builder */ public function newQueryForRestoration($ids) { return $this->newQueryWithoutScopes()->whereKey($ids); } /** * Create a new Eloquent query builder for the model. * * @param \Illuminate\Database\Query\Builder $query * @return \Illuminate\Database\Eloquent\Builder|static */ public function newEloquentBuilder($query) { return new Builder($query); } /** * Get a new query builder instance for the connection. * * @return \Illuminate\Database\Query\Builder */ protected function newBaseQueryBuilder() { return $this->getConnection()->query(); } /** * Create a new Eloquent Collection instance. * * @param array $models * @return \Illuminate\Database\Eloquent\Collection */ public function newCollection(array $models = []) { return new Collection($models); } /** * Create a new pivot model instance. * * @param \Illuminate\Database\Eloquent\Model $parent * @param array $attributes * @param string $table * @param bool $exists * @param string|null $using * @return \Illuminate\Database\Eloquent\Relations\Pivot */ public function newPivot(self $parent, array $attributes, $table, $exists, $using = null) { return $using ? $using::fromRawAttributes($parent, $attributes, $table, $exists) : Pivot::fromAttributes($parent, $attributes, $table, $exists); } /** * Determine if the model has a given scope. * * @param string $scope * @return bool */ public function hasNamedScope($scope) { return method_exists($this, 'scope'.ucfirst($scope)); } /** * Apply the given named scope if possible. * * @param string $scope * @param array $parameters * @return mixed */ public function callNamedScope($scope, array $parameters = []) { return $this->{'scope'.ucfirst($scope)}(...$parameters); } /** * Convert the model instance to an array. * * @return array */ public function toArray() { return array_merge($this->attributesToArray(), $this->relationsToArray()); } /** * Convert the model instance to JSON. * * @param int $options * @return string * * @throws \Illuminate\Database\Eloquent\JsonEncodingException */ public function toJson($options = 0) { $json = json_encode($this->jsonSerialize(), $options); if (json_last_error() !== JSON_ERROR_NONE) { throw JsonEncodingException::forModel($this, json_last_error_msg()); } return $json; } /** * Convert the object into something JSON serializable. * * @return mixed */ public function jsonSerialize(): mixed { return $this->toArray(); } /** * Reload a fresh model instance from the database. * * @param array|string $with * @return static|null */ public function fresh($with = []) { if (! $this->exists) { return; } return $this->setKeysForSelectQuery($this->newQueryWithoutScopes()) ->useWritePdo() ->with(is_string($with) ? func_get_args() : $with) ->first(); } /** * Reload the current model instance with fresh attributes from the database. * * @return $this */ public function refresh() { if (! $this->exists) { return $this; } $this->setRawAttributes( $this->setKeysForSelectQuery($this->newQueryWithoutScopes()) ->useWritePdo() ->firstOrFail() ->attributes ); $this->load(collect($this->relations)->reject(function ($relation) { return $relation instanceof Pivot || (is_object($relation) && in_array(AsPivot::class, class_uses_recursive($relation), true)); })->keys()->all()); $this->syncOriginal(); return $this; } /** * Clone the model into a new, non-existing instance. * * @param array|null $except * @return static */ public function replicate(array $except = null) { $defaults = array_values(array_filter([ $this->getKeyName(), $this->getCreatedAtColumn(), $this->getUpdatedAtColumn(), ...$this->uniqueIds(), ])); $attributes = Arr::except( $this->getAttributes(), $except ? array_unique(array_merge($except, $defaults)) : $defaults ); return tap(new static, function ($instance) use ($attributes) { $instance->setRawAttributes($attributes); $instance->setRelations($this->relations); $instance->fireModelEvent('replicating', false); }); } /** * Clone the model into a new, non-existing instance without raising any events. * * @param array|null $except * @return static */ public function replicateQuietly(array $except = null) { return static::withoutEvents(fn () => $this->replicate($except)); } /** * Determine if two models have the same ID and belong to the same table. * * @param \Illuminate\Database\Eloquent\Model|null $model * @return bool */ public function is($model) { return ! is_null($model) && $this->getKey() === $model->getKey() && $this->getTable() === $model->getTable() && $this->getConnectionName() === $model->getConnectionName(); } /** * Determine if two models are not the same. * * @param \Illuminate\Database\Eloquent\Model|null $model * @return bool */ public function isNot($model) { return ! $this->is($model); } /** * Get the database connection for the model. * * @return \Illuminate\Database\Connection */ public function getConnection() { return static::resolveConnection($this->getConnectionName()); } /** * Get the current connection name for the model. * * @return string|null */ public function getConnectionName() { return $this->connection; } /** * Set the connection associated with the model. * * @param string|null $name * @return $this */ public function setConnection($name) { $this->connection = $name; return $this; } /** * Resolve a connection instance. * * @param string|null $connection * @return \Illuminate\Database\Connection */ public static function resolveConnection($connection = null) { return static::$resolver->connection($connection); } /** * Get the connection resolver instance. * * @return \Illuminate\Database\ConnectionResolverInterface|null */ public static function getConnectionResolver() { return static::$resolver; } /** * Set the connection resolver instance. * * @param \Illuminate\Database\ConnectionResolverInterface $resolver * @return void */ public static function setConnectionResolver(Resolver $resolver) { static::$resolver = $resolver; } /** * Unset the connection resolver for models. * * @return void */ public static function unsetConnectionResolver() { static::$resolver = null; } /** * Get the table associated with the model. * * @return string */ public function getTable() { return $this->table ?? Str::snake(Str::pluralStudly(class_basename($this))); } /** * Set the table associated with the model. * * @param string $table * @return $this */ public function setTable($table) { $this->table = $table; return $this; } /** * Get the primary key for the model. * * @return string */ public function getKeyName() { return $this->primaryKey; } /** * Set the primary key for the model. * * @param string $key * @return $this */ public function setKeyName($key) { $this->primaryKey = $key; return $this; } /** * Get the table qualified key name. * * @return string */ public function getQualifiedKeyName() { return $this->qualifyColumn($this->getKeyName()); } /** * Get the auto-incrementing key type. * * @return string */ public function getKeyType() { return $this->keyType; } /** * Set the data type for the primary key. * * @param string $type * @return $this */ public function setKeyType($type) { $this->keyType = $type; return $this; } /** * Get the value indicating whether the IDs are incrementing. * * @return bool */ public function getIncrementing() { return $this->incrementing; } /** * Set whether IDs are incrementing. * * @param bool $value * @return $this */ public function setIncrementing($value) { $this->incrementing = $value; return $this; } /** * Get the value of the model's primary key. * * @return mixed */ public function getKey() { return $this->getAttribute($this->getKeyName()); } /** * Get the queueable identity for the entity. * * @return mixed */ public function getQueueableId() { return $this->getKey(); } /** * Get the queueable relationships for the entity. * * @return array */ public function getQueueableRelations() { $relations = []; foreach ($this->getRelations() as $key => $relation) { if (! method_exists($this, $key)) { continue; } $relations[] = $key; if ($relation instanceof QueueableCollection) { foreach ($relation->getQueueableRelations() as $collectionValue) { $relations[] = $key.'.'.$collectionValue; } } if ($relation instanceof QueueableEntity) { foreach ($relation->getQueueableRelations() as $entityValue) { $relations[] = $key.'.'.$entityValue; } } } return array_unique($relations); } /** * Get the queueable connection for the entity. * * @return string|null */ public function getQueueableConnection() { return $this->getConnectionName(); } /** * Get the value of the model's route key. * * @return mixed */ public function getRouteKey() { return $this->getAttribute($this->getRouteKeyName()); } /** * Get the route key for the model. * * @return string */ public function getRouteKeyName() { return $this->getKeyName(); } /** * Retrieve the model for a bound value. * * @param mixed $value * @param string|null $field * @return \Illuminate\Database\Eloquent\Model|null */ public function resolveRouteBinding($value, $field = null) { return $this->resolveRouteBindingQuery($this, $value, $field)->first(); } /** * Retrieve the model for a bound value. * * @param mixed $value * @param string|null $field * @return \Illuminate\Database\Eloquent\Model|null */ public function resolveSoftDeletableRouteBinding($value, $field = null) { return $this->resolveRouteBindingQuery($this, $value, $field)->withTrashed()->first(); } /** * Retrieve the child model for a bound value. * * @param string $childType * @param mixed $value * @param string|null $field * @return \Illuminate\Database\Eloquent\Model|null */ public function resolveChildRouteBinding($childType, $value, $field) { return $this->resolveChildRouteBindingQuery($childType, $value, $field)->first(); } /** * Retrieve the child model for a bound value. * * @param string $childType * @param mixed $value * @param string|null $field * @return \Illuminate\Database\Eloquent\Model|null */ public function resolveSoftDeletableChildRouteBinding($childType, $value, $field) { return $this->resolveChildRouteBindingQuery($childType, $value, $field)->withTrashed()->first(); } /** * Retrieve the child model query for a bound value. * * @param string $childType * @param mixed $value * @param string|null $field * @return \Illuminate\Database\Eloquent\Relations\Relation */ protected function resolveChildRouteBindingQuery($childType, $value, $field) { $relationship = $this->{$this->childRouteBindingRelationshipName($childType)}(); $field = $field ?: $relationship->getRelated()->getRouteKeyName(); if ($relationship instanceof HasManyThrough || $relationship instanceof BelongsToMany) { $field = $relationship->getRelated()->getTable().'.'.$field; } return $relationship instanceof Model ? $relationship->resolveRouteBindingQuery($relationship, $value, $field) : $relationship->getRelated()->resolveRouteBindingQuery($relationship, $value, $field); } /** * Retrieve the child route model binding relationship name for the given child type. * * @param string $childType * @return string */ protected function childRouteBindingRelationshipName($childType) { return Str::plural(Str::camel($childType)); } /** * Retrieve the model for a bound value. * * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation $query * @param mixed $value * @param string|null $field * @return \Illuminate\Database\Eloquent\Relations\Relation */ public function resolveRouteBindingQuery($query, $value, $field = null) { return $query->where($field ?? $this->getRouteKeyName(), $value); } /** * Get the default foreign key name for the model. * * @return string */ public function getForeignKey() { return Str::snake(class_basename($this)).'_'.$this->getKeyName(); } /** * Get the number of models to return per page. * * @return int */ public function getPerPage() { return $this->perPage; } /** * Set the number of models to return per page. * * @param int $perPage * @return $this */ public function setPerPage($perPage) { $this->perPage = $perPage; return $this; } /** * Determine if lazy loading is disabled. * * @return bool */ public static function preventsLazyLoading() { return static::$modelsShouldPreventLazyLoading; } /** * Determine if discarding guarded attribute fills is disabled. * * @return bool */ public static function preventsSilentlyDiscardingAttributes() { return static::$modelsShouldPreventSilentlyDiscardingAttributes; } /** * Determine if accessing missing attributes is disabled. * * @return bool */ public static function preventsAccessingMissingAttributes() { return static::$modelsShouldPreventAccessingMissingAttributes; } /** * Get the broadcast channel route definition that is associated with the given entity. * * @return string */ public function broadcastChannelRoute() { return str_replace('\\', '.', get_class($this)).'.{'.Str::camel(class_basename($this)).'}'; } /** * Get the broadcast channel name that is associated with the given entity. * * @return string */ public function broadcastChannel() { return str_replace('\\', '.', get_class($this)).'.'.$this->getKey(); } /** * Dynamically retrieve attributes on the model. * * @param string $key * @return mixed */ public function __get($key) { return $this->getAttribute($key); } /** * Dynamically set attributes on the model. * * @param string $key * @param mixed $value * @return void */ public function __set($key, $value) { $this->setAttribute($key, $value); } /** * Determine if the given attribute exists. * * @param mixed $offset * @return bool */ public function offsetExists($offset): bool { try { return ! is_null($this->getAttribute($offset)); } catch (MissingAttributeException) { return false; } } /** * Get the value for a given offset. * * @param mixed $offset * @return mixed */ public function offsetGet($offset): mixed { return $this->getAttribute($offset); } /** * Set the value for a given offset. * * @param mixed $offset * @param mixed $value * @return void */ public function offsetSet($offset, $value): void { $this->setAttribute($offset, $value); } /** * Unset the value for a given offset. * * @param mixed $offset * @return void */ public function offsetUnset($offset): void { unset($this->attributes[$offset], $this->relations[$offset]); } /** * Determine if an attribute or relation exists on the model. * * @param string $key * @return bool */ public function __isset($key) { return $this->offsetExists($key); } /** * Unset an attribute on the model. * * @param string $key * @return void */ public function __unset($key) { $this->offsetUnset($key); } /** * Handle dynamic method calls into the model. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (in_array($method, ['increment', 'decrement', 'incrementQuietly', 'decrementQuietly'])) { return $this->$method(...$parameters); } if ($resolver = $this->relationResolver(static::class, $method)) { return $resolver($this); } if (Str::startsWith($method, 'through') && method_exists($this, $relationMethod = Str::of($method)->after('through')->lcfirst()->toString())) { return $this->through($relationMethod); } return $this->forwardCallTo($this->newQuery(), $method, $parameters); } /** * Handle dynamic static method calls into the model. * * @param string $method * @param array $parameters * @return mixed */ public static function __callStatic($method, $parameters) { return (new static)->$method(...$parameters); } /** * Convert the model to its string representation. * * @return string */ public function __toString() { return $this->escapeWhenCastingToString ? e($this->toJson()) : $this->toJson(); } /** * Indicate that the object's string representation should be escaped when __toString is invoked. * * @param bool $escape * @return $this */ public function escapeWhenCastingToString($escape = true) { $this->escapeWhenCastingToString = $escape; return $this; } /** * Prepare the object for serialization. * * @return array */ public function __sleep() { $this->mergeAttributesFromCachedCasts(); $this->classCastCache = []; $this->attributeCastCache = []; return array_keys(get_object_vars($this)); } /** * When a model is being unserialized, check if it needs to be booted. * * @return void */ public function __wakeup() { $this->bootIfNotBooted(); $this->initializeTraits(); } } src/Illuminate/Database/Eloquent/PendingHasThroughRelationship.php 0000644 00000005714 15107327037 0021426 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use BadMethodCallException; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Str; class PendingHasThroughRelationship { /** * The root model that the relationship exists on. * * @var \Illuminate\Database\Eloquent\Model */ protected $rootModel; /** * The local relationship. * * @var \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\HasOne */ protected $localRelationship; /** * Create a pending has-many-through or has-one-through relationship. * * @param \Illuminate\Database\Eloquent\Model $rootModel * @param \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\HasOne $localRelationship */ public function __construct($rootModel, $localRelationship) { $this->rootModel = $rootModel; $this->localRelationship = $localRelationship; } /** * Define the distant relationship that this model has. * * @param string|(callable(\Illuminate\Database\Eloquent\Model): (\Illuminate\Database\Eloquent\Relations\HasOne|\Illuminate\Database\Eloquent\Relations\HasMany)) $callback * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough|\Illuminate\Database\Eloquent\Relations\HasOneThrough */ public function has($callback) { if (is_string($callback)) { $callback = fn () => $this->localRelationship->getRelated()->{$callback}(); } $distantRelation = $callback($this->localRelationship->getRelated()); if ($distantRelation instanceof HasMany) { return $this->rootModel->hasManyThrough( $distantRelation->getRelated()::class, $this->localRelationship->getRelated()::class, $this->localRelationship->getForeignKeyName(), $distantRelation->getForeignKeyName(), $this->localRelationship->getLocalKeyName(), $distantRelation->getLocalKeyName(), ); } return $this->rootModel->hasOneThrough( $distantRelation->getRelated()::class, $this->localRelationship->getRelated()::class, $this->localRelationship->getForeignKeyName(), $distantRelation->getForeignKeyName(), $this->localRelationship->getLocalKeyName(), $distantRelation->getLocalKeyName(), ); } /** * Handle dynamic method calls into the model. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (Str::startsWith($method, 'has')) { return $this->has(Str::of($method)->after('has')->lcfirst()->toString()); } throw new BadMethodCallException(sprintf( 'Call to undefined method %s::%s()', static::class, $method )); } } src/Illuminate/Database/Eloquent/HigherOrderBuilderProxy.php 0000644 00000002055 15107327037 0020231 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; /** * @mixin \Illuminate\Database\Eloquent\Builder */ class HigherOrderBuilderProxy { /** * The collection being operated on. * * @var \Illuminate\Database\Eloquent\Builder */ protected $builder; /** * The method being proxied. * * @var string */ protected $method; /** * Create a new proxy instance. * * @param \Illuminate\Database\Eloquent\Builder $builder * @param string $method * @return void */ public function __construct(Builder $builder, $method) { $this->method = $method; $this->builder = $builder; } /** * Proxy a scope call onto the query builder. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->builder->{$this->method}(function ($value) use ($method, $parameters) { return $value->{$method}(...$parameters); }); } } src/Illuminate/Database/Eloquent/Factories/CrossJoinSequence.php 0000644 00000001035 15107327037 0020774 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Factories; use Illuminate\Support\Arr; class CrossJoinSequence extends Sequence { /** * Create a new cross join sequence instance. * * @param array ...$sequences * @return void */ public function __construct(...$sequences) { $crossJoined = array_map( function ($a) { return array_merge(...$a); }, Arr::crossJoin(...$sequences), ); parent::__construct(...$crossJoined); } } src/Illuminate/Database/Eloquent/Factories/BelongsToManyRelationship.php 0000644 00000004035 15107327037 0022500 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Factories; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; class BelongsToManyRelationship { /** * The related factory instance. * * @var \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array */ protected $factory; /** * The pivot attributes / attribute resolver. * * @var callable|array */ protected $pivot; /** * The relationship name. * * @var string */ protected $relationship; /** * Create a new attached relationship definition. * * @param \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $factory * @param callable|array $pivot * @param string $relationship * @return void */ public function __construct($factory, $pivot, $relationship) { $this->factory = $factory; $this->pivot = $pivot; $this->relationship = $relationship; } /** * Create the attached relationship for the given model. * * @param \Illuminate\Database\Eloquent\Model $model * @return void */ public function createFor(Model $model) { Collection::wrap($this->factory instanceof Factory ? $this->factory->create([], $model) : $this->factory)->each(function ($attachable) use ($model) { $model->{$this->relationship}()->attach( $attachable, is_callable($this->pivot) ? call_user_func($this->pivot, $model) : $this->pivot ); }); } /** * Specify the model instances to always use when creating relationships. * * @param \Illuminate\Support\Collection $recycle * @return $this */ public function recycle($recycle) { if ($this->factory instanceof Factory) { $this->factory = $this->factory->recycle($recycle); } return $this; } } src/Illuminate/Database/Eloquent/Factories/BelongsToRelationship.php 0000644 00000005265 15107327037 0021661 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Factories; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphTo; class BelongsToRelationship { /** * The related factory instance. * * @var \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Database\Eloquent\Model */ protected $factory; /** * The relationship name. * * @var string */ protected $relationship; /** * The cached, resolved parent instance ID. * * @var mixed */ protected $resolved; /** * Create a new "belongs to" relationship definition. * * @param \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Database\Eloquent\Model $factory * @param string $relationship * @return void */ public function __construct($factory, $relationship) { $this->factory = $factory; $this->relationship = $relationship; } /** * Get the parent model attributes and resolvers for the given child model. * * @param \Illuminate\Database\Eloquent\Model $model * @return array */ public function attributesFor(Model $model) { $relationship = $model->{$this->relationship}(); return $relationship instanceof MorphTo ? [ $relationship->getMorphType() => $this->factory instanceof Factory ? $this->factory->newModel()->getMorphClass() : $this->factory->getMorphClass(), $relationship->getForeignKeyName() => $this->resolver($relationship->getOwnerKeyName()), ] : [ $relationship->getForeignKeyName() => $this->resolver($relationship->getOwnerKeyName()), ]; } /** * Get the deferred resolver for this relationship's parent ID. * * @param string|null $key * @return \Closure */ protected function resolver($key) { return function () use ($key) { if (! $this->resolved) { $instance = $this->factory instanceof Factory ? ($this->factory->getRandomRecycledModel($this->factory->modelName()) ?? $this->factory->create()) : $this->factory; return $this->resolved = $key ? $instance->{$key} : $instance->getKey(); } return $this->resolved; }; } /** * Specify the model instances to always use when creating relationships. * * @param \Illuminate\Support\Collection $recycle * @return $this */ public function recycle($recycle) { if ($this->factory instanceof Factory) { $this->factory = $this->factory->recycle($recycle); } return $this; } } src/Illuminate/Database/Eloquent/Factories/HasFactory.php 0000644 00000001535 15107327037 0017442 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Factories; trait HasFactory { /** * Get a new factory instance for the model. * * @param callable|array|int|null $count * @param callable|array $state * @return \Illuminate\Database\Eloquent\Factories\Factory<static> */ public static function factory($count = null, $state = []) { $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<static> */ protected static function newFactory() { // } } src/Illuminate/Database/Eloquent/Factories/Factory.php 0000644 00000071262 15107327037 0017012 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Factories; use Closure; use Faker\Generator; use Illuminate\Container\Container; use Illuminate\Contracts\Foundation\Application; use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\Enumerable; use Illuminate\Support\Str; use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\Macroable; use Throwable; /** * @template TModel of \Illuminate\Database\Eloquent\Model * * @method $this trashed() */ abstract class Factory { use Conditionable, ForwardsCalls, Macroable { __call as macroCall; } /** * The name of the factory's corresponding model. * * @var class-string<\Illuminate\Database\Eloquent\Model|TModel> */ protected $model; /** * The number of models that should be generated. * * @var int|null */ protected $count; /** * The state transformations that will be applied to the model. * * @var \Illuminate\Support\Collection */ protected $states; /** * The parent relationships that will be applied to the model. * * @var \Illuminate\Support\Collection */ protected $has; /** * The child relationships that will be applied to the model. * * @var \Illuminate\Support\Collection */ protected $for; /** * The model instances to always use when creating relationships. * * @var \Illuminate\Support\Collection */ protected $recycle; /** * The "after making" callbacks that will be applied to the model. * * @var \Illuminate\Support\Collection */ protected $afterMaking; /** * The "after creating" callbacks that will be applied to the model. * * @var \Illuminate\Support\Collection */ protected $afterCreating; /** * The name of the database connection that will be used to create the models. * * @var string|null */ protected $connection; /** * The current Faker instance. * * @var \Faker\Generator */ protected $faker; /** * The default namespace where factories reside. * * @var string */ public static $namespace = 'Database\\Factories\\'; /** * The default model name resolver. * * @var callable */ protected static $modelNameResolver; /** * The factory name resolver. * * @var callable */ protected static $factoryNameResolver; /** * Create a new factory instance. * * @param int|null $count * @param \Illuminate\Support\Collection|null $states * @param \Illuminate\Support\Collection|null $has * @param \Illuminate\Support\Collection|null $for * @param \Illuminate\Support\Collection|null $afterMaking * @param \Illuminate\Support\Collection|null $afterCreating * @param string|null $connection * @param \Illuminate\Support\Collection|null $recycle * @return void */ public function __construct($count = null, ?Collection $states = null, ?Collection $has = null, ?Collection $for = null, ?Collection $afterMaking = null, ?Collection $afterCreating = null, $connection = null, ?Collection $recycle = null) { $this->count = $count; $this->states = $states ?? new Collection; $this->has = $has ?? new Collection; $this->for = $for ?? new Collection; $this->afterMaking = $afterMaking ?? new Collection; $this->afterCreating = $afterCreating ?? new Collection; $this->connection = $connection; $this->recycle = $recycle ?? new Collection; $this->faker = $this->withFaker(); } /** * Define the model's default state. * * @return array<string, mixed> */ abstract public function definition(); /** * Get a new factory instance for the given attributes. * * @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes * @return static */ public static function new($attributes = []) { return (new static)->state($attributes)->configure(); } /** * Get a new factory instance for the given number of models. * * @param int $count * @return static */ public static function times(int $count) { return static::new()->count($count); } /** * Configure the factory. * * @return static */ public function configure() { return $this; } /** * Get the raw attributes generated by the factory. * * @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes * @param \Illuminate\Database\Eloquent\Model|null $parent * @return array<int|string, mixed> */ public function raw($attributes = [], ?Model $parent = null) { if ($this->count === null) { return $this->state($attributes)->getExpandedAttributes($parent); } return array_map(function () use ($attributes, $parent) { return $this->state($attributes)->getExpandedAttributes($parent); }, range(1, $this->count)); } /** * Create a single model and persist it to the database. * * @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes * @return \Illuminate\Database\Eloquent\Model|TModel */ public function createOne($attributes = []) { return $this->count(null)->create($attributes); } /** * Create a single model and persist it to the database without dispatching any model events. * * @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes * @return \Illuminate\Database\Eloquent\Model|TModel */ public function createOneQuietly($attributes = []) { return $this->count(null)->createQuietly($attributes); } /** * Create a collection of models and persist them to the database. * * @param int|null|iterable<int, array<string, mixed>> $records * @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel> */ public function createMany(int|iterable|null $records = null) { if (is_null($records)) { $records = $this->count ?? 1; } if (is_numeric($records)) { $records = array_fill(0, $records, []); } return new EloquentCollection( collect($records)->map(function ($record) { return $this->state($record)->create(); }) ); } /** * Create a collection of models and persist them to the database without dispatching any model events. * * @param int|null|iterable<int, array<string, mixed>> $records * @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel> */ public function createManyQuietly(int|iterable|null $records = null) { return Model::withoutEvents(function () use ($records) { return $this->createMany($records); }); } /** * Create a collection of models and persist them to the database. * * @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes * @param \Illuminate\Database\Eloquent\Model|null $parent * @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>|\Illuminate\Database\Eloquent\Model|TModel */ public function create($attributes = [], ?Model $parent = null) { if (! empty($attributes)) { return $this->state($attributes)->create([], $parent); } $results = $this->make($attributes, $parent); if ($results instanceof Model) { $this->store(collect([$results])); $this->callAfterCreating(collect([$results]), $parent); } else { $this->store($results); $this->callAfterCreating($results, $parent); } return $results; } /** * Create a collection of models and persist them to the database without dispatching any model events. * * @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes * @param \Illuminate\Database\Eloquent\Model|null $parent * @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>|\Illuminate\Database\Eloquent\Model|TModel */ public function createQuietly($attributes = [], ?Model $parent = null) { return Model::withoutEvents(function () use ($attributes, $parent) { return $this->create($attributes, $parent); }); } /** * Create a callback that persists a model in the database when invoked. * * @param array<string, mixed> $attributes * @param \Illuminate\Database\Eloquent\Model|null $parent * @return \Closure(): (\Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>|\Illuminate\Database\Eloquent\Model|TModel) */ public function lazy(array $attributes = [], ?Model $parent = null) { return fn () => $this->create($attributes, $parent); } /** * Set the connection name on the results and store them. * * @param \Illuminate\Support\Collection $results * @return void */ protected function store(Collection $results) { $results->each(function ($model) { if (! isset($this->connection)) { $model->setConnection($model->newQueryWithoutScopes()->getConnection()->getName()); } $model->save(); foreach ($model->getRelations() as $name => $items) { if ($items instanceof Enumerable && $items->isEmpty()) { $model->unsetRelation($name); } } $this->createChildren($model); }); } /** * Create the children for the given model. * * @param \Illuminate\Database\Eloquent\Model $model * @return void */ protected function createChildren(Model $model) { Model::unguarded(function () use ($model) { $this->has->each(function ($has) use ($model) { $has->recycle($this->recycle)->createFor($model); }); }); } /** * Make a single instance of the model. * * @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes * @return \Illuminate\Database\Eloquent\Model|TModel */ public function makeOne($attributes = []) { return $this->count(null)->make($attributes); } /** * Create a collection of models. * * @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes * @param \Illuminate\Database\Eloquent\Model|null $parent * @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>|\Illuminate\Database\Eloquent\Model|TModel */ public function make($attributes = [], ?Model $parent = null) { if (! empty($attributes)) { return $this->state($attributes)->make([], $parent); } if ($this->count === null) { return tap($this->makeInstance($parent), function ($instance) { $this->callAfterMaking(collect([$instance])); }); } if ($this->count < 1) { return $this->newModel()->newCollection(); } $instances = $this->newModel()->newCollection(array_map(function () use ($parent) { return $this->makeInstance($parent); }, range(1, $this->count))); $this->callAfterMaking($instances); return $instances; } /** * Make an instance of the model with the given attributes. * * @param \Illuminate\Database\Eloquent\Model|null $parent * @return \Illuminate\Database\Eloquent\Model */ protected function makeInstance(?Model $parent) { return Model::unguarded(function () use ($parent) { return tap($this->newModel($this->getExpandedAttributes($parent)), function ($instance) { if (isset($this->connection)) { $instance->setConnection($this->connection); } }); }); } /** * Get a raw attributes array for the model. * * @param \Illuminate\Database\Eloquent\Model|null $parent * @return mixed */ protected function getExpandedAttributes(?Model $parent) { return $this->expandAttributes($this->getRawAttributes($parent)); } /** * Get the raw attributes for the model as an array. * * @param \Illuminate\Database\Eloquent\Model|null $parent * @return array */ protected function getRawAttributes(?Model $parent) { return $this->states->pipe(function ($states) { return $this->for->isEmpty() ? $states : new Collection(array_merge([function () { return $this->parentResolvers(); }], $states->all())); })->reduce(function ($carry, $state) use ($parent) { if ($state instanceof Closure) { $state = $state->bindTo($this); } return array_merge($carry, $state($carry, $parent)); }, $this->definition()); } /** * Create the parent relationship resolvers (as deferred Closures). * * @return array */ protected function parentResolvers() { $model = $this->newModel(); return $this->for->map(function (BelongsToRelationship $for) use ($model) { return $for->recycle($this->recycle)->attributesFor($model); })->collapse()->all(); } /** * Expand all attributes to their underlying values. * * @param array $definition * @return array */ protected function expandAttributes(array $definition) { return collect($definition) ->map($evaluateRelations = function ($attribute) { if ($attribute instanceof self) { $attribute = $this->getRandomRecycledModel($attribute->modelName())?->getKey() ?? $attribute->recycle($this->recycle)->create()->getKey(); } elseif ($attribute instanceof Model) { $attribute = $attribute->getKey(); } return $attribute; }) ->map(function ($attribute, $key) use (&$definition, $evaluateRelations) { if (is_callable($attribute) && ! is_string($attribute) && ! is_array($attribute)) { $attribute = $attribute($definition); } $attribute = $evaluateRelations($attribute); $definition[$key] = $attribute; return $attribute; }) ->all(); } /** * Add a new state transformation to the model definition. * * @param (callable(array<string, mixed>, \Illuminate\Database\Eloquent\Model|null): array<string, mixed>)|array<string, mixed> $state * @return static */ public function state($state) { return $this->newInstance([ 'states' => $this->states->concat([ is_callable($state) ? $state : function () use ($state) { return $state; }, ]), ]); } /** * Set a single model attribute. * * @param string|int $key * @param mixed $value * @return static */ public function set($key, $value) { return $this->state([$key => $value]); } /** * Add a new sequenced state transformation to the model definition. * * @param mixed ...$sequence * @return static */ public function sequence(...$sequence) { return $this->state(new Sequence(...$sequence)); } /** * Add a new sequenced state transformation to the model definition and update the pending creation count to the size of the sequence. * * @param array ...$sequence * @return static */ public function forEachSequence(...$sequence) { return $this->state(new Sequence(...$sequence))->count(count($sequence)); } /** * Add a new cross joined sequenced state transformation to the model definition. * * @param array ...$sequence * @return static */ public function crossJoinSequence(...$sequence) { return $this->state(new CrossJoinSequence(...$sequence)); } /** * Define a child relationship for the model. * * @param \Illuminate\Database\Eloquent\Factories\Factory $factory * @param string|null $relationship * @return static */ public function has(self $factory, $relationship = null) { return $this->newInstance([ 'has' => $this->has->concat([new Relationship( $factory, $relationship ?? $this->guessRelationship($factory->modelName()) )]), ]); } /** * Attempt to guess the relationship name for a "has" relationship. * * @param string $related * @return string */ protected function guessRelationship(string $related) { $guess = Str::camel(Str::plural(class_basename($related))); return method_exists($this->modelName(), $guess) ? $guess : Str::singular($guess); } /** * Define an attached relationship for the model. * * @param \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $factory * @param (callable(): array<string, mixed>)|array<string, mixed> $pivot * @param string|null $relationship * @return static */ public function hasAttached($factory, $pivot = [], $relationship = null) { return $this->newInstance([ 'has' => $this->has->concat([new BelongsToManyRelationship( $factory, $pivot, $relationship ?? Str::camel(Str::plural(class_basename( $factory instanceof Factory ? $factory->modelName() : Collection::wrap($factory)->first() ))) )]), ]); } /** * Define a parent relationship for the model. * * @param \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Database\Eloquent\Model $factory * @param string|null $relationship * @return static */ public function for($factory, $relationship = null) { return $this->newInstance(['for' => $this->for->concat([new BelongsToRelationship( $factory, $relationship ?? Str::camel(class_basename( $factory instanceof Factory ? $factory->modelName() : $factory )) )])]); } /** * Provide model instances to use instead of any nested factory calls when creating relationships. * * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection|array $model * @return static */ public function recycle($model) { // Group provided models by the type and merge them into existing recycle collection return $this->newInstance([ 'recycle' => $this->recycle ->flatten() ->merge( Collection::wrap($model instanceof Model ? func_get_args() : $model) ->flatten() )->groupBy(fn ($model) => get_class($model)), ]); } /** * Retrieve a random model of a given type from previously provided models to recycle. * * @param string $modelClassName * @return \Illuminate\Database\Eloquent\Model|null */ public function getRandomRecycledModel($modelClassName) { return $this->recycle->get($modelClassName)?->random(); } /** * Add a new "after making" callback to the model definition. * * @param \Closure(\Illuminate\Database\Eloquent\Model|TModel): mixed $callback * @return static */ public function afterMaking(Closure $callback) { return $this->newInstance(['afterMaking' => $this->afterMaking->concat([$callback])]); } /** * Add a new "after creating" callback to the model definition. * * @param \Closure(\Illuminate\Database\Eloquent\Model|TModel): mixed $callback * @return static */ public function afterCreating(Closure $callback) { return $this->newInstance(['afterCreating' => $this->afterCreating->concat([$callback])]); } /** * Call the "after making" callbacks for the given model instances. * * @param \Illuminate\Support\Collection $instances * @return void */ protected function callAfterMaking(Collection $instances) { $instances->each(function ($model) { $this->afterMaking->each(function ($callback) use ($model) { $callback($model); }); }); } /** * Call the "after creating" callbacks for the given model instances. * * @param \Illuminate\Support\Collection $instances * @param \Illuminate\Database\Eloquent\Model|null $parent * @return void */ protected function callAfterCreating(Collection $instances, ?Model $parent = null) { $instances->each(function ($model) use ($parent) { $this->afterCreating->each(function ($callback) use ($model, $parent) { $callback($model, $parent); }); }); } /** * Specify how many models should be generated. * * @param int|null $count * @return static */ public function count(?int $count) { return $this->newInstance(['count' => $count]); } /** * Specify the database connection that should be used to generate models. * * @param string $connection * @return static */ public function connection(string $connection) { return $this->newInstance(['connection' => $connection]); } /** * Create a new instance of the factory builder with the given mutated properties. * * @param array $arguments * @return static */ protected function newInstance(array $arguments = []) { return new static(...array_values(array_merge([ 'count' => $this->count, 'states' => $this->states, 'has' => $this->has, 'for' => $this->for, 'afterMaking' => $this->afterMaking, 'afterCreating' => $this->afterCreating, 'connection' => $this->connection, 'recycle' => $this->recycle, ], $arguments))); } /** * Get a new model instance. * * @param array<string, mixed> $attributes * @return \Illuminate\Database\Eloquent\Model|TModel */ public function newModel(array $attributes = []) { $model = $this->modelName(); return new $model($attributes); } /** * Get the name of the model that is generated by the factory. * * @return class-string<\Illuminate\Database\Eloquent\Model|TModel> */ public function modelName() { $resolver = static::$modelNameResolver ?? function (self $factory) { $namespacedFactoryBasename = Str::replaceLast( 'Factory', '', Str::replaceFirst(static::$namespace, '', get_class($factory)) ); $factoryBasename = Str::replaceLast('Factory', '', class_basename($factory)); $appNamespace = static::appNamespace(); return class_exists($appNamespace.'Models\\'.$namespacedFactoryBasename) ? $appNamespace.'Models\\'.$namespacedFactoryBasename : $appNamespace.$factoryBasename; }; return $this->model ?? $resolver($this); } /** * Specify the callback that should be invoked to guess model names based on factory names. * * @param callable(self): class-string<\Illuminate\Database\Eloquent\Model|TModel> $callback * @return void */ public static function guessModelNamesUsing(callable $callback) { static::$modelNameResolver = $callback; } /** * Specify the default namespace that contains the application's model factories. * * @param string $namespace * @return void */ public static function useNamespace(string $namespace) { static::$namespace = $namespace; } /** * Get a new factory instance for the given model name. * * @param class-string<\Illuminate\Database\Eloquent\Model> $modelName * @return \Illuminate\Database\Eloquent\Factories\Factory */ public static function factoryForModel(string $modelName) { $factory = static::resolveFactoryName($modelName); return $factory::new(); } /** * Specify the callback that should be invoked to guess factory names based on dynamic relationship names. * * @param callable(class-string<\Illuminate\Database\Eloquent\Model>): class-string<\Illuminate\Database\Eloquent\Factories\Factory> $callback * @return void */ public static function guessFactoryNamesUsing(callable $callback) { static::$factoryNameResolver = $callback; } /** * Get a new Faker instance. * * @return \Faker\Generator */ protected function withFaker() { return Container::getInstance()->make(Generator::class); } /** * Get the factory name for the given model name. * * @param class-string<\Illuminate\Database\Eloquent\Model> $modelName * @return class-string<\Illuminate\Database\Eloquent\Factories\Factory> */ public static function resolveFactoryName(string $modelName) { $resolver = static::$factoryNameResolver ?? function (string $modelName) { $appNamespace = static::appNamespace(); $modelName = Str::startsWith($modelName, $appNamespace.'Models\\') ? Str::after($modelName, $appNamespace.'Models\\') : Str::after($modelName, $appNamespace); return static::$namespace.$modelName.'Factory'; }; return $resolver($modelName); } /** * Get the application namespace for the application. * * @return string */ protected static function appNamespace() { try { return Container::getInstance() ->make(Application::class) ->getNamespace(); } catch (Throwable) { return 'App\\'; } } /** * Proxy dynamic factory methods onto their proper methods. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } if ($method === 'trashed' && in_array(SoftDeletes::class, class_uses_recursive($this->modelName()))) { return $this->state([ $this->newModel()->getDeletedAtColumn() => $parameters[0] ?? Carbon::now()->subDay(), ]); } if (! Str::startsWith($method, ['for', 'has'])) { static::throwBadMethodCallException($method); } $relationship = Str::camel(Str::substr($method, 3)); $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_starts_with($method, 'for')) { return $this->for($factory->state($parameters[0] ?? []), $relationship); } elseif (str_starts_with($method, 'has')) { return $this->has( $factory ->count(is_numeric($parameters[0] ?? null) ? $parameters[0] : 1) ->state((is_callable($parameters[0] ?? null) || is_array($parameters[0] ?? null)) ? $parameters[0] : ($parameters[1] ?? [])), $relationship ); } } } src/Illuminate/Database/Eloquent/Factories/Sequence.php 0000644 00000002160 15107327037 0017142 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Factories; use Countable; class Sequence implements Countable { /** * The sequence of return values. * * @var array */ protected $sequence; /** * The count of the sequence items. * * @var int */ public $count; /** * The current index of the sequence iteration. * * @var int */ public $index = 0; /** * Create a new sequence instance. * * @param mixed ...$sequence * @return void */ public function __construct(...$sequence) { $this->sequence = $sequence; $this->count = count($sequence); } /** * Get the current count of the sequence items. * * @return int */ public function count(): int { return $this->count; } /** * Get the next value in the sequence. * * @return mixed */ public function __invoke() { return tap(value($this->sequence[$this->index % $this->count], $this), function () { $this->index = $this->index + 1; }); } } src/Illuminate/Database/Eloquent/Factories/Relationship.php 0000644 00000004150 15107327037 0020034 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Factories; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasOneOrMany; use Illuminate\Database\Eloquent\Relations\MorphOneOrMany; class Relationship { /** * The related factory instance. * * @var \Illuminate\Database\Eloquent\Factories\Factory */ protected $factory; /** * The relationship name. * * @var string */ protected $relationship; /** * Create a new child relationship instance. * * @param \Illuminate\Database\Eloquent\Factories\Factory $factory * @param string $relationship * @return void */ public function __construct(Factory $factory, $relationship) { $this->factory = $factory; $this->relationship = $relationship; } /** * Create the child relationship for the given parent model. * * @param \Illuminate\Database\Eloquent\Model $parent * @return void */ public function createFor(Model $parent) { $relationship = $parent->{$this->relationship}(); if ($relationship instanceof MorphOneOrMany) { $this->factory->state([ $relationship->getMorphType() => $relationship->getMorphClass(), $relationship->getForeignKeyName() => $relationship->getParentKey(), ])->create([], $parent); } elseif ($relationship instanceof HasOneOrMany) { $this->factory->state([ $relationship->getForeignKeyName() => $relationship->getParentKey(), ])->create([], $parent); } elseif ($relationship instanceof BelongsToMany) { $relationship->attach($this->factory->create([], $parent)); } } /** * Specify the model instances to always use when creating relationships. * * @param \Illuminate\Support\Collection $recycle * @return $this */ public function recycle($recycle) { $this->factory = $this->factory->recycle($recycle); return $this; } } src/Illuminate/Database/ConnectionResolver.php 0000644 00000003720 15107327037 0015503 0 ustar 00 <?php namespace Illuminate\Database; class ConnectionResolver implements ConnectionResolverInterface { /** * All of the registered connections. * * @var \Illuminate\Database\ConnectionInterface[] */ protected $connections = []; /** * The default connection name. * * @var string */ protected $default; /** * Create a new connection resolver instance. * * @param array<string, \Illuminate\Database\ConnectionInterface> $connections * @return void */ public function __construct(array $connections = []) { foreach ($connections as $name => $connection) { $this->addConnection($name, $connection); } } /** * Get a database connection instance. * * @param string|null $name * @return \Illuminate\Database\ConnectionInterface */ public function connection($name = null) { if (is_null($name)) { $name = $this->getDefaultConnection(); } return $this->connections[$name]; } /** * Add a connection to the resolver. * * @param string $name * @param \Illuminate\Database\ConnectionInterface $connection * @return void */ public function addConnection($name, ConnectionInterface $connection) { $this->connections[$name] = $connection; } /** * Check if a connection has been registered. * * @param string $name * @return bool */ public function hasConnection($name) { return isset($this->connections[$name]); } /** * Get the default connection name. * * @return string */ public function getDefaultConnection() { return $this->default; } /** * Set the default connection name. * * @param string $name * @return void */ public function setDefaultConnection($name) { $this->default = $name; } } src/Illuminate/Database/Query/IndexHint.php 0000644 00000000777 15107327037 0014672 0 ustar 00 <?php namespace Illuminate\Database\Query; class IndexHint { /** * The type of query hint. * * @var string */ public $type; /** * The name of the index. * * @var string */ public $index; /** * Create a new index hint instance. * * @param string $type * @param string $index * @return void */ public function __construct($type, $index) { $this->type = $type; $this->index = $index; } } src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php 0000644 00000033476 15107327037 0020010 0 ustar 00 <?php namespace Illuminate\Database\Query\Grammars; use Illuminate\Database\Query\Builder; use Illuminate\Support\Arr; use Illuminate\Support\Str; class SqlServerGrammar extends Grammar { /** * All of the available clause operators. * * @var string[] */ protected $operators = [ '=', '<', '>', '<=', '>=', '!<', '!>', '<>', '!=', 'like', 'not like', 'ilike', '&', '&=', '|', '|=', '^', '^=', ]; /** * The components that make up a select clause. * * @var string[] */ protected $selectComponents = [ 'aggregate', 'columns', 'from', 'indexHint', 'joins', 'wheres', 'groups', 'havings', 'orders', 'offset', 'limit', 'lock', ]; /** * Compile a select query into SQL. * * @param \Illuminate\Database\Query\Builder $query * @return string */ public function compileSelect(Builder $query) { // An order by clause is required for SQL Server offset to function... if ($query->offset && empty($query->orders)) { $query->orders[] = ['sql' => '(SELECT 0)']; } return parent::compileSelect($query); } /** * Compile the "select *" portion of the query. * * @param \Illuminate\Database\Query\Builder $query * @param array $columns * @return string|null */ protected function compileColumns(Builder $query, $columns) { if (! is_null($query->aggregate)) { return; } $select = $query->distinct ? 'select distinct ' : 'select '; // If there is a limit on the query, but not an offset, we will add the top // clause to the query, which serves as a "limit" type clause within the // SQL Server system similar to the limit keywords available in MySQL. if (is_numeric($query->limit) && $query->limit > 0 && $query->offset <= 0) { $select .= 'top '.((int) $query->limit).' '; } return $select.$this->columnize($columns); } /** * Compile the "from" portion of the query. * * @param \Illuminate\Database\Query\Builder $query * @param string $table * @return string */ protected function compileFrom(Builder $query, $table) { $from = parent::compileFrom($query, $table); if (is_string($query->lock)) { return $from.' '.$query->lock; } if (! is_null($query->lock)) { return $from.' with(rowlock,'.($query->lock ? 'updlock,' : '').'holdlock)'; } return $from; } /** * Compile the index hints for the query. * * @param \Illuminate\Database\Query\Builder $query * @param \Illuminate\Database\Query\IndexHint $indexHint * @return string */ protected function compileIndexHint(Builder $query, $indexHint) { return $indexHint->type === 'force' ? "with (index({$indexHint->index}))" : ''; } /** * {@inheritdoc} * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereBitwise(Builder $query, $where) { $value = $this->parameter($where['value']); $operator = str_replace('?', '??', $where['operator']); return '('.$this->wrap($where['column']).' '.$operator.' '.$value.') != 0'; } /** * Compile a "where date" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereDate(Builder $query, $where) { $value = $this->parameter($where['value']); return 'cast('.$this->wrap($where['column']).' as date) '.$where['operator'].' '.$value; } /** * Compile a "where time" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereTime(Builder $query, $where) { $value = $this->parameter($where['value']); return 'cast('.$this->wrap($where['column']).' as time) '.$where['operator'].' '.$value; } /** * Compile a "JSON contains" statement into SQL. * * @param string $column * @param string $value * @return string */ protected function compileJsonContains($column, $value) { [$field, $path] = $this->wrapJsonFieldAndPath($column); return $value.' in (select [value] from openjson('.$field.$path.'))'; } /** * Prepare the binding for a "JSON contains" statement. * * @param mixed $binding * @return string */ public function prepareBindingForJsonContains($binding) { return is_bool($binding) ? json_encode($binding) : $binding; } /** * Compile a "JSON contains key" statement into SQL. * * @param string $column * @return string */ protected function compileJsonContainsKey($column) { $segments = explode('->', $column); $lastSegment = array_pop($segments); if (preg_match('/\[([0-9]+)\]$/', $lastSegment, $matches)) { $segments[] = Str::beforeLast($lastSegment, $matches[0]); $key = $matches[1]; } else { $key = "'".str_replace("'", "''", $lastSegment)."'"; } [$field, $path] = $this->wrapJsonFieldAndPath(implode('->', $segments)); return $key.' in (select [key] from openjson('.$field.$path.'))'; } /** * Compile a "JSON length" statement into SQL. * * @param string $column * @param string $operator * @param string $value * @return string */ protected function compileJsonLength($column, $operator, $value) { [$field, $path] = $this->wrapJsonFieldAndPath($column); return '(select count(*) from openjson('.$field.$path.')) '.$operator.' '.$value; } /** * Compile a "JSON value cast" statement into SQL. * * @param string $value * @return string */ public function compileJsonValueCast($value) { return 'json_query('.$value.')'; } /** * Compile a single having clause. * * @param array $having * @return string */ protected function compileHaving(array $having) { if ($having['type'] === 'Bitwise') { return $this->compileHavingBitwise($having); } return parent::compileHaving($having); } /** * Compile a having clause involving a bitwise operator. * * @param array $having * @return string */ protected function compileHavingBitwise($having) { $column = $this->wrap($having['column']); $parameter = $this->parameter($having['value']); return '('.$column.' '.$having['operator'].' '.$parameter.') != 0'; } /** * Compile a delete statement without joins into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param string $table * @param string $where * @return string */ protected function compileDeleteWithoutJoins(Builder $query, $table, $where) { $sql = parent::compileDeleteWithoutJoins($query, $table, $where); return ! is_null($query->limit) && $query->limit > 0 && $query->offset <= 0 ? Str::replaceFirst('delete', 'delete top ('.$query->limit.')', $sql) : $sql; } /** * Compile the random statement into SQL. * * @param string|int $seed * @return string */ public function compileRandom($seed) { return 'NEWID()'; } /** * Compile the "limit" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @param int $limit * @return string */ protected function compileLimit(Builder $query, $limit) { $limit = (int) $limit; if ($limit && $query->offset > 0) { return "fetch next {$limit} rows only"; } return ''; } /** * Compile the "offset" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @param int $offset * @return string */ protected function compileOffset(Builder $query, $offset) { $offset = (int) $offset; if ($offset) { return "offset {$offset} rows"; } return ''; } /** * Compile the lock into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param bool|string $value * @return string */ protected function compileLock(Builder $query, $value) { return ''; } /** * Wrap a union subquery in parentheses. * * @param string $sql * @return string */ protected function wrapUnion($sql) { return 'select * from ('.$sql.') as '.$this->wrapTable('temp_table'); } /** * Compile an exists statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @return string */ public function compileExists(Builder $query) { $existsQuery = clone $query; $existsQuery->columns = []; return $this->compileSelect($existsQuery->selectRaw('1 [exists]')->limit(1)); } /** * Compile an update statement with joins into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param string $table * @param string $columns * @param string $where * @return string */ protected function compileUpdateWithJoins(Builder $query, $table, $columns, $where) { $alias = last(explode(' as ', $table)); $joins = $this->compileJoins($query, $query->joins); return "update {$alias} set {$columns} from {$table} {$joins} {$where}"; } /** * Compile an "upsert" statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @param array $uniqueBy * @param array $update * @return string */ public function compileUpsert(Builder $query, array $values, array $uniqueBy, array $update) { $columns = $this->columnize(array_keys(reset($values))); $sql = 'merge '.$this->wrapTable($query->from).' '; $parameters = collect($values)->map(function ($record) { return '('.$this->parameterize($record).')'; })->implode(', '); $sql .= 'using (values '.$parameters.') '.$this->wrapTable('laravel_source').' ('.$columns.') '; $on = collect($uniqueBy)->map(function ($column) use ($query) { return $this->wrap('laravel_source.'.$column).' = '.$this->wrap($query->from.'.'.$column); })->implode(' and '); $sql .= 'on '.$on.' '; if ($update) { $update = collect($update)->map(function ($value, $key) { return is_numeric($key) ? $this->wrap($value).' = '.$this->wrap('laravel_source.'.$value) : $this->wrap($key).' = '.$this->parameter($value); })->implode(', '); $sql .= 'when matched then update set '.$update.' '; } $sql .= 'when not matched then insert ('.$columns.') values ('.$columns.');'; return $sql; } /** * Prepare the bindings for an update statement. * * @param array $bindings * @param array $values * @return array */ public function prepareBindingsForUpdate(array $bindings, array $values) { $cleanBindings = Arr::except($bindings, 'select'); return array_values( array_merge($values, Arr::flatten($cleanBindings)) ); } /** * Compile the SQL statement to define a savepoint. * * @param string $name * @return string */ public function compileSavepoint($name) { return 'SAVE TRANSACTION '.$name; } /** * Compile the SQL statement to execute a savepoint rollback. * * @param string $name * @return string */ public function compileSavepointRollBack($name) { return 'ROLLBACK TRANSACTION '.$name; } /** * Get the format for database stored dates. * * @return string */ public function getDateFormat() { return 'Y-m-d H:i:s.v'; } /** * Wrap a single string in keyword identifiers. * * @param string $value * @return string */ protected function wrapValue($value) { return $value === '*' ? $value : '['.str_replace(']', ']]', $value).']'; } /** * Wrap the given JSON selector. * * @param string $value * @return string */ protected function wrapJsonSelector($value) { [$field, $path] = $this->wrapJsonFieldAndPath($value); return 'json_value('.$field.$path.')'; } /** * Wrap the given JSON boolean value. * * @param string $value * @return string */ protected function wrapJsonBooleanValue($value) { return "'".$value."'"; } /** * Wrap a table in keyword identifiers. * * @param \Illuminate\Contracts\Database\Query\Expression|string $table * @return string */ public function wrapTable($table) { if (! $this->isExpression($table)) { return $this->wrapTableValuedFunction(parent::wrapTable($table)); } return $this->getValue($table); } /** * Wrap a table in keyword identifiers. * * @param string $table * @return string */ protected function wrapTableValuedFunction($table) { if (preg_match('/^(.+?)(\(.*?\))]$/', $table, $matches) === 1) { $table = $matches[1].']'.$matches[2]; } return $table; } } src/Illuminate/Database/Query/Grammars/PostgresGrammar.php 0000644 00000050710 15107327037 0017656 0 ustar 00 <?php namespace Illuminate\Database\Query\Grammars; use Illuminate\Database\Query\Builder; use Illuminate\Support\Arr; use Illuminate\Support\Str; class PostgresGrammar extends Grammar { /** * All of the available clause operators. * * @var string[] */ protected $operators = [ '=', '<', '>', '<=', '>=', '<>', '!=', 'like', 'not like', 'between', 'ilike', 'not ilike', '~', '&', '|', '#', '<<', '>>', '<<=', '>>=', '&&', '@>', '<@', '?', '?|', '?&', '||', '-', '@?', '@@', '#-', 'is distinct from', 'is not distinct from', ]; /** * The grammar specific bitwise operators. * * @var array */ protected $bitwiseOperators = [ '~', '&', '|', '#', '<<', '>>', '<<=', '>>=', ]; /** * Compile a basic where clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereBasic(Builder $query, $where) { if (str_contains(strtolower($where['operator']), 'like')) { return sprintf( '%s::text %s %s', $this->wrap($where['column']), $where['operator'], $this->parameter($where['value']) ); } return parent::whereBasic($query, $where); } /** * Compile a bitwise operator where clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereBitwise(Builder $query, $where) { $value = $this->parameter($where['value']); $operator = str_replace('?', '??', $where['operator']); return '('.$this->wrap($where['column']).' '.$operator.' '.$value.')::bool'; } /** * Compile a "where date" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereDate(Builder $query, $where) { $value = $this->parameter($where['value']); return $this->wrap($where['column']).'::date '.$where['operator'].' '.$value; } /** * Compile a "where time" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereTime(Builder $query, $where) { $value = $this->parameter($where['value']); return $this->wrap($where['column']).'::time '.$where['operator'].' '.$value; } /** * Compile a date based where clause. * * @param string $type * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function dateBasedWhere($type, Builder $query, $where) { $value = $this->parameter($where['value']); return 'extract('.$type.' from '.$this->wrap($where['column']).') '.$where['operator'].' '.$value; } /** * Compile a "where fulltext" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ public function whereFullText(Builder $query, $where) { $language = $where['options']['language'] ?? 'english'; if (! in_array($language, $this->validFullTextLanguages())) { $language = 'english'; } $columns = collect($where['columns'])->map(function ($column) use ($language) { return "to_tsvector('{$language}', {$this->wrap($column)})"; })->implode(' || '); $mode = 'plainto_tsquery'; if (($where['options']['mode'] ?? []) === 'phrase') { $mode = 'phraseto_tsquery'; } if (($where['options']['mode'] ?? []) === 'websearch') { $mode = 'websearch_to_tsquery'; } return "({$columns}) @@ {$mode}('{$language}', {$this->parameter($where['value'])})"; } /** * Get an array of valid full text languages. * * @return array */ protected function validFullTextLanguages() { return [ 'simple', 'arabic', 'danish', 'dutch', 'english', 'finnish', 'french', 'german', 'hungarian', 'indonesian', 'irish', 'italian', 'lithuanian', 'nepali', 'norwegian', 'portuguese', 'romanian', 'russian', 'spanish', 'swedish', 'tamil', 'turkish', ]; } /** * Compile the "select *" portion of the query. * * @param \Illuminate\Database\Query\Builder $query * @param array $columns * @return string|null */ protected function compileColumns(Builder $query, $columns) { // If the query is actually performing an aggregating select, we will let that // compiler handle the building of the select clauses, as it will need some // more syntax that is best handled by that function to keep things neat. if (! is_null($query->aggregate)) { return; } if (is_array($query->distinct)) { $select = 'select distinct on ('.$this->columnize($query->distinct).') '; } elseif ($query->distinct) { $select = 'select distinct '; } else { $select = 'select '; } return $select.$this->columnize($columns); } /** * Compile a "JSON contains" statement into SQL. * * @param string $column * @param string $value * @return string */ protected function compileJsonContains($column, $value) { $column = str_replace('->>', '->', $this->wrap($column)); return '('.$column.')::jsonb @> '.$value; } /** * Compile a "JSON contains key" statement into SQL. * * @param string $column * @return string */ protected function compileJsonContainsKey($column) { $segments = explode('->', $column); $lastSegment = array_pop($segments); if (filter_var($lastSegment, FILTER_VALIDATE_INT) !== false) { $i = $lastSegment; } elseif (preg_match('/\[(-?[0-9]+)\]$/', $lastSegment, $matches)) { $segments[] = Str::beforeLast($lastSegment, $matches[0]); $i = $matches[1]; } $column = str_replace('->>', '->', $this->wrap(implode('->', $segments))); if (isset($i)) { return vsprintf('case when %s then %s else false end', [ 'jsonb_typeof(('.$column.")::jsonb) = 'array'", 'jsonb_array_length(('.$column.')::jsonb) >= '.($i < 0 ? abs($i) : $i + 1), ]); } $key = "'".str_replace("'", "''", $lastSegment)."'"; return 'coalesce(('.$column.')::jsonb ?? '.$key.', false)'; } /** * Compile a "JSON length" statement into SQL. * * @param string $column * @param string $operator * @param string $value * @return string */ protected function compileJsonLength($column, $operator, $value) { $column = str_replace('->>', '->', $this->wrap($column)); return 'jsonb_array_length(('.$column.')::jsonb) '.$operator.' '.$value; } /** * Compile a single having clause. * * @param array $having * @return string */ protected function compileHaving(array $having) { if ($having['type'] === 'Bitwise') { return $this->compileHavingBitwise($having); } return parent::compileHaving($having); } /** * Compile a having clause involving a bitwise operator. * * @param array $having * @return string */ protected function compileHavingBitwise($having) { $column = $this->wrap($having['column']); $parameter = $this->parameter($having['value']); return '('.$column.' '.$having['operator'].' '.$parameter.')::bool'; } /** * Compile the lock into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param bool|string $value * @return string */ protected function compileLock(Builder $query, $value) { if (! is_string($value)) { return $value ? 'for update' : 'for share'; } return $value; } /** * Compile an insert ignore statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ public function compileInsertOrIgnore(Builder $query, array $values) { return $this->compileInsert($query, $values).' on conflict do nothing'; } /** * Compile an insert and get ID statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @param string $sequence * @return string */ public function compileInsertGetId(Builder $query, $values, $sequence) { return $this->compileInsert($query, $values).' returning '.$this->wrap($sequence ?: 'id'); } /** * Compile an update statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ public function compileUpdate(Builder $query, array $values) { if (isset($query->joins) || isset($query->limit)) { return $this->compileUpdateWithJoinsOrLimit($query, $values); } return parent::compileUpdate($query, $values); } /** * Compile the columns for an update statement. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ protected function compileUpdateColumns(Builder $query, array $values) { return collect($values)->map(function ($value, $key) { $column = last(explode('.', $key)); if ($this->isJsonSelector($key)) { return $this->compileJsonUpdateColumn($column, $value); } return $this->wrap($column).' = '.$this->parameter($value); })->implode(', '); } /** * Compile an "upsert" statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @param array $uniqueBy * @param array $update * @return string */ public function compileUpsert(Builder $query, array $values, array $uniqueBy, array $update) { $sql = $this->compileInsert($query, $values); $sql .= ' on conflict ('.$this->columnize($uniqueBy).') do update set '; $columns = collect($update)->map(function ($value, $key) { return is_numeric($key) ? $this->wrap($value).' = '.$this->wrapValue('excluded').'.'.$this->wrap($value) : $this->wrap($key).' = '.$this->parameter($value); })->implode(', '); return $sql.$columns; } /** * Prepares a JSON column being updated using the JSONB_SET function. * * @param string $key * @param mixed $value * @return string */ protected function compileJsonUpdateColumn($key, $value) { $segments = explode('->', $key); $field = $this->wrap(array_shift($segments)); $path = "'{".implode(',', $this->wrapJsonPathAttributes($segments, '"'))."}'"; return "{$field} = jsonb_set({$field}::jsonb, {$path}, {$this->parameter($value)})"; } /** * Compile an update from statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ public function compileUpdateFrom(Builder $query, $values) { $table = $this->wrapTable($query->from); // Each one of the columns in the update statements needs to be wrapped in the // keyword identifiers, also a place-holder needs to be created for each of // the values in the list of bindings so we can make the sets statements. $columns = $this->compileUpdateColumns($query, $values); $from = ''; if (isset($query->joins)) { // When using Postgres, updates with joins list the joined tables in the from // clause, which is different than other systems like MySQL. Here, we will // compile out the tables that are joined and add them to a from clause. $froms = collect($query->joins)->map(function ($join) { return $this->wrapTable($join->table); })->all(); if (count($froms) > 0) { $from = ' from '.implode(', ', $froms); } } $where = $this->compileUpdateWheres($query); return trim("update {$table} set {$columns}{$from} {$where}"); } /** * Compile the additional where clauses for updates with joins. * * @param \Illuminate\Database\Query\Builder $query * @return string */ protected function compileUpdateWheres(Builder $query) { $baseWheres = $this->compileWheres($query); if (! isset($query->joins)) { return $baseWheres; } // Once we compile the join constraints, we will either use them as the where // clause or append them to the existing base where clauses. If we need to // strip the leading boolean we will do so when using as the only where. $joinWheres = $this->compileUpdateJoinWheres($query); if (trim($baseWheres) == '') { return 'where '.$this->removeLeadingBoolean($joinWheres); } return $baseWheres.' '.$joinWheres; } /** * Compile the "join" clause where clauses for an update. * * @param \Illuminate\Database\Query\Builder $query * @return string */ protected function compileUpdateJoinWheres(Builder $query) { $joinWheres = []; // Here we will just loop through all of the join constraints and compile them // all out then implode them. This should give us "where" like syntax after // everything has been built and then we will join it to the real wheres. foreach ($query->joins as $join) { foreach ($join->wheres as $where) { $method = "where{$where['type']}"; $joinWheres[] = $where['boolean'].' '.$this->$method($query, $where); } } return implode(' ', $joinWheres); } /** * Prepare the bindings for an update statement. * * @param array $bindings * @param array $values * @return array */ public function prepareBindingsForUpdateFrom(array $bindings, array $values) { $values = collect($values)->map(function ($value, $column) { return is_array($value) || ($this->isJsonSelector($column) && ! $this->isExpression($value)) ? json_encode($value) : $value; })->all(); $bindingsWithoutWhere = Arr::except($bindings, ['select', 'where']); return array_values( array_merge($values, $bindings['where'], Arr::flatten($bindingsWithoutWhere)) ); } /** * Compile an update statement with joins or limit into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ protected function compileUpdateWithJoinsOrLimit(Builder $query, array $values) { $table = $this->wrapTable($query->from); $columns = $this->compileUpdateColumns($query, $values); $alias = last(preg_split('/\s+as\s+/i', $query->from)); $selectSql = $this->compileSelect($query->select($alias.'.ctid')); return "update {$table} set {$columns} where {$this->wrap('ctid')} in ({$selectSql})"; } /** * Prepare the bindings for an update statement. * * @param array $bindings * @param array $values * @return array */ public function prepareBindingsForUpdate(array $bindings, array $values) { $values = collect($values)->map(function ($value, $column) { return is_array($value) || ($this->isJsonSelector($column) && ! $this->isExpression($value)) ? json_encode($value) : $value; })->all(); $cleanBindings = Arr::except($bindings, 'select'); return array_values( array_merge($values, Arr::flatten($cleanBindings)) ); } /** * Compile a delete statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @return string */ public function compileDelete(Builder $query) { if (isset($query->joins) || isset($query->limit)) { return $this->compileDeleteWithJoinsOrLimit($query); } return parent::compileDelete($query); } /** * Compile a delete statement with joins or limit into SQL. * * @param \Illuminate\Database\Query\Builder $query * @return string */ protected function compileDeleteWithJoinsOrLimit(Builder $query) { $table = $this->wrapTable($query->from); $alias = last(preg_split('/\s+as\s+/i', $query->from)); $selectSql = $this->compileSelect($query->select($alias.'.ctid')); return "delete from {$table} where {$this->wrap('ctid')} in ({$selectSql})"; } /** * Compile a truncate table statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @return array */ public function compileTruncate(Builder $query) { return ['truncate '.$this->wrapTable($query->from).' restart identity cascade' => []]; } /** * Wrap the given JSON selector. * * @param string $value * @return string */ protected function wrapJsonSelector($value) { $path = explode('->', $value); $field = $this->wrapSegments(explode('.', array_shift($path))); $wrappedPath = $this->wrapJsonPathAttributes($path); $attribute = array_pop($wrappedPath); if (! empty($wrappedPath)) { return $field.'->'.implode('->', $wrappedPath).'->>'.$attribute; } return $field.'->>'.$attribute; } /** * Wrap the given JSON selector for boolean values. * * @param string $value * @return string */ protected function wrapJsonBooleanSelector($value) { $selector = str_replace( '->>', '->', $this->wrapJsonSelector($value) ); return '('.$selector.')::jsonb'; } /** * Wrap the given JSON boolean value. * * @param string $value * @return string */ protected function wrapJsonBooleanValue($value) { return "'".$value."'::jsonb"; } /** * Wrap the attributes of the given JSON path. * * @param array $path * @return array */ protected function wrapJsonPathAttributes($path) { $quote = func_num_args() === 2 ? func_get_arg(1) : "'"; return collect($path)->map(function ($attribute) { return $this->parseJsonPathArrayKeys($attribute); })->collapse()->map(function ($attribute) use ($quote) { return filter_var($attribute, FILTER_VALIDATE_INT) !== false ? $attribute : $quote.$attribute.$quote; })->all(); } /** * Parse the given JSON path attribute for array keys. * * @param string $attribute * @return array */ protected function parseJsonPathArrayKeys($attribute) { if (preg_match('/(\[[^\]]+\])+$/', $attribute, $parts)) { $key = Str::beforeLast($attribute, $parts[0]); preg_match_all('/\[([^\]]+)\]/', $parts[0], $keys); return collect([$key]) ->merge($keys[1]) ->diff('') ->values() ->all(); } return [$attribute]; } /** * Substitute the given bindings into the given raw SQL query. * * @param string $sql * @param array $bindings * @return string */ public function substituteBindingsIntoRawSql($sql, $bindings) { $query = parent::substituteBindingsIntoRawSql($sql, $bindings); foreach ($this->operators as $operator) { if (! str_contains($operator, '?')) { continue; } $query = str_replace(str_replace('?', '??', $operator), $operator, $query); } return $query; } } src/Illuminate/Database/Query/Grammars/MySqlGrammar.php 0000644 00000025262 15107327037 0017121 0 ustar 00 <?php namespace Illuminate\Database\Query\Grammars; use Illuminate\Database\Query\Builder; use Illuminate\Support\Str; class MySqlGrammar extends Grammar { /** * The grammar specific operators. * * @var string[] */ protected $operators = ['sounds like']; /** * Add a "where null" clause to the query. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereNull(Builder $query, $where) { $columnValue = (string) $this->getValue($where['column']); if ($this->isJsonSelector($columnValue)) { [$field, $path] = $this->wrapJsonFieldAndPath($columnValue); return '(json_extract('.$field.$path.') is null OR json_type(json_extract('.$field.$path.')) = \'NULL\')'; } return parent::whereNull($query, $where); } /** * Add a "where not null" clause to the query. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereNotNull(Builder $query, $where) { $columnValue = (string) $this->getValue($where['column']); if ($this->isJsonSelector($columnValue)) { [$field, $path] = $this->wrapJsonFieldAndPath($columnValue); return '(json_extract('.$field.$path.') is not null AND json_type(json_extract('.$field.$path.')) != \'NULL\')'; } return parent::whereNotNull($query, $where); } /** * Compile a "where fulltext" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ public function whereFullText(Builder $query, $where) { $columns = $this->columnize($where['columns']); $value = $this->parameter($where['value']); $mode = ($where['options']['mode'] ?? []) === 'boolean' ? ' in boolean mode' : ' in natural language mode'; $expanded = ($where['options']['expanded'] ?? []) && ($where['options']['mode'] ?? []) !== 'boolean' ? ' with query expansion' : ''; return "match ({$columns}) against (".$value."{$mode}{$expanded})"; } /** * Compile the index hints for the query. * * @param \Illuminate\Database\Query\Builder $query * @param \Illuminate\Database\Query\IndexHint $indexHint * @return string */ protected function compileIndexHint(Builder $query, $indexHint) { return match ($indexHint->type) { 'hint' => "use index ({$indexHint->index})", 'force' => "force index ({$indexHint->index})", default => "ignore index ({$indexHint->index})", }; } /** * Compile an insert ignore statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ public function compileInsertOrIgnore(Builder $query, array $values) { return Str::replaceFirst('insert', 'insert ignore', $this->compileInsert($query, $values)); } /** * Compile a "JSON contains" statement into SQL. * * @param string $column * @param string $value * @return string */ protected function compileJsonContains($column, $value) { [$field, $path] = $this->wrapJsonFieldAndPath($column); return 'json_contains('.$field.', '.$value.$path.')'; } /** * Compile a "JSON contains key" statement into SQL. * * @param string $column * @return string */ protected function compileJsonContainsKey($column) { [$field, $path] = $this->wrapJsonFieldAndPath($column); return 'ifnull(json_contains_path('.$field.', \'one\''.$path.'), 0)'; } /** * Compile a "JSON length" statement into SQL. * * @param string $column * @param string $operator * @param string $value * @return string */ protected function compileJsonLength($column, $operator, $value) { [$field, $path] = $this->wrapJsonFieldAndPath($column); return 'json_length('.$field.$path.') '.$operator.' '.$value; } /** * Compile a "JSON value cast" statement into SQL. * * @param string $value * @return string */ public function compileJsonValueCast($value) { return 'cast('.$value.' as json)'; } /** * Compile the random statement into SQL. * * @param string|int $seed * @return string */ public function compileRandom($seed) { return 'RAND('.$seed.')'; } /** * Compile the lock into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param bool|string $value * @return string */ protected function compileLock(Builder $query, $value) { if (! is_string($value)) { return $value ? 'for update' : 'lock in share mode'; } return $value; } /** * Compile an insert statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ public function compileInsert(Builder $query, array $values) { if (empty($values)) { $values = [[]]; } return parent::compileInsert($query, $values); } /** * Compile the columns for an update statement. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ protected function compileUpdateColumns(Builder $query, array $values) { return collect($values)->map(function ($value, $key) { if ($this->isJsonSelector($key)) { return $this->compileJsonUpdateColumn($key, $value); } return $this->wrap($key).' = '.$this->parameter($value); })->implode(', '); } /** * Compile an "upsert" statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @param array $uniqueBy * @param array $update * @return string */ public function compileUpsert(Builder $query, array $values, array $uniqueBy, array $update) { $useUpsertAlias = $query->connection->getConfig('use_upsert_alias'); $sql = $this->compileInsert($query, $values); if ($useUpsertAlias) { $sql .= ' as laravel_upsert_alias'; } $sql .= ' on duplicate key update '; $columns = collect($update)->map(function ($value, $key) use ($useUpsertAlias) { if (! is_numeric($key)) { return $this->wrap($key).' = '.$this->parameter($value); } return $useUpsertAlias ? $this->wrap($value).' = '.$this->wrap('laravel_upsert_alias').'.'.$this->wrap($value) : $this->wrap($value).' = values('.$this->wrap($value).')'; })->implode(', '); return $sql.$columns; } /** * Prepare a JSON column being updated using the JSON_SET function. * * @param string $key * @param mixed $value * @return string */ protected function compileJsonUpdateColumn($key, $value) { if (is_bool($value)) { $value = $value ? 'true' : 'false'; } elseif (is_array($value)) { $value = 'cast(? as json)'; } else { $value = $this->parameter($value); } [$field, $path] = $this->wrapJsonFieldAndPath($key); return "{$field} = json_set({$field}{$path}, {$value})"; } /** * Compile an update statement without joins into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param string $table * @param string $columns * @param string $where * @return string */ protected function compileUpdateWithoutJoins(Builder $query, $table, $columns, $where) { $sql = parent::compileUpdateWithoutJoins($query, $table, $columns, $where); if (! empty($query->orders)) { $sql .= ' '.$this->compileOrders($query, $query->orders); } if (isset($query->limit)) { $sql .= ' '.$this->compileLimit($query, $query->limit); } return $sql; } /** * Prepare the bindings for an update statement. * * Booleans, integers, and doubles are inserted into JSON updates as raw values. * * @param array $bindings * @param array $values * @return array */ public function prepareBindingsForUpdate(array $bindings, array $values) { $values = collect($values)->reject(function ($value, $column) { return $this->isJsonSelector($column) && is_bool($value); })->map(function ($value) { return is_array($value) ? json_encode($value) : $value; })->all(); return parent::prepareBindingsForUpdate($bindings, $values); } /** * Compile a delete query that does not use joins. * * @param \Illuminate\Database\Query\Builder $query * @param string $table * @param string $where * @return string */ protected function compileDeleteWithoutJoins(Builder $query, $table, $where) { $sql = parent::compileDeleteWithoutJoins($query, $table, $where); // When using MySQL, delete statements may contain order by statements and limits // so we will compile both of those here. Once we have finished compiling this // we will return the completed SQL statement so it will be executed for us. if (! empty($query->orders)) { $sql .= ' '.$this->compileOrders($query, $query->orders); } if (isset($query->limit)) { $sql .= ' '.$this->compileLimit($query, $query->limit); } return $sql; } /** * Wrap a single string in keyword identifiers. * * @param string $value * @return string */ protected function wrapValue($value) { return $value === '*' ? $value : '`'.str_replace('`', '``', $value).'`'; } /** * Wrap the given JSON selector. * * @param string $value * @return string */ protected function wrapJsonSelector($value) { [$field, $path] = $this->wrapJsonFieldAndPath($value); return 'json_unquote(json_extract('.$field.$path.'))'; } /** * Wrap the given JSON selector for boolean values. * * @param string $value * @return string */ protected function wrapJsonBooleanSelector($value) { [$field, $path] = $this->wrapJsonFieldAndPath($value); return 'json_extract('.$field.$path.')'; } } src/Illuminate/Database/Query/Grammars/Grammar.php 0000644 00000114327 15107327037 0016134 0 ustar 00 <?php namespace Illuminate\Database\Query\Grammars; use Illuminate\Contracts\Database\Query\Expression; use Illuminate\Database\Concerns\CompilesJsonPaths; use Illuminate\Database\Grammar as BaseGrammar; use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Arr; use RuntimeException; class Grammar extends BaseGrammar { use CompilesJsonPaths; /** * The grammar specific operators. * * @var array */ protected $operators = []; /** * The grammar specific bitwise operators. * * @var array */ protected $bitwiseOperators = []; /** * The components that make up a select clause. * * @var string[] */ protected $selectComponents = [ 'aggregate', 'columns', 'from', 'indexHint', 'joins', 'wheres', 'groups', 'havings', 'orders', 'limit', 'offset', 'lock', ]; /** * Compile a select query into SQL. * * @param \Illuminate\Database\Query\Builder $query * @return string */ public function compileSelect(Builder $query) { if (($query->unions || $query->havings) && $query->aggregate) { return $this->compileUnionAggregate($query); } // If the query does not have any columns set, we'll set the columns to the // * character to just get all of the columns from the database. Then we // can build the query and concatenate all the pieces together as one. $original = $query->columns; if (is_null($query->columns)) { $query->columns = ['*']; } // To compile the query, we'll spin through each component of the query and // see if that component exists. If it does we'll just call the compiler // function for the component which is responsible for making the SQL. $sql = trim($this->concatenate( $this->compileComponents($query)) ); if ($query->unions) { $sql = $this->wrapUnion($sql).' '.$this->compileUnions($query); } $query->columns = $original; return $sql; } /** * Compile the components necessary for a select clause. * * @param \Illuminate\Database\Query\Builder $query * @return array */ protected function compileComponents(Builder $query) { $sql = []; foreach ($this->selectComponents as $component) { if (isset($query->$component)) { $method = 'compile'.ucfirst($component); $sql[$component] = $this->$method($query, $query->$component); } } return $sql; } /** * Compile an aggregated select clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $aggregate * @return string */ protected function compileAggregate(Builder $query, $aggregate) { $column = $this->columnize($aggregate['columns']); // If the query has a "distinct" constraint and we're not asking for all columns // we need to prepend "distinct" onto the column name so that the query takes // it into account when it performs the aggregating operations on the data. if (is_array($query->distinct)) { $column = 'distinct '.$this->columnize($query->distinct); } elseif ($query->distinct && $column !== '*') { $column = 'distinct '.$column; } return 'select '.$aggregate['function'].'('.$column.') as aggregate'; } /** * Compile the "select *" portion of the query. * * @param \Illuminate\Database\Query\Builder $query * @param array $columns * @return string|null */ protected function compileColumns(Builder $query, $columns) { // If the query is actually performing an aggregating select, we will let that // compiler handle the building of the select clauses, as it will need some // more syntax that is best handled by that function to keep things neat. if (! is_null($query->aggregate)) { return; } if ($query->distinct) { $select = 'select distinct '; } else { $select = 'select '; } return $select.$this->columnize($columns); } /** * Compile the "from" portion of the query. * * @param \Illuminate\Database\Query\Builder $query * @param string $table * @return string */ protected function compileFrom(Builder $query, $table) { return 'from '.$this->wrapTable($table); } /** * Compile the "join" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @param array $joins * @return string */ protected function compileJoins(Builder $query, $joins) { return collect($joins)->map(function ($join) use ($query) { $table = $this->wrapTable($join->table); $nestedJoins = is_null($join->joins) ? '' : ' '.$this->compileJoins($query, $join->joins); $tableAndNestedJoins = is_null($join->joins) ? $table : '('.$table.$nestedJoins.')'; return trim("{$join->type} join {$tableAndNestedJoins} {$this->compileWheres($join)}"); })->implode(' '); } /** * Compile the "where" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @return string */ public function compileWheres(Builder $query) { // Each type of where clause has its own compiler function, which is responsible // for actually creating the where clauses SQL. This helps keep the code nice // and maintainable since each clause has a very small method that it uses. if (is_null($query->wheres)) { return ''; } // If we actually have some where clauses, we will strip off the first boolean // operator, which is added by the query builders for convenience so we can // avoid checking for the first clauses in each of the compilers methods. if (count($sql = $this->compileWheresToArray($query)) > 0) { return $this->concatenateWhereClauses($query, $sql); } return ''; } /** * Get an array of all the where clauses for the query. * * @param \Illuminate\Database\Query\Builder $query * @return array */ protected function compileWheresToArray($query) { return collect($query->wheres)->map(function ($where) use ($query) { return $where['boolean'].' '.$this->{"where{$where['type']}"}($query, $where); })->all(); } /** * Format the where clause statements into one string. * * @param \Illuminate\Database\Query\Builder $query * @param array $sql * @return string */ protected function concatenateWhereClauses($query, $sql) { $conjunction = $query instanceof JoinClause ? 'on' : 'where'; return $conjunction.' '.$this->removeLeadingBoolean(implode(' ', $sql)); } /** * Compile a raw where clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereRaw(Builder $query, $where) { return $where['sql'] instanceof Expression ? $where['sql']->getValue($this) : $where['sql']; } /** * Compile a basic where clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereBasic(Builder $query, $where) { $value = $this->parameter($where['value']); $operator = str_replace('?', '??', $where['operator']); return $this->wrap($where['column']).' '.$operator.' '.$value; } /** * Compile a bitwise operator where clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereBitwise(Builder $query, $where) { return $this->whereBasic($query, $where); } /** * Compile a "where in" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereIn(Builder $query, $where) { if (! empty($where['values'])) { return $this->wrap($where['column']).' in ('.$this->parameterize($where['values']).')'; } return '0 = 1'; } /** * Compile a "where not in" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereNotIn(Builder $query, $where) { if (! empty($where['values'])) { return $this->wrap($where['column']).' not in ('.$this->parameterize($where['values']).')'; } return '1 = 1'; } /** * Compile a "where not in raw" clause. * * For safety, whereIntegerInRaw ensures this method is only used with integer values. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereNotInRaw(Builder $query, $where) { if (! empty($where['values'])) { return $this->wrap($where['column']).' not in ('.implode(', ', $where['values']).')'; } return '1 = 1'; } /** * Compile a "where in raw" clause. * * For safety, whereIntegerInRaw ensures this method is only used with integer values. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereInRaw(Builder $query, $where) { if (! empty($where['values'])) { return $this->wrap($where['column']).' in ('.implode(', ', $where['values']).')'; } return '0 = 1'; } /** * Compile a "where null" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereNull(Builder $query, $where) { return $this->wrap($where['column']).' is null'; } /** * Compile a "where not null" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereNotNull(Builder $query, $where) { return $this->wrap($where['column']).' is not null'; } /** * Compile a "between" where clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereBetween(Builder $query, $where) { $between = $where['not'] ? 'not between' : 'between'; $min = $this->parameter(is_array($where['values']) ? reset($where['values']) : $where['values'][0]); $max = $this->parameter(is_array($where['values']) ? end($where['values']) : $where['values'][1]); return $this->wrap($where['column']).' '.$between.' '.$min.' and '.$max; } /** * Compile a "between" where clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereBetweenColumns(Builder $query, $where) { $between = $where['not'] ? 'not between' : 'between'; $min = $this->wrap(is_array($where['values']) ? reset($where['values']) : $where['values'][0]); $max = $this->wrap(is_array($where['values']) ? end($where['values']) : $where['values'][1]); return $this->wrap($where['column']).' '.$between.' '.$min.' and '.$max; } /** * Compile a "where date" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereDate(Builder $query, $where) { return $this->dateBasedWhere('date', $query, $where); } /** * Compile a "where time" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereTime(Builder $query, $where) { return $this->dateBasedWhere('time', $query, $where); } /** * Compile a "where day" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereDay(Builder $query, $where) { return $this->dateBasedWhere('day', $query, $where); } /** * Compile a "where month" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereMonth(Builder $query, $where) { return $this->dateBasedWhere('month', $query, $where); } /** * Compile a "where year" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereYear(Builder $query, $where) { return $this->dateBasedWhere('year', $query, $where); } /** * Compile a date based where clause. * * @param string $type * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function dateBasedWhere($type, Builder $query, $where) { $value = $this->parameter($where['value']); return $type.'('.$this->wrap($where['column']).') '.$where['operator'].' '.$value; } /** * Compile a where clause comparing two columns. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereColumn(Builder $query, $where) { return $this->wrap($where['first']).' '.$where['operator'].' '.$this->wrap($where['second']); } /** * Compile a nested where clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereNested(Builder $query, $where) { // Here we will calculate what portion of the string we need to remove. If this // is a join clause query, we need to remove the "on" portion of the SQL and // if it is a normal query we need to take the leading "where" of queries. $offset = $where['query'] instanceof JoinClause ? 3 : 6; return '('.substr($this->compileWheres($where['query']), $offset).')'; } /** * Compile a where condition with a sub-select. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereSub(Builder $query, $where) { $select = $this->compileSelect($where['query']); return $this->wrap($where['column']).' '.$where['operator']." ($select)"; } /** * Compile a where exists clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereExists(Builder $query, $where) { return 'exists ('.$this->compileSelect($where['query']).')'; } /** * Compile a where exists clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereNotExists(Builder $query, $where) { return 'not exists ('.$this->compileSelect($where['query']).')'; } /** * Compile a where row values condition. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereRowValues(Builder $query, $where) { $columns = $this->columnize($where['columns']); $values = $this->parameterize($where['values']); return '('.$columns.') '.$where['operator'].' ('.$values.')'; } /** * Compile a "where JSON boolean" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereJsonBoolean(Builder $query, $where) { $column = $this->wrapJsonBooleanSelector($where['column']); $value = $this->wrapJsonBooleanValue( $this->parameter($where['value']) ); return $column.' '.$where['operator'].' '.$value; } /** * Compile a "where JSON contains" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereJsonContains(Builder $query, $where) { $not = $where['not'] ? 'not ' : ''; return $not.$this->compileJsonContains( $where['column'], $this->parameter($where['value']) ); } /** * Compile a "JSON contains" statement into SQL. * * @param string $column * @param string $value * @return string * * @throws \RuntimeException */ protected function compileJsonContains($column, $value) { throw new RuntimeException('This database engine does not support JSON contains operations.'); } /** * Prepare the binding for a "JSON contains" statement. * * @param mixed $binding * @return string */ public function prepareBindingForJsonContains($binding) { return json_encode($binding, JSON_UNESCAPED_UNICODE); } /** * Compile a "where JSON contains key" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereJsonContainsKey(Builder $query, $where) { $not = $where['not'] ? 'not ' : ''; return $not.$this->compileJsonContainsKey( $where['column'] ); } /** * Compile a "JSON contains key" statement into SQL. * * @param string $column * @return string * * @throws \RuntimeException */ protected function compileJsonContainsKey($column) { throw new RuntimeException('This database engine does not support JSON contains key operations.'); } /** * Compile a "where JSON length" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereJsonLength(Builder $query, $where) { return $this->compileJsonLength( $where['column'], $where['operator'], $this->parameter($where['value']) ); } /** * Compile a "JSON length" statement into SQL. * * @param string $column * @param string $operator * @param string $value * @return string * * @throws \RuntimeException */ protected function compileJsonLength($column, $operator, $value) { throw new RuntimeException('This database engine does not support JSON length operations.'); } /** * Compile a "JSON value cast" statement into SQL. * * @param string $value * @return string */ public function compileJsonValueCast($value) { return $value; } /** * Compile a "where fulltext" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ public function whereFullText(Builder $query, $where) { throw new RuntimeException('This database engine does not support fulltext search operations.'); } /** * Compile a clause based on an expression. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ public function whereExpression(Builder $query, $where) { return $where['column']->getValue($this); } /** * Compile the "group by" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @param array $groups * @return string */ protected function compileGroups(Builder $query, $groups) { return 'group by '.$this->columnize($groups); } /** * Compile the "having" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @return string */ protected function compileHavings(Builder $query) { return 'having '.$this->removeLeadingBoolean(collect($query->havings)->map(function ($having) { return $having['boolean'].' '.$this->compileHaving($having); })->implode(' ')); } /** * Compile a single having clause. * * @param array $having * @return string */ protected function compileHaving(array $having) { // If the having clause is "raw", we can just return the clause straight away // without doing any more processing on it. Otherwise, we will compile the // clause into SQL based on the components that make it up from builder. return match ($having['type']) { 'Raw' => $having['sql'], 'between' => $this->compileHavingBetween($having), 'Null' => $this->compileHavingNull($having), 'NotNull' => $this->compileHavingNotNull($having), 'bit' => $this->compileHavingBit($having), 'Expression' => $this->compileHavingExpression($having), 'Nested' => $this->compileNestedHavings($having), default => $this->compileBasicHaving($having), }; } /** * Compile a basic having clause. * * @param array $having * @return string */ protected function compileBasicHaving($having) { $column = $this->wrap($having['column']); $parameter = $this->parameter($having['value']); return $column.' '.$having['operator'].' '.$parameter; } /** * Compile a "between" having clause. * * @param array $having * @return string */ protected function compileHavingBetween($having) { $between = $having['not'] ? 'not between' : 'between'; $column = $this->wrap($having['column']); $min = $this->parameter(head($having['values'])); $max = $this->parameter(last($having['values'])); return $column.' '.$between.' '.$min.' and '.$max; } /** * Compile a having null clause. * * @param array $having * @return string */ protected function compileHavingNull($having) { $column = $this->wrap($having['column']); return $column.' is null'; } /** * Compile a having not null clause. * * @param array $having * @return string */ protected function compileHavingNotNull($having) { $column = $this->wrap($having['column']); return $column.' is not null'; } /** * Compile a having clause involving a bit operator. * * @param array $having * @return string */ protected function compileHavingBit($having) { $column = $this->wrap($having['column']); $parameter = $this->parameter($having['value']); return '('.$column.' '.$having['operator'].' '.$parameter.') != 0'; } /** * Compile a having clause involving an expression. * * @param array $having * @return string */ protected function compileHavingExpression($having) { return $having['column']->getValue($this); } /** * Compile a nested having clause. * * @param array $having * @return string */ protected function compileNestedHavings($having) { return '('.substr($this->compileHavings($having['query']), 7).')'; } /** * Compile the "order by" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @param array $orders * @return string */ protected function compileOrders(Builder $query, $orders) { if (! empty($orders)) { return 'order by '.implode(', ', $this->compileOrdersToArray($query, $orders)); } return ''; } /** * Compile the query orders to an array. * * @param \Illuminate\Database\Query\Builder $query * @param array $orders * @return array */ protected function compileOrdersToArray(Builder $query, $orders) { return array_map(function ($order) { return $order['sql'] ?? $this->wrap($order['column']).' '.$order['direction']; }, $orders); } /** * Compile the random statement into SQL. * * @param string|int $seed * @return string */ public function compileRandom($seed) { return 'RANDOM()'; } /** * Compile the "limit" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @param int $limit * @return string */ protected function compileLimit(Builder $query, $limit) { return 'limit '.(int) $limit; } /** * Compile the "offset" portions of the query. * * @param \Illuminate\Database\Query\Builder $query * @param int $offset * @return string */ protected function compileOffset(Builder $query, $offset) { return 'offset '.(int) $offset; } /** * Compile the "union" queries attached to the main query. * * @param \Illuminate\Database\Query\Builder $query * @return string */ protected function compileUnions(Builder $query) { $sql = ''; foreach ($query->unions as $union) { $sql .= $this->compileUnion($union); } if (! empty($query->unionOrders)) { $sql .= ' '.$this->compileOrders($query, $query->unionOrders); } if (isset($query->unionLimit)) { $sql .= ' '.$this->compileLimit($query, $query->unionLimit); } if (isset($query->unionOffset)) { $sql .= ' '.$this->compileOffset($query, $query->unionOffset); } return ltrim($sql); } /** * Compile a single union statement. * * @param array $union * @return string */ protected function compileUnion(array $union) { $conjunction = $union['all'] ? ' union all ' : ' union '; return $conjunction.$this->wrapUnion($union['query']->toSql()); } /** * Wrap a union subquery in parentheses. * * @param string $sql * @return string */ protected function wrapUnion($sql) { return '('.$sql.')'; } /** * Compile a union aggregate query into SQL. * * @param \Illuminate\Database\Query\Builder $query * @return string */ protected function compileUnionAggregate(Builder $query) { $sql = $this->compileAggregate($query, $query->aggregate); $query->aggregate = null; return $sql.' from ('.$this->compileSelect($query).') as '.$this->wrapTable('temp_table'); } /** * Compile an exists statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @return string */ public function compileExists(Builder $query) { $select = $this->compileSelect($query); return "select exists({$select}) as {$this->wrap('exists')}"; } /** * Compile an insert statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ public function compileInsert(Builder $query, array $values) { // Essentially we will force every insert to be treated as a batch insert which // simply makes creating the SQL easier for us since we can utilize the same // basic routine regardless of an amount of records given to us to insert. $table = $this->wrapTable($query->from); if (empty($values)) { return "insert into {$table} default values"; } if (! is_array(reset($values))) { $values = [$values]; } $columns = $this->columnize(array_keys(reset($values))); // We need to build a list of parameter place-holders of values that are bound // to the query. Each insert should have the exact same number of parameter // bindings so we will loop through the record and parameterize them all. $parameters = collect($values)->map(function ($record) { return '('.$this->parameterize($record).')'; })->implode(', '); return "insert into $table ($columns) values $parameters"; } /** * Compile an insert ignore statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string * * @throws \RuntimeException */ public function compileInsertOrIgnore(Builder $query, array $values) { throw new RuntimeException('This database engine does not support inserting while ignoring errors.'); } /** * Compile an insert and get ID statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @param string $sequence * @return string */ public function compileInsertGetId(Builder $query, $values, $sequence) { return $this->compileInsert($query, $values); } /** * Compile an insert statement using a subquery into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $columns * @param string $sql * @return string */ public function compileInsertUsing(Builder $query, array $columns, string $sql) { $table = $this->wrapTable($query->from); if (empty($columns) || $columns === ['*']) { return "insert into {$table} $sql"; } return "insert into {$table} ({$this->columnize($columns)}) $sql"; } /** * Compile an update statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ public function compileUpdate(Builder $query, array $values) { $table = $this->wrapTable($query->from); $columns = $this->compileUpdateColumns($query, $values); $where = $this->compileWheres($query); return trim( isset($query->joins) ? $this->compileUpdateWithJoins($query, $table, $columns, $where) : $this->compileUpdateWithoutJoins($query, $table, $columns, $where) ); } /** * Compile the columns for an update statement. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ protected function compileUpdateColumns(Builder $query, array $values) { return collect($values)->map(function ($value, $key) { return $this->wrap($key).' = '.$this->parameter($value); })->implode(', '); } /** * Compile an update statement without joins into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param string $table * @param string $columns * @param string $where * @return string */ protected function compileUpdateWithoutJoins(Builder $query, $table, $columns, $where) { return "update {$table} set {$columns} {$where}"; } /** * Compile an update statement with joins into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param string $table * @param string $columns * @param string $where * @return string */ protected function compileUpdateWithJoins(Builder $query, $table, $columns, $where) { $joins = $this->compileJoins($query, $query->joins); return "update {$table} {$joins} set {$columns} {$where}"; } /** * Compile an "upsert" statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @param array $uniqueBy * @param array $update * @return string * * @throws \RuntimeException */ public function compileUpsert(Builder $query, array $values, array $uniqueBy, array $update) { throw new RuntimeException('This database engine does not support upserts.'); } /** * Prepare the bindings for an update statement. * * @param array $bindings * @param array $values * @return array */ public function prepareBindingsForUpdate(array $bindings, array $values) { $cleanBindings = Arr::except($bindings, ['select', 'join']); return array_values( array_merge($bindings['join'], $values, Arr::flatten($cleanBindings)) ); } /** * Compile a delete statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @return string */ public function compileDelete(Builder $query) { $table = $this->wrapTable($query->from); $where = $this->compileWheres($query); return trim( isset($query->joins) ? $this->compileDeleteWithJoins($query, $table, $where) : $this->compileDeleteWithoutJoins($query, $table, $where) ); } /** * Compile a delete statement without joins into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param string $table * @param string $where * @return string */ protected function compileDeleteWithoutJoins(Builder $query, $table, $where) { return "delete from {$table} {$where}"; } /** * Compile a delete statement with joins into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param string $table * @param string $where * @return string */ protected function compileDeleteWithJoins(Builder $query, $table, $where) { $alias = last(explode(' as ', $table)); $joins = $this->compileJoins($query, $query->joins); return "delete {$alias} from {$table} {$joins} {$where}"; } /** * Prepare the bindings for a delete statement. * * @param array $bindings * @return array */ public function prepareBindingsForDelete(array $bindings) { return Arr::flatten( Arr::except($bindings, 'select') ); } /** * Compile a truncate table statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @return array */ public function compileTruncate(Builder $query) { return ['truncate table '.$this->wrapTable($query->from) => []]; } /** * Compile the lock into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param bool|string $value * @return string */ protected function compileLock(Builder $query, $value) { return is_string($value) ? $value : ''; } /** * Determine if the grammar supports savepoints. * * @return bool */ public function supportsSavepoints() { return true; } /** * Compile the SQL statement to define a savepoint. * * @param string $name * @return string */ public function compileSavepoint($name) { return 'SAVEPOINT '.$name; } /** * Compile the SQL statement to execute a savepoint rollback. * * @param string $name * @return string */ public function compileSavepointRollBack($name) { return 'ROLLBACK TO SAVEPOINT '.$name; } /** * Wrap the given JSON selector for boolean values. * * @param string $value * @return string */ protected function wrapJsonBooleanSelector($value) { return $this->wrapJsonSelector($value); } /** * Wrap the given JSON boolean value. * * @param string $value * @return string */ protected function wrapJsonBooleanValue($value) { return $value; } /** * Concatenate an array of segments, removing empties. * * @param array $segments * @return string */ protected function concatenate($segments) { return implode(' ', array_filter($segments, function ($value) { return (string) $value !== ''; })); } /** * Remove the leading boolean from a statement. * * @param string $value * @return string */ protected function removeLeadingBoolean($value) { return preg_replace('/and |or /i', '', $value, 1); } /** * Substitute the given bindings into the given raw SQL query. * * @param string $sql * @param array $bindings * @return string */ public function substituteBindingsIntoRawSql($sql, $bindings) { $bindings = array_map(fn ($value) => $this->escape($value), $bindings); $query = ''; $isStringLiteral = false; for ($i = 0; $i < strlen($sql); $i++) { $char = $sql[$i]; $nextChar = $sql[$i + 1] ?? null; // Single quotes can be escaped as '' according to the SQL standard while // MySQL uses \'. Postgres has operators like ?| that must get encoded // in PHP like ??|. We should skip over the escaped characters here. if (in_array($char.$nextChar, ["\'", "''", '??'])) { $query .= $char.$nextChar; $i += 1; } elseif ($char === "'") { // Starting / leaving string literal... $query .= $char; $isStringLiteral = ! $isStringLiteral; } elseif ($char === '?' && ! $isStringLiteral) { // Substitutable binding... $query .= array_shift($bindings) ?? '?'; } else { // Normal character... $query .= $char; } } return $query; } /** * Get the grammar specific operators. * * @return array */ public function getOperators() { return $this->operators; } /** * Get the grammar specific bitwise operators. * * @return array */ public function getBitwiseOperators() { return $this->bitwiseOperators; } } src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php 0000644 00000023722 15107327037 0017214 0 ustar 00 <?php namespace Illuminate\Database\Query\Grammars; use Illuminate\Database\Query\Builder; use Illuminate\Support\Arr; use Illuminate\Support\Str; class SQLiteGrammar extends Grammar { /** * All of the available clause operators. * * @var string[] */ protected $operators = [ '=', '<', '>', '<=', '>=', '<>', '!=', 'like', 'not like', 'ilike', '&', '|', '<<', '>>', ]; /** * Compile the lock into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param bool|string $value * @return string */ protected function compileLock(Builder $query, $value) { return ''; } /** * Wrap a union subquery in parentheses. * * @param string $sql * @return string */ protected function wrapUnion($sql) { return 'select * from ('.$sql.')'; } /** * Compile a "where date" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereDate(Builder $query, $where) { return $this->dateBasedWhere('%Y-%m-%d', $query, $where); } /** * Compile a "where day" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereDay(Builder $query, $where) { return $this->dateBasedWhere('%d', $query, $where); } /** * Compile a "where month" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereMonth(Builder $query, $where) { return $this->dateBasedWhere('%m', $query, $where); } /** * Compile a "where year" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereYear(Builder $query, $where) { return $this->dateBasedWhere('%Y', $query, $where); } /** * Compile a "where time" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereTime(Builder $query, $where) { return $this->dateBasedWhere('%H:%M:%S', $query, $where); } /** * Compile a date based where clause. * * @param string $type * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function dateBasedWhere($type, Builder $query, $where) { $value = $this->parameter($where['value']); return "strftime('{$type}', {$this->wrap($where['column'])}) {$where['operator']} cast({$value} as text)"; } /** * Compile the index hints for the query. * * @param \Illuminate\Database\Query\Builder $query * @param \Illuminate\Database\Query\IndexHint $indexHint * @return string */ protected function compileIndexHint(Builder $query, $indexHint) { return $indexHint->type === 'force' ? "indexed by {$indexHint->index}" : ''; } /** * Compile a "JSON length" statement into SQL. * * @param string $column * @param string $operator * @param string $value * @return string */ protected function compileJsonLength($column, $operator, $value) { [$field, $path] = $this->wrapJsonFieldAndPath($column); return 'json_array_length('.$field.$path.') '.$operator.' '.$value; } /** * Compile a "JSON contains key" statement into SQL. * * @param string $column * @return string */ protected function compileJsonContainsKey($column) { [$field, $path] = $this->wrapJsonFieldAndPath($column); return 'json_type('.$field.$path.') is not null'; } /** * Compile an update statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ public function compileUpdate(Builder $query, array $values) { if (isset($query->joins) || isset($query->limit)) { return $this->compileUpdateWithJoinsOrLimit($query, $values); } return parent::compileUpdate($query, $values); } /** * Compile an insert ignore statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ public function compileInsertOrIgnore(Builder $query, array $values) { return Str::replaceFirst('insert', 'insert or ignore', $this->compileInsert($query, $values)); } /** * Compile the columns for an update statement. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ protected function compileUpdateColumns(Builder $query, array $values) { $jsonGroups = $this->groupJsonColumnsForUpdate($values); return collect($values)->reject(function ($value, $key) { return $this->isJsonSelector($key); })->merge($jsonGroups)->map(function ($value, $key) use ($jsonGroups) { $column = last(explode('.', $key)); $value = isset($jsonGroups[$key]) ? $this->compileJsonPatch($column, $value) : $this->parameter($value); return $this->wrap($column).' = '.$value; })->implode(', '); } /** * Compile an "upsert" statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @param array $uniqueBy * @param array $update * @return string */ public function compileUpsert(Builder $query, array $values, array $uniqueBy, array $update) { $sql = $this->compileInsert($query, $values); $sql .= ' on conflict ('.$this->columnize($uniqueBy).') do update set '; $columns = collect($update)->map(function ($value, $key) { return is_numeric($key) ? $this->wrap($value).' = '.$this->wrapValue('excluded').'.'.$this->wrap($value) : $this->wrap($key).' = '.$this->parameter($value); })->implode(', '); return $sql.$columns; } /** * Group the nested JSON columns. * * @param array $values * @return array */ protected function groupJsonColumnsForUpdate(array $values) { $groups = []; foreach ($values as $key => $value) { if ($this->isJsonSelector($key)) { Arr::set($groups, str_replace('->', '.', Str::after($key, '.')), $value); } } return $groups; } /** * Compile a "JSON" patch statement into SQL. * * @param string $column * @param mixed $value * @return string */ protected function compileJsonPatch($column, $value) { return "json_patch(ifnull({$this->wrap($column)}, json('{}')), json({$this->parameter($value)}))"; } /** * Compile an update statement with joins or limit into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @return string */ protected function compileUpdateWithJoinsOrLimit(Builder $query, array $values) { $table = $this->wrapTable($query->from); $columns = $this->compileUpdateColumns($query, $values); $alias = last(preg_split('/\s+as\s+/i', $query->from)); $selectSql = $this->compileSelect($query->select($alias.'.rowid')); return "update {$table} set {$columns} where {$this->wrap('rowid')} in ({$selectSql})"; } /** * Prepare the bindings for an update statement. * * @param array $bindings * @param array $values * @return array */ public function prepareBindingsForUpdate(array $bindings, array $values) { $groups = $this->groupJsonColumnsForUpdate($values); $values = collect($values)->reject(function ($value, $key) { return $this->isJsonSelector($key); })->merge($groups)->map(function ($value) { return is_array($value) ? json_encode($value) : $value; })->all(); $cleanBindings = Arr::except($bindings, 'select'); return array_values( array_merge($values, Arr::flatten($cleanBindings)) ); } /** * Compile a delete statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @return string */ public function compileDelete(Builder $query) { if (isset($query->joins) || isset($query->limit)) { return $this->compileDeleteWithJoinsOrLimit($query); } return parent::compileDelete($query); } /** * Compile a delete statement with joins or limit into SQL. * * @param \Illuminate\Database\Query\Builder $query * @return string */ protected function compileDeleteWithJoinsOrLimit(Builder $query) { $table = $this->wrapTable($query->from); $alias = last(preg_split('/\s+as\s+/i', $query->from)); $selectSql = $this->compileSelect($query->select($alias.'.rowid')); return "delete from {$table} where {$this->wrap('rowid')} in ({$selectSql})"; } /** * Compile a truncate table statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @return array */ public function compileTruncate(Builder $query) { return [ 'delete from sqlite_sequence where name = ?' => [$query->from], 'delete from '.$this->wrapTable($query->from) => [], ]; } /** * Wrap the given JSON selector. * * @param string $value * @return string */ protected function wrapJsonSelector($value) { [$field, $path] = $this->wrapJsonFieldAndPath($value); return 'json_extract('.$field.$path.')'; } } src/Illuminate/Database/Query/JoinClause.php 0000644 00000007224 15107327037 0015026 0 ustar 00 <?php namespace Illuminate\Database\Query; use Closure; class JoinClause extends Builder { /** * The type of join being performed. * * @var string */ public $type; /** * The table the join clause is joining to. * * @var string */ public $table; /** * The connection of the parent query builder. * * @var \Illuminate\Database\ConnectionInterface */ protected $parentConnection; /** * The grammar of the parent query builder. * * @var \Illuminate\Database\Query\Grammars\Grammar */ protected $parentGrammar; /** * The processor of the parent query builder. * * @var \Illuminate\Database\Query\Processors\Processor */ protected $parentProcessor; /** * The class name of the parent query builder. * * @var string */ protected $parentClass; /** * Create a new join clause instance. * * @param \Illuminate\Database\Query\Builder $parentQuery * @param string $type * @param string $table * @return void */ public function __construct(Builder $parentQuery, $type, $table) { $this->type = $type; $this->table = $table; $this->parentClass = get_class($parentQuery); $this->parentGrammar = $parentQuery->getGrammar(); $this->parentProcessor = $parentQuery->getProcessor(); $this->parentConnection = $parentQuery->getConnection(); parent::__construct( $this->parentConnection, $this->parentGrammar, $this->parentProcessor ); } /** * Add an "on" clause to the join. * * On clauses can be chained, e.g. * * $join->on('contacts.user_id', '=', 'users.id') * ->on('contacts.info_id', '=', 'info.id') * * will produce the following SQL: * * on `contacts`.`user_id` = `users`.`id` and `contacts`.`info_id` = `info`.`id` * * @param \Closure|string $first * @param string|null $operator * @param \Illuminate\Contracts\Database\Query\Expression|string|null $second * @param string $boolean * @return $this * * @throws \InvalidArgumentException */ public function on($first, $operator = null, $second = null, $boolean = 'and') { if ($first instanceof Closure) { return $this->whereNested($first, $boolean); } return $this->whereColumn($first, $operator, $second, $boolean); } /** * Add an "or on" clause to the join. * * @param \Closure|string $first * @param string|null $operator * @param \Illuminate\Contracts\Database\Query\Expression|string|null $second * @return \Illuminate\Database\Query\JoinClause */ public function orOn($first, $operator = null, $second = null) { return $this->on($first, $operator, $second, 'or'); } /** * Get a new instance of the join clause builder. * * @return \Illuminate\Database\Query\JoinClause */ public function newQuery() { return new static($this->newParentQuery(), $this->type, $this->table); } /** * Create a new query instance for sub-query. * * @return \Illuminate\Database\Query\Builder */ protected function forSubQuery() { return $this->newParentQuery()->newQuery(); } /** * Create a new parent query instance. * * @return \Illuminate\Database\Query\Builder */ protected function newParentQuery() { $class = $this->parentClass; return new $class($this->parentConnection, $this->parentGrammar, $this->parentProcessor); } } src/Illuminate/Database/Query/Processors/Processor.php 0000644 00000002601 15107327037 0017105 0 ustar 00 <?php namespace Illuminate\Database\Query\Processors; use Illuminate\Database\Query\Builder; class Processor { /** * Process the results of a "select" query. * * @param \Illuminate\Database\Query\Builder $query * @param array $results * @return array */ public function processSelect(Builder $query, $results) { return $results; } /** * Process an "insert get ID" query. * * @param \Illuminate\Database\Query\Builder $query * @param string $sql * @param array $values * @param string|null $sequence * @return int */ public function processInsertGetId(Builder $query, $sql, $values, $sequence = null) { $query->getConnection()->insert($sql, $values); $id = $query->getConnection()->getPdo()->lastInsertId($sequence); return is_numeric($id) ? (int) $id : $id; } /** * Process the results of a column listing query. * * @deprecated Will be removed in a future Laravel version. * * @param array $results * @return array */ public function processColumnListing($results) { return $results; } /** * Process the results of a columns query. * * @param array $results * @return array */ public function processColumns($results) { return $results; } } src/Illuminate/Database/Query/Processors/PostgresProcessor.php 0000644 00000004113 15107327037 0020634 0 ustar 00 <?php namespace Illuminate\Database\Query\Processors; use Illuminate\Database\Query\Builder; class PostgresProcessor extends Processor { /** * Process an "insert get ID" query. * * @param \Illuminate\Database\Query\Builder $query * @param string $sql * @param array $values * @param string|null $sequence * @return int */ public function processInsertGetId(Builder $query, $sql, $values, $sequence = null) { $connection = $query->getConnection(); $connection->recordsHaveBeenModified(); $result = $connection->selectFromWriteConnection($sql, $values)[0]; $sequence = $sequence ?: 'id'; $id = is_object($result) ? $result->{$sequence} : $result[$sequence]; return is_numeric($id) ? (int) $id : $id; } /** * Process the results of a column listing query. * * @deprecated Will be removed in a future Laravel version. * * @param array $results * @return array */ public function processColumnListing($results) { return array_map(function ($result) { return ((object) $result)->column_name; }, $results); } /** * Process the results of a columns query. * * @param array $results * @return array */ public function processColumns($results) { return array_map(function ($result) { $result = (object) $result; $autoincrement = $result->default !== null && str_starts_with($result->default, 'nextval('); return [ 'name' => str_starts_with($result->name, '"') ? str_replace('"', '', $result->name) : $result->name, 'type_name' => $result->type_name, 'type' => $result->type, 'collation' => $result->collation, 'nullable' => (bool) $result->nullable, 'default' => $autoincrement ? null : $result->default, 'auto_increment' => $autoincrement, 'comment' => $result->comment, ]; }, $results); } } src/Illuminate/Database/Query/Processors/SQLiteProcessor.php 0000644 00000002477 15107327037 0020202 0 ustar 00 <?php namespace Illuminate\Database\Query\Processors; class SQLiteProcessor extends Processor { /** * Process the results of a column listing query. * * @deprecated Will be removed in a future Laravel version. * * @param array $results * @return array */ public function processColumnListing($results) { return array_map(function ($result) { return ((object) $result)->name; }, $results); } /** * Process the results of a columns query. * * @param array $results * @return array */ public function processColumns($results) { $hasPrimaryKey = array_sum(array_column($results, 'primary')) === 1; return array_map(function ($result) use ($hasPrimaryKey) { $result = (object) $result; $type = strtolower($result->type); return [ 'name' => $result->name, 'type_name' => strtok($type, '('), 'type' => $type, 'collation' => null, 'nullable' => (bool) $result->nullable, 'default' => $result->default, 'auto_increment' => $hasPrimaryKey && $result->primary && $type === 'integer', 'comment' => null, ]; }, $results); } } src/Illuminate/Database/Query/Processors/SqlServerProcessor.php 0000644 00000005777 15107327037 0020775 0 ustar 00 <?php namespace Illuminate\Database\Query\Processors; use Exception; use Illuminate\Database\Connection; use Illuminate\Database\Query\Builder; class SqlServerProcessor extends Processor { /** * Process an "insert get ID" query. * * @param \Illuminate\Database\Query\Builder $query * @param string $sql * @param array $values * @param string|null $sequence * @return int */ public function processInsertGetId(Builder $query, $sql, $values, $sequence = null) { $connection = $query->getConnection(); $connection->insert($sql, $values); if ($connection->getConfig('odbc') === true) { $id = $this->processInsertGetIdForOdbc($connection); } else { $id = $connection->getPdo()->lastInsertId(); } return is_numeric($id) ? (int) $id : $id; } /** * Process an "insert get ID" query for ODBC. * * @param \Illuminate\Database\Connection $connection * @return int * * @throws \Exception */ protected function processInsertGetIdForOdbc(Connection $connection) { $result = $connection->selectFromWriteConnection( 'SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS int) AS insertid' ); if (! $result) { throw new Exception('Unable to retrieve lastInsertID for ODBC.'); } $row = $result[0]; return is_object($row) ? $row->insertid : $row['insertid']; } /** * Process the results of a column listing query. * * @deprecated Will be removed in a future Laravel version. * * @param array $results * @return array */ public function processColumnListing($results) { return array_map(function ($result) { return ((object) $result)->name; }, $results); } /** * Process the results of a columns query. * * @param array $results * @return array */ public function processColumns($results) { return array_map(function ($result) { $result = (object) $result; $type = match ($typeName = $result->type_name) { 'binary', 'varbinary', 'char', 'varchar', 'nchar', 'nvarchar' => $result->length == -1 ? $typeName.'(max)' : $typeName."($result->length)", 'decimal', 'numeric' => $typeName."($result->precision,$result->places)", 'float', 'datetime2', 'datetimeoffset', 'time' => $typeName."($result->precision)", default => $typeName, }; return [ 'name' => $result->name, 'type_name' => $result->type_name, 'type' => $type, 'collation' => $result->collation, 'nullable' => (bool) $result->nullable, 'default' => $result->default, 'auto_increment' => (bool) $result->autoincrement, 'comment' => $result->comment, ]; }, $results); } } src/Illuminate/Database/Query/Processors/MySqlProcessor.php 0000644 00000002301 15107327037 0020070 0 ustar 00 <?php namespace Illuminate\Database\Query\Processors; class MySqlProcessor extends Processor { /** * Process the results of a column listing query. * * @deprecated Will be removed in a future Laravel version. * * @param array $results * @return array */ public function processColumnListing($results) { return array_map(function ($result) { return ((object) $result)->column_name; }, $results); } /** * Process the results of a columns query. * * @param array $results * @return array */ public function processColumns($results) { return array_map(function ($result) { $result = (object) $result; return [ 'name' => $result->name, 'type_name' => $result->type_name, 'type' => $result->type, 'collation' => $result->collation, 'nullable' => $result->nullable === 'YES', 'default' => $result->default, 'auto_increment' => $result->extra === 'auto_increment', 'comment' => $result->comment, ]; }, $results); } } src/Illuminate/Database/Query/Builder.php 0000644 00000336371 15107327037 0014370 0 ustar 00 <?php namespace Illuminate\Database\Query; use BackedEnum; use Carbon\CarbonPeriod; use Closure; use DateTimeInterface; use Illuminate\Contracts\Database\Query\Builder as BuilderContract; use Illuminate\Contracts\Database\Query\ConditionExpression; use Illuminate\Contracts\Database\Query\Expression as ExpressionContract; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Concerns\BuildsQueries; use Illuminate\Database\Concerns\ExplainsQueries; use Illuminate\Database\ConnectionInterface; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Query\Grammars\Grammar; use Illuminate\Database\Query\Processors\Processor; use Illuminate\Pagination\Paginator; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\LazyCollection; use Illuminate\Support\Str; use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; use LogicException; use RuntimeException; class Builder implements BuilderContract { use BuildsQueries, ExplainsQueries, ForwardsCalls, Macroable { __call as macroCall; } /** * The database connection instance. * * @var \Illuminate\Database\ConnectionInterface */ public $connection; /** * The database query grammar instance. * * @var \Illuminate\Database\Query\Grammars\Grammar */ public $grammar; /** * The database query post processor instance. * * @var \Illuminate\Database\Query\Processors\Processor */ public $processor; /** * The current query value bindings. * * @var array */ public $bindings = [ 'select' => [], 'from' => [], 'join' => [], 'where' => [], 'groupBy' => [], 'having' => [], 'order' => [], 'union' => [], 'unionOrder' => [], ]; /** * An aggregate function and column to be run. * * @var array */ public $aggregate; /** * The columns that should be returned. * * @var array */ public $columns; /** * Indicates if the query returns distinct results. * * Occasionally contains the columns that should be distinct. * * @var bool|array */ public $distinct = false; /** * The table which the query is targeting. * * @var string */ public $from; /** * The index hint for the query. * * @var \Illuminate\Database\Query\IndexHint */ public $indexHint; /** * The table joins for the query. * * @var array */ public $joins; /** * The where constraints for the query. * * @var array */ public $wheres = []; /** * The groupings for the query. * * @var array */ public $groups; /** * The having constraints for the query. * * @var array */ public $havings; /** * The orderings for the query. * * @var array */ public $orders; /** * The maximum number of records to return. * * @var int */ public $limit; /** * The number of records to skip. * * @var int */ public $offset; /** * The query union statements. * * @var array */ public $unions; /** * The maximum number of union records to return. * * @var int */ public $unionLimit; /** * The number of union records to skip. * * @var int */ public $unionOffset; /** * The orderings for the union query. * * @var array */ public $unionOrders; /** * Indicates whether row locking is being used. * * @var string|bool */ public $lock; /** * The callbacks that should be invoked before the query is executed. * * @var array */ public $beforeQueryCallbacks = []; /** * All of the available clause operators. * * @var string[] */ public $operators = [ '=', '<', '>', '<=', '>=', '<>', '!=', '<=>', 'like', 'like binary', 'not like', 'ilike', '&', '|', '^', '<<', '>>', '&~', 'is', 'is not', 'rlike', 'not rlike', 'regexp', 'not regexp', '~', '~*', '!~', '!~*', 'similar to', 'not similar to', 'not ilike', '~~*', '!~~*', ]; /** * All of the available bitwise operators. * * @var string[] */ public $bitwiseOperators = [ '&', '|', '^', '<<', '>>', '&~', ]; /** * Whether to use write pdo for the select. * * @var bool */ public $useWritePdo = false; /** * Create a new query builder instance. * * @param \Illuminate\Database\ConnectionInterface $connection * @param \Illuminate\Database\Query\Grammars\Grammar|null $grammar * @param \Illuminate\Database\Query\Processors\Processor|null $processor * @return void */ public function __construct(ConnectionInterface $connection, Grammar $grammar = null, Processor $processor = null) { $this->connection = $connection; $this->grammar = $grammar ?: $connection->getQueryGrammar(); $this->processor = $processor ?: $connection->getPostProcessor(); } /** * Set the columns to be selected. * * @param array|mixed $columns * @return $this */ public function select($columns = ['*']) { $this->columns = []; $this->bindings['select'] = []; $columns = is_array($columns) ? $columns : func_get_args(); foreach ($columns as $as => $column) { if (is_string($as) && $this->isQueryable($column)) { $this->selectSub($column, $as); } else { $this->columns[] = $column; } } return $this; } /** * Add a subselect expression to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query * @param string $as * @return $this * * @throws \InvalidArgumentException */ public function selectSub($query, $as) { [$query, $bindings] = $this->createSub($query); return $this->selectRaw( '('.$query.') as '.$this->grammar->wrap($as), $bindings ); } /** * Add a new "raw" select expression to the query. * * @param string $expression * @param array $bindings * @return $this */ public function selectRaw($expression, array $bindings = []) { $this->addSelect(new Expression($expression)); if ($bindings) { $this->addBinding($bindings, 'select'); } return $this; } /** * Makes "from" fetch from a subquery. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query * @param string $as * @return $this * * @throws \InvalidArgumentException */ public function fromSub($query, $as) { [$query, $bindings] = $this->createSub($query); return $this->fromRaw('('.$query.') as '.$this->grammar->wrapTable($as), $bindings); } /** * Add a raw from clause to the query. * * @param string $expression * @param mixed $bindings * @return $this */ public function fromRaw($expression, $bindings = []) { $this->from = new Expression($expression); $this->addBinding($bindings, 'from'); return $this; } /** * Creates a subquery and parse it. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query * @return array */ protected function createSub($query) { // If the given query is a Closure, we will execute it while passing in a new // query instance to the Closure. This will give the developer a chance to // format and work with the query before we cast it to a raw SQL string. if ($query instanceof Closure) { $callback = $query; $callback($query = $this->forSubQuery()); } return $this->parseSub($query); } /** * Parse the subquery into SQL and bindings. * * @param mixed $query * @return array * * @throws \InvalidArgumentException */ protected function parseSub($query) { if ($query instanceof self || $query instanceof EloquentBuilder || $query instanceof Relation) { $query = $this->prependDatabaseNameIfCrossDatabaseQuery($query); return [$query->toSql(), $query->getBindings()]; } elseif (is_string($query)) { return [$query, []]; } else { throw new InvalidArgumentException( 'A subquery must be a query builder instance, a Closure, or a string.' ); } } /** * Prepend the database name if the given query is on another database. * * @param mixed $query * @return mixed */ protected function prependDatabaseNameIfCrossDatabaseQuery($query) { if ($query->getConnection()->getDatabaseName() !== $this->getConnection()->getDatabaseName()) { $databaseName = $query->getConnection()->getDatabaseName(); if (! str_starts_with($query->from, $databaseName) && ! str_contains($query->from, '.')) { $query->from($databaseName.'.'.$query->from); } } return $query; } /** * Add a new select column to the query. * * @param array|mixed $column * @return $this */ public function addSelect($column) { $columns = is_array($column) ? $column : func_get_args(); foreach ($columns as $as => $column) { if (is_string($as) && $this->isQueryable($column)) { if (is_null($this->columns)) { $this->select($this->from.'.*'); } $this->selectSub($column, $as); } else { if (is_array($this->columns) && in_array($column, $this->columns, true)) { continue; } $this->columns[] = $column; } } return $this; } /** * Force the query to only return distinct results. * * @return $this */ public function distinct() { $columns = func_get_args(); if (count($columns) > 0) { $this->distinct = is_array($columns[0]) || is_bool($columns[0]) ? $columns[0] : $columns; } else { $this->distinct = true; } return $this; } /** * Set the table which the query is targeting. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $table * @param string|null $as * @return $this */ public function from($table, $as = null) { if ($this->isQueryable($table)) { return $this->fromSub($table, $as); } $this->from = $as ? "{$table} as {$as}" : $table; return $this; } /** * Add an index hint to suggest a query index. * * @param string $index * @return $this */ public function useIndex($index) { $this->indexHint = new IndexHint('hint', $index); return $this; } /** * Add an index hint to force a query index. * * @param string $index * @return $this */ public function forceIndex($index) { $this->indexHint = new IndexHint('force', $index); return $this; } /** * Add an index hint to ignore a query index. * * @param string $index * @return $this */ public function ignoreIndex($index) { $this->indexHint = new IndexHint('ignore', $index); return $this; } /** * Add a join clause to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $table * @param \Closure|string $first * @param string|null $operator * @param \Illuminate\Contracts\Database\Query\Expression|string|null $second * @param string $type * @param bool $where * @return $this */ public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false) { $join = $this->newJoinClause($this, $type, $table); // If the first "column" of the join is really a Closure instance the developer // is trying to build a join with a complex "on" clause containing more than // one condition, so we'll add the join and call a Closure with the query. if ($first instanceof Closure) { $first($join); $this->joins[] = $join; $this->addBinding($join->getBindings(), 'join'); } // If the column is simply a string, we can assume the join simply has a basic // "on" clause with a single condition. So we will just build the join with // this simple join clauses attached to it. There is not a join callback. else { $method = $where ? 'where' : 'on'; $this->joins[] = $join->$method($first, $operator, $second); $this->addBinding($join->getBindings(), 'join'); } return $this; } /** * Add a "join where" clause to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $table * @param \Closure|string $first * @param string $operator * @param \Illuminate\Contracts\Database\Query\Expression|string $second * @param string $type * @return $this */ public function joinWhere($table, $first, $operator, $second, $type = 'inner') { return $this->join($table, $first, $operator, $second, $type, true); } /** * Add a subquery join clause to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query * @param string $as * @param \Closure|string $first * @param string|null $operator * @param \Illuminate\Contracts\Database\Query\Expression|string|null $second * @param string $type * @param bool $where * @return $this * * @throws \InvalidArgumentException */ public function joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false) { [$query, $bindings] = $this->createSub($query); $expression = '('.$query.') as '.$this->grammar->wrapTable($as); $this->addBinding($bindings, 'join'); return $this->join(new Expression($expression), $first, $operator, $second, $type, $where); } /** * Add a left join to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $table * @param \Closure|string $first * @param string|null $operator * @param \Illuminate\Contracts\Database\Query\Expression|string|null $second * @return $this */ public function leftJoin($table, $first, $operator = null, $second = null) { return $this->join($table, $first, $operator, $second, 'left'); } /** * Add a "join where" clause to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $table * @param \Closure|string $first * @param string $operator * @param \Illuminate\Contracts\Database\Query\Expression|string|null $second * @return $this */ public function leftJoinWhere($table, $first, $operator, $second) { return $this->joinWhere($table, $first, $operator, $second, 'left'); } /** * Add a subquery left join to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query * @param string $as * @param \Closure|string $first * @param string|null $operator * @param \Illuminate\Contracts\Database\Query\Expression|string|null $second * @return $this */ public function leftJoinSub($query, $as, $first, $operator = null, $second = null) { return $this->joinSub($query, $as, $first, $operator, $second, 'left'); } /** * Add a right join to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $table * @param \Closure|string $first * @param string|null $operator * @param \Illuminate\Contracts\Database\Query\Expression|string|null $second * @return $this */ public function rightJoin($table, $first, $operator = null, $second = null) { return $this->join($table, $first, $operator, $second, 'right'); } /** * Add a "right join where" clause to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $table * @param \Closure|string $first * @param string $operator * @param \Illuminate\Contracts\Database\Query\Expression|string $second * @return $this */ public function rightJoinWhere($table, $first, $operator, $second) { return $this->joinWhere($table, $first, $operator, $second, 'right'); } /** * Add a subquery right join to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query * @param string $as * @param \Closure|string $first * @param string|null $operator * @param \Illuminate\Contracts\Database\Query\Expression|string|null $second * @return $this */ public function rightJoinSub($query, $as, $first, $operator = null, $second = null) { return $this->joinSub($query, $as, $first, $operator, $second, 'right'); } /** * Add a "cross join" clause to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $table * @param \Closure|string|null $first * @param string|null $operator * @param \Illuminate\Contracts\Database\Query\Expression|string|null $second * @return $this */ public function crossJoin($table, $first = null, $operator = null, $second = null) { if ($first) { return $this->join($table, $first, $operator, $second, 'cross'); } $this->joins[] = $this->newJoinClause($this, 'cross', $table); return $this; } /** * Add a subquery cross join to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\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. * * @param \Illuminate\Database\Query\Builder $parentQuery * @param string $type * @param string $table * @return \Illuminate\Database\Query\JoinClause */ protected function newJoinClause(self $parentQuery, $type, $table) { return new JoinClause($parentQuery, $type, $table); } /** * Merge an array of where clauses and bindings. * * @param array $wheres * @param array $bindings * @return $this */ public function mergeWheres($wheres, $bindings) { $this->wheres = array_merge($this->wheres, (array) $wheres); $this->bindings['where'] = array_values( array_merge($this->bindings['where'], (array) $bindings) ); return $this; } /** * Add a basic where clause to the query. * * @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column * @param mixed $operator * @param mixed $value * @param string $boolean * @return $this */ public function where($column, $operator = null, $value = null, $boolean = 'and') { if ($column instanceof ConditionExpression) { $type = 'Expression'; $this->wheres[] = compact('type', 'column', 'boolean'); return $this; } // If the column is an array, we will assume it is an array of key-value pairs // and can add them each as a where clause. We will maintain the boolean we // received when the method was called and pass it into the nested where. if (is_array($column)) { return $this->addArrayOfWheres($column, $boolean); } // Here we will make some assumptions about the operator. If only 2 values are // passed to the method, we will assume that the operator is an equals sign // and keep going. Otherwise, we'll require the operator to be passed in. [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); // If the column is actually a Closure instance, we will assume the developer // wants to begin a nested where statement which is wrapped in parentheses. // We will add that Closure to the query and return back out immediately. if ($column instanceof Closure && is_null($operator)) { return $this->whereNested($column, $boolean); } // If the column is a Closure instance and there is an operator value, we will // assume the developer wants to run a subquery and then compare the result // of that subquery with the given value that was provided to the method. if ($this->isQueryable($column) && ! is_null($operator)) { [$sub, $bindings] = $this->createSub($column); return $this->addBinding($bindings, 'where') ->where(new Expression('('.$sub.')'), $operator, $value, $boolean); } // If the given operator is not found in the list of valid operators we will // assume that the developer is just short-cutting the '=' operators and // we will set the operators to '=' and set the values appropriately. if ($this->invalidOperator($operator)) { [$value, $operator] = [$operator, '=']; } // If the value is a Closure, it means the developer is performing an entire // sub-select within the query and we will need to compile the sub-select // within the where clause to get the appropriate query record results. if ($this->isQueryable($value)) { return $this->whereSub($column, $operator, $value, $boolean); } // If the value is "null", we will just assume the developer wants to add a // where null clause to the query. So, we will allow a short-cut here to // that method for convenience so the developer doesn't have to check. if (is_null($value)) { return $this->whereNull($column, $boolean, $operator !== '='); } $type = 'Basic'; $columnString = ($column instanceof ExpressionContract) ? $this->grammar->getValue($column) : $column; // If the column is making a JSON reference we'll check to see if the value // is a boolean. If it is, we'll add the raw boolean string as an actual // value to the query to ensure this is properly handled by the query. if (str_contains($columnString, '->') && is_bool($value)) { $value = new Expression($value ? 'true' : 'false'); if (is_string($column)) { $type = 'JsonBoolean'; } } if ($this->isBitwiseOperator($operator)) { $type = 'Bitwise'; } // Now that we are working with just a simple query we can put the elements // in our array and add the query binding to our array of bindings that // will be bound to each SQL statements when it is finally executed. $this->wheres[] = compact( 'type', 'column', 'operator', 'value', 'boolean' ); if (! $value instanceof ExpressionContract) { $this->addBinding($this->flattenValue($value), 'where'); } return $this; } /** * Add an array of where clauses to the query. * * @param array $column * @param string $boolean * @param string $method * @return $this */ protected function addArrayOfWheres($column, $boolean, $method = 'where') { return $this->whereNested(function ($query) use ($column, $method, $boolean) { foreach ($column as $key => $value) { if (is_numeric($key) && is_array($value)) { $query->{$method}(...array_values($value)); } else { $query->{$method}($key, '=', $value, $boolean); } } }, $boolean); } /** * Prepare the value and operator for a where clause. * * @param string $value * @param string $operator * @param bool $useDefault * @return array * * @throws \InvalidArgumentException */ public function prepareValueAndOperator($value, $operator, $useDefault = false) { if ($useDefault) { return [$operator, '=']; } elseif ($this->invalidOperatorAndValue($operator, $value)) { throw new InvalidArgumentException('Illegal operator and value combination.'); } return [$value, $operator]; } /** * Determine if the given operator and value combination is legal. * * Prevents using Null values with invalid operators. * * @param string $operator * @param mixed $value * @return bool */ protected function invalidOperatorAndValue($operator, $value) { return is_null($value) && in_array($operator, $this->operators) && ! in_array($operator, ['=', '<>', '!=']); } /** * Determine if the given operator is supported. * * @param string $operator * @return bool */ protected function invalidOperator($operator) { return ! is_string($operator) || (! in_array(strtolower($operator), $this->operators, true) && ! in_array(strtolower($operator), $this->grammar->getOperators(), true)); } /** * Determine if the operator is a bitwise operator. * * @param string $operator * @return bool */ protected function isBitwiseOperator($operator) { return in_array(strtolower($operator), $this->bitwiseOperators, true) || in_array(strtolower($operator), $this->grammar->getBitwiseOperators(), true); } /** * Add an "or where" clause to the query. * * @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column * @param mixed $operator * @param mixed $value * @return $this */ public function orWhere($column, $operator = null, $value = null) { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); return $this->where($column, $operator, $value, 'or'); } /** * Add a basic "where not" clause to the query. * * @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column * @param mixed $operator * @param mixed $value * @param string $boolean * @return $this */ public function whereNot($column, $operator = null, $value = null, $boolean = 'and') { if (is_array($column)) { return $this->whereNested(function ($query) use ($column, $operator, $value, $boolean) { $query->where($column, $operator, $value, $boolean); }, $boolean.' not'); } return $this->where($column, $operator, $value, $boolean.' not'); } /** * Add an "or where not" clause to the query. * * @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column * @param mixed $operator * @param mixed $value * @return $this */ public function orWhereNot($column, $operator = null, $value = null) { return $this->whereNot($column, $operator, $value, 'or'); } /** * Add a "where" clause comparing two columns to the query. * * @param string|array $first * @param string|null $operator * @param string|null $second * @param string|null $boolean * @return $this */ public function whereColumn($first, $operator = null, $second = null, $boolean = 'and') { // If the column is an array, we will assume it is an array of key-value pairs // and can add them each as a where clause. We will maintain the boolean we // received when the method was called and pass it into the nested where. if (is_array($first)) { return $this->addArrayOfWheres($first, $boolean, 'whereColumn'); } // If the given operator is not found in the list of valid operators we will // assume that the developer is just short-cutting the '=' operators and // we will set the operators to '=' and set the values appropriately. if ($this->invalidOperator($operator)) { [$second, $operator] = [$operator, '=']; } // Finally, we will add this where clause into this array of clauses that we // are building for the query. All of them will be compiled via a grammar // once the query is about to be executed and run against the database. $type = 'Column'; $this->wheres[] = compact( 'type', 'first', 'operator', 'second', 'boolean' ); return $this; } /** * Add an "or where" clause comparing two columns to the query. * * @param string|array $first * @param string|null $operator * @param string|null $second * @return $this */ public function orWhereColumn($first, $operator = null, $second = null) { return $this->whereColumn($first, $operator, $second, 'or'); } /** * Add a raw where clause to the query. * * @param string $sql * @param mixed $bindings * @param string $boolean * @return $this */ public function whereRaw($sql, $bindings = [], $boolean = 'and') { $this->wheres[] = ['type' => 'raw', 'sql' => $sql, 'boolean' => $boolean]; $this->addBinding((array) $bindings, 'where'); return $this; } /** * Add a raw or where clause to the query. * * @param string $sql * @param mixed $bindings * @return $this */ public function orWhereRaw($sql, $bindings = []) { return $this->whereRaw($sql, $bindings, 'or'); } /** * Add a "where in" clause to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param mixed $values * @param string $boolean * @param bool $not * @return $this */ public function whereIn($column, $values, $boolean = 'and', $not = false) { $type = $not ? 'NotIn' : 'In'; // If the value is a query builder instance we will assume the developer wants to // look for any values that exist within this given query. So, we will add the // query accordingly so that this query is properly executed when it is run. if ($this->isQueryable($values)) { [$query, $bindings] = $this->createSub($values); $values = [new Expression($query)]; $this->addBinding($bindings, 'where'); } // Next, if the value is Arrayable we need to cast it to its raw array form so we // have the underlying array value instead of an Arrayable object which is not // able to be added as a binding, etc. We will then add to the wheres array. if ($values instanceof Arrayable) { $values = $values->toArray(); } $this->wheres[] = compact('type', 'column', 'values', 'boolean'); if (count($values) !== count(Arr::flatten($values, 1))) { throw new InvalidArgumentException('Nested arrays may not be passed to whereIn method.'); } // Finally, we'll add a binding for each value unless that value is an expression // in which case we will just skip over it since it will be the query as a raw // string and not as a parameterized place-holder to be replaced by the PDO. $this->addBinding($this->cleanBindings($values), 'where'); return $this; } /** * Add an "or where in" clause to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param mixed $values * @return $this */ public function orWhereIn($column, $values) { return $this->whereIn($column, $values, 'or'); } /** * Add a "where not in" clause to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param mixed $values * @param string $boolean * @return $this */ public function whereNotIn($column, $values, $boolean = 'and') { return $this->whereIn($column, $values, $boolean, true); } /** * Add an "or where not in" clause to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param mixed $values * @return $this */ public function orWhereNotIn($column, $values) { return $this->whereNotIn($column, $values, 'or'); } /** * Add a "where in raw" clause for integer values to the query. * * @param string $column * @param \Illuminate\Contracts\Support\Arrayable|array $values * @param string $boolean * @param bool $not * @return $this */ public function whereIntegerInRaw($column, $values, $boolean = 'and', $not = false) { $type = $not ? 'NotInRaw' : 'InRaw'; if ($values instanceof Arrayable) { $values = $values->toArray(); } $values = Arr::flatten($values); foreach ($values as &$value) { $value = (int) $value; } $this->wheres[] = compact('type', 'column', 'values', 'boolean'); return $this; } /** * Add an "or where in raw" clause for integer values to the query. * * @param string $column * @param \Illuminate\Contracts\Support\Arrayable|array $values * @return $this */ public function orWhereIntegerInRaw($column, $values) { return $this->whereIntegerInRaw($column, $values, 'or'); } /** * Add a "where not in raw" clause for integer values to the query. * * @param string $column * @param \Illuminate\Contracts\Support\Arrayable|array $values * @param string $boolean * @return $this */ public function whereIntegerNotInRaw($column, $values, $boolean = 'and') { return $this->whereIntegerInRaw($column, $values, $boolean, true); } /** * Add an "or where not in raw" clause for integer values to the query. * * @param string $column * @param \Illuminate\Contracts\Support\Arrayable|array $values * @return $this */ public function orWhereIntegerNotInRaw($column, $values) { return $this->whereIntegerNotInRaw($column, $values, 'or'); } /** * Add a "where null" clause to the query. * * @param string|array|\Illuminate\Contracts\Database\Query\Expression $columns * @param string $boolean * @param bool $not * @return $this */ public function whereNull($columns, $boolean = 'and', $not = false) { $type = $not ? 'NotNull' : 'Null'; foreach (Arr::wrap($columns) as $column) { $this->wheres[] = compact('type', 'column', 'boolean'); } return $this; } /** * Add an "or where null" clause to the query. * * @param string|array|\Illuminate\Contracts\Database\Query\Expression $column * @return $this */ public function orWhereNull($column) { return $this->whereNull($column, 'or'); } /** * Add a "where not null" clause to the query. * * @param string|array|\Illuminate\Contracts\Database\Query\Expression $columns * @param string $boolean * @return $this */ public function whereNotNull($columns, $boolean = 'and') { return $this->whereNull($columns, $boolean, true); } /** * Add a where between statement to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param iterable $values * @param string $boolean * @param bool $not * @return $this */ public function whereBetween($column, iterable $values, $boolean = 'and', $not = false) { $type = 'between'; if ($values instanceof CarbonPeriod) { $values = [$values->start, $values->end]; } $this->wheres[] = compact('type', 'column', 'values', 'boolean', 'not'); $this->addBinding(array_slice($this->cleanBindings(Arr::flatten($values)), 0, 2), 'where'); return $this; } /** * Add a where between statement using columns to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param array $values * @param string $boolean * @param bool $not * @return $this */ public function whereBetweenColumns($column, array $values, $boolean = 'and', $not = false) { $type = 'betweenColumns'; $this->wheres[] = compact('type', 'column', 'values', 'boolean', 'not'); return $this; } /** * Add an or where between statement to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param iterable $values * @return $this */ public function orWhereBetween($column, iterable $values) { return $this->whereBetween($column, $values, 'or'); } /** * Add an or where between statement using columns to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param array $values * @return $this */ public function orWhereBetweenColumns($column, array $values) { return $this->whereBetweenColumns($column, $values, 'or'); } /** * Add a where not between statement to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param iterable $values * @param string $boolean * @return $this */ public function whereNotBetween($column, iterable $values, $boolean = 'and') { return $this->whereBetween($column, $values, $boolean, true); } /** * Add a where not between statement using columns to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param array $values * @param string $boolean * @return $this */ public function whereNotBetweenColumns($column, array $values, $boolean = 'and') { return $this->whereBetweenColumns($column, $values, $boolean, true); } /** * Add an or where not between statement to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param iterable $values * @return $this */ public function orWhereNotBetween($column, iterable $values) { return $this->whereNotBetween($column, $values, 'or'); } /** * Add an or where not between statement using columns to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param array $values * @return $this */ public function orWhereNotBetweenColumns($column, array $values) { return $this->whereNotBetweenColumns($column, $values, 'or'); } /** * Add an "or where not null" clause to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @return $this */ public function orWhereNotNull($column) { return $this->whereNotNull($column, 'or'); } /** * Add a "where date" statement to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param string $operator * @param \DateTimeInterface|string|null $value * @param string $boolean * @return $this */ public function whereDate($column, $operator, $value = null, $boolean = 'and') { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); $value = $this->flattenValue($value); if ($value instanceof DateTimeInterface) { $value = $value->format('Y-m-d'); } return $this->addDateBasedWhere('Date', $column, $operator, $value, $boolean); } /** * Add an "or where date" statement to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param string $operator * @param \DateTimeInterface|string|null $value * @return $this */ public function orWhereDate($column, $operator, $value = null) { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); return $this->whereDate($column, $operator, $value, 'or'); } /** * Add a "where time" statement to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param string $operator * @param \DateTimeInterface|string|null $value * @param string $boolean * @return $this */ public function whereTime($column, $operator, $value = null, $boolean = 'and') { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); $value = $this->flattenValue($value); if ($value instanceof DateTimeInterface) { $value = $value->format('H:i:s'); } return $this->addDateBasedWhere('Time', $column, $operator, $value, $boolean); } /** * Add an "or where time" statement to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param string $operator * @param \DateTimeInterface|string|null $value * @return $this */ public function orWhereTime($column, $operator, $value = null) { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); return $this->whereTime($column, $operator, $value, 'or'); } /** * Add a "where day" statement to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param string $operator * @param \DateTimeInterface|string|int|null $value * @param string $boolean * @return $this */ public function whereDay($column, $operator, $value = null, $boolean = 'and') { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); $value = $this->flattenValue($value); if ($value instanceof DateTimeInterface) { $value = $value->format('d'); } if (! $value instanceof ExpressionContract) { $value = sprintf('%02d', $value); } return $this->addDateBasedWhere('Day', $column, $operator, $value, $boolean); } /** * Add an "or where day" statement to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param string $operator * @param \DateTimeInterface|string|int|null $value * @return $this */ public function orWhereDay($column, $operator, $value = null) { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); return $this->whereDay($column, $operator, $value, 'or'); } /** * Add a "where month" statement to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param string $operator * @param \DateTimeInterface|string|int|null $value * @param string $boolean * @return $this */ public function whereMonth($column, $operator, $value = null, $boolean = 'and') { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); $value = $this->flattenValue($value); if ($value instanceof DateTimeInterface) { $value = $value->format('m'); } if (! $value instanceof ExpressionContract) { $value = sprintf('%02d', $value); } return $this->addDateBasedWhere('Month', $column, $operator, $value, $boolean); } /** * Add an "or where month" statement to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param string $operator * @param \DateTimeInterface|string|int|null $value * @return $this */ public function orWhereMonth($column, $operator, $value = null) { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); return $this->whereMonth($column, $operator, $value, 'or'); } /** * Add a "where year" statement to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param string $operator * @param \DateTimeInterface|string|int|null $value * @param string $boolean * @return $this */ public function whereYear($column, $operator, $value = null, $boolean = 'and') { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); $value = $this->flattenValue($value); if ($value instanceof DateTimeInterface) { $value = $value->format('Y'); } return $this->addDateBasedWhere('Year', $column, $operator, $value, $boolean); } /** * Add an "or where year" statement to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param string $operator * @param \DateTimeInterface|string|int|null $value * @return $this */ public function orWhereYear($column, $operator, $value = null) { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); return $this->whereYear($column, $operator, $value, 'or'); } /** * Add a date based (year, month, day, time) statement to the query. * * @param string $type * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param string $operator * @param mixed $value * @param string $boolean * @return $this */ protected function addDateBasedWhere($type, $column, $operator, $value, $boolean = 'and') { $this->wheres[] = compact('column', 'type', 'boolean', 'operator', 'value'); if (! $value instanceof ExpressionContract) { $this->addBinding($value, 'where'); } return $this; } /** * Add a nested where statement to the query. * * @param \Closure $callback * @param string $boolean * @return $this */ public function whereNested(Closure $callback, $boolean = 'and') { $callback($query = $this->forNestedWhere()); return $this->addNestedWhereQuery($query, $boolean); } /** * Create a new query instance for nested where condition. * * @return \Illuminate\Database\Query\Builder */ public function forNestedWhere() { return $this->newQuery()->from($this->from); } /** * Add another query builder as a nested where to the query builder. * * @param \Illuminate\Database\Query\Builder $query * @param string $boolean * @return $this */ public function addNestedWhereQuery($query, $boolean = 'and') { if (count($query->wheres)) { $type = 'Nested'; $this->wheres[] = compact('type', 'query', 'boolean'); $this->addBinding($query->getRawBindings()['where'], 'where'); } return $this; } /** * Add a full sub-select to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param string $operator * @param \Closure||\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $callback * @param string $boolean * @return $this */ protected function whereSub($column, $operator, $callback, $boolean) { $type = 'Sub'; if ($callback instanceof Closure) { // Once we have the query instance we can simply execute it so it can add all // of the sub-select's conditions to itself, and then we can cache it off // in the array of where clauses for the "main" parent query instance. $callback($query = $this->forSubQuery()); } else { $query = $callback instanceof EloquentBuilder ? $callback->toBase() : $callback; } $this->wheres[] = compact( 'type', 'column', 'operator', 'query', 'boolean' ); $this->addBinding($query->getBindings(), 'where'); return $this; } /** * Add an exists clause to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $callback * @param string $boolean * @param bool $not * @return $this */ public function whereExists($callback, $boolean = 'and', $not = false) { if ($callback instanceof Closure) { $query = $this->forSubQuery(); // Similar to the sub-select clause, we will create a new query instance so // the developer may cleanly specify the entire exists query and we will // compile the whole thing in the grammar and insert it into the SQL. $callback($query); } else { $query = $callback instanceof EloquentBuilder ? $callback->toBase() : $callback; } return $this->addWhereExistsQuery($query, $boolean, $not); } /** * Add an or exists clause to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $callback * @param bool $not * @return $this */ public function orWhereExists($callback, $not = false) { return $this->whereExists($callback, 'or', $not); } /** * Add a where not exists clause to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $callback * @param string $boolean * @return $this */ public function whereNotExists($callback, $boolean = 'and') { return $this->whereExists($callback, $boolean, true); } /** * Add a where not exists clause to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $callback * @return $this */ public function orWhereNotExists($callback) { return $this->orWhereExists($callback, true); } /** * Add an exists clause to the query. * * @param \Illuminate\Database\Query\Builder $query * @param string $boolean * @param bool $not * @return $this */ public function addWhereExistsQuery(self $query, $boolean = 'and', $not = false) { $type = $not ? 'NotExists' : 'Exists'; $this->wheres[] = compact('type', 'query', 'boolean'); $this->addBinding($query->getBindings(), 'where'); return $this; } /** * Adds a where condition using row values. * * @param array $columns * @param string $operator * @param array $values * @param string $boolean * @return $this * * @throws \InvalidArgumentException */ public function whereRowValues($columns, $operator, $values, $boolean = 'and') { if (count($columns) !== count($values)) { throw new InvalidArgumentException('The number of columns must match the number of values'); } $type = 'RowValues'; $this->wheres[] = compact('type', 'columns', 'operator', 'values', 'boolean'); $this->addBinding($this->cleanBindings($values)); return $this; } /** * Adds an or where condition using row values. * * @param array $columns * @param string $operator * @param array $values * @return $this */ public function orWhereRowValues($columns, $operator, $values) { return $this->whereRowValues($columns, $operator, $values, 'or'); } /** * Add a "where JSON contains" clause to the query. * * @param string $column * @param mixed $value * @param string $boolean * @param bool $not * @return $this */ public function whereJsonContains($column, $value, $boolean = 'and', $not = false) { $type = 'JsonContains'; $this->wheres[] = compact('type', 'column', 'value', 'boolean', 'not'); if (! $value instanceof ExpressionContract) { $this->addBinding($this->grammar->prepareBindingForJsonContains($value)); } return $this; } /** * Add an "or where JSON contains" clause to the query. * * @param string $column * @param mixed $value * @return $this */ public function orWhereJsonContains($column, $value) { return $this->whereJsonContains($column, $value, 'or'); } /** * Add a "where JSON not contains" clause to the query. * * @param string $column * @param mixed $value * @param string $boolean * @return $this */ public function whereJsonDoesntContain($column, $value, $boolean = 'and') { return $this->whereJsonContains($column, $value, $boolean, true); } /** * Add an "or where JSON not contains" clause to the query. * * @param string $column * @param mixed $value * @return $this */ public function orWhereJsonDoesntContain($column, $value) { return $this->whereJsonDoesntContain($column, $value, 'or'); } /** * Add a clause that determines if a JSON path exists to the query. * * @param string $column * @param string $boolean * @param bool $not * @return $this */ public function whereJsonContainsKey($column, $boolean = 'and', $not = false) { $type = 'JsonContainsKey'; $this->wheres[] = compact('type', 'column', 'boolean', 'not'); return $this; } /** * Add an "or" clause that determines if a JSON path exists to the query. * * @param string $column * @return $this */ public function orWhereJsonContainsKey($column) { return $this->whereJsonContainsKey($column, 'or'); } /** * Add a clause that determines if a JSON path does not exist to the query. * * @param string $column * @param string $boolean * @return $this */ public function whereJsonDoesntContainKey($column, $boolean = 'and') { return $this->whereJsonContainsKey($column, $boolean, true); } /** * Add an "or" clause that determines if a JSON path does not exist to the query. * * @param string $column * @return $this */ public function orWhereJsonDoesntContainKey($column) { return $this->whereJsonDoesntContainKey($column, 'or'); } /** * Add a "where JSON length" clause to the query. * * @param string $column * @param mixed $operator * @param mixed $value * @param string $boolean * @return $this */ public function whereJsonLength($column, $operator, $value = null, $boolean = 'and') { $type = 'JsonLength'; [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); $this->wheres[] = compact('type', 'column', 'operator', 'value', 'boolean'); if (! $value instanceof ExpressionContract) { $this->addBinding((int) $this->flattenValue($value)); } return $this; } /** * Add an "or where JSON length" clause to the query. * * @param string $column * @param mixed $operator * @param mixed $value * @return $this */ public function orWhereJsonLength($column, $operator, $value = null) { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); return $this->whereJsonLength($column, $operator, $value, 'or'); } /** * Handles dynamic "where" clauses to the query. * * @param string $method * @param array $parameters * @return $this */ public function dynamicWhere($method, $parameters) { $finder = substr($method, 5); $segments = preg_split( '/(And|Or)(?=[A-Z])/', $finder, -1, PREG_SPLIT_DELIM_CAPTURE ); // The connector variable will determine which connector will be used for the // query condition. We will change it as we come across new boolean values // in the dynamic method strings, which could contain a number of these. $connector = 'and'; $index = 0; foreach ($segments as $segment) { // If the segment is not a boolean connector, we can assume it is a column's name // and we will add it to the query as a new constraint as a where clause, then // we can keep iterating through the dynamic method string's segments again. if ($segment !== 'And' && $segment !== 'Or') { $this->addDynamic($segment, $connector, $parameters, $index); $index++; } // Otherwise, we will store the connector so we know how the next where clause we // find in the query should be connected to the previous ones, meaning we will // have the proper boolean connector to connect the next where clause found. else { $connector = $segment; } } return $this; } /** * Add a single dynamic where clause statement to the query. * * @param string $segment * @param string $connector * @param array $parameters * @param int $index * @return void */ protected function addDynamic($segment, $connector, $parameters, $index) { // Once we have parsed out the columns and formatted the boolean operators we // are ready to add it to this query as a where clause just like any other // clause on the query. Then we'll increment the parameter index values. $bool = strtolower($connector); $this->where(Str::snake($segment), '=', $parameters[$index], $bool); } /** * Add a "where fulltext" clause to the query. * * @param string|string[] $columns * @param string $value * @param string $boolean * @return $this */ public function whereFullText($columns, $value, array $options = [], $boolean = 'and') { $type = 'Fulltext'; $columns = (array) $columns; $this->wheres[] = compact('type', 'columns', 'value', 'options', 'boolean'); $this->addBinding($value); return $this; } /** * Add a "or where fulltext" clause to the query. * * @param string|string[] $columns * @param string $value * @return $this */ public function orWhereFullText($columns, $value, array $options = []) { return $this->whereFulltext($columns, $value, $options, 'or'); } /** * Add a "group by" clause to the query. * * @param array|\Illuminate\Contracts\Database\Query\Expression|string ...$groups * @return $this */ public function groupBy(...$groups) { foreach ($groups as $group) { $this->groups = array_merge( (array) $this->groups, Arr::wrap($group) ); } return $this; } /** * Add a raw groupBy clause to the query. * * @param string $sql * @param array $bindings * @return $this */ public function groupByRaw($sql, array $bindings = []) { $this->groups[] = new Expression($sql); $this->addBinding($bindings, 'groupBy'); return $this; } /** * Add a "having" clause to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|\Closure|string $column * @param string|int|float|null $operator * @param string|int|float|null $value * @param string $boolean * @return $this */ public function having($column, $operator = null, $value = null, $boolean = 'and') { $type = 'Basic'; if ($column instanceof ConditionExpression) { $type = 'Expression'; $this->havings[] = compact('type', 'column', 'boolean'); return $this; } // Here we will make some assumptions about the operator. If only 2 values are // passed to the method, we will assume that the operator is an equals sign // and keep going. Otherwise, we'll require the operator to be passed in. [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); if ($column instanceof Closure && is_null($operator)) { return $this->havingNested($column, $boolean); } // If the given operator is not found in the list of valid operators we will // assume that the developer is just short-cutting the '=' operators and // we will set the operators to '=' and set the values appropriately. if ($this->invalidOperator($operator)) { [$value, $operator] = [$operator, '=']; } if ($this->isBitwiseOperator($operator)) { $type = 'Bitwise'; } $this->havings[] = compact('type', 'column', 'operator', 'value', 'boolean'); if (! $value instanceof ExpressionContract) { $this->addBinding($this->flattenValue($value), 'having'); } return $this; } /** * Add an "or having" clause to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|\Closure|string $column * @param string|int|float|null $operator * @param string|int|float|null $value * @return $this */ public function orHaving($column, $operator = null, $value = null) { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); return $this->having($column, $operator, $value, 'or'); } /** * Add a nested having statement to the query. * * @param \Closure $callback * @param string $boolean * @return $this */ public function havingNested(Closure $callback, $boolean = 'and') { $callback($query = $this->forNestedWhere()); return $this->addNestedHavingQuery($query, $boolean); } /** * Add another query builder as a nested having to the query builder. * * @param \Illuminate\Database\Query\Builder $query * @param string $boolean * @return $this */ public function addNestedHavingQuery($query, $boolean = 'and') { if (count($query->havings)) { $type = 'Nested'; $this->havings[] = compact('type', 'query', 'boolean'); $this->addBinding($query->getRawBindings()['having'], 'having'); } return $this; } /** * Add a "having null" clause to the query. * * @param string|array $columns * @param string $boolean * @param bool $not * @return $this */ public function havingNull($columns, $boolean = 'and', $not = false) { $type = $not ? 'NotNull' : 'Null'; foreach (Arr::wrap($columns) as $column) { $this->havings[] = compact('type', 'column', 'boolean'); } return $this; } /** * Add an "or having null" clause to the query. * * @param string $column * @return $this */ public function orHavingNull($column) { return $this->havingNull($column, 'or'); } /** * Add a "having not null" clause to the query. * * @param string|array $columns * @param string $boolean * @return $this */ public function havingNotNull($columns, $boolean = 'and') { return $this->havingNull($columns, $boolean, true); } /** * Add an "or having not null" clause to the query. * * @param string $column * @return $this */ public function orHavingNotNull($column) { return $this->havingNotNull($column, 'or'); } /** * Add a "having between " clause to the query. * * @param string $column * @param iterable $values * @param string $boolean * @param bool $not * @return $this */ public function havingBetween($column, iterable $values, $boolean = 'and', $not = false) { $type = 'between'; if ($values instanceof CarbonPeriod) { $values = [$values->start, $values->end]; } $this->havings[] = compact('type', 'column', 'values', 'boolean', 'not'); $this->addBinding(array_slice($this->cleanBindings(Arr::flatten($values)), 0, 2), 'having'); return $this; } /** * Add a raw having clause to the query. * * @param string $sql * @param array $bindings * @param string $boolean * @return $this */ public function havingRaw($sql, array $bindings = [], $boolean = 'and') { $type = 'Raw'; $this->havings[] = compact('type', 'sql', 'boolean'); $this->addBinding($bindings, 'having'); return $this; } /** * Add a raw or having clause to the query. * * @param string $sql * @param array $bindings * @return $this */ public function orHavingRaw($sql, array $bindings = []) { return $this->havingRaw($sql, $bindings, 'or'); } /** * Add an "order by" clause to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Contracts\Database\Query\Expression|string $column * @param string $direction * @return $this * * @throws \InvalidArgumentException */ public function orderBy($column, $direction = 'asc') { if ($this->isQueryable($column)) { [$query, $bindings] = $this->createSub($column); $column = new Expression('('.$query.')'); $this->addBinding($bindings, $this->unions ? 'unionOrder' : 'order'); } $direction = strtolower($direction); if (! in_array($direction, ['asc', 'desc'], true)) { throw new InvalidArgumentException('Order direction must be "asc" or "desc".'); } $this->{$this->unions ? 'unionOrders' : 'orders'}[] = [ 'column' => $column, 'direction' => $direction, ]; return $this; } /** * Add a descending "order by" clause to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Contracts\Database\Query\Expression|string $column * @return $this */ public function orderByDesc($column) { return $this->orderBy($column, 'desc'); } /** * Add an "order by" clause for a timestamp to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Contracts\Database\Query\Expression|string $column * @return $this */ public function latest($column = 'created_at') { return $this->orderBy($column, 'desc'); } /** * Add an "order by" clause for a timestamp to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Contracts\Database\Query\Expression|string $column * @return $this */ public function oldest($column = 'created_at') { return $this->orderBy($column, 'asc'); } /** * Put the query's results in random order. * * @param string|int $seed * @return $this */ public function inRandomOrder($seed = '') { return $this->orderByRaw($this->grammar->compileRandom($seed)); } /** * Add a raw "order by" clause to the query. * * @param string $sql * @param array $bindings * @return $this */ public function orderByRaw($sql, $bindings = []) { $type = 'Raw'; $this->{$this->unions ? 'unionOrders' : 'orders'}[] = compact('type', 'sql'); $this->addBinding($bindings, $this->unions ? 'unionOrder' : 'order'); return $this; } /** * Alias to set the "offset" value of the query. * * @param int $value * @return $this */ public function skip($value) { return $this->offset($value); } /** * Set the "offset" value of the query. * * @param int $value * @return $this */ public function offset($value) { $property = $this->unions ? 'unionOffset' : 'offset'; $this->$property = max(0, (int) $value); return $this; } /** * Alias to set the "limit" value of the query. * * @param int $value * @return $this */ public function take($value) { return $this->limit($value); } /** * Set the "limit" value of the query. * * @param int $value * @return $this */ public function limit($value) { $property = $this->unions ? 'unionLimit' : 'limit'; if ($value >= 0) { $this->$property = ! is_null($value) ? (int) $value : null; } return $this; } /** * Set the limit and offset for a given page. * * @param int $page * @param int $perPage * @return $this */ public function forPage($page, $perPage = 15) { return $this->offset(($page - 1) * $perPage)->limit($perPage); } /** * Constrain the query to the previous "page" of results before a given ID. * * @param int $perPage * @param int|null $lastId * @param string $column * @return $this */ public function forPageBeforeId($perPage = 15, $lastId = 0, $column = 'id') { $this->orders = $this->removeExistingOrdersFor($column); if (! is_null($lastId)) { $this->where($column, '<', $lastId); } return $this->orderBy($column, 'desc') ->limit($perPage); } /** * Constrain the query to the next "page" of results after a given ID. * * @param int $perPage * @param int|null $lastId * @param string $column * @return $this */ public function forPageAfterId($perPage = 15, $lastId = 0, $column = 'id') { $this->orders = $this->removeExistingOrdersFor($column); if (! is_null($lastId)) { $this->where($column, '>', $lastId); } return $this->orderBy($column, 'asc') ->limit($perPage); } /** * Remove all existing orders and optionally add a new order. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Contracts\Database\Query\Expression|string|null $column * @param string $direction * @return $this */ public function reorder($column = null, $direction = 'asc') { $this->orders = null; $this->unionOrders = null; $this->bindings['order'] = []; $this->bindings['unionOrder'] = []; if ($column) { return $this->orderBy($column, $direction); } return $this; } /** * Get an array with all orders with a given column removed. * * @param string $column * @return array */ protected function removeExistingOrdersFor($column) { return Collection::make($this->orders) ->reject(function ($order) use ($column) { return isset($order['column']) ? $order['column'] === $column : false; })->values()->all(); } /** * Add a union statement to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query * @param bool $all * @return $this */ public function union($query, $all = false) { if ($query instanceof Closure) { $query($query = $this->newQuery()); } $this->unions[] = compact('query', 'all'); $this->addBinding($query->getBindings(), 'union'); return $this; } /** * Add a union all statement to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query * @return $this */ public function unionAll($query) { return $this->union($query, true); } /** * Lock the selected rows in the table. * * @param string|bool $value * @return $this */ public function lock($value = true) { $this->lock = $value; if (! is_null($this->lock)) { $this->useWritePdo(); } return $this; } /** * Lock the selected rows in the table for updating. * * @return $this */ public function lockForUpdate() { return $this->lock(true); } /** * Share lock the selected rows in the table. * * @return $this */ public function sharedLock() { return $this->lock(false); } /** * Register a closure to be invoked before the query is executed. * * @param callable $callback * @return $this */ public function beforeQuery(callable $callback) { $this->beforeQueryCallbacks[] = $callback; return $this; } /** * Invoke the "before query" modification callbacks. * * @return void */ public function applyBeforeQueryCallbacks() { foreach ($this->beforeQueryCallbacks as $callback) { $callback($this); } $this->beforeQueryCallbacks = []; } /** * Get the SQL representation of the query. * * @return string */ public function toSql() { $this->applyBeforeQueryCallbacks(); return $this->grammar->compileSelect($this); } /** * Get the raw SQL representation of the query with embedded bindings. * * @return string */ public function toRawSql() { return $this->grammar->substituteBindingsIntoRawSql( $this->toSql(), $this->connection->prepareBindings($this->getBindings()) ); } /** * Execute a query for a single record by ID. * * @param int|string $id * @param array|string $columns * @return mixed|static */ public function find($id, $columns = ['*']) { return $this->where('id', '=', $id)->first($columns); } /** * Execute a query for a single record by ID or call a callback. * * @param mixed $id * @param \Closure|array|string $columns * @param \Closure|null $callback * @return mixed|static */ public function findOr($id, $columns = ['*'], Closure $callback = null) { if ($columns instanceof Closure) { $callback = $columns; $columns = ['*']; } if (! is_null($data = $this->find($id, $columns))) { return $data; } return $callback(); } /** * Get a single column's value from the first result of a query. * * @param string $column * @return mixed */ public function value($column) { $result = (array) $this->first([$column]); return count($result) > 0 ? reset($result) : null; } /** * Get a single expression value from the first result of a query. * * @param string $expression * @param array $bindings * @return mixed */ public function rawValue(string $expression, array $bindings = []) { $result = (array) $this->selectRaw($expression, $bindings)->first(); return count($result) > 0 ? reset($result) : null; } /** * Get a single column's value from the first result of a query if it's the sole matching record. * * @param string $column * @return mixed * * @throws \Illuminate\Database\RecordsNotFoundException * @throws \Illuminate\Database\MultipleRecordsFoundException */ public function soleValue($column) { $result = (array) $this->sole([$column]); return reset($result); } /** * Execute the query as a "select" statement. * * @param array|string $columns * @return \Illuminate\Support\Collection */ public function get($columns = ['*']) { return collect($this->onceWithColumns(Arr::wrap($columns), function () { return $this->processor->processSelect($this, $this->runSelect()); })); } /** * Run the query as a "select" statement against the connection. * * @return array */ protected function runSelect() { return $this->connection->select( $this->toSql(), $this->getBindings(), ! $this->useWritePdo ); } /** * Paginate the given query into a simple paginator. * * @param int|\Closure $perPage * @param array|string $columns * @param string $pageName * @param int|null $page * @param \Closure|int|null $total * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null) { $page = $page ?: Paginator::resolveCurrentPage($pageName); $total = func_num_args() === 5 ? value(func_get_arg(4)) : $this->getCountForPagination(); $perPage = $perPage instanceof Closure ? $perPage($total) : $perPage; $results = $total ? $this->forPage($page, $perPage)->get($columns) : collect(); return $this->paginator($results, $total, $perPage, $page, [ 'path' => Paginator::resolveCurrentPath(), 'pageName' => $pageName, ]); } /** * Get a paginator only supporting simple next and previous links. * * This is more efficient on larger data-sets, etc. * * @param int $perPage * @param array|string $columns * @param string $pageName * @param int|null $page * @return \Illuminate\Contracts\Pagination\Paginator */ public function simplePaginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null) { $page = $page ?: Paginator::resolveCurrentPage($pageName); $this->offset(($page - 1) * $perPage)->limit($perPage + 1); return $this->simplePaginator($this->get($columns), $perPage, $page, [ 'path' => Paginator::resolveCurrentPath(), 'pageName' => $pageName, ]); } /** * Get a paginator only supporting simple next and previous links. * * This is more efficient on larger data-sets, etc. * * @param int|null $perPage * @param array|string $columns * @param string $cursorName * @param \Illuminate\Pagination\Cursor|string|null $cursor * @return \Illuminate\Contracts\Pagination\CursorPaginator */ public function cursorPaginate($perPage = 15, $columns = ['*'], $cursorName = 'cursor', $cursor = null) { return $this->paginateUsingCursor($perPage, $columns, $cursorName, $cursor); } /** * Ensure the proper order by required for cursor pagination. * * @param bool $shouldReverse * @return \Illuminate\Support\Collection */ protected function ensureOrderForCursorPagination($shouldReverse = false) { if (empty($this->orders) && empty($this->unionOrders)) { $this->enforceOrderBy(); } $reverseDirection = function ($order) { if (! isset($order['direction'])) { return $order; } $order['direction'] = $order['direction'] === 'asc' ? 'desc' : 'asc'; return $order; }; if ($shouldReverse) { $this->orders = collect($this->orders)->map($reverseDirection)->toArray(); $this->unionOrders = collect($this->unionOrders)->map($reverseDirection)->toArray(); } $orders = ! empty($this->unionOrders) ? $this->unionOrders : $this->orders; return collect($orders) ->filter(fn ($order) => Arr::has($order, 'direction')) ->values(); } /** * Get the count of the total records for the paginator. * * @param array $columns * @return int */ public function getCountForPagination($columns = ['*']) { $results = $this->runPaginationCountQuery($columns); // Once we have run the pagination count query, we will get the resulting count and // take into account what type of query it was. When there is a group by we will // just return the count of the entire results set since that will be correct. if (! isset($results[0])) { return 0; } elseif (is_object($results[0])) { return (int) $results[0]->aggregate; } return (int) array_change_key_case((array) $results[0])['aggregate']; } /** * Run a pagination count query. * * @param array $columns * @return array */ protected function runPaginationCountQuery($columns = ['*']) { if ($this->groups || $this->havings) { $clone = $this->cloneForPaginationCount(); if (is_null($clone->columns) && ! empty($this->joins)) { $clone->select($this->from.'.*'); } return $this->newQuery() ->from(new Expression('('.$clone->toSql().') as '.$this->grammar->wrap('aggregate_table'))) ->mergeBindings($clone) ->setAggregate('count', $this->withoutSelectAliases($columns)) ->get()->all(); } $without = $this->unions ? ['orders', 'limit', 'offset'] : ['columns', 'orders', 'limit', 'offset']; return $this->cloneWithout($without) ->cloneWithoutBindings($this->unions ? ['order'] : ['select', 'order']) ->setAggregate('count', $this->withoutSelectAliases($columns)) ->get()->all(); } /** * Clone the existing query instance for usage in a pagination subquery. * * @return self */ protected function cloneForPaginationCount() { return $this->cloneWithout(['orders', 'limit', 'offset']) ->cloneWithoutBindings(['order']); } /** * Remove the column aliases since they will break count queries. * * @param array $columns * @return array */ protected function withoutSelectAliases(array $columns) { return array_map(function ($column) { return is_string($column) && ($aliasPosition = stripos($column, ' as ')) !== false ? substr($column, 0, $aliasPosition) : $column; }, $columns); } /** * Get a lazy collection for the given query. * * @return \Illuminate\Support\LazyCollection */ public function cursor() { if (is_null($this->columns)) { $this->columns = ['*']; } return new LazyCollection(function () { yield from $this->connection->cursor( $this->toSql(), $this->getBindings(), ! $this->useWritePdo ); }); } /** * Throw an exception if the query doesn't have an orderBy clause. * * @return void * * @throws \RuntimeException */ protected function enforceOrderBy() { if (empty($this->orders) && empty($this->unionOrders)) { throw new RuntimeException('You must specify an orderBy clause when using this function.'); } } /** * Get a collection instance containing the values of a given column. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @param string|null $key * @return \Illuminate\Support\Collection */ public function pluck($column, $key = null) { // First, we will need to select the results of the query accounting for the // given columns / key. Once we have the results, we will be able to take // the results and get the exact data that was requested for the query. $queryResult = $this->onceWithColumns( is_null($key) ? [$column] : [$column, $key], function () { return $this->processor->processSelect( $this, $this->runSelect() ); } ); if (empty($queryResult)) { return collect(); } // If the columns are qualified with a table or have an alias, we cannot use // those directly in the "pluck" operations since the results from the DB // are only keyed by the column itself. We'll strip the table out here. $column = $this->stripTableForPluck($column); $key = $this->stripTableForPluck($key); return is_array($queryResult[0]) ? $this->pluckFromArrayColumn($queryResult, $column, $key) : $this->pluckFromObjectColumn($queryResult, $column, $key); } /** * Strip off the table name or alias from a column identifier. * * @param string $column * @return string|null */ protected function stripTableForPluck($column) { if (is_null($column)) { return $column; } $columnString = $column instanceof ExpressionContract ? $this->grammar->getValue($column) : $column; $separator = str_contains(strtolower($columnString), ' as ') ? ' as ' : '\.'; return last(preg_split('~'.$separator.'~i', $columnString)); } /** * Retrieve column values from rows represented as objects. * * @param array $queryResult * @param string $column * @param string $key * @return \Illuminate\Support\Collection */ protected function pluckFromObjectColumn($queryResult, $column, $key) { $results = []; if (is_null($key)) { foreach ($queryResult as $row) { $results[] = $row->$column; } } else { foreach ($queryResult as $row) { $results[$row->$key] = $row->$column; } } return collect($results); } /** * Retrieve column values from rows represented as arrays. * * @param array $queryResult * @param string $column * @param string $key * @return \Illuminate\Support\Collection */ protected function pluckFromArrayColumn($queryResult, $column, $key) { $results = []; if (is_null($key)) { foreach ($queryResult as $row) { $results[] = $row[$column]; } } else { foreach ($queryResult as $row) { $results[$row[$key]] = $row[$column]; } } return collect($results); } /** * Concatenate values of a given column as a string. * * @param string $column * @param string $glue * @return string */ public function implode($column, $glue = '') { return $this->pluck($column)->implode($glue); } /** * Determine if any rows exist for the current query. * * @return bool */ public function exists() { $this->applyBeforeQueryCallbacks(); $results = $this->connection->select( $this->grammar->compileExists($this), $this->getBindings(), ! $this->useWritePdo ); // If the results have rows, we will get the row and see if the exists column is a // boolean true. If there are no results for this query we will return false as // there are no rows for this query at all, and we can return that info here. if (isset($results[0])) { $results = (array) $results[0]; return (bool) $results['exists']; } return false; } /** * Determine if no rows exist for the current query. * * @return bool */ public function doesntExist() { return ! $this->exists(); } /** * Execute the given callback if no rows exist for the current query. * * @param \Closure $callback * @return mixed */ public function existsOr(Closure $callback) { return $this->exists() ? true : $callback(); } /** * Execute the given callback if rows exist for the current query. * * @param \Closure $callback * @return mixed */ public function doesntExistOr(Closure $callback) { return $this->doesntExist() ? true : $callback(); } /** * Retrieve the "count" result of the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $columns * @return int */ public function count($columns = '*') { return (int) $this->aggregate(__FUNCTION__, Arr::wrap($columns)); } /** * Retrieve the minimum value of a given column. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @return mixed */ public function min($column) { return $this->aggregate(__FUNCTION__, [$column]); } /** * Retrieve the maximum value of a given column. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @return mixed */ public function max($column) { return $this->aggregate(__FUNCTION__, [$column]); } /** * Retrieve the sum of the values of a given column. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @return mixed */ public function sum($column) { $result = $this->aggregate(__FUNCTION__, [$column]); return $result ?: 0; } /** * Retrieve the average of the values of a given column. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @return mixed */ public function avg($column) { return $this->aggregate(__FUNCTION__, [$column]); } /** * Alias for the "avg" method. * * @param \Illuminate\Contracts\Database\Query\Expression|string $column * @return mixed */ public function average($column) { return $this->avg($column); } /** * Execute an aggregate function on the database. * * @param string $function * @param array $columns * @return mixed */ public function aggregate($function, $columns = ['*']) { $results = $this->cloneWithout($this->unions || $this->havings ? [] : ['columns']) ->cloneWithoutBindings($this->unions || $this->havings ? [] : ['select']) ->setAggregate($function, $columns) ->get($columns); if (! $results->isEmpty()) { return array_change_key_case((array) $results[0])['aggregate']; } } /** * Execute a numeric aggregate function on the database. * * @param string $function * @param array $columns * @return float|int */ public function numericAggregate($function, $columns = ['*']) { $result = $this->aggregate($function, $columns); // If there is no result, we can obviously just return 0 here. Next, we will check // if the result is an integer or float. If it is already one of these two data // types we can just return the result as-is, otherwise we will convert this. if (! $result) { return 0; } if (is_int($result) || is_float($result)) { return $result; } // If the result doesn't contain a decimal place, we will assume it is an int then // cast it to one. When it does we will cast it to a float since it needs to be // cast to the expected data type for the developers out of pure convenience. return ! str_contains((string) $result, '.') ? (int) $result : (float) $result; } /** * Set the aggregate property without running the query. * * @param string $function * @param array $columns * @return $this */ protected function setAggregate($function, $columns) { $this->aggregate = compact('function', 'columns'); if (empty($this->groups)) { $this->orders = null; $this->bindings['order'] = []; } return $this; } /** * Execute the given callback while selecting the given columns. * * After running the callback, the columns are reset to the original value. * * @param array $columns * @param callable $callback * @return mixed */ protected function onceWithColumns($columns, $callback) { $original = $this->columns; if (is_null($original)) { $this->columns = $columns; } $result = $callback(); $this->columns = $original; return $result; } /** * Insert new records into the database. * * @param array $values * @return bool */ public function insert(array $values) { // Since every insert gets treated like a batch insert, we will make sure the // bindings are structured in a way that is convenient when building these // inserts statements by verifying these elements are actually an array. if (empty($values)) { return true; } if (! is_array(reset($values))) { $values = [$values]; } // Here, we will sort the insert keys for every record so that each insert is // in the same order for the record. We need to make sure this is the case // so there are not any errors or problems when inserting these records. else { foreach ($values as $key => $value) { ksort($value); $values[$key] = $value; } } $this->applyBeforeQueryCallbacks(); // Finally, we will run this query against the database connection and return // the results. We will need to also flatten these bindings before running // the query so they are all in one huge, flattened array for execution. return $this->connection->insert( $this->grammar->compileInsert($this, $values), $this->cleanBindings(Arr::flatten($values, 1)) ); } /** * Insert new records into the database while ignoring errors. * * @param array $values * @return int */ public function insertOrIgnore(array $values) { if (empty($values)) { return 0; } if (! is_array(reset($values))) { $values = [$values]; } else { foreach ($values as $key => $value) { ksort($value); $values[$key] = $value; } } $this->applyBeforeQueryCallbacks(); return $this->connection->affectingStatement( $this->grammar->compileInsertOrIgnore($this, $values), $this->cleanBindings(Arr::flatten($values, 1)) ); } /** * Insert a new record and get the value of the primary key. * * @param array $values * @param string|null $sequence * @return int */ public function insertGetId(array $values, $sequence = null) { $this->applyBeforeQueryCallbacks(); $sql = $this->grammar->compileInsertGetId($this, $values, $sequence); $values = $this->cleanBindings($values); return $this->processor->processInsertGetId($this, $sql, $values, $sequence); } /** * Insert new records into the table using a subquery. * * @param array $columns * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query * @return int */ public function insertUsing(array $columns, $query) { $this->applyBeforeQueryCallbacks(); [$sql, $bindings] = $this->createSub($query); return $this->connection->affectingStatement( $this->grammar->compileInsertUsing($this, $columns, $sql), $this->cleanBindings($bindings) ); } /** * Update records in the database. * * @param array $values * @return int */ public function update(array $values) { $this->applyBeforeQueryCallbacks(); $sql = $this->grammar->compileUpdate($this, $values); return $this->connection->update($sql, $this->cleanBindings( $this->grammar->prepareBindingsForUpdate($this->bindings, $values) )); } /** * Update records in a PostgreSQL database using the update from syntax. * * @param array $values * @return int */ public function updateFrom(array $values) { if (! method_exists($this->grammar, 'compileUpdateFrom')) { throw new LogicException('This database engine does not support the updateFrom method.'); } $this->applyBeforeQueryCallbacks(); $sql = $this->grammar->compileUpdateFrom($this, $values); return $this->connection->update($sql, $this->cleanBindings( $this->grammar->prepareBindingsForUpdateFrom($this->bindings, $values) )); } /** * Insert or update a record matching the attributes, and fill it with values. * * @param array $attributes * @param array $values * @return bool */ public function updateOrInsert(array $attributes, array $values = []) { if (! $this->where($attributes)->exists()) { return $this->insert(array_merge($attributes, $values)); } if (empty($values)) { return true; } return (bool) $this->limit(1)->update($values); } /** * Insert new records or update the existing ones. * * @param array $values * @param array|string $uniqueBy * @param array|null $update * @return int */ public function upsert(array $values, $uniqueBy, $update = null) { if (empty($values)) { return 0; } elseif ($update === []) { return (int) $this->insert($values); } if (! is_array(reset($values))) { $values = [$values]; } else { foreach ($values as $key => $value) { ksort($value); $values[$key] = $value; } } if (is_null($update)) { $update = array_keys(reset($values)); } $this->applyBeforeQueryCallbacks(); $bindings = $this->cleanBindings(array_merge( Arr::flatten($values, 1), collect($update)->reject(function ($value, $key) { return is_int($key); })->all() )); return $this->connection->affectingStatement( $this->grammar->compileUpsert($this, $values, (array) $uniqueBy, $update), $bindings ); } /** * Increment a column's value by a given amount. * * @param string $column * @param float|int $amount * @param array $extra * @return int * * @throws \InvalidArgumentException */ public function increment($column, $amount = 1, array $extra = []) { if (! is_numeric($amount)) { throw new InvalidArgumentException('Non-numeric value passed to increment method.'); } return $this->incrementEach([$column => $amount], $extra); } /** * Increment the given column's values by the given amounts. * * @param array<string, float|int|numeric-string> $columns * @param array<string, mixed> $extra * @return int * * @throws \InvalidArgumentException */ public function incrementEach(array $columns, array $extra = []) { foreach ($columns as $column => $amount) { if (! is_numeric($amount)) { throw new InvalidArgumentException("Non-numeric value passed as increment amount for column: '$column'."); } elseif (! is_string($column)) { throw new InvalidArgumentException('Non-associative array passed to incrementEach method.'); } $columns[$column] = $this->raw("{$this->grammar->wrap($column)} + $amount"); } return $this->update(array_merge($columns, $extra)); } /** * Decrement a column's value by a given amount. * * @param string $column * @param float|int $amount * @param array $extra * @return int * * @throws \InvalidArgumentException */ public function decrement($column, $amount = 1, array $extra = []) { if (! is_numeric($amount)) { throw new InvalidArgumentException('Non-numeric value passed to decrement method.'); } return $this->decrementEach([$column => $amount], $extra); } /** * Decrement the given column's values by the given amounts. * * @param array<string, float|int|numeric-string> $columns * @param array<string, mixed> $extra * @return int * * @throws \InvalidArgumentException */ public function decrementEach(array $columns, array $extra = []) { foreach ($columns as $column => $amount) { if (! is_numeric($amount)) { throw new InvalidArgumentException("Non-numeric value passed as decrement amount for column: '$column'."); } elseif (! is_string($column)) { throw new InvalidArgumentException('Non-associative array passed to decrementEach method.'); } $columns[$column] = $this->raw("{$this->grammar->wrap($column)} - $amount"); } return $this->update(array_merge($columns, $extra)); } /** * Delete records from the database. * * @param mixed $id * @return int */ public function delete($id = null) { // If an ID is passed to the method, we will set the where clause to check the // ID to let developers to simply and quickly remove a single row from this // database without manually specifying the "where" clauses on the query. if (! is_null($id)) { $this->where($this->from.'.id', '=', $id); } $this->applyBeforeQueryCallbacks(); return $this->connection->delete( $this->grammar->compileDelete($this), $this->cleanBindings( $this->grammar->prepareBindingsForDelete($this->bindings) ) ); } /** * Run a truncate statement on the table. * * @return void */ public function truncate() { $this->applyBeforeQueryCallbacks(); foreach ($this->grammar->compileTruncate($this) as $sql => $bindings) { $this->connection->statement($sql, $bindings); } } /** * Get a new instance of the query builder. * * @return \Illuminate\Database\Query\Builder */ public function newQuery() { return new static($this->connection, $this->grammar, $this->processor); } /** * Create a new query instance for a sub-query. * * @return \Illuminate\Database\Query\Builder */ protected function forSubQuery() { return $this->newQuery(); } /** * Get all of the query builder's columns in a text-only array with all expressions evaluated. * * @return array */ public function getColumns() { return ! is_null($this->columns) ? array_map(fn ($column) => $this->grammar->getValue($column), $this->columns) : []; } /** * Create a raw database expression. * * @param mixed $value * @return \Illuminate\Contracts\Database\Query\Expression */ public function raw($value) { return $this->connection->raw($value); } /** * Get the current query value bindings in a flattened array. * * @return array */ public function getBindings() { return Arr::flatten($this->bindings); } /** * Get the raw array of bindings. * * @return array */ public function getRawBindings() { return $this->bindings; } /** * Set the bindings on the query builder. * * @param array $bindings * @param string $type * @return $this * * @throws \InvalidArgumentException */ public function setBindings(array $bindings, $type = 'where') { if (! array_key_exists($type, $this->bindings)) { throw new InvalidArgumentException("Invalid binding type: {$type}."); } $this->bindings[$type] = $bindings; return $this; } /** * Add a binding to the query. * * @param mixed $value * @param string $type * @return $this * * @throws \InvalidArgumentException */ public function addBinding($value, $type = 'where') { if (! array_key_exists($type, $this->bindings)) { throw new InvalidArgumentException("Invalid binding type: {$type}."); } if (is_array($value)) { $this->bindings[$type] = array_values(array_map( [$this, 'castBinding'], array_merge($this->bindings[$type], $value), )); } else { $this->bindings[$type][] = $this->castBinding($value); } return $this; } /** * Cast the given binding value. * * @param mixed $value * @return mixed */ public function castBinding($value) { return $value instanceof BackedEnum ? $value->value : $value; } /** * Merge an array of bindings into our bindings. * * @param \Illuminate\Database\Query\Builder $query * @return $this */ public function mergeBindings(self $query) { $this->bindings = array_merge_recursive($this->bindings, $query->bindings); return $this; } /** * Remove all of the expressions from a list of bindings. * * @param array $bindings * @return array */ public function cleanBindings(array $bindings) { return collect($bindings) ->reject(function ($binding) { return $binding instanceof ExpressionContract; }) ->map([$this, 'castBinding']) ->values() ->all(); } /** * Get a scalar type value from an unknown type of input. * * @param mixed $value * @return mixed */ protected function flattenValue($value) { return is_array($value) ? head(Arr::flatten($value)) : $value; } /** * Get the default key name of the table. * * @return string */ protected function defaultKeyName() { return 'id'; } /** * Get the database connection instance. * * @return \Illuminate\Database\ConnectionInterface */ public function getConnection() { return $this->connection; } /** * Get the database query processor instance. * * @return \Illuminate\Database\Query\Processors\Processor */ public function getProcessor() { return $this->processor; } /** * Get the query grammar instance. * * @return \Illuminate\Database\Query\Grammars\Grammar */ public function getGrammar() { return $this->grammar; } /** * Use the "write" PDO connection when executing the query. * * @return $this */ public function useWritePdo() { $this->useWritePdo = true; return $this; } /** * Determine if the value is a query builder instance or a Closure. * * @param mixed $value * @return bool */ protected function isQueryable($value) { return $value instanceof self || $value instanceof EloquentBuilder || $value instanceof Relation || $value instanceof Closure; } /** * Clone the query. * * @return static */ public function clone() { return clone $this; } /** * Clone the query without the given properties. * * @param array $properties * @return static */ public function cloneWithout(array $properties) { return tap($this->clone(), function ($clone) use ($properties) { foreach ($properties as $property) { $clone->{$property} = null; } }); } /** * Clone the query without the given bindings. * * @param array $except * @return static */ public function cloneWithoutBindings(array $except) { return tap($this->clone(), function ($clone) use ($except) { foreach ($except as $type) { $clone->bindings[$type] = []; } }); } /** * Dump the current SQL and bindings. * * @return $this */ public function dump() { dump($this->toSql(), $this->getBindings()); return $this; } /** * Dump the raw current SQL with embedded bindings. * * @return $this */ public function dumpRawSql() { dump($this->toRawSql()); return $this; } /** * Die and dump the current SQL and bindings. * * @return never */ public function dd() { dd($this->toSql(), $this->getBindings()); } /** * Die and dump the current SQL with embedded bindings. * * @return never */ public function ddRawSql() { dd($this->toRawSql()); } /** * Handle dynamic method calls into the method. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } if (str_starts_with($method, 'where')) { return $this->dynamicWhere($method, $parameters); } static::throwBadMethodCallException($method); } } src/Illuminate/Database/Query/Expression.php 0000644 00000001371 15107327037 0015126 0 ustar 00 <?php namespace Illuminate\Database\Query; use Illuminate\Contracts\Database\Query\Expression as ExpressionContract; use Illuminate\Database\Grammar; class Expression implements ExpressionContract { /** * The value of the expression. * * @var string|int|float */ protected $value; /** * Create a new raw query expression. * * @param string|int|float $value * @return void */ public function __construct($value) { $this->value = $value; } /** * Get the value of the expression. * * @param \Illuminate\Database\Grammar $grammar * @return string|int|float */ public function getValue(Grammar $grammar) { return $this->value; } } src/Illuminate/Database/MySqlConnection.php 0000644 00000006074 15107327037 0014754 0 ustar 00 <?php namespace Illuminate\Database; use Exception; use Illuminate\Database\PDO\MySqlDriver; use Illuminate\Database\Query\Grammars\MySqlGrammar as QueryGrammar; use Illuminate\Database\Query\Processors\MySqlProcessor; use Illuminate\Database\Schema\Grammars\MySqlGrammar as SchemaGrammar; use Illuminate\Database\Schema\MySqlBuilder; use Illuminate\Database\Schema\MySqlSchemaState; use Illuminate\Filesystem\Filesystem; use PDO; class MySqlConnection extends Connection { /** * Escape a binary value for safe SQL embedding. * * @param string $value * @return string */ protected function escapeBinary($value) { $hex = bin2hex($value); return "x'{$hex}'"; } /** * Determine if the given database exception was caused by a unique constraint violation. * * @param \Exception $exception * @return bool */ protected function isUniqueConstraintError(Exception $exception) { return boolval(preg_match('#Integrity constraint violation: 1062#i', $exception->getMessage())); } /** * Determine if the connected database is a MariaDB database. * * @return bool */ public function isMaria() { return str_contains($this->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), 'MariaDB'); } /** * Get the default query grammar instance. * * @return \Illuminate\Database\Query\Grammars\MySqlGrammar */ protected function getDefaultQueryGrammar() { ($grammar = new QueryGrammar)->setConnection($this); return $this->withTablePrefix($grammar); } /** * Get a schema builder instance for the connection. * * @return \Illuminate\Database\Schema\MySqlBuilder */ public function getSchemaBuilder() { if (is_null($this->schemaGrammar)) { $this->useDefaultSchemaGrammar(); } return new MySqlBuilder($this); } /** * Get the default schema grammar instance. * * @return \Illuminate\Database\Schema\Grammars\MySqlGrammar */ protected function getDefaultSchemaGrammar() { ($grammar = new SchemaGrammar)->setConnection($this); return $this->withTablePrefix($grammar); } /** * Get the schema state for the connection. * * @param \Illuminate\Filesystem\Filesystem|null $files * @param callable|null $processFactory * @return \Illuminate\Database\Schema\MySqlSchemaState */ public function getSchemaState(Filesystem $files = null, callable $processFactory = null) { return new MySqlSchemaState($this, $files, $processFactory); } /** * Get the default post processor instance. * * @return \Illuminate\Database\Query\Processors\MySqlProcessor */ protected function getDefaultPostProcessor() { return new MySqlProcessor; } /** * Get the Doctrine DBAL driver. * * @return \Illuminate\Database\PDO\MySqlDriver */ protected function getDoctrineDriver() { return new MySqlDriver; } } src/Illuminate/Database/Migrations/Migration.php 0000644 00000001025 15107327037 0015723 0 ustar 00 <?php namespace Illuminate\Database\Migrations; abstract class Migration { /** * The name of the database connection to use. * * @var string|null */ protected $connection; /** * Enables, if supported, wrapping the migration within a transaction. * * @var bool */ public $withinTransaction = true; /** * Get the migration connection name. * * @return string|null */ public function getConnection() { return $this->connection; } } src/Illuminate/Database/Migrations/DatabaseMigrationRepository.php 0000644 00000012570 15107327037 0021457 0 ustar 00 <?php namespace Illuminate\Database\Migrations; use Illuminate\Database\ConnectionResolverInterface as Resolver; class DatabaseMigrationRepository implements MigrationRepositoryInterface { /** * The database connection resolver instance. * * @var \Illuminate\Database\ConnectionResolverInterface */ protected $resolver; /** * The name of the migration table. * * @var string */ protected $table; /** * The name of the database connection to use. * * @var string */ protected $connection; /** * Create a new database migration repository instance. * * @param \Illuminate\Database\ConnectionResolverInterface $resolver * @param string $table * @return void */ public function __construct(Resolver $resolver, $table) { $this->table = $table; $this->resolver = $resolver; } /** * Get the completed migrations. * * @return array */ public function getRan() { return $this->table() ->orderBy('batch', 'asc') ->orderBy('migration', 'asc') ->pluck('migration')->all(); } /** * Get the list of migrations. * * @param int $steps * @return array */ public function getMigrations($steps) { $query = $this->table()->where('batch', '>=', '1'); return $query->orderBy('batch', 'desc') ->orderBy('migration', 'desc') ->take($steps)->get()->all(); } /** * Get the list of the migrations by batch number. * * @param int $batchNumber * @return array */ public function getMigrationsByBatch($batch) { return $this->table() ->where('batch', $batch) ->orderBy('migration', 'desc') ->get() ->all(); } /** * Get the last migration batch. * * @return array */ public function getLast() { $query = $this->table()->where('batch', $this->getLastBatchNumber()); return $query->orderBy('migration', 'desc')->get()->all(); } /** * Get the completed migrations with their batch numbers. * * @return array */ public function getMigrationBatches() { return $this->table() ->orderBy('batch', 'asc') ->orderBy('migration', 'asc') ->pluck('batch', 'migration')->all(); } /** * Log that a migration was run. * * @param string $file * @param int $batch * @return void */ public function log($file, $batch) { $record = ['migration' => $file, 'batch' => $batch]; $this->table()->insert($record); } /** * Remove a migration from the log. * * @param object $migration * @return void */ public function delete($migration) { $this->table()->where('migration', $migration->migration)->delete(); } /** * Get the next migration batch number. * * @return int */ public function getNextBatchNumber() { return $this->getLastBatchNumber() + 1; } /** * Get the last migration batch number. * * @return int */ public function getLastBatchNumber() { return $this->table()->max('batch'); } /** * Create the migration repository data store. * * @return void */ public function createRepository() { $schema = $this->getConnection()->getSchemaBuilder(); $schema->create($this->table, function ($table) { // The migrations table is responsible for keeping track of which of the // migrations have actually run for the application. We'll create the // table to hold the migration file's path as well as the batch ID. $table->increments('id'); $table->string('migration'); $table->integer('batch'); }); } /** * Determine if the migration repository exists. * * @return bool */ public function repositoryExists() { $schema = $this->getConnection()->getSchemaBuilder(); return $schema->hasTable($this->table); } /** * Delete the migration repository data store. * * @return void */ public function deleteRepository() { $schema = $this->getConnection()->getSchemaBuilder(); $schema->drop($this->table); } /** * Get a query builder for the migration table. * * @return \Illuminate\Database\Query\Builder */ protected function table() { return $this->getConnection()->table($this->table)->useWritePdo(); } /** * Get the connection resolver instance. * * @return \Illuminate\Database\ConnectionResolverInterface */ public function getConnectionResolver() { return $this->resolver; } /** * Resolve the database connection instance. * * @return \Illuminate\Database\Connection */ public function getConnection() { return $this->resolver->connection($this->connection); } /** * Set the information source to gather data. * * @param string $name * @return void */ public function setSource($name) { $this->connection = $name; } } src/Illuminate/Database/Migrations/MigrationRepositoryInterface.php 0000644 00000003433 15107327037 0021651 0 ustar 00 <?php namespace Illuminate\Database\Migrations; interface MigrationRepositoryInterface { /** * Get the completed migrations. * * @return array */ public function getRan(); /** * Get the list of migrations. * * @param int $steps * @return array */ public function getMigrations($steps); /** * Get the list of the migrations by batch. * * @param int $batch * @return array */ public function getMigrationsByBatch($batch); /** * Get the last migration batch. * * @return array */ public function getLast(); /** * Get the completed migrations with their batch numbers. * * @return array */ public function getMigrationBatches(); /** * Log that a migration was run. * * @param string $file * @param int $batch * @return void */ public function log($file, $batch); /** * Remove a migration from the log. * * @param object $migration * @return void */ public function delete($migration); /** * Get the next migration batch number. * * @return int */ public function getNextBatchNumber(); /** * Create the migration repository data store. * * @return void */ public function createRepository(); /** * Determine if the migration repository exists. * * @return bool */ public function repositoryExists(); /** * Delete the migration repository data store. * * @return void */ public function deleteRepository(); /** * Set the information source to gather data. * * @param string $name * @return void */ public function setSource($name); } src/Illuminate/Database/Migrations/MigrationCreator.php 0000644 00000013675 15107327037 0017261 0 ustar 00 <?php namespace Illuminate\Database\Migrations; use Closure; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Str; use InvalidArgumentException; class MigrationCreator { /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * The custom app stubs directory. * * @var string */ protected $customStubPath; /** * The registered post create hooks. * * @var array */ protected $postCreate = []; /** * Create a new migration creator instance. * * @param \Illuminate\Filesystem\Filesystem $files * @param string $customStubPath * @return void */ public function __construct(Filesystem $files, $customStubPath) { $this->files = $files; $this->customStubPath = $customStubPath; } /** * Create a new migration at the given path. * * @param string $name * @param string $path * @param string|null $table * @param bool $create * @return string * * @throws \Exception */ public function create($name, $path, $table = null, $create = false) { $this->ensureMigrationDoesntAlreadyExist($name, $path); // First we will get the stub file for the migration, which serves as a type // of template for the migration. Once we have those we will populate the // various place-holders, save the file, and run the post create event. $stub = $this->getStub($table, $create); $path = $this->getPath($name, $path); $this->files->ensureDirectoryExists(dirname($path)); $this->files->put( $path, $this->populateStub($stub, $table) ); // Next, we will fire any hooks that are supposed to fire after a migration is // created. Once that is done we'll be ready to return the full path to the // migration file so it can be used however it's needed by the developer. $this->firePostCreateHooks($table, $path); return $path; } /** * Ensure that a migration with the given name doesn't already exist. * * @param string $name * @param string $migrationPath * @return void * * @throws \InvalidArgumentException */ protected function ensureMigrationDoesntAlreadyExist($name, $migrationPath = null) { if (! empty($migrationPath)) { $migrationFiles = $this->files->glob($migrationPath.'/*.php'); foreach ($migrationFiles as $migrationFile) { $this->files->requireOnce($migrationFile); } } if (class_exists($className = $this->getClassName($name))) { throw new InvalidArgumentException("A {$className} class already exists."); } } /** * Get the migration stub file. * * @param string|null $table * @param bool $create * @return string */ protected function getStub($table, $create) { if (is_null($table)) { $stub = $this->files->exists($customPath = $this->customStubPath.'/migration.stub') ? $customPath : $this->stubPath().'/migration.stub'; } elseif ($create) { $stub = $this->files->exists($customPath = $this->customStubPath.'/migration.create.stub') ? $customPath : $this->stubPath().'/migration.create.stub'; } else { $stub = $this->files->exists($customPath = $this->customStubPath.'/migration.update.stub') ? $customPath : $this->stubPath().'/migration.update.stub'; } return $this->files->get($stub); } /** * Populate the place-holders in the migration stub. * * @param string $stub * @param string|null $table * @return string */ protected function populateStub($stub, $table) { // Here we will replace the table place-holders with the table specified by // the developer, which is useful for quickly creating a tables creation // or update migration from the console instead of typing it manually. if (! is_null($table)) { $stub = str_replace( ['DummyTable', '{{ table }}', '{{table}}'], $table, $stub ); } return $stub; } /** * Get the class name of a migration name. * * @param string $name * @return string */ protected function getClassName($name) { return Str::studly($name); } /** * Get the full path to the migration. * * @param string $name * @param string $path * @return string */ protected function getPath($name, $path) { return $path.'/'.$this->getDatePrefix().'_'.$name.'.php'; } /** * Fire the registered post create hooks. * * @param string|null $table * @param string $path * @return void */ protected function firePostCreateHooks($table, $path) { foreach ($this->postCreate as $callback) { $callback($table, $path); } } /** * Register a post migration create hook. * * @param \Closure $callback * @return void */ public function afterCreate(Closure $callback) { $this->postCreate[] = $callback; } /** * Get the date prefix for the migration. * * @return string */ protected function getDatePrefix() { return date('Y_m_d_His'); } /** * Get the path to the stubs. * * @return string */ public function stubPath() { return __DIR__.'/stubs'; } /** * Get the filesystem instance. * * @return \Illuminate\Filesystem\Filesystem */ public function getFilesystem() { return $this->files; } } src/Illuminate/Database/Migrations/Migrator.php 0000644 00000054510 15107327037 0015565 0 ustar 00 <?php namespace Illuminate\Database\Migrations; use Doctrine\DBAL\Schema\SchemaException; use Illuminate\Console\View\Components\BulletList; use Illuminate\Console\View\Components\Error; use Illuminate\Console\View\Components\Info; use Illuminate\Console\View\Components\Task; use Illuminate\Console\View\Components\TwoColumnDetail; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\ConnectionResolverInterface as Resolver; use Illuminate\Database\Events\MigrationEnded; use Illuminate\Database\Events\MigrationsEnded; use Illuminate\Database\Events\MigrationsStarted; use Illuminate\Database\Events\MigrationStarted; use Illuminate\Database\Events\NoPendingMigrations; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Str; use ReflectionClass; use Symfony\Component\Console\Output\OutputInterface; class Migrator { /** * The event dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * The migration repository implementation. * * @var \Illuminate\Database\Migrations\MigrationRepositoryInterface */ protected $repository; /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * The connection resolver instance. * * @var \Illuminate\Database\ConnectionResolverInterface */ protected $resolver; /** * The name of the default connection. * * @var string */ protected $connection; /** * The paths to all of the migration files. * * @var array */ protected $paths = []; /** * The paths that have already been required. * * @var array<string, \Illuminate\Database\Migrations\Migration|null> */ protected static $requiredPathCache = []; /** * The output interface implementation. * * @var \Symfony\Component\Console\Output\OutputInterface */ protected $output; /** * Create a new migrator instance. * * @param \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository * @param \Illuminate\Database\ConnectionResolverInterface $resolver * @param \Illuminate\Filesystem\Filesystem $files * @param \Illuminate\Contracts\Events\Dispatcher|null $dispatcher * @return void */ public function __construct(MigrationRepositoryInterface $repository, Resolver $resolver, Filesystem $files, Dispatcher $dispatcher = null) { $this->files = $files; $this->events = $dispatcher; $this->resolver = $resolver; $this->repository = $repository; } /** * Run the pending migrations at a given path. * * @param array|string $paths * @param array $options * @return array */ public function run($paths = [], array $options = []) { // Once we grab all of the migration files for the path, we will compare them // against the migrations that have already been run for this package then // run each of the outstanding migrations against a database connection. $files = $this->getMigrationFiles($paths); $this->requireFiles($migrations = $this->pendingMigrations( $files, $this->repository->getRan() )); // Once we have all these migrations that are outstanding we are ready to run // we will go ahead and run them "up". This will execute each migration as // an operation against a database. Then we'll return this list of them. $this->runPending($migrations, $options); return $migrations; } /** * Get the migration files that have not yet run. * * @param array $files * @param array $ran * @return array */ protected function pendingMigrations($files, $ran) { return Collection::make($files) ->reject(function ($file) use ($ran) { return in_array($this->getMigrationName($file), $ran); })->values()->all(); } /** * Run an array of migrations. * * @param array $migrations * @param array $options * @return void */ public function runPending(array $migrations, array $options = []) { // First we will just make sure that there are any migrations to run. If there // aren't, we will just make a note of it to the developer so they're aware // that all of the migrations have been run against this database system. if (count($migrations) === 0) { $this->fireMigrationEvent(new NoPendingMigrations('up')); $this->write(Info::class, 'Nothing to migrate'); return; } // Next, we will get the next batch number for the migrations so we can insert // correct batch number in the database migrations repository when we store // each migration's execution. We will also extract a few of the options. $batch = $this->repository->getNextBatchNumber(); $pretend = $options['pretend'] ?? false; $step = $options['step'] ?? false; $this->fireMigrationEvent(new MigrationsStarted('up')); $this->write(Info::class, 'Running migrations.'); // Once we have the array of migrations, we will spin through them and run the // migrations "up" so the changes are made to the databases. We'll then log // that the migration was run so we don't repeat it next time we execute. foreach ($migrations as $file) { $this->runUp($file, $batch, $pretend); if ($step) { $batch++; } } $this->fireMigrationEvent(new MigrationsEnded('up')); if ($this->output) { $this->output->writeln(''); } } /** * Run "up" a migration instance. * * @param string $file * @param int $batch * @param bool $pretend * @return void */ protected function runUp($file, $batch, $pretend) { // First we will resolve a "real" instance of the migration class from this // migration file name. Once we have the instances we can run the actual // command such as "up" or "down", or we can just simulate the action. $migration = $this->resolvePath($file); $name = $this->getMigrationName($file); if ($pretend) { return $this->pretendToRun($migration, 'up'); } $this->write(Task::class, $name, fn () => $this->runMigration($migration, 'up')); // Once we have run a migrations class, we will log that it was run in this // repository so that we don't try to run it next time we do a migration // in the application. A migration repository keeps the migrate order. $this->repository->log($name, $batch); } /** * Rollback the last migration operation. * * @param array|string $paths * @param array $options * @return array */ public function rollback($paths = [], array $options = []) { // We want to pull in the last batch of migrations that ran on the previous // migration operation. We'll then reverse those migrations and run each // of them "down" to reverse the last migration "operation" which ran. $migrations = $this->getMigrationsForRollback($options); if (count($migrations) === 0) { $this->fireMigrationEvent(new NoPendingMigrations('down')); $this->write(Info::class, 'Nothing to rollback.'); return []; } return tap($this->rollbackMigrations($migrations, $paths, $options), function () { if ($this->output) { $this->output->writeln(''); } }); } /** * Get the migrations for a rollback operation. * * @param array $options * @return array */ protected function getMigrationsForRollback(array $options) { if (($steps = $options['step'] ?? 0) > 0) { return $this->repository->getMigrations($steps); } if (($batch = $options['batch'] ?? 0) > 0) { return $this->repository->getMigrationsByBatch($batch); } return $this->repository->getLast(); } /** * Rollback the given migrations. * * @param array $migrations * @param array|string $paths * @param array $options * @return array */ protected function rollbackMigrations(array $migrations, $paths, array $options) { $rolledBack = []; $this->requireFiles($files = $this->getMigrationFiles($paths)); $this->fireMigrationEvent(new MigrationsStarted('down')); $this->write(Info::class, 'Rolling back migrations.'); // Next we will run through all of the migrations and call the "down" method // which will reverse each migration in order. This getLast method on the // repository already returns these migration's names in reverse order. foreach ($migrations as $migration) { $migration = (object) $migration; if (! $file = Arr::get($files, $migration->migration)) { $this->write(TwoColumnDetail::class, $migration->migration, '<fg=yellow;options=bold>Migration not found</>'); continue; } $rolledBack[] = $file; $this->runDown( $file, $migration, $options['pretend'] ?? false ); } $this->fireMigrationEvent(new MigrationsEnded('down')); return $rolledBack; } /** * Rolls all of the currently applied migrations back. * * @param array|string $paths * @param bool $pretend * @return array */ public function reset($paths = [], $pretend = false) { // Next, we will reverse the migration list so we can run them back in the // correct order for resetting this database. This will allow us to get // the database back into its "empty" state ready for the migrations. $migrations = array_reverse($this->repository->getRan()); if (count($migrations) === 0) { $this->write(Info::class, 'Nothing to rollback.'); return []; } return tap($this->resetMigrations($migrations, Arr::wrap($paths), $pretend), function () { if ($this->output) { $this->output->writeln(''); } }); } /** * Reset the given migrations. * * @param array $migrations * @param array $paths * @param bool $pretend * @return array */ protected function resetMigrations(array $migrations, array $paths, $pretend = false) { // Since the getRan method that retrieves the migration name just gives us the // migration name, we will format the names into objects with the name as a // property on the objects so that we can pass it to the rollback method. $migrations = collect($migrations)->map(function ($m) { return (object) ['migration' => $m]; })->all(); return $this->rollbackMigrations( $migrations, $paths, compact('pretend') ); } /** * Run "down" a migration instance. * * @param string $file * @param object $migration * @param bool $pretend * @return void */ protected function runDown($file, $migration, $pretend) { // First we will get the file name of the migration so we can resolve out an // instance of the migration. Once we get an instance we can either run a // pretend execution of the migration or we can run the real migration. $instance = $this->resolvePath($file); $name = $this->getMigrationName($file); if ($pretend) { return $this->pretendToRun($instance, 'down'); } $this->write(Task::class, $name, fn () => $this->runMigration($instance, 'down')); // Once we have successfully run the migration "down" we will remove it from // the migration repository so it will be considered to have not been run // by the application then will be able to fire by any later operation. $this->repository->delete($migration); } /** * Run a migration inside a transaction if the database supports it. * * @param object $migration * @param string $method * @return void */ protected function runMigration($migration, $method) { $connection = $this->resolveConnection( $migration->getConnection() ); $callback = function () use ($connection, $migration, $method) { if (method_exists($migration, $method)) { $this->fireMigrationEvent(new MigrationStarted($migration, $method)); $this->runMethod($connection, $migration, $method); $this->fireMigrationEvent(new MigrationEnded($migration, $method)); } }; $this->getSchemaGrammar($connection)->supportsSchemaTransactions() && $migration->withinTransaction ? $connection->transaction($callback) : $callback(); } /** * Pretend to run the migrations. * * @param object $migration * @param string $method * @return void */ protected function pretendToRun($migration, $method) { try { $name = get_class($migration); $reflectionClass = new ReflectionClass($migration); if ($reflectionClass->isAnonymous()) { $name = $this->getMigrationName($reflectionClass->getFileName()); } $this->write(TwoColumnDetail::class, $name); $this->write(BulletList::class, collect($this->getQueries($migration, $method))->map(function ($query) { return $query['query']; })); } catch (SchemaException) { $name = get_class($migration); $this->write(Error::class, sprintf( '[%s] failed to dump queries. This may be due to changing database columns using Doctrine, which is not supported while pretending to run migrations.', $name, )); } } /** * Get all of the queries that would be run for a migration. * * @param object $migration * @param string $method * @return array */ protected function getQueries($migration, $method) { // Now that we have the connections we can resolve it and pretend to run the // queries against the database returning the array of raw SQL statements // that would get fired against the database system for this migration. $db = $this->resolveConnection( $migration->getConnection() ); return $db->pretend(function () use ($db, $migration, $method) { if (method_exists($migration, $method)) { $this->runMethod($db, $migration, $method); } }); } /** * Run a migration method on the given connection. * * @param \Illuminate\Database\Connection $connection * @param object $migration * @param string $method * @return void */ protected function runMethod($connection, $migration, $method) { $previousConnection = $this->resolver->getDefaultConnection(); try { $this->resolver->setDefaultConnection($connection->getName()); $migration->{$method}(); } finally { $this->resolver->setDefaultConnection($previousConnection); } } /** * Resolve a migration instance from a file. * * @param string $file * @return object */ public function resolve($file) { $class = $this->getMigrationClass($file); return new $class; } /** * Resolve a migration instance from a migration path. * * @param string $path * @return object */ protected function resolvePath(string $path) { $class = $this->getMigrationClass($this->getMigrationName($path)); if (class_exists($class) && realpath($path) == (new ReflectionClass($class))->getFileName()) { return new $class; } $migration = static::$requiredPathCache[$path] ??= $this->files->getRequire($path); if (is_object($migration)) { return method_exists($migration, '__construct') ? $this->files->getRequire($path) : clone $migration; } return new $class; } /** * Generate a migration class name based on the migration file name. * * @param string $migrationName * @return string */ protected function getMigrationClass(string $migrationName): string { return Str::studly(implode('_', array_slice(explode('_', $migrationName), 4))); } /** * Get all of the migration files in a given path. * * @param string|array $paths * @return array */ public function getMigrationFiles($paths) { return Collection::make($paths)->flatMap(function ($path) { return str_ends_with($path, '.php') ? [$path] : $this->files->glob($path.'/*_*.php'); })->filter()->values()->keyBy(function ($file) { return $this->getMigrationName($file); })->sortBy(function ($file, $key) { return $key; })->all(); } /** * Require in all the migration files in a given path. * * @param array $files * @return void */ public function requireFiles(array $files) { foreach ($files as $file) { $this->files->requireOnce($file); } } /** * Get the name of the migration. * * @param string $path * @return string */ public function getMigrationName($path) { return str_replace('.php', '', basename($path)); } /** * Register a custom migration path. * * @param string $path * @return void */ public function path($path) { $this->paths = array_unique(array_merge($this->paths, [$path])); } /** * Get all of the custom migration paths. * * @return array */ public function paths() { return $this->paths; } /** * Get the default connection name. * * @return string */ public function getConnection() { return $this->connection; } /** * Execute the given callback using the given connection as the default connection. * * @param string $name * @param callable $callback * @return mixed */ public function usingConnection($name, callable $callback) { $previousConnection = $this->resolver->getDefaultConnection(); $this->setConnection($name); return tap($callback(), function () use ($previousConnection) { $this->setConnection($previousConnection); }); } /** * Set the default connection name. * * @param string $name * @return void */ public function setConnection($name) { if (! is_null($name)) { $this->resolver->setDefaultConnection($name); } $this->repository->setSource($name); $this->connection = $name; } /** * Resolve the database connection instance. * * @param string $connection * @return \Illuminate\Database\Connection */ public function resolveConnection($connection) { return $this->resolver->connection($connection ?: $this->connection); } /** * Get the schema grammar out of a migration connection. * * @param \Illuminate\Database\Connection $connection * @return \Illuminate\Database\Schema\Grammars\Grammar */ protected function getSchemaGrammar($connection) { if (is_null($grammar = $connection->getSchemaGrammar())) { $connection->useDefaultSchemaGrammar(); $grammar = $connection->getSchemaGrammar(); } return $grammar; } /** * Get the migration repository instance. * * @return \Illuminate\Database\Migrations\MigrationRepositoryInterface */ public function getRepository() { return $this->repository; } /** * Determine if the migration repository exists. * * @return bool */ public function repositoryExists() { return $this->repository->repositoryExists(); } /** * Determine if any migrations have been run. * * @return bool */ public function hasRunAnyMigrations() { return $this->repositoryExists() && count($this->repository->getRan()) > 0; } /** * Delete the migration repository data store. * * @return void */ public function deleteRepository() { $this->repository->deleteRepository(); } /** * Get the file system instance. * * @return \Illuminate\Filesystem\Filesystem */ public function getFilesystem() { return $this->files; } /** * Set the output implementation that should be used by the console. * * @param \Symfony\Component\Console\Output\OutputInterface $output * @return $this */ public function setOutput(OutputInterface $output) { $this->output = $output; return $this; } /** * Write to the console's output. * * @param string $component * @param array<int, string>|string ...$arguments * @return void */ protected function write($component, ...$arguments) { if ($this->output && class_exists($component)) { (new $component($this->output))->render(...$arguments); } else { foreach ($arguments as $argument) { if (is_callable($argument)) { $argument(); } } } } /** * Fire the given event for the migration. * * @param \Illuminate\Contracts\Database\Events\MigrationEvent $event * @return void */ public function fireMigrationEvent($event) { if ($this->events) { $this->events->dispatch($event); } } } src/Illuminate/Database/Migrations/stubs/migration.stub 0000644 00000000570 15107327037 0017315 0 ustar 00 <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { // } /** * Reverse the migrations. */ public function down(): void { // } }; src/Illuminate/Database/Migrations/stubs/migration.create.stub 0000644 00000001033 15107327037 0020552 0 ustar 00 <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('{{ table }}', function (Blueprint $table) { $table->id(); $table->timestamps(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('{{ table }}'); } }; src/Illuminate/Database/Migrations/stubs/migration.update.stub 0000644 00000001036 15107327037 0020574 0 ustar 00 <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::table('{{ table }}', function (Blueprint $table) { // }); } /** * Reverse the migrations. */ public function down(): void { Schema::table('{{ table }}', function (Blueprint $table) { // }); } }; src/Illuminate/Database/DatabaseManager.php 0000644 00000031435 15107327037 0014665 0 ustar 00 <?php namespace Illuminate\Database; use Doctrine\DBAL\Types\Type; use Illuminate\Database\Connectors\ConnectionFactory; use Illuminate\Database\Events\ConnectionEstablished; use Illuminate\Support\Arr; use Illuminate\Support\ConfigurationUrlParser; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; use PDO; use RuntimeException; /** * @mixin \Illuminate\Database\Connection */ class DatabaseManager implements ConnectionResolverInterface { use Macroable { __call as macroCall; } /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The database connection factory instance. * * @var \Illuminate\Database\Connectors\ConnectionFactory */ protected $factory; /** * The active connection instances. * * @var array<string, \Illuminate\Database\Connection> */ protected $connections = []; /** * The custom connection resolvers. * * @var array<string, callable> */ protected $extensions = []; /** * The callback to be executed to reconnect to a database. * * @var callable */ protected $reconnector; /** * The custom Doctrine column types. * * @var array<string, array> */ protected $doctrineTypes = []; /** * Create a new database manager instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @param \Illuminate\Database\Connectors\ConnectionFactory $factory * @return void */ public function __construct($app, ConnectionFactory $factory) { $this->app = $app; $this->factory = $factory; $this->reconnector = function ($connection) { $this->reconnect($connection->getNameWithReadWriteType()); }; } /** * Get a database connection instance. * * @param string|null $name * @return \Illuminate\Database\Connection */ public function connection($name = null) { [$database, $type] = $this->parseConnectionName($name); $name = $name ?: $database; // If we haven't created this connection, we'll create it based on the config // provided in the application. Once we've created the connections we will // set the "fetch mode" for PDO which determines the query return types. if (! isset($this->connections[$name])) { $this->connections[$name] = $this->configure( $this->makeConnection($database), $type ); if ($this->app->bound('events')) { $this->app['events']->dispatch( new ConnectionEstablished($this->connections[$name]) ); } } return $this->connections[$name]; } /** * Parse the connection into an array of the name and read / write type. * * @param string $name * @return array */ protected function parseConnectionName($name) { $name = $name ?: $this->getDefaultConnection(); return Str::endsWith($name, ['::read', '::write']) ? explode('::', $name, 2) : [$name, null]; } /** * Make the database connection instance. * * @param string $name * @return \Illuminate\Database\Connection */ protected function makeConnection($name) { $config = $this->configuration($name); // First we will check by the connection name to see if an extension has been // registered specifically for that connection. If it has we will call the // Closure and pass it the config allowing it to resolve the connection. if (isset($this->extensions[$name])) { return call_user_func($this->extensions[$name], $config, $name); } // Next we will check to see if an extension has been registered for a driver // and will call the Closure if so, which allows us to have a more generic // resolver for the drivers themselves which applies to all connections. if (isset($this->extensions[$driver = $config['driver']])) { return call_user_func($this->extensions[$driver], $config, $name); } return $this->factory->make($config, $name); } /** * Get the configuration for a connection. * * @param string $name * @return array * * @throws \InvalidArgumentException */ protected function configuration($name) { $name = $name ?: $this->getDefaultConnection(); // To get the database connection configuration, we will just pull each of the // connection configurations and get the configurations for the given name. // If the configuration doesn't exist, we'll throw an exception and bail. $connections = $this->app['config']['database.connections']; if (is_null($config = Arr::get($connections, $name))) { throw new InvalidArgumentException("Database connection [{$name}] not configured."); } return (new ConfigurationUrlParser) ->parseConfiguration($config); } /** * Prepare the database connection instance. * * @param \Illuminate\Database\Connection $connection * @param string $type * @return \Illuminate\Database\Connection */ protected function configure(Connection $connection, $type) { $connection = $this->setPdoForType($connection, $type)->setReadWriteType($type); // First we'll set the fetch mode and a few other dependencies of the database // connection. This method basically just configures and prepares it to get // used by the application. Once we're finished we'll return it back out. if ($this->app->bound('events')) { $connection->setEventDispatcher($this->app['events']); } if ($this->app->bound('db.transactions')) { $connection->setTransactionManager($this->app['db.transactions']); } // Here we'll set a reconnector callback. This reconnector can be any callable // so we will set a Closure to reconnect from this manager with the name of // the connection, which will allow us to reconnect from the connections. $connection->setReconnector($this->reconnector); $this->registerConfiguredDoctrineTypes($connection); return $connection; } /** * Prepare the read / write mode for database connection instance. * * @param \Illuminate\Database\Connection $connection * @param string|null $type * @return \Illuminate\Database\Connection */ protected function setPdoForType(Connection $connection, $type = null) { if ($type === 'read') { $connection->setPdo($connection->getReadPdo()); } elseif ($type === 'write') { $connection->setReadPdo($connection->getPdo()); } return $connection; } /** * Register custom Doctrine types with the connection. * * @param \Illuminate\Database\Connection $connection * @return void */ protected function registerConfiguredDoctrineTypes(Connection $connection): void { foreach ($this->app['config']->get('database.dbal.types', []) as $name => $class) { $this->registerDoctrineType($class, $name, $name); } foreach ($this->doctrineTypes as $name => [$type, $class]) { $connection->registerDoctrineType($class, $name, $type); } } /** * Register a custom Doctrine type. * * @param string $class * @param string $name * @param string $type * @return void * * @throws \Doctrine\DBAL\Exception * @throws \RuntimeException */ public function registerDoctrineType(string $class, string $name, string $type): void { if (! class_exists('Doctrine\DBAL\Connection')) { throw new RuntimeException( 'Registering a custom Doctrine type requires Doctrine DBAL (doctrine/dbal).' ); } if (! Type::hasType($name)) { Type::addType($name, $class); } $this->doctrineTypes[$name] = [$type, $class]; } /** * Disconnect from the given database and remove from local cache. * * @param string|null $name * @return void */ public function purge($name = null) { $name = $name ?: $this->getDefaultConnection(); $this->disconnect($name); unset($this->connections[$name]); } /** * Disconnect from the given database. * * @param string|null $name * @return void */ public function disconnect($name = null) { if (isset($this->connections[$name = $name ?: $this->getDefaultConnection()])) { $this->connections[$name]->disconnect(); } } /** * Reconnect to the given database. * * @param string|null $name * @return \Illuminate\Database\Connection */ public function reconnect($name = null) { $this->disconnect($name = $name ?: $this->getDefaultConnection()); if (! isset($this->connections[$name])) { return $this->connection($name); } return $this->refreshPdoConnections($name); } /** * Set the default database connection for the callback execution. * * @param string $name * @param callable $callback * @return mixed */ public function usingConnection($name, callable $callback) { $previousName = $this->getDefaultConnection(); $this->setDefaultConnection($name); return tap($callback(), function () use ($previousName) { $this->setDefaultConnection($previousName); }); } /** * Refresh the PDO connections on a given connection. * * @param string $name * @return \Illuminate\Database\Connection */ protected function refreshPdoConnections($name) { [$database, $type] = $this->parseConnectionName($name); $fresh = $this->configure( $this->makeConnection($database), $type ); return $this->connections[$name] ->setPdo($fresh->getRawPdo()) ->setReadPdo($fresh->getRawReadPdo()); } /** * Get the default connection name. * * @return string */ public function getDefaultConnection() { return $this->app['config']['database.default']; } /** * Set the default connection name. * * @param string $name * @return void */ public function setDefaultConnection($name) { $this->app['config']['database.default'] = $name; } /** * Get all of the support drivers. * * @return string[] */ public function supportedDrivers() { return ['mysql', 'pgsql', 'sqlite', 'sqlsrv']; } /** * Get all of the drivers that are actually available. * * @return string[] */ public function availableDrivers() { return array_intersect( $this->supportedDrivers(), str_replace('dblib', 'sqlsrv', PDO::getAvailableDrivers()) ); } /** * Register an extension connection resolver. * * @param string $name * @param callable $resolver * @return void */ public function extend($name, callable $resolver) { $this->extensions[$name] = $resolver; } /** * Remove an extension connection resolver. * * @param string $name * @return void */ public function forgetExtension($name) { unset($this->extensions[$name]); } /** * Return all of the created connections. * * @return array<string, \Illuminate\Database\Connection> */ public function getConnections() { return $this->connections; } /** * Set the database reconnector callback. * * @param callable $reconnector * @return void */ public function setReconnector(callable $reconnector) { $this->reconnector = $reconnector; } /** * Set the application instance used by the manager. * * @param \Illuminate\Contracts\Foundation\Application $app * @return $this */ public function setApplication($app) { $this->app = $app; return $this; } /** * Dynamically pass methods to the default connection. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } return $this->connection()->$method(...$parameters); } } src/Illuminate/Database/ClassMorphViolationException.php 0000644 00000001015 15107327037 0017474 0 ustar 00 <?php namespace Illuminate\Database; use RuntimeException; class ClassMorphViolationException extends RuntimeException { /** * The name of the affected Eloquent model. * * @var string */ public $model; /** * Create a new exception instance. * * @param object $model */ public function __construct($model) { $class = get_class($model); parent::__construct("No morph map defined for model [{$class}]."); $this->model = $class; } } src/Illuminate/Database/Connection.php 0000644 00000126772 15107327037 0013776 0 ustar 00 <?php namespace Illuminate\Database; use Carbon\CarbonInterval; use Closure; use DateTimeInterface; use Doctrine\DBAL\Connection as DoctrineConnection; use Doctrine\DBAL\Types\Type; use Exception; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Events\QueryExecuted; use Illuminate\Database\Events\StatementPrepared; use Illuminate\Database\Events\TransactionBeginning; use Illuminate\Database\Events\TransactionCommitted; use Illuminate\Database\Events\TransactionCommitting; use Illuminate\Database\Events\TransactionRolledBack; use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Query\Expression; use Illuminate\Database\Query\Grammars\Grammar as QueryGrammar; use Illuminate\Database\Query\Processors\Processor; use Illuminate\Database\Schema\Builder as SchemaBuilder; use Illuminate\Support\Arr; use Illuminate\Support\InteractsWithTime; use Illuminate\Support\Traits\Macroable; use PDO; use PDOStatement; use RuntimeException; class Connection implements ConnectionInterface { use DetectsConcurrencyErrors, DetectsLostConnections, Concerns\ManagesTransactions, InteractsWithTime, Macroable; /** * The active PDO connection. * * @var \PDO|\Closure */ protected $pdo; /** * The active PDO connection used for reads. * * @var \PDO|\Closure */ protected $readPdo; /** * The name of the connected database. * * @var string */ protected $database; /** * The type of the connection. * * @var string|null */ protected $readWriteType; /** * The table prefix for the connection. * * @var string */ protected $tablePrefix = ''; /** * The database connection configuration options. * * @var array */ protected $config = []; /** * The reconnector instance for the connection. * * @var callable */ protected $reconnector; /** * The query grammar implementation. * * @var \Illuminate\Database\Query\Grammars\Grammar */ protected $queryGrammar; /** * The schema grammar implementation. * * @var \Illuminate\Database\Schema\Grammars\Grammar */ protected $schemaGrammar; /** * The query post processor implementation. * * @var \Illuminate\Database\Query\Processors\Processor */ protected $postProcessor; /** * The event dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * The default fetch mode of the connection. * * @var int */ protected $fetchMode = PDO::FETCH_OBJ; /** * The number of active transactions. * * @var int */ protected $transactions = 0; /** * The transaction manager instance. * * @var \Illuminate\Database\DatabaseTransactionsManager */ protected $transactionsManager; /** * Indicates if changes have been made to the database. * * @var bool */ protected $recordsModified = false; /** * Indicates if the connection should use the "write" PDO connection. * * @var bool */ protected $readOnWriteConnection = false; /** * All of the queries run against the connection. * * @var array */ protected $queryLog = []; /** * Indicates whether queries are being logged. * * @var bool */ protected $loggingQueries = false; /** * The duration of all executed queries in milliseconds. * * @var float */ protected $totalQueryDuration = 0.0; /** * All of the registered query duration handlers. * * @var array */ protected $queryDurationHandlers = []; /** * Indicates if the connection is in a "dry run". * * @var bool */ protected $pretending = false; /** * All of the callbacks that should be invoked before a query is executed. * * @var \Closure[] */ protected $beforeExecutingCallbacks = []; /** * The instance of Doctrine connection. * * @var \Doctrine\DBAL\Connection */ protected $doctrineConnection; /** * Type mappings that should be registered with new Doctrine connections. * * @var array<string, string> */ protected $doctrineTypeMappings = []; /** * The connection resolvers. * * @var \Closure[] */ protected static $resolvers = []; /** * Create a new database connection instance. * * @param \PDO|\Closure $pdo * @param string $database * @param string $tablePrefix * @param array $config * @return void */ public function __construct($pdo, $database = '', $tablePrefix = '', array $config = []) { $this->pdo = $pdo; // First we will setup the default properties. We keep track of the DB // name we are connected to since it is needed when some reflective // type commands are run such as checking whether a table exists. $this->database = $database; $this->tablePrefix = $tablePrefix; $this->config = $config; // We need to initialize a query grammar and the query post processors // which are both very important parts of the database abstractions // so we initialize these to their default values while starting. $this->useDefaultQueryGrammar(); $this->useDefaultPostProcessor(); } /** * Set the query grammar to the default implementation. * * @return void */ public function useDefaultQueryGrammar() { $this->queryGrammar = $this->getDefaultQueryGrammar(); } /** * Get the default query grammar instance. * * @return \Illuminate\Database\Query\Grammars\Grammar */ protected function getDefaultQueryGrammar() { ($grammar = new QueryGrammar)->setConnection($this); return $grammar; } /** * Set the schema grammar to the default implementation. * * @return void */ public function useDefaultSchemaGrammar() { $this->schemaGrammar = $this->getDefaultSchemaGrammar(); } /** * Get the default schema grammar instance. * * @return \Illuminate\Database\Schema\Grammars\Grammar|null */ protected function getDefaultSchemaGrammar() { // } /** * Set the query post processor to the default implementation. * * @return void */ public function useDefaultPostProcessor() { $this->postProcessor = $this->getDefaultPostProcessor(); } /** * Get the default post processor instance. * * @return \Illuminate\Database\Query\Processors\Processor */ protected function getDefaultPostProcessor() { return new Processor; } /** * Get a schema builder instance for the connection. * * @return \Illuminate\Database\Schema\Builder */ public function getSchemaBuilder() { if (is_null($this->schemaGrammar)) { $this->useDefaultSchemaGrammar(); } return new SchemaBuilder($this); } /** * Begin a fluent query against a database table. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Contracts\Database\Query\Expression|string $table * @param string|null $as * @return \Illuminate\Database\Query\Builder */ public function table($table, $as = null) { return $this->query()->from($table, $as); } /** * Get a new query builder instance. * * @return \Illuminate\Database\Query\Builder */ public function query() { return new QueryBuilder( $this, $this->getQueryGrammar(), $this->getPostProcessor() ); } /** * Run a select statement and return a single result. * * @param string $query * @param array $bindings * @param bool $useReadPdo * @return mixed */ public function selectOne($query, $bindings = [], $useReadPdo = true) { $records = $this->select($query, $bindings, $useReadPdo); return array_shift($records); } /** * Run a select statement and return the first column of the first row. * * @param string $query * @param array $bindings * @param bool $useReadPdo * @return mixed * * @throws \Illuminate\Database\MultipleColumnsSelectedException */ public function scalar($query, $bindings = [], $useReadPdo = true) { $record = $this->selectOne($query, $bindings, $useReadPdo); if (is_null($record)) { return null; } $record = (array) $record; if (count($record) > 1) { throw new MultipleColumnsSelectedException; } return reset($record); } /** * Run a select statement against the database. * * @param string $query * @param array $bindings * @return array */ public function selectFromWriteConnection($query, $bindings = []) { return $this->select($query, $bindings, false); } /** * Run a select statement against the database. * * @param string $query * @param array $bindings * @param bool $useReadPdo * @return array */ public function select($query, $bindings = [], $useReadPdo = true) { return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) { if ($this->pretending()) { return []; } // For select statements, we'll simply execute the query and return an array // of the database result set. Each element in the array will be a single // row from the database table, and will either be an array or objects. $statement = $this->prepared( $this->getPdoForSelect($useReadPdo)->prepare($query) ); $this->bindValues($statement, $this->prepareBindings($bindings)); $statement->execute(); return $statement->fetchAll(); }); } /** * Run a select statement against the database and returns all of the result sets. * * @param string $query * @param array $bindings * @param bool $useReadPdo * @return array */ public function selectResultSets($query, $bindings = [], $useReadPdo = true) { return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) { if ($this->pretending()) { return []; } $statement = $this->prepared( $this->getPdoForSelect($useReadPdo)->prepare($query) ); $this->bindValues($statement, $this->prepareBindings($bindings)); $statement->execute(); $sets = []; do { $sets[] = $statement->fetchAll(); } while ($statement->nextRowset()); return $sets; }); } /** * Run a select statement against the database and returns a generator. * * @param string $query * @param array $bindings * @param bool $useReadPdo * @return \Generator */ public function cursor($query, $bindings = [], $useReadPdo = true) { $statement = $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) { if ($this->pretending()) { return []; } // First we will create a statement for the query. Then, we will set the fetch // mode and prepare the bindings for the query. Once that's done we will be // ready to execute the query against the database and return the cursor. $statement = $this->prepared($this->getPdoForSelect($useReadPdo) ->prepare($query)); $this->bindValues( $statement, $this->prepareBindings($bindings) ); // Next, we'll execute the query against the database and return the statement // so we can return the cursor. The cursor will use a PHP generator to give // back one row at a time without using a bunch of memory to render them. $statement->execute(); return $statement; }); while ($record = $statement->fetch()) { yield $record; } } /** * Configure the PDO prepared statement. * * @param \PDOStatement $statement * @return \PDOStatement */ protected function prepared(PDOStatement $statement) { $statement->setFetchMode($this->fetchMode); $this->event(new StatementPrepared($this, $statement)); return $statement; } /** * Get the PDO connection to use for a select query. * * @param bool $useReadPdo * @return \PDO */ protected function getPdoForSelect($useReadPdo = true) { return $useReadPdo ? $this->getReadPdo() : $this->getPdo(); } /** * Run an insert statement against the database. * * @param string $query * @param array $bindings * @return bool */ public function insert($query, $bindings = []) { return $this->statement($query, $bindings); } /** * Run an update statement against the database. * * @param string $query * @param array $bindings * @return int */ public function update($query, $bindings = []) { return $this->affectingStatement($query, $bindings); } /** * Run a delete statement against the database. * * @param string $query * @param array $bindings * @return int */ public function delete($query, $bindings = []) { return $this->affectingStatement($query, $bindings); } /** * Execute an SQL statement and return the boolean result. * * @param string $query * @param array $bindings * @return bool */ public function statement($query, $bindings = []) { return $this->run($query, $bindings, function ($query, $bindings) { if ($this->pretending()) { return true; } $statement = $this->getPdo()->prepare($query); $this->bindValues($statement, $this->prepareBindings($bindings)); $this->recordsHaveBeenModified(); return $statement->execute(); }); } /** * Run an SQL statement and get the number of rows affected. * * @param string $query * @param array $bindings * @return int */ public function affectingStatement($query, $bindings = []) { return $this->run($query, $bindings, function ($query, $bindings) { if ($this->pretending()) { return 0; } // For update or delete statements, we want to get the number of rows affected // by the statement and return that back to the developer. We'll first need // to execute the statement and then we'll use PDO to fetch the affected. $statement = $this->getPdo()->prepare($query); $this->bindValues($statement, $this->prepareBindings($bindings)); $statement->execute(); $this->recordsHaveBeenModified( ($count = $statement->rowCount()) > 0 ); return $count; }); } /** * Run a raw, unprepared query against the PDO connection. * * @param string $query * @return bool */ public function unprepared($query) { return $this->run($query, [], function ($query) { if ($this->pretending()) { return true; } $this->recordsHaveBeenModified( $change = $this->getPdo()->exec($query) !== false ); return $change; }); } /** * Execute the given callback in "dry run" mode. * * @param \Closure $callback * @return array */ public function pretend(Closure $callback) { return $this->withFreshQueryLog(function () use ($callback) { $this->pretending = true; // Basically to make the database connection "pretend", we will just return // the default values for all the query methods, then we will return an // array of queries that were "executed" within the Closure callback. $callback($this); $this->pretending = false; return $this->queryLog; }); } /** * Execute the given callback without "pretending". * * @param \Closure $callback * @return mixed */ public function withoutPretending(Closure $callback) { if (! $this->pretending) { return $callback(); } $this->pretending = false; $result = $callback(); $this->pretending = true; return $result; } /** * Execute the given callback in "dry run" mode. * * @param \Closure $callback * @return array */ protected function withFreshQueryLog($callback) { $loggingQueries = $this->loggingQueries; // First we will back up the value of the logging queries property and then // we'll be ready to run callbacks. This query log will also get cleared // so we will have a new log of all the queries that are executed now. $this->enableQueryLog(); $this->queryLog = []; // Now we'll execute this callback and capture the result. Once it has been // executed we will restore the value of query logging and give back the // value of the callback so the original callers can have the results. $result = $callback(); $this->loggingQueries = $loggingQueries; return $result; } /** * Bind values to their parameters in the given statement. * * @param \PDOStatement $statement * @param array $bindings * @return void */ public function bindValues($statement, $bindings) { foreach ($bindings as $key => $value) { $statement->bindValue( is_string($key) ? $key : $key + 1, $value, match (true) { is_int($value) => PDO::PARAM_INT, is_resource($value) => PDO::PARAM_LOB, default => PDO::PARAM_STR }, ); } } /** * Prepare the query bindings for execution. * * @param array $bindings * @return array */ public function prepareBindings(array $bindings) { $grammar = $this->getQueryGrammar(); foreach ($bindings as $key => $value) { // We need to transform all instances of DateTimeInterface into the actual // date string. Each query grammar maintains its own date string format // so we'll just ask the grammar for the format to get from the date. if ($value instanceof DateTimeInterface) { $bindings[$key] = $value->format($grammar->getDateFormat()); } elseif (is_bool($value)) { $bindings[$key] = (int) $value; } } return $bindings; } /** * Run a SQL statement and log its execution context. * * @param string $query * @param array $bindings * @param \Closure $callback * @return mixed * * @throws \Illuminate\Database\QueryException */ protected function run($query, $bindings, Closure $callback) { foreach ($this->beforeExecutingCallbacks as $beforeExecutingCallback) { $beforeExecutingCallback($query, $bindings, $this); } $this->reconnectIfMissingConnection(); $start = microtime(true); // Here we will run this query. If an exception occurs we'll determine if it was // caused by a connection that has been lost. If that is the cause, we'll try // to re-establish connection and re-run the query with a fresh connection. try { $result = $this->runQueryCallback($query, $bindings, $callback); } catch (QueryException $e) { $result = $this->handleQueryException( $e, $query, $bindings, $callback ); } // Once we have run the query we will calculate the time that it took to run and // then log the query, bindings, and execution time so we will report them on // the event that the developer needs them. We'll log time in milliseconds. $this->logQuery( $query, $bindings, $this->getElapsedTime($start) ); return $result; } /** * Run a SQL statement. * * @param string $query * @param array $bindings * @param \Closure $callback * @return mixed * * @throws \Illuminate\Database\QueryException */ protected function runQueryCallback($query, $bindings, Closure $callback) { // To execute the statement, we'll simply call the callback, which will actually // run the SQL against the PDO connection. Then we can calculate the time it // took to execute and log the query SQL, bindings and time in our memory. try { return $callback($query, $bindings); } // If an exception occurs when attempting to run a query, we'll format the error // message to include the bindings with SQL, which will make this exception a // lot more helpful to the developer instead of just the database's errors. catch (Exception $e) { if ($this->isUniqueConstraintError($e)) { throw new UniqueConstraintViolationException( $this->getName(), $query, $this->prepareBindings($bindings), $e ); } throw new QueryException( $this->getName(), $query, $this->prepareBindings($bindings), $e ); } } /** * Determine if the given database exception was caused by a unique constraint violation. * * @param \Exception $exception * @return bool */ protected function isUniqueConstraintError(Exception $exception) { return false; } /** * Log a query in the connection's query log. * * @param string $query * @param array $bindings * @param float|null $time * @return void */ public function logQuery($query, $bindings, $time = null) { $this->totalQueryDuration += $time ?? 0.0; $this->event(new QueryExecuted($query, $bindings, $time, $this)); $query = $this->pretending === true ? $this->queryGrammar?->substituteBindingsIntoRawSql($query, $bindings) ?? $query : $query; if ($this->loggingQueries) { $this->queryLog[] = compact('query', 'bindings', 'time'); } } /** * Get the elapsed time since a given starting point. * * @param int $start * @return float */ protected function getElapsedTime($start) { return round((microtime(true) - $start) * 1000, 2); } /** * Register a callback to be invoked when the connection queries for longer than a given amount of time. * * @param \DateTimeInterface|\Carbon\CarbonInterval|float|int $threshold * @param callable $handler * @return void */ public function whenQueryingForLongerThan($threshold, $handler) { $threshold = $threshold instanceof DateTimeInterface ? $this->secondsUntil($threshold) * 1000 : $threshold; $threshold = $threshold instanceof CarbonInterval ? $threshold->totalMilliseconds : $threshold; $this->queryDurationHandlers[] = [ 'has_run' => false, 'handler' => $handler, ]; $key = count($this->queryDurationHandlers) - 1; $this->listen(function ($event) use ($threshold, $handler, $key) { if (! $this->queryDurationHandlers[$key]['has_run'] && $this->totalQueryDuration() > $threshold) { $handler($this, $event); $this->queryDurationHandlers[$key]['has_run'] = true; } }); } /** * Allow all the query duration handlers to run again, even if they have already run. * * @return void */ public function allowQueryDurationHandlersToRunAgain() { foreach ($this->queryDurationHandlers as $key => $queryDurationHandler) { $this->queryDurationHandlers[$key]['has_run'] = false; } } /** * Get the duration of all run queries in milliseconds. * * @return float */ public function totalQueryDuration() { return $this->totalQueryDuration; } /** * Reset the duration of all run queries. * * @return void */ public function resetTotalQueryDuration() { $this->totalQueryDuration = 0.0; } /** * Handle a query exception. * * @param \Illuminate\Database\QueryException $e * @param string $query * @param array $bindings * @param \Closure $callback * @return mixed * * @throws \Illuminate\Database\QueryException */ protected function handleQueryException(QueryException $e, $query, $bindings, Closure $callback) { if ($this->transactions >= 1) { throw $e; } return $this->tryAgainIfCausedByLostConnection( $e, $query, $bindings, $callback ); } /** * Handle a query exception that occurred during query execution. * * @param \Illuminate\Database\QueryException $e * @param string $query * @param array $bindings * @param \Closure $callback * @return mixed * * @throws \Illuminate\Database\QueryException */ protected function tryAgainIfCausedByLostConnection(QueryException $e, $query, $bindings, Closure $callback) { if ($this->causedByLostConnection($e->getPrevious())) { $this->reconnect(); return $this->runQueryCallback($query, $bindings, $callback); } throw $e; } /** * Reconnect to the database. * * @return mixed|false * * @throws \Illuminate\Database\LostConnectionException */ public function reconnect() { if (is_callable($this->reconnector)) { $this->doctrineConnection = null; return call_user_func($this->reconnector, $this); } throw new LostConnectionException('Lost connection and no reconnector available.'); } /** * Reconnect to the database if a PDO connection is missing. * * @return void */ public function reconnectIfMissingConnection() { if (is_null($this->pdo)) { $this->reconnect(); } } /** * Disconnect from the underlying PDO connection. * * @return void */ public function disconnect() { $this->setPdo(null)->setReadPdo(null); $this->doctrineConnection = null; } /** * Register a hook to be run just before a database query is executed. * * @param \Closure $callback * @return $this */ public function beforeExecuting(Closure $callback) { $this->beforeExecutingCallbacks[] = $callback; return $this; } /** * Register a database query listener with the connection. * * @param \Closure $callback * @return void */ public function listen(Closure $callback) { $this->events?->listen(Events\QueryExecuted::class, $callback); } /** * Fire an event for this connection. * * @param string $event * @return array|null */ protected function fireConnectionEvent($event) { return $this->events?->dispatch(match ($event) { 'beganTransaction' => new TransactionBeginning($this), 'committed' => new TransactionCommitted($this), 'committing' => new TransactionCommitting($this), 'rollingBack' => new TransactionRolledBack($this), default => null, }); } /** * Fire the given event if possible. * * @param mixed $event * @return void */ protected function event($event) { $this->events?->dispatch($event); } /** * Get a new raw query expression. * * @param mixed $value * @return \Illuminate\Contracts\Database\Query\Expression */ public function raw($value) { return new Expression($value); } /** * Escape a value for safe SQL embedding. * * @param string|float|int|bool|null $value * @param bool $binary * @return string */ public function escape($value, $binary = false) { if ($value === null) { return 'null'; } elseif ($binary) { return $this->escapeBinary($value); } elseif (is_int($value) || is_float($value)) { return (string) $value; } elseif (is_bool($value)) { return $this->escapeBool($value); } elseif (is_array($value)) { throw new RuntimeException('The database connection does not support escaping arrays.'); } else { if (str_contains($value, "\00")) { throw new RuntimeException('Strings with null bytes cannot be escaped. Use the binary escape option.'); } if (preg_match('//u', $value) === false) { throw new RuntimeException('Strings with invalid UTF-8 byte sequences cannot be escaped.'); } return $this->escapeString($value); } } /** * Escape a string value for safe SQL embedding. * * @param string $value * @return string */ protected function escapeString($value) { return $this->getReadPdo()->quote($value); } /** * Escape a boolean value for safe SQL embedding. * * @param bool $value * @return string */ protected function escapeBool($value) { return $value ? '1' : '0'; } /** * Escape a binary value for safe SQL embedding. * * @param string $value * @return string */ protected function escapeBinary($value) { throw new RuntimeException('The database connection does not support escaping binary values.'); } /** * Determine if the database connection has modified any database records. * * @return bool */ public function hasModifiedRecords() { return $this->recordsModified; } /** * Indicate if any records have been modified. * * @param bool $value * @return void */ public function recordsHaveBeenModified($value = true) { if (! $this->recordsModified) { $this->recordsModified = $value; } } /** * Set the record modification state. * * @param bool $value * @return $this */ public function setRecordModificationState(bool $value) { $this->recordsModified = $value; return $this; } /** * Reset the record modification state. * * @return void */ public function forgetRecordModificationState() { $this->recordsModified = false; } /** * Indicate that the connection should use the write PDO connection for reads. * * @param bool $value * @return $this */ public function useWriteConnectionWhenReading($value = true) { $this->readOnWriteConnection = $value; return $this; } /** * Is Doctrine available? * * @return bool */ public function isDoctrineAvailable() { return class_exists('Doctrine\DBAL\Connection'); } /** * Indicates whether native alter operations will be used when dropping, renaming, or modifying columns, even if Doctrine DBAL is installed. * * @return bool */ public function usingNativeSchemaOperations() { return ! $this->isDoctrineAvailable() || SchemaBuilder::$alwaysUsesNativeSchemaOperationsIfPossible; } /** * Get a Doctrine Schema Column instance. * * @param string $table * @param string $column * @return \Doctrine\DBAL\Schema\Column */ public function getDoctrineColumn($table, $column) { $schema = $this->getDoctrineSchemaManager(); return $schema->introspectTable($table)->getColumn($column); } /** * Get the Doctrine DBAL schema manager for the connection. * * @return \Doctrine\DBAL\Schema\AbstractSchemaManager */ public function getDoctrineSchemaManager() { $connection = $this->getDoctrineConnection(); return $connection->createSchemaManager(); } /** * Get the Doctrine DBAL database connection instance. * * @return \Doctrine\DBAL\Connection */ public function getDoctrineConnection() { if (is_null($this->doctrineConnection)) { $driver = $this->getDoctrineDriver(); $this->doctrineConnection = new DoctrineConnection(array_filter([ 'pdo' => $this->getPdo(), 'dbname' => $this->getDatabaseName(), 'driver' => $driver->getName(), 'serverVersion' => $this->getConfig('server_version'), ]), $driver); foreach ($this->doctrineTypeMappings as $name => $type) { $this->doctrineConnection ->getDatabasePlatform() ->registerDoctrineTypeMapping($type, $name); } } return $this->doctrineConnection; } /** * Register a custom Doctrine mapping type. * * @param Type|class-string<Type> $class * @param string $name * @param string $type * @return void * * @throws \Doctrine\DBAL\Exception * @throws \RuntimeException */ public function registerDoctrineType(Type|string $class, string $name, string $type): void { if (! $this->isDoctrineAvailable()) { throw new RuntimeException( 'Registering a custom Doctrine type requires Doctrine DBAL (doctrine/dbal).' ); } if (! Type::hasType($name)) { Type::getTypeRegistry() ->register($name, is_string($class) ? new $class() : $class); } $this->doctrineTypeMappings[$name] = $type; } /** * Get the current PDO connection. * * @return \PDO */ public function getPdo() { if ($this->pdo instanceof Closure) { return $this->pdo = call_user_func($this->pdo); } return $this->pdo; } /** * Get the current PDO connection parameter without executing any reconnect logic. * * @return \PDO|\Closure|null */ public function getRawPdo() { return $this->pdo; } /** * Get the current PDO connection used for reading. * * @return \PDO */ public function getReadPdo() { if ($this->transactions > 0) { return $this->getPdo(); } if ($this->readOnWriteConnection || ($this->recordsModified && $this->getConfig('sticky'))) { return $this->getPdo(); } if ($this->readPdo instanceof Closure) { return $this->readPdo = call_user_func($this->readPdo); } return $this->readPdo ?: $this->getPdo(); } /** * Get the current read PDO connection parameter without executing any reconnect logic. * * @return \PDO|\Closure|null */ public function getRawReadPdo() { return $this->readPdo; } /** * Set the PDO connection. * * @param \PDO|\Closure|null $pdo * @return $this */ public function setPdo($pdo) { $this->transactions = 0; $this->pdo = $pdo; return $this; } /** * Set the PDO connection used for reading. * * @param \PDO|\Closure|null $pdo * @return $this */ public function setReadPdo($pdo) { $this->readPdo = $pdo; return $this; } /** * Set the reconnect instance on the connection. * * @param callable $reconnector * @return $this */ public function setReconnector(callable $reconnector) { $this->reconnector = $reconnector; return $this; } /** * Get the database connection name. * * @return string|null */ public function getName() { return $this->getConfig('name'); } /** * Get the database connection full name. * * @return string|null */ public function getNameWithReadWriteType() { return $this->getName().($this->readWriteType ? '::'.$this->readWriteType : ''); } /** * Get an option from the configuration options. * * @param string|null $option * @return mixed */ public function getConfig($option = null) { return Arr::get($this->config, $option); } /** * Get the PDO driver name. * * @return string */ public function getDriverName() { return $this->getConfig('driver'); } /** * Get the query grammar used by the connection. * * @return \Illuminate\Database\Query\Grammars\Grammar */ public function getQueryGrammar() { return $this->queryGrammar; } /** * Set the query grammar used by the connection. * * @param \Illuminate\Database\Query\Grammars\Grammar $grammar * @return $this */ public function setQueryGrammar(Query\Grammars\Grammar $grammar) { $this->queryGrammar = $grammar; return $this; } /** * Get the schema grammar used by the connection. * * @return \Illuminate\Database\Schema\Grammars\Grammar */ public function getSchemaGrammar() { return $this->schemaGrammar; } /** * Set the schema grammar used by the connection. * * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar * @return $this */ public function setSchemaGrammar(Schema\Grammars\Grammar $grammar) { $this->schemaGrammar = $grammar; return $this; } /** * Get the query post processor used by the connection. * * @return \Illuminate\Database\Query\Processors\Processor */ public function getPostProcessor() { return $this->postProcessor; } /** * Set the query post processor used by the connection. * * @param \Illuminate\Database\Query\Processors\Processor $processor * @return $this */ public function setPostProcessor(Processor $processor) { $this->postProcessor = $processor; return $this; } /** * Get the event dispatcher used by the connection. * * @return \Illuminate\Contracts\Events\Dispatcher */ public function getEventDispatcher() { return $this->events; } /** * Set the event dispatcher instance on the connection. * * @param \Illuminate\Contracts\Events\Dispatcher $events * @return $this */ public function setEventDispatcher(Dispatcher $events) { $this->events = $events; return $this; } /** * Unset the event dispatcher for this connection. * * @return void */ public function unsetEventDispatcher() { $this->events = null; } /** * Set the transaction manager instance on the connection. * * @param \Illuminate\Database\DatabaseTransactionsManager $manager * @return $this */ public function setTransactionManager($manager) { $this->transactionsManager = $manager; return $this; } /** * Unset the transaction manager for this connection. * * @return void */ public function unsetTransactionManager() { $this->transactionsManager = null; } /** * Determine if the connection is in a "dry run". * * @return bool */ public function pretending() { return $this->pretending === true; } /** * Get the connection query log. * * @return array */ public function getQueryLog() { return $this->queryLog; } /** * Get the connection query log with embedded bindings. * * @return array */ public function getRawQueryLog() { return array_map(fn (array $log) => [ 'raw_query' => $this->queryGrammar->substituteBindingsIntoRawSql( $log['query'], $this->prepareBindings($log['bindings']) ), 'time' => $log['time'], ], $this->getQueryLog()); } /** * Clear the query log. * * @return void */ public function flushQueryLog() { $this->queryLog = []; } /** * Enable the query log on the connection. * * @return void */ public function enableQueryLog() { $this->loggingQueries = true; } /** * Disable the query log on the connection. * * @return void */ public function disableQueryLog() { $this->loggingQueries = false; } /** * Determine whether we're logging queries. * * @return bool */ public function logging() { return $this->loggingQueries; } /** * Get the name of the connected database. * * @return string */ public function getDatabaseName() { return $this->database; } /** * Set the name of the connected database. * * @param string $database * @return $this */ public function setDatabaseName($database) { $this->database = $database; return $this; } /** * Set the read / write type of the connection. * * @param string|null $readWriteType * @return $this */ public function setReadWriteType($readWriteType) { $this->readWriteType = $readWriteType; return $this; } /** * Get the table prefix for the connection. * * @return string */ public function getTablePrefix() { return $this->tablePrefix; } /** * Set the table prefix in use by the connection. * * @param string $prefix * @return $this */ public function setTablePrefix($prefix) { $this->tablePrefix = $prefix; $this->getQueryGrammar()->setTablePrefix($prefix); return $this; } /** * Set the table prefix and return the grammar. * * @param \Illuminate\Database\Grammar $grammar * @return \Illuminate\Database\Grammar */ public function withTablePrefix(Grammar $grammar) { $grammar->setTablePrefix($this->tablePrefix); return $grammar; } /** * Register a connection resolver. * * @param string $driver * @param \Closure $callback * @return void */ public static function resolverFor($driver, Closure $callback) { static::$resolvers[$driver] = $callback; } /** * Get the connection resolver for the given driver. * * @param string $driver * @return mixed */ public static function getResolver($driver) { return static::$resolvers[$driver] ?? null; } } src/Illuminate/Database/Capsule/Manager.php 0000644 00000012400 15107327037 0014623 0 ustar 00 <?php namespace Illuminate\Database\Capsule; use Illuminate\Container\Container; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Connectors\ConnectionFactory; use Illuminate\Database\DatabaseManager; use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Support\Traits\CapsuleManagerTrait; use PDO; class Manager { use CapsuleManagerTrait; /** * The database manager instance. * * @var \Illuminate\Database\DatabaseManager */ protected $manager; /** * Create a new database capsule manager. * * @param \Illuminate\Container\Container|null $container * @return void */ public function __construct(Container $container = null) { $this->setupContainer($container ?: new Container); // Once we have the container setup, we will setup the default configuration // options in the container "config" binding. This will make the database // manager work correctly out of the box without extreme configuration. $this->setupDefaultConfiguration(); $this->setupManager(); } /** * Setup the default database configuration options. * * @return void */ protected function setupDefaultConfiguration() { $this->container['config']['database.fetch'] = PDO::FETCH_OBJ; $this->container['config']['database.default'] = 'default'; } /** * Build the database manager instance. * * @return void */ protected function setupManager() { $factory = new ConnectionFactory($this->container); $this->manager = new DatabaseManager($this->container, $factory); } /** * Get a connection instance from the global manager. * * @param string|null $connection * @return \Illuminate\Database\Connection */ public static function connection($connection = null) { return static::$instance->getConnection($connection); } /** * Get a fluent query builder instance. * * @param \Closure|\Illuminate\Database\Query\Builder|string $table * @param string|null $as * @param string|null $connection * @return \Illuminate\Database\Query\Builder */ public static function table($table, $as = null, $connection = null) { return static::$instance->connection($connection)->table($table, $as); } /** * Get a schema builder instance. * * @param string|null $connection * @return \Illuminate\Database\Schema\Builder */ public static function schema($connection = null) { return static::$instance->connection($connection)->getSchemaBuilder(); } /** * Get a registered connection instance. * * @param string|null $name * @return \Illuminate\Database\Connection */ public function getConnection($name = null) { return $this->manager->connection($name); } /** * Register a connection with the manager. * * @param array $config * @param string $name * @return void */ public function addConnection(array $config, $name = 'default') { $connections = $this->container['config']['database.connections']; $connections[$name] = $config; $this->container['config']['database.connections'] = $connections; } /** * Bootstrap Eloquent so it is ready for usage. * * @return void */ public function bootEloquent() { Eloquent::setConnectionResolver($this->manager); // If we have an event dispatcher instance, we will go ahead and register it // with the Eloquent ORM, allowing for model callbacks while creating and // updating "model" instances; however, it is not necessary to operate. if ($dispatcher = $this->getEventDispatcher()) { Eloquent::setEventDispatcher($dispatcher); } } /** * Set the fetch mode for the database connections. * * @param int $fetchMode * @return $this */ public function setFetchMode($fetchMode) { $this->container['config']['database.fetch'] = $fetchMode; return $this; } /** * Get the database manager instance. * * @return \Illuminate\Database\DatabaseManager */ public function getDatabaseManager() { return $this->manager; } /** * Get the current event dispatcher instance. * * @return \Illuminate\Contracts\Events\Dispatcher|null */ public function getEventDispatcher() { if ($this->container->bound('events')) { return $this->container['events']; } } /** * Set the event dispatcher instance to be used by connections. * * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher * @return void */ public function setEventDispatcher(Dispatcher $dispatcher) { $this->container->instance('events', $dispatcher); } /** * Dynamically pass methods to the default connection. * * @param string $method * @param array $parameters * @return mixed */ public static function __callStatic($method, $parameters) { return static::connection()->$method(...$parameters); } } src/Illuminate/Database/DeadlockException.php 0000644 00000000162 15107327037 0015244 0 ustar 00 <?php namespace Illuminate\Database; use PDOException; class DeadlockException extends PDOException { // } src/Illuminate/Database/UniqueConstraintViolationException.php 0000644 00000000153 15107327037 0020736 0 ustar 00 <?php namespace Illuminate\Database; class UniqueConstraintViolationException extends QueryException { } src/Illuminate/Database/DetectsLostConnections.php 0000644 00000007512 15107327037 0016325 0 ustar 00 <?php namespace Illuminate\Database; use Illuminate\Support\Str; use Throwable; trait DetectsLostConnections { /** * Determine if the given exception was caused by a lost connection. * * @param \Throwable $e * @return bool */ protected function causedByLostConnection(Throwable $e) { $message = $e->getMessage(); return Str::contains($message, [ 'server has gone away', 'no connection to the server', 'Lost connection', 'is dead or not enabled', 'Error while sending', 'decryption failed or bad record mac', 'server closed the connection unexpectedly', 'SSL connection has been closed unexpectedly', 'Error writing data to the connection', 'Resource deadlock avoided', 'Transaction() on null', 'child connection forced to terminate due to client_idle_limit', 'query_wait_timeout', 'reset by peer', 'Physical connection is not usable', 'TCP Provider: Error code 0x68', 'ORA-03114', 'Packets out of order. Expected', 'Adaptive Server connection failed', 'Communication link failure', 'connection is no longer usable', 'Login timeout expired', 'SQLSTATE[HY000] [2002] Connection refused', 'running with the --read-only option so it cannot execute this statement', 'The connection is broken and recovery is not possible. The connection is marked by the client driver as unrecoverable. No attempt was made to restore the connection.', 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Try again', 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known', 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for', 'SQLSTATE[HY000]: General error: 7 SSL SYSCALL error: EOF detected', 'SQLSTATE[HY000] [2002] Connection timed out', 'SSL: Connection timed out', 'SQLSTATE[HY000]: General error: 1105 The last transaction was aborted due to Seamless Scaling. Please retry.', 'Temporary failure in name resolution', 'SSL: Broken pipe', 'SQLSTATE[08S01]: Communication link failure', 'SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host', 'SQLSTATE[HY000]: General error: 7 SSL SYSCALL error: No route to host', 'The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior.', 'SQLSTATE[08006] [7] could not translate host name', 'TCP Provider: Error code 0x274C', 'SQLSTATE[HY000] [2002] No such file or directory', 'SSL: Operation timed out', 'Reason: Server is in script upgrade mode. Only administrator can connect at this time.', 'Unknown $curl_error_code: 77', 'SSL: Handshake timed out', 'SQLSTATE[08006] [7] SSL error: sslv3 alert unexpected message', 'SQLSTATE[08006] [7] unrecognized SSL error code:', 'SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it', 'SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond', 'SQLSTATE[HY000] [2002] Network is unreachable', 'SQLSTATE[HY000] [2002] The requested address is not valid in its context', 'SQLSTATE[HY000] [2002] A socket operation was attempted to an unreachable network', ]); } } src/Illuminate/Broadcasting/InteractsWithBroadcasting.php 0000644 00000001524 15107327037 0017667 0 ustar 00 <?php namespace Illuminate\Broadcasting; use Illuminate\Support\Arr; trait InteractsWithBroadcasting { /** * The broadcaster connection to use to broadcast the event. * * @var array */ protected $broadcastConnection = [null]; /** * Broadcast the event using a specific broadcaster. * * @param array|string|null $connection * @return $this */ public function broadcastVia($connection = null) { $this->broadcastConnection = is_null($connection) ? [null] : Arr::wrap($connection); return $this; } /** * Get the broadcaster connections the event should be broadcast on. * * @return array */ public function broadcastConnections() { return $this->broadcastConnection; } } src/Illuminate/Broadcasting/Channel.php 0000644 00000001261 15107327037 0014124 0 ustar 00 <?php namespace Illuminate\Broadcasting; use Illuminate\Contracts\Broadcasting\HasBroadcastChannel; class Channel { /** * The channel's name. * * @var string */ public $name; /** * Create a new channel instance. * * @param \Illuminate\Contracts\Broadcasting\HasBroadcastChannel|string $name * @return void */ public function __construct($name) { $this->name = $name instanceof HasBroadcastChannel ? $name->broadcastChannel() : $name; } /** * Convert the channel instance to a string. * * @return string */ public function __toString() { return $this->name; } } src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php 0000644 00000025470 15107327037 0017431 0 ustar 00 <?php namespace Illuminate\Broadcasting\Broadcasters; use Closure; use Exception; use Illuminate\Container\Container; use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract; use Illuminate\Contracts\Broadcasting\HasBroadcastChannel; use Illuminate\Contracts\Routing\BindingRegistrar; use Illuminate\Contracts\Routing\UrlRoutable; use Illuminate\Support\Arr; use Illuminate\Support\Reflector; use ReflectionClass; use ReflectionFunction; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; abstract class Broadcaster implements BroadcasterContract { /** * The callback to resolve the authenticated user information. * * @var \Closure|null */ protected $authenticatedUserCallback = null; /** * The registered channel authenticators. * * @var array */ protected $channels = []; /** * The registered channel options. * * @var array */ protected $channelOptions = []; /** * The binding registrar instance. * * @var \Illuminate\Contracts\Routing\BindingRegistrar */ protected $bindingRegistrar; /** * Resolve the authenticated user payload for the incoming connection request. * * See: https://pusher.com/docs/channels/library_auth_reference/auth-signatures/#user-authentication. * * @param \Illuminate\Http\Request $request * @return array|null */ public function resolveAuthenticatedUser($request) { if ($this->authenticatedUserCallback) { return $this->authenticatedUserCallback->__invoke($request); } } /** * Register the user retrieval callback used to authenticate connections. * * See: https://pusher.com/docs/channels/library_auth_reference/auth-signatures/#user-authentication. * * @param \Closure $callback * @return void */ public function resolveAuthenticatedUserUsing(Closure $callback) { $this->authenticatedUserCallback = $callback; } /** * Register a channel authenticator. * * @param \Illuminate\Contracts\Broadcasting\HasBroadcastChannel|string $channel * @param callable|string $callback * @param array $options * @return $this */ public function channel($channel, $callback, $options = []) { if ($channel instanceof HasBroadcastChannel) { $channel = $channel->broadcastChannelRoute(); } elseif (is_string($channel) && class_exists($channel) && is_a($channel, HasBroadcastChannel::class, true)) { $channel = (new $channel)->broadcastChannelRoute(); } $this->channels[$channel] = $callback; $this->channelOptions[$channel] = $options; return $this; } /** * Authenticate the incoming request for a given channel. * * @param \Illuminate\Http\Request $request * @param string $channel * @return mixed * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException */ protected function verifyUserCanAccessChannel($request, $channel) { foreach ($this->channels as $pattern => $callback) { if (! $this->channelNameMatchesPattern($channel, $pattern)) { continue; } $parameters = $this->extractAuthParameters($pattern, $channel, $callback); $handler = $this->normalizeChannelHandlerToCallable($callback); $result = $handler($this->retrieveUser($request, $channel), ...$parameters); if ($result === false) { throw new AccessDeniedHttpException; } elseif ($result) { return $this->validAuthenticationResponse($request, $result); } } throw new AccessDeniedHttpException; } /** * Extract the parameters from the given pattern and channel. * * @param string $pattern * @param string $channel * @param callable|string $callback * @return array */ protected function extractAuthParameters($pattern, $channel, $callback) { $callbackParameters = $this->extractParameters($callback); return collect($this->extractChannelKeys($pattern, $channel))->reject(function ($value, $key) { return is_numeric($key); })->map(function ($value, $key) use ($callbackParameters) { return $this->resolveBinding($key, $value, $callbackParameters); })->values()->all(); } /** * Extracts the parameters out of what the user passed to handle the channel authentication. * * @param callable|string $callback * @return \ReflectionParameter[] * * @throws \Exception */ protected function extractParameters($callback) { if (is_callable($callback)) { return (new ReflectionFunction($callback))->getParameters(); } elseif (is_string($callback)) { return $this->extractParametersFromClass($callback); } throw new Exception('Given channel handler is an unknown type.'); } /** * Extracts the parameters out of a class channel's "join" method. * * @param string $callback * @return \ReflectionParameter[] * * @throws \Exception */ protected function extractParametersFromClass($callback) { $reflection = new ReflectionClass($callback); if (! $reflection->hasMethod('join')) { throw new Exception('Class based channel must define a "join" method.'); } return $reflection->getMethod('join')->getParameters(); } /** * Extract the channel keys from the incoming channel name. * * @param string $pattern * @param string $channel * @return array */ protected function extractChannelKeys($pattern, $channel) { preg_match('/^'.preg_replace('/\{(.*?)\}/', '(?<$1>[^\.]+)', $pattern).'/', $channel, $keys); return $keys; } /** * Resolve the given parameter binding. * * @param string $key * @param string $value * @param array $callbackParameters * @return mixed */ protected function resolveBinding($key, $value, $callbackParameters) { $newValue = $this->resolveExplicitBindingIfPossible($key, $value); return $newValue === $value ? $this->resolveImplicitBindingIfPossible( $key, $value, $callbackParameters ) : $newValue; } /** * Resolve an explicit parameter binding if applicable. * * @param string $key * @param mixed $value * @return mixed */ protected function resolveExplicitBindingIfPossible($key, $value) { $binder = $this->binder(); if ($binder && $binder->getBindingCallback($key)) { return call_user_func($binder->getBindingCallback($key), $value); } return $value; } /** * Resolve an implicit parameter binding if applicable. * * @param string $key * @param mixed $value * @param array $callbackParameters * @return mixed * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException */ protected function resolveImplicitBindingIfPossible($key, $value, $callbackParameters) { foreach ($callbackParameters as $parameter) { if (! $this->isImplicitlyBindable($key, $parameter)) { continue; } $className = Reflector::getParameterClassName($parameter); if (is_null($model = (new $className)->resolveRouteBinding($value))) { throw new AccessDeniedHttpException; } return $model; } return $value; } /** * Determine if a given key and parameter is implicitly bindable. * * @param string $key * @param \ReflectionParameter $parameter * @return bool */ protected function isImplicitlyBindable($key, $parameter) { return $parameter->getName() === $key && Reflector::isParameterSubclassOf($parameter, UrlRoutable::class); } /** * Format the channel array into an array of strings. * * @param array $channels * @return array */ protected function formatChannels(array $channels) { return array_map(function ($channel) { return (string) $channel; }, $channels); } /** * Get the model binding registrar instance. * * @return \Illuminate\Contracts\Routing\BindingRegistrar */ protected function binder() { if (! $this->bindingRegistrar) { $this->bindingRegistrar = Container::getInstance()->bound(BindingRegistrar::class) ? Container::getInstance()->make(BindingRegistrar::class) : null; } return $this->bindingRegistrar; } /** * Normalize the given callback into a callable. * * @param mixed $callback * @return callable */ protected function normalizeChannelHandlerToCallable($callback) { return is_callable($callback) ? $callback : function (...$args) use ($callback) { return Container::getInstance() ->make($callback) ->join(...$args); }; } /** * Retrieve the authenticated user using the configured guard (if any). * * @param \Illuminate\Http\Request $request * @param string $channel * @return mixed */ protected function retrieveUser($request, $channel) { $options = $this->retrieveChannelOptions($channel); $guards = $options['guards'] ?? null; if (is_null($guards)) { return $request->user(); } foreach (Arr::wrap($guards) as $guard) { if ($user = $request->user($guard)) { return $user; } } } /** * Retrieve options for a certain channel. * * @param string $channel * @return array */ protected function retrieveChannelOptions($channel) { foreach ($this->channelOptions as $pattern => $options) { if (! $this->channelNameMatchesPattern($channel, $pattern)) { continue; } return $options; } return []; } /** * Check if the channel name from the request matches a pattern from registered channels. * * @param string $channel * @param string $pattern * @return bool */ protected function channelNameMatchesPattern($channel, $pattern) { return preg_match('/^'.preg_replace('/\{(.*?)\}/', '([^\.]+)', $pattern).'$/', $channel); } /** * Get all of the registered channels. * * @return \Illuminate\Support\Collection */ public function getChannels() { return collect($this->channels); } } src/Illuminate/Broadcasting/Broadcasters/LogBroadcaster.php 0000644 00000002074 15107327037 0020066 0 ustar 00 <?php namespace Illuminate\Broadcasting\Broadcasters; use Psr\Log\LoggerInterface; class LogBroadcaster extends Broadcaster { /** * The logger implementation. * * @var \Psr\Log\LoggerInterface */ protected $logger; /** * Create a new broadcaster instance. * * @param \Psr\Log\LoggerInterface $logger * @return void */ public function __construct(LoggerInterface $logger) { $this->logger = $logger; } /** * {@inheritdoc} */ public function auth($request) { // } /** * {@inheritdoc} */ public function validAuthenticationResponse($request, $result) { // } /** * {@inheritdoc} */ public function broadcast(array $channels, $event, array $payload = []) { $channels = implode(', ', $this->formatChannels($channels)); $payload = json_encode($payload, JSON_PRETTY_PRINT); $this->logger->info('Broadcasting ['.$event.'] on channels ['.$channels.'] with payload:'.PHP_EOL.$payload); } } src/Illuminate/Broadcasting/Broadcasters/NullBroadcaster.php 0000644 00000000716 15107327037 0020260 0 ustar 00 <?php namespace Illuminate\Broadcasting\Broadcasters; class NullBroadcaster extends Broadcaster { /** * {@inheritdoc} */ public function auth($request) { // } /** * {@inheritdoc} */ public function validAuthenticationResponse($request, $result) { // } /** * {@inheritdoc} */ public function broadcast(array $channels, $event, array $payload = []) { // } } src/Illuminate/Broadcasting/Broadcasters/UsePusherChannelConventions.php 0000644 00000001465 15107327037 0022640 0 ustar 00 <?php namespace Illuminate\Broadcasting\Broadcasters; use Illuminate\Support\Str; trait UsePusherChannelConventions { /** * Return true if the channel is protected by authentication. * * @param string $channel * @return bool */ public function isGuardedChannel($channel) { return Str::startsWith($channel, ['private-', 'presence-']); } /** * Remove prefix from channel name. * * @param string $channel * @return string */ public function normalizeChannelName($channel) { foreach (['private-encrypted-', 'private-', 'presence-'] as $prefix) { if (Str::startsWith($channel, $prefix)) { return Str::replaceFirst($prefix, '', $channel); } } return $channel; } } src/Illuminate/Broadcasting/Broadcasters/AblyBroadcaster.php 0000644 00000014523 15107327037 0020236 0 ustar 00 <?php namespace Illuminate\Broadcasting\Broadcasters; use Ably\AblyRest; use Ably\Exceptions\AblyException; use Ably\Models\Message as AblyMessage; use Illuminate\Broadcasting\BroadcastException; use Illuminate\Support\Str; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; /** * @author Matthew Hall (matthall28@gmail.com) * @author Taylor Otwell (taylor@laravel.com) */ class AblyBroadcaster extends Broadcaster { /** * The AblyRest SDK instance. * * @var \Ably\AblyRest */ protected $ably; /** * Create a new broadcaster instance. * * @param \Ably\AblyRest $ably * @return void */ public function __construct(AblyRest $ably) { $this->ably = $ably; } /** * Authenticate the incoming request for a given channel. * * @param \Illuminate\Http\Request $request * @return mixed * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException */ public function auth($request) { $channelName = $this->normalizeChannelName($request->channel_name); if (empty($request->channel_name) || ($this->isGuardedChannel($request->channel_name) && ! $this->retrieveUser($request, $channelName))) { throw new AccessDeniedHttpException; } return parent::verifyUserCanAccessChannel( $request, $channelName ); } /** * Return the valid authentication response. * * @param \Illuminate\Http\Request $request * @param mixed $result * @return mixed */ public function validAuthenticationResponse($request, $result) { if (str_starts_with($request->channel_name, 'private')) { $signature = $this->generateAblySignature( $request->channel_name, $request->socket_id ); return ['auth' => $this->getPublicToken().':'.$signature]; } $channelName = $this->normalizeChannelName($request->channel_name); $user = $this->retrieveUser($request, $channelName); $broadcastIdentifier = method_exists($user, 'getAuthIdentifierForBroadcasting') ? $user->getAuthIdentifierForBroadcasting() : $user->getAuthIdentifier(); $signature = $this->generateAblySignature( $request->channel_name, $request->socket_id, $userData = array_filter([ 'user_id' => (string) $broadcastIdentifier, 'user_info' => $result, ]) ); return [ 'auth' => $this->getPublicToken().':'.$signature, 'channel_data' => json_encode($userData), ]; } /** * Generate the signature needed for Ably authentication headers. * * @param string $channelName * @param string $socketId * @param array|null $userData * @return string */ public function generateAblySignature($channelName, $socketId, $userData = null) { return hash_hmac( 'sha256', sprintf('%s:%s%s', $socketId, $channelName, $userData ? ':'.json_encode($userData) : ''), $this->getPrivateToken(), ); } /** * Broadcast the given event. * * @param array $channels * @param string $event * @param array $payload * @return void * * @throws \Illuminate\Broadcasting\BroadcastException */ public function broadcast(array $channels, $event, array $payload = []) { try { foreach ($this->formatChannels($channels) as $channel) { $this->ably->channels->get($channel)->publish( $this->buildAblyMessage($event, $payload) ); } } catch (AblyException $e) { throw new BroadcastException( sprintf('Ably error: %s', $e->getMessage()) ); } } /** * Build an Ably message object for broadcasting. * * @param string $event * @param array $payload * @return \Ably\Models\Message */ protected function buildAblyMessage($event, array $payload = []) { return tap(new AblyMessage, function ($message) use ($event, $payload) { $message->name = $event; $message->data = $payload; $message->connectionKey = data_get($payload, 'socket'); }); } /** * Return true if the channel is protected by authentication. * * @param string $channel * @return bool */ public function isGuardedChannel($channel) { return Str::startsWith($channel, ['private-', 'presence-']); } /** * Remove prefix from channel name. * * @param string $channel * @return string */ public function normalizeChannelName($channel) { if ($this->isGuardedChannel($channel)) { return str_starts_with($channel, 'private-') ? Str::replaceFirst('private-', '', $channel) : Str::replaceFirst('presence-', '', $channel); } return $channel; } /** * Format the channel array into an array of strings. * * @param array $channels * @return array */ protected function formatChannels(array $channels) { return array_map(function ($channel) { $channel = (string) $channel; if (Str::startsWith($channel, ['private-', 'presence-'])) { return str_starts_with($channel, 'private-') ? Str::replaceFirst('private-', 'private:', $channel) : Str::replaceFirst('presence-', 'presence:', $channel); } return 'public:'.$channel; }, $channels); } /** * Get the public token value from the Ably key. * * @return string */ protected function getPublicToken() { return Str::before($this->ably->options->key, ':'); } /** * Get the private token value from the Ably key. * * @return string */ protected function getPrivateToken() { return Str::after($this->ably->options->key, ':'); } /** * Get the underlying Ably SDK instance. * * @return \Ably\AblyRest */ public function getAbly() { return $this->ably; } } src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php 0000644 00000010507 15107327037 0020413 0 ustar 00 <?php namespace Illuminate\Broadcasting\Broadcasters; use Illuminate\Broadcasting\BroadcastException; use Illuminate\Contracts\Redis\Factory as Redis; use Illuminate\Support\Arr; use Predis\Connection\ConnectionException; use RedisException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; class RedisBroadcaster extends Broadcaster { use UsePusherChannelConventions; /** * The Redis instance. * * @var \Illuminate\Contracts\Redis\Factory */ protected $redis; /** * The Redis connection to use for broadcasting. * * @var string|null */ protected $connection = null; /** * The Redis key prefix. * * @var string */ protected $prefix = ''; /** * Create a new broadcaster instance. * * @param \Illuminate\Contracts\Redis\Factory $redis * @param string|null $connection * @param string $prefix * @return void */ public function __construct(Redis $redis, $connection = null, $prefix = '') { $this->redis = $redis; $this->prefix = $prefix; $this->connection = $connection; } /** * Authenticate the incoming request for a given channel. * * @param \Illuminate\Http\Request $request * @return mixed * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException */ public function auth($request) { $channelName = $this->normalizeChannelName( str_replace($this->prefix, '', $request->channel_name) ); if (empty($request->channel_name) || ($this->isGuardedChannel($request->channel_name) && ! $this->retrieveUser($request, $channelName))) { throw new AccessDeniedHttpException; } return parent::verifyUserCanAccessChannel( $request, $channelName ); } /** * Return the valid authentication response. * * @param \Illuminate\Http\Request $request * @param mixed $result * @return mixed */ public function validAuthenticationResponse($request, $result) { if (is_bool($result)) { return json_encode($result); } $channelName = $this->normalizeChannelName($request->channel_name); $user = $this->retrieveUser($request, $channelName); $broadcastIdentifier = method_exists($user, 'getAuthIdentifierForBroadcasting') ? $user->getAuthIdentifierForBroadcasting() : $user->getAuthIdentifier(); return json_encode(['channel_data' => [ 'user_id' => $broadcastIdentifier, 'user_info' => $result, ]]); } /** * Broadcast the given event. * * @param array $channels * @param string $event * @param array $payload * @return void * * @throws \Illuminate\Broadcasting\BroadcastException */ public function broadcast(array $channels, $event, array $payload = []) { if (empty($channels)) { return; } $connection = $this->redis->connection($this->connection); $payload = json_encode([ 'event' => $event, 'data' => $payload, 'socket' => Arr::pull($payload, 'socket'), ]); try { $connection->eval( $this->broadcastMultipleChannelsScript(), 0, $payload, ...$this->formatChannels($channels) ); } catch (ConnectionException|RedisException $e) { throw new BroadcastException( sprintf('Redis error: %s.', $e->getMessage()) ); } } /** * Get the Lua script for broadcasting to multiple channels. * * ARGV[1] - The payload * ARGV[2...] - The channels * * @return string */ protected function broadcastMultipleChannelsScript() { return <<<'LUA' for i = 2, #ARGV do redis.call('publish', ARGV[i], ARGV[1]) end LUA; } /** * Format the channel array into an array of strings. * * @param array $channels * @return array */ protected function formatChannels(array $channels) { return array_map(function ($channel) { return $this->prefix.$channel; }, parent::formatChannels($channels)); } } src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php 0000644 00000013044 15107327037 0020612 0 ustar 00 <?php namespace Illuminate\Broadcasting\Broadcasters; use Illuminate\Broadcasting\BroadcastException; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Pusher\ApiErrorException; use Pusher\Pusher; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; class PusherBroadcaster extends Broadcaster { use UsePusherChannelConventions; /** * The Pusher SDK instance. * * @var \Pusher\Pusher */ protected $pusher; /** * Create a new broadcaster instance. * * @param \Pusher\Pusher $pusher * @return void */ public function __construct(Pusher $pusher) { $this->pusher = $pusher; } /** * Resolve the authenticated user payload for an incoming connection request. * * See: https://pusher.com/docs/channels/library_auth_reference/auth-signatures/#user-authentication * See: https://pusher.com/docs/channels/server_api/authenticating-users/#response * * @param \Illuminate\Http\Request $request * @return array|null */ public function resolveAuthenticatedUser($request) { if (! $user = parent::resolveAuthenticatedUser($request)) { return; } if (method_exists($this->pusher, 'authenticateUser')) { return $this->pusher->authenticateUser($request->socket_id, $user); } $settings = $this->pusher->getSettings(); $encodedUser = json_encode($user); $decodedString = "{$request->socket_id}::user::{$encodedUser}"; $auth = $settings['auth_key'].':'.hash_hmac( 'sha256', $decodedString, $settings['secret'] ); return [ 'auth' => $auth, 'user_data' => $encodedUser, ]; } /** * Authenticate the incoming request for a given channel. * * @param \Illuminate\Http\Request $request * @return mixed * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException */ public function auth($request) { $channelName = $this->normalizeChannelName($request->channel_name); if (empty($request->channel_name) || ($this->isGuardedChannel($request->channel_name) && ! $this->retrieveUser($request, $channelName))) { throw new AccessDeniedHttpException; } return parent::verifyUserCanAccessChannel( $request, $channelName ); } /** * Return the valid authentication response. * * @param \Illuminate\Http\Request $request * @param mixed $result * @return mixed */ public function validAuthenticationResponse($request, $result) { if (str_starts_with($request->channel_name, 'private')) { return $this->decodePusherResponse( $request, method_exists($this->pusher, 'authorizeChannel') ? $this->pusher->authorizeChannel($request->channel_name, $request->socket_id) : $this->pusher->socket_auth($request->channel_name, $request->socket_id) ); } $channelName = $this->normalizeChannelName($request->channel_name); $user = $this->retrieveUser($request, $channelName); $broadcastIdentifier = method_exists($user, 'getAuthIdentifierForBroadcasting') ? $user->getAuthIdentifierForBroadcasting() : $user->getAuthIdentifier(); return $this->decodePusherResponse( $request, method_exists($this->pusher, 'authorizePresenceChannel') ? $this->pusher->authorizePresenceChannel($request->channel_name, $request->socket_id, $broadcastIdentifier, $result) : $this->pusher->presence_auth($request->channel_name, $request->socket_id, $broadcastIdentifier, $result) ); } /** * Decode the given Pusher response. * * @param \Illuminate\Http\Request $request * @param mixed $response * @return array */ protected function decodePusherResponse($request, $response) { if (! $request->input('callback', false)) { return json_decode($response, true); } return response()->json(json_decode($response, true)) ->withCallback($request->callback); } /** * Broadcast the given event. * * @param array $channels * @param string $event * @param array $payload * @return void * * @throws \Illuminate\Broadcasting\BroadcastException */ public function broadcast(array $channels, $event, array $payload = []) { $socket = Arr::pull($payload, 'socket'); $parameters = $socket !== null ? ['socket_id' => $socket] : []; $channels = Collection::make($this->formatChannels($channels)); try { $channels->chunk(100)->each(function ($channels) use ($event, $payload, $parameters) { $this->pusher->trigger($channels->toArray(), $event, $payload, $parameters); }); } catch (ApiErrorException $e) { throw new BroadcastException( sprintf('Pusher error: %s.', $e->getMessage()) ); } } /** * Get the Pusher SDK instance. * * @return \Pusher\Pusher */ public function getPusher() { return $this->pusher; } /** * Set the Pusher SDK instance. * * @param \Pusher\Pusher $pusher * @return void */ public function setPusher($pusher) { $this->pusher = $pusher; } } src/Illuminate/Broadcasting/BroadcastManager.php 0000644 00000030162 15107327037 0015753 0 ustar 00 <?php namespace Illuminate\Broadcasting; use Ably\AblyRest; use Closure; use GuzzleHttp\Client as GuzzleClient; use Illuminate\Broadcasting\Broadcasters\AblyBroadcaster; use Illuminate\Broadcasting\Broadcasters\LogBroadcaster; use Illuminate\Broadcasting\Broadcasters\NullBroadcaster; use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster; use Illuminate\Broadcasting\Broadcasters\RedisBroadcaster; use Illuminate\Bus\UniqueLock; use Illuminate\Contracts\Broadcasting\Factory as FactoryContract; use Illuminate\Contracts\Broadcasting\ShouldBeUnique; use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; use Illuminate\Contracts\Bus\Dispatcher as BusDispatcherContract; use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Contracts\Foundation\CachesRoutes; use InvalidArgumentException; use Psr\Log\LoggerInterface; use Pusher\Pusher; /** * @mixin \Illuminate\Contracts\Broadcasting\Broadcaster */ class BroadcastManager implements FactoryContract { /** * The application instance. * * @var \Illuminate\Contracts\Container\Container */ protected $app; /** * The array of resolved broadcast drivers. * * @var array */ protected $drivers = []; /** * The registered custom driver creators. * * @var array */ protected $customCreators = []; /** * Create a new manager instance. * * @param \Illuminate\Contracts\Container\Container $app * @return void */ public function __construct($app) { $this->app = $app; } /** * Register the routes for handling broadcast channel authentication and sockets. * * @param array|null $attributes * @return void */ public function routes(array $attributes = null) { if ($this->app instanceof CachesRoutes && $this->app->routesAreCached()) { return; } $attributes = $attributes ?: ['middleware' => ['web']]; $this->app['router']->group($attributes, function ($router) { $router->match( ['get', 'post'], '/broadcasting/auth', '\\'.BroadcastController::class.'@authenticate' )->withoutMiddleware([\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class]); }); } /** * Register the routes for handling broadcast user authentication. * * @param array|null $attributes * @return void */ public function userRoutes(array $attributes = null) { if ($this->app instanceof CachesRoutes && $this->app->routesAreCached()) { return; } $attributes = $attributes ?: ['middleware' => ['web']]; $this->app['router']->group($attributes, function ($router) { $router->match( ['get', 'post'], '/broadcasting/user-auth', '\\'.BroadcastController::class.'@authenticateUser' )->withoutMiddleware([\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class]); }); } /** * Register the routes for handling broadcast authentication and sockets. * * Alias of "routes" method. * * @param array|null $attributes * @return void */ public function channelRoutes(array $attributes = null) { $this->routes($attributes); } /** * Get the socket ID for the given request. * * @param \Illuminate\Http\Request|null $request * @return string|null */ public function socket($request = null) { if (! $request && ! $this->app->bound('request')) { return; } $request = $request ?: $this->app['request']; return $request->header('X-Socket-ID'); } /** * Begin broadcasting an event. * * @param mixed|null $event * @return \Illuminate\Broadcasting\PendingBroadcast */ public function event($event = null) { return new PendingBroadcast($this->app->make('events'), $event); } /** * Queue the given event for broadcast. * * @param mixed $event * @return void */ public function queue($event) { if ($event instanceof ShouldBroadcastNow || (is_object($event) && method_exists($event, 'shouldBroadcastNow') && $event->shouldBroadcastNow())) { return $this->app->make(BusDispatcherContract::class)->dispatchNow(new BroadcastEvent(clone $event)); } $queue = null; if (method_exists($event, 'broadcastQueue')) { $queue = $event->broadcastQueue(); } elseif (isset($event->broadcastQueue)) { $queue = $event->broadcastQueue; } elseif (isset($event->queue)) { $queue = $event->queue; } $broadcastEvent = new BroadcastEvent(clone $event); if ($event instanceof ShouldBeUnique) { $broadcastEvent = new UniqueBroadcastEvent(clone $event); if ($this->mustBeUniqueAndCannotAcquireLock($broadcastEvent)) { return; } } $this->app->make('queue') ->connection($event->connection ?? null) ->pushOn($queue, $broadcastEvent); } /** * Determine if the broadcastable event must be unique and determine if we can acquire the necessary lock. * * @param mixed $event * @return bool */ protected function mustBeUniqueAndCannotAcquireLock($event) { return ! (new UniqueLock( method_exists($event, 'uniqueVia') ? $event->uniqueVia() : $this->app->make(Cache::class) ))->acquire($event); } /** * Get a driver instance. * * @param string|null $driver * @return mixed */ public function connection($driver = null) { return $this->driver($driver); } /** * Get a driver instance. * * @param string|null $name * @return mixed */ public function driver($name = null) { $name = $name ?: $this->getDefaultDriver(); return $this->drivers[$name] = $this->get($name); } /** * Attempt to get the connection from the local cache. * * @param string $name * @return \Illuminate\Contracts\Broadcasting\Broadcaster */ protected function get($name) { return $this->drivers[$name] ?? $this->resolve($name); } /** * Resolve the given broadcaster. * * @param string $name * @return \Illuminate\Contracts\Broadcasting\Broadcaster * * @throws \InvalidArgumentException */ protected function resolve($name) { $config = $this->getConfig($name); if (is_null($config)) { throw new InvalidArgumentException("Broadcast connection [{$name}] is not defined."); } if (isset($this->customCreators[$config['driver']])) { return $this->callCustomCreator($config); } $driverMethod = 'create'.ucfirst($config['driver']).'Driver'; if (! method_exists($this, $driverMethod)) { throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported."); } return $this->{$driverMethod}($config); } /** * Call a custom driver creator. * * @param array $config * @return mixed */ protected function callCustomCreator(array $config) { return $this->customCreators[$config['driver']]($this->app, $config); } /** * Create an instance of the driver. * * @param array $config * @return \Illuminate\Contracts\Broadcasting\Broadcaster */ protected function createPusherDriver(array $config) { return new PusherBroadcaster($this->pusher($config)); } /** * Get a Pusher instance for the given configuration. * * @param array $config * @return \Pusher\Pusher */ public function pusher(array $config) { $pusher = new Pusher( $config['key'], $config['secret'], $config['app_id'], $config['options'] ?? [], isset($config['client_options']) && ! empty($config['client_options']) ? new GuzzleClient($config['client_options']) : null, ); if ($config['log'] ?? false) { $pusher->setLogger($this->app->make(LoggerInterface::class)); } return $pusher; } /** * Create an instance of the driver. * * @param array $config * @return \Illuminate\Contracts\Broadcasting\Broadcaster */ protected function createAblyDriver(array $config) { return new AblyBroadcaster($this->ably($config)); } /** * Get an Ably instance for the given configuration. * * @param array $config * @return \Ably\AblyRest */ public function ably(array $config) { return new AblyRest($config); } /** * Create an instance of the driver. * * @param array $config * @return \Illuminate\Contracts\Broadcasting\Broadcaster */ protected function createRedisDriver(array $config) { return new RedisBroadcaster( $this->app->make('redis'), $config['connection'] ?? null, $this->app['config']->get('database.redis.options.prefix', '') ); } /** * Create an instance of the driver. * * @param array $config * @return \Illuminate\Contracts\Broadcasting\Broadcaster */ protected function createLogDriver(array $config) { return new LogBroadcaster( $this->app->make(LoggerInterface::class) ); } /** * Create an instance of the driver. * * @param array $config * @return \Illuminate\Contracts\Broadcasting\Broadcaster */ protected function createNullDriver(array $config) { return new NullBroadcaster; } /** * Get the connection configuration. * * @param string $name * @return array */ protected function getConfig($name) { if (! is_null($name) && $name !== 'null') { return $this->app['config']["broadcasting.connections.{$name}"]; } return ['driver' => 'null']; } /** * Get the default driver name. * * @return string */ public function getDefaultDriver() { return $this->app['config']['broadcasting.default']; } /** * Set the default driver name. * * @param string $name * @return void */ public function setDefaultDriver($name) { $this->app['config']['broadcasting.default'] = $name; } /** * Disconnect the given disk and remove from local cache. * * @param string|null $name * @return void */ public function purge($name = null) { $name ??= $this->getDefaultDriver(); unset($this->drivers[$name]); } /** * Register a custom driver creator Closure. * * @param string $driver * @param \Closure $callback * @return $this */ public function extend($driver, Closure $callback) { $this->customCreators[$driver] = $callback; return $this; } /** * Get the application instance used by the manager. * * @return \Illuminate\Contracts\Foundation\Application */ public function getApplication() { return $this->app; } /** * Set the application instance used by the manager. * * @param \Illuminate\Contracts\Foundation\Application $app * @return $this */ public function setApplication($app) { $this->app = $app; return $this; } /** * Forget all of the resolved driver instances. * * @return $this */ public function forgetDrivers() { $this->drivers = []; return $this; } /** * Dynamically call the default driver instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->driver()->$method(...$parameters); } } src/Illuminate/Broadcasting/BroadcastServiceProvider.php 0000644 00000002160 15107327037 0017511 0 ustar 00 <?php namespace Illuminate\Broadcasting; use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract; use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactory; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; class BroadcastServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton(BroadcastManager::class, fn ($app) => new BroadcastManager($app)); $this->app->singleton(BroadcasterContract::class, function ($app) { return $app->make(BroadcastManager::class)->connection(); }); $this->app->alias( BroadcastManager::class, BroadcastingFactory::class ); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return [ BroadcastManager::class, BroadcastingFactory::class, BroadcasterContract::class, ]; } } src/Illuminate/Broadcasting/UniqueBroadcastEvent.php 0000644 00000002776 15107327037 0016663 0 ustar 00 <?php namespace Illuminate\Broadcasting; use Illuminate\Container\Container; use Illuminate\Contracts\Cache\Repository; use Illuminate\Contracts\Queue\ShouldBeUnique; class UniqueBroadcastEvent extends BroadcastEvent implements ShouldBeUnique { /** * The unique lock identifier. * * @var mixed */ public $uniqueId; /** * The number of seconds the unique lock should be maintained. * * @var int */ public $uniqueFor; /** * Create a new event instance. * * @param mixed $event * @return void */ public function __construct($event) { $this->uniqueId = get_class($event); if (method_exists($event, 'uniqueId')) { $this->uniqueId .= $event->uniqueId(); } elseif (property_exists($event, 'uniqueId')) { $this->uniqueId .= $event->uniqueId; } if (method_exists($event, 'uniqueFor')) { $this->uniqueFor = $event->uniqueFor(); } elseif (property_exists($event, 'uniqueFor')) { $this->uniqueFor = $event->uniqueFor; } parent::__construct($event); } /** * Resolve the cache implementation that should manage the event's uniqueness. * * @return \Illuminate\Contracts\Cache\Repository */ public function uniqueVia() { return method_exists($this->event, 'uniqueVia') ? $this->event->uniqueVia() : Container::getInstance()->make(Repository::class); } } src/Illuminate/Broadcasting/PendingBroadcast.php 0000644 00000002736 15107327037 0015773 0 ustar 00 <?php namespace Illuminate\Broadcasting; use Illuminate\Contracts\Events\Dispatcher; class PendingBroadcast { /** * The event dispatcher implementation. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * The event instance. * * @var mixed */ protected $event; /** * Create a new pending broadcast instance. * * @param \Illuminate\Contracts\Events\Dispatcher $events * @param mixed $event * @return void */ public function __construct(Dispatcher $events, $event) { $this->event = $event; $this->events = $events; } /** * Broadcast the event using a specific broadcaster. * * @param string|null $connection * @return $this */ public function via($connection = null) { if (method_exists($this->event, 'broadcastVia')) { $this->event->broadcastVia($connection); } return $this; } /** * Broadcast the event to everyone except the current user. * * @return $this */ public function toOthers() { if (method_exists($this->event, 'dontBroadcastToCurrentUser')) { $this->event->dontBroadcastToCurrentUser(); } return $this; } /** * Handle the object's destruction. * * @return void */ public function __destruct() { $this->events->dispatch($this->event); } } src/Illuminate/Broadcasting/composer.json 0000644 00000002430 15107327037 0014564 0 ustar 00 { "name": "illuminate/broadcasting", "description": "The Illuminate Broadcasting package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "psr/log": "^1.0|^2.0|^3.0", "illuminate/bus": "^10.0", "illuminate/collections": "^10.0", "illuminate/container": "^10.0", "illuminate/contracts": "^10.0", "illuminate/queue": "^10.0", "illuminate/support": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Broadcasting\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "ext-hash": "Required to use the Ably and Pusher broadcast drivers.", "ably/ably-php": "Required to use the Ably broadcast driver (^1.0).", "pusher/pusher-php-server": "Required to use the Pusher broadcast driver (^6.0|^7.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Broadcasting/LICENSE.md 0000644 00000002063 15107327037 0013450 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Broadcasting/PresenceChannel.php 0000644 00000000446 15107327037 0015615 0 ustar 00 <?php namespace Illuminate\Broadcasting; class PresenceChannel extends Channel { /** * Create a new channel instance. * * @param string $name * @return void */ public function __construct($name) { parent::__construct('presence-'.$name); } } src/Illuminate/Broadcasting/BroadcastEvent.php 0000644 00000007453 15107327037 0015471 0 ustar 00 <?php namespace Illuminate\Broadcasting; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactory; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Arr; use ReflectionClass; use ReflectionProperty; class BroadcastEvent implements ShouldQueue { use Queueable; /** * The event instance. * * @var mixed */ public $event; /** * The number of times the job may be attempted. * * @var int */ public $tries; /** * The number of seconds the job can run before timing out. * * @var int */ public $timeout; /** * The number of seconds to wait before retrying the job when encountering an uncaught exception. * * @var int */ public $backoff; /** * The maximum number of unhandled exceptions to allow before failing. * * @var int */ public $maxExceptions; /** * Create a new job handler instance. * * @param mixed $event * @return void */ public function __construct($event) { $this->event = $event; $this->tries = property_exists($event, 'tries') ? $event->tries : null; $this->timeout = property_exists($event, 'timeout') ? $event->timeout : null; $this->backoff = property_exists($event, 'backoff') ? $event->backoff : null; $this->afterCommit = property_exists($event, 'afterCommit') ? $event->afterCommit : null; $this->maxExceptions = property_exists($event, 'maxExceptions') ? $event->maxExceptions : null; } /** * Handle the queued job. * * @param \Illuminate\Contracts\Broadcasting\Factory $manager * @return void */ public function handle(BroadcastingFactory $manager) { $name = method_exists($this->event, 'broadcastAs') ? $this->event->broadcastAs() : get_class($this->event); $channels = Arr::wrap($this->event->broadcastOn()); if (empty($channels)) { return; } $connections = method_exists($this->event, 'broadcastConnections') ? $this->event->broadcastConnections() : [null]; $payload = $this->getPayloadFromEvent($this->event); foreach ($connections as $connection) { $manager->connection($connection)->broadcast( $channels, $name, $payload ); } } /** * Get the payload for the given event. * * @param mixed $event * @return array */ protected function getPayloadFromEvent($event) { if (method_exists($event, 'broadcastWith') && ! is_null($payload = $event->broadcastWith())) { return array_merge($payload, ['socket' => data_get($event, 'socket')]); } $payload = []; foreach ((new ReflectionClass($event))->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { $payload[$property->getName()] = $this->formatProperty($property->getValue($event)); } unset($payload['broadcastQueue']); return $payload; } /** * Format the given value for a property. * * @param mixed $value * @return mixed */ protected function formatProperty($value) { if ($value instanceof Arrayable) { return $value->toArray(); } return $value; } /** * Get the display name for the queued job. * * @return string */ public function displayName() { return get_class($this->event); } /** * Prepare the instance for cloning. * * @return void */ public function __clone() { $this->event = clone $this->event; } } src/Illuminate/Broadcasting/PrivateChannel.php 0000644 00000000762 15107327037 0015464 0 ustar 00 <?php namespace Illuminate\Broadcasting; use Illuminate\Contracts\Broadcasting\HasBroadcastChannel; class PrivateChannel extends Channel { /** * Create a new channel instance. * * @param \Illuminate\Contracts\Broadcasting\HasBroadcastChannel|string $name * @return void */ public function __construct($name) { $name = $name instanceof HasBroadcastChannel ? $name->broadcastChannel() : $name; parent::__construct('private-'.$name); } } src/Illuminate/Broadcasting/BroadcastController.php 0000644 00000002225 15107327037 0016523 0 ustar 00 <?php namespace Illuminate\Broadcasting; use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Broadcast; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; class BroadcastController extends Controller { /** * Authenticate the request for channel access. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function authenticate(Request $request) { if ($request->hasSession()) { $request->session()->reflash(); } return Broadcast::auth($request); } /** * Authenticate the current user. * * See: https://pusher.com/docs/channels/server_api/authenticating-users/#user-authentication. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function authenticateUser(Request $request) { if ($request->hasSession()) { $request->session()->reflash(); } return Broadcast::resolveAuthenticatedUser($request) ?? throw new AccessDeniedHttpException; } } src/Illuminate/Broadcasting/EncryptedPrivateChannel.php 0000644 00000000467 15107327037 0017344 0 ustar 00 <?php namespace Illuminate\Broadcasting; class EncryptedPrivateChannel extends Channel { /** * Create a new channel instance. * * @param string $name * @return void */ public function __construct($name) { parent::__construct('private-encrypted-'.$name); } } src/Illuminate/Broadcasting/InteractsWithSockets.php 0000644 00000001235 15107327037 0016701 0 ustar 00 <?php namespace Illuminate\Broadcasting; use Illuminate\Support\Facades\Broadcast; trait InteractsWithSockets { /** * The socket ID for the user that raised the event. * * @var string|null */ public $socket; /** * Exclude the current user from receiving the broadcast. * * @return $this */ public function dontBroadcastToCurrentUser() { $this->socket = Broadcast::socket(); return $this; } /** * Broadcast the event to everyone. * * @return $this */ public function broadcastToEveryone() { $this->socket = null; return $this; } } src/Illuminate/Broadcasting/BroadcastException.php 0000644 00000000177 15107327037 0016342 0 ustar 00 <?php namespace Illuminate\Broadcasting; use RuntimeException; class BroadcastException extends RuntimeException { // } src/Illuminate/Filesystem/LockableFile.php 0000644 00000007336 15107327037 0014625 0 ustar 00 <?php namespace Illuminate\Filesystem; use Exception; use Illuminate\Contracts\Filesystem\LockTimeoutException; class LockableFile { /** * The file resource. * * @var resource */ protected $handle; /** * The file path. * * @var string */ protected $path; /** * Indicates if the file is locked. * * @var bool */ protected $isLocked = false; /** * Create a new File instance. * * @param string $path * @param string $mode * @return void */ public function __construct($path, $mode) { $this->path = $path; $this->ensureDirectoryExists($path); $this->createResource($path, $mode); } /** * Create the file's directory if necessary. * * @param string $path * @return void */ protected function ensureDirectoryExists($path) { if (! file_exists(dirname($path))) { @mkdir(dirname($path), 0777, true); } } /** * Create the file resource. * * @param string $path * @param string $mode * @return void * * @throws \Exception */ protected function createResource($path, $mode) { $this->handle = @fopen($path, $mode); if (! $this->handle) { throw new Exception('Unable to create lockable file: '.$path.'. Please ensure you have permission to create files in this location.'); } } /** * Read the file contents. * * @param int|null $length * @return string */ public function read($length = null) { clearstatcache(true, $this->path); return fread($this->handle, $length ?? ($this->size() ?: 1)); } /** * Get the file size. * * @return int */ public function size() { return filesize($this->path); } /** * Write to the file. * * @param string $contents * @return $this */ public function write($contents) { fwrite($this->handle, $contents); fflush($this->handle); return $this; } /** * Truncate the file. * * @return $this */ public function truncate() { rewind($this->handle); ftruncate($this->handle, 0); return $this; } /** * Get a shared lock on the file. * * @param bool $block * @return $this * * @throws \Illuminate\Contracts\Filesystem\LockTimeoutException */ public function getSharedLock($block = false) { if (! flock($this->handle, LOCK_SH | ($block ? 0 : LOCK_NB))) { throw new LockTimeoutException("Unable to acquire file lock at path [{$this->path}]."); } $this->isLocked = true; return $this; } /** * Get an exclusive lock on the file. * * @param bool $block * @return bool * * @throws \Illuminate\Contracts\Filesystem\LockTimeoutException */ public function getExclusiveLock($block = false) { if (! flock($this->handle, LOCK_EX | ($block ? 0 : LOCK_NB))) { throw new LockTimeoutException("Unable to acquire file lock at path [{$this->path}]."); } $this->isLocked = true; return $this; } /** * Release the lock on the file. * * @return $this */ public function releaseLock() { flock($this->handle, LOCK_UN); $this->isLocked = false; return $this; } /** * Close the file. * * @return bool */ public function close() { if ($this->isLocked) { $this->releaseLock(); } return fclose($this->handle); } } src/Illuminate/Filesystem/FilesystemManager.php 0000644 00000026571 15107327037 0015732 0 ustar 00 <?php namespace Illuminate\Filesystem; use Aws\S3\S3Client; use Closure; use Illuminate\Contracts\Filesystem\Factory as FactoryContract; use Illuminate\Support\Arr; use InvalidArgumentException; use League\Flysystem\AwsS3V3\AwsS3V3Adapter as S3Adapter; use League\Flysystem\AwsS3V3\PortableVisibilityConverter as AwsS3PortableVisibilityConverter; use League\Flysystem\Filesystem as Flysystem; use League\Flysystem\FilesystemAdapter as FlysystemAdapter; use League\Flysystem\Ftp\FtpAdapter; use League\Flysystem\Ftp\FtpConnectionOptions; use League\Flysystem\Local\LocalFilesystemAdapter as LocalAdapter; use League\Flysystem\PathPrefixing\PathPrefixedAdapter; use League\Flysystem\PhpseclibV3\SftpAdapter; use League\Flysystem\PhpseclibV3\SftpConnectionProvider; use League\Flysystem\ReadOnly\ReadOnlyFilesystemAdapter; use League\Flysystem\UnixVisibility\PortableVisibilityConverter; use League\Flysystem\Visibility; /** * @mixin \Illuminate\Contracts\Filesystem\Filesystem * @mixin \Illuminate\Filesystem\FilesystemAdapter */ class FilesystemManager implements FactoryContract { /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The array of resolved filesystem drivers. * * @var array */ protected $disks = []; /** * The registered custom driver creators. * * @var array */ protected $customCreators = []; /** * Create a new filesystem manager instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function __construct($app) { $this->app = $app; } /** * Get a filesystem instance. * * @param string|null $name * @return \Illuminate\Contracts\Filesystem\Filesystem */ public function drive($name = null) { return $this->disk($name); } /** * Get a filesystem instance. * * @param string|null $name * @return \Illuminate\Contracts\Filesystem\Filesystem */ public function disk($name = null) { $name = $name ?: $this->getDefaultDriver(); return $this->disks[$name] = $this->get($name); } /** * Get a default cloud filesystem instance. * * @return \Illuminate\Contracts\Filesystem\Cloud */ public function cloud() { $name = $this->getDefaultCloudDriver(); return $this->disks[$name] = $this->get($name); } /** * Build an on-demand disk. * * @param string|array $config * @return \Illuminate\Contracts\Filesystem\Filesystem */ public function build($config) { return $this->resolve('ondemand', is_array($config) ? $config : [ 'driver' => 'local', 'root' => $config, ]); } /** * Attempt to get the disk from the local cache. * * @param string $name * @return \Illuminate\Contracts\Filesystem\Filesystem */ protected function get($name) { return $this->disks[$name] ?? $this->resolve($name); } /** * Resolve the given disk. * * @param string $name * @param array|null $config * @return \Illuminate\Contracts\Filesystem\Filesystem * * @throws \InvalidArgumentException */ protected function resolve($name, $config = null) { $config ??= $this->getConfig($name); if (empty($config['driver'])) { throw new InvalidArgumentException("Disk [{$name}] does not have a configured driver."); } $name = $config['driver']; if (isset($this->customCreators[$name])) { return $this->callCustomCreator($config); } $driverMethod = 'create'.ucfirst($name).'Driver'; if (! method_exists($this, $driverMethod)) { throw new InvalidArgumentException("Driver [{$name}] is not supported."); } return $this->{$driverMethod}($config); } /** * Call a custom driver creator. * * @param array $config * @return \Illuminate\Contracts\Filesystem\Filesystem */ protected function callCustomCreator(array $config) { return $this->customCreators[$config['driver']]($this->app, $config); } /** * Create an instance of the local driver. * * @param array $config * @return \Illuminate\Contracts\Filesystem\Filesystem */ public function createLocalDriver(array $config) { $visibility = PortableVisibilityConverter::fromArray( $config['permissions'] ?? [], $config['directory_visibility'] ?? $config['visibility'] ?? Visibility::PRIVATE ); $links = ($config['links'] ?? null) === 'skip' ? LocalAdapter::SKIP_LINKS : LocalAdapter::DISALLOW_LINKS; $adapter = new LocalAdapter( $config['root'], $visibility, $config['lock'] ?? LOCK_EX, $links ); return new FilesystemAdapter($this->createFlysystem($adapter, $config), $adapter, $config); } /** * Create an instance of the ftp driver. * * @param array $config * @return \Illuminate\Contracts\Filesystem\Filesystem */ public function createFtpDriver(array $config) { if (! isset($config['root'])) { $config['root'] = ''; } $adapter = new FtpAdapter(FtpConnectionOptions::fromArray($config)); return new FilesystemAdapter($this->createFlysystem($adapter, $config), $adapter, $config); } /** * Create an instance of the sftp driver. * * @param array $config * @return \Illuminate\Contracts\Filesystem\Filesystem */ public function createSftpDriver(array $config) { $provider = SftpConnectionProvider::fromArray($config); $root = $config['root'] ?? '/'; $visibility = PortableVisibilityConverter::fromArray( $config['permissions'] ?? [] ); $adapter = new SftpAdapter($provider, $root, $visibility); return new FilesystemAdapter($this->createFlysystem($adapter, $config), $adapter, $config); } /** * Create an instance of the Amazon S3 driver. * * @param array $config * @return \Illuminate\Contracts\Filesystem\Cloud */ public function createS3Driver(array $config) { $s3Config = $this->formatS3Config($config); $root = (string) ($s3Config['root'] ?? ''); $visibility = new AwsS3PortableVisibilityConverter( $config['visibility'] ?? Visibility::PUBLIC ); $streamReads = $s3Config['stream_reads'] ?? false; $client = new S3Client($s3Config); $adapter = new S3Adapter($client, $s3Config['bucket'], $root, $visibility, null, $config['options'] ?? [], $streamReads); return new AwsS3V3Adapter( $this->createFlysystem($adapter, $config), $adapter, $s3Config, $client ); } /** * Format the given S3 configuration with the default options. * * @param array $config * @return array */ protected function formatS3Config(array $config) { $config += ['version' => 'latest']; if (! empty($config['key']) && ! empty($config['secret'])) { $config['credentials'] = Arr::only($config, ['key', 'secret']); } if (! empty($config['token'])) { $config['credentials']['token'] = $config['token']; } return Arr::except($config, ['token']); } /** * Create a scoped driver. * * @param array $config * @return \Illuminate\Contracts\Filesystem\Filesystem */ public function createScopedDriver(array $config) { if (empty($config['disk'])) { throw new InvalidArgumentException('Scoped disk is missing "disk" configuration option.'); } elseif (empty($config['prefix'])) { throw new InvalidArgumentException('Scoped disk is missing "prefix" configuration option.'); } return $this->build(tap( is_string($config['disk']) ? $this->getConfig($config['disk']) : $config['disk'], function (&$parent) use ($config) { $parent['prefix'] = $config['prefix']; if (isset($config['visibility'])) { $parent['visibility'] = $config['visibility']; } } )); } /** * Create a Flysystem instance with the given adapter. * * @param \League\Flysystem\FilesystemAdapter $adapter * @param array $config * @return \League\Flysystem\FilesystemOperator */ protected function createFlysystem(FlysystemAdapter $adapter, array $config) { if ($config['read-only'] ?? false === true) { $adapter = new ReadOnlyFilesystemAdapter($adapter); } if (! empty($config['prefix'])) { $adapter = new PathPrefixedAdapter($adapter, $config['prefix']); } return new Flysystem($adapter, Arr::only($config, [ 'directory_visibility', 'disable_asserts', 'temporary_url', 'url', 'visibility', ])); } /** * Set the given disk instance. * * @param string $name * @param mixed $disk * @return $this */ public function set($name, $disk) { $this->disks[$name] = $disk; return $this; } /** * Get the filesystem connection configuration. * * @param string $name * @return array */ protected function getConfig($name) { return $this->app['config']["filesystems.disks.{$name}"] ?: []; } /** * Get the default driver name. * * @return string */ public function getDefaultDriver() { return $this->app['config']['filesystems.default']; } /** * Get the default cloud driver name. * * @return string */ public function getDefaultCloudDriver() { return $this->app['config']['filesystems.cloud'] ?? 's3'; } /** * Unset the given disk instances. * * @param array|string $disk * @return $this */ public function forgetDisk($disk) { foreach ((array) $disk as $diskName) { unset($this->disks[$diskName]); } return $this; } /** * Disconnect the given disk and remove from local cache. * * @param string|null $name * @return void */ public function purge($name = null) { $name ??= $this->getDefaultDriver(); unset($this->disks[$name]); } /** * Register a custom driver creator Closure. * * @param string $driver * @param \Closure $callback * @return $this */ public function extend($driver, Closure $callback) { $this->customCreators[$driver] = $callback; return $this; } /** * Set the application instance used by the manager. * * @param \Illuminate\Contracts\Foundation\Application $app * @return $this */ public function setApplication($app) { $this->app = $app; return $this; } /** * Dynamically call the default driver instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->disk()->$method(...$parameters); } } src/Illuminate/Filesystem/FilesystemAdapter.php 0000644 00000062741 15107327037 0015737 0 ustar 00 <?php namespace Illuminate\Filesystem; use Closure; use Illuminate\Contracts\Filesystem\Cloud as CloudFilesystemContract; use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract; use Illuminate\Http\File; use Illuminate\Http\UploadedFile; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; use League\Flysystem\FilesystemAdapter as FlysystemAdapter; use League\Flysystem\FilesystemOperator; use League\Flysystem\Ftp\FtpAdapter; use League\Flysystem\Local\LocalFilesystemAdapter as LocalAdapter; use League\Flysystem\PathPrefixer; use League\Flysystem\PhpseclibV3\SftpAdapter; use League\Flysystem\StorageAttributes; use League\Flysystem\UnableToCopyFile; use League\Flysystem\UnableToCreateDirectory; use League\Flysystem\UnableToDeleteDirectory; use League\Flysystem\UnableToDeleteFile; use League\Flysystem\UnableToMoveFile; use League\Flysystem\UnableToProvideChecksum; use League\Flysystem\UnableToReadFile; use League\Flysystem\UnableToRetrieveMetadata; use League\Flysystem\UnableToSetVisibility; use League\Flysystem\UnableToWriteFile; use League\Flysystem\Visibility; use PHPUnit\Framework\Assert as PHPUnit; use Psr\Http\Message\StreamInterface; use RuntimeException; use Symfony\Component\HttpFoundation\StreamedResponse; /** * @mixin \League\Flysystem\FilesystemOperator */ class FilesystemAdapter implements CloudFilesystemContract { use Conditionable; use Macroable { __call as macroCall; } /** * The Flysystem filesystem implementation. * * @var \League\Flysystem\FilesystemOperator */ protected $driver; /** * The Flysystem adapter implementation. * * @var \League\Flysystem\FilesystemAdapter */ protected $adapter; /** * The filesystem configuration. * * @var array */ protected $config; /** * The Flysystem PathPrefixer instance. * * @var \League\Flysystem\PathPrefixer */ protected $prefixer; /** * The temporary URL builder callback. * * @var \Closure|null */ protected $temporaryUrlCallback; /** * Create a new filesystem adapter instance. * * @param \League\Flysystem\FilesystemOperator $driver * @param \League\Flysystem\FilesystemAdapter $adapter * @param array $config * @return void */ public function __construct(FilesystemOperator $driver, FlysystemAdapter $adapter, array $config = []) { $this->driver = $driver; $this->adapter = $adapter; $this->config = $config; $separator = $config['directory_separator'] ?? DIRECTORY_SEPARATOR; $this->prefixer = new PathPrefixer($config['root'] ?? '', $separator); if (isset($config['prefix'])) { $this->prefixer = new PathPrefixer($this->prefixer->prefixPath($config['prefix']), $separator); } } /** * Assert that the given file or directory exists. * * @param string|array $path * @param string|null $content * @return $this */ public function assertExists($path, $content = null) { clearstatcache(); $paths = Arr::wrap($path); foreach ($paths as $path) { PHPUnit::assertTrue( $this->exists($path), "Unable to find a file or directory at path [{$path}]." ); if (! is_null($content)) { $actual = $this->get($path); PHPUnit::assertSame( $content, $actual, "File or directory [{$path}] was found, but content [{$actual}] does not match [{$content}]." ); } } return $this; } /** * Assert that the given file or directory does not exist. * * @param string|array $path * @return $this */ public function assertMissing($path) { clearstatcache(); $paths = Arr::wrap($path); foreach ($paths as $path) { PHPUnit::assertFalse( $this->exists($path), "Found unexpected file or directory at path [{$path}]." ); } return $this; } /** * Assert that the given directory is empty. * * @param string $path * @return $this */ public function assertDirectoryEmpty($path) { PHPUnit::assertEmpty( $this->allFiles($path), "Directory [{$path}] is not empty." ); return $this; } /** * Determine if a file or directory exists. * * @param string $path * @return bool */ public function exists($path) { return $this->driver->has($path); } /** * Determine if a file or directory is missing. * * @param string $path * @return bool */ public function missing($path) { return ! $this->exists($path); } /** * Determine if a file exists. * * @param string $path * @return bool */ public function fileExists($path) { return $this->driver->fileExists($path); } /** * Determine if a file is missing. * * @param string $path * @return bool */ public function fileMissing($path) { return ! $this->fileExists($path); } /** * Determine if a directory exists. * * @param string $path * @return bool */ public function directoryExists($path) { return $this->driver->directoryExists($path); } /** * Determine if a directory is missing. * * @param string $path * @return bool */ public function directoryMissing($path) { return ! $this->directoryExists($path); } /** * Get the full path for the file at the given "short" path. * * @param string $path * @return string */ public function path($path) { return $this->prefixer->prefixPath($path); } /** * Get the contents of a file. * * @param string $path * @return string|null */ public function get($path) { try { return $this->driver->read($path); } catch (UnableToReadFile $e) { throw_if($this->throwsExceptions(), $e); } } /** * Get the contents of a file as decoded JSON. * * @param string $path * @param int $flags * @return array|null */ public function json($path, $flags = 0) { $content = $this->get($path); return is_null($content) ? null : json_decode($content, true, 512, $flags); } /** * Create a streamed response for a given file. * * @param string $path * @param string|null $name * @param array $headers * @param string|null $disposition * @return \Symfony\Component\HttpFoundation\StreamedResponse */ public function response($path, $name = null, array $headers = [], $disposition = 'inline') { $response = new StreamedResponse; if (! array_key_exists('Content-Type', $headers)) { $headers['Content-Type'] = $this->mimeType($path); } if (! array_key_exists('Content-Length', $headers)) { $headers['Content-Length'] = $this->size($path); } if (! array_key_exists('Content-Disposition', $headers)) { $filename = $name ?? basename($path); $disposition = $response->headers->makeDisposition( $disposition, $filename, $this->fallbackName($filename) ); $headers['Content-Disposition'] = $disposition; } $response->headers->replace($headers); $response->setCallback(function () use ($path) { $stream = $this->readStream($path); fpassthru($stream); fclose($stream); }); return $response; } /** * Create a streamed download response for a given file. * * @param string $path * @param string|null $name * @return \Symfony\Component\HttpFoundation\StreamedResponse */ public function download($path, $name = null, array $headers = []) { return $this->response($path, $name, $headers, 'attachment'); } /** * Convert the string to ASCII characters that are equivalent to the given name. * * @param string $name * @return string */ protected function fallbackName($name) { return str_replace('%', '', Str::ascii($name)); } /** * Write the contents of a file. * * @param string $path * @param \Psr\Http\Message\StreamInterface|\Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|resource $contents * @param mixed $options * @return string|bool */ public function put($path, $contents, $options = []) { $options = is_string($options) ? ['visibility' => $options] : (array) $options; // If the given contents is actually a file or uploaded file instance than we will // automatically store the file using a stream. This provides a convenient path // for the developer to store streams without managing them manually in code. if ($contents instanceof File || $contents instanceof UploadedFile) { return $this->putFile($path, $contents, $options); } try { if ($contents instanceof StreamInterface) { $this->driver->writeStream($path, $contents->detach(), $options); return true; } is_resource($contents) ? $this->driver->writeStream($path, $contents, $options) : $this->driver->write($path, $contents, $options); } catch (UnableToWriteFile|UnableToSetVisibility $e) { throw_if($this->throwsExceptions(), $e); return false; } return true; } /** * Store the uploaded file on the disk. * * @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $path * @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|array|null $file * @param mixed $options * @return string|false */ public function putFile($path, $file = null, $options = []) { if (is_null($file) || is_array($file)) { [$path, $file, $options] = ['', $path, $file ?? []]; } $file = is_string($file) ? new File($file) : $file; return $this->putFileAs($path, $file, $file->hashName(), $options); } /** * Store the uploaded file on the disk with a given name. * * @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $path * @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|array|null $file * @param string|array|null $name * @param mixed $options * @return string|false */ public function putFileAs($path, $file, $name = null, $options = []) { if (is_null($name) || is_array($name)) { [$path, $file, $name, $options] = ['', $path, $file, $name ?? []]; } $stream = fopen(is_string($file) ? $file : $file->getRealPath(), 'r'); // Next, we will format the path of the file and store the file using a stream since // they provide better performance than alternatives. Once we write the file this // stream will get closed automatically by us so the developer doesn't have to. $result = $this->put( $path = trim($path.'/'.$name, '/'), $stream, $options ); if (is_resource($stream)) { fclose($stream); } return $result ? $path : false; } /** * Get the visibility for the given path. * * @param string $path * @return string */ public function getVisibility($path) { if ($this->driver->visibility($path) == Visibility::PUBLIC) { return FilesystemContract::VISIBILITY_PUBLIC; } return FilesystemContract::VISIBILITY_PRIVATE; } /** * Set the visibility for the given path. * * @param string $path * @param string $visibility * @return bool */ public function setVisibility($path, $visibility) { try { $this->driver->setVisibility($path, $this->parseVisibility($visibility)); } catch (UnableToSetVisibility $e) { throw_if($this->throwsExceptions(), $e); return false; } return true; } /** * Prepend to a file. * * @param string $path * @param string $data * @param string $separator * @return bool */ public function prepend($path, $data, $separator = PHP_EOL) { if ($this->fileExists($path)) { return $this->put($path, $data.$separator.$this->get($path)); } return $this->put($path, $data); } /** * Append to a file. * * @param string $path * @param string $data * @param string $separator * @return bool */ public function append($path, $data, $separator = PHP_EOL) { if ($this->fileExists($path)) { return $this->put($path, $this->get($path).$separator.$data); } return $this->put($path, $data); } /** * Delete the file at a given path. * * @param string|array $paths * @return bool */ public function delete($paths) { $paths = is_array($paths) ? $paths : func_get_args(); $success = true; foreach ($paths as $path) { try { $this->driver->delete($path); } catch (UnableToDeleteFile $e) { throw_if($this->throwsExceptions(), $e); $success = false; } } return $success; } /** * Copy a file to a new location. * * @param string $from * @param string $to * @return bool */ public function copy($from, $to) { try { $this->driver->copy($from, $to); } catch (UnableToCopyFile $e) { throw_if($this->throwsExceptions(), $e); return false; } return true; } /** * Move a file to a new location. * * @param string $from * @param string $to * @return bool */ public function move($from, $to) { try { $this->driver->move($from, $to); } catch (UnableToMoveFile $e) { throw_if($this->throwsExceptions(), $e); return false; } return true; } /** * Get the file size of a given file. * * @param string $path * @return int */ public function size($path) { return $this->driver->fileSize($path); } /** * Get the checksum for a file. * * @return string|false * * @throws UnableToProvideChecksum */ public function checksum(string $path, array $options = []) { try { return $this->driver->checksum($path, $options); } catch (UnableToProvideChecksum $e) { throw_if($this->throwsExceptions(), $e); return false; } } /** * Get the mime-type of a given file. * * @param string $path * @return string|false */ public function mimeType($path) { try { return $this->driver->mimeType($path); } catch (UnableToRetrieveMetadata $e) { throw_if($this->throwsExceptions(), $e); } return false; } /** * Get the file's last modification time. * * @param string $path * @return int */ public function lastModified($path) { return $this->driver->lastModified($path); } /** * {@inheritdoc} */ public function readStream($path) { try { return $this->driver->readStream($path); } catch (UnableToReadFile $e) { throw_if($this->throwsExceptions(), $e); } } /** * {@inheritdoc} */ public function writeStream($path, $resource, array $options = []) { try { $this->driver->writeStream($path, $resource, $options); } catch (UnableToWriteFile|UnableToSetVisibility $e) { throw_if($this->throwsExceptions(), $e); return false; } return true; } /** * Get the URL for the file at the given path. * * @param string $path * @return string * * @throws \RuntimeException */ public function url($path) { if (isset($this->config['prefix'])) { $path = $this->concatPathToUrl($this->config['prefix'], $path); } $adapter = $this->adapter; if (method_exists($adapter, 'getUrl')) { return $adapter->getUrl($path); } elseif (method_exists($this->driver, 'getUrl')) { return $this->driver->getUrl($path); } elseif ($adapter instanceof FtpAdapter || $adapter instanceof SftpAdapter) { return $this->getFtpUrl($path); } elseif ($adapter instanceof LocalAdapter) { return $this->getLocalUrl($path); } else { throw new RuntimeException('This driver does not support retrieving URLs.'); } } /** * Get the URL for the file at the given path. * * @param string $path * @return string */ protected function getFtpUrl($path) { return isset($this->config['url']) ? $this->concatPathToUrl($this->config['url'], $path) : $path; } /** * Get the URL for the file at the given path. * * @param string $path * @return string */ protected function getLocalUrl($path) { // If an explicit base URL has been set on the disk configuration then we will use // it as the base URL instead of the default path. This allows the developer to // have full control over the base path for this filesystem's generated URLs. if (isset($this->config['url'])) { return $this->concatPathToUrl($this->config['url'], $path); } $path = '/storage/'.$path; // If the path contains "storage/public", it probably means the developer is using // the default disk to generate the path instead of the "public" disk like they // are really supposed to use. We will remove the public from this path here. if (str_contains($path, '/storage/public/')) { return Str::replaceFirst('/public/', '/', $path); } return $path; } /** * Determine if temporary URLs can be generated. * * @return bool */ public function providesTemporaryUrls() { return method_exists($this->adapter, 'getTemporaryUrl') || isset($this->temporaryUrlCallback); } /** * Get a temporary URL for the file at the given path. * * @param string $path * @param \DateTimeInterface $expiration * @param array $options * @return string * * @throws \RuntimeException */ public function temporaryUrl($path, $expiration, array $options = []) { if (method_exists($this->adapter, 'getTemporaryUrl')) { return $this->adapter->getTemporaryUrl($path, $expiration, $options); } if ($this->temporaryUrlCallback) { return $this->temporaryUrlCallback->bindTo($this, static::class)( $path, $expiration, $options ); } throw new RuntimeException('This driver does not support creating temporary URLs.'); } /** * Get a temporary upload URL for the file at the given path. * * @param string $path * @param \DateTimeInterface $expiration * @param array $options * @return array * * @throws \RuntimeException */ public function temporaryUploadUrl($path, $expiration, array $options = []) { if (method_exists($this->adapter, 'temporaryUploadUrl')) { return $this->adapter->temporaryUploadUrl($path, $expiration, $options); } throw new RuntimeException('This driver does not support creating temporary upload URLs.'); } /** * Concatenate a path to a URL. * * @param string $url * @param string $path * @return string */ protected function concatPathToUrl($url, $path) { return rtrim($url, '/').'/'.ltrim($path, '/'); } /** * Replace the scheme, host and port of the given UriInterface with values from the given URL. * * @param \Psr\Http\Message\UriInterface $uri * @param string $url * @return \Psr\Http\Message\UriInterface */ protected function replaceBaseUrl($uri, $url) { $parsed = parse_url($url); return $uri ->withScheme($parsed['scheme']) ->withHost($parsed['host']) ->withPort($parsed['port'] ?? null); } /** * Get an array of all files in a directory. * * @param string|null $directory * @param bool $recursive * @return array */ public function files($directory = null, $recursive = false) { return $this->driver->listContents($directory ?? '', $recursive) ->filter(function (StorageAttributes $attributes) { return $attributes->isFile(); }) ->sortByPath() ->map(function (StorageAttributes $attributes) { return $attributes->path(); }) ->toArray(); } /** * Get all of the files from the given directory (recursive). * * @param string|null $directory * @return array */ public function allFiles($directory = null) { return $this->files($directory, true); } /** * Get all of the directories within a given directory. * * @param string|null $directory * @param bool $recursive * @return array */ public function directories($directory = null, $recursive = false) { return $this->driver->listContents($directory ?? '', $recursive) ->filter(function (StorageAttributes $attributes) { return $attributes->isDir(); }) ->map(function (StorageAttributes $attributes) { return $attributes->path(); }) ->toArray(); } /** * Get all the directories within a given directory (recursive). * * @param string|null $directory * @return array */ public function allDirectories($directory = null) { return $this->directories($directory, true); } /** * Create a directory. * * @param string $path * @return bool */ public function makeDirectory($path) { try { $this->driver->createDirectory($path); } catch (UnableToCreateDirectory|UnableToSetVisibility $e) { throw_if($this->throwsExceptions(), $e); return false; } return true; } /** * Recursively delete a directory. * * @param string $directory * @return bool */ public function deleteDirectory($directory) { try { $this->driver->deleteDirectory($directory); } catch (UnableToDeleteDirectory $e) { throw_if($this->throwsExceptions(), $e); return false; } return true; } /** * Get the Flysystem driver. * * @return \League\Flysystem\FilesystemOperator */ public function getDriver() { return $this->driver; } /** * Get the Flysystem adapter. * * @return \League\Flysystem\FilesystemAdapter */ public function getAdapter() { return $this->adapter; } /** * Get the configuration values. * * @return array */ public function getConfig() { return $this->config; } /** * Parse the given visibility value. * * @param string|null $visibility * @return string|null * * @throws \InvalidArgumentException */ protected function parseVisibility($visibility) { if (is_null($visibility)) { return; } return match ($visibility) { FilesystemContract::VISIBILITY_PUBLIC => Visibility::PUBLIC, FilesystemContract::VISIBILITY_PRIVATE => Visibility::PRIVATE, default => throw new InvalidArgumentException("Unknown visibility: {$visibility}."), }; } /** * Define a custom temporary URL builder callback. * * @param \Closure $callback * @return void */ public function buildTemporaryUrlsUsing(Closure $callback) { $this->temporaryUrlCallback = $callback; } /** * Determine if Flysystem exceptions should be thrown. * * @return bool */ protected function throwsExceptions(): bool { return (bool) ($this->config['throw'] ?? false); } /** * Pass dynamic methods call onto Flysystem. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } return $this->driver->{$method}(...$parameters); } } src/Illuminate/Filesystem/composer.json 0000644 00000003507 15107327037 0014316 0 ustar 00 { "name": "illuminate/filesystem", "description": "The Illuminate Filesystem package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/collections": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0", "illuminate/support": "^10.0", "symfony/finder": "^6.2" }, "autoload": { "psr-4": { "Illuminate\\Filesystem\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "ext-fileinfo": "Required to use the Filesystem class.", "ext-ftp": "Required to use the Flysystem FTP driver.", "ext-hash": "Required to use the Filesystem class.", "illuminate/http": "Required for handling uploaded files (^7.0).", "league/flysystem": "Required to use the Flysystem local driver (^3.0.16).", "league/flysystem-aws-s3-v3": "Required to use the Flysystem S3 driver (^3.0).", "league/flysystem-ftp": "Required to use the Flysystem FTP driver (^3.0).", "league/flysystem-sftp-v3": "Required to use the Flysystem SFTP driver (^3.0).", "psr/http-message": "Required to allow Storage::put to accept a StreamInterface (^1.0).", "symfony/filesystem": "Required to enable support for relative symbolic links (^6.2).", "symfony/mime": "Required to enable support for guessing extensions (^6.2)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Filesystem/FilesystemServiceProvider.php 0000644 00000003322 15107327037 0017460 0 ustar 00 <?php namespace Illuminate\Filesystem; use Illuminate\Support\ServiceProvider; class FilesystemServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { $this->registerNativeFilesystem(); $this->registerFlysystem(); } /** * Register the native filesystem implementation. * * @return void */ protected function registerNativeFilesystem() { $this->app->singleton('files', function () { return new Filesystem; }); } /** * Register the driver based filesystem. * * @return void */ protected function registerFlysystem() { $this->registerManager(); $this->app->singleton('filesystem.disk', function ($app) { return $app['filesystem']->disk($this->getDefaultDriver()); }); $this->app->singleton('filesystem.cloud', function ($app) { return $app['filesystem']->disk($this->getCloudDriver()); }); } /** * Register the filesystem manager. * * @return void */ protected function registerManager() { $this->app->singleton('filesystem', function ($app) { return new FilesystemManager($app); }); } /** * Get the default file driver. * * @return string */ protected function getDefaultDriver() { return $this->app['config']['filesystems.default']; } /** * Get the default cloud based file driver. * * @return string */ protected function getCloudDriver() { return $this->app['config']['filesystems.cloud']; } } src/Illuminate/Filesystem/Filesystem.php 0000644 00000046615 15107327037 0014440 0 ustar 00 <?php namespace Illuminate\Filesystem; use ErrorException; use FilesystemIterator; use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Support\LazyCollection; use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\Macroable; use RuntimeException; use SplFileObject; use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem; use Symfony\Component\Finder\Finder; use Symfony\Component\Mime\MimeTypes; class Filesystem { use Conditionable; use Macroable; /** * Determine if a file or directory exists. * * @param string $path * @return bool */ public function exists($path) { return file_exists($path); } /** * Determine if a file or directory is missing. * * @param string $path * @return bool */ public function missing($path) { return ! $this->exists($path); } /** * Get the contents of a file. * * @param string $path * @param bool $lock * @return string * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function get($path, $lock = false) { if ($this->isFile($path)) { return $lock ? $this->sharedGet($path) : file_get_contents($path); } throw new FileNotFoundException("File does not exist at path {$path}."); } /** * Get the contents of a file as decoded JSON. * * @param string $path * @param int $flags * @param bool $lock * @return array * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function json($path, $flags = 0, $lock = false) { return json_decode($this->get($path, $lock), true, 512, $flags); } /** * Get contents of a file with shared access. * * @param string $path * @return string */ public function sharedGet($path) { $contents = ''; $handle = fopen($path, 'rb'); if ($handle) { try { if (flock($handle, LOCK_SH)) { clearstatcache(true, $path); $contents = fread($handle, $this->size($path) ?: 1); flock($handle, LOCK_UN); } } finally { fclose($handle); } } return $contents; } /** * Get the returned value of a file. * * @param string $path * @param array $data * @return mixed * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function getRequire($path, array $data = []) { if ($this->isFile($path)) { $__path = $path; $__data = $data; return (static function () use ($__path, $__data) { extract($__data, EXTR_SKIP); return require $__path; })(); } throw new FileNotFoundException("File does not exist at path {$path}."); } /** * Require the given file once. * * @param string $path * @param array $data * @return mixed * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function requireOnce($path, array $data = []) { if ($this->isFile($path)) { $__path = $path; $__data = $data; return (static function () use ($__path, $__data) { extract($__data, EXTR_SKIP); return require_once $__path; })(); } throw new FileNotFoundException("File does not exist at path {$path}."); } /** * Get the contents of a file one line at a time. * * @param string $path * @return \Illuminate\Support\LazyCollection * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function lines($path) { if (! $this->isFile($path)) { throw new FileNotFoundException( "File does not exist at path {$path}." ); } return LazyCollection::make(function () use ($path) { $file = new SplFileObject($path); $file->setFlags(SplFileObject::DROP_NEW_LINE); while (! $file->eof()) { yield $file->fgets(); } }); } /** * Get the hash of the file at the given path. * * @param string $path * @param string $algorithm * @return string */ public function hash($path, $algorithm = 'md5') { return hash_file($algorithm, $path); } /** * Write the contents of a file. * * @param string $path * @param string $contents * @param bool $lock * @return int|bool */ public function put($path, $contents, $lock = false) { return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); } /** * Write the contents of a file, replacing it atomically if it already exists. * * @param string $path * @param string $content * @param int|null $mode * @return void */ public function replace($path, $content, $mode = null) { // If the path already exists and is a symlink, get the real path... clearstatcache(true, $path); $path = realpath($path) ?: $path; $tempPath = tempnam(dirname($path), basename($path)); // Fix permissions of tempPath because `tempnam()` creates it with permissions set to 0600... if (! is_null($mode)) { chmod($tempPath, $mode); } else { chmod($tempPath, 0777 - umask()); } file_put_contents($tempPath, $content); rename($tempPath, $path); } /** * Replace a given string within a given file. * * @param array|string $search * @param array|string $replace * @param string $path * @return void */ public function replaceInFile($search, $replace, $path) { file_put_contents($path, str_replace($search, $replace, file_get_contents($path))); } /** * Prepend to a file. * * @param string $path * @param string $data * @return int */ public function prepend($path, $data) { if ($this->exists($path)) { return $this->put($path, $data.$this->get($path)); } return $this->put($path, $data); } /** * Append to a file. * * @param string $path * @param string $data * @return int */ public function append($path, $data) { return file_put_contents($path, $data, FILE_APPEND); } /** * Get or set UNIX mode of a file or directory. * * @param string $path * @param int|null $mode * @return mixed */ public function chmod($path, $mode = null) { if ($mode) { return chmod($path, $mode); } return substr(sprintf('%o', fileperms($path)), -4); } /** * Delete the file at a given path. * * @param string|array $paths * @return bool */ public function delete($paths) { $paths = is_array($paths) ? $paths : func_get_args(); $success = true; foreach ($paths as $path) { try { if (@unlink($path)) { clearstatcache(false, $path); } else { $success = false; } } catch (ErrorException) { $success = false; } } return $success; } /** * Move a file to a new location. * * @param string $path * @param string $target * @return bool */ public function move($path, $target) { return rename($path, $target); } /** * Copy a file to a new location. * * @param string $path * @param string $target * @return bool */ public function copy($path, $target) { return copy($path, $target); } /** * Create a symlink to the target file or directory. On Windows, a hard link is created if the target is a file. * * @param string $target * @param string $link * @return void */ public function link($target, $link) { if (! windows_os()) { return symlink($target, $link); } $mode = $this->isDirectory($target) ? 'J' : 'H'; exec("mklink /{$mode} ".escapeshellarg($link).' '.escapeshellarg($target)); } /** * Create a relative symlink to the target file or directory. * * @param string $target * @param string $link * @return void * * @throws \RuntimeException */ public function relativeLink($target, $link) { if (! class_exists(SymfonyFilesystem::class)) { throw new RuntimeException( 'To enable support for relative links, please install the symfony/filesystem package.' ); } $relativeTarget = (new SymfonyFilesystem)->makePathRelative($target, dirname($link)); $this->link($this->isFile($target) ? rtrim($relativeTarget, '/') : $relativeTarget, $link); } /** * Extract the file name from a file path. * * @param string $path * @return string */ public function name($path) { return pathinfo($path, PATHINFO_FILENAME); } /** * Extract the trailing name component from a file path. * * @param string $path * @return string */ public function basename($path) { return pathinfo($path, PATHINFO_BASENAME); } /** * Extract the parent directory from a file path. * * @param string $path * @return string */ public function dirname($path) { return pathinfo($path, PATHINFO_DIRNAME); } /** * Extract the file extension from a file path. * * @param string $path * @return string */ public function extension($path) { return pathinfo($path, PATHINFO_EXTENSION); } /** * Guess the file extension from the mime-type of a given file. * * @param string $path * @return string|null * * @throws \RuntimeException */ public function guessExtension($path) { if (! class_exists(MimeTypes::class)) { throw new RuntimeException( 'To enable support for guessing extensions, please install the symfony/mime package.' ); } return (new MimeTypes)->getExtensions($this->mimeType($path))[0] ?? null; } /** * Get the file type of a given file. * * @param string $path * @return string */ public function type($path) { return filetype($path); } /** * Get the mime-type of a given file. * * @param string $path * @return string|false */ public function mimeType($path) { return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path); } /** * Get the file size of a given file. * * @param string $path * @return int */ public function size($path) { return filesize($path); } /** * Get the file's last modification time. * * @param string $path * @return int */ public function lastModified($path) { return filemtime($path); } /** * Determine if the given path is a directory. * * @param string $directory * @return bool */ public function isDirectory($directory) { return is_dir($directory); } /** * Determine if the given path is a directory that does not contain any other files or directories. * * @param string $directory * @param bool $ignoreDotFiles * @return bool */ public function isEmptyDirectory($directory, $ignoreDotFiles = false) { return ! Finder::create()->ignoreDotFiles($ignoreDotFiles)->in($directory)->depth(0)->hasResults(); } /** * Determine if the given path is readable. * * @param string $path * @return bool */ public function isReadable($path) { return is_readable($path); } /** * Determine if the given path is writable. * * @param string $path * @return bool */ public function isWritable($path) { return is_writable($path); } /** * Determine if two files are the same by comparing their hashes. * * @param string $firstFile * @param string $secondFile * @return bool */ public function hasSameHash($firstFile, $secondFile) { $hash = @md5_file($firstFile); return $hash && $hash === @md5_file($secondFile); } /** * Determine if the given path is a file. * * @param string $file * @return bool */ public function isFile($file) { return is_file($file); } /** * Find path names matching a given pattern. * * @param string $pattern * @param int $flags * @return array */ public function glob($pattern, $flags = 0) { return glob($pattern, $flags); } /** * Get an array of all files in a directory. * * @param string $directory * @param bool $hidden * @return \Symfony\Component\Finder\SplFileInfo[] */ public function files($directory, $hidden = false) { return iterator_to_array( Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory)->depth(0)->sortByName(), false ); } /** * Get all of the files from the given directory (recursive). * * @param string $directory * @param bool $hidden * @return \Symfony\Component\Finder\SplFileInfo[] */ public function allFiles($directory, $hidden = false) { return iterator_to_array( Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory)->sortByName(), false ); } /** * Get all of the directories within a given directory. * * @param string $directory * @return array */ public function directories($directory) { $directories = []; foreach (Finder::create()->in($directory)->directories()->depth(0)->sortByName() as $dir) { $directories[] = $dir->getPathname(); } return $directories; } /** * Ensure a directory exists. * * @param string $path * @param int $mode * @param bool $recursive * @return void */ public function ensureDirectoryExists($path, $mode = 0755, $recursive = true) { if (! $this->isDirectory($path)) { $this->makeDirectory($path, $mode, $recursive); } } /** * Create a directory. * * @param string $path * @param int $mode * @param bool $recursive * @param bool $force * @return bool */ public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false) { if ($force) { return @mkdir($path, $mode, $recursive); } return mkdir($path, $mode, $recursive); } /** * Move a directory. * * @param string $from * @param string $to * @param bool $overwrite * @return bool */ public function moveDirectory($from, $to, $overwrite = false) { if ($overwrite && $this->isDirectory($to) && ! $this->deleteDirectory($to)) { return false; } return @rename($from, $to) === true; } /** * Copy a directory from one location to another. * * @param string $directory * @param string $destination * @param int|null $options * @return bool */ public function copyDirectory($directory, $destination, $options = null) { if (! $this->isDirectory($directory)) { return false; } $options = $options ?: FilesystemIterator::SKIP_DOTS; // If the destination directory does not actually exist, we will go ahead and // create it recursively, which just gets the destination prepared to copy // the files over. Once we make the directory we'll proceed the copying. $this->ensureDirectoryExists($destination, 0777); $items = new FilesystemIterator($directory, $options); foreach ($items as $item) { // As we spin through items, we will check to see if the current file is actually // a directory or a file. When it is actually a directory we will need to call // back into this function recursively to keep copying these nested folders. $target = $destination.'/'.$item->getBasename(); if ($item->isDir()) { $path = $item->getPathname(); if (! $this->copyDirectory($path, $target, $options)) { return false; } } // If the current items is just a regular file, we will just copy this to the new // location and keep looping. If for some reason the copy fails we'll bail out // and return false, so the developer is aware that the copy process failed. elseif (! $this->copy($item->getPathname(), $target)) { return false; } } return true; } /** * Recursively delete a directory. * * The directory itself may be optionally preserved. * * @param string $directory * @param bool $preserve * @return bool */ public function deleteDirectory($directory, $preserve = false) { if (! $this->isDirectory($directory)) { return false; } $items = new FilesystemIterator($directory); foreach ($items as $item) { // If the item is a directory, we can just recurse into the function and // delete that sub-directory otherwise we'll just delete the file and // keep iterating through each file until the directory is cleaned. if ($item->isDir() && ! $item->isLink()) { $this->deleteDirectory($item->getPathname()); } // If the item is just a file, we can go ahead and delete it since we're // just looping through and waxing all of the files in this directory // and calling directories recursively, so we delete the real path. else { $this->delete($item->getPathname()); } } unset($items); if (! $preserve) { @rmdir($directory); } return true; } /** * Remove all of the directories within a given directory. * * @param string $directory * @return bool */ public function deleteDirectories($directory) { $allDirectories = $this->directories($directory); if (! empty($allDirectories)) { foreach ($allDirectories as $directoryName) { $this->deleteDirectory($directoryName); } return true; } return false; } /** * Empty the specified directory of all files and folders. * * @param string $directory * @return bool */ public function cleanDirectory($directory) { return $this->deleteDirectory($directory, true); } } src/Illuminate/Filesystem/AwsS3V3Adapter.php 0000644 00000010236 15107327037 0014754 0 ustar 00 <?php namespace Illuminate\Filesystem; use Aws\S3\S3Client; use Illuminate\Support\Traits\Conditionable; use League\Flysystem\AwsS3V3\AwsS3V3Adapter as S3Adapter; use League\Flysystem\FilesystemOperator; class AwsS3V3Adapter extends FilesystemAdapter { use Conditionable; /** * The AWS S3 client. * * @var \Aws\S3\S3Client */ protected $client; /** * Create a new AwsS3V3FilesystemAdapter instance. * * @param \League\Flysystem\FilesystemOperator $driver * @param \League\Flysystem\AwsS3V3\AwsS3V3Adapter $adapter * @param array $config * @param \Aws\S3\S3Client $client * @return void */ public function __construct(FilesystemOperator $driver, S3Adapter $adapter, array $config, S3Client $client) { parent::__construct($driver, $adapter, $config); $this->client = $client; } /** * Get the URL for the file at the given path. * * @param string $path * @return string * * @throws \RuntimeException */ public function url($path) { // If an explicit base URL has been set on the disk configuration then we will use // it as the base URL instead of the default path. This allows the developer to // have full control over the base path for this filesystem's generated URLs. if (isset($this->config['url'])) { return $this->concatPathToUrl($this->config['url'], $this->prefixer->prefixPath($path)); } return $this->client->getObjectUrl( $this->config['bucket'], $this->prefixer->prefixPath($path) ); } /** * Determine if temporary URLs can be generated. * * @return bool */ public function providesTemporaryUrls() { return true; } /** * Get a temporary URL for the file at the given path. * * @param string $path * @param \DateTimeInterface $expiration * @param array $options * @return string */ public function temporaryUrl($path, $expiration, array $options = []) { $command = $this->client->getCommand('GetObject', array_merge([ 'Bucket' => $this->config['bucket'], 'Key' => $this->prefixer->prefixPath($path), ], $options)); $uri = $this->client->createPresignedRequest( $command, $expiration, $options )->getUri(); // If an explicit base URL has been set on the disk configuration then we will use // it as the base URL instead of the default path. This allows the developer to // have full control over the base path for this filesystem's generated URLs. if (isset($this->config['temporary_url'])) { $uri = $this->replaceBaseUrl($uri, $this->config['temporary_url']); } return (string) $uri; } /** * Get a temporary upload URL for the file at the given path. * * @param string $path * @param \DateTimeInterface $expiration * @param array $options * @return array */ public function temporaryUploadUrl($path, $expiration, array $options = []) { $command = $this->client->getCommand('PutObject', array_merge([ 'Bucket' => $this->config['bucket'], 'Key' => $this->prefixer->prefixPath($path), ], $options)); $signedRequest = $this->client->createPresignedRequest( $command, $expiration, $options ); $uri = $signedRequest->getUri(); // If an explicit base URL has been set on the disk configuration then we will use // it as the base URL instead of the default path. This allows the developer to // have full control over the base path for this filesystem's generated URLs. if (isset($this->config['temporary_url'])) { $uri = $this->replaceBaseUrl($uri, $this->config['temporary_url']); } return [ 'url' => (string) $uri, 'headers' => $signedRequest->getHeaders(), ]; } /** * Get the underlying S3 client. * * @return \Aws\S3\S3Client */ public function getClient() { return $this->client; } } src/Illuminate/Filesystem/LICENSE.md 0000644 00000002063 15107327037 0013174 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Events/EventServiceProvider.php 0000644 00000001371 15107327037 0015537 0 ustar 00 <?php namespace Illuminate\Events; use Illuminate\Contracts\Queue\Factory as QueueFactoryContract; use Illuminate\Support\ServiceProvider; class EventServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton('events', function ($app) { return (new Dispatcher($app))->setQueueResolver(function () use ($app) { return $app->make(QueueFactoryContract::class); })->setTransactionManagerResolver(function () use ($app) { return $app->bound('db.transactions') ? $app->make('db.transactions') : null; }); }); } } src/Illuminate/Events/CallQueuedListener.php 0000644 00000007232 15107327037 0015156 0 ustar 00 <?php namespace Illuminate\Events; use Illuminate\Bus\Queueable; use Illuminate\Container\Container; use Illuminate\Contracts\Queue\Job; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; class CallQueuedListener implements ShouldQueue { use InteractsWithQueue, Queueable; /** * The listener class name. * * @var string */ public $class; /** * The listener method. * * @var string */ public $method; /** * The data to be passed to the listener. * * @var array */ public $data; /** * The number of times the job may be attempted. * * @var int */ public $tries; /** * The maximum number of exceptions allowed, regardless of attempts. * * @var int */ public $maxExceptions; /** * The number of seconds to wait before retrying a job that encountered an uncaught exception. * * @var int */ public $backoff; /** * The timestamp indicating when the job should timeout. * * @var int */ public $retryUntil; /** * The number of seconds the job can run before timing out. * * @var int */ public $timeout; /** * Indicates if the job should be encrypted. * * @var bool */ public $shouldBeEncrypted = false; /** * Create a new job instance. * * @param string $class * @param string $method * @param array $data * @return void */ public function __construct($class, $method, $data) { $this->data = $data; $this->class = $class; $this->method = $method; } /** * Handle the queued job. * * @param \Illuminate\Container\Container $container * @return void */ public function handle(Container $container) { $this->prepareData(); $handler = $this->setJobInstanceIfNecessary( $this->job, $container->make($this->class) ); $handler->{$this->method}(...array_values($this->data)); } /** * Set the job instance of the given class if necessary. * * @param \Illuminate\Contracts\Queue\Job $job * @param object $instance * @return object */ protected function setJobInstanceIfNecessary(Job $job, $instance) { if (in_array(InteractsWithQueue::class, class_uses_recursive($instance))) { $instance->setJob($job); } return $instance; } /** * Call the failed method on the job instance. * * The event instance and the exception will be passed. * * @param \Throwable $e * @return void */ public function failed($e) { $this->prepareData(); $handler = Container::getInstance()->make($this->class); $parameters = array_merge(array_values($this->data), [$e]); if (method_exists($handler, 'failed')) { $handler->failed(...$parameters); } } /** * Unserialize the data if needed. * * @return void */ protected function prepareData() { if (is_string($this->data)) { $this->data = unserialize($this->data); } } /** * Get the display name for the queued job. * * @return string */ public function displayName() { return $this->class; } /** * Prepare the instance for cloning. * * @return void */ public function __clone() { $this->data = array_map(function ($data) { return is_object($data) ? clone $data : $data; }, $this->data); } } src/Illuminate/Events/Dispatcher.php 0000644 00000053254 15107327037 0013517 0 ustar 00 <?php namespace Illuminate\Events; use Closure; use Exception; use Illuminate\Container\Container; use Illuminate\Contracts\Broadcasting\Factory as BroadcastFactory; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Contracts\Container\Container as ContainerContract; use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; use Illuminate\Contracts\Events\ShouldDispatchAfterCommit; use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit; use Illuminate\Contracts\Queue\ShouldBeEncrypted; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueueAfterCommit; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use Illuminate\Support\Traits\ReflectsClosures; use ReflectionClass; class Dispatcher implements DispatcherContract { use Macroable, ReflectsClosures; /** * The IoC container instance. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * The registered event listeners. * * @var array */ protected $listeners = []; /** * The wildcard listeners. * * @var array */ protected $wildcards = []; /** * The cached wildcard listeners. * * @var array */ protected $wildcardsCache = []; /** * The queue resolver instance. * * @var callable */ protected $queueResolver; /** * The database transaction manager resolver instance. * * @var callable */ protected $transactionManagerResolver; /** * Create a new event dispatcher instance. * * @param \Illuminate\Contracts\Container\Container|null $container * @return void */ public function __construct(ContainerContract $container = null) { $this->container = $container ?: new Container; } /** * Register an event listener with the dispatcher. * * @param \Closure|string|array $events * @param \Closure|string|array|null $listener * @return void */ public function listen($events, $listener = null) { if ($events instanceof Closure) { return collect($this->firstClosureParameterTypes($events)) ->each(function ($event) use ($events) { $this->listen($event, $events); }); } elseif ($events instanceof QueuedClosure) { return collect($this->firstClosureParameterTypes($events->closure)) ->each(function ($event) use ($events) { $this->listen($event, $events->resolve()); }); } elseif ($listener instanceof QueuedClosure) { $listener = $listener->resolve(); } foreach ((array) $events as $event) { if (str_contains($event, '*')) { $this->setupWildcardListen($event, $listener); } else { $this->listeners[$event][] = $listener; } } } /** * Setup a wildcard listener callback. * * @param string $event * @param \Closure|string $listener * @return void */ protected function setupWildcardListen($event, $listener) { $this->wildcards[$event][] = $listener; $this->wildcardsCache = []; } /** * Determine if a given event has listeners. * * @param string $eventName * @return bool */ public function hasListeners($eventName) { return isset($this->listeners[$eventName]) || isset($this->wildcards[$eventName]) || $this->hasWildcardListeners($eventName); } /** * Determine if the given event has any wildcard listeners. * * @param string $eventName * @return bool */ public function hasWildcardListeners($eventName) { foreach ($this->wildcards as $key => $listeners) { if (Str::is($key, $eventName)) { return true; } } return false; } /** * Register an event and payload to be fired later. * * @param string $event * @param object|array $payload * @return void */ public function push($event, $payload = []) { $this->listen($event.'_pushed', function () use ($event, $payload) { $this->dispatch($event, $payload); }); } /** * Flush a set of pushed events. * * @param string $event * @return void */ public function flush($event) { $this->dispatch($event.'_pushed'); } /** * Register an event subscriber with the dispatcher. * * @param object|string $subscriber * @return void */ public function subscribe($subscriber) { $subscriber = $this->resolveSubscriber($subscriber); $events = $subscriber->subscribe($this); if (is_array($events)) { foreach ($events as $event => $listeners) { foreach (Arr::wrap($listeners) as $listener) { if (is_string($listener) && method_exists($subscriber, $listener)) { $this->listen($event, [get_class($subscriber), $listener]); continue; } $this->listen($event, $listener); } } } } /** * Resolve the subscriber instance. * * @param object|string $subscriber * @return mixed */ protected function resolveSubscriber($subscriber) { if (is_string($subscriber)) { return $this->container->make($subscriber); } return $subscriber; } /** * Fire an event until the first non-null response is returned. * * @param string|object $event * @param mixed $payload * @return mixed */ public function until($event, $payload = []) { return $this->dispatch($event, $payload, true); } /** * Fire an event and call the listeners. * * @param string|object $event * @param mixed $payload * @param bool $halt * @return array|null */ public function dispatch($event, $payload = [], $halt = false) { // When the given "event" is actually an object we will assume it is an event // object and use the class as the event name and this event itself as the // payload to the handler, which makes object based events quite simple. [$isEventObject, $event, $payload] = [ is_object($event), ...$this->parseEventAndPayload($event, $payload), ]; // If the event is not intended to be dispatched unless the current database // transaction is successful, we'll register a callback which will handle // dispatching this event on the next successful DB transaction commit. if ($isEventObject && $payload[0] instanceof ShouldDispatchAfterCommit && ! is_null($transactions = $this->resolveTransactionManager())) { $transactions->addCallback( fn () => $this->invokeListeners($event, $payload, $halt) ); return null; } return $this->invokeListeners($event, $payload, $halt); } /** * Broadcast an event and call its listeners. * * @param string|object $event * @param mixed $payload * @param bool $halt * @return array|null */ protected function invokeListeners($event, $payload, $halt = false) { if ($this->shouldBroadcast($payload)) { $this->broadcastEvent($payload[0]); } $responses = []; foreach ($this->getListeners($event) as $listener) { $response = $listener($event, $payload); // If a response is returned from the listener and event halting is enabled // we will just return this response, and not call the rest of the event // listeners. Otherwise we will add the response on the response list. if ($halt && ! is_null($response)) { return $response; } // If a boolean false is returned from a listener, we will stop propagating // the event to any further listeners down in the chain, else we keep on // looping through the listeners and firing every one in our sequence. if ($response === false) { break; } $responses[] = $response; } return $halt ? null : $responses; } /** * Parse the given event and payload and prepare them for dispatching. * * @param mixed $event * @param mixed $payload * @return array */ protected function parseEventAndPayload($event, $payload) { if (is_object($event)) { [$payload, $event] = [[$event], get_class($event)]; } return [$event, Arr::wrap($payload)]; } /** * Determine if the payload has a broadcastable event. * * @param array $payload * @return bool */ protected function shouldBroadcast(array $payload) { return isset($payload[0]) && $payload[0] instanceof ShouldBroadcast && $this->broadcastWhen($payload[0]); } /** * Check if the event should be broadcasted by the condition. * * @param mixed $event * @return bool */ protected function broadcastWhen($event) { return method_exists($event, 'broadcastWhen') ? $event->broadcastWhen() : true; } /** * Broadcast the given event class. * * @param \Illuminate\Contracts\Broadcasting\ShouldBroadcast $event * @return void */ protected function broadcastEvent($event) { $this->container->make(BroadcastFactory::class)->queue($event); } /** * Get all of the listeners for a given event name. * * @param string $eventName * @return array */ public function getListeners($eventName) { $listeners = array_merge( $this->prepareListeners($eventName), $this->wildcardsCache[$eventName] ?? $this->getWildcardListeners($eventName) ); return class_exists($eventName, false) ? $this->addInterfaceListeners($eventName, $listeners) : $listeners; } /** * Get the wildcard listeners for the event. * * @param string $eventName * @return array */ protected function getWildcardListeners($eventName) { $wildcards = []; foreach ($this->wildcards as $key => $listeners) { if (Str::is($key, $eventName)) { foreach ($listeners as $listener) { $wildcards[] = $this->makeListener($listener, true); } } } return $this->wildcardsCache[$eventName] = $wildcards; } /** * Add the listeners for the event's interfaces to the given array. * * @param string $eventName * @param array $listeners * @return array */ protected function addInterfaceListeners($eventName, array $listeners = []) { foreach (class_implements($eventName) as $interface) { if (isset($this->listeners[$interface])) { foreach ($this->prepareListeners($interface) as $names) { $listeners = array_merge($listeners, (array) $names); } } } return $listeners; } /** * Prepare the listeners for a given event. * * @param string $eventName * @return \Closure[] */ protected function prepareListeners(string $eventName) { $listeners = []; foreach ($this->listeners[$eventName] ?? [] as $listener) { $listeners[] = $this->makeListener($listener); } return $listeners; } /** * Register an event listener with the dispatcher. * * @param \Closure|string|array $listener * @param bool $wildcard * @return \Closure */ public function makeListener($listener, $wildcard = false) { if (is_string($listener)) { return $this->createClassListener($listener, $wildcard); } if (is_array($listener) && isset($listener[0]) && is_string($listener[0])) { return $this->createClassListener($listener, $wildcard); } return function ($event, $payload) use ($listener, $wildcard) { if ($wildcard) { return $listener($event, $payload); } return $listener(...array_values($payload)); }; } /** * Create a class based listener using the IoC container. * * @param string $listener * @param bool $wildcard * @return \Closure */ public function createClassListener($listener, $wildcard = false) { return function ($event, $payload) use ($listener, $wildcard) { if ($wildcard) { return call_user_func($this->createClassCallable($listener), $event, $payload); } $callable = $this->createClassCallable($listener); return $callable(...array_values($payload)); }; } /** * Create the class based event callable. * * @param array|string $listener * @return callable */ protected function createClassCallable($listener) { [$class, $method] = is_array($listener) ? $listener : $this->parseClassCallable($listener); if (! method_exists($class, $method)) { $method = '__invoke'; } if ($this->handlerShouldBeQueued($class)) { return $this->createQueuedHandlerCallable($class, $method); } $listener = $this->container->make($class); return $this->handlerShouldBeDispatchedAfterDatabaseTransactions($listener) ? $this->createCallbackForListenerRunningAfterCommits($listener, $method) : [$listener, $method]; } /** * Parse the class listener into class and method. * * @param string $listener * @return array */ protected function parseClassCallable($listener) { return Str::parseCallback($listener, 'handle'); } /** * Determine if the event handler class should be queued. * * @param string $class * @return bool */ protected function handlerShouldBeQueued($class) { try { return (new ReflectionClass($class))->implementsInterface( ShouldQueue::class ); } catch (Exception) { return false; } } /** * Create a callable for putting an event handler on the queue. * * @param string $class * @param string $method * @return \Closure */ protected function createQueuedHandlerCallable($class, $method) { return function () use ($class, $method) { $arguments = array_map(function ($a) { return is_object($a) ? clone $a : $a; }, func_get_args()); if ($this->handlerWantsToBeQueued($class, $arguments)) { $this->queueHandler($class, $method, $arguments); } }; } /** * Determine if the given event handler should be dispatched after all database transactions have committed. * * @param object|mixed $listener * @return bool */ protected function handlerShouldBeDispatchedAfterDatabaseTransactions($listener) { return (($listener->afterCommit ?? null) || $listener instanceof ShouldHandleEventsAfterCommit) && $this->resolveTransactionManager(); } /** * Create a callable for dispatching a listener after database transactions. * * @param mixed $listener * @param string $method * @return \Closure */ protected function createCallbackForListenerRunningAfterCommits($listener, $method) { return function () use ($method, $listener) { $payload = func_get_args(); $this->resolveTransactionManager()->addCallback( function () use ($listener, $method, $payload) { $listener->$method(...$payload); } ); }; } /** * Determine if the event handler wants to be queued. * * @param string $class * @param array $arguments * @return bool */ protected function handlerWantsToBeQueued($class, $arguments) { $instance = $this->container->make($class); if (method_exists($instance, 'shouldQueue')) { return $instance->shouldQueue($arguments[0]); } return true; } /** * Queue the handler class. * * @param string $class * @param string $method * @param array $arguments * @return void */ protected function queueHandler($class, $method, $arguments) { [$listener, $job] = $this->createListenerAndJob($class, $method, $arguments); $connection = $this->resolveQueue()->connection(method_exists($listener, 'viaConnection') ? (isset($arguments[0]) ? $listener->viaConnection($arguments[0]) : $listener->viaConnection()) : $listener->connection ?? null); $queue = method_exists($listener, 'viaQueue') ? (isset($arguments[0]) ? $listener->viaQueue($arguments[0]) : $listener->viaQueue()) : $listener->queue ?? null; $delay = method_exists($listener, 'withDelay') ? (isset($arguments[0]) ? $listener->withDelay($arguments[0]) : $listener->withDelay()) : $listener->delay ?? null; is_null($delay) ? $connection->pushOn($queue, $job) : $connection->laterOn($queue, $delay, $job); } /** * Create the listener and job for a queued listener. * * @param string $class * @param string $method * @param array $arguments * @return array */ protected function createListenerAndJob($class, $method, $arguments) { $listener = (new ReflectionClass($class))->newInstanceWithoutConstructor(); return [$listener, $this->propagateListenerOptions( $listener, new CallQueuedListener($class, $method, $arguments) )]; } /** * Propagate listener options to the job. * * @param mixed $listener * @param \Illuminate\Events\CallQueuedListener $job * @return mixed */ protected function propagateListenerOptions($listener, $job) { return tap($job, function ($job) use ($listener) { $data = array_values($job->data); if ($listener instanceof ShouldQueueAfterCommit) { $job->afterCommit = true; } else { $job->afterCommit = property_exists($listener, 'afterCommit') ? $listener->afterCommit : null; } $job->backoff = method_exists($listener, 'backoff') ? $listener->backoff(...$data) : ($listener->backoff ?? null); $job->maxExceptions = $listener->maxExceptions ?? null; $job->retryUntil = method_exists($listener, 'retryUntil') ? $listener->retryUntil(...$data) : null; $job->shouldBeEncrypted = $listener instanceof ShouldBeEncrypted; $job->timeout = $listener->timeout ?? null; $job->tries = $listener->tries ?? null; $job->through(array_merge( method_exists($listener, 'middleware') ? $listener->middleware(...$data) : [], $listener->middleware ?? [] )); }); } /** * Remove a set of listeners from the dispatcher. * * @param string $event * @return void */ public function forget($event) { if (str_contains($event, '*')) { unset($this->wildcards[$event]); } else { unset($this->listeners[$event]); } foreach ($this->wildcardsCache as $key => $listeners) { if (Str::is($event, $key)) { unset($this->wildcardsCache[$key]); } } } /** * Forget all of the pushed listeners. * * @return void */ public function forgetPushed() { foreach ($this->listeners as $key => $value) { if (str_ends_with($key, '_pushed')) { $this->forget($key); } } } /** * Get the queue implementation from the resolver. * * @return \Illuminate\Contracts\Queue\Queue */ protected function resolveQueue() { return call_user_func($this->queueResolver); } /** * Set the queue resolver implementation. * * @param callable $resolver * @return $this */ public function setQueueResolver(callable $resolver) { $this->queueResolver = $resolver; return $this; } /** * Get the database transaction manager implementation from the resolver. * * @return \Illuminate\Database\DatabaseTransactionsManager|null */ protected function resolveTransactionManager() { return call_user_func($this->transactionManagerResolver); } /** * Set the database transaction manager resolver implementation. * * @param callable $resolver * @return $this */ public function setTransactionManagerResolver(callable $resolver) { $this->transactionManagerResolver = $resolver; return $this; } /** * Gets the raw, unprepared listeners. * * @return array */ public function getRawListeners() { return $this->listeners; } } src/Illuminate/Events/composer.json 0000644 00000002014 15107327037 0013426 0 ustar 00 { "name": "illuminate/events", "description": "The Illuminate Events package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/bus": "^10.0", "illuminate/collections": "^10.0", "illuminate/container": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0", "illuminate/support": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Events\\": "" }, "files": [ "functions.php" ] }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Events/InvokeQueuedClosure.php 0000644 00000001477 15107327037 0015372 0 ustar 00 <?php namespace Illuminate\Events; class InvokeQueuedClosure { /** * Handle the event. * * @param \Laravel\SerializableClosure\SerializableClosure $closure * @param array $arguments * @return void */ public function handle($closure, array $arguments) { call_user_func($closure->getClosure(), ...$arguments); } /** * Handle a job failure. * * @param \Laravel\SerializableClosure\SerializableClosure $closure * @param array $arguments * @param array $catchCallbacks * @param \Throwable $exception * @return void */ public function failed($closure, array $arguments, array $catchCallbacks, $exception) { $arguments[] = $exception; collect($catchCallbacks)->each->__invoke(...$arguments); } } src/Illuminate/Events/NullDispatcher.php 0000644 00000006055 15107327037 0014347 0 ustar 00 <?php namespace Illuminate\Events; use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; use Illuminate\Support\Traits\ForwardsCalls; class NullDispatcher implements DispatcherContract { use ForwardsCalls; /** * The underlying event dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $dispatcher; /** * Create a new event dispatcher instance that does not fire. * * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher * @return void */ public function __construct(DispatcherContract $dispatcher) { $this->dispatcher = $dispatcher; } /** * Don't fire an event. * * @param string|object $event * @param mixed $payload * @param bool $halt * @return void */ public function dispatch($event, $payload = [], $halt = false) { // } /** * Don't register an event and payload to be fired later. * * @param string $event * @param array $payload * @return void */ public function push($event, $payload = []) { // } /** * Don't dispatch an event. * * @param string|object $event * @param mixed $payload * @return mixed */ public function until($event, $payload = []) { // } /** * Register an event listener with the dispatcher. * * @param \Closure|string|array $events * @param \Closure|string|array|null $listener * @return void */ public function listen($events, $listener = null) { $this->dispatcher->listen($events, $listener); } /** * Determine if a given event has listeners. * * @param string $eventName * @return bool */ public function hasListeners($eventName) { return $this->dispatcher->hasListeners($eventName); } /** * Register an event subscriber with the dispatcher. * * @param object|string $subscriber * @return void */ public function subscribe($subscriber) { $this->dispatcher->subscribe($subscriber); } /** * Flush a set of pushed events. * * @param string $event * @return void */ public function flush($event) { $this->dispatcher->flush($event); } /** * Remove a set of listeners from the dispatcher. * * @param string $event * @return void */ public function forget($event) { $this->dispatcher->forget($event); } /** * Forget all of the queued listeners. * * @return void */ public function forgetPushed() { $this->dispatcher->forgetPushed(); } /** * Dynamically pass method calls to the underlying dispatcher. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->forwardDecoratedCallTo($this->dispatcher, $method, $parameters); } } src/Illuminate/Events/LICENSE.md 0000644 00000002063 15107327037 0012314 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Events/functions.php 0000644 00000000552 15107327037 0013432 0 ustar 00 <?php namespace Illuminate\Events; use Closure; if (! function_exists('Illuminate\Events\queueable')) { /** * Create a new queued Closure event listener. * * @param \Closure $closure * @return \Illuminate\Events\QueuedClosure */ function queueable(Closure $closure) { return new QueuedClosure($closure); } } src/Illuminate/Events/QueuedClosure.php 0000644 00000005220 15107327037 0014204 0 ustar 00 <?php namespace Illuminate\Events; use Closure; use Laravel\SerializableClosure\SerializableClosure; class QueuedClosure { /** * The underlying Closure. * * @var \Closure */ public $closure; /** * The name of the connection the job should be sent to. * * @var string|null */ public $connection; /** * The name of the queue the job should be sent to. * * @var string|null */ public $queue; /** * The number of seconds before the job should be made available. * * @var \DateTimeInterface|\DateInterval|int|null */ public $delay; /** * All of the "catch" callbacks for the queued closure. * * @var array */ public $catchCallbacks = []; /** * Create a new queued closure event listener resolver. * * @param \Closure $closure * @return void */ public function __construct(Closure $closure) { $this->closure = $closure; } /** * Set the desired connection for the job. * * @param string|null $connection * @return $this */ public function onConnection($connection) { $this->connection = $connection; return $this; } /** * Set the desired queue for the job. * * @param string|null $queue * @return $this */ public function onQueue($queue) { $this->queue = $queue; return $this; } /** * Set the desired delay in seconds for the job. * * @param \DateTimeInterface|\DateInterval|int|null $delay * @return $this */ public function delay($delay) { $this->delay = $delay; return $this; } /** * Specify a callback that should be invoked if the queued listener job fails. * * @param \Closure $closure * @return $this */ public function catch(Closure $closure) { $this->catchCallbacks[] = $closure; return $this; } /** * Resolve the actual event listener callback. * * @return \Closure */ public function resolve() { return function (...$arguments) { dispatch(new CallQueuedListener(InvokeQueuedClosure::class, 'handle', [ 'closure' => new SerializableClosure($this->closure), 'arguments' => $arguments, 'catch' => collect($this->catchCallbacks)->map(function ($callback) { return new SerializableClosure($callback); })->all(), ]))->onConnection($this->connection)->onQueue($this->queue)->delay($this->delay); }; } } src/Illuminate/Mail/Transport/LogTransport.php 0000644 00000002701 15107327037 0015470 0 ustar 00 <?php namespace Illuminate\Mail\Transport; use Psr\Log\LoggerInterface; use Symfony\Component\Mailer\Envelope; use Symfony\Component\Mailer\SentMessage; use Symfony\Component\Mailer\Transport\TransportInterface; use Symfony\Component\Mime\RawMessage; class LogTransport implements TransportInterface { /** * The Logger instance. * * @var \Psr\Log\LoggerInterface */ protected $logger; /** * Create a new log transport instance. * * @param \Psr\Log\LoggerInterface $logger * @return void */ public function __construct(LoggerInterface $logger) { $this->logger = $logger; } /** * {@inheritdoc} */ public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage { $string = $message->toString(); if (str_contains($string, 'Content-Transfer-Encoding: quoted-printable')) { $string = quoted_printable_decode($string); } $this->logger->debug($string); return new SentMessage($message, $envelope ?? Envelope::create($message)); } /** * Get the logger for the LogTransport instance. * * @return \Psr\Log\LoggerInterface */ public function logger() { return $this->logger; } /** * Get the string representation of the transport. * * @return string */ public function __toString(): string { return 'log'; } } src/Illuminate/Mail/Transport/ArrayTransport.php 0000644 00000002655 15107327037 0016035 0 ustar 00 <?php namespace Illuminate\Mail\Transport; use Illuminate\Support\Collection; use Symfony\Component\Mailer\Envelope; use Symfony\Component\Mailer\SentMessage; use Symfony\Component\Mailer\Transport\TransportInterface; use Symfony\Component\Mime\RawMessage; class ArrayTransport implements TransportInterface { /** * The collection of Symfony Messages. * * @var \Illuminate\Support\Collection */ protected $messages; /** * Create a new array transport instance. * * @return void */ public function __construct() { $this->messages = new Collection; } /** * {@inheritdoc} */ public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage { return $this->messages[] = new SentMessage($message, $envelope ?? Envelope::create($message)); } /** * Retrieve the collection of messages. * * @return \Illuminate\Support\Collection */ public function messages() { return $this->messages; } /** * Clear all of the messages from the local collection. * * @return \Illuminate\Support\Collection */ public function flush() { return $this->messages = new Collection; } /** * Get the string representation of the transport. * * @return string */ public function __toString(): string { return 'array'; } } src/Illuminate/Mail/Transport/SesV2Transport.php 0000644 00000007105 15107327037 0015714 0 ustar 00 <?php namespace Illuminate\Mail\Transport; use Aws\Exception\AwsException; use Aws\SesV2\SesV2Client; use Symfony\Component\Mailer\Exception\TransportException; use Symfony\Component\Mailer\Header\MetadataHeader; use Symfony\Component\Mailer\SentMessage; use Symfony\Component\Mailer\Transport\AbstractTransport; use Symfony\Component\Mime\Message; class SesV2Transport extends AbstractTransport { /** * The Amazon SES V2 instance. * * @var \Aws\SesV2\SesV2Client */ protected $ses; /** * The Amazon SES transmission options. * * @var array */ protected $options = []; /** * Create a new SES V2 transport instance. * * @param \Aws\SesV2\SesV2Client $ses * @param array $options * @return void */ public function __construct(SesV2Client $ses, $options = []) { $this->ses = $ses; $this->options = $options; parent::__construct(); } /** * {@inheritDoc} */ protected function doSend(SentMessage $message): void { $options = $this->options; if ($message->getOriginalMessage() instanceof Message) { foreach ($message->getOriginalMessage()->getHeaders()->all() as $header) { if ($header instanceof MetadataHeader) { $options['Tags'][] = ['Name' => $header->getKey(), 'Value' => $header->getValue()]; } } } try { $result = $this->ses->sendEmail( array_merge( $options, [ 'Source' => $message->getEnvelope()->getSender()->toString(), 'Destination' => [ 'ToAddresses' => collect($message->getEnvelope()->getRecipients()) ->map ->toString() ->values() ->all(), ], 'Content' => [ 'Raw' => [ 'Data' => $message->toString(), ], ], ] ) ); } catch (AwsException $e) { $reason = $e->getAwsErrorMessage() ?? $e->getMessage(); throw new TransportException( sprintf('Request to AWS SES V2 API failed. Reason: %s.', $reason), is_int($e->getCode()) ? $e->getCode() : 0, $e ); } $messageId = $result->get('MessageId'); $message->getOriginalMessage()->getHeaders()->addHeader('X-Message-ID', $messageId); $message->getOriginalMessage()->getHeaders()->addHeader('X-SES-Message-ID', $messageId); } /** * Get the Amazon SES V2 client for the SesV2Transport instance. * * @return \Aws\SesV2\SesV2Client */ public function ses() { return $this->ses; } /** * Get the transmission options being used by the transport. * * @return array */ public function getOptions() { return $this->options; } /** * Set the transmission options being used by the transport. * * @param array $options * @return array */ public function setOptions(array $options) { return $this->options = $options; } /** * Get the string representation of the transport. * * @return string */ public function __toString(): string { return 'ses-v2'; } } src/Illuminate/Mail/Transport/SesTransport.php 0000644 00000006603 15107327037 0015506 0 ustar 00 <?php namespace Illuminate\Mail\Transport; use Aws\Exception\AwsException; use Aws\Ses\SesClient; use Symfony\Component\Mailer\Exception\TransportException; use Symfony\Component\Mailer\Header\MetadataHeader; use Symfony\Component\Mailer\SentMessage; use Symfony\Component\Mailer\Transport\AbstractTransport; use Symfony\Component\Mime\Message; class SesTransport extends AbstractTransport { /** * The Amazon SES instance. * * @var \Aws\Ses\SesClient */ protected $ses; /** * The Amazon SES transmission options. * * @var array */ protected $options = []; /** * Create a new SES transport instance. * * @param \Aws\Ses\SesClient $ses * @param array $options * @return void */ public function __construct(SesClient $ses, $options = []) { $this->ses = $ses; $this->options = $options; parent::__construct(); } /** * {@inheritDoc} */ protected function doSend(SentMessage $message): void { $options = $this->options; if ($message->getOriginalMessage() instanceof Message) { foreach ($message->getOriginalMessage()->getHeaders()->all() as $header) { if ($header instanceof MetadataHeader) { $options['Tags'][] = ['Name' => $header->getKey(), 'Value' => $header->getValue()]; } } } try { $result = $this->ses->sendRawEmail( array_merge( $options, [ 'Source' => $message->getEnvelope()->getSender()->toString(), 'Destinations' => collect($message->getEnvelope()->getRecipients()) ->map ->toString() ->values() ->all(), 'RawMessage' => [ 'Data' => $message->toString(), ], ] ) ); } catch (AwsException $e) { $reason = $e->getAwsErrorMessage() ?? $e->getMessage(); throw new TransportException( sprintf('Request to AWS SES API failed. Reason: %s.', $reason), is_int($e->getCode()) ? $e->getCode() : 0, $e ); } $messageId = $result->get('MessageId'); $message->getOriginalMessage()->getHeaders()->addHeader('X-Message-ID', $messageId); $message->getOriginalMessage()->getHeaders()->addHeader('X-SES-Message-ID', $messageId); } /** * Get the Amazon SES client for the SesTransport instance. * * @return \Aws\Ses\SesClient */ public function ses() { return $this->ses; } /** * Get the transmission options being used by the transport. * * @return array */ public function getOptions() { return $this->options; } /** * Set the transmission options being used by the transport. * * @param array $options * @return array */ public function setOptions(array $options) { return $this->options = $options; } /** * Get the string representation of the transport. * * @return string */ public function __toString(): string { return 'ses'; } } src/Illuminate/Mail/Mailables/Content.php 0000644 00000006044 15107327037 0014345 0 ustar 00 <?php namespace Illuminate\Mail\Mailables; use Illuminate\Support\Traits\Conditionable; class Content { use Conditionable; /** * The Blade view that should be rendered for the mailable. * * @var string|null */ public $view; /** * The Blade view that should be rendered for the mailable. * * Alternative syntax for "view". * * @var string|null */ public $html; /** * The Blade view that represents the text version of the message. * * @var string|null */ public $text; /** * The Blade view that represents the Markdown version of the message. * * @var string|null */ public $markdown; /** * The pre-rendered HTML of the message. * * @var string|null */ public $htmlString; /** * The message's view data. * * @var array */ public $with; /** * Create a new content definition. * * @param string|null $view * @param string|null $html * @param string|null $text * @param string|null $markdown * @param array $with * @param string|null $htmlString * * @named-arguments-supported */ public function __construct(string $view = null, string $html = null, string $text = null, $markdown = null, array $with = [], string $htmlString = null) { $this->view = $view; $this->html = $html; $this->text = $text; $this->markdown = $markdown; $this->with = $with; $this->htmlString = $htmlString; } /** * Set the view for the message. * * @param string $view * @return $this */ public function view(string $view) { $this->view = $view; return $this; } /** * Set the view for the message. * * @param string $view * @return $this */ public function html(string $view) { return $this->view($view); } /** * Set the plain text view for the message. * * @param string $view * @return $this */ public function text(string $view) { $this->text = $view; return $this; } /** * Set the Markdown view for the message. * * @param string $view * @return $this */ public function markdown(string $view) { $this->markdown = $view; return $this; } /** * Set the pre-rendered HTML for the message. * * @param string $html * @return $this */ public function htmlString(string $html) { $this->htmlString = $html; return $this; } /** * Add a piece of view data to the message. * * @param array|string $key * @param mixed|null $value * @return $this */ public function with($key, $value = null) { if (is_array($key)) { $this->with = array_merge($this->with, $key); } else { $this->with[$key] = $value; } return $this; } } src/Illuminate/Mail/Mailables/Envelope.php 0000644 00000021546 15107327037 0014514 0 ustar 00 <?php namespace Illuminate\Mail\Mailables; use Closure; use Illuminate\Support\Arr; use Illuminate\Support\Traits\Conditionable; class Envelope { use Conditionable; /** * The address sending the message. * * @var \Illuminate\Mail\Mailables\Address|string|null */ public $from; /** * The recipients of the message. * * @var array */ public $to; /** * The recipients receiving a copy of the message. * * @var array */ public $cc; /** * The recipients receiving a blind copy of the message. * * @var array */ public $bcc; /** * The recipients that should be replied to. * * @var array */ public $replyTo; /** * The subject of the message. * * @var string|null */ public $subject; /** * The message's tags. * * @var array */ public $tags = []; /** * The message's meta data. * * @var array */ public $metadata = []; /** * The message's Symfony Message customization callbacks. * * @var array */ public $using = []; /** * Create a new message envelope instance. * * @param \Illuminate\Mail\Mailables\Address|string|null $from * @param array $to * @param array $cc * @param array $bcc * @param array $replyTo * @param string|null $subject * @param array $tags * @param array $metadata * @param \Closure|array $using * @return void * * @named-arguments-supported */ public function __construct(Address|string $from = null, $to = [], $cc = [], $bcc = [], $replyTo = [], string $subject = null, array $tags = [], array $metadata = [], Closure|array $using = []) { $this->from = is_string($from) ? new Address($from) : $from; $this->to = $this->normalizeAddresses($to); $this->cc = $this->normalizeAddresses($cc); $this->bcc = $this->normalizeAddresses($bcc); $this->replyTo = $this->normalizeAddresses($replyTo); $this->subject = $subject; $this->tags = $tags; $this->metadata = $metadata; $this->using = Arr::wrap($using); } /** * Normalize the given array of addresses. * * @param array $addresses * @return array */ protected function normalizeAddresses($addresses) { return collect($addresses)->map(function ($address) { return is_string($address) ? new Address($address) : $address; })->all(); } /** * Specify who the message will be "from". * * @param \Illuminate\Mail\Mailables\Address|string $address * @param string|null $name * @return $this */ public function from(Address|string $address, $name = null) { $this->from = is_string($address) ? new Address($address, $name) : $address; return $this; } /** * Add a "to" recipient to the message envelope. * * @param \Illuminate\Mail\Mailables\Address|array|string $address * @param string|null $name * @return $this */ public function to(Address|array|string $address, $name = null) { $this->to = array_merge($this->to, $this->normalizeAddresses( is_string($name) ? [new Address($address, $name)] : Arr::wrap($address), )); return $this; } /** * Add a "cc" recipient to the message envelope. * * @param \Illuminate\Mail\Mailables\Address|array|string $address * @param string|null $name * @return $this */ public function cc(Address|array|string $address, $name = null) { $this->cc = array_merge($this->cc, $this->normalizeAddresses( is_string($name) ? [new Address($address, $name)] : Arr::wrap($address), )); return $this; } /** * Add a "bcc" recipient to the message envelope. * * @param \Illuminate\Mail\Mailables\Address|array|string $address * @param string|null $name * @return $this */ public function bcc(Address|array|string $address, $name = null) { $this->bcc = array_merge($this->bcc, $this->normalizeAddresses( is_string($name) ? [new Address($address, $name)] : Arr::wrap($address), )); return $this; } /** * Add a "reply to" recipient to the message envelope. * * @param \Illuminate\Mail\Mailables\Address|array|string $address * @param string|null $name * @return $this */ public function replyTo(Address|array|string $address, $name = null) { $this->replyTo = array_merge($this->replyTo, $this->normalizeAddresses( is_string($name) ? [new Address($address, $name)] : Arr::wrap($address), )); return $this; } /** * Set the subject of the message. * * @param string $subject * @return $this */ public function subject(string $subject) { $this->subject = $subject; return $this; } /** * Add "tags" to the message. * * @param array $tags * @return $this */ public function tags(array $tags) { $this->tags = array_merge($this->tags, $tags); return $this; } /** * Add a "tag" to the message. * * @param string $tag * @return $this */ public function tag(string $tag) { $this->tags[] = $tag; return $this; } /** * Add metadata to the message. * * @param string $key * @param string|int $value * @return $this */ public function metadata(string $key, string|int $value) { $this->metadata[$key] = $value; return $this; } /** * Add a Symfony Message customization callback to the message. * * @param \Closure $callback * @return $this */ public function using(Closure $callback) { $this->using[] = $callback; return $this; } /** * Determine if the message is from the given address. * * @param string $address * @param string|null $name * @return bool */ public function isFrom(string $address, string $name = null) { if (is_null($name)) { return $this->from->address === $address; } return $this->from->address === $address && $this->from->name === $name; } /** * Determine if the message has the given address as a recipient. * * @param string $address * @param string|null $name * @return bool */ public function hasTo(string $address, string $name = null) { return $this->hasRecipient($this->to, $address, $name); } /** * Determine if the message has the given address as a "cc" recipient. * * @param string $address * @param string|null $name * @return bool */ public function hasCc(string $address, string $name = null) { return $this->hasRecipient($this->cc, $address, $name); } /** * Determine if the message has the given address as a "bcc" recipient. * * @param string $address * @param string|null $name * @return bool */ public function hasBcc(string $address, string $name = null) { return $this->hasRecipient($this->bcc, $address, $name); } /** * Determine if the message has the given address as a "reply to" recipient. * * @param string $address * @param string|null $name * @return bool */ public function hasReplyTo(string $address, string $name = null) { return $this->hasRecipient($this->replyTo, $address, $name); } /** * Determine if the message has the given recipient. * * @param array $recipients * @param string $address * @param string|null $name * @return bool */ protected function hasRecipient(array $recipients, string $address, ?string $name = null) { return collect($recipients)->contains(function ($recipient) use ($address, $name) { if (is_null($name)) { return $recipient->address === $address; } return $recipient->address === $address && $recipient->name === $name; }); } /** * Determine if the message has the given subject. * * @param string $subject * @return bool */ public function hasSubject(string $subject) { return $this->subject === $subject; } /** * Determine if the message has the given metadata. * * @param string $key * @param string $value * @return bool */ public function hasMetadata(string $key, string $value) { return isset($this->metadata[$key]) && (string) $this->metadata[$key] === $value; } } src/Illuminate/Mail/Mailables/Headers.php 0000644 00000003755 15107327037 0014314 0 ustar 00 <?php namespace Illuminate\Mail\Mailables; use Illuminate\Support\Str; use Illuminate\Support\Traits\Conditionable; class Headers { use Conditionable; /** * The message's message ID. * * @var string|null */ public $messageId; /** * The message IDs that are referenced by the message. * * @var array */ public $references; /** * The message's text headers. * * @var array */ public $text; /** * Create a new instance of headers for a message. * * @param string|null $messageId * @param array $references * @param array $text * @return void * * @named-arguments-supported */ public function __construct(string $messageId = null, array $references = [], array $text = []) { $this->messageId = $messageId; $this->references = $references; $this->text = $text; } /** * Set the message ID. * * @param string $messageId * @return $this */ public function messageId(string $messageId) { $this->messageId = $messageId; return $this; } /** * Set the message IDs referenced by this message. * * @param array $references * @return $this */ public function references(array $references) { $this->references = array_merge($this->references, $references); return $this; } /** * Set the headers for this message. * * @param array $references * @return $this */ public function text(array $text) { $this->text = array_merge($this->text, $text); return $this; } /** * Get the references header as a string. * * @return string */ public function referencesString(): string { return collect($this->references)->map(function ($messageId) { return Str::finish(Str::start($messageId, '<'), '>'); })->implode(' '); } } src/Illuminate/Mail/Mailables/Address.php 0000644 00000001051 15107327037 0014311 0 ustar 00 <?php namespace Illuminate\Mail\Mailables; class Address { /** * The recipient's email address. * * @var string */ public $address; /** * The recipient's name. * * @var string|null */ public $name; /** * Create a new address instance. * * @param string $address * @param string|null $name * @return void */ public function __construct(string $address, string $name = null) { $this->address = $address; $this->name = $name; } } src/Illuminate/Mail/Mailables/Attachment.php 0000644 00000000265 15107327037 0015022 0 ustar 00 <?php namespace Illuminate\Mail\Mailables; use Illuminate\Mail\Attachment as BaseAttachment; class Attachment extends BaseAttachment { // Here for namespace consistency... } src/Illuminate/Mail/PendingMail.php 0000644 00000006733 15107327037 0013236 0 ustar 00 <?php namespace Illuminate\Mail; use Illuminate\Contracts\Mail\Mailable as MailableContract; use Illuminate\Contracts\Mail\Mailer as MailerContract; use Illuminate\Contracts\Translation\HasLocalePreference; use Illuminate\Support\Traits\Conditionable; class PendingMail { use Conditionable; /** * The mailer instance. * * @var \Illuminate\Contracts\Mail\Mailer */ protected $mailer; /** * The locale of the message. * * @var string */ protected $locale; /** * The "to" recipients of the message. * * @var array */ protected $to = []; /** * The "cc" recipients of the message. * * @var array */ protected $cc = []; /** * The "bcc" recipients of the message. * * @var array */ protected $bcc = []; /** * Create a new mailable mailer instance. * * @param \Illuminate\Contracts\Mail\Mailer $mailer * @return void */ public function __construct(MailerContract $mailer) { $this->mailer = $mailer; } /** * Set the locale of the message. * * @param string $locale * @return $this */ public function locale($locale) { $this->locale = $locale; return $this; } /** * Set the recipients of the message. * * @param mixed $users * @return $this */ public function to($users) { $this->to = $users; if (! $this->locale && $users instanceof HasLocalePreference) { $this->locale($users->preferredLocale()); } return $this; } /** * Set the recipients of the message. * * @param mixed $users * @return $this */ public function cc($users) { $this->cc = $users; return $this; } /** * Set the recipients of the message. * * @param mixed $users * @return $this */ public function bcc($users) { $this->bcc = $users; return $this; } /** * Send a new mailable message instance. * * @param \Illuminate\Contracts\Mail\Mailable $mailable * @return \Illuminate\Mail\SentMessage|null */ public function send(MailableContract $mailable) { return $this->mailer->send($this->fill($mailable)); } /** * Push the given mailable onto the queue. * * @param \Illuminate\Contracts\Mail\Mailable $mailable * @return mixed */ public function queue(MailableContract $mailable) { return $this->mailer->queue($this->fill($mailable)); } /** * Deliver the queued message after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param \Illuminate\Contracts\Mail\Mailable $mailable * @return mixed */ public function later($delay, MailableContract $mailable) { return $this->mailer->later($delay, $this->fill($mailable)); } /** * Populate the mailable with the addresses. * * @param \Illuminate\Contracts\Mail\Mailable $mailable * @return \Illuminate\Mail\Mailable */ protected function fill(MailableContract $mailable) { return tap($mailable->to($this->to) ->cc($this->cc) ->bcc($this->bcc), function (MailableContract $mailable) { if ($this->locale) { $mailable->locale($this->locale); } }); } } src/Illuminate/Mail/SentMessage.php 0000644 00000003744 15107327037 0013264 0 ustar 00 <?php namespace Illuminate\Mail; use Illuminate\Support\Traits\ForwardsCalls; use Symfony\Component\Mailer\SentMessage as SymfonySentMessage; /** * @mixin \Symfony\Component\Mailer\SentMessage */ class SentMessage { use ForwardsCalls; /** * The Symfony SentMessage instance. * * @var \Symfony\Component\Mailer\SentMessage */ protected $sentMessage; /** * Create a new SentMessage instance. * * @param \Symfony\Component\Mailer\SentMessage $sentMessage * @return void */ public function __construct(SymfonySentMessage $sentMessage) { $this->sentMessage = $sentMessage; } /** * Get the underlying Symfony Email instance. * * @return \Symfony\Component\Mailer\SentMessage */ public function getSymfonySentMessage() { return $this->sentMessage; } /** * Dynamically pass missing methods to the Symfony instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->forwardCallTo($this->sentMessage, $method, $parameters); } /** * Get the serializable representation of the object. * * @return array */ public function __serialize() { $hasAttachments = collect($this->sentMessage->getOriginalMessage()->getAttachments())->isNotEmpty(); return [ 'hasAttachments' => $hasAttachments, 'sentMessage' => $hasAttachments ? base64_encode(serialize($this->sentMessage)) : $this->sentMessage, ]; } /** * Marshal the object from its serialized data. * * @param array $data * @return void */ public function __unserialize(array $data) { $hasAttachments = ($data['hasAttachments'] ?? false) === true; $this->sentMessage = $hasAttachments ? unserialize(base64_decode($data['sentMessage'])) : $data['sentMessage']; } } src/Illuminate/Mail/SendQueuedMailable.php 0000644 00000007143 15107327037 0014534 0 ustar 00 <?php namespace Illuminate\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Mail\Factory as MailFactory; use Illuminate\Contracts\Mail\Mailable as MailableContract; use Illuminate\Contracts\Queue\ShouldBeEncrypted; use Illuminate\Contracts\Queue\ShouldQueueAfterCommit; use Illuminate\Queue\InteractsWithQueue; class SendQueuedMailable { use Queueable, InteractsWithQueue; /** * The mailable message instance. * * @var \Illuminate\Contracts\Mail\Mailable */ public $mailable; /** * The number of times the job may be attempted. * * @var int */ public $tries; /** * The number of seconds the job can run before timing out. * * @var int */ public $timeout; /** * The maximum number of unhandled exceptions to allow before failing. * * @return int|null */ public $maxExceptions; /** * Indicates if the job should be encrypted. * * @var bool */ public $shouldBeEncrypted = false; /** * Create a new job instance. * * @param \Illuminate\Contracts\Mail\Mailable $mailable * @return void */ public function __construct(MailableContract $mailable) { $this->mailable = $mailable; if ($mailable instanceof ShouldQueueAfterCommit) { $this->afterCommit = true; } else { $this->afterCommit = property_exists($mailable, 'afterCommit') ? $mailable->afterCommit : null; } $this->connection = property_exists($mailable, 'connection') ? $mailable->connection : null; $this->maxExceptions = property_exists($mailable, 'maxExceptions') ? $mailable->maxExceptions : null; $this->queue = property_exists($mailable, 'queue') ? $mailable->queue : null; $this->shouldBeEncrypted = $mailable instanceof ShouldBeEncrypted; $this->timeout = property_exists($mailable, 'timeout') ? $mailable->timeout : null; $this->tries = property_exists($mailable, 'tries') ? $mailable->tries : null; } /** * Handle the queued job. * * @param \Illuminate\Contracts\Mail\Factory $factory * @return void */ public function handle(MailFactory $factory) { $this->mailable->send($factory); } /** * Get the number of seconds before a released mailable will be available. * * @return mixed */ public function backoff() { if (! method_exists($this->mailable, 'backoff') && ! isset($this->mailable->backoff)) { return; } return $this->mailable->backoff ?? $this->mailable->backoff(); } /** * Determine the time at which the job should timeout. * * @return \DateTime|null */ public function retryUntil() { if (! method_exists($this->mailable, 'retryUntil') && ! isset($this->mailable->retryUntil)) { return; } return $this->mailable->retryUntil ?? $this->mailable->retryUntil(); } /** * Call the failed method on the mailable instance. * * @param \Throwable $e * @return void */ public function failed($e) { if (method_exists($this->mailable, 'failed')) { $this->mailable->failed($e); } } /** * Get the display name for the queued job. * * @return string */ public function displayName() { return get_class($this->mailable); } /** * Prepare the instance for cloning. * * @return void */ public function __clone() { $this->mailable = clone $this->mailable; } } src/Illuminate/Mail/Mailable.php 0000644 00000133700 15107327037 0012550 0 ustar 00 <?php namespace Illuminate\Mail; use Illuminate\Config\Repository as ConfigRepository; use Illuminate\Container\Container; use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory; use Illuminate\Contracts\Mail\Attachable; use Illuminate\Contracts\Mail\Factory as MailFactory; use Illuminate\Contracts\Mail\Mailable as MailableContract; use Illuminate\Contracts\Queue\Factory as Queue; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Contracts\Support\Renderable; use Illuminate\Contracts\Translation\HasLocalePreference; use Illuminate\Support\Collection; use Illuminate\Support\HtmlString; use Illuminate\Support\Str; use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\Localizable; use Illuminate\Support\Traits\Macroable; use Illuminate\Testing\Constraints\SeeInOrder; use PHPUnit\Framework\Assert as PHPUnit; use ReflectionClass; use ReflectionProperty; use Symfony\Component\Mailer\Header\MetadataHeader; use Symfony\Component\Mailer\Header\TagHeader; use Symfony\Component\Mime\Address; class Mailable implements MailableContract, Renderable { use Conditionable, ForwardsCalls, Localizable, Macroable { __call as macroCall; } /** * The locale of the message. * * @var string */ public $locale; /** * The person the message is from. * * @var array */ public $from = []; /** * The "to" recipients of the message. * * @var array */ public $to = []; /** * The "cc" recipients of the message. * * @var array */ public $cc = []; /** * The "bcc" recipients of the message. * * @var array */ public $bcc = []; /** * The "reply to" recipients of the message. * * @var array */ public $replyTo = []; /** * The subject of the message. * * @var string */ public $subject; /** * The Markdown template for the message (if applicable). * * @var string */ public $markdown; /** * The HTML to use for the message. * * @var string */ protected $html; /** * The view to use for the message. * * @var string */ public $view; /** * The plain text view to use for the message. * * @var string */ public $textView; /** * The view data for the message. * * @var array */ public $viewData = []; /** * The attachments for the message. * * @var array */ public $attachments = []; /** * The raw attachments for the message. * * @var array */ public $rawAttachments = []; /** * The attachments from a storage disk. * * @var array */ public $diskAttachments = []; /** * The tags for the message. * * @var array */ protected $tags = []; /** * The metadata for the message. * * @var array */ protected $metadata = []; /** * The callbacks for the message. * * @var array */ public $callbacks = []; /** * The name of the theme that should be used when formatting the message. * * @var string|null */ public $theme; /** * The name of the mailer that should send the message. * * @var string */ public $mailer; /** * The rendered mailable views for testing / assertions. * * @var array */ protected $assertionableRenderStrings; /** * The callback that should be invoked while building the view data. * * @var callable */ public static $viewDataCallback; /** * Send the message using the given mailer. * * @param \Illuminate\Contracts\Mail\Factory|\Illuminate\Contracts\Mail\Mailer $mailer * @return \Illuminate\Mail\SentMessage|null */ public function send($mailer) { return $this->withLocale($this->locale, function () use ($mailer) { $this->prepareMailableForDelivery(); $mailer = $mailer instanceof MailFactory ? $mailer->mailer($this->mailer) : $mailer; return $mailer->send($this->buildView(), $this->buildViewData(), function ($message) { $this->buildFrom($message) ->buildRecipients($message) ->buildSubject($message) ->buildTags($message) ->buildMetadata($message) ->runCallbacks($message) ->buildAttachments($message); }); }); } /** * Queue the message for sending. * * @param \Illuminate\Contracts\Queue\Factory $queue * @return mixed */ public function queue(Queue $queue) { if (isset($this->delay)) { return $this->later($this->delay, $queue); } $connection = property_exists($this, 'connection') ? $this->connection : null; $queueName = property_exists($this, 'queue') ? $this->queue : null; return $queue->connection($connection)->pushOn( $queueName ?: null, $this->newQueuedJob() ); } /** * Deliver the queued message after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param \Illuminate\Contracts\Queue\Factory $queue * @return mixed */ public function later($delay, Queue $queue) { $connection = property_exists($this, 'connection') ? $this->connection : null; $queueName = property_exists($this, 'queue') ? $this->queue : null; return $queue->connection($connection)->laterOn( $queueName ?: null, $delay, $this->newQueuedJob() ); } /** * Make the queued mailable job instance. * * @return mixed */ protected function newQueuedJob() { return Container::getInstance()->make(SendQueuedMailable::class, ['mailable' => $this]) ->through(array_merge( method_exists($this, 'middleware') ? $this->middleware() : [], $this->middleware ?? [] )); } /** * Render the mailable into a view. * * @return string * * @throws \ReflectionException */ public function render() { return $this->withLocale($this->locale, function () { $this->prepareMailableForDelivery(); return Container::getInstance()->make('mailer')->render( $this->buildView(), $this->buildViewData() ); }); } /** * Build the view for the message. * * @return array|string * * @throws \ReflectionException */ protected function buildView() { if (isset($this->html)) { return array_filter([ 'html' => new HtmlString($this->html), 'text' => $this->textView ?? null, ]); } if (isset($this->markdown)) { return $this->buildMarkdownView(); } if (isset($this->view, $this->textView)) { return [$this->view, $this->textView]; } elseif (isset($this->textView)) { return ['text' => $this->textView]; } return $this->view; } /** * Build the Markdown view for the message. * * @return array * * @throws \ReflectionException */ protected function buildMarkdownView() { $data = $this->buildViewData(); return [ 'html' => $this->buildMarkdownHtml($data), 'text' => $this->buildMarkdownText($data), ]; } /** * Build the view data for the message. * * @return array * * @throws \ReflectionException */ public function buildViewData() { $data = $this->viewData; if (static::$viewDataCallback) { $data = array_merge($data, call_user_func(static::$viewDataCallback, $this)); } foreach ((new ReflectionClass($this))->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { if ($property->getDeclaringClass()->getName() !== self::class) { $data[$property->getName()] = $property->getValue($this); } } return $data; } /** * Build the HTML view for a Markdown message. * * @param array $viewData * @return \Closure */ protected function buildMarkdownHtml($viewData) { return fn ($data) => $this->markdownRenderer()->render( $this->markdown, array_merge($data, $viewData), ); } /** * Build the text view for a Markdown message. * * @param array $viewData * @return \Closure */ protected function buildMarkdownText($viewData) { return function ($data) use ($viewData) { if (isset($data['message'])) { $data = array_merge($data, [ 'message' => new TextMessage($data['message']), ]); } return $this->textView ?? $this->markdownRenderer()->renderText( $this->markdown, array_merge($data, $viewData) ); }; } /** * Resolves a Markdown instance with the mail's theme. * * @return \Illuminate\Mail\Markdown */ protected function markdownRenderer() { return tap(Container::getInstance()->make(Markdown::class), function ($markdown) { $markdown->theme($this->theme ?: Container::getInstance()->get(ConfigRepository::class)->get( 'mail.markdown.theme', 'default') ); }); } /** * Add the sender to the message. * * @param \Illuminate\Mail\Message $message * @return $this */ protected function buildFrom($message) { if (! empty($this->from)) { $message->from($this->from[0]['address'], $this->from[0]['name']); } return $this; } /** * Add all of the recipients to the message. * * @param \Illuminate\Mail\Message $message * @return $this */ protected function buildRecipients($message) { foreach (['to', 'cc', 'bcc', 'replyTo'] as $type) { foreach ($this->{$type} as $recipient) { $message->{$type}($recipient['address'], $recipient['name']); } } return $this; } /** * Set the subject for the message. * * @param \Illuminate\Mail\Message $message * @return $this */ protected function buildSubject($message) { if ($this->subject) { $message->subject($this->subject); } else { $message->subject(Str::title(Str::snake(class_basename($this), ' '))); } return $this; } /** * Add all of the attachments to the message. * * @param \Illuminate\Mail\Message $message * @return $this */ protected function buildAttachments($message) { foreach ($this->attachments as $attachment) { $message->attach($attachment['file'], $attachment['options']); } foreach ($this->rawAttachments as $attachment) { $message->attachData( $attachment['data'], $attachment['name'], $attachment['options'] ); } $this->buildDiskAttachments($message); return $this; } /** * Add all of the disk attachments to the message. * * @param \Illuminate\Mail\Message $message * @return void */ protected function buildDiskAttachments($message) { foreach ($this->diskAttachments as $attachment) { $storage = Container::getInstance()->make( FilesystemFactory::class )->disk($attachment['disk']); $message->attachData( $storage->get($attachment['path']), $attachment['name'] ?? basename($attachment['path']), array_merge(['mime' => $storage->mimeType($attachment['path'])], $attachment['options']) ); } } /** * Add all defined tags to the message. * * @param \Illuminate\Mail\Message $message * @return $this */ protected function buildTags($message) { if ($this->tags) { foreach ($this->tags as $tag) { $message->getHeaders()->add(new TagHeader($tag)); } } return $this; } /** * Add all defined metadata to the message. * * @param \Illuminate\Mail\Message $message * @return $this */ protected function buildMetadata($message) { if ($this->metadata) { foreach ($this->metadata as $key => $value) { $message->getHeaders()->add(new MetadataHeader($key, $value)); } } return $this; } /** * Run the callbacks for the message. * * @param \Illuminate\Mail\Message $message * @return $this */ protected function runCallbacks($message) { foreach ($this->callbacks as $callback) { $callback($message->getSymfonyMessage()); } return $this; } /** * Set the locale of the message. * * @param string $locale * @return $this */ public function locale($locale) { $this->locale = $locale; return $this; } /** * Set the priority of this message. * * The value is an integer where 1 is the highest priority and 5 is the lowest. * * @param int $level * @return $this */ public function priority($level = 3) { $this->callbacks[] = function ($message) use ($level) { $message->priority($level); }; return $this; } /** * Set the sender of the message. * * @param object|array|string $address * @param string|null $name * @return $this */ public function from($address, $name = null) { return $this->setAddress($address, $name, 'from'); } /** * Determine if the given recipient is set on the mailable. * * @param object|array|string $address * @param string|null $name * @return bool */ public function hasFrom($address, $name = null) { return $this->hasRecipient($address, $name, 'from'); } /** * Set the recipients of the message. * * @param object|array|string $address * @param string|null $name * @return $this */ public function to($address, $name = null) { if (! $this->locale && $address instanceof HasLocalePreference) { $this->locale($address->preferredLocale()); } return $this->setAddress($address, $name, 'to'); } /** * Determine if the given recipient is set on the mailable. * * @param object|array|string $address * @param string|null $name * @return bool */ public function hasTo($address, $name = null) { return $this->hasRecipient($address, $name, 'to'); } /** * Set the recipients of the message. * * @param object|array|string $address * @param string|null $name * @return $this */ public function cc($address, $name = null) { return $this->setAddress($address, $name, 'cc'); } /** * Determine if the given recipient is set on the mailable. * * @param object|array|string $address * @param string|null $name * @return bool */ public function hasCc($address, $name = null) { return $this->hasRecipient($address, $name, 'cc'); } /** * Set the recipients of the message. * * @param object|array|string $address * @param string|null $name * @return $this */ public function bcc($address, $name = null) { return $this->setAddress($address, $name, 'bcc'); } /** * Determine if the given recipient is set on the mailable. * * @param object|array|string $address * @param string|null $name * @return bool */ public function hasBcc($address, $name = null) { return $this->hasRecipient($address, $name, 'bcc'); } /** * Set the "reply to" address of the message. * * @param object|array|string $address * @param string|null $name * @return $this */ public function replyTo($address, $name = null) { return $this->setAddress($address, $name, 'replyTo'); } /** * Determine if the given replyTo is set on the mailable. * * @param object|array|string $address * @param string|null $name * @return bool */ public function hasReplyTo($address, $name = null) { return $this->hasRecipient($address, $name, 'replyTo'); } /** * Set the recipients of the message. * * All recipients are stored internally as [['name' => ?, 'address' => ?]] * * @param object|array|string $address * @param string|null $name * @param string $property * @return $this */ protected function setAddress($address, $name = null, $property = 'to') { if (empty($address)) { return $this; } foreach ($this->addressesToArray($address, $name) as $recipient) { $recipient = $this->normalizeRecipient($recipient); $this->{$property}[] = [ 'name' => $recipient->name ?? null, 'address' => $recipient->email, ]; } $this->{$property} = collect($this->{$property}) ->reverse() ->unique('address') ->reverse() ->values() ->all(); return $this; } /** * Convert the given recipient arguments to an array. * * @param object|array|string $address * @param string|null $name * @return array */ protected function addressesToArray($address, $name) { if (! is_array($address) && ! $address instanceof Collection) { $address = is_string($name) ? [['name' => $name, 'email' => $address]] : [$address]; } return $address; } /** * Convert the given recipient into an object. * * @param mixed $recipient * @return object */ protected function normalizeRecipient($recipient) { if (is_array($recipient)) { if (array_values($recipient) === $recipient) { return (object) array_map(function ($email) { return compact('email'); }, $recipient); } return (object) $recipient; } elseif (is_string($recipient)) { return (object) ['email' => $recipient]; } elseif ($recipient instanceof Address) { return (object) ['email' => $recipient->getAddress(), 'name' => $recipient->getName()]; } elseif ($recipient instanceof Mailables\Address) { return (object) ['email' => $recipient->address, 'name' => $recipient->name]; } return $recipient; } /** * Determine if the given recipient is set on the mailable. * * @param object|array|string $address * @param string|null $name * @param string $property * @return bool */ protected function hasRecipient($address, $name = null, $property = 'to') { if (empty($address)) { return false; } $expected = $this->normalizeRecipient( $this->addressesToArray($address, $name)[0] ); $expected = [ 'name' => $expected->name ?? null, 'address' => $expected->email, ]; if ($this->hasEnvelopeRecipient($expected['address'], $expected['name'], $property)) { return true; } return collect($this->{$property})->contains(function ($actual) use ($expected) { if (! isset($expected['name'])) { return $actual['address'] == $expected['address']; } return $actual == $expected; }); } /** * Determine if the mailable "envelope" method defines a recipient. * * @param string $address * @param string|null $name * @param string $property * @return bool */ private function hasEnvelopeRecipient($address, $name, $property) { return method_exists($this, 'envelope') && match ($property) { 'from' => $this->envelope()->isFrom($address, $name), 'to' => $this->envelope()->hasTo($address, $name), 'cc' => $this->envelope()->hasCc($address, $name), 'bcc' => $this->envelope()->hasBcc($address, $name), 'replyTo' => $this->envelope()->hasReplyTo($address, $name), }; } /** * Set the subject of the message. * * @param string $subject * @return $this */ public function subject($subject) { $this->subject = $subject; return $this; } /** * Determine if the mailable has the given subject. * * @param string $subject * @return bool */ public function hasSubject($subject) { return $this->subject === $subject || (method_exists($this, 'envelope') && $this->envelope()->hasSubject($subject)); } /** * Set the Markdown template for the message. * * @param string $view * @param array $data * @return $this */ public function markdown($view, array $data = []) { $this->markdown = $view; $this->viewData = array_merge($this->viewData, $data); return $this; } /** * Set the view and view data for the message. * * @param string $view * @param array $data * @return $this */ public function view($view, array $data = []) { $this->view = $view; $this->viewData = array_merge($this->viewData, $data); return $this; } /** * Set the rendered HTML content for the message. * * @param string $html * @return $this */ public function html($html) { $this->html = $html; return $this; } /** * Set the plain text view for the message. * * @param string $textView * @param array $data * @return $this */ public function text($textView, array $data = []) { $this->textView = $textView; $this->viewData = array_merge($this->viewData, $data); return $this; } /** * Set the view data for the message. * * @param string|array $key * @param mixed $value * @return $this */ public function with($key, $value = null) { if (is_array($key)) { $this->viewData = array_merge($this->viewData, $key); } else { $this->viewData[$key] = $value; } return $this; } /** * Attach a file to the message. * * @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file * @param array $options * @return $this */ public function attach($file, array $options = []) { if ($file instanceof Attachable) { $file = $file->toMailAttachment(); } if ($file instanceof Attachment) { return $file->attachTo($this, $options); } $this->attachments = collect($this->attachments) ->push(compact('file', 'options')) ->unique('file') ->all(); return $this; } /** * Attach multiple files to the message. * * @param array $files * @return $this */ public function attachMany($files) { foreach ($files as $file => $options) { if (is_int($file)) { $this->attach($options); } else { $this->attach($file, $options); } } return $this; } /** * Determine if the mailable has the given attachment. * * @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file * @param array $options * @return bool */ public function hasAttachment($file, array $options = []) { if ($file instanceof Attachable) { $file = $file->toMailAttachment(); } if ($file instanceof Attachment && $this->hasEnvelopeAttachment($file, $options)) { return true; } if ($file instanceof Attachment) { $parts = $file->attachWith( fn ($path) => [$path, [ 'as' => $options['as'] ?? $file->as, 'mime' => $options['mime'] ?? $file->mime, ]], fn ($data) => $this->hasAttachedData($data(), $options['as'] ?? $file->as, ['mime' => $options['mime'] ?? $file->mime]) ); if ($parts === true) { return true; } [$file, $options] = $parts === false ? [null, []] : $parts; } return collect($this->attachments)->contains( fn ($attachment) => $attachment['file'] === $file && array_filter($attachment['options']) === array_filter($options) ); } /** * Determine if the mailable has the given envelope attachment. * * @param \Illuminate\Mail\Attachment $attachment * @param array $options * @return bool */ private function hasEnvelopeAttachment($attachment, $options = []) { if (! method_exists($this, 'envelope')) { return false; } $attachments = $this->attachments(); return Collection::make(is_object($attachments) ? [$attachments] : $attachments) ->map(fn ($attached) => $attached instanceof Attachable ? $attached->toMailAttachment() : $attached) ->contains(fn ($attached) => $attached->isEquivalent($attachment, $options)); } /** * Attach a file to the message from storage. * * @param string $path * @param string|null $name * @param array $options * @return $this */ public function attachFromStorage($path, $name = null, array $options = []) { return $this->attachFromStorageDisk(null, $path, $name, $options); } /** * Attach a file to the message from storage. * * @param string $disk * @param string $path * @param string|null $name * @param array $options * @return $this */ public function attachFromStorageDisk($disk, $path, $name = null, array $options = []) { $this->diskAttachments = collect($this->diskAttachments)->push([ 'disk' => $disk, 'path' => $path, 'name' => $name ?? basename($path), 'options' => $options, ])->unique(function ($file) { return $file['name'].$file['disk'].$file['path']; })->all(); return $this; } /** * Determine if the mailable has the given attachment from storage. * * @param string $path * @param string|null $name * @param array $options * @return bool */ public function hasAttachmentFromStorage($path, $name = null, array $options = []) { return $this->hasAttachmentFromStorageDisk(null, $path, $name, $options); } /** * Determine if the mailable has the given attachment from a specific storage disk. * * @param string $disk * @param string $path * @param string|null $name * @param array $options * @return bool */ public function hasAttachmentFromStorageDisk($disk, $path, $name = null, array $options = []) { return collect($this->diskAttachments)->contains( fn ($attachment) => $attachment['disk'] === $disk && $attachment['path'] === $path && $attachment['name'] === ($name ?? basename($path)) && $attachment['options'] === $options ); } /** * Attach in-memory data as an attachment. * * @param string $data * @param string $name * @param array $options * @return $this */ public function attachData($data, $name, array $options = []) { $this->rawAttachments = collect($this->rawAttachments) ->push(compact('data', 'name', 'options')) ->unique(function ($file) { return $file['name'].$file['data']; })->all(); return $this; } /** * Determine if the mailable has the given data as an attachment. * * @param string $data * @param string $name * @param array $options * @return bool */ public function hasAttachedData($data, $name, array $options = []) { return collect($this->rawAttachments)->contains( fn ($attachment) => $attachment['data'] === $data && $attachment['name'] === $name && array_filter($attachment['options']) === array_filter($options) ); } /** * Add a tag header to the message when supported by the underlying transport. * * @param string $value * @return $this */ public function tag($value) { array_push($this->tags, $value); return $this; } /** * Determine if the mailable has the given tag. * * @param string $value * @return bool */ public function hasTag($value) { return in_array($value, $this->tags) || (method_exists($this, 'envelope') && in_array($value, $this->envelope()->tags)); } /** * Add a metadata header to the message when supported by the underlying transport. * * @param string $key * @param string $value * @return $this */ public function metadata($key, $value) { $this->metadata[$key] = $value; return $this; } /** * Determine if the mailable has the given metadata. * * @param string $key * @param string $value * @return bool */ public function hasMetadata($key, $value) { return (isset($this->metadata[$key]) && $this->metadata[$key] === $value) || (method_exists($this, 'envelope') && $this->envelope()->hasMetadata($key, $value)); } /** * Assert that the mailable is from the given address. * * @param object|array|string $address * @param string|null $name * @return $this */ public function assertFrom($address, $name = null) { $this->renderForAssertions(); $recipient = $this->formatAssertionRecipient($address, $name); PHPUnit::assertTrue( $this->hasFrom($address, $name), "Email was not from expected address [{$recipient}]." ); return $this; } /** * Assert that the mailable has the given recipient. * * @param object|array|string $address * @param string|null $name * @return $this */ public function assertTo($address, $name = null) { $this->renderForAssertions(); $recipient = $this->formatAssertionRecipient($address, $name); PHPUnit::assertTrue( $this->hasTo($address, $name), "Did not see expected recipient [{$recipient}] in email recipients." ); return $this; } /** * Assert that the mailable has the given recipient. * * @param object|array|string $address * @param string|null $name * @return $this */ public function assertHasTo($address, $name = null) { return $this->assertTo($address, $name); } /** * Assert that the mailable has the given recipient. * * @param object|array|string $address * @param string|null $name * @return $this */ public function assertHasCc($address, $name = null) { $this->renderForAssertions(); $recipient = $this->formatAssertionRecipient($address, $name); PHPUnit::assertTrue( $this->hasCc($address, $name), "Did not see expected recipient [{$recipient}] in email recipients." ); return $this; } /** * Assert that the mailable has the given recipient. * * @param object|array|string $address * @param string|null $name * @return $this */ public function assertHasBcc($address, $name = null) { $this->renderForAssertions(); $recipient = $this->formatAssertionRecipient($address, $name); PHPUnit::assertTrue( $this->hasBcc($address, $name), "Did not see expected recipient [{$recipient}] in email recipients." ); return $this; } /** * Assert that the mailable has the given "reply to" address. * * @param object|array|string $address * @param string|null $name * @return $this */ public function assertHasReplyTo($address, $name = null) { $this->renderForAssertions(); $replyTo = $this->formatAssertionRecipient($address, $name); PHPUnit::assertTrue( $this->hasReplyTo($address, $name), "Did not see expected address [{$replyTo}] as email 'reply to' recipient." ); return $this; } /** * Format the mailable recipient for display in an assertion message. * * @param object|array|string $address * @param string|null $name * @return string */ private function formatAssertionRecipient($address, $name = null) { if (! is_string($address)) { $address = json_encode($address); } if (filled($name)) { $address .= ' ('.$name.')'; } return $address; } /** * Assert that the mailable has the given subject. * * @param string $subject * @return $this */ public function assertHasSubject($subject) { $this->renderForAssertions(); PHPUnit::assertTrue( $this->hasSubject($subject), "Did not see expected text [{$subject}] in email subject." ); return $this; } /** * Assert that the given text is present in the HTML email body. * * @param string $string * @param bool $escape * @return $this */ public function assertSeeInHtml($string, $escape = true) { $string = $escape ? e($string) : $string; [$html, $text] = $this->renderForAssertions(); PHPUnit::assertStringContainsString( $string, $html, "Did not see expected text [{$string}] within email body." ); return $this; } /** * Assert that the given text is not present in the HTML email body. * * @param string $string * @param bool $escape * @return $this */ public function assertDontSeeInHtml($string, $escape = true) { $string = $escape ? e($string) : $string; [$html, $text] = $this->renderForAssertions(); PHPUnit::assertStringNotContainsString( $string, $html, "Saw unexpected text [{$string}] within email body." ); return $this; } /** * Assert that the given text strings are present in order in the HTML email body. * * @param array $strings * @param bool $escape * @return $this */ public function assertSeeInOrderInHtml($strings, $escape = true) { $strings = $escape ? array_map('e', $strings) : $strings; [$html, $text] = $this->renderForAssertions(); PHPUnit::assertThat($strings, new SeeInOrder($html)); return $this; } /** * Assert that the given text is present in the plain-text email body. * * @param string $string * @return $this */ public function assertSeeInText($string) { [$html, $text] = $this->renderForAssertions(); PHPUnit::assertStringContainsString( $string, $text, "Did not see expected text [{$string}] within text email body." ); return $this; } /** * Assert that the given text is not present in the plain-text email body. * * @param string $string * @return $this */ public function assertDontSeeInText($string) { [$html, $text] = $this->renderForAssertions(); PHPUnit::assertStringNotContainsString( $string, $text, "Saw unexpected text [{$string}] within text email body." ); return $this; } /** * Assert that the given text strings are present in order in the plain-text email body. * * @param array $strings * @return $this */ public function assertSeeInOrderInText($strings) { [$html, $text] = $this->renderForAssertions(); PHPUnit::assertThat($strings, new SeeInOrder($text)); return $this; } /** * Assert the mailable has the given attachment. * * @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file * @param array $options * @return $this */ public function assertHasAttachment($file, array $options = []) { $this->renderForAssertions(); PHPUnit::assertTrue( $this->hasAttachment($file, $options), 'Did not find the expected attachment.' ); return $this; } /** * Assert the mailable has the given data as an attachment. * * @param string $data * @param string $name * @param array $options * @return $this */ public function assertHasAttachedData($data, $name, array $options = []) { $this->renderForAssertions(); PHPUnit::assertTrue( $this->hasAttachedData($data, $name, $options), 'Did not find the expected attachment.' ); return $this; } /** * Assert the mailable has the given attachment from storage. * * @param string $path * @param string|null $name * @param array $options * @return $this */ public function assertHasAttachmentFromStorage($path, $name = null, array $options = []) { $this->renderForAssertions(); PHPUnit::assertTrue( $this->hasAttachmentFromStorage($path, $name, $options), 'Did not find the expected attachment.' ); return $this; } /** * Assert the mailable has the given attachment from a specific storage disk. * * @param string $disk * @param string $path * @param string|null $name * @param array $options * @return $this */ public function assertHasAttachmentFromStorageDisk($disk, $path, $name = null, array $options = []) { $this->renderForAssertions(); PHPUnit::assertTrue( $this->hasAttachmentFromStorageDisk($disk, $path, $name, $options), 'Did not find the expected attachment.' ); return $this; } /** * Assert that the mailable has the given tag. * * @param string $tag * @return $this */ public function assertHasTag($tag) { $this->renderForAssertions(); PHPUnit::assertTrue( $this->hasTag($tag), "Did not see expected tag [{$tag}] in email tags." ); return $this; } /** * Assert that the mailable has the given metadata. * * @param string $key * @param string $value * @return $this */ public function assertHasMetadata($key, $value) { $this->renderForAssertions(); PHPUnit::assertTrue( $this->hasMetadata($key, $value), "Did not see expected key [{$key}] and value [{$value}] in email metadata." ); return $this; } /** * Render the HTML and plain-text version of the mailable into views for assertions. * * @return array * * @throws \ReflectionException */ protected function renderForAssertions() { if ($this->assertionableRenderStrings) { return $this->assertionableRenderStrings; } return $this->assertionableRenderStrings = $this->withLocale($this->locale, function () { $this->prepareMailableForDelivery(); $html = Container::getInstance()->make('mailer')->render( $view = $this->buildView(), $this->buildViewData() ); if (is_array($view) && isset($view[1])) { $text = $view[1]; } $text ??= $view['text'] ?? ''; if (! empty($text) && ! $text instanceof Htmlable) { $text = Container::getInstance()->make('mailer')->render( $text, $this->buildViewData() ); } return [(string) $html, (string) $text]; }); } /** * Prepare the mailable instance for delivery. * * @return void */ protected function prepareMailableForDelivery() { if (method_exists($this, 'build')) { Container::getInstance()->call([$this, 'build']); } $this->ensureHeadersAreHydrated(); $this->ensureEnvelopeIsHydrated(); $this->ensureContentIsHydrated(); $this->ensureAttachmentsAreHydrated(); } /** * Ensure the mailable's headers are hydrated from the "headers" method. * * @return void */ private function ensureHeadersAreHydrated() { if (! method_exists($this, 'headers')) { return; } $headers = $this->headers(); $this->withSymfonyMessage(function ($message) use ($headers) { if ($headers->messageId) { $message->getHeaders()->addIdHeader('Message-Id', $headers->messageId); } if (count($headers->references) > 0) { $message->getHeaders()->addTextHeader('References', $headers->referencesString()); } foreach ($headers->text as $key => $value) { $message->getHeaders()->addTextHeader($key, $value); } }); } /** * Ensure the mailable's "envelope" data is hydrated from the "envelope" method. * * @return void */ private function ensureEnvelopeIsHydrated() { if (! method_exists($this, 'envelope')) { return; } $envelope = $this->envelope(); if (isset($envelope->from)) { $this->from($envelope->from->address, $envelope->from->name); } foreach (['to', 'cc', 'bcc', 'replyTo'] as $type) { foreach ($envelope->{$type} as $address) { $this->{$type}($address->address, $address->name); } } if ($envelope->subject) { $this->subject($envelope->subject); } foreach ($envelope->tags as $tag) { $this->tag($tag); } foreach ($envelope->metadata as $key => $value) { $this->metadata($key, $value); } foreach ($envelope->using as $callback) { $this->withSymfonyMessage($callback); } } /** * Ensure the mailable's content is hydrated from the "content" method. * * @return void */ private function ensureContentIsHydrated() { if (! method_exists($this, 'content')) { return; } $content = $this->content(); if ($content->view) { $this->view($content->view); } if ($content->html) { $this->view($content->html); } if ($content->text) { $this->text($content->text); } if ($content->markdown) { $this->markdown($content->markdown); } if ($content->htmlString) { $this->html($content->htmlString); } foreach ($content->with as $key => $value) { $this->with($key, $value); } } /** * Ensure the mailable's attachments are hydrated from the "attachments" method. * * @return void */ private function ensureAttachmentsAreHydrated() { if (! method_exists($this, 'attachments')) { return; } $attachments = $this->attachments(); Collection::make(is_object($attachments) ? [$attachments] : $attachments) ->each(function ($attachment) { $this->attach($attachment); }); } /** * Set the name of the mailer that should send the message. * * @param string $mailer * @return $this */ public function mailer($mailer) { $this->mailer = $mailer; return $this; } /** * Register a callback to be called with the Symfony message instance. * * @param callable $callback * @return $this */ public function withSymfonyMessage($callback) { $this->callbacks[] = $callback; return $this; } /** * Register a callback to be called while building the view data. * * @param callable $callback * @return void */ public static function buildViewDataUsing(callable $callback) { static::$viewDataCallback = $callback; } /** * Dynamically bind parameters to the message. * * @param string $method * @param array $parameters * @return $this * * @throws \BadMethodCallException */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } if (str_starts_with($method, 'with')) { return $this->with(Str::camel(substr($method, 4)), $parameters[0]); } static::throwBadMethodCallException($method); } } src/Illuminate/Mail/Mailer.php 0000644 00000044177 15107327037 0012264 0 ustar 00 <?php namespace Illuminate\Mail; use Closure; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Mail\Mailable as MailableContract; use Illuminate\Contracts\Mail\Mailer as MailerContract; use Illuminate\Contracts\Mail\MailQueue as MailQueueContract; use Illuminate\Contracts\Queue\Factory as QueueContract; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Contracts\View\Factory; use Illuminate\Mail\Events\MessageSending; use Illuminate\Mail\Events\MessageSent; use Illuminate\Mail\Mailables\Address; use Illuminate\Support\HtmlString; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; use Symfony\Component\Mailer\Envelope; use Symfony\Component\Mailer\Transport\TransportInterface; use Symfony\Component\Mime\Email; class Mailer implements MailerContract, MailQueueContract { use Macroable; /** * The name that is configured for the mailer. * * @var string */ protected $name; /** * The view factory instance. * * @var \Illuminate\Contracts\View\Factory */ protected $views; /** * The Symfony Transport instance. * * @var \Symfony\Component\Mailer\Transport\TransportInterface */ protected $transport; /** * The event dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher|null */ protected $events; /** * The global from address and name. * * @var array */ protected $from; /** * The global reply-to address and name. * * @var array */ protected $replyTo; /** * The global return path address. * * @var array */ protected $returnPath; /** * The global to address and name. * * @var array */ protected $to; /** * The queue factory implementation. * * @var \Illuminate\Contracts\Queue\Factory */ protected $queue; /** * Create a new Mailer instance. * * @param string $name * @param \Illuminate\Contracts\View\Factory $views * @param \Symfony\Component\Mailer\Transport\TransportInterface $transport * @param \Illuminate\Contracts\Events\Dispatcher|null $events * @return void */ public function __construct(string $name, Factory $views, TransportInterface $transport, Dispatcher $events = null) { $this->name = $name; $this->views = $views; $this->events = $events; $this->transport = $transport; } /** * Set the global from address and name. * * @param string $address * @param string|null $name * @return void */ public function alwaysFrom($address, $name = null) { $this->from = compact('address', 'name'); } /** * Set the global reply-to address and name. * * @param string $address * @param string|null $name * @return void */ public function alwaysReplyTo($address, $name = null) { $this->replyTo = compact('address', 'name'); } /** * Set the global return path address. * * @param string $address * @return void */ public function alwaysReturnPath($address) { $this->returnPath = compact('address'); } /** * Set the global to address and name. * * @param string $address * @param string|null $name * @return void */ public function alwaysTo($address, $name = null) { $this->to = compact('address', 'name'); } /** * Begin the process of mailing a mailable class instance. * * @param mixed $users * @param string|null $name * @return \Illuminate\Mail\PendingMail */ public function to($users, $name = null) { if (! is_null($name) && is_string($users)) { $users = new Address($users, $name); } return (new PendingMail($this))->to($users); } /** * Begin the process of mailing a mailable class instance. * * @param mixed $users * @param string|null $name * @return \Illuminate\Mail\PendingMail */ public function cc($users, $name = null) { if (! is_null($name) && is_string($users)) { $users = new Address($users, $name); } return (new PendingMail($this))->cc($users); } /** * Begin the process of mailing a mailable class instance. * * @param mixed $users * @param string|null $name * @return \Illuminate\Mail\PendingMail */ public function bcc($users, $name = null) { if (! is_null($name) && is_string($users)) { $users = new Address($users, $name); } return (new PendingMail($this))->bcc($users); } /** * Send a new message with only an HTML part. * * @param string $html * @param mixed $callback * @return \Illuminate\Mail\SentMessage|null */ public function html($html, $callback) { return $this->send(['html' => new HtmlString($html)], [], $callback); } /** * Send a new message with only a raw text part. * * @param string $text * @param mixed $callback * @return \Illuminate\Mail\SentMessage|null */ public function raw($text, $callback) { return $this->send(['raw' => $text], [], $callback); } /** * Send a new message with only a plain part. * * @param string $view * @param array $data * @param mixed $callback * @return \Illuminate\Mail\SentMessage|null */ public function plain($view, array $data, $callback) { return $this->send(['text' => $view], $data, $callback); } /** * Render the given message as a view. * * @param string|array $view * @param array $data * @return string */ public function render($view, array $data = []) { // First we need to parse the view, which could either be a string or an array // containing both an HTML and plain text versions of the view which should // be used when sending an e-mail. We will extract both of them out here. [$view, $plain, $raw] = $this->parseView($view); $data['message'] = $this->createMessage(); return $this->replaceEmbeddedAttachments( $this->renderView($view ?: $plain, $data), $data['message']->getSymfonyMessage()->getAttachments() ); } /** * Replace the embedded image attachments with raw, inline image data for browser rendering. * * @param string $renderedView * @param array $attachments * @return string */ protected function replaceEmbeddedAttachments(string $renderedView, array $attachments) { if (preg_match_all('/<img.+?src=[\'"]cid:([^\'"]+)[\'"].*?>/i', $renderedView, $matches)) { foreach (array_unique($matches[1]) as $image) { foreach ($attachments as $attachment) { if ($attachment->getFilename() === $image) { $renderedView = str_replace( 'cid:'.$image, 'data:'.$attachment->getContentType().';base64,'.$attachment->bodyToString(), $renderedView ); break; } } } } return $renderedView; } /** * Send a new message using a view. * * @param \Illuminate\Contracts\Mail\Mailable|string|array $view * @param array $data * @param \Closure|string|null $callback * @return \Illuminate\Mail\SentMessage|null */ public function send($view, array $data = [], $callback = null) { if ($view instanceof MailableContract) { return $this->sendMailable($view); } $data['mailer'] = $this->name; // First we need to parse the view, which could either be a string or an array // containing both an HTML and plain text versions of the view which should // be used when sending an e-mail. We will extract both of them out here. [$view, $plain, $raw] = $this->parseView($view); $data['message'] = $message = $this->createMessage(); // Once we have retrieved the view content for the e-mail we will set the body // of this message using the HTML type, which will provide a simple wrapper // to creating view based emails that are able to receive arrays of data. if (! is_null($callback)) { $callback($message); } $this->addContent($message, $view, $plain, $raw, $data); // If a global "to" address has been set, we will set that address on the mail // message. This is primarily useful during local development in which each // message should be delivered into a single mail address for inspection. if (isset($this->to['address'])) { $this->setGlobalToAndRemoveCcAndBcc($message); } // Next we will determine if the message should be sent. We give the developer // one final chance to stop this message and then we will send it to all of // its recipients. We will then fire the sent event for the sent message. $symfonyMessage = $message->getSymfonyMessage(); if ($this->shouldSendMessage($symfonyMessage, $data)) { $symfonySentMessage = $this->sendSymfonyMessage($symfonyMessage); if ($symfonySentMessage) { $sentMessage = new SentMessage($symfonySentMessage); $this->dispatchSentEvent($sentMessage, $data); return $sentMessage; } } } /** * Send the given mailable. * * @param \Illuminate\Contracts\Mail\Mailable $mailable * @return \Illuminate\Mail\SentMessage|null */ protected function sendMailable(MailableContract $mailable) { return $mailable instanceof ShouldQueue ? $mailable->mailer($this->name)->queue($this->queue) : $mailable->mailer($this->name)->send($this); } /** * Parse the given view name or array. * * @param \Closure|array|string $view * @return array * * @throws \InvalidArgumentException */ protected function parseView($view) { if (is_string($view) || $view instanceof Closure) { return [$view, null, null]; } // If the given view is an array with numeric keys, we will just assume that // both a "pretty" and "plain" view were provided, so we will return this // array as is, since it should contain both views with numerical keys. if (is_array($view) && isset($view[0])) { return [$view[0], $view[1], null]; } // If this view is an array but doesn't contain numeric keys, we will assume // the views are being explicitly specified and will extract them via the // named keys instead, allowing the developers to use one or the other. if (is_array($view)) { return [ $view['html'] ?? null, $view['text'] ?? null, $view['raw'] ?? null, ]; } throw new InvalidArgumentException('Invalid view.'); } /** * Add the content to a given message. * * @param \Illuminate\Mail\Message $message * @param string $view * @param string $plain * @param string $raw * @param array $data * @return void */ protected function addContent($message, $view, $plain, $raw, $data) { if (isset($view)) { $message->html($this->renderView($view, $data) ?: ' '); } if (isset($plain)) { $message->text($this->renderView($plain, $data) ?: ' '); } if (isset($raw)) { $message->text($raw); } } /** * Render the given view. * * @param \Closure|string $view * @param array $data * @return string */ protected function renderView($view, $data) { $view = value($view, $data); return $view instanceof Htmlable ? $view->toHtml() : $this->views->make($view, $data)->render(); } /** * Set the global "to" address on the given message. * * @param \Illuminate\Mail\Message $message * @return void */ protected function setGlobalToAndRemoveCcAndBcc($message) { $message->forgetTo(); $message->to($this->to['address'], $this->to['name'], true); $message->forgetCc(); $message->forgetBcc(); } /** * Queue a new e-mail message for sending. * * @param \Illuminate\Contracts\Mail\Mailable|string|array $view * @param string|null $queue * @return mixed * * @throws \InvalidArgumentException */ public function queue($view, $queue = null) { if (! $view instanceof MailableContract) { throw new InvalidArgumentException('Only mailables may be queued.'); } if (is_string($queue)) { $view->onQueue($queue); } return $view->mailer($this->name)->queue($this->queue); } /** * Queue a new e-mail message for sending on the given queue. * * @param string $queue * @param \Illuminate\Contracts\Mail\Mailable $view * @return mixed */ public function onQueue($queue, $view) { return $this->queue($view, $queue); } /** * Queue a new e-mail message for sending on the given queue. * * This method didn't match rest of framework's "onQueue" phrasing. Added "onQueue". * * @param string $queue * @param \Illuminate\Contracts\Mail\Mailable $view * @return mixed */ public function queueOn($queue, $view) { return $this->onQueue($queue, $view); } /** * Queue a new e-mail message for sending after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param \Illuminate\Contracts\Mail\Mailable $view * @param string|null $queue * @return mixed * * @throws \InvalidArgumentException */ public function later($delay, $view, $queue = null) { if (! $view instanceof MailableContract) { throw new InvalidArgumentException('Only mailables may be queued.'); } return $view->mailer($this->name)->later( $delay, is_null($queue) ? $this->queue : $queue ); } /** * Queue a new e-mail message for sending after (n) seconds on the given queue. * * @param string $queue * @param \DateTimeInterface|\DateInterval|int $delay * @param \Illuminate\Contracts\Mail\Mailable $view * @return mixed */ public function laterOn($queue, $delay, $view) { return $this->later($delay, $view, $queue); } /** * Create a new message instance. * * @return \Illuminate\Mail\Message */ protected function createMessage() { $message = new Message(new Email()); // If a global from address has been specified we will set it on every message // instance so the developer does not have to repeat themselves every time // they create a new message. We'll just go ahead and push this address. if (! empty($this->from['address'])) { $message->from($this->from['address'], $this->from['name']); } // When a global reply address was specified we will set this on every message // instance so the developer does not have to repeat themselves every time // they create a new message. We will just go ahead and push this address. if (! empty($this->replyTo['address'])) { $message->replyTo($this->replyTo['address'], $this->replyTo['name']); } if (! empty($this->returnPath['address'])) { $message->returnPath($this->returnPath['address']); } return $message; } /** * Send a Symfony Email instance. * * @param \Symfony\Component\Mime\Email $message * @return \Symfony\Component\Mailer\SentMessage|null */ protected function sendSymfonyMessage(Email $message) { try { return $this->transport->send($message, Envelope::create($message)); } finally { // } } /** * Determines if the email can be sent. * * @param \Symfony\Component\Mime\Email $message * @param array $data * @return bool */ protected function shouldSendMessage($message, $data = []) { if (! $this->events) { return true; } return $this->events->until( new MessageSending($message, $data) ) !== false; } /** * Dispatch the message sent event. * * @param \Illuminate\Mail\SentMessage $message * @param array $data * @return void */ protected function dispatchSentEvent($message, $data = []) { if ($this->events) { $this->events->dispatch( new MessageSent($message, $data) ); } } /** * Get the Symfony Transport instance. * * @return \Symfony\Component\Mailer\Transport\TransportInterface */ public function getSymfonyTransport() { return $this->transport; } /** * Get the view factory instance. * * @return \Illuminate\Contracts\View\Factory */ public function getViewFactory() { return $this->views; } /** * Set the Symfony Transport instance. * * @param \Symfony\Component\Mailer\Transport\TransportInterface $transport * @return void */ public function setSymfonyTransport(TransportInterface $transport) { $this->transport = $transport; } /** * Set the queue manager instance. * * @param \Illuminate\Contracts\Queue\Factory $queue * @return $this */ public function setQueue(QueueContract $queue) { $this->queue = $queue; return $this; } } src/Illuminate/Mail/TextMessage.php 0000644 00000002612 15107327037 0013270 0 ustar 00 <?php namespace Illuminate\Mail; use Illuminate\Support\Traits\ForwardsCalls; /** * @mixin \Illuminate\Mail\Message */ class TextMessage { use ForwardsCalls; /** * The underlying message instance. * * @var \Illuminate\Mail\Message */ protected $message; /** * Create a new text message instance. * * @param \Illuminate\Mail\Message $message * @return void */ public function __construct($message) { $this->message = $message; } /** * Embed a file in the message and get the CID. * * @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file * @return string */ public function embed($file) { return ''; } /** * Embed in-memory data in the message and get the CID. * * @param string|resource $data * @param string $name * @param string|null $contentType * @return string */ public function embedData($data, $name, $contentType = null) { return ''; } /** * Dynamically pass missing methods to the underlying message instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->forwardDecoratedCallTo($this->message, $method, $parameters); } } src/Illuminate/Mail/resources/views/html/panel.blade.php 0000644 00000000465 15107327037 0017323 0 ustar 00 <table class="panel" width="100%" cellpadding="0" cellspacing="0" role="presentation"> <tr> <td class="panel-content"> <table width="100%" cellpadding="0" cellspacing="0" role="presentation"> <tr> <td class="panel-item"> {{ Illuminate\Mail\Markdown::parse($slot) }} </td> </tr> </table> </td> </tr> </table> src/Illuminate/Mail/resources/views/html/layout.blade.php 0000644 00000002525 15107327037 0017540 0 ustar 00 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>{{ config('app.name') }}</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="color-scheme" content="light"> <meta name="supported-color-schemes" content="light"> <style> @media only screen and (max-width: 600px) { .inner-body { width: 100% !important; } .footer { width: 100% !important; } } @media only screen and (max-width: 500px) { .button { width: 100% !important; } } </style> </head> <body> <table class="wrapper" width="100%" cellpadding="0" cellspacing="0" role="presentation"> <tr> <td align="center"> <table class="content" width="100%" cellpadding="0" cellspacing="0" role="presentation"> {{ $header ?? '' }} <!-- Email Body --> <tr> <td class="body" width="100%" cellpadding="0" cellspacing="0" style="border: hidden !important;"> <table class="inner-body" align="center" width="570" cellpadding="0" cellspacing="0" role="presentation"> <!-- Body content --> <tr> <td class="content-cell"> {{ Illuminate\Mail\Markdown::parse($slot) }} {{ $subcopy ?? '' }} </td> </tr> </table> </td> </tr> {{ $footer ?? '' }} </table> </td> </tr> </table> </body> </html> src/Illuminate/Mail/resources/views/html/table.blade.php 0000644 00000000110 15107327037 0017276 0 ustar 00 <div class="table"> {{ Illuminate\Mail\Markdown::parse($slot) }} </div> src/Illuminate/Mail/resources/views/html/header.blade.php 0000644 00000000402 15107327037 0017443 0 ustar 00 @props(['url']) <tr> <td class="header"> <a href="{{ $url }}" style="display: inline-block;"> @if (trim($slot) === 'Laravel') <img src="https://laravel.com/img/notification-logo.png" class="logo" alt="Laravel Logo"> @else {{ $slot }} @endif </a> </td> </tr> src/Illuminate/Mail/resources/views/html/button.blade.php 0000644 00000001100 15107327037 0017522 0 ustar 00 @props([ 'url', 'color' => 'primary', 'align' => 'center', ]) <table class="action" align="{{ $align }}" width="100%" cellpadding="0" cellspacing="0" role="presentation"> <tr> <td align="{{ $align }}"> <table width="100%" border="0" cellpadding="0" cellspacing="0" role="presentation"> <tr> <td align="{{ $align }}"> <table border="0" cellpadding="0" cellspacing="0" role="presentation"> <tr> <td> <a href="{{ $url }}" class="button button-{{ $color }}" target="_blank" rel="noopener">{{ $slot }}</a> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> src/Illuminate/Mail/resources/views/html/footer.blade.php 0000644 00000000354 15107327037 0017517 0 ustar 00 <tr> <td> <table class="footer" align="center" width="570" cellpadding="0" cellspacing="0" role="presentation"> <tr> <td class="content-cell" align="center"> {{ Illuminate\Mail\Markdown::parse($slot) }} </td> </tr> </table> </td> </tr> src/Illuminate/Mail/resources/views/html/message.blade.php 0000644 00000000754 15107327037 0017651 0 ustar 00 <x-mail::layout> {{-- Header --}} <x-slot:header> <x-mail::header :url="config('app.url')"> {{ config('app.name') }} </x-mail::header> </x-slot:header> {{-- Body --}} {{ $slot }} {{-- Subcopy --}} @isset($subcopy) <x-slot:subcopy> <x-mail::subcopy> {{ $subcopy }} </x-mail::subcopy> </x-slot:subcopy> @endisset {{-- Footer --}} <x-slot:footer> <x-mail::footer> © {{ date('Y') }} {{ config('app.name') }}. @lang('All rights reserved.') </x-mail::footer> </x-slot:footer> </x-mail::layout> src/Illuminate/Mail/resources/views/html/themes/default.css 0000644 00000011013 15107327037 0020057 0 ustar 00 /* Base */ body, body *:not(html):not(style):not(br):not(tr):not(code) { box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; position: relative; } body { -webkit-text-size-adjust: none; background-color: #ffffff; color: #718096; height: 100%; line-height: 1.4; margin: 0; padding: 0; width: 100% !important; } p, ul, ol, blockquote { line-height: 1.4; text-align: left; } a { color: #3869d4; } a img { border: none; } /* Typography */ h1 { color: #3d4852; font-size: 18px; font-weight: bold; margin-top: 0; text-align: left; } h2 { font-size: 16px; font-weight: bold; margin-top: 0; text-align: left; } h3 { font-size: 14px; font-weight: bold; margin-top: 0; text-align: left; } p { font-size: 16px; line-height: 1.5em; margin-top: 0; text-align: left; } p.sub { font-size: 12px; } img { max-width: 100%; } /* Layout */ .wrapper { -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 100%; background-color: #edf2f7; margin: 0; padding: 0; width: 100%; } .content { -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 100%; margin: 0; padding: 0; width: 100%; } /* Header */ .header { padding: 25px 0; text-align: center; } .header a { color: #3d4852; font-size: 19px; font-weight: bold; text-decoration: none; } /* Logo */ .logo { height: 75px; max-height: 75px; width: 75px; } /* Body */ .body { -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 100%; background-color: #edf2f7; border-bottom: 1px solid #edf2f7; border-top: 1px solid #edf2f7; margin: 0; padding: 0; width: 100%; } .inner-body { -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 570px; background-color: #ffffff; border-color: #e8e5ef; border-radius: 2px; border-width: 1px; box-shadow: 0 2px 0 rgba(0, 0, 150, 0.025), 2px 4px 0 rgba(0, 0, 150, 0.015); margin: 0 auto; padding: 0; width: 570px; } /* Subcopy */ .subcopy { border-top: 1px solid #e8e5ef; margin-top: 25px; padding-top: 25px; } .subcopy p { font-size: 14px; } /* Footer */ .footer { -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 570px; margin: 0 auto; padding: 0; text-align: center; width: 570px; } .footer p { color: #b0adc5; font-size: 12px; text-align: center; } .footer a { color: #b0adc5; text-decoration: underline; } /* Tables */ .table table { -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 100%; margin: 30px auto; width: 100%; } .table th { border-bottom: 1px solid #edeff2; margin: 0; padding-bottom: 8px; } .table td { color: #74787e; font-size: 15px; line-height: 18px; margin: 0; padding: 10px 0; } .content-cell { max-width: 100vw; padding: 32px; } /* Buttons */ .action { -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 100%; margin: 30px auto; padding: 0; text-align: center; width: 100%; } .button { -webkit-text-size-adjust: none; border-radius: 4px; color: #fff; display: inline-block; overflow: hidden; text-decoration: none; } .button-blue, .button-primary { background-color: #2d3748; border-bottom: 8px solid #2d3748; border-left: 18px solid #2d3748; border-right: 18px solid #2d3748; border-top: 8px solid #2d3748; } .button-green, .button-success { background-color: #48bb78; border-bottom: 8px solid #48bb78; border-left: 18px solid #48bb78; border-right: 18px solid #48bb78; border-top: 8px solid #48bb78; } .button-red, .button-error { background-color: #e53e3e; border-bottom: 8px solid #e53e3e; border-left: 18px solid #e53e3e; border-right: 18px solid #e53e3e; border-top: 8px solid #e53e3e; } /* Panels */ .panel { border-left: #2d3748 solid 4px; margin: 21px 0; } .panel-content { background-color: #edf2f7; color: #718096; padding: 16px; } .panel-content p { color: #718096; } .panel-item { padding: 0; } .panel-item p:last-of-type { margin-bottom: 0; padding-bottom: 0; } /* Utilities */ .break-all { word-break: break-all; } src/Illuminate/Mail/resources/views/html/subcopy.blade.php 0000644 00000000245 15107327037 0017704 0 ustar 00 <table class="subcopy" width="100%" cellpadding="0" cellspacing="0" role="presentation"> <tr> <td> {{ Illuminate\Mail\Markdown::parse($slot) }} </td> </tr> </table> src/Illuminate/Mail/resources/views/text/panel.blade.php 0000644 00000000014 15107327037 0017331 0 ustar 00 {{ $slot }} src/Illuminate/Mail/resources/views/text/layout.blade.php 0000644 00000000231 15107327037 0017550 0 ustar 00 {!! strip_tags($header ?? '') !!} {!! strip_tags($slot) !!} @isset($subcopy) {!! strip_tags($subcopy) !!} @endisset {!! strip_tags($footer ?? '') !!} src/Illuminate/Mail/resources/views/text/table.blade.php 0000644 00000000014 15107327037 0017321 0 ustar 00 {{ $slot }} src/Illuminate/Mail/resources/views/text/header.blade.php 0000644 00000000032 15107327037 0017462 0 ustar 00 [{{ $slot }}]({{ $url }}) src/Illuminate/Mail/resources/views/text/button.blade.php 0000644 00000000030 15107327037 0017543 0 ustar 00 {{ $slot }}: {{ $url }} src/Illuminate/Mail/resources/views/text/footer.blade.php 0000644 00000000014 15107327037 0017530 0 ustar 00 {{ $slot }} src/Illuminate/Mail/resources/views/text/message.blade.php 0000644 00000001210 15107327037 0017655 0 ustar 00 <x-mail::layout> {{-- Header --}} <x-slot:header> <x-mail::header :url="config('app.url')"> {{ config('app.name') }} </x-mail::header> </x-slot:header> {{-- Body --}} {{ $slot }} {{-- Subcopy --}} @isset($subcopy) <x-slot:subcopy> <x-mail::subcopy> {{ $subcopy }} </x-mail::subcopy> </x-slot:subcopy> @endisset {{-- Footer --}} <x-slot:footer> <x-mail::footer> © {{ date('Y') }} {{ config('app.name') }}. @lang('All rights reserved.') </x-mail::footer> </x-slot:footer> </x-mail::layout> src/Illuminate/Mail/resources/views/text/subcopy.blade.php 0000644 00000000014 15107327037 0017716 0 ustar 00 {{ $slot }} src/Illuminate/Mail/composer.json 0000644 00000002733 15107327037 0013054 0 ustar 00 { "name": "illuminate/mail", "description": "The Illuminate Mail package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/collections": "^10.0", "illuminate/container": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0", "illuminate/support": "^10.0", "league/commonmark": "^2.2", "psr/log": "^1.0|^2.0|^3.0", "symfony/mailer": "^6.2", "tijsverkoyen/css-to-inline-styles": "^2.2.5" }, "autoload": { "psr-4": { "Illuminate\\Mail\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "aws/aws-sdk-php": "Required to use the SES mail driver (^3.235.5).", "symfony/http-client": "Required to use the Symfony API mail transports (^6.2).", "symfony/mailgun-mailer": "Required to enable support for the Mailgun mail transport (^6.2).", "symfony/postmark-mailer": "Required to enable support for the Postmark mail transport (^6.2)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Mail/Events/MessageSending.php 0000644 00000001145 15107327037 0015177 0 ustar 00 <?php namespace Illuminate\Mail\Events; use Symfony\Component\Mime\Email; class MessageSending { /** * The Symfony Email instance. * * @var \Symfony\Component\Mime\Email */ public $message; /** * The message data. * * @var array */ public $data; /** * Create a new event instance. * * @param \Symfony\Component\Mime\Email $message * @param array $data * @return void */ public function __construct(Email $message, array $data = []) { $this->data = $data; $this->message = $message; } } src/Illuminate/Mail/Events/MessageSent.php 0000644 00000003527 15107327037 0014527 0 ustar 00 <?php namespace Illuminate\Mail\Events; use Exception; use Illuminate\Mail\SentMessage; /** * @property \Symfony\Component\Mime\Email $message */ class MessageSent { /** * The message that was sent. * * @var \Illuminate\Mail\SentMessage */ public $sent; /** * The message data. * * @var array */ public $data; /** * Create a new event instance. * * @param \Illuminate\Mail\SentMessage $message * @param array $data * @return void */ public function __construct(SentMessage $message, array $data = []) { $this->sent = $message; $this->data = $data; } /** * Get the serializable representation of the object. * * @return array */ public function __serialize() { $hasAttachments = collect($this->message->getAttachments())->isNotEmpty(); return [ 'sent' => $this->sent, 'data' => $hasAttachments ? base64_encode(serialize($this->data)) : $this->data, 'hasAttachments' => $hasAttachments, ]; } /** * Marshal the object from its serialized data. * * @param array $data * @return void */ public function __unserialize(array $data) { $this->sent = $data['sent']; $this->data = (($data['hasAttachments'] ?? false) === true) ? unserialize(base64_decode($data['data'])) : $data['data']; } /** * Dynamically get the original message. * * @param string $key * @return mixed * * @throws \Exception */ public function __get($key) { if ($key === 'message') { return $this->sent->getOriginalMessage(); } throw new Exception('Unable to access undefined property on '.__CLASS__.': '.$key); } } src/Illuminate/Mail/Markdown.php 0000644 00000011071 15107327037 0012620 0 ustar 00 <?php namespace Illuminate\Mail; use Illuminate\Contracts\View\Factory as ViewFactory; use Illuminate\Support\HtmlString; use Illuminate\Support\Str; use League\CommonMark\Environment\Environment; use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; use League\CommonMark\Extension\Table\TableExtension; use League\CommonMark\MarkdownConverter; use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles; class Markdown { /** * The view factory implementation. * * @var \Illuminate\Contracts\View\Factory */ protected $view; /** * The current theme being used when generating emails. * * @var string */ protected $theme = 'default'; /** * The registered component paths. * * @var array */ protected $componentPaths = []; /** * Create a new Markdown renderer instance. * * @param \Illuminate\Contracts\View\Factory $view * @param array $options * @return void */ public function __construct(ViewFactory $view, array $options = []) { $this->view = $view; $this->theme = $options['theme'] ?? 'default'; $this->loadComponentsFrom($options['paths'] ?? []); } /** * Render the Markdown template into HTML. * * @param string $view * @param array $data * @param \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles|null $inliner * @return \Illuminate\Support\HtmlString */ public function render($view, array $data = [], $inliner = null) { $this->view->flushFinderCache(); $contents = $this->view->replaceNamespace( 'mail', $this->htmlComponentPaths() )->make($view, $data)->render(); if ($this->view->exists($customTheme = Str::start($this->theme, 'mail.'))) { $theme = $customTheme; } else { $theme = str_contains($this->theme, '::') ? $this->theme : 'mail::themes.'.$this->theme; } return new HtmlString(($inliner ?: new CssToInlineStyles)->convert( $contents, $this->view->make($theme, $data)->render() )); } /** * Render the Markdown template into text. * * @param string $view * @param array $data * @return \Illuminate\Support\HtmlString */ public function renderText($view, array $data = []) { $this->view->flushFinderCache(); $contents = $this->view->replaceNamespace( 'mail', $this->textComponentPaths() )->make($view, $data)->render(); return new HtmlString( html_entity_decode(preg_replace("/[\r\n]{2,}/", "\n\n", $contents), ENT_QUOTES, 'UTF-8') ); } /** * Parse the given Markdown text into HTML. * * @param string $text * @return \Illuminate\Support\HtmlString */ public static function parse($text) { $environment = new Environment([ 'allow_unsafe_links' => false, ]); $environment->addExtension(new CommonMarkCoreExtension); $environment->addExtension(new TableExtension); $converter = new MarkdownConverter($environment); return new HtmlString($converter->convert($text)->getContent()); } /** * Get the HTML component paths. * * @return array */ public function htmlComponentPaths() { return array_map(function ($path) { return $path.'/html'; }, $this->componentPaths()); } /** * Get the text component paths. * * @return array */ public function textComponentPaths() { return array_map(function ($path) { return $path.'/text'; }, $this->componentPaths()); } /** * Get the component paths. * * @return array */ protected function componentPaths() { return array_unique(array_merge($this->componentPaths, [ __DIR__.'/resources/views', ])); } /** * Register new mail component paths. * * @param array $paths * @return void */ public function loadComponentsFrom(array $paths = []) { $this->componentPaths = $paths; } /** * Set the default theme to be used. * * @param string $theme * @return $this */ public function theme($theme) { $this->theme = $theme; return $this; } /** * Get the theme currently being used by the renderer. * * @return string */ public function getTheme() { return $this->theme; } } src/Illuminate/Mail/Message.php 0000644 00000023563 15107327037 0012433 0 ustar 00 <?php namespace Illuminate\Mail; use Illuminate\Contracts\Mail\Attachable; use Illuminate\Support\Str; use Illuminate\Support\Traits\ForwardsCalls; use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Email; use Symfony\Component\Mime\Part\DataPart; use Symfony\Component\Mime\Part\File; /** * @mixin \Symfony\Component\Mime\Email */ class Message { use ForwardsCalls; /** * The Symfony Email instance. * * @var \Symfony\Component\Mime\Email */ protected $message; /** * CIDs of files embedded in the message. * * @deprecated Will be removed in a future Laravel version. * * @var array */ protected $embeddedFiles = []; /** * Create a new message instance. * * @param \Symfony\Component\Mime\Email $message * @return void */ public function __construct(Email $message) { $this->message = $message; } /** * Add a "from" address to the message. * * @param string|array $address * @param string|null $name * @return $this */ public function from($address, $name = null) { is_array($address) ? $this->message->from(...$address) : $this->message->from(new Address($address, (string) $name)); return $this; } /** * Set the "sender" of the message. * * @param string|array $address * @param string|null $name * @return $this */ public function sender($address, $name = null) { is_array($address) ? $this->message->sender(...$address) : $this->message->sender(new Address($address, (string) $name)); return $this; } /** * Set the "return path" of the message. * * @param string $address * @return $this */ public function returnPath($address) { $this->message->returnPath($address); return $this; } /** * Add a recipient to the message. * * @param string|array $address * @param string|null $name * @param bool $override * @return $this */ public function to($address, $name = null, $override = false) { if ($override) { is_array($address) ? $this->message->to(...$address) : $this->message->to(new Address($address, (string) $name)); return $this; } return $this->addAddresses($address, $name, 'To'); } /** * Remove all "to" addresses from the message. * * @return $this */ public function forgetTo() { if ($header = $this->message->getHeaders()->get('To')) { $this->addAddressDebugHeader('X-To', $this->message->getTo()); $header->setAddresses([]); } return $this; } /** * Add a carbon copy to the message. * * @param string|array $address * @param string|null $name * @param bool $override * @return $this */ public function cc($address, $name = null, $override = false) { if ($override) { is_array($address) ? $this->message->cc(...$address) : $this->message->cc(new Address($address, (string) $name)); return $this; } return $this->addAddresses($address, $name, 'Cc'); } /** * Remove all carbon copy addresses from the message. * * @return $this */ public function forgetCc() { if ($header = $this->message->getHeaders()->get('Cc')) { $this->addAddressDebugHeader('X-Cc', $this->message->getCC()); $header->setAddresses([]); } return $this; } /** * Add a blind carbon copy to the message. * * @param string|array $address * @param string|null $name * @param bool $override * @return $this */ public function bcc($address, $name = null, $override = false) { if ($override) { is_array($address) ? $this->message->bcc(...$address) : $this->message->bcc(new Address($address, (string) $name)); return $this; } return $this->addAddresses($address, $name, 'Bcc'); } /** * Remove all of the blind carbon copy addresses from the message. * * @return $this */ public function forgetBcc() { if ($header = $this->message->getHeaders()->get('Bcc')) { $this->addAddressDebugHeader('X-Bcc', $this->message->getBcc()); $header->setAddresses([]); } return $this; } /** * Add a "reply to" address to the message. * * @param string|array $address * @param string|null $name * @return $this */ public function replyTo($address, $name = null) { return $this->addAddresses($address, $name, 'ReplyTo'); } /** * Add a recipient to the message. * * @param string|array $address * @param string $name * @param string $type * @return $this */ protected function addAddresses($address, $name, $type) { if (is_array($address)) { $type = lcfirst($type); $addresses = collect($address)->map(function ($address, $key) { if (is_string($key) && is_string($address)) { return new Address($key, $address); } if (is_array($address)) { return new Address($address['email'] ?? $address['address'], $address['name'] ?? null); } if (is_null($address)) { return new Address($key); } return $address; })->all(); $this->message->{"{$type}"}(...$addresses); } else { $this->message->{"add{$type}"}(new Address($address, (string) $name)); } return $this; } /** * Add an address debug header for a list of recipients. * * @param string $header * @param \Symfony\Component\Mime\Address[] $addresses * @return $this */ protected function addAddressDebugHeader(string $header, array $addresses) { $this->message->getHeaders()->addTextHeader( $header, implode(', ', array_map(fn ($a) => $a->toString(), $addresses)), ); return $this; } /** * Set the subject of the message. * * @param string $subject * @return $this */ public function subject($subject) { $this->message->subject($subject); return $this; } /** * Set the message priority level. * * @param int $level * @return $this */ public function priority($level) { $this->message->priority($level); return $this; } /** * Attach a file to the message. * * @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file * @param array $options * @return $this */ public function attach($file, array $options = []) { if ($file instanceof Attachable) { $file = $file->toMailAttachment(); } if ($file instanceof Attachment) { return $file->attachTo($this); } $this->message->attachFromPath($file, $options['as'] ?? null, $options['mime'] ?? null); return $this; } /** * Attach in-memory data as an attachment. * * @param string|resource $data * @param string $name * @param array $options * @return $this */ public function attachData($data, $name, array $options = []) { $this->message->attach($data, $name, $options['mime'] ?? null); return $this; } /** * Embed a file in the message and get the CID. * * @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file * @return string */ public function embed($file) { if ($file instanceof Attachable) { $file = $file->toMailAttachment(); } if ($file instanceof Attachment) { return $file->attachWith( function ($path) use ($file) { $cid = $file->as ?? Str::random(); $this->message->addPart( (new DataPart(new File($path), $cid, $file->mime))->asInline() ); return "cid:{$cid}"; }, function ($data) use ($file) { $this->message->addPart( (new DataPart($data(), $file->as, $file->mime))->asInline() ); return "cid:{$file->as}"; } ); } $cid = Str::random(10); $this->message->addPart( (new DataPart(new File($file), $cid))->asInline() ); return "cid:$cid"; } /** * Embed in-memory data in the message and get the CID. * * @param string|resource $data * @param string $name * @param string|null $contentType * @return string */ public function embedData($data, $name, $contentType = null) { $this->message->addPart( (new DataPart($data, $name, $contentType))->asInline() ); return "cid:$name"; } /** * Get the underlying Symfony Email instance. * * @return \Symfony\Component\Mime\Email */ public function getSymfonyMessage() { return $this->message; } /** * Dynamically pass missing methods to the Symfony instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->forwardDecoratedCallTo($this->message, $method, $parameters); } } src/Illuminate/Mail/LICENSE.md 0000644 00000002063 15107327037 0011732 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Mail/MailServiceProvider.php 0000644 00000003345 15107327037 0014761 0 ustar 00 <?php namespace Illuminate\Mail; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; class MailServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Register the service provider. * * @return void */ public function register() { $this->registerIlluminateMailer(); $this->registerMarkdownRenderer(); } /** * Register the Illuminate mailer instance. * * @return void */ protected function registerIlluminateMailer() { $this->app->singleton('mail.manager', function ($app) { return new MailManager($app); }); $this->app->bind('mailer', function ($app) { return $app->make('mail.manager')->mailer(); }); } /** * Register the Markdown renderer instance. * * @return void */ protected function registerMarkdownRenderer() { if ($this->app->runningInConsole()) { $this->publishes([ __DIR__.'/resources/views' => $this->app->resourcePath('views/vendor/mail'), ], 'laravel-mail'); } $this->app->singleton(Markdown::class, function ($app) { $config = $app->make('config'); return new Markdown($app->make('view'), [ 'theme' => $config->get('mail.markdown.theme', 'default'), 'paths' => $config->get('mail.markdown.paths', []), ]); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return [ 'mail.manager', 'mailer', Markdown::class, ]; } } src/Illuminate/Mail/MailManager.php 0000644 00000040066 15107327037 0013221 0 ustar 00 <?php namespace Illuminate\Mail; use Aws\Ses\SesClient; use Aws\SesV2\SesV2Client; use Closure; use Illuminate\Contracts\Mail\Factory as FactoryContract; use Illuminate\Log\LogManager; use Illuminate\Mail\Transport\ArrayTransport; use Illuminate\Mail\Transport\LogTransport; use Illuminate\Mail\Transport\SesTransport; use Illuminate\Mail\Transport\SesV2Transport; use Illuminate\Support\Arr; use Illuminate\Support\ConfigurationUrlParser; use Illuminate\Support\Str; use InvalidArgumentException; use Psr\Log\LoggerInterface; use Symfony\Component\HttpClient\HttpClient; use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory; use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory; use Symfony\Component\Mailer\Transport\Dsn; use Symfony\Component\Mailer\Transport\FailoverTransport; use Symfony\Component\Mailer\Transport\SendmailTransport; use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport; use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory; use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream; /** * @mixin \Illuminate\Mail\Mailer */ class MailManager implements FactoryContract { /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The array of resolved mailers. * * @var array */ protected $mailers = []; /** * The registered custom driver creators. * * @var array */ protected $customCreators = []; /** * Create a new Mail manager instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function __construct($app) { $this->app = $app; } /** * Get a mailer instance by name. * * @param string|null $name * @return \Illuminate\Contracts\Mail\Mailer */ public function mailer($name = null) { $name = $name ?: $this->getDefaultDriver(); return $this->mailers[$name] = $this->get($name); } /** * Get a mailer driver instance. * * @param string|null $driver * @return \Illuminate\Mail\Mailer */ public function driver($driver = null) { return $this->mailer($driver); } /** * Attempt to get the mailer from the local cache. * * @param string $name * @return \Illuminate\Mail\Mailer */ protected function get($name) { return $this->mailers[$name] ?? $this->resolve($name); } /** * Resolve the given mailer. * * @param string $name * @return \Illuminate\Mail\Mailer * * @throws \InvalidArgumentException */ protected function resolve($name) { $config = $this->getConfig($name); if (is_null($config)) { throw new InvalidArgumentException("Mailer [{$name}] is not defined."); } // Once we have created the mailer instance we will set a container instance // on the mailer. This allows us to resolve mailer classes via containers // for maximum testability on said classes instead of passing Closures. $mailer = new Mailer( $name, $this->app['view'], $this->createSymfonyTransport($config), $this->app['events'] ); if ($this->app->bound('queue')) { $mailer->setQueue($this->app['queue']); } // Next we will set all of the global addresses on this mailer, which allows // for easy unification of all "from" addresses as well as easy debugging // of sent messages since these will be sent to a single email address. foreach (['from', 'reply_to', 'to', 'return_path'] as $type) { $this->setGlobalAddress($mailer, $config, $type); } return $mailer; } /** * Create a new transport instance. * * @param array $config * @return \Symfony\Component\Mailer\Transport\TransportInterface * * @throws \InvalidArgumentException */ public function createSymfonyTransport(array $config) { // Here we will check if the "transport" key exists and if it doesn't we will // assume an application is still using the legacy mail configuration file // format and use the "mail.driver" configuration option instead for BC. $transport = $config['transport'] ?? $this->app['config']['mail.driver']; if (isset($this->customCreators[$transport])) { return call_user_func($this->customCreators[$transport], $config); } if (trim($transport ?? '') === '' || ! method_exists($this, $method = 'create'.ucfirst(Str::camel($transport)).'Transport')) { throw new InvalidArgumentException("Unsupported mail transport [{$transport}]."); } return $this->{$method}($config); } /** * Create an instance of the Symfony SMTP Transport driver. * * @param array $config * @return \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport */ protected function createSmtpTransport(array $config) { $factory = new EsmtpTransportFactory; $scheme = $config['scheme'] ?? null; if (! $scheme) { $scheme = ! empty($config['encryption']) && $config['encryption'] === 'tls' ? (($config['port'] == 465) ? 'smtps' : 'smtp') : ''; } $transport = $factory->create(new Dsn( $scheme, $config['host'], $config['username'] ?? null, $config['password'] ?? null, $config['port'] ?? null, $config )); return $this->configureSmtpTransport($transport, $config); } /** * Configure the additional SMTP driver options. * * @param \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport $transport * @param array $config * @return \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport */ protected function configureSmtpTransport(EsmtpTransport $transport, array $config) { $stream = $transport->getStream(); if ($stream instanceof SocketStream) { if (isset($config['source_ip'])) { $stream->setSourceIp($config['source_ip']); } if (isset($config['timeout'])) { $stream->setTimeout($config['timeout']); } } return $transport; } /** * Create an instance of the Symfony Sendmail Transport driver. * * @param array $config * @return \Symfony\Component\Mailer\Transport\SendmailTransport */ protected function createSendmailTransport(array $config) { return new SendmailTransport( $config['path'] ?? $this->app['config']->get('mail.sendmail') ); } /** * Create an instance of the Symfony Amazon SES Transport driver. * * @param array $config * @return \Illuminate\Mail\Transport\SesTransport */ protected function createSesTransport(array $config) { $config = array_merge( $this->app['config']->get('services.ses', []), ['version' => 'latest', 'service' => 'email'], $config ); $config = Arr::except($config, ['transport']); return new SesTransport( new SesClient($this->addSesCredentials($config)), $config['options'] ?? [] ); } /** * Create an instance of the Symfony Amazon SES V2 Transport driver. * * @param array $config * @return \Illuminate\Mail\Transport\Se2VwTransport */ protected function createSesV2Transport(array $config) { $config = array_merge( $this->app['config']->get('services.ses', []), ['version' => 'latest'], $config ); $config = Arr::except($config, ['transport']); return new SesV2Transport( new SesV2Client($this->addSesCredentials($config)), $config['options'] ?? [] ); } /** * Add the SES credentials to the configuration array. * * @param array $config * @return array */ protected function addSesCredentials(array $config) { if (! empty($config['key']) && ! empty($config['secret'])) { $config['credentials'] = Arr::only($config, ['key', 'secret', 'token']); } return Arr::except($config, ['token']); } /** * Create an instance of the Symfony Mail Transport driver. * * @return \Symfony\Component\Mailer\Transport\SendmailTransport */ protected function createMailTransport() { return new SendmailTransport; } /** * Create an instance of the Symfony Mailgun Transport driver. * * @param array $config * @return \Symfony\Component\Mailer\Transport\TransportInterface */ protected function createMailgunTransport(array $config) { $factory = new MailgunTransportFactory(null, $this->getHttpClient($config)); if (! isset($config['secret'])) { $config = $this->app['config']->get('services.mailgun', []); } return $factory->create(new Dsn( 'mailgun+'.($config['scheme'] ?? 'https'), $config['endpoint'] ?? 'default', $config['secret'], $config['domain'] )); } /** * Create an instance of the Symfony Postmark Transport driver. * * @param array $config * @return \Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkApiTransport */ protected function createPostmarkTransport(array $config) { $factory = new PostmarkTransportFactory(null, $this->getHttpClient($config)); $options = isset($config['message_stream_id']) ? ['message_stream' => $config['message_stream_id']] : []; return $factory->create(new Dsn( 'postmark+api', 'default', $config['token'] ?? $this->app['config']->get('services.postmark.token'), null, null, $options )); } /** * Create an instance of the Symfony Failover Transport driver. * * @param array $config * @return \Symfony\Component\Mailer\Transport\FailoverTransport */ protected function createFailoverTransport(array $config) { $transports = []; foreach ($config['mailers'] as $name) { $config = $this->getConfig($name); if (is_null($config)) { throw new InvalidArgumentException("Mailer [{$name}] is not defined."); } // Now, we will check if the "driver" key exists and if it does we will set // the transport configuration parameter in order to offer compatibility // with any Laravel <= 6.x application style mail configuration files. $transports[] = $this->app['config']['mail.driver'] ? $this->createSymfonyTransport(array_merge($config, ['transport' => $name])) : $this->createSymfonyTransport($config); } return new FailoverTransport($transports); } /** * Create an instance of the Log Transport driver. * * @param array $config * @return \Illuminate\Mail\Transport\LogTransport */ protected function createLogTransport(array $config) { $logger = $this->app->make(LoggerInterface::class); if ($logger instanceof LogManager) { $logger = $logger->channel( $config['channel'] ?? $this->app['config']->get('mail.log_channel') ); } return new LogTransport($logger); } /** * Create an instance of the Array Transport Driver. * * @return \Illuminate\Mail\Transport\ArrayTransport */ protected function createArrayTransport() { return new ArrayTransport; } /** * Get a configured Symfony HTTP client instance. * * @return \Symfony\Contracts\HttpClient\HttpClientInterface|null */ protected function getHttpClient(array $config) { if ($options = ($config['client'] ?? false)) { $maxHostConnections = Arr::pull($options, 'max_host_connections', 6); $maxPendingPushes = Arr::pull($options, 'max_pending_pushes', 50); return HttpClient::create($options, $maxHostConnections, $maxPendingPushes); } } /** * Set a global address on the mailer by type. * * @param \Illuminate\Mail\Mailer $mailer * @param array $config * @param string $type * @return void */ protected function setGlobalAddress($mailer, array $config, string $type) { $address = Arr::get($config, $type, $this->app['config']['mail.'.$type]); if (is_array($address) && isset($address['address'])) { $mailer->{'always'.Str::studly($type)}($address['address'], $address['name']); } } /** * Get the mail connection configuration. * * @param string $name * @return array */ protected function getConfig(string $name) { // Here we will check if the "driver" key exists and if it does we will use // the entire mail configuration file as the "driver" config in order to // provide "BC" for any Laravel <= 6.x style mail configuration files. $config = $this->app['config']['mail.driver'] ? $this->app['config']['mail'] : $this->app['config']["mail.mailers.{$name}"]; if (isset($config['url'])) { $config = array_merge($config, (new ConfigurationUrlParser)->parseConfiguration($config)); $config['transport'] = Arr::pull($config, 'driver'); } return $config; } /** * Get the default mail driver name. * * @return string */ public function getDefaultDriver() { // Here we will check if the "driver" key exists and if it does we will use // that as the default driver in order to provide support for old styles // of the Laravel mail configuration file for backwards compatibility. return $this->app['config']['mail.driver'] ?? $this->app['config']['mail.default']; } /** * Set the default mail driver name. * * @param string $name * @return void */ public function setDefaultDriver(string $name) { if ($this->app['config']['mail.driver']) { $this->app['config']['mail.driver'] = $name; } $this->app['config']['mail.default'] = $name; } /** * Disconnect the given mailer and remove from local cache. * * @param string|null $name * @return void */ public function purge($name = null) { $name = $name ?: $this->getDefaultDriver(); unset($this->mailers[$name]); } /** * Register a custom transport creator Closure. * * @param string $driver * @param \Closure $callback * @return $this */ public function extend($driver, Closure $callback) { $this->customCreators[$driver] = $callback; return $this; } /** * Get the application instance used by the manager. * * @return \Illuminate\Contracts\Foundation\Application */ public function getApplication() { return $this->app; } /** * Set the application instance used by the manager. * * @param \Illuminate\Contracts\Foundation\Application $app * @return $this */ public function setApplication($app) { $this->app = $app; return $this; } /** * Forget all of the resolved mailer instances. * * @return $this */ public function forgetMailers() { $this->mailers = []; return $this; } /** * Dynamically call the default driver instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->mailer()->$method(...$parameters); } } src/Illuminate/Mail/Attachment.php 0000644 00000011651 15107327037 0013132 0 ustar 00 <?php namespace Illuminate\Mail; use Closure; use Illuminate\Container\Container; use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory; use Illuminate\Support\Traits\Macroable; use RuntimeException; class Attachment { use Macroable; /** * The attached file's filename. * * @var string|null */ public $as; /** * The attached file's mime type. * * @var string|null */ public $mime; /** * A callback that attaches the attachment to the mail message. * * @var \Closure */ protected $resolver; /** * Create a mail attachment. * * @param \Closure $resolver * @return void */ private function __construct(Closure $resolver) { $this->resolver = $resolver; } /** * Create a mail attachment from a path. * * @param string $path * @return static */ public static function fromPath($path) { return new static(fn ($attachment, $pathStrategy) => $pathStrategy($path, $attachment)); } /** * Create a mail attachment from in-memory data. * * @param \Closure $data * @param string|null $name * @return static */ public static function fromData(Closure $data, $name = null) { return (new static( fn ($attachment, $pathStrategy, $dataStrategy) => $dataStrategy($data, $attachment) ))->as($name); } /** * Create a mail attachment from a file in the default storage disk. * * @param string $path * @return static */ public static function fromStorage($path) { return static::fromStorageDisk(null, $path); } /** * Create a mail attachment from a file in the specified storage disk. * * @param string|null $disk * @param string $path * @return static */ public static function fromStorageDisk($disk, $path) { return new static(function ($attachment, $pathStrategy, $dataStrategy) use ($disk, $path) { $storage = Container::getInstance()->make( FilesystemFactory::class )->disk($disk); $attachment ->as($attachment->as ?? basename($path)) ->withMime($attachment->mime ?? $storage->mimeType($path)); return $dataStrategy(fn () => $storage->get($path), $attachment); }); } /** * Set the attached file's filename. * * @param string|null $name * @return $this */ public function as($name) { $this->as = $name; return $this; } /** * Set the attached file's mime type. * * @param string $mime * @return $this */ public function withMime($mime) { $this->mime = $mime; return $this; } /** * Attach the attachment with the given strategies. * * @param \Closure $pathStrategy * @param \Closure $dataStrategy * @return mixed */ public function attachWith(Closure $pathStrategy, Closure $dataStrategy) { return ($this->resolver)($this, $pathStrategy, $dataStrategy); } /** * Attach the attachment to a built-in mail type. * * @param \Illuminate\Mail\Mailable|\Illuminate\Mail\Message|\Illuminate\Notifications\Messages\MailMessage $mail * @param array $options * @return mixed */ public function attachTo($mail, $options = []) { return $this->attachWith( fn ($path) => $mail->attach($path, [ 'as' => $options['as'] ?? $this->as, 'mime' => $options['mime'] ?? $this->mime, ]), function ($data) use ($mail, $options) { $options = [ 'as' => $options['as'] ?? $this->as, 'mime' => $options['mime'] ?? $this->mime, ]; if ($options['as'] === null) { throw new RuntimeException('Attachment requires a filename to be specified.'); } return $mail->attachData($data(), $options['as'], ['mime' => $options['mime']]); } ); } /** * Determine if the given attachment is equivalent to this attachment. * * @param \Illuminate\Mail\Attachment $attachment * @param array $options * @return bool */ public function isEquivalent(Attachment $attachment, $options = []) { return with([ 'as' => $options['as'] ?? $attachment->as, 'mime' => $options['mime'] ?? $attachment->mime, ], fn ($options) => $this->attachWith( fn ($path) => [$path, ['as' => $this->as, 'mime' => $this->mime]], fn ($data) => [$data(), ['as' => $this->as, 'mime' => $this->mime]], ) === $attachment->attachWith( fn ($path) => [$path, $options], fn ($data) => [$data(), $options], )); } } src/Illuminate/Auth/EloquentUserProvider.php 0000644 00000014272 15107327037 0015231 0 ustar 00 <?php namespace Illuminate\Auth; use Closure; use Illuminate\Contracts\Auth\Authenticatable as UserContract; use Illuminate\Contracts\Auth\UserProvider; use Illuminate\Contracts\Hashing\Hasher as HasherContract; use Illuminate\Contracts\Support\Arrayable; class EloquentUserProvider implements UserProvider { /** * The hasher implementation. * * @var \Illuminate\Contracts\Hashing\Hasher */ protected $hasher; /** * The Eloquent user model. * * @var string */ protected $model; /** * The callback that may modify the user retrieval queries. * * @var (\Closure(\Illuminate\Database\Eloquent\Builder):mixed)|null */ protected $queryCallback; /** * Create a new database user provider. * * @param \Illuminate\Contracts\Hashing\Hasher $hasher * @param string $model * @return void */ public function __construct(HasherContract $hasher, $model) { $this->model = $model; $this->hasher = $hasher; } /** * Retrieve a user by their unique identifier. * * @param mixed $identifier * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveById($identifier) { $model = $this->createModel(); return $this->newModelQuery($model) ->where($model->getAuthIdentifierName(), $identifier) ->first(); } /** * Retrieve a user by their unique identifier and "remember me" token. * * @param mixed $identifier * @param string $token * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByToken($identifier, $token) { $model = $this->createModel(); $retrievedModel = $this->newModelQuery($model)->where( $model->getAuthIdentifierName(), $identifier )->first(); if (! $retrievedModel) { return; } $rememberToken = $retrievedModel->getRememberToken(); return $rememberToken && hash_equals($rememberToken, $token) ? $retrievedModel : null; } /** * Update the "remember me" token for the given user in storage. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param string $token * @return void */ public function updateRememberToken(UserContract $user, $token) { $user->setRememberToken($token); $timestamps = $user->timestamps; $user->timestamps = false; $user->save(); $user->timestamps = $timestamps; } /** * Retrieve a user by the given credentials. * * @param array $credentials * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByCredentials(array $credentials) { $credentials = array_filter( $credentials, fn ($key) => ! str_contains($key, 'password'), ARRAY_FILTER_USE_KEY ); if (empty($credentials)) { return; } // First we will add each credential element to the query as a where clause. // Then we can execute the query and, if we found a user, return it in a // Eloquent User "model" that will be utilized by the Guard instances. $query = $this->newModelQuery(); foreach ($credentials as $key => $value) { if (is_array($value) || $value instanceof Arrayable) { $query->whereIn($key, $value); } elseif ($value instanceof Closure) { $value($query); } else { $query->where($key, $value); } } return $query->first(); } /** * Validate a user against the given credentials. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param array $credentials * @return bool */ public function validateCredentials(UserContract $user, array $credentials) { if (is_null($plain = $credentials['password'])) { return false; } return $this->hasher->check($plain, $user->getAuthPassword()); } /** * Get a new query builder for the model instance. * * @param \Illuminate\Database\Eloquent\Model|null $model * @return \Illuminate\Database\Eloquent\Builder */ protected function newModelQuery($model = null) { $query = is_null($model) ? $this->createModel()->newQuery() : $model->newQuery(); with($query, $this->queryCallback); return $query; } /** * Create a new instance of the model. * * @return \Illuminate\Database\Eloquent\Model */ public function createModel() { $class = '\\'.ltrim($this->model, '\\'); return new $class; } /** * Gets the hasher implementation. * * @return \Illuminate\Contracts\Hashing\Hasher */ public function getHasher() { return $this->hasher; } /** * Sets the hasher implementation. * * @param \Illuminate\Contracts\Hashing\Hasher $hasher * @return $this */ public function setHasher(HasherContract $hasher) { $this->hasher = $hasher; return $this; } /** * Gets the name of the Eloquent user model. * * @return string */ public function getModel() { return $this->model; } /** * Sets the name of the Eloquent user model. * * @param string $model * @return $this */ public function setModel($model) { $this->model = $model; return $this; } /** * Get the callback that modifies the query before retrieving users. * * @return \Closure|null */ public function getQueryCallback() { return $this->queryCallback; } /** * Sets the callback to modify the query before retrieving users. * * @param (\Closure(\Illuminate\Database\Eloquent\Builder):mixed)|null $queryCallback * @return $this */ public function withQuery($queryCallback = null) { $this->queryCallback = $queryCallback; return $this; } } src/Illuminate/Auth/Console/ClearResetsCommand.php 0000644 00000001532 15107327037 0016173 0 ustar 00 <?php namespace Illuminate\Auth\Console; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'auth:clear-resets')] class ClearResetsCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'auth:clear-resets {name? : The name of the password broker}'; /** * The console command description. * * @var string */ protected $description = 'Flush expired password reset tokens'; /** * Execute the console command. * * @return void */ public function handle() { $this->laravel['auth.password']->broker($this->argument('name'))->getRepository()->deleteExpired(); $this->components->info('Expired reset tokens cleared successfully.'); } } src/Illuminate/Auth/Console/stubs/make/views/layouts/app.stub 0000644 00000006443 15107327037 0020346 0 ustar 00 <!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- CSRF Token --> <meta name="csrf-token" content="{{ csrf_token() }}"> <title>{{ config('app.name', 'Laravel') }}</title> <!-- Scripts --> <script src="{{ asset('js/app.js') }}" defer></script> <!-- Styles --> <link href="{{ asset('css/app.css') }}" rel="stylesheet"> </head> <body> <div id="app"> <nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm"> <div class="container"> <a class="navbar-brand" href="{{ url('/') }}"> {{ config('app.name', 'Laravel') }} </a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <!-- Left Side Of Navbar --> <ul class="navbar-nav mr-auto"> </ul> <!-- Right Side Of Navbar --> <ul class="navbar-nav ml-auto"> <!-- Authentication Links --> @guest <li class="nav-item"> <a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a> </li> @if (Route::has('register')) <li class="nav-item"> <a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a> </li> @endif @else <li class="nav-item dropdown"> <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre> {{ Auth::user()->name }} <span class="caret"></span> </a> <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown"> <a class="dropdown-item" href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();"> {{ __('Logout') }} </a> <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;"> @csrf </form> </div> </li> @endguest </ul> </div> </div> </nav> <main class="py-4"> @yield('content') </main> </div> </body> </html> src/Illuminate/Auth/RequestGuard.php 0000644 00000004112 15107327037 0013466 0 ustar 00 <?php namespace Illuminate\Auth; use Illuminate\Contracts\Auth\Guard; use Illuminate\Contracts\Auth\UserProvider; use Illuminate\Http\Request; use Illuminate\Support\Traits\Macroable; class RequestGuard implements Guard { use GuardHelpers, Macroable; /** * The guard callback. * * @var callable */ protected $callback; /** * The request instance. * * @var \Illuminate\Http\Request */ protected $request; /** * Create a new authentication guard. * * @param callable $callback * @param \Illuminate\Http\Request $request * @param \Illuminate\Contracts\Auth\UserProvider|null $provider * @return void */ public function __construct(callable $callback, Request $request, UserProvider $provider = null) { $this->request = $request; $this->callback = $callback; $this->provider = $provider; } /** * Get the currently authenticated user. * * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function user() { // If we've already retrieved the user for the current request we can just // return it back immediately. We do not want to fetch the user data on // every call to this method because that would be tremendously slow. if (! is_null($this->user)) { return $this->user; } return $this->user = call_user_func( $this->callback, $this->request, $this->getProvider() ); } /** * Validate a user's credentials. * * @param array $credentials * @return bool */ public function validate(array $credentials = []) { return ! is_null((new static( $this->callback, $credentials['request'], $this->getProvider() ))->user()); } /** * Set the current request instance. * * @param \Illuminate\Http\Request $request * @return $this */ public function setRequest(Request $request) { $this->request = $request; return $this; } } src/Illuminate/Auth/AuthenticationException.php 0000644 00000002112 15107327037 0015707 0 ustar 00 <?php namespace Illuminate\Auth; use Exception; class AuthenticationException extends Exception { /** * All of the guards that were checked. * * @var array */ protected $guards; /** * The path the user should be redirected to. * * @var string|null */ protected $redirectTo; /** * Create a new authentication exception. * * @param string $message * @param array $guards * @param string|null $redirectTo * @return void */ public function __construct($message = 'Unauthenticated.', array $guards = [], $redirectTo = null) { parent::__construct($message); $this->guards = $guards; $this->redirectTo = $redirectTo; } /** * Get the guards that were checked. * * @return array */ public function guards() { return $this->guards; } /** * Get the path the user should be redirected to. * * @return string|null */ public function redirectTo() { return $this->redirectTo; } } src/Illuminate/Auth/MustVerifyEmail.php 0000644 00000001723 15107327037 0014145 0 ustar 00 <?php namespace Illuminate\Auth; use Illuminate\Auth\Notifications\VerifyEmail; trait MustVerifyEmail { /** * Determine if the user has verified their email address. * * @return bool */ public function hasVerifiedEmail() { return ! is_null($this->email_verified_at); } /** * Mark the given user's email as verified. * * @return bool */ public function markEmailAsVerified() { return $this->forceFill([ 'email_verified_at' => $this->freshTimestamp(), ])->save(); } /** * Send the email verification notification. * * @return void */ public function sendEmailVerificationNotification() { $this->notify(new VerifyEmail); } /** * Get the email address that should be used for verification. * * @return string */ public function getEmailForVerification() { return $this->email; } } src/Illuminate/Auth/GuardHelpers.php 0000644 00000005051 15107327037 0013443 0 ustar 00 <?php namespace Illuminate\Auth; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\UserProvider; /** * These methods are typically the same across all guards. */ trait GuardHelpers { /** * The currently authenticated user. * * @var \Illuminate\Contracts\Auth\Authenticatable|null */ protected $user; /** * The user provider implementation. * * @var \Illuminate\Contracts\Auth\UserProvider */ protected $provider; /** * Determine if the current user is authenticated. If not, throw an exception. * * @return \Illuminate\Contracts\Auth\Authenticatable * * @throws \Illuminate\Auth\AuthenticationException */ public function authenticate() { if (! is_null($user = $this->user())) { return $user; } throw new AuthenticationException; } /** * Determine if the guard has a user instance. * * @return bool */ public function hasUser() { return ! is_null($this->user); } /** * Determine if the current user is authenticated. * * @return bool */ public function check() { return ! is_null($this->user()); } /** * Determine if the current user is a guest. * * @return bool */ public function guest() { return ! $this->check(); } /** * Get the ID for the currently authenticated user. * * @return int|string|null */ public function id() { if ($this->user()) { return $this->user()->getAuthIdentifier(); } } /** * Set the current user. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return $this */ public function setUser(AuthenticatableContract $user) { $this->user = $user; return $this; } /** * Forget the current user. * * @return $this */ public function forgetUser() { $this->user = null; return $this; } /** * Get the user provider used by the guard. * * @return \Illuminate\Contracts\Auth\UserProvider */ public function getProvider() { return $this->provider; } /** * Set the user provider used by the guard. * * @param \Illuminate\Contracts\Auth\UserProvider $provider * @return void */ public function setProvider(UserProvider $provider) { $this->provider = $provider; } } src/Illuminate/Auth/AuthManager.php 0000644 00000020404 15107327037 0013251 0 ustar 00 <?php namespace Illuminate\Auth; use Closure; use Illuminate\Contracts\Auth\Factory as FactoryContract; use InvalidArgumentException; /** * @mixin \Illuminate\Contracts\Auth\Guard * @mixin \Illuminate\Contracts\Auth\StatefulGuard */ class AuthManager implements FactoryContract { use CreatesUserProviders; /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The registered custom driver creators. * * @var array */ protected $customCreators = []; /** * The array of created "drivers". * * @var array */ protected $guards = []; /** * The user resolver shared by various services. * * Determines the default user for Gate, Request, and the Authenticatable contract. * * @var \Closure */ protected $userResolver; /** * Create a new Auth manager instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function __construct($app) { $this->app = $app; $this->userResolver = fn ($guard = null) => $this->guard($guard)->user(); } /** * Attempt to get the guard from the local cache. * * @param string|null $name * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard */ public function guard($name = null) { $name = $name ?: $this->getDefaultDriver(); return $this->guards[$name] ?? $this->guards[$name] = $this->resolve($name); } /** * Resolve the given guard. * * @param string $name * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard * * @throws \InvalidArgumentException */ protected function resolve($name) { $config = $this->getConfig($name); if (is_null($config)) { throw new InvalidArgumentException("Auth guard [{$name}] is not defined."); } if (isset($this->customCreators[$config['driver']])) { return $this->callCustomCreator($name, $config); } $driverMethod = 'create'.ucfirst($config['driver']).'Driver'; if (method_exists($this, $driverMethod)) { return $this->{$driverMethod}($name, $config); } throw new InvalidArgumentException( "Auth driver [{$config['driver']}] for guard [{$name}] is not defined." ); } /** * Call a custom driver creator. * * @param string $name * @param array $config * @return mixed */ protected function callCustomCreator($name, array $config) { return $this->customCreators[$config['driver']]($this->app, $name, $config); } /** * Create a session based authentication guard. * * @param string $name * @param array $config * @return \Illuminate\Auth\SessionGuard */ public function createSessionDriver($name, $config) { $provider = $this->createUserProvider($config['provider'] ?? null); $guard = new SessionGuard( $name, $provider, $this->app['session.store'], ); // When using the remember me functionality of the authentication services we // will need to be set the encryption instance of the guard, which allows // secure, encrypted cookie values to get generated for those cookies. if (method_exists($guard, 'setCookieJar')) { $guard->setCookieJar($this->app['cookie']); } if (method_exists($guard, 'setDispatcher')) { $guard->setDispatcher($this->app['events']); } if (method_exists($guard, 'setRequest')) { $guard->setRequest($this->app->refresh('request', $guard, 'setRequest')); } if (isset($config['remember'])) { $guard->setRememberDuration($config['remember']); } return $guard; } /** * Create a token based authentication guard. * * @param string $name * @param array $config * @return \Illuminate\Auth\TokenGuard */ public function createTokenDriver($name, $config) { // The token guard implements a basic API token based guard implementation // that takes an API token field from the request and matches it to the // user in the database or another persistence layer where users are. $guard = new TokenGuard( $this->createUserProvider($config['provider'] ?? null), $this->app['request'], $config['input_key'] ?? 'api_token', $config['storage_key'] ?? 'api_token', $config['hash'] ?? false ); $this->app->refresh('request', $guard, 'setRequest'); return $guard; } /** * Get the guard configuration. * * @param string $name * @return array */ protected function getConfig($name) { return $this->app['config']["auth.guards.{$name}"]; } /** * Get the default authentication driver name. * * @return string */ public function getDefaultDriver() { return $this->app['config']['auth.defaults.guard']; } /** * Set the default guard driver the factory should serve. * * @param string $name * @return void */ public function shouldUse($name) { $name = $name ?: $this->getDefaultDriver(); $this->setDefaultDriver($name); $this->userResolver = fn ($name = null) => $this->guard($name)->user(); } /** * Set the default authentication driver name. * * @param string $name * @return void */ public function setDefaultDriver($name) { $this->app['config']['auth.defaults.guard'] = $name; } /** * Register a new callback based request guard. * * @param string $driver * @param callable $callback * @return $this */ public function viaRequest($driver, callable $callback) { return $this->extend($driver, function () use ($callback) { $guard = new RequestGuard($callback, $this->app['request'], $this->createUserProvider()); $this->app->refresh('request', $guard, 'setRequest'); return $guard; }); } /** * Get the user resolver callback. * * @return \Closure */ public function userResolver() { return $this->userResolver; } /** * Set the callback to be used to resolve users. * * @param \Closure $userResolver * @return $this */ public function resolveUsersUsing(Closure $userResolver) { $this->userResolver = $userResolver; return $this; } /** * Register a custom driver creator Closure. * * @param string $driver * @param \Closure $callback * @return $this */ public function extend($driver, Closure $callback) { $this->customCreators[$driver] = $callback; return $this; } /** * Register a custom provider creator Closure. * * @param string $name * @param \Closure $callback * @return $this */ public function provider($name, Closure $callback) { $this->customProviderCreators[$name] = $callback; return $this; } /** * Determines if any guards have already been resolved. * * @return bool */ public function hasResolvedGuards() { return count($this->guards) > 0; } /** * Forget all of the resolved guard instances. * * @return $this */ public function forgetGuards() { $this->guards = []; return $this; } /** * Set the application instance used by the manager. * * @param \Illuminate\Contracts\Foundation\Application $app * @return $this */ public function setApplication($app) { $this->app = $app; return $this; } /** * Dynamically call the default driver instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->guard()->{$method}(...$parameters); } } src/Illuminate/Auth/Recaller.php 0000644 00000003526 15107327037 0012614 0 ustar 00 <?php namespace Illuminate\Auth; class Recaller { /** * The "recaller" / "remember me" cookie string. * * @var string */ protected $recaller; /** * Create a new recaller instance. * * @param string $recaller * @return void */ public function __construct($recaller) { $this->recaller = @unserialize($recaller, ['allowed_classes' => false]) ?: $recaller; } /** * Get the user ID from the recaller. * * @return string */ public function id() { return explode('|', $this->recaller, 3)[0]; } /** * Get the "remember token" token from the recaller. * * @return string */ public function token() { return explode('|', $this->recaller, 3)[1]; } /** * Get the password from the recaller. * * @return string */ public function hash() { return explode('|', $this->recaller, 4)[2]; } /** * Determine if the recaller is valid. * * @return bool */ public function valid() { return $this->properString() && $this->hasAllSegments(); } /** * Determine if the recaller is an invalid string. * * @return bool */ protected function properString() { return is_string($this->recaller) && str_contains($this->recaller, '|'); } /** * Determine if the recaller has all segments. * * @return bool */ protected function hasAllSegments() { $segments = explode('|', $this->recaller); return count($segments) >= 3 && trim($segments[0]) !== '' && trim($segments[1]) !== ''; } /** * Get the recaller's segments. * * @return array */ public function segments() { return explode('|', $this->recaller); } } src/Illuminate/Auth/SessionGuard.php 0000644 00000064752 15107327037 0013501 0 ustar 00 <?php namespace Illuminate\Auth; use Illuminate\Auth\Events\Attempting; use Illuminate\Auth\Events\Authenticated; use Illuminate\Auth\Events\CurrentDeviceLogout; use Illuminate\Auth\Events\Failed; use Illuminate\Auth\Events\Login; use Illuminate\Auth\Events\Logout; use Illuminate\Auth\Events\OtherDeviceLogout; use Illuminate\Auth\Events\Validated; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\StatefulGuard; use Illuminate\Contracts\Auth\SupportsBasicAuth; use Illuminate\Contracts\Auth\UserProvider; use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Session\Session; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; use Illuminate\Support\Timebox; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; use RuntimeException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; class SessionGuard implements StatefulGuard, SupportsBasicAuth { use GuardHelpers, Macroable; /** * The name of the guard. Typically "web". * * Corresponds to guard name in authentication configuration. * * @var string */ public readonly string $name; /** * The user we last attempted to retrieve. * * @var \Illuminate\Contracts\Auth\Authenticatable */ protected $lastAttempted; /** * Indicates if the user was authenticated via a recaller cookie. * * @var bool */ protected $viaRemember = false; /** * The number of minutes that the "remember me" cookie should be valid for. * * @var int */ protected $rememberDuration = 576000; /** * The session used by the guard. * * @var \Illuminate\Contracts\Session\Session */ protected $session; /** * The Illuminate cookie creator service. * * @var \Illuminate\Contracts\Cookie\QueueingFactory */ protected $cookie; /** * The request instance. * * @var \Symfony\Component\HttpFoundation\Request */ protected $request; /** * The event dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * The timebox instance. * * @var \Illuminate\Support\Timebox */ protected $timebox; /** * Indicates if the logout method has been called. * * @var bool */ protected $loggedOut = false; /** * Indicates if a token user retrieval has been attempted. * * @var bool */ protected $recallAttempted = false; /** * Create a new authentication guard. * * @param string $name * @param \Illuminate\Contracts\Auth\UserProvider $provider * @param \Illuminate\Contracts\Session\Session $session * @param \Symfony\Component\HttpFoundation\Request|null $request * @param \Illuminate\Support\Timebox|null $timebox * @return void */ public function __construct($name, UserProvider $provider, Session $session, Request $request = null, Timebox $timebox = null) { $this->name = $name; $this->session = $session; $this->request = $request; $this->provider = $provider; $this->timebox = $timebox ?: new Timebox; } /** * Get the currently authenticated user. * * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function user() { if ($this->loggedOut) { return; } // If we've already retrieved the user for the current request we can just // return it back immediately. We do not want to fetch the user data on // every call to this method because that would be tremendously slow. if (! is_null($this->user)) { return $this->user; } $id = $this->session->get($this->getName()); // First we will try to load the user using the identifier in the session if // one exists. Otherwise we will check for a "remember me" cookie in this // request, and if one exists, attempt to retrieve the user using that. if (! is_null($id) && $this->user = $this->provider->retrieveById($id)) { $this->fireAuthenticatedEvent($this->user); } // If the user is null, but we decrypt a "recaller" cookie we can attempt to // pull the user data on that cookie which serves as a remember cookie on // the application. Once we have a user we can return it to the caller. if (is_null($this->user) && ! is_null($recaller = $this->recaller())) { $this->user = $this->userFromRecaller($recaller); if ($this->user) { $this->updateSession($this->user->getAuthIdentifier()); $this->fireLoginEvent($this->user, true); } } return $this->user; } /** * Pull a user from the repository by its "remember me" cookie token. * * @param \Illuminate\Auth\Recaller $recaller * @return mixed */ protected function userFromRecaller($recaller) { if (! $recaller->valid() || $this->recallAttempted) { return; } // If the user is null, but we decrypt a "recaller" cookie we can attempt to // pull the user data on that cookie which serves as a remember cookie on // the application. Once we have a user we can return it to the caller. $this->recallAttempted = true; $this->viaRemember = ! is_null($user = $this->provider->retrieveByToken( $recaller->id(), $recaller->token() )); return $user; } /** * Get the decrypted recaller cookie for the request. * * @return \Illuminate\Auth\Recaller|null */ protected function recaller() { if (is_null($this->request)) { return; } if ($recaller = $this->request->cookies->get($this->getRecallerName())) { return new Recaller($recaller); } } /** * Get the ID for the currently authenticated user. * * @return int|string|null */ public function id() { if ($this->loggedOut) { return; } return $this->user() ? $this->user()->getAuthIdentifier() : $this->session->get($this->getName()); } /** * Log a user into the application without sessions or cookies. * * @param array $credentials * @return bool */ public function once(array $credentials = []) { $this->fireAttemptEvent($credentials); if ($this->validate($credentials)) { $this->setUser($this->lastAttempted); return true; } return false; } /** * Log the given user ID into the application without sessions or cookies. * * @param mixed $id * @return \Illuminate\Contracts\Auth\Authenticatable|false */ public function onceUsingId($id) { if (! is_null($user = $this->provider->retrieveById($id))) { $this->setUser($user); return $user; } return false; } /** * Validate a user's credentials. * * @param array $credentials * @return bool */ public function validate(array $credentials = []) { $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials); return $this->hasValidCredentials($user, $credentials); } /** * Attempt to authenticate using HTTP Basic Auth. * * @param string $field * @param array $extraConditions * @return \Symfony\Component\HttpFoundation\Response|null * * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException */ public function basic($field = 'email', $extraConditions = []) { if ($this->check()) { return; } // If a username is set on the HTTP basic request, we will return out without // interrupting the request lifecycle. Otherwise, we'll need to generate a // request indicating that the given credentials were invalid for login. if ($this->attemptBasic($this->getRequest(), $field, $extraConditions)) { return; } return $this->failedBasicResponse(); } /** * Perform a stateless HTTP Basic login attempt. * * @param string $field * @param array $extraConditions * @return \Symfony\Component\HttpFoundation\Response|null * * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException */ public function onceBasic($field = 'email', $extraConditions = []) { $credentials = $this->basicCredentials($this->getRequest(), $field); if (! $this->once(array_merge($credentials, $extraConditions))) { return $this->failedBasicResponse(); } } /** * Attempt to authenticate using basic authentication. * * @param \Symfony\Component\HttpFoundation\Request $request * @param string $field * @param array $extraConditions * @return bool */ protected function attemptBasic(Request $request, $field, $extraConditions = []) { if (! $request->getUser()) { return false; } return $this->attempt(array_merge( $this->basicCredentials($request, $field), $extraConditions )); } /** * Get the credential array for an HTTP Basic request. * * @param \Symfony\Component\HttpFoundation\Request $request * @param string $field * @return array */ protected function basicCredentials(Request $request, $field) { return [$field => $request->getUser(), 'password' => $request->getPassword()]; } /** * Get the response for basic authentication. * * @return void * * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException */ protected function failedBasicResponse() { throw new UnauthorizedHttpException('Basic', 'Invalid credentials.'); } /** * Attempt to authenticate a user using the given credentials. * * @param array $credentials * @param bool $remember * @return bool */ public function attempt(array $credentials = [], $remember = false) { $this->fireAttemptEvent($credentials, $remember); $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials); // If an implementation of UserInterface was returned, we'll ask the provider // to validate the user against the given credentials, and if they are in // fact valid we'll log the users into the application and return true. if ($this->hasValidCredentials($user, $credentials)) { $this->login($user, $remember); return true; } // If the authentication attempt fails we will fire an event so that the user // may be notified of any suspicious attempts to access their account from // an unrecognized user. A developer may listen to this event as needed. $this->fireFailedEvent($user, $credentials); return false; } /** * Attempt to authenticate a user with credentials and additional callbacks. * * @param array $credentials * @param array|callable|null $callbacks * @param bool $remember * @return bool */ public function attemptWhen(array $credentials = [], $callbacks = null, $remember = false) { $this->fireAttemptEvent($credentials, $remember); $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials); // This method does the exact same thing as attempt, but also executes callbacks after // the user is retrieved and validated. If one of the callbacks returns falsy we do // not login the user. Instead, we will fail the specific authentication attempt. if ($this->hasValidCredentials($user, $credentials) && $this->shouldLogin($callbacks, $user)) { $this->login($user, $remember); return true; } $this->fireFailedEvent($user, $credentials); return false; } /** * Determine if the user matches the credentials. * * @param mixed $user * @param array $credentials * @return bool */ protected function hasValidCredentials($user, $credentials) { return $this->timebox->call(function ($timebox) use ($user, $credentials) { $validated = ! is_null($user) && $this->provider->validateCredentials($user, $credentials); if ($validated) { $timebox->returnEarly(); $this->fireValidatedEvent($user); } return $validated; }, 200 * 1000); } /** * Determine if the user should login by executing the given callbacks. * * @param array|callable|null $callbacks * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return bool */ protected function shouldLogin($callbacks, AuthenticatableContract $user) { foreach (Arr::wrap($callbacks) as $callback) { if (! $callback($user, $this)) { return false; } } return true; } /** * Log the given user ID into the application. * * @param mixed $id * @param bool $remember * @return \Illuminate\Contracts\Auth\Authenticatable|false */ public function loginUsingId($id, $remember = false) { if (! is_null($user = $this->provider->retrieveById($id))) { $this->login($user, $remember); return $user; } return false; } /** * Log a user into the application. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param bool $remember * @return void */ public function login(AuthenticatableContract $user, $remember = false) { $this->updateSession($user->getAuthIdentifier()); // If the user should be permanently "remembered" by the application we will // queue a permanent cookie that contains the encrypted copy of the user // identifier. We will then decrypt this later to retrieve the users. if ($remember) { $this->ensureRememberTokenIsSet($user); $this->queueRecallerCookie($user); } // If we have an event dispatcher instance set we will fire an event so that // any listeners will hook into the authentication events and run actions // based on the login and logout events fired from the guard instances. $this->fireLoginEvent($user, $remember); $this->setUser($user); } /** * Update the session with the given ID. * * @param string $id * @return void */ protected function updateSession($id) { $this->session->put($this->getName(), $id); $this->session->migrate(true); } /** * Create a new "remember me" token for the user if one doesn't already exist. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ protected function ensureRememberTokenIsSet(AuthenticatableContract $user) { if (empty($user->getRememberToken())) { $this->cycleRememberToken($user); } } /** * Queue the recaller cookie into the cookie jar. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ protected function queueRecallerCookie(AuthenticatableContract $user) { $this->getCookieJar()->queue($this->createRecaller( $user->getAuthIdentifier().'|'.$user->getRememberToken().'|'.$user->getAuthPassword() )); } /** * Create a "remember me" cookie for a given ID. * * @param string $value * @return \Symfony\Component\HttpFoundation\Cookie */ protected function createRecaller($value) { return $this->getCookieJar()->make($this->getRecallerName(), $value, $this->getRememberDuration()); } /** * Log the user out of the application. * * @return void */ public function logout() { $user = $this->user(); $this->clearUserDataFromStorage(); if (! is_null($this->user) && ! empty($user->getRememberToken())) { $this->cycleRememberToken($user); } // If we have an event dispatcher instance, we can fire off the logout event // so any further processing can be done. This allows the developer to be // listening for anytime a user signs out of this application manually. if (isset($this->events)) { $this->events->dispatch(new Logout($this->name, $user)); } // Once we have fired the logout event we will clear the users out of memory // so they are no longer available as the user is no longer considered as // being signed into this application and should not be available here. $this->user = null; $this->loggedOut = true; } /** * Log the user out of the application on their current device only. * * This method does not cycle the "remember" token. * * @return void */ public function logoutCurrentDevice() { $user = $this->user(); $this->clearUserDataFromStorage(); // If we have an event dispatcher instance, we can fire off the logout event // so any further processing can be done. This allows the developer to be // listening for anytime a user signs out of this application manually. if (isset($this->events)) { $this->events->dispatch(new CurrentDeviceLogout($this->name, $user)); } // Once we have fired the logout event we will clear the users out of memory // so they are no longer available as the user is no longer considered as // being signed into this application and should not be available here. $this->user = null; $this->loggedOut = true; } /** * Remove the user data from the session and cookies. * * @return void */ protected function clearUserDataFromStorage() { $this->session->remove($this->getName()); $this->getCookieJar()->unqueue($this->getRecallerName()); if (! is_null($this->recaller())) { $this->getCookieJar()->queue( $this->getCookieJar()->forget($this->getRecallerName()) ); } } /** * Refresh the "remember me" token for the user. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ protected function cycleRememberToken(AuthenticatableContract $user) { $user->setRememberToken($token = Str::random(60)); $this->provider->updateRememberToken($user, $token); } /** * Invalidate other sessions for the current user. * * The application must be using the AuthenticateSession middleware. * * @param string $password * @param string $attribute * @return \Illuminate\Contracts\Auth\Authenticatable|null * * @throws \Illuminate\Auth\AuthenticationException */ public function logoutOtherDevices($password, $attribute = 'password') { if (! $this->user()) { return; } $result = $this->rehashUserPassword($password, $attribute); if ($this->recaller() || $this->getCookieJar()->hasQueued($this->getRecallerName())) { $this->queueRecallerCookie($this->user()); } $this->fireOtherDeviceLogoutEvent($this->user()); return $result; } /** * Rehash the current user's password. * * @param string $password * @param string $attribute * @return \Illuminate\Contracts\Auth\Authenticatable|null * * @throws \InvalidArgumentException */ protected function rehashUserPassword($password, $attribute) { if (! Hash::check($password, $this->user()->{$attribute})) { throw new InvalidArgumentException('The given password does not match the current password.'); } return tap($this->user()->forceFill([ $attribute => Hash::make($password), ]))->save(); } /** * Register an authentication attempt event listener. * * @param mixed $callback * @return void */ public function attempting($callback) { $this->events?->listen(Events\Attempting::class, $callback); } /** * Fire the attempt event with the arguments. * * @param array $credentials * @param bool $remember * @return void */ protected function fireAttemptEvent(array $credentials, $remember = false) { $this->events?->dispatch(new Attempting($this->name, $credentials, $remember)); } /** * Fires the validated event if the dispatcher is set. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ protected function fireValidatedEvent($user) { $this->events?->dispatch(new Validated($this->name, $user)); } /** * Fire the login event if the dispatcher is set. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param bool $remember * @return void */ protected function fireLoginEvent($user, $remember = false) { $this->events?->dispatch(new Login($this->name, $user, $remember)); } /** * Fire the authenticated event if the dispatcher is set. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ protected function fireAuthenticatedEvent($user) { $this->events?->dispatch(new Authenticated($this->name, $user)); } /** * Fire the other device logout event if the dispatcher is set. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ protected function fireOtherDeviceLogoutEvent($user) { $this->events?->dispatch(new OtherDeviceLogout($this->name, $user)); } /** * Fire the failed authentication attempt event with the given arguments. * * @param \Illuminate\Contracts\Auth\Authenticatable|null $user * @param array $credentials * @return void */ protected function fireFailedEvent($user, array $credentials) { $this->events?->dispatch(new Failed($this->name, $user, $credentials)); } /** * Get the last user we attempted to authenticate. * * @return \Illuminate\Contracts\Auth\Authenticatable */ public function getLastAttempted() { return $this->lastAttempted; } /** * Get a unique identifier for the auth session value. * * @return string */ public function getName() { return 'login_'.$this->name.'_'.sha1(static::class); } /** * Get the name of the cookie used to store the "recaller". * * @return string */ public function getRecallerName() { return 'remember_'.$this->name.'_'.sha1(static::class); } /** * Determine if the user was authenticated via "remember me" cookie. * * @return bool */ public function viaRemember() { return $this->viaRemember; } /** * Get the number of minutes the remember me cookie should be valid for. * * @return int */ protected function getRememberDuration() { return $this->rememberDuration; } /** * Set the number of minutes the remember me cookie should be valid for. * * @param int $minutes * @return $this */ public function setRememberDuration($minutes) { $this->rememberDuration = $minutes; return $this; } /** * Get the cookie creator instance used by the guard. * * @return \Illuminate\Contracts\Cookie\QueueingFactory * * @throws \RuntimeException */ public function getCookieJar() { if (! isset($this->cookie)) { throw new RuntimeException('Cookie jar has not been set.'); } return $this->cookie; } /** * Set the cookie creator instance used by the guard. * * @param \Illuminate\Contracts\Cookie\QueueingFactory $cookie * @return void */ public function setCookieJar(CookieJar $cookie) { $this->cookie = $cookie; } /** * Get the event dispatcher instance. * * @return \Illuminate\Contracts\Events\Dispatcher */ public function getDispatcher() { return $this->events; } /** * Set the event dispatcher instance. * * @param \Illuminate\Contracts\Events\Dispatcher $events * @return void */ public function setDispatcher(Dispatcher $events) { $this->events = $events; } /** * Get the session store used by the guard. * * @return \Illuminate\Contracts\Session\Session */ public function getSession() { return $this->session; } /** * Return the currently cached user. * * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function getUser() { return $this->user; } /** * Set the current user. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return $this */ public function setUser(AuthenticatableContract $user) { $this->user = $user; $this->loggedOut = false; $this->fireAuthenticatedEvent($user); return $this; } /** * Get the current request instance. * * @return \Symfony\Component\HttpFoundation\Request */ public function getRequest() { return $this->request ?: Request::createFromGlobals(); } /** * Set the current request instance. * * @param \Symfony\Component\HttpFoundation\Request $request * @return $this */ public function setRequest(Request $request) { $this->request = $request; return $this; } /** * Get the timebox instance used by the guard. * * @return \Illuminate\Support\Timebox */ public function getTimebox() { return $this->timebox; } } src/Illuminate/Auth/Listeners/SendEmailVerificationNotification.php 0000644 00000001016 15107327037 0021576 0 ustar 00 <?php namespace Illuminate\Auth\Listeners; use Illuminate\Auth\Events\Registered; use Illuminate\Contracts\Auth\MustVerifyEmail; class SendEmailVerificationNotification { /** * Handle the event. * * @param \Illuminate\Auth\Events\Registered $event * @return void */ public function handle(Registered $event) { if ($event->user instanceof MustVerifyEmail && ! $event->user->hasVerifiedEmail()) { $event->user->sendEmailVerificationNotification(); } } } src/Illuminate/Auth/DatabaseUserProvider.php 0000644 00000011145 15107327037 0015135 0 ustar 00 <?php namespace Illuminate\Auth; use Closure; use Illuminate\Contracts\Auth\Authenticatable as UserContract; use Illuminate\Contracts\Auth\UserProvider; use Illuminate\Contracts\Hashing\Hasher as HasherContract; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\ConnectionInterface; class DatabaseUserProvider implements UserProvider { /** * The active database connection. * * @var \Illuminate\Database\ConnectionInterface */ protected $connection; /** * The hasher implementation. * * @var \Illuminate\Contracts\Hashing\Hasher */ protected $hasher; /** * The table containing the users. * * @var string */ protected $table; /** * Create a new database user provider. * * @param \Illuminate\Database\ConnectionInterface $connection * @param \Illuminate\Contracts\Hashing\Hasher $hasher * @param string $table * @return void */ public function __construct(ConnectionInterface $connection, HasherContract $hasher, $table) { $this->connection = $connection; $this->table = $table; $this->hasher = $hasher; } /** * Retrieve a user by their unique identifier. * * @param mixed $identifier * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveById($identifier) { $user = $this->connection->table($this->table)->find($identifier); return $this->getGenericUser($user); } /** * Retrieve a user by their unique identifier and "remember me" token. * * @param mixed $identifier * @param string $token * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByToken($identifier, $token) { $user = $this->getGenericUser( $this->connection->table($this->table)->find($identifier) ); return $user && $user->getRememberToken() && hash_equals($user->getRememberToken(), $token) ? $user : null; } /** * Update the "remember me" token for the given user in storage. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param string $token * @return void */ public function updateRememberToken(UserContract $user, $token) { $this->connection->table($this->table) ->where($user->getAuthIdentifierName(), $user->getAuthIdentifier()) ->update([$user->getRememberTokenName() => $token]); } /** * Retrieve a user by the given credentials. * * @param array $credentials * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByCredentials(array $credentials) { $credentials = array_filter( $credentials, fn ($key) => ! str_contains($key, 'password'), ARRAY_FILTER_USE_KEY ); if (empty($credentials)) { return; } // First we will add each credential element to the query as a where clause. // Then we can execute the query and, if we found a user, return it in a // generic "user" object that will be utilized by the Guard instances. $query = $this->connection->table($this->table); foreach ($credentials as $key => $value) { if (is_array($value) || $value instanceof Arrayable) { $query->whereIn($key, $value); } elseif ($value instanceof Closure) { $value($query); } else { $query->where($key, $value); } } // Now we are ready to execute the query to see if we have a user matching // the given credentials. If not, we will just return null and indicate // that there are no matching users from the given credential arrays. $user = $query->first(); return $this->getGenericUser($user); } /** * Get the generic user. * * @param mixed $user * @return \Illuminate\Auth\GenericUser|null */ protected function getGenericUser($user) { if (! is_null($user)) { return new GenericUser((array) $user); } } /** * Validate a user against the given credentials. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param array $credentials * @return bool */ public function validateCredentials(UserContract $user, array $credentials) { return $this->hasher->check( $credentials['password'], $user->getAuthPassword() ); } } src/Illuminate/Auth/CreatesUserProviders.php 0000644 00000004652 15107327037 0015207 0 ustar 00 <?php namespace Illuminate\Auth; use InvalidArgumentException; trait CreatesUserProviders { /** * The registered custom provider creators. * * @var array */ protected $customProviderCreators = []; /** * Create the user provider implementation for the driver. * * @param string|null $provider * @return \Illuminate\Contracts\Auth\UserProvider|null * * @throws \InvalidArgumentException */ public function createUserProvider($provider = null) { if (is_null($config = $this->getProviderConfiguration($provider))) { return; } if (isset($this->customProviderCreators[$driver = ($config['driver'] ?? null)])) { return call_user_func( $this->customProviderCreators[$driver], $this->app, $config ); } return match ($driver) { 'database' => $this->createDatabaseProvider($config), 'eloquent' => $this->createEloquentProvider($config), default => throw new InvalidArgumentException( "Authentication user provider [{$driver}] is not defined." ), }; } /** * Get the user provider configuration. * * @param string|null $provider * @return array|null */ protected function getProviderConfiguration($provider) { if ($provider = $provider ?: $this->getDefaultUserProvider()) { return $this->app['config']['auth.providers.'.$provider]; } } /** * Create an instance of the database user provider. * * @param array $config * @return \Illuminate\Auth\DatabaseUserProvider */ protected function createDatabaseProvider($config) { $connection = $this->app['db']->connection($config['connection'] ?? null); return new DatabaseUserProvider($connection, $this->app['hash'], $config['table']); } /** * Create an instance of the Eloquent user provider. * * @param array $config * @return \Illuminate\Auth\EloquentUserProvider */ protected function createEloquentProvider($config) { return new EloquentUserProvider($this->app['hash'], $config['model']); } /** * Get the default user provider name. * * @return string */ public function getDefaultUserProvider() { return $this->app['config']['auth.defaults.provider']; } } src/Illuminate/Auth/TokenGuard.php 0000644 00000006563 15107327037 0013132 0 ustar 00 <?php namespace Illuminate\Auth; use Illuminate\Contracts\Auth\Guard; use Illuminate\Contracts\Auth\UserProvider; use Illuminate\Http\Request; class TokenGuard implements Guard { use GuardHelpers; /** * The request instance. * * @var \Illuminate\Http\Request */ protected $request; /** * The name of the query string item from the request containing the API token. * * @var string */ protected $inputKey; /** * The name of the token "column" in persistent storage. * * @var string */ protected $storageKey; /** * Indicates if the API token is hashed in storage. * * @var bool */ protected $hash = false; /** * Create a new authentication guard. * * @param \Illuminate\Contracts\Auth\UserProvider $provider * @param \Illuminate\Http\Request $request * @param string $inputKey * @param string $storageKey * @param bool $hash * @return void */ public function __construct( UserProvider $provider, Request $request, $inputKey = 'api_token', $storageKey = 'api_token', $hash = false) { $this->hash = $hash; $this->request = $request; $this->provider = $provider; $this->inputKey = $inputKey; $this->storageKey = $storageKey; } /** * Get the currently authenticated user. * * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function user() { // If we've already retrieved the user for the current request we can just // return it back immediately. We do not want to fetch the user data on // every call to this method because that would be tremendously slow. if (! is_null($this->user)) { return $this->user; } $user = null; $token = $this->getTokenForRequest(); if (! empty($token)) { $user = $this->provider->retrieveByCredentials([ $this->storageKey => $this->hash ? hash('sha256', $token) : $token, ]); } return $this->user = $user; } /** * Get the token for the current request. * * @return string */ public function getTokenForRequest() { $token = $this->request->query($this->inputKey); if (empty($token)) { $token = $this->request->input($this->inputKey); } if (empty($token)) { $token = $this->request->bearerToken(); } if (empty($token)) { $token = $this->request->getPassword(); } return $token; } /** * Validate a user's credentials. * * @param array $credentials * @return bool */ public function validate(array $credentials = []) { if (empty($credentials[$this->inputKey])) { return false; } $credentials = [$this->storageKey => $credentials[$this->inputKey]]; if ($this->provider->retrieveByCredentials($credentials)) { return true; } return false; } /** * Set the current request instance. * * @param \Illuminate\Http\Request $request * @return $this */ public function setRequest(Request $request) { $this->request = $request; return $this; } } src/Illuminate/Auth/composer.json 0000644 00000002362 15107327037 0013071 0 ustar 00 { "name": "illuminate/auth", "description": "The Illuminate Auth package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "ext-hash": "*", "illuminate/collections": "^10.0", "illuminate/contracts": "^10.0", "illuminate/http": "^10.0", "illuminate/macroable": "^10.0", "illuminate/queue": "^10.0", "illuminate/support": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Auth\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "illuminate/console": "Required to use the auth:clear-resets command (^10.0).", "illuminate/queue": "Required to fire login / logout events (^10.0).", "illuminate/session": "Required to use the session based guard (^10.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Auth/Events/Login.php 0000644 00000001511 15107327037 0013367 0 ustar 00 <?php namespace Illuminate\Auth\Events; use Illuminate\Queue\SerializesModels; class Login { use SerializesModels; /** * The authentication guard name. * * @var string */ public $guard; /** * The authenticated user. * * @var \Illuminate\Contracts\Auth\Authenticatable */ public $user; /** * Indicates if the user should be "remembered". * * @var bool */ public $remember; /** * Create a new event instance. * * @param string $guard * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param bool $remember * @return void */ public function __construct($guard, $user, $remember) { $this->user = $user; $this->guard = $guard; $this->remember = $remember; } } src/Illuminate/Auth/Events/Lockout.php 0000644 00000000654 15107327037 0013746 0 ustar 00 <?php namespace Illuminate\Auth\Events; use Illuminate\Http\Request; class Lockout { /** * The throttled request. * * @var \Illuminate\Http\Request */ public $request; /** * Create a new event instance. * * @param \Illuminate\Http\Request $request * @return void */ public function __construct(Request $request) { $this->request = $request; } } src/Illuminate/Auth/Events/Validated.php 0000644 00000001253 15107327037 0014217 0 ustar 00 <?php namespace Illuminate\Auth\Events; use Illuminate\Queue\SerializesModels; class Validated { use SerializesModels; /** * The authentication guard name. * * @var string */ public $guard; /** * The user retrieved and validated from the User Provider. * * @var \Illuminate\Contracts\Auth\Authenticatable */ public $user; /** * Create a new event instance. * * @param string $guard * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ public function __construct($guard, $user) { $this->user = $user; $this->guard = $guard; } } src/Illuminate/Auth/Events/Attempting.php 0000644 00000001352 15107327037 0014436 0 ustar 00 <?php namespace Illuminate\Auth\Events; class Attempting { /** * The authentication guard name. * * @var string */ public $guard; /** * The credentials for the user. * * @var array */ public $credentials; /** * Indicates if the user should be "remembered". * * @var bool */ public $remember; /** * Create a new event instance. * * @param string $guard * @param array $credentials * @param bool $remember * @return void */ public function __construct($guard, $credentials, $remember) { $this->guard = $guard; $this->remember = $remember; $this->credentials = $credentials; } } src/Illuminate/Auth/Events/Registered.php 0000644 00000000742 15107327037 0014421 0 ustar 00 <?php namespace Illuminate\Auth\Events; use Illuminate\Queue\SerializesModels; class Registered { use SerializesModels; /** * The authenticated user. * * @var \Illuminate\Contracts\Auth\Authenticatable */ public $user; /** * Create a new event instance. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ public function __construct($user) { $this->user = $user; } } src/Illuminate/Auth/Events/CurrentDeviceLogout.php 0000644 00000001224 15107327037 0016254 0 ustar 00 <?php namespace Illuminate\Auth\Events; use Illuminate\Queue\SerializesModels; class CurrentDeviceLogout { use SerializesModels; /** * The authentication guard name. * * @var string */ public $guard; /** * The authenticated user. * * @var \Illuminate\Contracts\Auth\Authenticatable */ public $user; /** * Create a new event instance. * * @param string $guard * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ public function __construct($guard, $user) { $this->user = $user; $this->guard = $guard; } } src/Illuminate/Auth/Events/Authenticated.php 0000644 00000001216 15107327037 0015103 0 ustar 00 <?php namespace Illuminate\Auth\Events; use Illuminate\Queue\SerializesModels; class Authenticated { use SerializesModels; /** * The authentication guard name. * * @var string */ public $guard; /** * The authenticated user. * * @var \Illuminate\Contracts\Auth\Authenticatable */ public $user; /** * Create a new event instance. * * @param string $guard * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ public function __construct($guard, $user) { $this->user = $user; $this->guard = $guard; } } src/Illuminate/Auth/Events/PasswordReset.php 0000644 00000000727 15107327037 0015134 0 ustar 00 <?php namespace Illuminate\Auth\Events; use Illuminate\Queue\SerializesModels; class PasswordReset { use SerializesModels; /** * The user. * * @var \Illuminate\Contracts\Auth\Authenticatable */ public $user; /** * Create a new event instance. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ public function __construct($user) { $this->user = $user; } } src/Illuminate/Auth/Events/Logout.php 0000644 00000001207 15107327037 0013572 0 ustar 00 <?php namespace Illuminate\Auth\Events; use Illuminate\Queue\SerializesModels; class Logout { use SerializesModels; /** * The authentication guard name. * * @var string */ public $guard; /** * The authenticated user. * * @var \Illuminate\Contracts\Auth\Authenticatable */ public $user; /** * Create a new event instance. * * @param string $guard * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ public function __construct($guard, $user) { $this->user = $user; $this->guard = $guard; } } src/Illuminate/Auth/Events/Verified.php 0000644 00000000733 15107327037 0014061 0 ustar 00 <?php namespace Illuminate\Auth\Events; use Illuminate\Queue\SerializesModels; class Verified { use SerializesModels; /** * The verified user. * * @var \Illuminate\Contracts\Auth\MustVerifyEmail */ public $user; /** * Create a new event instance. * * @param \Illuminate\Contracts\Auth\MustVerifyEmail $user * @return void */ public function __construct($user) { $this->user = $user; } } src/Illuminate/Auth/Events/Failed.php 0000644 00000001475 15107327037 0013514 0 ustar 00 <?php namespace Illuminate\Auth\Events; class Failed { /** * The authentication guard name. * * @var string */ public $guard; /** * The user the attempter was trying to authenticate as. * * @var \Illuminate\Contracts\Auth\Authenticatable|null */ public $user; /** * The credentials provided by the attempter. * * @var array */ public $credentials; /** * Create a new event instance. * * @param string $guard * @param \Illuminate\Contracts\Auth\Authenticatable|null $user * @param array $credentials * @return void */ public function __construct($guard, $user, $credentials) { $this->user = $user; $this->guard = $guard; $this->credentials = $credentials; } } src/Illuminate/Auth/Events/OtherDeviceLogout.php 0000644 00000001222 15107327037 0015711 0 ustar 00 <?php namespace Illuminate\Auth\Events; use Illuminate\Queue\SerializesModels; class OtherDeviceLogout { use SerializesModels; /** * The authentication guard name. * * @var string */ public $guard; /** * The authenticated user. * * @var \Illuminate\Contracts\Auth\Authenticatable */ public $user; /** * Create a new event instance. * * @param string $guard * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ public function __construct($guard, $user) { $this->user = $user; $this->guard = $guard; } } src/Illuminate/Auth/Passwords/DatabaseTokenRepository.php 0000644 00000014075 15107327037 0017656 0 ustar 00 <?php namespace Illuminate\Auth\Passwords; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; use Illuminate\Contracts\Hashing\Hasher as HasherContract; use Illuminate\Database\ConnectionInterface; use Illuminate\Support\Carbon; use Illuminate\Support\Str; class DatabaseTokenRepository implements TokenRepositoryInterface { /** * The database connection instance. * * @var \Illuminate\Database\ConnectionInterface */ protected $connection; /** * The Hasher implementation. * * @var \Illuminate\Contracts\Hashing\Hasher */ protected $hasher; /** * The token database table. * * @var string */ protected $table; /** * The hashing key. * * @var string */ protected $hashKey; /** * The number of seconds a token should last. * * @var int */ protected $expires; /** * Minimum number of seconds before re-redefining the token. * * @var int */ protected $throttle; /** * Create a new token repository instance. * * @param \Illuminate\Database\ConnectionInterface $connection * @param \Illuminate\Contracts\Hashing\Hasher $hasher * @param string $table * @param string $hashKey * @param int $expires * @param int $throttle * @return void */ public function __construct(ConnectionInterface $connection, HasherContract $hasher, $table, $hashKey, $expires = 60, $throttle = 60) { $this->table = $table; $this->hasher = $hasher; $this->hashKey = $hashKey; $this->expires = $expires * 60; $this->connection = $connection; $this->throttle = $throttle; } /** * Create a new token record. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @return string */ public function create(CanResetPasswordContract $user) { $email = $user->getEmailForPasswordReset(); $this->deleteExisting($user); // We will create a new, random token for the user so that we can e-mail them // a safe link to the password reset form. Then we will insert a record in // the database so that we can verify the token within the actual reset. $token = $this->createNewToken(); $this->getTable()->insert($this->getPayload($email, $token)); return $token; } /** * Delete all existing reset tokens from the database. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @return int */ protected function deleteExisting(CanResetPasswordContract $user) { return $this->getTable()->where('email', $user->getEmailForPasswordReset())->delete(); } /** * Build the record payload for the table. * * @param string $email * @param string $token * @return array */ protected function getPayload($email, $token) { return ['email' => $email, 'token' => $this->hasher->make($token), 'created_at' => new Carbon]; } /** * Determine if a token record exists and is valid. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @param string $token * @return bool */ public function exists(CanResetPasswordContract $user, $token) { $record = (array) $this->getTable()->where( 'email', $user->getEmailForPasswordReset() )->first(); return $record && ! $this->tokenExpired($record['created_at']) && $this->hasher->check($token, $record['token']); } /** * Determine if the token has expired. * * @param string $createdAt * @return bool */ protected function tokenExpired($createdAt) { return Carbon::parse($createdAt)->addSeconds($this->expires)->isPast(); } /** * Determine if the given user recently created a password reset token. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @return bool */ public function recentlyCreatedToken(CanResetPasswordContract $user) { $record = (array) $this->getTable()->where( 'email', $user->getEmailForPasswordReset() )->first(); return $record && $this->tokenRecentlyCreated($record['created_at']); } /** * Determine if the token was recently created. * * @param string $createdAt * @return bool */ protected function tokenRecentlyCreated($createdAt) { if ($this->throttle <= 0) { return false; } return Carbon::parse($createdAt)->addSeconds( $this->throttle )->isFuture(); } /** * Delete a token record by user. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @return void */ public function delete(CanResetPasswordContract $user) { $this->deleteExisting($user); } /** * Delete expired tokens. * * @return void */ public function deleteExpired() { $expiredAt = Carbon::now()->subSeconds($this->expires); $this->getTable()->where('created_at', '<', $expiredAt)->delete(); } /** * Create a new token for the user. * * @return string */ public function createNewToken() { return hash_hmac('sha256', Str::random(40), $this->hashKey); } /** * Get the database connection instance. * * @return \Illuminate\Database\ConnectionInterface */ public function getConnection() { return $this->connection; } /** * Begin a new database query against the table. * * @return \Illuminate\Database\Query\Builder */ protected function getTable() { return $this->connection->table($this->table); } /** * Get the hasher instance. * * @return \Illuminate\Contracts\Hashing\Hasher */ public function getHasher() { return $this->hasher; } } src/Illuminate/Auth/Passwords/PasswordBrokerManager.php 0000644 00000007137 15107327037 0017314 0 ustar 00 <?php namespace Illuminate\Auth\Passwords; use Illuminate\Contracts\Auth\PasswordBrokerFactory as FactoryContract; use InvalidArgumentException; /** * @mixin \Illuminate\Contracts\Auth\PasswordBroker */ class PasswordBrokerManager implements FactoryContract { /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The array of created "drivers". * * @var array */ protected $brokers = []; /** * Create a new PasswordBroker manager instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function __construct($app) { $this->app = $app; } /** * Attempt to get the broker from the local cache. * * @param string|null $name * @return \Illuminate\Contracts\Auth\PasswordBroker */ public function broker($name = null) { $name = $name ?: $this->getDefaultDriver(); return $this->brokers[$name] ?? ($this->brokers[$name] = $this->resolve($name)); } /** * Resolve the given broker. * * @param string $name * @return \Illuminate\Contracts\Auth\PasswordBroker * * @throws \InvalidArgumentException */ protected function resolve($name) { $config = $this->getConfig($name); if (is_null($config)) { throw new InvalidArgumentException("Password resetter [{$name}] is not defined."); } // The password broker uses a token repository to validate tokens and send user // password e-mails, as well as validating that password reset process as an // aggregate service of sorts providing a convenient interface for resets. return new PasswordBroker( $this->createTokenRepository($config), $this->app['auth']->createUserProvider($config['provider'] ?? null) ); } /** * Create a token repository instance based on the given configuration. * * @param array $config * @return \Illuminate\Auth\Passwords\TokenRepositoryInterface */ protected function createTokenRepository(array $config) { $key = $this->app['config']['app.key']; if (str_starts_with($key, 'base64:')) { $key = base64_decode(substr($key, 7)); } $connection = $config['connection'] ?? null; return new DatabaseTokenRepository( $this->app['db']->connection($connection), $this->app['hash'], $config['table'], $key, $config['expire'], $config['throttle'] ?? 0 ); } /** * Get the password broker configuration. * * @param string $name * @return array|null */ protected function getConfig($name) { return $this->app['config']["auth.passwords.{$name}"]; } /** * Get the default password broker name. * * @return string */ public function getDefaultDriver() { return $this->app['config']['auth.defaults.passwords']; } /** * Set the default password broker name. * * @param string $name * @return void */ public function setDefaultDriver($name) { $this->app['config']['auth.defaults.passwords'] = $name; } /** * Dynamically call the default driver instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->broker()->{$method}(...$parameters); } } src/Illuminate/Auth/Passwords/PasswordBroker.php 0000644 00000012730 15107327037 0016014 0 ustar 00 <?php namespace Illuminate\Auth\Passwords; use Closure; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; use Illuminate\Contracts\Auth\PasswordBroker as PasswordBrokerContract; use Illuminate\Contracts\Auth\UserProvider; use Illuminate\Support\Arr; use UnexpectedValueException; class PasswordBroker implements PasswordBrokerContract { /** * The password token repository. * * @var \Illuminate\Auth\Passwords\TokenRepositoryInterface */ protected $tokens; /** * The user provider implementation. * * @var \Illuminate\Contracts\Auth\UserProvider */ protected $users; /** * Create a new password broker instance. * * @param \Illuminate\Auth\Passwords\TokenRepositoryInterface $tokens * @param \Illuminate\Contracts\Auth\UserProvider $users * @return void */ public function __construct(TokenRepositoryInterface $tokens, UserProvider $users) { $this->users = $users; $this->tokens = $tokens; } /** * Send a password reset link to a user. * * @param array $credentials * @param \Closure|null $callback * @return string */ public function sendResetLink(array $credentials, Closure $callback = null) { // First we will check to see if we found a user at the given credentials and // if we did not we will redirect back to this current URI with a piece of // "flash" data in the session to indicate to the developers the errors. $user = $this->getUser($credentials); if (is_null($user)) { return static::INVALID_USER; } if ($this->tokens->recentlyCreatedToken($user)) { return static::RESET_THROTTLED; } $token = $this->tokens->create($user); if ($callback) { return $callback($user, $token) ?? static::RESET_LINK_SENT; } // Once we have the reset token, we are ready to send the message out to this // user with a link to reset their password. We will then redirect back to // the current URI having nothing set in the session to indicate errors. $user->sendPasswordResetNotification($token); return static::RESET_LINK_SENT; } /** * Reset the password for the given token. * * @param array $credentials * @param \Closure $callback * @return mixed */ public function reset(array $credentials, Closure $callback) { $user = $this->validateReset($credentials); // If the responses from the validate method is not a user instance, we will // assume that it is a redirect and simply return it from this method and // the user is properly redirected having an error message on the post. if (! $user instanceof CanResetPasswordContract) { return $user; } $password = $credentials['password']; // Once the reset has been validated, we'll call the given callback with the // new password. This gives the user an opportunity to store the password // in their persistent storage. Then we'll delete the token and return. $callback($user, $password); $this->tokens->delete($user); return static::PASSWORD_RESET; } /** * Validate a password reset for the given credentials. * * @param array $credentials * @return \Illuminate\Contracts\Auth\CanResetPassword|string */ protected function validateReset(array $credentials) { if (is_null($user = $this->getUser($credentials))) { return static::INVALID_USER; } if (! $this->tokens->exists($user, $credentials['token'])) { return static::INVALID_TOKEN; } return $user; } /** * Get the user for the given credentials. * * @param array $credentials * @return \Illuminate\Contracts\Auth\CanResetPassword|null * * @throws \UnexpectedValueException */ public function getUser(array $credentials) { $credentials = Arr::except($credentials, ['token']); $user = $this->users->retrieveByCredentials($credentials); if ($user && ! $user instanceof CanResetPasswordContract) { throw new UnexpectedValueException('User must implement CanResetPassword interface.'); } return $user; } /** * Create a new password reset token for the given user. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @return string */ public function createToken(CanResetPasswordContract $user) { return $this->tokens->create($user); } /** * Delete password reset tokens of the given user. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @return void */ public function deleteToken(CanResetPasswordContract $user) { $this->tokens->delete($user); } /** * Validate the given password reset token. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @param string $token * @return bool */ public function tokenExists(CanResetPasswordContract $user, $token) { return $this->tokens->exists($user, $token); } /** * Get the password reset token repository implementation. * * @return \Illuminate\Auth\Passwords\TokenRepositoryInterface */ public function getRepository() { return $this->tokens; } } src/Illuminate/Auth/Passwords/TokenRepositoryInterface.php 0000644 00000002252 15107327037 0020044 0 ustar 00 <?php namespace Illuminate\Auth\Passwords; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; interface TokenRepositoryInterface { /** * Create a new token. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @return string */ public function create(CanResetPasswordContract $user); /** * Determine if a token record exists and is valid. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @param string $token * @return bool */ public function exists(CanResetPasswordContract $user, $token); /** * Determine if the given user recently created a password reset token. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @return bool */ public function recentlyCreatedToken(CanResetPasswordContract $user); /** * Delete a token record. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @return void */ public function delete(CanResetPasswordContract $user); /** * Delete expired tokens. * * @return void */ public function deleteExpired(); } src/Illuminate/Auth/Passwords/CanResetPassword.php 0000644 00000001132 15107327037 0016266 0 ustar 00 <?php namespace Illuminate\Auth\Passwords; use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification; trait CanResetPassword { /** * Get the e-mail address where password reset links are sent. * * @return string */ public function getEmailForPasswordReset() { return $this->email; } /** * Send the password reset notification. * * @param string $token * @return void */ public function sendPasswordResetNotification($token) { $this->notify(new ResetPasswordNotification($token)); } } src/Illuminate/Auth/Passwords/PasswordResetServiceProvider.php 0000644 00000001762 15107327037 0020711 0 ustar 00 <?php namespace Illuminate\Auth\Passwords; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; class PasswordResetServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Register the service provider. * * @return void */ public function register() { $this->registerPasswordBroker(); } /** * Register the password broker instance. * * @return void */ protected function registerPasswordBroker() { $this->app->singleton('auth.password', function ($app) { return new PasswordBrokerManager($app); }); $this->app->bind('auth.password.broker', function ($app) { return $app->make('auth.password')->broker(); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return ['auth.password', 'auth.password.broker']; } } src/Illuminate/Auth/Authenticatable.php 0000644 00000003372 15107327037 0014157 0 ustar 00 <?php namespace Illuminate\Auth; trait Authenticatable { /** * The column name of the "remember me" token. * * @var string */ protected $rememberTokenName = 'remember_token'; /** * Get the name of the unique identifier for the user. * * @return string */ public function getAuthIdentifierName() { return $this->getKeyName(); } /** * Get the unique identifier for the user. * * @return mixed */ public function getAuthIdentifier() { return $this->{$this->getAuthIdentifierName()}; } /** * Get the unique broadcast identifier for the user. * * @return mixed */ public function getAuthIdentifierForBroadcasting() { return $this->getAuthIdentifier(); } /** * Get the password for the user. * * @return string */ public function getAuthPassword() { return $this->password; } /** * Get the token value for the "remember me" session. * * @return string|null */ public function getRememberToken() { if (! empty($this->getRememberTokenName())) { return (string) $this->{$this->getRememberTokenName()}; } } /** * Set the token value for the "remember me" session. * * @param string $value * @return void */ public function setRememberToken($value) { if (! empty($this->getRememberTokenName())) { $this->{$this->getRememberTokenName()} = $value; } } /** * Get the column name for the "remember me" token. * * @return string */ public function getRememberTokenName() { return $this->rememberTokenName; } } src/Illuminate/Auth/Notifications/ResetPassword.php 0000644 00000006650 15107327037 0016502 0 ustar 00 <?php namespace Illuminate\Auth\Notifications; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; use Illuminate\Support\Facades\Lang; class ResetPassword extends Notification { /** * The password reset token. * * @var string */ public $token; /** * The callback that should be used to create the reset password URL. * * @var (\Closure(mixed, string): string)|null */ public static $createUrlCallback; /** * The callback that should be used to build the mail message. * * @var (\Closure(mixed, string): \Illuminate\Notifications\Messages\MailMessage)|null */ public static $toMailCallback; /** * Create a notification instance. * * @param string $token * @return void */ public function __construct($token) { $this->token = $token; } /** * Get the notification's channels. * * @param mixed $notifiable * @return array|string */ public function via($notifiable) { return ['mail']; } /** * Build the mail representation of the notification. * * @param mixed $notifiable * @return \Illuminate\Notifications\Messages\MailMessage */ public function toMail($notifiable) { if (static::$toMailCallback) { return call_user_func(static::$toMailCallback, $notifiable, $this->token); } return $this->buildMailMessage($this->resetUrl($notifiable)); } /** * Get the reset password notification mail message for the given URL. * * @param string $url * @return \Illuminate\Notifications\Messages\MailMessage */ protected function buildMailMessage($url) { return (new MailMessage) ->subject(Lang::get('Reset Password Notification')) ->line(Lang::get('You are receiving this email because we received a password reset request for your account.')) ->action(Lang::get('Reset Password'), $url) ->line(Lang::get('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.'.config('auth.defaults.passwords').'.expire')])) ->line(Lang::get('If you did not request a password reset, no further action is required.')); } /** * Get the reset URL for the given notifiable. * * @param mixed $notifiable * @return string */ protected function resetUrl($notifiable) { if (static::$createUrlCallback) { return call_user_func(static::$createUrlCallback, $notifiable, $this->token); } return url(route('password.reset', [ 'token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset(), ], false)); } /** * Set a callback that should be used when creating the reset password button URL. * * @param \Closure(mixed, string): string $callback * @return void */ public static function createUrlUsing($callback) { static::$createUrlCallback = $callback; } /** * Set a callback that should be used when building the notification mail message. * * @param \Closure(mixed, string): \Illuminate\Notifications\Messages\MailMessage $callback * @return void */ public static function toMailUsing($callback) { static::$toMailCallback = $callback; } } src/Illuminate/Auth/Notifications/VerifyEmail.php 0000644 00000006063 15107327037 0016107 0 ustar 00 <?php namespace Illuminate\Auth\Notifications; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\URL; class VerifyEmail extends Notification { /** * The callback that should be used to create the verify email URL. * * @var \Closure|null */ public static $createUrlCallback; /** * The callback that should be used to build the mail message. * * @var \Closure|null */ public static $toMailCallback; /** * Get the notification's channels. * * @param mixed $notifiable * @return array|string */ public function via($notifiable) { return ['mail']; } /** * Build the mail representation of the notification. * * @param mixed $notifiable * @return \Illuminate\Notifications\Messages\MailMessage */ public function toMail($notifiable) { $verificationUrl = $this->verificationUrl($notifiable); if (static::$toMailCallback) { return call_user_func(static::$toMailCallback, $notifiable, $verificationUrl); } return $this->buildMailMessage($verificationUrl); } /** * Get the verify email notification mail message for the given URL. * * @param string $url * @return \Illuminate\Notifications\Messages\MailMessage */ protected function buildMailMessage($url) { return (new MailMessage) ->subject(Lang::get('Verify Email Address')) ->line(Lang::get('Please click the button below to verify your email address.')) ->action(Lang::get('Verify Email Address'), $url) ->line(Lang::get('If you did not create an account, no further action is required.')); } /** * Get the verification URL for the given notifiable. * * @param mixed $notifiable * @return string */ protected function verificationUrl($notifiable) { if (static::$createUrlCallback) { return call_user_func(static::$createUrlCallback, $notifiable); } return URL::temporarySignedRoute( 'verification.verify', Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)), [ 'id' => $notifiable->getKey(), 'hash' => sha1($notifiable->getEmailForVerification()), ] ); } /** * Set a callback that should be used when creating the email verification URL. * * @param \Closure $callback * @return void */ public static function createUrlUsing($callback) { static::$createUrlCallback = $callback; } /** * Set a callback that should be used when building the notification mail message. * * @param \Closure $callback * @return void */ public static function toMailUsing($callback) { static::$toMailCallback = $callback; } } src/Illuminate/Auth/LICENSE.md 0000644 00000002063 15107327037 0011751 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Auth/Middleware/RequirePassword.php 0000644 00000005531 15107327037 0016275 0 ustar 00 <?php namespace Illuminate\Auth\Middleware; use Closure; use Illuminate\Contracts\Routing\ResponseFactory; use Illuminate\Contracts\Routing\UrlGenerator; class RequirePassword { /** * The response factory instance. * * @var \Illuminate\Contracts\Routing\ResponseFactory */ protected $responseFactory; /** * The URL generator instance. * * @var \Illuminate\Contracts\Routing\UrlGenerator */ protected $urlGenerator; /** * The password timeout. * * @var int */ protected $passwordTimeout; /** * Create a new middleware instance. * * @param \Illuminate\Contracts\Routing\ResponseFactory $responseFactory * @param \Illuminate\Contracts\Routing\UrlGenerator $urlGenerator * @param int|null $passwordTimeout * @return void */ public function __construct(ResponseFactory $responseFactory, UrlGenerator $urlGenerator, $passwordTimeout = null) { $this->responseFactory = $responseFactory; $this->urlGenerator = $urlGenerator; $this->passwordTimeout = $passwordTimeout ?: 10800; } /** * Specify the redirect route and timeout for the middleware. * * @param string|null $redirectToRoute * @param string|int|null $passwordTimeoutSeconds * @return string * * @named-arguments-supported */ public static function using($redirectToRoute = null, $passwordTimeoutSeconds = null) { return static::class.':'.implode(',', func_get_args()); } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string|null $redirectToRoute * @param string|int|null $passwordTimeoutSeconds * @return mixed */ public function handle($request, Closure $next, $redirectToRoute = null, $passwordTimeoutSeconds = null) { if ($this->shouldConfirmPassword($request, $passwordTimeoutSeconds)) { if ($request->expectsJson()) { return $this->responseFactory->json([ 'message' => 'Password confirmation required.', ], 423); } return $this->responseFactory->redirectGuest( $this->urlGenerator->route($redirectToRoute ?: 'password.confirm') ); } return $next($request); } /** * Determine if the confirmation timeout has expired. * * @param \Illuminate\Http\Request $request * @param int|null $passwordTimeoutSeconds * @return bool */ protected function shouldConfirmPassword($request, $passwordTimeoutSeconds = null) { $confirmedAt = time() - $request->session()->get('auth.password_confirmed_at', 0); return $confirmedAt > ($passwordTimeoutSeconds ?? $this->passwordTimeout); } } src/Illuminate/Auth/Middleware/EnsureEmailIsVerified.php 0000644 00000002257 15107327037 0017323 0 ustar 00 <?php namespace Illuminate\Auth\Middleware; use Closure; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\URL; class EnsureEmailIsVerified { /** * Specify the redirect route for the middleware. * * @param string $route * @return string */ public static function redirectTo($route) { return static::class.':'.$route; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string|null $redirectToRoute * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse|null */ public function handle($request, Closure $next, $redirectToRoute = null) { if (! $request->user() || ($request->user() instanceof MustVerifyEmail && ! $request->user()->hasVerifiedEmail())) { return $request->expectsJson() ? abort(403, 'Your email address is not verified.') : Redirect::guest(URL::route($redirectToRoute ?: 'verification.notice')); } return $next($request); } } src/Illuminate/Auth/Middleware/Authenticate.php 0000644 00000005167 15107327037 0015561 0 ustar 00 <?php namespace Illuminate\Auth\Middleware; use Closure; use Illuminate\Auth\AuthenticationException; use Illuminate\Contracts\Auth\Factory as Auth; use Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests; use Illuminate\Http\Request; class Authenticate implements AuthenticatesRequests { /** * The authentication factory instance. * * @var \Illuminate\Contracts\Auth\Factory */ protected $auth; /** * Create a new middleware instance. * * @param \Illuminate\Contracts\Auth\Factory $auth * @return void */ public function __construct(Auth $auth) { $this->auth = $auth; } /** * Specify the guards for the middleware. * * @param string $guard * @param string $others * @return string */ public static function using($guard, ...$others) { return static::class.':'.implode(',', [$guard, ...$others]); } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string[] ...$guards * @return mixed * * @throws \Illuminate\Auth\AuthenticationException */ public function handle($request, Closure $next, ...$guards) { $this->authenticate($request, $guards); return $next($request); } /** * Determine if the user is logged in to any of the given guards. * * @param \Illuminate\Http\Request $request * @param array $guards * @return void * * @throws \Illuminate\Auth\AuthenticationException */ protected function authenticate($request, array $guards) { if (empty($guards)) { $guards = [null]; } foreach ($guards as $guard) { if ($this->auth->guard($guard)->check()) { return $this->auth->shouldUse($guard); } } $this->unauthenticated($request, $guards); } /** * Handle an unauthenticated user. * * @param \Illuminate\Http\Request $request * @param array $guards * @return void * * @throws \Illuminate\Auth\AuthenticationException */ protected function unauthenticated($request, array $guards) { throw new AuthenticationException( 'Unauthenticated.', $guards, $this->redirectTo($request) ); } /** * Get the path the user should be redirected to when they are not authenticated. * * @param \Illuminate\Http\Request $request * @return string|null */ protected function redirectTo(Request $request) { // } } src/Illuminate/Auth/Middleware/Authorize.php 0000644 00000005142 15107327037 0015106 0 ustar 00 <?php namespace Illuminate\Auth\Middleware; use Closure; use Illuminate\Contracts\Auth\Access\Gate; use Illuminate\Database\Eloquent\Model; class Authorize { /** * The gate instance. * * @var \Illuminate\Contracts\Auth\Access\Gate */ protected $gate; /** * Create a new middleware instance. * * @param \Illuminate\Contracts\Auth\Access\Gate $gate * @return void */ public function __construct(Gate $gate) { $this->gate = $gate; } /** * Specify the ability and models for the middleware. * * @param string $ability * @param string ...$models * @return string */ public static function using($ability, ...$models) { return static::class.':'.implode(',', [$ability, ...$models]); } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string $ability * @param array|null ...$models * @return mixed * * @throws \Illuminate\Auth\AuthenticationException * @throws \Illuminate\Auth\Access\AuthorizationException */ public function handle($request, Closure $next, $ability, ...$models) { $this->gate->authorize($ability, $this->getGateArguments($request, $models)); return $next($request); } /** * Get the arguments parameter for the gate. * * @param \Illuminate\Http\Request $request * @param array|null $models * @return \Illuminate\Database\Eloquent\Model|array|string */ protected function getGateArguments($request, $models) { if (is_null($models)) { return []; } return collect($models)->map(function ($model) use ($request) { return $model instanceof Model ? $model : $this->getModel($request, $model); })->all(); } /** * Get the model to authorize. * * @param \Illuminate\Http\Request $request * @param string $model * @return \Illuminate\Database\Eloquent\Model|string */ protected function getModel($request, $model) { if ($this->isClassName($model)) { return trim($model); } return $request->route($model, null) ?? ((preg_match("/^['\"](.*)['\"]$/", trim($model), $matches)) ? $matches[1] : null); } /** * Checks if the given string looks like a fully qualified class name. * * @param string $value * @return bool */ protected function isClassName($value) { return str_contains($value, '\\'); } } src/Illuminate/Auth/Middleware/AuthenticateWithBasicAuth.php 0000644 00000002531 15107327037 0020171 0 ustar 00 <?php namespace Illuminate\Auth\Middleware; use Closure; use Illuminate\Contracts\Auth\Factory as AuthFactory; class AuthenticateWithBasicAuth { /** * The guard factory instance. * * @var \Illuminate\Contracts\Auth\Factory */ protected $auth; /** * Create a new middleware instance. * * @param \Illuminate\Contracts\Auth\Factory $auth * @return void */ public function __construct(AuthFactory $auth) { $this->auth = $auth; } /** * Specify the guard and field for the middleware. * * @param string|null $guard * @param string|null $field * @return string * * @named-arguments-supported */ public static function using($guard = null, $field = null) { return static::class.':'.implode(',', func_get_args()); } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string|null $guard * @param string|null $field * @return mixed * * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException */ public function handle($request, Closure $next, $guard = null, $field = null) { $this->auth->guard($guard)->basic($field ?: 'email'); return $next($request); } } src/Illuminate/Auth/GenericUser.php 0000644 00000004772 15107327037 0013302 0 ustar 00 <?php namespace Illuminate\Auth; use Illuminate\Contracts\Auth\Authenticatable as UserContract; class GenericUser implements UserContract { /** * All of the user's attributes. * * @var array */ protected $attributes; /** * Create a new generic User object. * * @param array $attributes * @return void */ public function __construct(array $attributes) { $this->attributes = $attributes; } /** * Get the name of the unique identifier for the user. * * @return string */ public function getAuthIdentifierName() { return 'id'; } /** * Get the unique identifier for the user. * * @return mixed */ public function getAuthIdentifier() { return $this->attributes[$this->getAuthIdentifierName()]; } /** * Get the password for the user. * * @return string */ public function getAuthPassword() { return $this->attributes['password']; } /** * Get the "remember me" token value. * * @return string */ public function getRememberToken() { return $this->attributes[$this->getRememberTokenName()]; } /** * Set the "remember me" token value. * * @param string $value * @return void */ public function setRememberToken($value) { $this->attributes[$this->getRememberTokenName()] = $value; } /** * Get the column name for the "remember me" token. * * @return string */ public function getRememberTokenName() { return 'remember_token'; } /** * Dynamically access the user's attributes. * * @param string $key * @return mixed */ public function __get($key) { return $this->attributes[$key]; } /** * Dynamically set an attribute on the user. * * @param string $key * @param mixed $value * @return void */ public function __set($key, $value) { $this->attributes[$key] = $value; } /** * Dynamically check if a value is set on the user. * * @param string $key * @return bool */ public function __isset($key) { return isset($this->attributes[$key]); } /** * Dynamically unset a value on the user. * * @param string $key * @return void */ public function __unset($key) { unset($this->attributes[$key]); } } src/Illuminate/Auth/Access/HandlesAuthorization.php 0000644 00000002506 15107327037 0016420 0 ustar 00 <?php namespace Illuminate\Auth\Access; trait HandlesAuthorization { /** * Create a new access response. * * @param string|null $message * @param mixed $code * @return \Illuminate\Auth\Access\Response */ protected function allow($message = null, $code = null) { return Response::allow($message, $code); } /** * Throws an unauthorized exception. * * @param string|null $message * @param mixed|null $code * @return \Illuminate\Auth\Access\Response */ protected function deny($message = null, $code = null) { return Response::deny($message, $code); } /** * Deny with a HTTP status code. * * @param int $status * @param string|null $message * @param int|null $code * @return \Illuminate\Auth\Access\Response */ public function denyWithStatus($status, $message = null, $code = null) { return Response::denyWithStatus($status, $message, $code); } /** * Deny with a 404 HTTP status code. * * @param string|null $message * @param int|null $code * @return \Illuminate\Auth\Access\Response */ public function denyAsNotFound($message = null, $code = null) { return Response::denyWithStatus(404, $message, $code); } } src/Illuminate/Auth/Access/Events/GateEvaluated.php 0000644 00000001735 15107327037 0016243 0 ustar 00 <?php namespace Illuminate\Auth\Access\Events; class GateEvaluated { /** * The authenticatable model. * * @var \Illuminate\Contracts\Auth\Authenticatable|null */ public $user; /** * The ability being evaluated. * * @var string */ public $ability; /** * The result of the evaluation. * * @var bool|null */ public $result; /** * The arguments given during evaluation. * * @var array */ public $arguments; /** * Create a new event instance. * * @param \Illuminate\Contracts\Auth\Authenticatable|null $user * @param string $ability * @param bool|null $result * @param array $arguments * @return void */ public function __construct($user, $ability, $result, $arguments) { $this->user = $user; $this->ability = $ability; $this->result = $result; $this->arguments = $arguments; } } src/Illuminate/Auth/Access/Gate.php 0000644 00000062127 15107327037 0013146 0 ustar 00 <?php namespace Illuminate\Auth\Access; use Closure; use Exception; use Illuminate\Auth\Access\Events\GateEvaluated; use Illuminate\Contracts\Auth\Access\Gate as GateContract; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Str; use InvalidArgumentException; use ReflectionClass; use ReflectionFunction; class Gate implements GateContract { use HandlesAuthorization; /** * The container instance. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * The user resolver callable. * * @var callable */ protected $userResolver; /** * All of the defined abilities. * * @var array */ protected $abilities = []; /** * All of the defined policies. * * @var array */ protected $policies = []; /** * All of the registered before callbacks. * * @var array */ protected $beforeCallbacks = []; /** * All of the registered after callbacks. * * @var array */ protected $afterCallbacks = []; /** * All of the defined abilities using class@method notation. * * @var array */ protected $stringCallbacks = []; /** * The default denial response for gates and policies. * * @var \Illuminate\Auth\Access\Response|null */ protected $defaultDenialResponse; /** * The callback to be used to guess policy names. * * @var callable|null */ protected $guessPolicyNamesUsingCallback; /** * Create a new gate instance. * * @param \Illuminate\Contracts\Container\Container $container * @param callable $userResolver * @param array $abilities * @param array $policies * @param array $beforeCallbacks * @param array $afterCallbacks * @param callable|null $guessPolicyNamesUsingCallback * @return void */ public function __construct(Container $container, callable $userResolver, array $abilities = [], array $policies = [], array $beforeCallbacks = [], array $afterCallbacks = [], callable $guessPolicyNamesUsingCallback = null) { $this->policies = $policies; $this->container = $container; $this->abilities = $abilities; $this->userResolver = $userResolver; $this->afterCallbacks = $afterCallbacks; $this->beforeCallbacks = $beforeCallbacks; $this->guessPolicyNamesUsingCallback = $guessPolicyNamesUsingCallback; } /** * Determine if a given ability has been defined. * * @param string|array $ability * @return bool */ public function has($ability) { $abilities = is_array($ability) ? $ability : func_get_args(); foreach ($abilities as $ability) { if (! isset($this->abilities[$ability])) { return false; } } return true; } /** * Perform an on-demand authorization check. Throw an authorization exception if the condition or callback is false. * * @param \Illuminate\Auth\Access\Response|\Closure|bool $condition * @param string|null $message * @param string|null $code * @return \Illuminate\Auth\Access\Response * * @throws \Illuminate\Auth\Access\AuthorizationException */ public function allowIf($condition, $message = null, $code = null) { return $this->authorizeOnDemand($condition, $message, $code, true); } /** * Perform an on-demand authorization check. Throw an authorization exception if the condition or callback is true. * * @param \Illuminate\Auth\Access\Response|\Closure|bool $condition * @param string|null $message * @param string|null $code * @return \Illuminate\Auth\Access\Response * * @throws \Illuminate\Auth\Access\AuthorizationException */ public function denyIf($condition, $message = null, $code = null) { return $this->authorizeOnDemand($condition, $message, $code, false); } /** * Authorize a given condition or callback. * * @param \Illuminate\Auth\Access\Response|\Closure|bool $condition * @param string|null $message * @param string|null $code * @param bool $allowWhenResponseIs * @return \Illuminate\Auth\Access\Response * * @throws \Illuminate\Auth\Access\AuthorizationException */ protected function authorizeOnDemand($condition, $message, $code, $allowWhenResponseIs) { $user = $this->resolveUser(); if ($condition instanceof Closure) { $response = $this->canBeCalledWithUser($user, $condition) ? $condition($user) : new Response(false, $message, $code); } else { $response = $condition; } return with($response instanceof Response ? $response : new Response( (bool) $response === $allowWhenResponseIs, $message, $code ))->authorize(); } /** * Define a new ability. * * @param string $ability * @param callable|array|string $callback * @return $this * * @throws \InvalidArgumentException */ public function define($ability, $callback) { if (is_array($callback) && isset($callback[0]) && is_string($callback[0])) { $callback = $callback[0].'@'.$callback[1]; } if (is_callable($callback)) { $this->abilities[$ability] = $callback; } elseif (is_string($callback)) { $this->stringCallbacks[$ability] = $callback; $this->abilities[$ability] = $this->buildAbilityCallback($ability, $callback); } else { throw new InvalidArgumentException("Callback must be a callable, callback array, or a 'Class@method' string."); } return $this; } /** * Define abilities for a resource. * * @param string $name * @param string $class * @param array|null $abilities * @return $this */ public function resource($name, $class, array $abilities = null) { $abilities = $abilities ?: [ 'viewAny' => 'viewAny', 'view' => 'view', 'create' => 'create', 'update' => 'update', 'delete' => 'delete', ]; foreach ($abilities as $ability => $method) { $this->define($name.'.'.$ability, $class.'@'.$method); } return $this; } /** * Create the ability callback for a callback string. * * @param string $ability * @param string $callback * @return \Closure */ protected function buildAbilityCallback($ability, $callback) { return function () use ($ability, $callback) { if (str_contains($callback, '@')) { [$class, $method] = Str::parseCallback($callback); } else { $class = $callback; } $policy = $this->resolvePolicy($class); $arguments = func_get_args(); $user = array_shift($arguments); $result = $this->callPolicyBefore( $policy, $user, $ability, $arguments ); if (! is_null($result)) { return $result; } return isset($method) ? $policy->{$method}(...func_get_args()) : $policy(...func_get_args()); }; } /** * Define a policy class for a given class type. * * @param string $class * @param string $policy * @return $this */ public function policy($class, $policy) { $this->policies[$class] = $policy; return $this; } /** * Register a callback to run before all Gate checks. * * @param callable $callback * @return $this */ public function before(callable $callback) { $this->beforeCallbacks[] = $callback; return $this; } /** * Register a callback to run after all Gate checks. * * @param callable $callback * @return $this */ public function after(callable $callback) { $this->afterCallbacks[] = $callback; return $this; } /** * Determine if the given ability should be granted for the current user. * * @param string $ability * @param array|mixed $arguments * @return bool */ public function allows($ability, $arguments = []) { return $this->check($ability, $arguments); } /** * Determine if the given ability should be denied for the current user. * * @param string $ability * @param array|mixed $arguments * @return bool */ public function denies($ability, $arguments = []) { return ! $this->allows($ability, $arguments); } /** * Determine if all of the given abilities should be granted for the current user. * * @param iterable|string $abilities * @param array|mixed $arguments * @return bool */ public function check($abilities, $arguments = []) { return collect($abilities)->every( fn ($ability) => $this->inspect($ability, $arguments)->allowed() ); } /** * Determine if any one of the given abilities should be granted for the current user. * * @param iterable|string $abilities * @param array|mixed $arguments * @return bool */ public function any($abilities, $arguments = []) { return collect($abilities)->contains(fn ($ability) => $this->check($ability, $arguments)); } /** * Determine if all of the given abilities should be denied for the current user. * * @param iterable|string $abilities * @param array|mixed $arguments * @return bool */ public function none($abilities, $arguments = []) { return ! $this->any($abilities, $arguments); } /** * Determine if the given ability should be granted for the current user. * * @param string $ability * @param array|mixed $arguments * @return \Illuminate\Auth\Access\Response * * @throws \Illuminate\Auth\Access\AuthorizationException */ public function authorize($ability, $arguments = []) { return $this->inspect($ability, $arguments)->authorize(); } /** * Inspect the user for the given ability. * * @param string $ability * @param array|mixed $arguments * @return \Illuminate\Auth\Access\Response */ public function inspect($ability, $arguments = []) { try { $result = $this->raw($ability, $arguments); if ($result instanceof Response) { return $result; } return $result ? Response::allow() : ($this->defaultDenialResponse ?? Response::deny()); } catch (AuthorizationException $e) { return $e->toResponse(); } } /** * Get the raw result from the authorization callback. * * @param string $ability * @param array|mixed $arguments * @return mixed * * @throws \Illuminate\Auth\Access\AuthorizationException */ public function raw($ability, $arguments = []) { $arguments = Arr::wrap($arguments); $user = $this->resolveUser(); // First we will call the "before" callbacks for the Gate. If any of these give // back a non-null response, we will immediately return that result in order // to let the developers override all checks for some authorization cases. $result = $this->callBeforeCallbacks( $user, $ability, $arguments ); if (is_null($result)) { $result = $this->callAuthCallback($user, $ability, $arguments); } // After calling the authorization callback, we will call the "after" callbacks // that are registered with the Gate, which allows a developer to do logging // if that is required for this application. Then we'll return the result. return tap($this->callAfterCallbacks( $user, $ability, $arguments, $result ), function ($result) use ($user, $ability, $arguments) { $this->dispatchGateEvaluatedEvent($user, $ability, $arguments, $result); }); } /** * Determine whether the callback/method can be called with the given user. * * @param \Illuminate\Contracts\Auth\Authenticatable|null $user * @param \Closure|string|array $class * @param string|null $method * @return bool */ protected function canBeCalledWithUser($user, $class, $method = null) { if (! is_null($user)) { return true; } if (! is_null($method)) { return $this->methodAllowsGuests($class, $method); } if (is_array($class)) { $className = is_string($class[0]) ? $class[0] : get_class($class[0]); return $this->methodAllowsGuests($className, $class[1]); } return $this->callbackAllowsGuests($class); } /** * Determine if the given class method allows guests. * * @param string $class * @param string $method * @return bool */ protected function methodAllowsGuests($class, $method) { try { $reflection = new ReflectionClass($class); $method = $reflection->getMethod($method); } catch (Exception) { return false; } if ($method) { $parameters = $method->getParameters(); return isset($parameters[0]) && $this->parameterAllowsGuests($parameters[0]); } return false; } /** * Determine if the callback allows guests. * * @param callable $callback * @return bool * * @throws \ReflectionException */ protected function callbackAllowsGuests($callback) { $parameters = (new ReflectionFunction($callback))->getParameters(); return isset($parameters[0]) && $this->parameterAllowsGuests($parameters[0]); } /** * Determine if the given parameter allows guests. * * @param \ReflectionParameter $parameter * @return bool */ protected function parameterAllowsGuests($parameter) { return ($parameter->hasType() && $parameter->allowsNull()) || ($parameter->isDefaultValueAvailable() && is_null($parameter->getDefaultValue())); } /** * Resolve and call the appropriate authorization callback. * * @param \Illuminate\Contracts\Auth\Authenticatable|null $user * @param string $ability * @param array $arguments * @return bool */ protected function callAuthCallback($user, $ability, array $arguments) { $callback = $this->resolveAuthCallback($user, $ability, $arguments); return $callback($user, ...$arguments); } /** * Call all of the before callbacks and return if a result is given. * * @param \Illuminate\Contracts\Auth\Authenticatable|null $user * @param string $ability * @param array $arguments * @return bool|null */ protected function callBeforeCallbacks($user, $ability, array $arguments) { foreach ($this->beforeCallbacks as $before) { if (! $this->canBeCalledWithUser($user, $before)) { continue; } if (! is_null($result = $before($user, $ability, $arguments))) { return $result; } } } /** * Call all of the after callbacks with check result. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param string $ability * @param array $arguments * @param bool $result * @return bool|null */ protected function callAfterCallbacks($user, $ability, array $arguments, $result) { foreach ($this->afterCallbacks as $after) { if (! $this->canBeCalledWithUser($user, $after)) { continue; } $afterResult = $after($user, $ability, $result, $arguments); $result ??= $afterResult; } return $result; } /** * Dispatch a gate evaluation event. * * @param \Illuminate\Contracts\Auth\Authenticatable|null $user * @param string $ability * @param array $arguments * @param bool|null $result * @return void */ protected function dispatchGateEvaluatedEvent($user, $ability, array $arguments, $result) { if ($this->container->bound(Dispatcher::class)) { $this->container->make(Dispatcher::class)->dispatch( new GateEvaluated($user, $ability, $result, $arguments) ); } } /** * Resolve the callable for the given ability and arguments. * * @param \Illuminate\Contracts\Auth\Authenticatable|null $user * @param string $ability * @param array $arguments * @return callable */ protected function resolveAuthCallback($user, $ability, array $arguments) { if (isset($arguments[0]) && ! is_null($policy = $this->getPolicyFor($arguments[0])) && $callback = $this->resolvePolicyCallback($user, $ability, $arguments, $policy)) { return $callback; } if (isset($this->stringCallbacks[$ability])) { [$class, $method] = Str::parseCallback($this->stringCallbacks[$ability]); if ($this->canBeCalledWithUser($user, $class, $method ?: '__invoke')) { return $this->abilities[$ability]; } } if (isset($this->abilities[$ability]) && $this->canBeCalledWithUser($user, $this->abilities[$ability])) { return $this->abilities[$ability]; } return function () { // }; } /** * Get a policy instance for a given class. * * @param object|string $class * @return mixed */ public function getPolicyFor($class) { if (is_object($class)) { $class = get_class($class); } if (! is_string($class)) { return; } if (isset($this->policies[$class])) { return $this->resolvePolicy($this->policies[$class]); } foreach ($this->guessPolicyName($class) as $guessedPolicy) { if (class_exists($guessedPolicy)) { return $this->resolvePolicy($guessedPolicy); } } foreach ($this->policies as $expected => $policy) { if (is_subclass_of($class, $expected)) { return $this->resolvePolicy($policy); } } } /** * Guess the policy name for the given class. * * @param string $class * @return array */ protected function guessPolicyName($class) { if ($this->guessPolicyNamesUsingCallback) { return Arr::wrap(call_user_func($this->guessPolicyNamesUsingCallback, $class)); } $classDirname = str_replace('/', '\\', dirname(str_replace('\\', '/', $class))); $classDirnameSegments = explode('\\', $classDirname); return Arr::wrap(Collection::times(count($classDirnameSegments), function ($index) use ($class, $classDirnameSegments) { $classDirname = implode('\\', array_slice($classDirnameSegments, 0, $index)); return $classDirname.'\\Policies\\'.class_basename($class).'Policy'; })->reverse()->values()->first(function ($class) { return class_exists($class); }) ?: [$classDirname.'\\Policies\\'.class_basename($class).'Policy']); } /** * Specify a callback to be used to guess policy names. * * @param callable $callback * @return $this */ public function guessPolicyNamesUsing(callable $callback) { $this->guessPolicyNamesUsingCallback = $callback; return $this; } /** * Build a policy class instance of the given type. * * @param object|string $class * @return mixed * * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function resolvePolicy($class) { return $this->container->make($class); } /** * Resolve the callback for a policy check. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param string $ability * @param array $arguments * @param mixed $policy * @return bool|callable */ protected function resolvePolicyCallback($user, $ability, array $arguments, $policy) { if (! is_callable([$policy, $this->formatAbilityToMethod($ability)])) { return false; } return function () use ($user, $ability, $arguments, $policy) { // This callback will be responsible for calling the policy's before method and // running this policy method if necessary. This is used to when objects are // mapped to policy objects in the user's configurations or on this class. $result = $this->callPolicyBefore( $policy, $user, $ability, $arguments ); // When we receive a non-null result from this before method, we will return it // as the "final" results. This will allow developers to override the checks // in this policy to return the result for all rules defined in the class. if (! is_null($result)) { return $result; } $method = $this->formatAbilityToMethod($ability); return $this->callPolicyMethod($policy, $method, $user, $arguments); }; } /** * Call the "before" method on the given policy, if applicable. * * @param mixed $policy * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param string $ability * @param array $arguments * @return mixed */ protected function callPolicyBefore($policy, $user, $ability, $arguments) { if (! method_exists($policy, 'before')) { return; } if ($this->canBeCalledWithUser($user, $policy, 'before')) { return $policy->before($user, $ability, ...$arguments); } } /** * Call the appropriate method on the given policy. * * @param mixed $policy * @param string $method * @param \Illuminate\Contracts\Auth\Authenticatable|null $user * @param array $arguments * @return mixed */ protected function callPolicyMethod($policy, $method, $user, array $arguments) { // If this first argument is a string, that means they are passing a class name // to the policy. We will remove the first argument from this argument array // because this policy already knows what type of models it can authorize. if (isset($arguments[0]) && is_string($arguments[0])) { array_shift($arguments); } if (! is_callable([$policy, $method])) { return; } if ($this->canBeCalledWithUser($user, $policy, $method)) { return $policy->{$method}($user, ...$arguments); } } /** * Format the policy ability into a method name. * * @param string $ability * @return string */ protected function formatAbilityToMethod($ability) { return str_contains($ability, '-') ? Str::camel($ability) : $ability; } /** * Get a gate instance for the given user. * * @param \Illuminate\Contracts\Auth\Authenticatable|mixed $user * @return static */ public function forUser($user) { $callback = fn () => $user; return new static( $this->container, $callback, $this->abilities, $this->policies, $this->beforeCallbacks, $this->afterCallbacks, $this->guessPolicyNamesUsingCallback ); } /** * Resolve the user from the user resolver. * * @return mixed */ protected function resolveUser() { return call_user_func($this->userResolver); } /** * Get all of the defined abilities. * * @return array */ public function abilities() { return $this->abilities; } /** * Get all of the defined policies. * * @return array */ public function policies() { return $this->policies; } /** * Set the default denial response for gates and policies. * * @param \Illuminate\Auth\Access\Response $response * @return $this */ public function defaultDenialResponse(Response $response) { $this->defaultDenialResponse = $response; return $this; } /** * Set the container instance used by the gate. * * @param \Illuminate\Contracts\Container\Container $container * @return $this */ public function setContainer(Container $container) { $this->container = $container; return $this; } } src/Illuminate/Auth/Access/Response.php 0000644 00000010404 15107327037 0014053 0 ustar 00 <?php namespace Illuminate\Auth\Access; use Illuminate\Contracts\Support\Arrayable; class Response implements Arrayable { /** * Indicates whether the response was allowed. * * @var bool */ protected $allowed; /** * The response message. * * @var string|null */ protected $message; /** * The response code. * * @var mixed */ protected $code; /** * The HTTP response status code. * * @var int|null */ protected $status; /** * Create a new response. * * @param bool $allowed * @param string|null $message * @param mixed $code * @return void */ public function __construct($allowed, $message = '', $code = null) { $this->code = $code; $this->allowed = $allowed; $this->message = $message; } /** * Create a new "allow" Response. * * @param string|null $message * @param mixed $code * @return \Illuminate\Auth\Access\Response */ public static function allow($message = null, $code = null) { return new static(true, $message, $code); } /** * Create a new "deny" Response. * * @param string|null $message * @param mixed $code * @return \Illuminate\Auth\Access\Response */ public static function deny($message = null, $code = null) { return new static(false, $message, $code); } /** * Create a new "deny" Response with a HTTP status code. * * @param int $status * @param string|null $message * @param mixed $code * @return \Illuminate\Auth\Access\Response */ public static function denyWithStatus($status, $message = null, $code = null) { return static::deny($message, $code)->withStatus($status); } /** * Create a new "deny" Response with a 404 HTTP status code. * * @param string|null $message * @param mixed $code * @return \Illuminate\Auth\Access\Response */ public static function denyAsNotFound($message = null, $code = null) { return static::denyWithStatus(404, $message, $code); } /** * Determine if the response was allowed. * * @return bool */ public function allowed() { return $this->allowed; } /** * Determine if the response was denied. * * @return bool */ public function denied() { return ! $this->allowed(); } /** * Get the response message. * * @return string|null */ public function message() { return $this->message; } /** * Get the response code / reason. * * @return mixed */ public function code() { return $this->code; } /** * Throw authorization exception if response was denied. * * @return \Illuminate\Auth\Access\Response * * @throws \Illuminate\Auth\Access\AuthorizationException */ public function authorize() { if ($this->denied()) { throw (new AuthorizationException($this->message(), $this->code())) ->setResponse($this) ->withStatus($this->status); } return $this; } /** * Set the HTTP response status code. * * @param null|int $status * @return $this */ public function withStatus($status) { $this->status = $status; return $this; } /** * Set the HTTP response status code to 404. * * @return $this */ public function asNotFound() { return $this->withStatus(404); } /** * Get the HTTP status code. * * @return int|null */ public function status() { return $this->status; } /** * Convert the response to an array. * * @return array */ public function toArray() { return [ 'allowed' => $this->allowed(), 'message' => $this->message(), 'code' => $this->code(), ]; } /** * Get the string representation of the message. * * @return string */ public function __toString() { return (string) $this->message(); } } src/Illuminate/Auth/Access/AuthorizationException.php 0000644 00000004272 15107327037 0017002 0 ustar 00 <?php namespace Illuminate\Auth\Access; use Exception; use Throwable; class AuthorizationException extends Exception { /** * The response from the gate. * * @var \Illuminate\Auth\Access\Response */ protected $response; /** * The HTTP response status code. * * @var int|null */ protected $status; /** * Create a new authorization exception instance. * * @param string|null $message * @param mixed $code * @param \Throwable|null $previous * @return void */ public function __construct($message = null, $code = null, Throwable $previous = null) { parent::__construct($message ?? 'This action is unauthorized.', 0, $previous); $this->code = $code ?: 0; } /** * Get the response from the gate. * * @return \Illuminate\Auth\Access\Response */ public function response() { return $this->response; } /** * Set the response from the gate. * * @param \Illuminate\Auth\Access\Response $response * @return $this */ public function setResponse($response) { $this->response = $response; return $this; } /** * Set the HTTP response status code. * * @param int|null $status * @return $this */ public function withStatus($status) { $this->status = $status; return $this; } /** * Set the HTTP response status code to 404. * * @return $this */ public function asNotFound() { return $this->withStatus(404); } /** * Determine if the HTTP status code has been set. * * @return bool */ public function hasStatus() { return $this->status !== null; } /** * Get the HTTP status code. * * @return int|null */ public function status() { return $this->status; } /** * Create a deny response object from this exception. * * @return \Illuminate\Auth\Access\Response */ public function toResponse() { return Response::deny($this->message, $this->code)->withStatus($this->status); } } src/Illuminate/Auth/AuthServiceProvider.php 0000644 00000006046 15107327037 0015020 0 ustar 00 <?php namespace Illuminate\Auth; use Illuminate\Auth\Access\Gate; use Illuminate\Auth\Middleware\RequirePassword; use Illuminate\Contracts\Auth\Access\Gate as GateContract; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Routing\ResponseFactory; use Illuminate\Contracts\Routing\UrlGenerator; use Illuminate\Support\ServiceProvider; class AuthServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { $this->registerAuthenticator(); $this->registerUserResolver(); $this->registerAccessGate(); $this->registerRequirePassword(); $this->registerRequestRebindHandler(); $this->registerEventRebindHandler(); } /** * Register the authenticator services. * * @return void */ protected function registerAuthenticator() { $this->app->singleton('auth', fn ($app) => new AuthManager($app)); $this->app->singleton('auth.driver', fn ($app) => $app['auth']->guard()); } /** * Register a resolver for the authenticated user. * * @return void */ protected function registerUserResolver() { $this->app->bind(AuthenticatableContract::class, fn ($app) => call_user_func($app['auth']->userResolver())); } /** * Register the access gate service. * * @return void */ protected function registerAccessGate() { $this->app->singleton(GateContract::class, function ($app) { return new Gate($app, fn () => call_user_func($app['auth']->userResolver())); }); } /** * Register a resolver for the authenticated user. * * @return void */ protected function registerRequirePassword() { $this->app->bind(RequirePassword::class, function ($app) { return new RequirePassword( $app[ResponseFactory::class], $app[UrlGenerator::class], $app['config']->get('auth.password_timeout') ); }); } /** * Handle the re-binding of the request binding. * * @return void */ protected function registerRequestRebindHandler() { $this->app->rebinding('request', function ($app, $request) { $request->setUserResolver(function ($guard = null) use ($app) { return call_user_func($app['auth']->userResolver(), $guard); }); }); } /** * Handle the re-binding of the event dispatcher binding. * * @return void */ protected function registerEventRebindHandler() { $this->app->rebinding('events', function ($app, $dispatcher) { if (! $app->resolved('auth') || $app['auth']->hasResolvedGuards() === false) { return; } if (method_exists($guard = $app['auth']->guard(), 'setDispatcher')) { $guard->setDispatcher($dispatcher); } }); } } src/Illuminate/Session/TokenMismatchException.php 0000644 00000000160 15107327037 0016221 0 ustar 00 <?php namespace Illuminate\Session; use Exception; class TokenMismatchException extends Exception { // } src/Illuminate/Session/SessionManager.php 0000644 00000016146 15107327037 0014525 0 ustar 00 <?php namespace Illuminate\Session; use Illuminate\Support\Manager; /** * @mixin \Illuminate\Session\Store */ class SessionManager extends Manager { /** * Call a custom driver creator. * * @param string $driver * @return \Illuminate\Session\Store */ protected function callCustomCreator($driver) { return $this->buildSession(parent::callCustomCreator($driver)); } /** * Create an instance of the "null" session driver. * * @return \Illuminate\Session\Store */ protected function createNullDriver() { return $this->buildSession(new NullSessionHandler); } /** * Create an instance of the "array" session driver. * * @return \Illuminate\Session\Store */ protected function createArrayDriver() { return $this->buildSession(new ArraySessionHandler( $this->config->get('session.lifetime') )); } /** * Create an instance of the "cookie" session driver. * * @return \Illuminate\Session\Store */ protected function createCookieDriver() { return $this->buildSession(new CookieSessionHandler( $this->container->make('cookie'), $this->config->get('session.lifetime'), $this->config->get('session.expire_on_close') )); } /** * Create an instance of the file session driver. * * @return \Illuminate\Session\Store */ protected function createFileDriver() { return $this->createNativeDriver(); } /** * Create an instance of the file session driver. * * @return \Illuminate\Session\Store */ protected function createNativeDriver() { $lifetime = $this->config->get('session.lifetime'); return $this->buildSession(new FileSessionHandler( $this->container->make('files'), $this->config->get('session.files'), $lifetime )); } /** * Create an instance of the database session driver. * * @return \Illuminate\Session\Store */ protected function createDatabaseDriver() { $table = $this->config->get('session.table'); $lifetime = $this->config->get('session.lifetime'); return $this->buildSession(new DatabaseSessionHandler( $this->getDatabaseConnection(), $table, $lifetime, $this->container )); } /** * Get the database connection for the database driver. * * @return \Illuminate\Database\Connection */ protected function getDatabaseConnection() { $connection = $this->config->get('session.connection'); return $this->container->make('db')->connection($connection); } /** * Create an instance of the APC session driver. * * @return \Illuminate\Session\Store */ protected function createApcDriver() { return $this->createCacheBased('apc'); } /** * Create an instance of the Memcached session driver. * * @return \Illuminate\Session\Store */ protected function createMemcachedDriver() { return $this->createCacheBased('memcached'); } /** * Create an instance of the Redis session driver. * * @return \Illuminate\Session\Store */ protected function createRedisDriver() { $handler = $this->createCacheHandler('redis'); $handler->getCache()->getStore()->setConnection( $this->config->get('session.connection') ); return $this->buildSession($handler); } /** * Create an instance of the DynamoDB session driver. * * @return \Illuminate\Session\Store */ protected function createDynamodbDriver() { return $this->createCacheBased('dynamodb'); } /** * Create an instance of a cache driven driver. * * @param string $driver * @return \Illuminate\Session\Store */ protected function createCacheBased($driver) { return $this->buildSession($this->createCacheHandler($driver)); } /** * Create the cache based session handler instance. * * @param string $driver * @return \Illuminate\Session\CacheBasedSessionHandler */ protected function createCacheHandler($driver) { $store = $this->config->get('session.store') ?: $driver; return new CacheBasedSessionHandler( clone $this->container->make('cache')->store($store), $this->config->get('session.lifetime') ); } /** * Build the session instance. * * @param \SessionHandlerInterface $handler * @return \Illuminate\Session\Store */ protected function buildSession($handler) { return $this->config->get('session.encrypt') ? $this->buildEncryptedSession($handler) : new Store( $this->config->get('session.cookie'), $handler, $id = null, $this->config->get('session.serialization', 'php') ); } /** * Build the encrypted session instance. * * @param \SessionHandlerInterface $handler * @return \Illuminate\Session\EncryptedStore */ protected function buildEncryptedSession($handler) { return new EncryptedStore( $this->config->get('session.cookie'), $handler, $this->container['encrypter'], $id = null, $this->config->get('session.serialization', 'php'), ); } /** * Determine if requests for the same session should wait for each to finish before executing. * * @return bool */ public function shouldBlock() { return $this->config->get('session.block', false); } /** * Get the name of the cache store / driver that should be used to acquire session locks. * * @return string|null */ public function blockDriver() { return $this->config->get('session.block_store'); } /** * Get the maximum number of seconds the session lock should be held for. * * @return int */ public function defaultRouteBlockLockSeconds() { return $this->config->get('session.block_lock_seconds', 10); } /** * Get the maximum number of seconds to wait while attempting to acquire a route block session lock. * * @return int */ public function defaultRouteBlockWaitSeconds() { return $this->config->get('session.block_wait_seconds', 10); } /** * Get the session configuration. * * @return array */ public function getSessionConfig() { return $this->config->get('session'); } /** * Get the default session driver name. * * @return string */ public function getDefaultDriver() { return $this->config->get('session.driver'); } /** * Set the default session driver name. * * @param string $name * @return void */ public function setDefaultDriver($name) { $this->config->set('session.driver', $name); } } src/Illuminate/Session/Console/SessionTableCommand.php 0000644 00000001571 15107327037 0017077 0 ustar 00 <?php namespace Illuminate\Session\Console; use Illuminate\Console\MigrationGeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'session:table')] class SessionTableCommand extends MigrationGeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'session:table'; /** * The console command description. * * @var string */ protected $description = 'Create a migration for the session database table'; /** * Get the migration table name. * * @return string */ protected function migrationTableName() { return 'sessions'; } /** * Get the path to the migration stub file. * * @return string */ protected function migrationStubFile() { return __DIR__.'/stubs/database.stub'; } } src/Illuminate/Session/Console/stubs/database.stub 0000644 00000001423 15107327037 0016273 0 ustar 00 <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('sessions', function (Blueprint $table) { $table->string('id')->primary(); $table->foreignId('user_id')->nullable()->index(); $table->string('ip_address', 45)->nullable(); $table->text('user_agent')->nullable(); $table->longText('payload'); $table->integer('last_activity')->index(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('sessions'); } }; src/Illuminate/Session/SessionServiceProvider.php 0000644 00000002721 15107327037 0016260 0 ustar 00 <?php namespace Illuminate\Session; use Illuminate\Contracts\Cache\Factory as CacheFactory; use Illuminate\Session\Middleware\StartSession; use Illuminate\Support\ServiceProvider; class SessionServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { $this->registerSessionManager(); $this->registerSessionDriver(); $this->app->singleton(StartSession::class, function ($app) { return new StartSession($app->make(SessionManager::class), function () use ($app) { return $app->make(CacheFactory::class); }); }); } /** * Register the session manager instance. * * @return void */ protected function registerSessionManager() { $this->app->singleton('session', function ($app) { return new SessionManager($app); }); } /** * Register the session driver instance. * * @return void */ protected function registerSessionDriver() { $this->app->singleton('session.store', function ($app) { // First, we will create the session manager which is responsible for the // creation of the various session drivers when they are needed by the // application instance, and will resolve them on a lazy load basis. return $app->make('session')->driver(); }); } } src/Illuminate/Session/DatabaseSessionHandler.php 0000644 00000015733 15107327037 0016156 0 ustar 00 <?php namespace Illuminate\Session; use Illuminate\Contracts\Auth\Guard; use Illuminate\Contracts\Container\Container; use Illuminate\Database\ConnectionInterface; use Illuminate\Database\QueryException; use Illuminate\Support\Arr; use Illuminate\Support\Carbon; use Illuminate\Support\InteractsWithTime; use SessionHandlerInterface; class DatabaseSessionHandler implements ExistenceAwareInterface, SessionHandlerInterface { use InteractsWithTime; /** * The database connection instance. * * @var \Illuminate\Database\ConnectionInterface */ protected $connection; /** * The name of the session table. * * @var string */ protected $table; /** * The number of minutes the session should be valid. * * @var int */ protected $minutes; /** * The container instance. * * @var \Illuminate\Contracts\Container\Container|null */ protected $container; /** * The existence state of the session. * * @var bool */ protected $exists; /** * Create a new database session handler instance. * * @param \Illuminate\Database\ConnectionInterface $connection * @param string $table * @param int $minutes * @param \Illuminate\Contracts\Container\Container|null $container * @return void */ public function __construct(ConnectionInterface $connection, $table, $minutes, Container $container = null) { $this->table = $table; $this->minutes = $minutes; $this->container = $container; $this->connection = $connection; } /** * {@inheritdoc} * * @return bool */ public function open($savePath, $sessionName): bool { return true; } /** * {@inheritdoc} * * @return bool */ public function close(): bool { return true; } /** * {@inheritdoc} * * @return string|false */ public function read($sessionId): string|false { $session = (object) $this->getQuery()->find($sessionId); if ($this->expired($session)) { $this->exists = true; return ''; } if (isset($session->payload)) { $this->exists = true; return base64_decode($session->payload); } return ''; } /** * Determine if the session is expired. * * @param \stdClass $session * @return bool */ protected function expired($session) { return isset($session->last_activity) && $session->last_activity < Carbon::now()->subMinutes($this->minutes)->getTimestamp(); } /** * {@inheritdoc} * * @return bool */ public function write($sessionId, $data): bool { $payload = $this->getDefaultPayload($data); if (! $this->exists) { $this->read($sessionId); } if ($this->exists) { $this->performUpdate($sessionId, $payload); } else { $this->performInsert($sessionId, $payload); } return $this->exists = true; } /** * Perform an insert operation on the session ID. * * @param string $sessionId * @param array<string, mixed> $payload * @return bool|null */ protected function performInsert($sessionId, $payload) { try { return $this->getQuery()->insert(Arr::set($payload, 'id', $sessionId)); } catch (QueryException) { $this->performUpdate($sessionId, $payload); } } /** * Perform an update operation on the session ID. * * @param string $sessionId * @param array<string, mixed> $payload * @return int */ protected function performUpdate($sessionId, $payload) { return $this->getQuery()->where('id', $sessionId)->update($payload); } /** * Get the default payload for the session. * * @param string $data * @return array */ protected function getDefaultPayload($data) { $payload = [ 'payload' => base64_encode($data), 'last_activity' => $this->currentTime(), ]; if (! $this->container) { return $payload; } return tap($payload, function (&$payload) { $this->addUserInformation($payload) ->addRequestInformation($payload); }); } /** * Add the user information to the session payload. * * @param array $payload * @return $this */ protected function addUserInformation(&$payload) { if ($this->container->bound(Guard::class)) { $payload['user_id'] = $this->userId(); } return $this; } /** * Get the currently authenticated user's ID. * * @return mixed */ protected function userId() { return $this->container->make(Guard::class)->id(); } /** * Add the request information to the session payload. * * @param array $payload * @return $this */ protected function addRequestInformation(&$payload) { if ($this->container->bound('request')) { $payload = array_merge($payload, [ 'ip_address' => $this->ipAddress(), 'user_agent' => $this->userAgent(), ]); } return $this; } /** * Get the IP address for the current request. * * @return string|null */ protected function ipAddress() { return $this->container->make('request')->ip(); } /** * Get the user agent for the current request. * * @return string */ protected function userAgent() { return substr((string) $this->container->make('request')->header('User-Agent'), 0, 500); } /** * {@inheritdoc} * * @return bool */ public function destroy($sessionId): bool { $this->getQuery()->where('id', $sessionId)->delete(); return true; } /** * {@inheritdoc} * * @return int */ public function gc($lifetime): int { return $this->getQuery()->where('last_activity', '<=', $this->currentTime() - $lifetime)->delete(); } /** * Get a fresh query builder instance for the table. * * @return \Illuminate\Database\Query\Builder */ protected function getQuery() { return $this->connection->table($this->table); } /** * Set the application instance used by the handler. * * @param \Illuminate\Contracts\Foundation\Application $container * @return $this */ public function setContainer($container) { $this->container = $container; return $this; } /** * Set the existence state for the session. * * @param bool $value * @return $this */ public function setExists($value) { $this->exists = $value; return $this; } } src/Illuminate/Session/EncryptedStore.php 0000644 00000003354 15107327037 0014556 0 ustar 00 <?php namespace Illuminate\Session; use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract; use SessionHandlerInterface; class EncryptedStore extends Store { /** * The encrypter instance. * * @var \Illuminate\Contracts\Encryption\Encrypter */ protected $encrypter; /** * Create a new session instance. * * @param string $name * @param \SessionHandlerInterface $handler * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter * @param string|null $id * @param string $serialization * @return void */ public function __construct($name, SessionHandlerInterface $handler, EncrypterContract $encrypter, $id = null, $serialization = 'php') { $this->encrypter = $encrypter; parent::__construct($name, $handler, $id, $serialization); } /** * Prepare the raw string data from the session for unserialization. * * @param string $data * @return string */ protected function prepareForUnserialize($data) { try { return $this->encrypter->decrypt($data); } catch (DecryptException) { return $this->serialization === 'json' ? json_encode([]) : serialize([]); } } /** * Prepare the serialized session data for storage. * * @param string $data * @return string */ protected function prepareForStorage($data) { return $this->encrypter->encrypt($data); } /** * Get the encrypter instance. * * @return \Illuminate\Contracts\Encryption\Encrypter */ public function getEncrypter() { return $this->encrypter; } } src/Illuminate/Session/Store.php 0000644 00000040070 15107327037 0012674 0 ustar 00 <?php namespace Illuminate\Session; use Closure; use Illuminate\Contracts\Session\Session; use Illuminate\Support\Arr; use Illuminate\Support\MessageBag; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use Illuminate\Support\ViewErrorBag; use SessionHandlerInterface; use stdClass; class Store implements Session { use Macroable; /** * The session ID. * * @var string */ protected $id; /** * The session name. * * @var string */ protected $name; /** * The session attributes. * * @var array */ protected $attributes = []; /** * The session handler implementation. * * @var \SessionHandlerInterface */ protected $handler; /** * The session store's serialization strategy. * * @var string */ protected $serialization = 'php'; /** * Session store started status. * * @var bool */ protected $started = false; /** * Create a new session instance. * * @param string $name * @param \SessionHandlerInterface $handler * @param string|null $id * @param string $serialization * @return void */ public function __construct($name, SessionHandlerInterface $handler, $id = null, $serialization = 'php') { $this->setId($id); $this->name = $name; $this->handler = $handler; $this->serialization = $serialization; } /** * Start the session, reading the data from a handler. * * @return bool */ public function start() { $this->loadSession(); if (! $this->has('_token')) { $this->regenerateToken(); } return $this->started = true; } /** * Load the session data from the handler. * * @return void */ protected function loadSession() { $this->attributes = array_merge($this->attributes, $this->readFromHandler()); $this->marshalErrorBag(); } /** * Read the session data from the handler. * * @return array */ protected function readFromHandler() { if ($data = $this->handler->read($this->getId())) { if ($this->serialization === 'json') { $data = json_decode($this->prepareForUnserialize($data), true); } else { $data = @unserialize($this->prepareForUnserialize($data)); } if ($data !== false && is_array($data)) { return $data; } } return []; } /** * Prepare the raw string data from the session for unserialization. * * @param string $data * @return string */ protected function prepareForUnserialize($data) { return $data; } /** * Marshal the ViewErrorBag when using JSON serialization for sessions. * * @return void */ protected function marshalErrorBag() { if ($this->serialization !== 'json' || $this->missing('errors')) { return; } $errorBag = new ViewErrorBag; foreach ($this->get('errors') as $key => $value) { $messageBag = new MessageBag($value['messages']); $errorBag->put($key, $messageBag->setFormat($value['format'])); } $this->put('errors', $errorBag); } /** * Save the session data to storage. * * @return void */ public function save() { $this->ageFlashData(); $this->prepareErrorBagForSerialization(); $this->handler->write($this->getId(), $this->prepareForStorage( $this->serialization === 'json' ? json_encode($this->attributes) : serialize($this->attributes) )); $this->started = false; } /** * Prepare the ViewErrorBag instance for JSON serialization. * * @return void */ protected function prepareErrorBagForSerialization() { if ($this->serialization !== 'json' || $this->missing('errors')) { return; } $errors = []; foreach ($this->attributes['errors']->getBags() as $key => $value) { $errors[$key] = [ 'format' => $value->getFormat(), 'messages' => $value->getMessages(), ]; } $this->attributes['errors'] = $errors; } /** * Prepare the serialized session data for storage. * * @param string $data * @return string */ protected function prepareForStorage($data) { return $data; } /** * Age the flash data for the session. * * @return void */ public function ageFlashData() { $this->forget($this->get('_flash.old', [])); $this->put('_flash.old', $this->get('_flash.new', [])); $this->put('_flash.new', []); } /** * Get all of the session data. * * @return array */ public function all() { return $this->attributes; } /** * Get a subset of the session data. * * @param array $keys * @return array */ public function only(array $keys) { return Arr::only($this->attributes, $keys); } /** * Checks if a key exists. * * @param string|array $key * @return bool */ public function exists($key) { $placeholder = new stdClass; return ! collect(is_array($key) ? $key : func_get_args())->contains(function ($key) use ($placeholder) { return $this->get($key, $placeholder) === $placeholder; }); } /** * Determine if the given key is missing from the session data. * * @param string|array $key * @return bool */ public function missing($key) { return ! $this->exists($key); } /** * Checks if a key is present and not null. * * @param string|array $key * @return bool */ public function has($key) { return ! collect(is_array($key) ? $key : func_get_args())->contains(function ($key) { return is_null($this->get($key)); }); } /** * Get an item from the session. * * @param string $key * @param mixed $default * @return mixed */ public function get($key, $default = null) { return Arr::get($this->attributes, $key, $default); } /** * Get the value of a given key and then forget it. * * @param string $key * @param mixed $default * @return mixed */ public function pull($key, $default = null) { return Arr::pull($this->attributes, $key, $default); } /** * Determine if the session contains old input. * * @param string|null $key * @return bool */ public function hasOldInput($key = null) { $old = $this->getOldInput($key); return is_null($key) ? count($old) > 0 : ! is_null($old); } /** * Get the requested item from the flashed input array. * * @param string|null $key * @param mixed $default * @return mixed */ public function getOldInput($key = null, $default = null) { return Arr::get($this->get('_old_input', []), $key, $default); } /** * Replace the given session attributes entirely. * * @param array $attributes * @return void */ public function replace(array $attributes) { $this->put($attributes); } /** * Put a key / value pair or array of key / value pairs in the session. * * @param string|array $key * @param mixed $value * @return void */ public function put($key, $value = null) { if (! is_array($key)) { $key = [$key => $value]; } foreach ($key as $arrayKey => $arrayValue) { Arr::set($this->attributes, $arrayKey, $arrayValue); } } /** * Get an item from the session, or store the default value. * * @param string $key * @param \Closure $callback * @return mixed */ public function remember($key, Closure $callback) { if (! is_null($value = $this->get($key))) { return $value; } return tap($callback(), function ($value) use ($key) { $this->put($key, $value); }); } /** * Push a value onto a session array. * * @param string $key * @param mixed $value * @return void */ public function push($key, $value) { $array = $this->get($key, []); $array[] = $value; $this->put($key, $array); } /** * Increment the value of an item in the session. * * @param string $key * @param int $amount * @return mixed */ public function increment($key, $amount = 1) { $this->put($key, $value = $this->get($key, 0) + $amount); return $value; } /** * Decrement the value of an item in the session. * * @param string $key * @param int $amount * @return int */ public function decrement($key, $amount = 1) { return $this->increment($key, $amount * -1); } /** * Flash a key / value pair to the session. * * @param string $key * @param mixed $value * @return void */ public function flash(string $key, $value = true) { $this->put($key, $value); $this->push('_flash.new', $key); $this->removeFromOldFlashData([$key]); } /** * Flash a key / value pair to the session for immediate use. * * @param string $key * @param mixed $value * @return void */ public function now($key, $value) { $this->put($key, $value); $this->push('_flash.old', $key); } /** * Reflash all of the session flash data. * * @return void */ public function reflash() { $this->mergeNewFlashes($this->get('_flash.old', [])); $this->put('_flash.old', []); } /** * Reflash a subset of the current flash data. * * @param array|mixed $keys * @return void */ public function keep($keys = null) { $this->mergeNewFlashes($keys = is_array($keys) ? $keys : func_get_args()); $this->removeFromOldFlashData($keys); } /** * Merge new flash keys into the new flash array. * * @param array $keys * @return void */ protected function mergeNewFlashes(array $keys) { $values = array_unique(array_merge($this->get('_flash.new', []), $keys)); $this->put('_flash.new', $values); } /** * Remove the given keys from the old flash data. * * @param array $keys * @return void */ protected function removeFromOldFlashData(array $keys) { $this->put('_flash.old', array_diff($this->get('_flash.old', []), $keys)); } /** * Flash an input array to the session. * * @param array $value * @return void */ public function flashInput(array $value) { $this->flash('_old_input', $value); } /** * Remove an item from the session, returning its value. * * @param string $key * @return mixed */ public function remove($key) { return Arr::pull($this->attributes, $key); } /** * Remove one or many items from the session. * * @param string|array $keys * @return void */ public function forget($keys) { Arr::forget($this->attributes, $keys); } /** * Remove all of the items from the session. * * @return void */ public function flush() { $this->attributes = []; } /** * Flush the session data and regenerate the ID. * * @return bool */ public function invalidate() { $this->flush(); return $this->migrate(true); } /** * Generate a new session identifier. * * @param bool $destroy * @return bool */ public function regenerate($destroy = false) { return tap($this->migrate($destroy), function () { $this->regenerateToken(); }); } /** * Generate a new session ID for the session. * * @param bool $destroy * @return bool */ public function migrate($destroy = false) { if ($destroy) { $this->handler->destroy($this->getId()); } $this->setExists(false); $this->setId($this->generateSessionId()); return true; } /** * Determine if the session has been started. * * @return bool */ public function isStarted() { return $this->started; } /** * Get the name of the session. * * @return string */ public function getName() { return $this->name; } /** * Set the name of the session. * * @param string $name * @return void */ public function setName($name) { $this->name = $name; } /** * Get the current session ID. * * @return string */ public function getId() { return $this->id; } /** * Set the session ID. * * @param string|null $id * @return void */ public function setId($id) { $this->id = $this->isValidId($id) ? $id : $this->generateSessionId(); } /** * Determine if this is a valid session ID. * * @param string|null $id * @return bool */ public function isValidId($id) { return is_string($id) && ctype_alnum($id) && strlen($id) === 40; } /** * Get a new, random session ID. * * @return string */ protected function generateSessionId() { return Str::random(40); } /** * Set the existence of the session on the handler if applicable. * * @param bool $value * @return void */ public function setExists($value) { if ($this->handler instanceof ExistenceAwareInterface) { $this->handler->setExists($value); } } /** * Get the CSRF token value. * * @return string */ public function token() { return $this->get('_token'); } /** * Regenerate the CSRF token value. * * @return void */ public function regenerateToken() { $this->put('_token', Str::random(40)); } /** * Get the previous URL from the session. * * @return string|null */ public function previousUrl() { return $this->get('_previous.url'); } /** * Set the "previous" URL in the session. * * @param string $url * @return void */ public function setPreviousUrl($url) { $this->put('_previous.url', $url); } /** * Specify that the user has confirmed their password. * * @return void */ public function passwordConfirmed() { $this->put('auth.password_confirmed_at', time()); } /** * Get the underlying session handler implementation. * * @return \SessionHandlerInterface */ public function getHandler() { return $this->handler; } /** * Set the underlying session handler implementation. * * @param \SessionHandlerInterface $handler * @return \SessionHandlerInterface */ public function setHandler(SessionHandlerInterface $handler) { return $this->handler = $handler; } /** * Determine if the session handler needs a request. * * @return bool */ public function handlerNeedsRequest() { return $this->handler instanceof CookieSessionHandler; } /** * Set the request on the handler instance. * * @param \Illuminate\Http\Request $request * @return void */ public function setRequestOnHandler($request) { if ($this->handlerNeedsRequest()) { $this->handler->setRequest($request); } } } src/Illuminate/Session/CookieSessionHandler.php 0000644 00000005611 15107327037 0015655 0 ustar 00 <?php namespace Illuminate\Session; use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar; use Illuminate\Support\InteractsWithTime; use SessionHandlerInterface; use Symfony\Component\HttpFoundation\Request; class CookieSessionHandler implements SessionHandlerInterface { use InteractsWithTime; /** * The cookie jar instance. * * @var \Illuminate\Contracts\Cookie\Factory */ protected $cookie; /** * The request instance. * * @var \Symfony\Component\HttpFoundation\Request */ protected $request; /** * The number of minutes the session should be valid. * * @var int */ protected $minutes; /** * Indicates whether the session should be expired when the browser closes. * * @var bool */ protected $expireOnClose; /** * Create a new cookie driven handler instance. * * @param \Illuminate\Contracts\Cookie\QueueingFactory $cookie * @param int $minutes * @param bool $expireOnClose * @return void */ public function __construct(CookieJar $cookie, $minutes, $expireOnClose = false) { $this->cookie = $cookie; $this->minutes = $minutes; $this->expireOnClose = $expireOnClose; } /** * {@inheritdoc} * * @return bool */ public function open($savePath, $sessionName): bool { return true; } /** * {@inheritdoc} * * @return bool */ public function close(): bool { return true; } /** * {@inheritdoc} * * @return string|false */ public function read($sessionId): string|false { $value = $this->request->cookies->get($sessionId) ?: ''; if (! is_null($decoded = json_decode($value, true)) && is_array($decoded) && isset($decoded['expires']) && $this->currentTime() <= $decoded['expires']) { return $decoded['data']; } return ''; } /** * {@inheritdoc} * * @return bool */ public function write($sessionId, $data): bool { $this->cookie->queue($sessionId, json_encode([ 'data' => $data, 'expires' => $this->availableAt($this->minutes * 60), ]), $this->expireOnClose ? 0 : $this->minutes); return true; } /** * {@inheritdoc} * * @return bool */ public function destroy($sessionId): bool { $this->cookie->queue($this->cookie->forget($sessionId)); return true; } /** * {@inheritdoc} * * @return int */ public function gc($lifetime): int { return 0; } /** * Set the request instance. * * @param \Symfony\Component\HttpFoundation\Request $request * @return void */ public function setRequest(Request $request) { $this->request = $request; } } src/Illuminate/Session/FileSessionHandler.php 0000644 00000005060 15107327037 0015321 0 ustar 00 <?php namespace Illuminate\Session; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Carbon; use SessionHandlerInterface; use Symfony\Component\Finder\Finder; class FileSessionHandler implements SessionHandlerInterface { /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * The path where sessions should be stored. * * @var string */ protected $path; /** * The number of minutes the session should be valid. * * @var int */ protected $minutes; /** * Create a new file driven handler instance. * * @param \Illuminate\Filesystem\Filesystem $files * @param string $path * @param int $minutes * @return void */ public function __construct(Filesystem $files, $path, $minutes) { $this->path = $path; $this->files = $files; $this->minutes = $minutes; } /** * {@inheritdoc} * * @return bool */ public function open($savePath, $sessionName): bool { return true; } /** * {@inheritdoc} * * @return bool */ public function close(): bool { return true; } /** * {@inheritdoc} * * @return string|false */ public function read($sessionId): string|false { if ($this->files->isFile($path = $this->path.'/'.$sessionId) && $this->files->lastModified($path) >= Carbon::now()->subMinutes($this->minutes)->getTimestamp()) { return $this->files->sharedGet($path); } return ''; } /** * {@inheritdoc} * * @return bool */ public function write($sessionId, $data): bool { $this->files->put($this->path.'/'.$sessionId, $data, true); return true; } /** * {@inheritdoc} * * @return bool */ public function destroy($sessionId): bool { $this->files->delete($this->path.'/'.$sessionId); return true; } /** * {@inheritdoc} * * @return int */ public function gc($lifetime): int { $files = Finder::create() ->in($this->path) ->files() ->ignoreDotFiles(true) ->date('<= now - '.$lifetime.' seconds'); $deletedSessions = 0; foreach ($files as $file) { $this->files->delete($file->getRealPath()); $deletedSessions++; } return $deletedSessions; } } src/Illuminate/Session/composer.json 0000644 00000002170 15107327037 0013610 0 ustar 00 { "name": "illuminate/session", "description": "The Illuminate Session package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "ext-ctype": "*", "ext-session": "*", "illuminate/collections": "^10.0", "illuminate/contracts": "^10.0", "illuminate/filesystem": "^10.0", "illuminate/support": "^10.0", "symfony/finder": "^6.2", "symfony/http-foundation": "^6.2" }, "autoload": { "psr-4": { "Illuminate\\Session\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "illuminate/console": "Required to use the session:table command (^10.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Session/NullSessionHandler.php 0000644 00000001732 15107327037 0015356 0 ustar 00 <?php namespace Illuminate\Session; use SessionHandlerInterface; class NullSessionHandler implements SessionHandlerInterface { /** * {@inheritdoc} * * @return bool */ public function open($savePath, $sessionName): bool { return true; } /** * {@inheritdoc} * * @return bool */ public function close(): bool { return true; } /** * {@inheritdoc} * * @return string */ public function read($sessionId): string { return ''; } /** * {@inheritdoc} * * @return bool */ public function write($sessionId, $data): bool { return true; } /** * {@inheritdoc} * * @return bool */ public function destroy($sessionId): bool { return true; } /** * {@inheritdoc} * * @return int */ public function gc($lifetime): int { return 0; } } src/Illuminate/Session/LICENSE.md 0000644 00000002063 15107327037 0012473 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Session/ArraySessionHandler.php 0000644 00000005107 15107327037 0015522 0 ustar 00 <?php namespace Illuminate\Session; use Illuminate\Support\InteractsWithTime; use SessionHandlerInterface; class ArraySessionHandler implements SessionHandlerInterface { use InteractsWithTime; /** * The array of stored values. * * @var array */ protected $storage = []; /** * The number of minutes the session should be valid. * * @var int */ protected $minutes; /** * Create a new array driven handler instance. * * @param int $minutes * @return void */ public function __construct($minutes) { $this->minutes = $minutes; } /** * {@inheritdoc} * * @return bool */ public function open($savePath, $sessionName): bool { return true; } /** * {@inheritdoc} * * @return bool */ public function close(): bool { return true; } /** * {@inheritdoc} * * @return string|false */ public function read($sessionId): string|false { if (! isset($this->storage[$sessionId])) { return ''; } $session = $this->storage[$sessionId]; $expiration = $this->calculateExpiration($this->minutes * 60); if (isset($session['time']) && $session['time'] >= $expiration) { return $session['data']; } return ''; } /** * {@inheritdoc} * * @return bool */ public function write($sessionId, $data): bool { $this->storage[$sessionId] = [ 'data' => $data, 'time' => $this->currentTime(), ]; return true; } /** * {@inheritdoc} * * @return bool */ public function destroy($sessionId): bool { if (isset($this->storage[$sessionId])) { unset($this->storage[$sessionId]); } return true; } /** * {@inheritdoc} * * @return int */ public function gc($lifetime): int { $expiration = $this->calculateExpiration($lifetime); $deletedSessions = 0; foreach ($this->storage as $sessionId => $session) { if ($session['time'] < $expiration) { unset($this->storage[$sessionId]); $deletedSessions++; } } return $deletedSessions; } /** * Get the expiration time of the session. * * @param int $seconds * @return int */ protected function calculateExpiration($seconds) { return $this->currentTime() - $seconds; } } src/Illuminate/Session/Middleware/AuthenticateSession.php 0000644 00000006407 15107327037 0017645 0 ustar 00 <?php namespace Illuminate\Session\Middleware; use Closure; use Illuminate\Auth\AuthenticationException; use Illuminate\Contracts\Auth\Factory as AuthFactory; use Illuminate\Contracts\Session\Middleware\AuthenticatesSessions; use Illuminate\Http\Request; class AuthenticateSession implements AuthenticatesSessions { /** * The authentication factory implementation. * * @var \Illuminate\Contracts\Auth\Factory */ protected $auth; /** * Create a new middleware instance. * * @param \Illuminate\Contracts\Auth\Factory $auth * @return void */ public function __construct(AuthFactory $auth) { $this->auth = $auth; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (! $request->hasSession() || ! $request->user()) { return $next($request); } if ($this->guard()->viaRemember()) { $passwordHash = explode('|', $request->cookies->get($this->guard()->getRecallerName()))[2] ?? null; if (! $passwordHash || $passwordHash != $request->user()->getAuthPassword()) { $this->logout($request); } } if (! $request->session()->has('password_hash_'.$this->auth->getDefaultDriver())) { $this->storePasswordHashInSession($request); } if ($request->session()->get('password_hash_'.$this->auth->getDefaultDriver()) !== $request->user()->getAuthPassword()) { $this->logout($request); } return tap($next($request), function () use ($request) { if (! is_null($this->guard()->user())) { $this->storePasswordHashInSession($request); } }); } /** * Store the user's current password hash in the session. * * @param \Illuminate\Http\Request $request * @return void */ protected function storePasswordHashInSession($request) { if (! $request->user()) { return; } $request->session()->put([ 'password_hash_'.$this->auth->getDefaultDriver() => $request->user()->getAuthPassword(), ]); } /** * Log the user out of the application. * * @param \Illuminate\Http\Request $request * @return void * * @throws \Illuminate\Auth\AuthenticationException */ protected function logout($request) { $this->guard()->logoutCurrentDevice(); $request->session()->flush(); throw new AuthenticationException( 'Unauthenticated.', [$this->auth->getDefaultDriver()], $this->redirectTo($request) ); } /** * Get the guard instance that should be used by the middleware. * * @return \Illuminate\Contracts\Auth\Factory|\Illuminate\Contracts\Auth\Guard */ protected function guard() { return $this->auth; } /** * Get the path the user should be redirected to when their session is not authenticated. * * @param \Illuminate\Http\Request $request * @return string|null */ protected function redirectTo(Request $request) { // } } src/Illuminate/Session/Middleware/StartSession.php 0000644 00000021527 15107327037 0016324 0 ustar 00 <?php namespace Illuminate\Session\Middleware; use Closure; use Illuminate\Contracts\Session\Session; use Illuminate\Http\Request; use Illuminate\Routing\Route; use Illuminate\Session\SessionManager; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Date; use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpFoundation\Response; class StartSession { /** * The session manager. * * @var \Illuminate\Session\SessionManager */ protected $manager; /** * The callback that can resolve an instance of the cache factory. * * @var callable|null */ protected $cacheFactoryResolver; /** * Create a new session middleware. * * @param \Illuminate\Session\SessionManager $manager * @param callable|null $cacheFactoryResolver * @return void */ public function __construct(SessionManager $manager, callable $cacheFactoryResolver = null) { $this->manager = $manager; $this->cacheFactoryResolver = $cacheFactoryResolver; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (! $this->sessionConfigured()) { return $next($request); } $session = $this->getSession($request); if ($this->manager->shouldBlock() || ($request->route() instanceof Route && $request->route()->locksFor())) { return $this->handleRequestWhileBlocking($request, $session, $next); } return $this->handleStatefulRequest($request, $session, $next); } /** * Handle the given request within session state. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Contracts\Session\Session $session * @param \Closure $next * @return mixed */ protected function handleRequestWhileBlocking(Request $request, $session, Closure $next) { if (! $request->route() instanceof Route) { return; } $lockFor = $request->route() && $request->route()->locksFor() ? $request->route()->locksFor() : $this->manager->defaultRouteBlockLockSeconds(); $lock = $this->cache($this->manager->blockDriver()) ->lock('session:'.$session->getId(), $lockFor) ->betweenBlockedAttemptsSleepFor(50); try { $lock->block( ! is_null($request->route()->waitsFor()) ? $request->route()->waitsFor() : $this->manager->defaultRouteBlockWaitSeconds() ); return $this->handleStatefulRequest($request, $session, $next); } finally { $lock?->release(); } } /** * Handle the given request within session state. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Contracts\Session\Session $session * @param \Closure $next * @return mixed */ protected function handleStatefulRequest(Request $request, $session, Closure $next) { // If a session driver has been configured, we will need to start the session here // so that the data is ready for an application. Note that the Laravel sessions // do not make use of PHP "native" sessions in any way since they are crappy. $request->setLaravelSession( $this->startSession($request, $session) ); $this->collectGarbage($session); $response = $next($request); $this->storeCurrentUrl($request, $session); $this->addCookieToResponse($response, $session); // Again, if the session has been configured we will need to close out the session // so that the attributes may be persisted to some storage medium. We will also // add the session identifier cookie to the application response headers now. $this->saveSession($request); return $response; } /** * Start the session for the given request. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Contracts\Session\Session $session * @return \Illuminate\Contracts\Session\Session */ protected function startSession(Request $request, $session) { return tap($session, function ($session) use ($request) { $session->setRequestOnHandler($request); $session->start(); }); } /** * Get the session implementation from the manager. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Contracts\Session\Session */ public function getSession(Request $request) { return tap($this->manager->driver(), function ($session) use ($request) { $session->setId($request->cookies->get($session->getName())); }); } /** * Remove the garbage from the session if necessary. * * @param \Illuminate\Contracts\Session\Session $session * @return void */ protected function collectGarbage(Session $session) { $config = $this->manager->getSessionConfig(); // Here we will see if this request hits the garbage collection lottery by hitting // the odds needed to perform garbage collection on any given request. If we do // hit it, we'll call this handler to let it delete all the expired sessions. if ($this->configHitsLottery($config)) { $session->getHandler()->gc($this->getSessionLifetimeInSeconds()); } } /** * Determine if the configuration odds hit the lottery. * * @param array $config * @return bool */ protected function configHitsLottery(array $config) { return random_int(1, $config['lottery'][1]) <= $config['lottery'][0]; } /** * Store the current URL for the request if necessary. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Contracts\Session\Session $session * @return void */ protected function storeCurrentUrl(Request $request, $session) { if ($request->isMethod('GET') && $request->route() instanceof Route && ! $request->ajax() && ! $request->prefetch() && ! $request->isPrecognitive()) { $session->setPreviousUrl($request->fullUrl()); } } /** * Add the session cookie to the application response. * * @param \Symfony\Component\HttpFoundation\Response $response * @param \Illuminate\Contracts\Session\Session $session * @return void */ protected function addCookieToResponse(Response $response, Session $session) { if ($this->sessionIsPersistent($config = $this->manager->getSessionConfig())) { $response->headers->setCookie(new Cookie( $session->getName(), $session->getId(), $this->getCookieExpirationDate(), $config['path'], $config['domain'], $config['secure'] ?? false, $config['http_only'] ?? true, false, $config['same_site'] ?? null )); } } /** * Save the session data to storage. * * @param \Illuminate\Http\Request $request * @return void */ protected function saveSession($request) { if (! $request->isPrecognitive()) { $this->manager->driver()->save(); } } /** * Get the session lifetime in seconds. * * @return int */ protected function getSessionLifetimeInSeconds() { return ($this->manager->getSessionConfig()['lifetime'] ?? null) * 60; } /** * Get the cookie lifetime in seconds. * * @return \DateTimeInterface|int */ protected function getCookieExpirationDate() { $config = $this->manager->getSessionConfig(); return $config['expire_on_close'] ? 0 : Date::instance( Carbon::now()->addRealMinutes($config['lifetime']) ); } /** * Determine if a session driver has been configured. * * @return bool */ protected function sessionConfigured() { return ! is_null($this->manager->getSessionConfig()['driver'] ?? null); } /** * Determine if the configured session driver is persistent. * * @param array|null $config * @return bool */ protected function sessionIsPersistent(array $config = null) { $config = $config ?: $this->manager->getSessionConfig(); return ! is_null($config['driver'] ?? null); } /** * Resolve the given cache driver. * * @param string $driver * @return \Illuminate\Cache\Store */ protected function cache($driver) { return call_user_func($this->cacheFactoryResolver)->driver($driver); } } src/Illuminate/Session/SymfonySessionDecorator.php 0000644 00000007113 15107327037 0016454 0 ustar 00 <?php namespace Illuminate\Session; use BadMethodCallException; use Illuminate\Contracts\Session\Session; use Symfony\Component\HttpFoundation\Session\SessionBagInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag; class SymfonySessionDecorator implements SessionInterface { /** * The underlying Laravel session store. * * @var \Illuminate\Session\Store */ protected $store; /** * Create a new session decorator. * * @param \Illuminate\Contracts\Session\Session $store * @return void */ public function __construct(Session $store) { $this->store = $store; } /** * {@inheritdoc} */ public function start(): bool { return $this->store->start(); } /** * {@inheritdoc} */ public function getId(): string { return $this->store->getId(); } /** * {@inheritdoc} * * @return void */ public function setId(string $id) { $this->store->setId($id); } /** * {@inheritdoc} */ public function getName(): string { return $this->store->getName(); } /** * {@inheritdoc} * * @return void */ public function setName(string $name) { $this->store->setName($name); } /** * {@inheritdoc} */ public function invalidate(int $lifetime = null): bool { $this->store->invalidate(); return true; } /** * {@inheritdoc} */ public function migrate(bool $destroy = false, int $lifetime = null): bool { $this->store->migrate($destroy); return true; } /** * {@inheritdoc} * * @return void */ public function save() { $this->store->save(); } /** * {@inheritdoc} */ public function has(string $name): bool { return $this->store->has($name); } /** * {@inheritdoc} */ public function get(string $name, mixed $default = null): mixed { return $this->store->get($name, $default); } /** * {@inheritdoc} * * @return void */ public function set(string $name, mixed $value) { $this->store->put($name, $value); } /** * {@inheritdoc} */ public function all(): array { return $this->store->all(); } /** * {@inheritdoc} * * @return void */ public function replace(array $attributes) { $this->store->replace($attributes); } /** * {@inheritdoc} */ public function remove(string $name): mixed { return $this->store->remove($name); } /** * {@inheritdoc} * * @return void */ public function clear() { $this->store->flush(); } /** * {@inheritdoc} */ public function isStarted(): bool { return $this->store->isStarted(); } /** * {@inheritdoc} * * @return void */ public function registerBag(SessionBagInterface $bag) { throw new BadMethodCallException('Method not implemented by Laravel.'); } /** * {@inheritdoc} */ public function getBag(string $name): SessionBagInterface { throw new BadMethodCallException('Method not implemented by Laravel.'); } /** * {@inheritdoc} */ public function getMetadataBag(): MetadataBag { throw new BadMethodCallException('Method not implemented by Laravel.'); } } src/Illuminate/Session/ExistenceAwareInterface.php 0000644 00000000376 15107327037 0016335 0 ustar 00 <?php namespace Illuminate\Session; interface ExistenceAwareInterface { /** * Set the existence state for the session. * * @param bool $value * @return \SessionHandlerInterface */ public function setExists($value); } src/Illuminate/Session/CacheBasedSessionHandler.php 0000644 00000003632 15107327037 0016407 0 ustar 00 <?php namespace Illuminate\Session; use Illuminate\Contracts\Cache\Repository as CacheContract; use SessionHandlerInterface; class CacheBasedSessionHandler implements SessionHandlerInterface { /** * The cache repository instance. * * @var \Illuminate\Contracts\Cache\Repository */ protected $cache; /** * The number of minutes to store the data in the cache. * * @var int */ protected $minutes; /** * Create a new cache driven handler instance. * * @param \Illuminate\Contracts\Cache\Repository $cache * @param int $minutes * @return void */ public function __construct(CacheContract $cache, $minutes) { $this->cache = $cache; $this->minutes = $minutes; } /** * {@inheritdoc} * * @return bool */ public function open($savePath, $sessionName): bool { return true; } /** * {@inheritdoc} * * @return bool */ public function close(): bool { return true; } /** * {@inheritdoc} * * @return string */ public function read($sessionId): string { return $this->cache->get($sessionId, ''); } /** * {@inheritdoc} * * @return bool */ public function write($sessionId, $data): bool { return $this->cache->put($sessionId, $data, $this->minutes * 60); } /** * {@inheritdoc} * * @return bool */ public function destroy($sessionId): bool { return $this->cache->forget($sessionId); } /** * {@inheritdoc} * * @return int */ public function gc($lifetime): int { return 0; } /** * Get the underlying cache repository. * * @return \Illuminate\Contracts\Cache\Repository */ public function getCache() { return $this->cache; } } src/Illuminate/Process/FakeProcessDescription.php 0000644 00000012040 15107327037 0016200 0 ustar 00 <?php namespace Illuminate\Process; use Symfony\Component\Process\Process; class FakeProcessDescription { /** * The process' ID. * * @var int|null */ public $processId = 1000; /** * All of the process' output in the order it was described. * * @var array */ public $output = []; /** * The process' exit code. * * @var int */ public $exitCode = 0; /** * The number of times the process should indicate that it is "running". * * @var int */ public $runIterations = 0; /** * Specify the process ID that should be assigned to the process. * * @param int $processId * @return $this */ public function id(int $processId) { $this->processId = $processId; return $this; } /** * Describe a line of standard output. * * @param array|string $output * @return $this */ public function output(array|string $output) { if (is_array($output)) { collect($output)->each(fn ($line) => $this->output($line)); return $this; } $this->output[] = ['type' => 'out', 'buffer' => rtrim($output, "\n")."\n"]; return $this; } /** * Describe a line of error output. * * @param array|string $output * @return $this */ public function errorOutput(array|string $output) { if (is_array($output)) { collect($output)->each(fn ($line) => $this->errorOutput($line)); return $this; } $this->output[] = ['type' => 'err', 'buffer' => rtrim($output, "\n")."\n"]; return $this; } /** * Replace the entire output buffer with the given string. * * @param string $output * @return $this */ public function replaceOutput(string $output) { $this->output = collect($this->output)->reject(function ($output) { return $output['type'] === 'out'; })->values()->all(); if (strlen($output) > 0) { $this->output[] = [ 'type' => 'out', 'buffer' => rtrim($output, "\n")."\n", ]; } return $this; } /** * Replace the entire error output buffer with the given string. * * @param string $output * @return $this */ public function replaceErrorOutput(string $output) { $this->output = collect($this->output)->reject(function ($output) { return $output['type'] === 'err'; })->values()->all(); if (strlen($output) > 0) { $this->output[] = [ 'type' => 'err', 'buffer' => rtrim($output, "\n")."\n", ]; } return $this; } /** * Specify the process exit code. * * @param int $exitCode * @return $this */ public function exitCode(int $exitCode) { $this->exitCode = $exitCode; return $this; } /** * Specify how many times the "isRunning" method should return "true". * * @param int $iterations * @return $this */ public function iterations(int $iterations) { return $this->runsFor(iterations: $iterations); } /** * Specify how many times the "isRunning" method should return "true". * * @param int $iterations * @return $this */ public function runsFor(int $iterations) { $this->runIterations = $iterations; return $this; } /** * Turn the fake process description into an actual process. * * @param string $command * @return \Symfony\Component\Process\Process */ public function toSymfonyProcess(string $command) { return Process::fromShellCommandline($command); } /** * Convert the process description into a process result. * * @param string $command * @return \Illuminate\Contracts\Process\ProcessResult */ public function toProcessResult(string $command) { return new FakeProcessResult( command: $command, exitCode: $this->exitCode, output: $this->resolveOutput(), errorOutput: $this->resolveErrorOutput(), ); } /** * Resolve the standard output as a string. * * @return string */ protected function resolveOutput() { $output = collect($this->output) ->filter(fn ($output) => $output['type'] === 'out'); return $output->isNotEmpty() ? rtrim($output->map->buffer->implode(''), "\n")."\n" : ''; } /** * Resolve the error output as a string. * * @return string */ protected function resolveErrorOutput() { $output = collect($this->output) ->filter(fn ($output) => $output['type'] === 'err'); return $output->isNotEmpty() ? rtrim($output->map->buffer->implode(''), "\n")."\n" : ''; } } src/Illuminate/Process/Pool.php 0000644 00000005713 15107327037 0012511 0 ustar 00 <?php namespace Illuminate\Process; use InvalidArgumentException; /** * @mixin \Illuminate\Process\Factory * @mixin \Illuminate\Process\PendingProcess */ class Pool { /** * The process factory instance. * * @var \Illuminate\Process\Factory */ protected $factory; /** * The callback that resolves the pending processes. * * @var callable */ protected $callback; /** * The array of pending processes. * * @var array */ protected $pendingProcesses = []; /** * Create a new process pool. * * @param \Illuminate\Process\Factory $factory * @param callable $callback * @return void */ public function __construct(Factory $factory, callable $callback) { $this->factory = $factory; $this->callback = $callback; } /** * Add a process to the pool with a key. * * @param string $key * @return \Illuminate\Process\PendingProcess */ public function as(string $key) { return tap($this->factory->newPendingProcess(), function ($pendingProcess) use ($key) { $this->pendingProcesses[$key] = $pendingProcess; }); } /** * Start all of the processes in the pool. * * @param callable|null $output * @return \Illuminate\Process\InvokedProcessPool */ public function start(?callable $output = null) { call_user_func($this->callback, $this); return new InvokedProcessPool( collect($this->pendingProcesses) ->each(function ($pendingProcess) { if (! $pendingProcess instanceof PendingProcess) { throw new InvalidArgumentException('Process pool must only contain pending processes.'); } })->mapWithKeys(function ($pendingProcess, $key) use ($output) { return [$key => $pendingProcess->start(output: $output ? function ($type, $buffer) use ($key, $output) { $output($type, $buffer, $key); } : null)]; }) ->all() ); } /** * Start and wait for the processes to finish. * * @return \Illuminate\Process\ProcessPoolResults */ public function run() { return $this->wait(); } /** * Start and wait for the processes to finish. * * @return \Illuminate\Process\ProcessPoolResults */ public function wait() { return $this->start()->wait(); } /** * Dynamically proxy methods calls to a new pending process. * * @param string $method * @param array $parameters * @return \Illuminate\Process\PendingProcess */ public function __call($method, $parameters) { return tap($this->factory->{$method}(...$parameters), function ($pendingProcess) { $this->pendingProcesses[] = $pendingProcess; }); } } src/Illuminate/Process/FakeProcessSequence.php 0000644 00000006635 15107327040 0015474 0 ustar 00 <?php namespace Illuminate\Process; use Illuminate\Contracts\Process\ProcessResult as ProcessResultContract; use OutOfBoundsException; class FakeProcessSequence { /** * The fake process results and descriptions. * * @var array */ protected $processes = []; /** * Indicates that invoking this sequence when it is empty should throw an exception. * * @var bool */ protected $failWhenEmpty = true; /** * The response that should be returned when the sequence is empty. * * @var \Illuminate\Contracts\Process\ProcessResult|\Illuminate\Process\FakeProcessDescription */ protected $emptyProcess; /** * Create a new fake process sequence instance. * * @param array $processes * @return void */ public function __construct(array $processes = []) { $this->processes = $processes; } /** * Push a new process result or description onto the sequence. * * @param \Illuminate\Contracts\Process\ProcessResult|\Illuminate\Process\FakeProcessDescription|array|string $process * @return $this */ public function push(ProcessResultContract|FakeProcessDescription|array|string $process) { $this->processes[] = $this->toProcessResult($process); return $this; } /** * Make the sequence return a default result when it is empty. * * @param \Illuminate\Contracts\Process\ProcessResult|\Illuminate\Process\FakeProcessDescription|array|string $process * @return $this */ public function whenEmpty(ProcessResultContract|FakeProcessDescription|array|string $process) { $this->failWhenEmpty = false; $this->emptyProcess = $this->toProcessResult($process); return $this; } /** * Convert the given result into an actual process result or description. * * @param \Illuminate\Contracts\Process\ProcessResult|\Illuminate\Process\FakeProcessDescription|array|string $process * @return \Illuminate\Contracts\Process\ProcessResult|\Illuminate\Process\FakeProcessDescription */ protected function toProcessResult(ProcessResultContract|FakeProcessDescription|array|string $process) { return is_array($process) || is_string($process) ? new FakeProcessResult(output: $process) : $process; } /** * Make the sequence return a default result when it is empty. * * @return $this */ public function dontFailWhenEmpty() { return $this->whenEmpty(new FakeProcessResult); } /** * Indicate that this sequence has depleted all of its process results. * * @return bool */ public function isEmpty() { return count($this->processes) === 0; } /** * Get the next process in the sequence. * * @return \Illuminate\Contracts\Process\ProcessResult|\Illuminate\Process\FakeProcessDescription * * @throws \OutOfBoundsException */ public function __invoke() { if ($this->failWhenEmpty && count($this->processes) === 0) { throw new OutOfBoundsException('A process was invoked, but the process result sequence is empty.'); } if (! $this->failWhenEmpty && count($this->processes) === 0) { return value($this->emptyProcess ?? new FakeProcessResult); } return array_shift($this->processes); } } src/Illuminate/Process/Exceptions/ProcessFailedException.php 0000644 00000002063 15107327040 0020310 0 ustar 00 <?php namespace Illuminate\Process\Exceptions; use Illuminate\Contracts\Process\ProcessResult; use RuntimeException; class ProcessFailedException extends RuntimeException { /** * The process result instance. * * @var \Illuminate\Contracts\Process\ProcessResult */ public $result; /** * Create a new exception instance. * * @param \Illuminate\Contracts\Process\ProcessResult $result * @return void */ public function __construct(ProcessResult $result) { $this->result = $result; $error = sprintf('The command "%s" failed.'."\n\nExit Code: %s", $result->command(), $result->exitCode(), ); if (! empty($result->output())) { $error .= sprintf("\n\nOutput:\n================\n%s", $result->output()); } if (! empty($result->errorOutput())) { $error .= sprintf("\n\nError Output:\n================\n%s", $result->errorOutput()); } parent::__construct($error, $result->exitCode() ?? 1); } } src/Illuminate/Process/Exceptions/ProcessTimedOutException.php 0000644 00000001613 15107327040 0020656 0 ustar 00 <?php namespace Illuminate\Process\Exceptions; use Illuminate\Contracts\Process\ProcessResult; use Symfony\Component\Process\Exception\ProcessTimedOutException as SymfonyTimeoutException; use Symfony\Component\Process\Exception\RuntimeException; class ProcessTimedOutException extends RuntimeException { /** * The process result instance. * * @var \Illuminate\Contracts\Process\ProcessResult */ public $result; /** * Create a new exception instance. * * @param \Symfony\Component\Process\Exception\ProcessTimedOutException $original * @param \Illuminate\Contracts\Process\ProcessResult $result * @return void */ public function __construct(SymfonyTimeoutException $original, ProcessResult $result) { $this->result = $result; parent::__construct($original->getMessage(), $original->getCode(), $original); } } src/Illuminate/Process/FakeInvokedProcess.php 0000644 00000016310 15107327040 0015312 0 ustar 00 <?php namespace Illuminate\Process; use Illuminate\Contracts\Process\InvokedProcess as InvokedProcessContract; class FakeInvokedProcess implements InvokedProcessContract { /** * The command being faked. * * @var string */ protected $command; /** * The underlying process description. * * @var \Illuminate\Process\FakeProcessDescription */ protected $process; /** * The signals that have been received. * * @var array */ protected $receivedSignals = []; /** * The number of times the process should indicate that it is "running". * * @var int|null */ protected $remainingRunIterations; /** * The general output handler callback. * * @var callable|null */ protected $outputHandler; /** * The current output's index. * * @var int */ protected $nextOutputIndex = 0; /** * The current error output's index. * * @var int */ protected $nextErrorOutputIndex = 0; /** * Create a new invoked process instance. * * @param string $command * @param \Illuminate\Process\FakeProcessDescription $process * @return void */ public function __construct(string $command, FakeProcessDescription $process) { $this->command = $command; $this->process = $process; } /** * Get the process ID if the process is still running. * * @return int|null */ public function id() { $this->invokeOutputHandlerWithNextLineOfOutput(); return $this->process->processId; } /** * Send a signal to the process. * * @param int $signal * @return $this */ public function signal(int $signal) { $this->invokeOutputHandlerWithNextLineOfOutput(); $this->receivedSignals[] = $signal; return $this; } /** * Determine if the process has received the given signal. * * @param int $signal * @return bool */ public function hasReceivedSignal(int $signal) { return in_array($signal, $this->receivedSignals); } /** * Determine if the process is still running. * * @return bool */ public function running() { $this->invokeOutputHandlerWithNextLineOfOutput(); $this->remainingRunIterations = is_null($this->remainingRunIterations) ? $this->process->runIterations : $this->remainingRunIterations; if ($this->remainingRunIterations === 0) { while ($this->invokeOutputHandlerWithNextLineOfOutput()) { } return false; } $this->remainingRunIterations = $this->remainingRunIterations - 1; return true; } /** * Invoke the asynchronous output handler with the next single line of output if necessary. * * @return array|false */ protected function invokeOutputHandlerWithNextLineOfOutput() { if (! $this->outputHandler) { return false; } [$outputCount, $outputStartingPoint] = [ count($this->process->output), min($this->nextOutputIndex, $this->nextErrorOutputIndex), ]; for ($i = $outputStartingPoint; $i < $outputCount; $i++) { $currentOutput = $this->process->output[$i]; if ($currentOutput['type'] === 'out' && $i >= $this->nextOutputIndex) { call_user_func($this->outputHandler, 'out', $currentOutput['buffer']); $this->nextOutputIndex = $i + 1; return $currentOutput; } elseif ($currentOutput['type'] === 'err' && $i >= $this->nextErrorOutputIndex) { call_user_func($this->outputHandler, 'err', $currentOutput['buffer']); $this->nextErrorOutputIndex = $i + 1; return $currentOutput; } } return false; } /** * Get the standard output for the process. * * @return string */ public function output() { $this->latestOutput(); $output = []; for ($i = 0; $i < $this->nextOutputIndex; $i++) { if ($this->process->output[$i]['type'] === 'out') { $output[] = $this->process->output[$i]['buffer']; } } return rtrim(implode('', $output), "\n")."\n"; } /** * Get the error output for the process. * * @return string */ public function errorOutput() { $this->latestErrorOutput(); $output = []; for ($i = 0; $i < $this->nextErrorOutputIndex; $i++) { if ($this->process->output[$i]['type'] === 'err') { $output[] = $this->process->output[$i]['buffer']; } } return rtrim(implode('', $output), "\n")."\n"; } /** * Get the latest standard output for the process. * * @return string */ public function latestOutput() { $outputCount = count($this->process->output); for ($i = $this->nextOutputIndex; $i < $outputCount; $i++) { if ($this->process->output[$i]['type'] === 'out') { $output = $this->process->output[$i]['buffer']; $this->nextOutputIndex = $i + 1; break; } $this->nextOutputIndex = $i + 1; } return isset($output) ? $output : ''; } /** * Get the latest error output for the process. * * @return string */ public function latestErrorOutput() { $outputCount = count($this->process->output); for ($i = $this->nextErrorOutputIndex; $i < $outputCount; $i++) { if ($this->process->output[$i]['type'] === 'err') { $output = $this->process->output[$i]['buffer']; $this->nextErrorOutputIndex = $i + 1; break; } $this->nextErrorOutputIndex = $i + 1; } return isset($output) ? $output : ''; } /** * Wait for the process to finish. * * @param callable|null $output * @return \Illuminate\Contracts\Process\ProcessResult */ public function wait(callable $output = null) { $this->outputHandler = $output ?: $this->outputHandler; if (! $this->outputHandler) { $this->remainingRunIterations = 0; return $this->predictProcessResult(); } while ($this->invokeOutputHandlerWithNextLineOfOutput()) { // } $this->remainingRunIterations = 0; return $this->process->toProcessResult($this->command); } /** * Get the ultimate process result that will be returned by this "process". * * @return \Illuminate\Contracts\Process\ProcessResult */ public function predictProcessResult() { return $this->process->toProcessResult($this->command); } /** * Set the general output handler for the fake invoked process. * * @param callable|null $output * @return $this */ public function withOutputHandler(?callable $outputHandler) { $this->outputHandler = $outputHandler; return $this; } } src/Illuminate/Process/composer.json 0000644 00000001654 15107327040 0013603 0 ustar 00 { "name": "illuminate/process", "description": "The Illuminate Process package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/collections": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0", "illuminate/support": "^10.0", "symfony/process": "^6.2" }, "autoload": { "psr-4": { "Illuminate\\Process\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Process/ProcessResult.php 0000644 00000005761 15107327040 0014412 0 ustar 00 <?php namespace Illuminate\Process; use Illuminate\Contracts\Process\ProcessResult as ProcessResultContract; use Illuminate\Process\Exceptions\ProcessFailedException; use Symfony\Component\Process\Process; class ProcessResult implements ProcessResultContract { /** * The underlying process instance. * * @var \Symfony\Component\Process\Process */ protected $process; /** * Create a new process result instance. * * @param \Symfony\Component\Process\Process $process * @return void */ public function __construct(Process $process) { $this->process = $process; } /** * Get the original command executed by the process. * * @return string */ public function command() { return $this->process->getCommandLine(); } /** * Determine if the process was successful. * * @return bool */ public function successful() { return $this->process->isSuccessful(); } /** * Determine if the process failed. * * @return bool */ public function failed() { return ! $this->successful(); } /** * Get the exit code of the process. * * @return int|null */ public function exitCode() { return $this->process->getExitCode(); } /** * Get the standard output of the process. * * @return string */ public function output() { return $this->process->getOutput(); } /** * Determine if the output contains the given string. * * @param string $output * @return bool */ public function seeInOutput(string $output) { return str_contains($this->output(), $output); } /** * Get the error output of the process. * * @return string */ public function errorOutput() { return $this->process->getErrorOutput(); } /** * Determine if the error output contains the given string. * * @param string $output * @return bool */ public function seeInErrorOutput(string $output) { return str_contains($this->errorOutput(), $output); } /** * Throw an exception if the process failed. * * @param callable|null $callback * @return $this */ public function throw(callable $callback = null) { if ($this->successful()) { return $this; } $exception = new ProcessFailedException($this); if ($callback) { $callback($this, $exception); } throw $exception; } /** * Throw an exception if the process failed and the given condition is true. * * @param bool $condition * @param callable|null $callback * @return $this */ public function throwIf(bool $condition, callable $callback = null) { if ($condition) { return $this->throw($callback); } return $this; } } src/Illuminate/Process/ProcessPoolResults.php 0000644 00000003047 15107327040 0015422 0 ustar 00 <?php namespace Illuminate\Process; use ArrayAccess; use Illuminate\Support\Collection; class ProcessPoolResults implements ArrayAccess { /** * The results of the processes. * * @var array */ protected $results = []; /** * Create a new process pool result set. * * @param array $results * @return void */ public function __construct(array $results) { $this->results = $results; } /** * Get the results as a collection. * * @return \Illuminate\Support\Collection */ public function collect() { return new Collection($this->results); } /** * Determine if the given array offset exists. * * @param int $offset * @return bool */ public function offsetExists($offset): bool { return isset($this->results[$offset]); } /** * Get the result at the given offset. * * @param int $offset * @return mixed */ public function offsetGet($offset): mixed { return $this->results[$offset]; } /** * Set the result at the given offset. * * @param int $offset * @param mixed $value * @return void */ public function offsetSet($offset, $value): void { $this->results[$offset] = $value; } /** * Unset the result at the given offset. * * @param int $offset * @return void */ public function offsetUnset($offset): void { unset($this->results[$offset]); } } src/Illuminate/Process/Factory.php 0000644 00000020567 15107327040 0013205 0 ustar 00 <?php namespace Illuminate\Process; use Closure; use Illuminate\Contracts\Process\ProcessResult as ProcessResultContract; use Illuminate\Support\Traits\Macroable; use PHPUnit\Framework\Assert as PHPUnit; class Factory { use Macroable { __call as macroCall; } /** * Indicates if the process factory has faked process handlers. * * @var bool */ protected $recording = false; /** * All of the recorded processes. * * @var array */ protected $recorded = []; /** * The registered fake handler callbacks. * * @var array */ protected $fakeHandlers = []; /** * Indicates that an exception should be thrown if any process is not faked. * * @var bool */ protected $preventStrayProcesses = false; /** * Create a new fake process response for testing purposes. * * @param array|string $output * @param array|string $errorOutput * @param int $exitCode * @return \Illuminate\Process\FakeProcessResult */ public function result(array|string $output = '', array|string $errorOutput = '', int $exitCode = 0) { return new FakeProcessResult( output: $output, errorOutput: $errorOutput, exitCode: $exitCode, ); } /** * Begin describing a fake process lifecycle. * * @return \Illuminate\Process\FakeProcessDescription */ public function describe() { return new FakeProcessDescription; } /** * Begin describing a fake process sequence. * * @param array $processes * @return \Illuminate\Process\FakeProcessSequence */ public function sequence(array $processes = []) { return new FakeProcessSequence($processes); } /** * Indicate that the process factory should fake processes. * * @param \Closure|array|null $callback * @return $this */ public function fake(Closure|array $callback = null) { $this->recording = true; if (is_null($callback)) { $this->fakeHandlers = ['*' => fn () => new FakeProcessResult]; return $this; } if ($callback instanceof Closure) { $this->fakeHandlers = ['*' => $callback]; return $this; } foreach ($callback as $command => $handler) { $this->fakeHandlers[is_numeric($command) ? '*' : $command] = $handler instanceof Closure ? $handler : fn () => $handler; } return $this; } /** * Determine if the process factory has fake process handlers and is recording processes. * * @return bool */ public function isRecording() { return $this->recording; } /** * Record the given process if processes should be recorded. * * @param \Illuminate\Process\PendingProcess $process * @param \Illuminate\Contracts\Process\ProcessResult $result * @return $this */ public function recordIfRecording(PendingProcess $process, ProcessResultContract $result) { if ($this->isRecording()) { $this->record($process, $result); } return $this; } /** * Record the given process. * * @param \Illuminate\Process\PendingProcess $process * @param \Illuminate\Contracts\Process\ProcessResult $result * @return $this */ public function record(PendingProcess $process, ProcessResultContract $result) { $this->recorded[] = [$process, $result]; return $this; } /** * Indicate that an exception should be thrown if any process is not faked. * * @param bool $prevent * @return $this */ public function preventStrayProcesses(bool $prevent = true) { $this->preventStrayProcesses = $prevent; return $this; } /** * Determine if stray processes are being prevented. * * @return bool */ public function preventingStrayProcesses() { return $this->preventStrayProcesses; } /** * Assert that a process was recorded matching a given truth test. * * @param \Closure|string $callback * @return $this */ public function assertRan(Closure|string $callback) { $callback = is_string($callback) ? fn ($process) => $process->command === $callback : $callback; PHPUnit::assertTrue( collect($this->recorded)->filter(function ($pair) use ($callback) { return $callback($pair[0], $pair[1]); })->count() > 0, 'An expected process was not invoked.' ); return $this; } /** * Assert that a process was recorded a given number of times matching a given truth test. * * @param \Closure|string $callback * @param int $times * @return $this */ public function assertRanTimes(Closure|string $callback, int $times = 1) { $callback = is_string($callback) ? fn ($process) => $process->command === $callback : $callback; $count = collect($this->recorded)->filter(function ($pair) use ($callback) { return $callback($pair[0], $pair[1]); })->count(); PHPUnit::assertSame( $times, $count, "An expected process ran {$count} times instead of {$times} times." ); return $this; } /** * Assert that a process was not recorded matching a given truth test. * * @param \Closure|string $callback * @return $this */ public function assertNotRan(Closure|string $callback) { $callback = is_string($callback) ? fn ($process) => $process->command === $callback : $callback; PHPUnit::assertTrue( collect($this->recorded)->filter(function ($pair) use ($callback) { return $callback($pair[0], $pair[1]); })->count() === 0, 'An unexpected process was invoked.' ); return $this; } /** * Assert that a process was not recorded matching a given truth test. * * @param \Closure|string $callback * @return $this */ public function assertDidntRun(Closure|string $callback) { return $this->assertNotRan($callback); } /** * Assert that no processes were recorded. * * @return $this */ public function assertNothingRan() { PHPUnit::assertEmpty( $this->recorded, 'An unexpected process was invoked.' ); return $this; } /** * Start defining a pool of processes. * * @param callable $callback * @return \Illuminate\Process\Pool */ public function pool(callable $callback) { return new Pool($this, $callback); } /** * Start defining a series of piped processes. * * @param callable|array $callback * @return \Illuminate\Contracts\Process\ProcessResult */ public function pipe(callable|array $callback, ?callable $output = null) { return is_array($callback) ? (new Pipe($this, fn ($pipe) => collect($callback)->each( fn ($command) => $pipe->command($command) )))->run(output: $output) : (new Pipe($this, $callback))->run(output: $output); } /** * Run a pool of processes and wait for them to finish executing. * * @param callable $callback * @param callable|null $output * @return \Illuminate\Process\ProcessPoolResults */ public function concurrently(callable $callback, ?callable $output = null) { return (new Pool($this, $callback))->start($output)->wait(); } /** * Create a new pending process associated with this factory. * * @return \Illuminate\Process\PendingProcess */ public function newPendingProcess() { return (new PendingProcess($this))->withFakeHandlers($this->fakeHandlers); } /** * Dynamically proxy methods to a new pending process instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } return $this->newPendingProcess()->{$method}(...$parameters); } } src/Illuminate/Process/LICENSE.md 0000644 00000002063 15107327040 0012460 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Process/FakeProcessResult.php 0000644 00000010524 15107327040 0015172 0 ustar 00 <?php namespace Illuminate\Process; use Illuminate\Contracts\Process\ProcessResult as ProcessResultContract; use Illuminate\Process\Exceptions\ProcessFailedException; class FakeProcessResult implements ProcessResultContract { /** * The command string. * * @var string */ protected $command; /** * The process exit code. * * @var int */ protected $exitCode; /** * The process output. * * @var string */ protected $output = ''; /** * The process error output. * * @var string */ protected $errorOutput = ''; /** * Create a new process result instance. * * @param string $command * @param int $exitCode * @param array|string $output * @param array|string $errorOutput * @return void */ public function __construct(string $command = '', int $exitCode = 0, array|string $output = '', array|string $errorOutput = '') { $this->command = $command; $this->exitCode = $exitCode; $this->output = $this->normalizeOutput($output); $this->errorOutput = $this->normalizeOutput($errorOutput); } /** * Normalize the given output into a string with newlines. * * @param array|string $output * @return string */ protected function normalizeOutput(array|string $output) { if (empty($output)) { return ''; } elseif (is_string($output)) { return rtrim($output, "\n")."\n"; } elseif (is_array($output)) { return rtrim( collect($output) ->map(fn ($line) => rtrim($line, "\n")."\n") ->implode(''), "\n" ); } } /** * Get the original command executed by the process. * * @return string */ public function command() { return $this->command; } /** * Create a new fake process result with the given command. * * @param string $command * @return self */ public function withCommand(string $command) { return new FakeProcessResult($command, $this->exitCode, $this->output, $this->errorOutput); } /** * Determine if the process was successful. * * @return bool */ public function successful() { return $this->exitCode === 0; } /** * Determine if the process failed. * * @return bool */ public function failed() { return ! $this->successful(); } /** * Get the exit code of the process. * * @return int */ public function exitCode() { return $this->exitCode; } /** * Get the standard output of the process. * * @return string */ public function output() { return $this->output; } /** * Determine if the output contains the given string. * * @param string $output * @return bool */ public function seeInOutput(string $output) { return str_contains($this->output(), $output); } /** * Get the error output of the process. * * @return string */ public function errorOutput() { return $this->errorOutput; } /** * Determine if the error output contains the given string. * * @param string $output * @return bool */ public function seeInErrorOutput(string $output) { return str_contains($this->errorOutput(), $output); } /** * Throw an exception if the process failed. * * @param callable|null $callback * @return $this */ public function throw(callable $callback = null) { if ($this->successful()) { return $this; } $exception = new ProcessFailedException($this); if ($callback) { $callback($this, $exception); } throw $exception; } /** * Throw an exception if the process failed and the given condition is true. * * @param bool $condition * @param callable|null $callback * @return $this */ public function throwIf(bool $condition, callable $callback = null) { if ($condition) { return $this->throw($callback); } return $this; } } src/Illuminate/Process/PendingProcess.php 0000644 00000025654 15107327040 0014523 0 ustar 00 <?php namespace Illuminate\Process; use Closure; use Illuminate\Process\Exceptions\ProcessTimedOutException; use Illuminate\Support\Str; use Illuminate\Support\Traits\Conditionable; use LogicException; use RuntimeException; use Symfony\Component\Process\Exception\ProcessTimedOutException as SymfonyTimeoutException; use Symfony\Component\Process\Process; class PendingProcess { use Conditionable; /** * The process factory instance. * * @var \Illuminate\Process\Factory */ protected $factory; /** * The command to invoke the process. * * @var array<array-key, string>|string|null */ public $command; /** * The working directory of the process. * * @var string|null */ public $path; /** * The maximum number of seconds the process may run. * * @var int|null */ public $timeout = 60; /** * The maximum number of seconds the process may go without returning output. * * @var int */ public $idleTimeout; /** * The additional environment variables for the process. * * @var array */ public $environment = []; /** * The standard input data that should be piped into the command. * * @var string|int|float|bool|resource|\Traversable|null */ public $input; /** * Indicates whether output should be disabled for the process. * * @var bool */ public $quietly = false; /** * Indicates if TTY mode should be enabled. * * @var bool */ public $tty = false; /** * The options that will be passed to "proc_open". * * @var array */ public $options = []; /** * The registered fake handler callbacks. * * @var array */ protected $fakeHandlers = []; /** * Create a new pending process instance. * * @param \Illuminate\Process\Factory $factory * @return void */ public function __construct(Factory $factory) { $this->factory = $factory; } /** * Specify the command that will invoke the process. * * @param array<array-key, string>|string $command * @return $this */ public function command(array|string $command) { $this->command = $command; return $this; } /** * Specify the working directory of the process. * * @param string $path * @return $this */ public function path(string $path) { $this->path = $path; return $this; } /** * Specify the maximum number of seconds the process may run. * * @param int $timeout * @return $this */ public function timeout(int $timeout) { $this->timeout = $timeout; return $this; } /** * Specify the maximum number of seconds a process may go without returning output. * * @param int $timeout * @return $this */ public function idleTimeout(int $timeout) { $this->idleTimeout = $timeout; return $this; } /** * Indicate that the process may run forever without timing out. * * @return $this */ public function forever() { $this->timeout = null; return $this; } /** * Set the additional environment variables for the process. * * @param array $environment * @return $this */ public function env(array $environment) { $this->environment = $environment; return $this; } /** * Set the standard input that should be provided when invoking the process. * * @param \Traversable|resource|string|int|float|bool|null $input * @return $this */ public function input($input) { $this->input = $input; return $this; } /** * Disable output for the process. * * @return $this */ public function quietly() { $this->quietly = true; return $this; } /** * Enable TTY mode for the process. * * @param bool $tty * @return $this */ public function tty(bool $tty = true) { $this->tty = $tty; return $this; } /** * Set the "proc_open" options that should be used when invoking the process. * * @param array $options * @return $this */ public function options(array $options) { $this->options = $options; return $this; } /** * Run the process. * * @param array<array-key, string>|string|null $command * @param callable|null $output * @return \Illuminate\Contracts\Process\ProcessResult */ public function run(array|string $command = null, callable $output = null) { $this->command = $command ?: $this->command; try { $process = $this->toSymfonyProcess($command); if ($fake = $this->fakeFor($command = $process->getCommandline())) { return tap($this->resolveSynchronousFake($command, $fake), function ($result) { $this->factory->recordIfRecording($this, $result); }); } elseif ($this->factory->isRecording() && $this->factory->preventingStrayProcesses()) { throw new RuntimeException('Attempted process ['.$command.'] without a matching fake.'); } return new ProcessResult(tap($process)->run($output)); } catch (SymfonyTimeoutException $e) { throw new ProcessTimedOutException($e, new ProcessResult($process)); } } /** * Start the process in the background. * * @param array<array-key, string>|string|null $command * @param callable $output * @return \Illuminate\Process\InvokedProcess */ public function start(array|string $command = null, callable $output = null) { $this->command = $command ?: $this->command; $process = $this->toSymfonyProcess($command); if ($fake = $this->fakeFor($command = $process->getCommandline())) { return tap($this->resolveAsynchronousFake($command, $output, $fake), function (FakeInvokedProcess $process) { $this->factory->recordIfRecording($this, $process->predictProcessResult()); }); } elseif ($this->factory->isRecording() && $this->factory->preventingStrayProcesses()) { throw new RuntimeException('Attempted process ['.$command.'] without a matching fake.'); } return new InvokedProcess(tap($process)->start($output)); } /** * Get a Symfony Process instance from the current pending command. * * @param array<array-key, string>|string|null $command * @return \Symfony\Component\Process\Process */ protected function toSymfonyProcess(array|string|null $command) { $command = $command ?? $this->command; $process = is_iterable($command) ? new Process($command, null, $this->environment) : Process::fromShellCommandline((string) $command, null, $this->environment); $process->setWorkingDirectory((string) ($this->path ?? getcwd())); $process->setTimeout($this->timeout); if ($this->idleTimeout) { $process->setIdleTimeout($this->idleTimeout); } if ($this->input) { $process->setInput($this->input); } if ($this->quietly) { $process->disableOutput(); } if ($this->tty) { $process->setTty(true); } if (! empty($this->options)) { $process->setOptions($this->options); } return $process; } /** * Specify the fake process result handlers for the pending process. * * @param array $fakeHandlers * @return $this */ public function withFakeHandlers(array $fakeHandlers) { $this->fakeHandlers = $fakeHandlers; return $this; } /** * Get the fake handler for the given command, if applicable. * * @param string $command * @return \Closure|null */ protected function fakeFor(string $command) { return collect($this->fakeHandlers) ->first(fn ($handler, $pattern) => Str::is($pattern, $command)); } /** * Resolve the given fake handler for a synchronous process. * * @param string $command * @param \Closure $fake * @return mixed */ protected function resolveSynchronousFake(string $command, Closure $fake) { $result = $fake($this); if (is_string($result) || is_array($result)) { return (new FakeProcessResult(output: $result))->withCommand($command); } return match (true) { $result instanceof ProcessResult => $result, $result instanceof FakeProcessResult => $result->withCommand($command), $result instanceof FakeProcessDescription => $result->toProcessResult($command), $result instanceof FakeProcessSequence => $this->resolveSynchronousFake($command, fn () => $result()), default => throw new LogicException('Unsupported synchronous process fake result provided.'), }; } /** * Resolve the given fake handler for an asynchronous process. * * @param string $command * @param callable|null $output * @param \Closure $fake * @return \Illuminate\Process\FakeInvokedProcess */ protected function resolveAsynchronousFake(string $command, ?callable $output, Closure $fake) { $result = $fake($this); if (is_string($result) || is_array($result)) { $result = new FakeProcessResult(output: $result); } if ($result instanceof ProcessResult) { return (new FakeInvokedProcess( $command, (new FakeProcessDescription) ->replaceOutput($result->output()) ->replaceErrorOutput($result->errorOutput()) ->runsFor(iterations: 0) ->exitCode($result->exitCode()) ))->withOutputHandler($output); } elseif ($result instanceof FakeProcessResult) { return (new FakeInvokedProcess( $command, (new FakeProcessDescription) ->replaceOutput($result->output()) ->replaceErrorOutput($result->errorOutput()) ->runsFor(iterations: 0) ->exitCode($result->exitCode()) ))->withOutputHandler($output); } elseif ($result instanceof FakeProcessDescription) { return (new FakeInvokedProcess($command, $result))->withOutputHandler($output); } elseif ($result instanceof FakeProcessSequence) { return $this->resolveAsynchronousFake($command, $output, fn () => $result()); } throw new LogicException('Unsupported asynchronous process fake result provided.'); } } src/Illuminate/Process/InvokedProcess.php 0000644 00000005027 15107327040 0014526 0 ustar 00 <?php namespace Illuminate\Process; use Illuminate\Contracts\Process\InvokedProcess as InvokedProcessContract; use Illuminate\Process\Exceptions\ProcessTimedOutException; use Symfony\Component\Process\Exception\ProcessTimedOutException as SymfonyTimeoutException; use Symfony\Component\Process\Process; class InvokedProcess implements InvokedProcessContract { /** * The underlying process instance. * * @var \Symfony\Component\Process\Process */ protected $process; /** * Create a new invoked process instance. * * @param \Symfony\Component\Process\Process $process * @return void */ public function __construct(Process $process) { $this->process = $process; } /** * Get the process ID if the process is still running. * * @return int|null */ public function id() { return $this->process->getPid(); } /** * Send a signal to the process. * * @param int $signal * @return $this */ public function signal(int $signal) { $this->process->signal($signal); return $this; } /** * Determine if the process is still running. * * @return bool */ public function running() { return $this->process->isRunning(); } /** * Get the standard output for the process. * * @return string */ public function output() { return $this->process->getOutput(); } /** * Get the error output for the process. * * @return string */ public function errorOutput() { return $this->process->getErrorOutput(); } /** * Get the latest standard output for the process. * * @return string */ public function latestOutput() { return $this->process->getIncrementalOutput(); } /** * Get the latest error output for the process. * * @return string */ public function latestErrorOutput() { return $this->process->getIncrementalErrorOutput(); } /** * Wait for the process to finish. * * @param callable|null $output * @return \Illuminate\Process\ProcessResult */ public function wait(callable $output = null) { try { $this->process->wait($output); return new ProcessResult($this->process); } catch (SymfonyTimeoutException $e) { throw new ProcessTimedOutException($e, new ProcessResult($this->process)); } } } src/Illuminate/Process/Pipe.php 0000644 00000005432 15107327040 0012465 0 ustar 00 <?php namespace Illuminate\Process; use InvalidArgumentException; /** * @mixin \Illuminate\Process\Factory * @mixin \Illuminate\Process\PendingProcess */ class Pipe { /** * The process factory instance. * * @var \Illuminate\Process\Factory */ protected $factory; /** * The callback that resolves the pending processes. * * @var callable */ protected $callback; /** * The array of pending processes. * * @var array */ protected $pendingProcesses = []; /** * Create a new series of piped processes. * * @param \Illuminate\Process\Factory $factory * @param callable $callback * @return void */ public function __construct(Factory $factory, callable $callback) { $this->factory = $factory; $this->callback = $callback; } /** * Add a process to the pipe with a key. * * @param string $key * @return \Illuminate\Process\PendingProcess */ public function as(string $key) { return tap($this->factory->newPendingProcess(), function ($pendingProcess) use ($key) { $this->pendingProcesses[$key] = $pendingProcess; }); } /** * Runs the processes in the pipe. * * @param callable|null $output * @return \Illuminate\Contracts\Process\ProcessResult */ public function run(?callable $output = null) { call_user_func($this->callback, $this); return collect($this->pendingProcesses) ->reduce(function ($previousProcessResult, $pendingProcess, $key) use ($output) { if (! $pendingProcess instanceof PendingProcess) { throw new InvalidArgumentException('Process pipe must only contain pending processes.'); } if ($previousProcessResult && $previousProcessResult->failed()) { return $previousProcessResult; } return $pendingProcess->when( $previousProcessResult, fn () => $pendingProcess->input($previousProcessResult->output()) )->run(output: $output ? function ($type, $buffer) use ($key, $output) { $output($type, $buffer, $key); } : null); }); } /** * Dynamically proxy methods calls to a new pending process. * * @param string $method * @param array $parameters * @return \Illuminate\Process\PendingProcess */ public function __call($method, $parameters) { return tap($this->factory->{$method}(...$parameters), function ($pendingProcess) { $this->pendingProcesses[] = $pendingProcess; }); } } src/Illuminate/Process/InvokedProcessPool.php 0000644 00000002667 15107327040 0015367 0 ustar 00 <?php namespace Illuminate\Process; use Countable; class InvokedProcessPool implements Countable { /** * The array of invoked processes. * * @var array */ protected $invokedProcesses; /** * Create a new invoked process pool. * * @param array $invokedProcesses * @return void */ public function __construct(array $invokedProcesses) { $this->invokedProcesses = $invokedProcesses; } /** * Send a signal to each running process in the pool, returning the processes that were signalled. * * @param int $signal * @return \Illuminate\Support\Collection */ public function signal(int $signal) { return $this->running()->each->signal($signal); } /** * Get the processes in the pool that are still currently running. * * @return \Illuminate\Support\Collection */ public function running() { return collect($this->invokedProcesses)->filter->running()->values(); } /** * Wait for the processes to finish. * * @return \Illuminate\Process\ProcessPoolResults */ public function wait() { return new ProcessPoolResults(collect($this->invokedProcesses)->map->wait()->all()); } /** * Get the total number of processes. * * @return int */ public function count(): int { return count($this->invokedProcesses); } } src/Illuminate/Support/MultipleInstanceManager.php 0000644 00000010377 15107327040 0016405 0 ustar 00 <?php namespace Illuminate\Support; use Closure; use InvalidArgumentException; use RuntimeException; abstract class MultipleInstanceManager { /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The array of resolved instances. * * @var array */ protected $instances = []; /** * The registered custom instance creators. * * @var array */ protected $customCreators = []; /** * Create a new manager instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function __construct($app) { $this->app = $app; } /** * Get the default instance name. * * @return string */ abstract public function getDefaultInstance(); /** * Set the default instance name. * * @param string $name * @return void */ abstract public function setDefaultInstance($name); /** * Get the instance specific configuration. * * @param string $name * @return array */ abstract public function getInstanceConfig($name); /** * Get an instance instance by name. * * @param string|null $name * @return mixed */ public function instance($name = null) { $name = $name ?: $this->getDefaultInstance(); return $this->instances[$name] = $this->get($name); } /** * Attempt to get an instance from the local cache. * * @param string $name * @return mixed */ protected function get($name) { return $this->instances[$name] ?? $this->resolve($name); } /** * Resolve the given instance. * * @param string $name * @return mixed * * @throws \InvalidArgumentException */ protected function resolve($name) { $config = $this->getInstanceConfig($name); if (is_null($config)) { throw new InvalidArgumentException("Instance [{$name}] is not defined."); } if (! array_key_exists('driver', $config)) { throw new RuntimeException("Instance [{$name}] does not specify a driver."); } if (isset($this->customCreators[$config['driver']])) { return $this->callCustomCreator($config); } else { $driverMethod = 'create'.ucfirst($config['driver']).'Driver'; if (method_exists($this, $driverMethod)) { return $this->{$driverMethod}($config); } else { throw new InvalidArgumentException("Instance driver [{$config['driver']}] is not supported."); } } } /** * Call a custom instance creator. * * @param array $config * @return mixed */ protected function callCustomCreator(array $config) { return $this->customCreators[$config['driver']]($this->app, $config); } /** * Unset the given instances. * * @param array|string|null $name * @return $this */ public function forgetInstance($name = null) { $name ??= $this->getDefaultInstance(); foreach ((array) $name as $instanceName) { if (isset($this->instances[$instanceName])) { unset($this->instances[$instanceName]); } } return $this; } /** * Disconnect the given instance and remove from local cache. * * @param string|null $name * @return void */ public function purge($name = null) { $name ??= $this->getDefaultInstance(); unset($this->instances[$name]); } /** * Register a custom instance creator Closure. * * @param string $name * @param \Closure $callback * @return $this */ public function extend($name, Closure $callback) { $this->customCreators[$name] = $callback->bindTo($this, $this); return $this; } /** * Dynamically call the default instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->instance()->$method(...$parameters); } } src/Illuminate/Support/helpers.php 0000644 00000024441 15107327040 0013271 0 ustar 00 <?php use Illuminate\Contracts\Support\DeferringDisplayableValue; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\Arr; use Illuminate\Support\Env; use Illuminate\Support\HigherOrderTapProxy; use Illuminate\Support\Optional; use Illuminate\Support\Sleep; use Illuminate\Support\Str; if (! function_exists('append_config')) { /** * Assign high numeric IDs to a config item to force appending. * * @param array $array * @return array */ function append_config(array $array) { $start = 9999; foreach ($array as $key => $value) { if (is_numeric($key)) { $start++; $array[$start] = Arr::pull($array, $key); } } return $array; } } if (! function_exists('blank')) { /** * Determine if the given value is "blank". * * @param mixed $value * @return bool */ function blank($value) { if (is_null($value)) { return true; } if (is_string($value)) { return trim($value) === ''; } if (is_numeric($value) || is_bool($value)) { return false; } if ($value instanceof Countable) { return count($value) === 0; } return empty($value); } } if (! function_exists('class_basename')) { /** * Get the class "basename" of the given object / class. * * @param string|object $class * @return string */ function class_basename($class) { $class = is_object($class) ? get_class($class) : $class; return basename(str_replace('\\', '/', $class)); } } if (! function_exists('class_uses_recursive')) { /** * Returns all traits used by a class, its parent classes and trait of their traits. * * @param object|string $class * @return array */ function class_uses_recursive($class) { if (is_object($class)) { $class = get_class($class); } $results = []; foreach (array_reverse(class_parents($class)) + [$class => $class] as $class) { $results += trait_uses_recursive($class); } return array_unique($results); } } if (! function_exists('e')) { /** * Encode HTML special characters in a string. * * @param \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|\BackedEnum|string|null $value * @param bool $doubleEncode * @return string */ function e($value, $doubleEncode = true) { if ($value instanceof DeferringDisplayableValue) { $value = $value->resolveDisplayableValue(); } if ($value instanceof Htmlable) { return $value->toHtml(); } if ($value instanceof BackedEnum) { $value = $value->value; } return htmlspecialchars($value ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', $doubleEncode); } } if (! function_exists('env')) { /** * Gets the value of an environment variable. * * @param string $key * @param mixed $default * @return mixed */ function env($key, $default = null) { return Env::get($key, $default); } } if (! function_exists('filled')) { /** * Determine if a value is "filled". * * @param mixed $value * @return bool */ function filled($value) { return ! blank($value); } } if (! function_exists('object_get')) { /** * Get an item from an object using "dot" notation. * * @param object $object * @param string|null $key * @param mixed $default * @return mixed */ function object_get($object, $key, $default = null) { if (is_null($key) || trim($key) === '') { return $object; } foreach (explode('.', $key) as $segment) { if (! is_object($object) || ! isset($object->{$segment})) { return value($default); } $object = $object->{$segment}; } return $object; } } if (! function_exists('optional')) { /** * Provide access to optional objects. * * @param mixed $value * @param callable|null $callback * @return mixed */ function optional($value = null, callable $callback = null) { if (is_null($callback)) { return new Optional($value); } elseif (! is_null($value)) { return $callback($value); } } } if (! function_exists('preg_replace_array')) { /** * Replace a given pattern with each value in the array in sequentially. * * @param string $pattern * @param array $replacements * @param string $subject * @return string */ function preg_replace_array($pattern, array $replacements, $subject) { return preg_replace_callback($pattern, function () use (&$replacements) { foreach ($replacements as $value) { return array_shift($replacements); } }, $subject); } } if (! function_exists('retry')) { /** * Retry an operation a given number of times. * * @param int|array $times * @param callable $callback * @param int|\Closure $sleepMilliseconds * @param callable|null $when * @return mixed * * @throws \Exception */ function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null) { $attempts = 0; $backoff = []; if (is_array($times)) { $backoff = $times; $times = count($times) + 1; } beginning: $attempts++; $times--; try { return $callback($attempts); } catch (Exception $e) { if ($times < 1 || ($when && ! $when($e))) { throw $e; } $sleepMilliseconds = $backoff[$attempts - 1] ?? $sleepMilliseconds; if ($sleepMilliseconds) { Sleep::usleep(value($sleepMilliseconds, $attempts, $e) * 1000); } goto beginning; } } } if (! function_exists('str')) { /** * Get a new stringable object from the given string. * * @param string|null $string * @return \Illuminate\Support\Stringable|mixed */ function str($string = null) { if (func_num_args() === 0) { return new class { public function __call($method, $parameters) { return Str::$method(...$parameters); } public function __toString() { return ''; } }; } return Str::of($string); } } if (! function_exists('tap')) { /** * Call the given Closure with the given value then return the value. * * @param mixed $value * @param callable|null $callback * @return mixed */ function tap($value, $callback = null) { if (is_null($callback)) { return new HigherOrderTapProxy($value); } $callback($value); return $value; } } if (! function_exists('throw_if')) { /** * Throw the given exception if the given condition is true. * * @template TException of \Throwable * * @param mixed $condition * @param TException|class-string<TException>|string $exception * @param mixed ...$parameters * @return mixed * * @throws TException */ function throw_if($condition, $exception = 'RuntimeException', ...$parameters) { if ($condition) { if (is_string($exception) && class_exists($exception)) { $exception = new $exception(...$parameters); } throw is_string($exception) ? new RuntimeException($exception) : $exception; } return $condition; } } if (! function_exists('throw_unless')) { /** * Throw the given exception unless the given condition is true. * * @template TException of \Throwable * * @param mixed $condition * @param TException|class-string<TException>|string $exception * @param mixed ...$parameters * @return mixed * * @throws TException */ function throw_unless($condition, $exception = 'RuntimeException', ...$parameters) { throw_if(! $condition, $exception, ...$parameters); return $condition; } } if (! function_exists('trait_uses_recursive')) { /** * Returns all traits used by a trait and its traits. * * @param object|string $trait * @return array */ function trait_uses_recursive($trait) { $traits = class_uses($trait) ?: []; foreach ($traits as $trait) { $traits += trait_uses_recursive($trait); } return $traits; } } if (! function_exists('transform')) { /** * Transform the given value if it is present. * * @template TValue of mixed * @template TReturn of mixed * @template TDefault of mixed * * @param TValue $value * @param callable(TValue): TReturn $callback * @param TDefault|callable(TValue): TDefault|null $default * @return ($value is empty ? ($default is null ? null : TDefault) : TReturn) */ function transform($value, callable $callback, $default = null) { if (filled($value)) { return $callback($value); } if (is_callable($default)) { return $default($value); } return $default; } } if (! function_exists('windows_os')) { /** * Determine whether the current environment is Windows based. * * @return bool */ function windows_os() { return PHP_OS_FAMILY === 'Windows'; } } if (! function_exists('with')) { /** * Return the given value, optionally passed through the given callback. * * @template TValue * @template TReturn * * @param TValue $value * @param (callable(TValue): (TReturn))|null $callback * @return ($callback is null ? TValue : TReturn) */ function with($value, callable $callback = null) { return is_null($callback) ? $value : $callback($value); } } src/Illuminate/Support/Carbon.php 0000644 00000002406 15107327040 0013030 0 ustar 00 <?php namespace Illuminate\Support; use Carbon\Carbon as BaseCarbon; use Carbon\CarbonImmutable as BaseCarbonImmutable; use Illuminate\Support\Traits\Conditionable; use Ramsey\Uuid\Uuid; use Symfony\Component\Uid\Ulid; class Carbon extends BaseCarbon { use Conditionable; /** * {@inheritdoc} */ public static function setTestNow($testNow = null) { BaseCarbon::setTestNow($testNow); BaseCarbonImmutable::setTestNow($testNow); } /** * Create a Carbon instance from a given ordered UUID or ULID. * * @param \Ramsey\Uuid\Uuid|\Symfony\Component\Uid\Ulid|string $id * @return \Illuminate\Support\Carbon */ public static function createFromId($id) { return Ulid::isValid($id) ? static::createFromInterface(Ulid::fromString($id)->getDateTime()) : static::createFromInterface(Uuid::fromString($id)->getDateTime()); } /** * Dump the instance and end the script. * * @param mixed ...$args * @return never */ public function dd(...$args) { dd($this, ...$args); } /** * Dump the instance. * * @return $this */ public function dump() { dump($this); return $this; } } src/Illuminate/Support/Sleep.php 0000644 00000023722 15107327040 0012700 0 ustar 00 <?php namespace Illuminate\Support; use Carbon\Carbon; use Carbon\CarbonInterval; use DateInterval; use Illuminate\Support\Traits\Macroable; use PHPUnit\Framework\Assert as PHPUnit; use RuntimeException; class Sleep { use Macroable; /** * The fake sleep callbacks. * * @var array */ public static $fakeSleepCallbacks = []; /** * The total duration to sleep. * * @var \Carbon\CarbonInterval */ public $duration; /** * The pending duration to sleep. * * @var int|float|null */ protected $pending = null; /** * Indicates that all sleeping should be faked. * * @var bool */ protected static $fake = false; /** * The sequence of sleep durations encountered while faking. * * @var array<int, \Carbon\CarbonInterval> */ protected static $sequence = []; /** * Indicates if the instance should sleep. * * @var bool */ protected $shouldSleep = true; /** * Create a new class instance. * * @param int|float|\DateInterval $duration * @return void */ public function __construct($duration) { $this->duration($duration); } /** * Sleep for the given duration. * * @param \DateInterval|int|float $duration * @return static */ public static function for($duration) { return new static($duration); } /** * Sleep until the given timestamp. * * @param \DateTimeInterface|int $timestamp * @return static */ public static function until($timestamp) { if (is_int($timestamp)) { $timestamp = Carbon::createFromTimestamp($timestamp); } return new static(Carbon::now()->diff($timestamp)); } /** * Sleep for the given number of microseconds. * * @param int $duration * @return static */ public static function usleep($duration) { return (new static($duration))->microseconds(); } /** * Sleep for the given number of seconds. * * @param int|float $duration * @return static */ public static function sleep($duration) { return (new static($duration))->seconds(); } /** * Sleep for the given duration. Replaces any previously defined duration. * * @param \DateInterval|int|float $duration * @return $this */ protected function duration($duration) { if (! $duration instanceof DateInterval) { $this->duration = CarbonInterval::microsecond(0); $this->pending = $duration; } else { $duration = CarbonInterval::instance($duration); if ($duration->totalMicroseconds < 0) { $duration = CarbonInterval::seconds(0); } $this->duration = $duration; $this->pending = null; } return $this; } /** * Sleep for the given number of minutes. * * @return $this */ public function minutes() { $this->duration->add('minutes', $this->pullPending()); return $this; } /** * Sleep for one minute. * * @return $this */ public function minute() { return $this->minutes(); } /** * Sleep for the given number of seconds. * * @return $this */ public function seconds() { $this->duration->add('seconds', $this->pullPending()); return $this; } /** * Sleep for one second. * * @return $this */ public function second() { return $this->seconds(); } /** * Sleep for the given number of milliseconds. * * @return $this */ public function milliseconds() { $this->duration->add('milliseconds', $this->pullPending()); return $this; } /** * Sleep for one millisecond. * * @return $this */ public function millisecond() { return $this->milliseconds(); } /** * Sleep for the given number of microseconds. * * @return $this */ public function microseconds() { $this->duration->add('microseconds', $this->pullPending()); return $this; } /** * Sleep for on microsecond. * * @return $this */ public function microsecond() { return $this->microseconds(); } /** * Add additional time to sleep for. * * @param int|float $duration * @return $this */ public function and($duration) { $this->pending = $duration; return $this; } /** * Handle the object's destruction. * * @return void */ public function __destruct() { if (! $this->shouldSleep) { return; } if ($this->pending !== null) { throw new RuntimeException('Unknown duration unit.'); } if (static::$fake) { static::$sequence[] = $this->duration; foreach (static::$fakeSleepCallbacks as $callback) { $callback($this->duration); } return; } $remaining = $this->duration->copy(); $seconds = (int) $remaining->totalSeconds; if ($seconds > 0) { sleep($seconds); $remaining = $remaining->subSeconds($seconds); } $microseconds = (int) $remaining->totalMicroseconds; if ($microseconds > 0) { usleep($microseconds); } } /** * Resolve the pending duration. * * @return int|float */ protected function pullPending() { if ($this->pending === null) { $this->shouldNotSleep(); throw new RuntimeException('No duration specified.'); } if ($this->pending < 0) { $this->pending = 0; } return tap($this->pending, function () { $this->pending = null; }); } /** * Stay awake and capture any attempts to sleep. * * @param bool $value * @return void */ public static function fake($value = true) { static::$fake = $value; static::$sequence = []; static::$fakeSleepCallbacks = []; } /** * Assert a given amount of sleeping occurred a specific number of times. * * @param \Closure $expected * @param int $times * @return void */ public static function assertSlept($expected, $times = 1) { $count = collect(static::$sequence)->filter($expected)->count(); PHPUnit::assertSame( $times, $count, "The expected sleep was found [{$count}] times instead of [{$times}]." ); } /** * Assert sleeping occurred a given number of times. * * @param int $expected * @return void */ public static function assertSleptTimes($expected) { PHPUnit::assertSame($expected, $count = count(static::$sequence), "Expected [{$expected}] sleeps but found [{$count}]."); } /** * Assert the given sleep sequence was encountered. * * @param array $sequence * @return void */ public static function assertSequence($sequence) { static::assertSleptTimes(count($sequence)); collect($sequence) ->zip(static::$sequence) ->eachSpread(function (?Sleep $expected, CarbonInterval $actual) { if ($expected === null) { return; } PHPUnit::assertTrue( $expected->shouldNotSleep()->duration->equalTo($actual), vsprintf('Expected sleep duration of [%s] but actually slept for [%s].', [ $expected->duration->cascade()->forHumans([ 'options' => 0, 'minimumUnit' => 'microsecond', ]), $actual->cascade()->forHumans([ 'options' => 0, 'minimumUnit' => 'microsecond', ]), ]) ); }); } /** * Assert that no sleeping occurred. * * @return void */ public static function assertNeverSlept() { return static::assertSleptTimes(0); } /** * Assert that no sleeping occurred. * * @return void */ public static function assertInsomniac() { if (static::$sequence === []) { PHPUnit::assertTrue(true); } foreach (static::$sequence as $duration) { PHPUnit::assertSame(0, $duration->totalMicroseconds, vsprintf('Unexpected sleep duration of [%s] found.', [ $duration->cascade()->forHumans([ 'options' => 0, 'minimumUnit' => 'microsecond', ]), ])); } } /** * Indicate that the instance should not sleep. * * @return $this */ protected function shouldNotSleep() { $this->shouldSleep = false; return $this; } /** * Only sleep when the given condition is true. * * @param (\Closure($this): bool)|bool $condition * @return $this */ public function when($condition) { $this->shouldSleep = (bool) value($condition, $this); return $this; } /** * Don't sleep when the given condition is true. * * @param (\Closure($this): bool)|bool $condition * @return $this */ public function unless($condition) { return $this->when(! value($condition, $this)); } /** * Specify a callback that should be invoked when faking sleep within a test. * * @param callable $callback * @return void */ public static function whenFakingSleep($callback) { static::$fakeSleepCallbacks[] = $callback; } } src/Illuminate/Support/Js.php 0000644 00000006703 15107327040 0012204 0 ustar 00 <?php namespace Illuminate\Support; use BackedEnum; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Contracts\Support\Jsonable; use JsonSerializable; class Js implements Htmlable { /** * The JavaScript string. * * @var string */ protected $js; /** * Flags that should be used when encoding to JSON. * * @var int */ protected const REQUIRED_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_THROW_ON_ERROR; /** * Create a new class instance. * * @param mixed $data * @param int|null $flags * @param int $depth * @return void * * @throws \JsonException */ public function __construct($data, $flags = 0, $depth = 512) { $this->js = $this->convertDataToJavaScriptExpression($data, $flags, $depth); } /** * Create a new JavaScript string from the given data. * * @param mixed $data * @param int $flags * @param int $depth * @return static * * @throws \JsonException */ public static function from($data, $flags = 0, $depth = 512) { return new static($data, $flags, $depth); } /** * Convert the given data to a JavaScript expression. * * @param mixed $data * @param int $flags * @param int $depth * @return string * * @throws \JsonException */ protected function convertDataToJavaScriptExpression($data, $flags = 0, $depth = 512) { if ($data instanceof self) { return $data->toHtml(); } if ($data instanceof BackedEnum) { $data = $data->value; } $json = static::encode($data, $flags, $depth); if (is_string($data)) { return "'".substr($json, 1, -1)."'"; } return $this->convertJsonToJavaScriptExpression($json, $flags); } /** * Encode the given data as JSON. * * @param mixed $data * @param int $flags * @param int $depth * @return string * * @throws \JsonException */ public static function encode($data, $flags = 0, $depth = 512) { if ($data instanceof Jsonable) { return $data->toJson($flags | static::REQUIRED_FLAGS); } if ($data instanceof Arrayable && ! ($data instanceof JsonSerializable)) { $data = $data->toArray(); } return json_encode($data, $flags | static::REQUIRED_FLAGS, $depth); } /** * Convert the given JSON to a JavaScript expression. * * @param string $json * @param int $flags * @return string * * @throws \JsonException */ protected function convertJsonToJavaScriptExpression($json, $flags = 0) { if ($json === '[]' || $json === '{}') { return $json; } if (Str::startsWith($json, ['"', '{', '['])) { return "JSON.parse('".substr(json_encode($json, $flags | static::REQUIRED_FLAGS), 1, -1)."')"; } return $json; } /** * Get the string representation of the data for use in HTML. * * @return string */ public function toHtml() { return $this->js; } /** * Get the string representation of the data for use in HTML. * * @return string */ public function __toString() { return $this->toHtml(); } } src/Illuminate/Support/Exceptions/MathException.php 0000644 00000000200 15107327040 0016503 0 ustar 00 <?php namespace Illuminate\Support\Exceptions; use RuntimeException; class MathException extends RuntimeException { // } src/Illuminate/Support/Reflector.php 0000644 00000010777 15107327040 0013563 0 ustar 00 <?php namespace Illuminate\Support; use ReflectionClass; use ReflectionEnum; use ReflectionMethod; use ReflectionNamedType; use ReflectionUnionType; class Reflector { /** * This is a PHP 7.4 compatible implementation of is_callable. * * @param mixed $var * @param bool $syntaxOnly * @return bool */ public static function isCallable($var, $syntaxOnly = false) { if (! is_array($var)) { return is_callable($var, $syntaxOnly); } if (! isset($var[0], $var[1]) || ! is_string($var[1] ?? null)) { return false; } if ($syntaxOnly && (is_string($var[0]) || is_object($var[0])) && is_string($var[1])) { return true; } $class = is_object($var[0]) ? get_class($var[0]) : $var[0]; $method = $var[1]; if (! class_exists($class)) { return false; } if (method_exists($class, $method)) { return (new ReflectionMethod($class, $method))->isPublic(); } if (is_object($var[0]) && method_exists($class, '__call')) { return (new ReflectionMethod($class, '__call'))->isPublic(); } if (! is_object($var[0]) && method_exists($class, '__callStatic')) { return (new ReflectionMethod($class, '__callStatic'))->isPublic(); } return false; } /** * Get the class name of the given parameter's type, if possible. * * @param \ReflectionParameter $parameter * @return string|null */ public static function getParameterClassName($parameter) { $type = $parameter->getType(); if (! $type instanceof ReflectionNamedType || $type->isBuiltin()) { return; } return static::getTypeName($parameter, $type); } /** * Get the class names of the given parameter's type, including union types. * * @param \ReflectionParameter $parameter * @return array */ public static function getParameterClassNames($parameter) { $type = $parameter->getType(); if (! $type instanceof ReflectionUnionType) { return array_filter([static::getParameterClassName($parameter)]); } $unionTypes = []; foreach ($type->getTypes() as $listedType) { if (! $listedType instanceof ReflectionNamedType || $listedType->isBuiltin()) { continue; } $unionTypes[] = static::getTypeName($parameter, $listedType); } return array_filter($unionTypes); } /** * Get the given type's class name. * * @param \ReflectionParameter $parameter * @param \ReflectionNamedType $type * @return string */ protected static function getTypeName($parameter, $type) { $name = $type->getName(); if (! is_null($class = $parameter->getDeclaringClass())) { if ($name === 'self') { return $class->getName(); } if ($name === 'parent' && $parent = $class->getParentClass()) { return $parent->getName(); } } return $name; } /** * Determine if the parameter's type is a subclass of the given type. * * @param \ReflectionParameter $parameter * @param string $className * @return bool */ public static function isParameterSubclassOf($parameter, $className) { $paramClassName = static::getParameterClassName($parameter); return $paramClassName && (class_exists($paramClassName) || interface_exists($paramClassName)) && (new ReflectionClass($paramClassName))->isSubclassOf($className); } /** * Determine if the parameter's type is a Backed Enum with a string backing type. * * @param \ReflectionParameter $parameter * @return bool */ public static function isParameterBackedEnumWithStringBackingType($parameter) { if (! $parameter->getType() instanceof ReflectionNamedType) { return false; } $backedEnumClass = $parameter->getType()?->getName(); if (is_null($backedEnumClass)) { return false; } if (enum_exists($backedEnumClass)) { $reflectionBackedEnum = new ReflectionEnum($backedEnumClass); return $reflectionBackedEnum->isBacked() && $reflectionBackedEnum->getBackingType()->getName() == 'string'; } return false; } } src/Illuminate/Support/ConfigurationUrlParser.php 0000644 00000010403 15107327040 0016267 0 ustar 00 <?php namespace Illuminate\Support; use InvalidArgumentException; class ConfigurationUrlParser { /** * The drivers aliases map. * * @var array */ protected static $driverAliases = [ 'mssql' => 'sqlsrv', 'mysql2' => 'mysql', // RDS 'postgres' => 'pgsql', 'postgresql' => 'pgsql', 'sqlite3' => 'sqlite', 'redis' => 'tcp', 'rediss' => 'tls', ]; /** * Parse the database configuration, hydrating options using a database configuration URL if possible. * * @param array|string $config * @return array */ public function parseConfiguration($config) { if (is_string($config)) { $config = ['url' => $config]; } $url = Arr::pull($config, 'url'); if (! $url) { return $config; } $rawComponents = $this->parseUrl($url); $decodedComponents = $this->parseStringsToNativeTypes( array_map('rawurldecode', $rawComponents) ); return array_merge( $config, $this->getPrimaryOptions($decodedComponents), $this->getQueryOptions($rawComponents) ); } /** * Get the primary database connection options. * * @param array $url * @return array */ protected function getPrimaryOptions($url) { return array_filter([ 'driver' => $this->getDriver($url), 'database' => $this->getDatabase($url), 'host' => $url['host'] ?? null, 'port' => $url['port'] ?? null, 'username' => $url['user'] ?? null, 'password' => $url['pass'] ?? null, ], fn ($value) => ! is_null($value)); } /** * Get the database driver from the URL. * * @param array $url * @return string|null */ protected function getDriver($url) { $alias = $url['scheme'] ?? null; if (! $alias) { return; } return static::$driverAliases[$alias] ?? $alias; } /** * Get the database name from the URL. * * @param array $url * @return string|null */ protected function getDatabase($url) { $path = $url['path'] ?? null; return $path && $path !== '/' ? substr($path, 1) : null; } /** * Get all of the additional database options from the query string. * * @param array $url * @return array */ protected function getQueryOptions($url) { $queryString = $url['query'] ?? null; if (! $queryString) { return []; } $query = []; parse_str($queryString, $query); return $this->parseStringsToNativeTypes($query); } /** * Parse the string URL to an array of components. * * @param string $url * @return array * * @throws \InvalidArgumentException */ protected function parseUrl($url) { $url = preg_replace('#^(sqlite3?):///#', '$1://null/', $url); $parsedUrl = parse_url($url); if ($parsedUrl === false) { throw new InvalidArgumentException('The database configuration URL is malformed.'); } return $parsedUrl; } /** * Convert string casted values to their native types. * * @param mixed $value * @return mixed */ protected function parseStringsToNativeTypes($value) { if (is_array($value)) { return array_map([$this, 'parseStringsToNativeTypes'], $value); } if (! is_string($value)) { return $value; } $parsedValue = json_decode($value, true); if (json_last_error() === JSON_ERROR_NONE) { return $parsedValue; } return $value; } /** * Get all of the current drivers' aliases. * * @return array */ public static function getDriverAliases() { return static::$driverAliases; } /** * Add the given driver alias to the driver aliases array. * * @param string $alias * @param string $driver * @return void */ public static function addDriverAlias($alias, $driver) { static::$driverAliases[$alias] = $driver; } } src/Illuminate/Support/NamespacedItemResolver.php 0000644 00000006544 15107327040 0016234 0 ustar 00 <?php namespace Illuminate\Support; class NamespacedItemResolver { /** * A cache of the parsed items. * * @var array */ protected $parsed = []; /** * Parse a key into namespace, group, and item. * * @param string $key * @return array */ public function parseKey($key) { // If we've already parsed the given key, we'll return the cached version we // already have, as this will save us some processing. We cache off every // key we parse so we can quickly return it on all subsequent requests. if (isset($this->parsed[$key])) { return $this->parsed[$key]; } // If the key does not contain a double colon, it means the key is not in a // namespace, and is just a regular configuration item. Namespaces are a // tool for organizing configuration items for things such as modules. if (! str_contains($key, '::')) { $segments = explode('.', $key); $parsed = $this->parseBasicSegments($segments); } else { $parsed = $this->parseNamespacedSegments($key); } // Once we have the parsed array of this key's elements, such as its groups // and namespace, we will cache each array inside a simple list that has // the key and the parsed array for quick look-ups for later requests. return $this->parsed[$key] = $parsed; } /** * Parse an array of basic segments. * * @param array $segments * @return array */ protected function parseBasicSegments(array $segments) { // The first segment in a basic array will always be the group, so we can go // ahead and grab that segment. If there is only one total segment we are // just pulling an entire group out of the array and not a single item. $group = $segments[0]; // If there is more than one segment in this group, it means we are pulling // a specific item out of a group and will need to return this item name // as well as the group so we know which item to pull from the arrays. $item = count($segments) === 1 ? null : implode('.', array_slice($segments, 1)); return [null, $group, $item]; } /** * Parse an array of namespaced segments. * * @param string $key * @return array */ protected function parseNamespacedSegments($key) { [$namespace, $item] = explode('::', $key); // First we'll just explode the first segment to get the namespace and group // since the item should be in the remaining segments. Once we have these // two pieces of data we can proceed with parsing out the item's value. $itemSegments = explode('.', $item); $groupAndItem = array_slice( $this->parseBasicSegments($itemSegments), 1 ); return array_merge([$namespace], $groupAndItem); } /** * Set the parsed value of a key. * * @param string $key * @param array $parsed * @return void */ public function setParsedKey($key, $parsed) { $this->parsed[$key] = $parsed; } /** * Flush the cache of parsed keys. * * @return void */ public function flushParsedKeys() { $this->parsed = []; } } src/Illuminate/Support/Str.php 0000644 00000141707 15107327040 0012404 0 ustar 00 <?php namespace Illuminate\Support; use Closure; use Illuminate\Support\Traits\Macroable; use JsonException; use League\CommonMark\Environment\Environment; use League\CommonMark\Extension\GithubFlavoredMarkdownExtension; use League\CommonMark\Extension\InlinesOnly\InlinesOnlyExtension; use League\CommonMark\GithubFlavoredMarkdownConverter; use League\CommonMark\MarkdownConverter; use Ramsey\Uuid\Codec\TimestampFirstCombCodec; use Ramsey\Uuid\Generator\CombGenerator; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidFactory; use Symfony\Component\Uid\Ulid; use Throwable; use Traversable; use voku\helper\ASCII; class Str { use Macroable; /** * The cache of snake-cased words. * * @var array */ protected static $snakeCache = []; /** * The cache of camel-cased words. * * @var array */ protected static $camelCache = []; /** * The cache of studly-cased words. * * @var array */ protected static $studlyCache = []; /** * The callback that should be used to generate UUIDs. * * @var callable|null */ protected static $uuidFactory; /** * The callback that should be used to generate ULIDs. * * @var callable|null */ protected static $ulidFactory; /** * The callback that should be used to generate random strings. * * @var callable|null */ protected static $randomStringFactory; /** * Get a new stringable object from the given string. * * @param string $string * @return \Illuminate\Support\Stringable */ public static function of($string) { return new Stringable($string); } /** * Return the remainder of a string after the first occurrence of a given value. * * @param string $subject * @param string $search * @return string */ public static function after($subject, $search) { return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0]; } /** * Return the remainder of a string after the last occurrence of a given value. * * @param string $subject * @param string $search * @return string */ public static function afterLast($subject, $search) { if ($search === '') { return $subject; } $position = strrpos($subject, (string) $search); if ($position === false) { return $subject; } return substr($subject, $position + strlen($search)); } /** * Transliterate a UTF-8 value to ASCII. * * @param string $value * @param string $language * @return string */ public static function ascii($value, $language = 'en') { return ASCII::to_ascii((string) $value, $language); } /** * Transliterate a string to its closest ASCII representation. * * @param string $string * @param string|null $unknown * @param bool|null $strict * @return string */ public static function transliterate($string, $unknown = '?', $strict = false) { return ASCII::to_transliterate($string, $unknown, $strict); } /** * Get the portion of a string before the first occurrence of a given value. * * @param string $subject * @param string $search * @return string */ public static function before($subject, $search) { if ($search === '') { return $subject; } $result = strstr($subject, (string) $search, true); return $result === false ? $subject : $result; } /** * Get the portion of a string before the last occurrence of a given value. * * @param string $subject * @param string $search * @return string */ public static function beforeLast($subject, $search) { if ($search === '') { return $subject; } $pos = mb_strrpos($subject, $search); if ($pos === false) { return $subject; } return static::substr($subject, 0, $pos); } /** * Get the portion of a string between two given values. * * @param string $subject * @param string $from * @param string $to * @return string */ public static function between($subject, $from, $to) { if ($from === '' || $to === '') { return $subject; } return static::beforeLast(static::after($subject, $from), $to); } /** * Get the smallest possible portion of a string between two given values. * * @param string $subject * @param string $from * @param string $to * @return string */ public static function betweenFirst($subject, $from, $to) { if ($from === '' || $to === '') { return $subject; } return static::before(static::after($subject, $from), $to); } /** * Convert a value to camel case. * * @param string $value * @return string */ public static function camel($value) { if (isset(static::$camelCache[$value])) { return static::$camelCache[$value]; } return static::$camelCache[$value] = lcfirst(static::studly($value)); } /** * Get the character at the specified index. * * @param string $subject * @param int $index * @return string|false */ public static function charAt($subject, $index) { $length = mb_strlen($subject); if ($index < 0 ? $index < -$length : $index > $length - 1) { return false; } return mb_substr($subject, $index, 1); } /** * Determine if a given string contains a given substring. * * @param string $haystack * @param string|iterable<string> $needles * @param bool $ignoreCase * @return bool */ public static function contains($haystack, $needles, $ignoreCase = false) { if ($ignoreCase) { $haystack = mb_strtolower($haystack); } if (! is_iterable($needles)) { $needles = (array) $needles; } foreach ($needles as $needle) { if ($ignoreCase) { $needle = mb_strtolower($needle); } if ($needle !== '' && str_contains($haystack, $needle)) { return true; } } return false; } /** * Determine if a given string contains all array values. * * @param string $haystack * @param iterable<string> $needles * @param bool $ignoreCase * @return bool */ public static function containsAll($haystack, $needles, $ignoreCase = false) { foreach ($needles as $needle) { if (! static::contains($haystack, $needle, $ignoreCase)) { return false; } } return true; } /** * Convert the case of a string. * * @param string $string * @param int $mode * @param string|null $encoding * @return string */ public static function convertCase(string $string, int $mode = MB_CASE_FOLD, ?string $encoding = 'UTF-8') { return mb_convert_case($string, $mode, $encoding); } /** * Determine if a given string ends with a given substring. * * @param string $haystack * @param string|iterable<string> $needles * @return bool */ public static function endsWith($haystack, $needles) { if (! is_iterable($needles)) { $needles = (array) $needles; } foreach ($needles as $needle) { if ((string) $needle !== '' && str_ends_with($haystack, $needle)) { return true; } } return false; } /** * Extracts an excerpt from text that matches the first instance of a phrase. * * @param string $text * @param string $phrase * @param array $options * @return string|null */ public static function excerpt($text, $phrase = '', $options = []) { $radius = $options['radius'] ?? 100; $omission = $options['omission'] ?? '...'; preg_match('/^(.*?)('.preg_quote((string) $phrase, '/').')(.*)$/iu', (string) $text, $matches); if (empty($matches)) { return null; } $start = ltrim($matches[1]); $start = str(mb_substr($start, max(mb_strlen($start, 'UTF-8') - $radius, 0), $radius, 'UTF-8'))->ltrim()->unless( fn ($startWithRadius) => $startWithRadius->exactly($start), fn ($startWithRadius) => $startWithRadius->prepend($omission), ); $end = rtrim($matches[3]); $end = str(mb_substr($end, 0, $radius, 'UTF-8'))->rtrim()->unless( fn ($endWithRadius) => $endWithRadius->exactly($end), fn ($endWithRadius) => $endWithRadius->append($omission), ); return $start->append($matches[2], $end)->toString(); } /** * Cap a string with a single instance of a given value. * * @param string $value * @param string $cap * @return string */ public static function finish($value, $cap) { $quoted = preg_quote($cap, '/'); return preg_replace('/(?:'.$quoted.')+$/u', '', $value).$cap; } /** * Wrap the string with the given strings. * * @param string $value * @param string $before * @param string|null $after * @return string */ public static function wrap($value, $before, $after = null) { return $before.$value.($after ??= $before); } /** * Determine if a given string matches a given pattern. * * @param string|iterable<string> $pattern * @param string $value * @return bool */ public static function is($pattern, $value) { $value = (string) $value; if (! is_iterable($pattern)) { $pattern = [$pattern]; } foreach ($pattern as $pattern) { $pattern = (string) $pattern; // If the given value is an exact match we can of course return true right // from the beginning. Otherwise, we will translate asterisks and do an // actual pattern match against the two strings to see if they match. if ($pattern === $value) { return true; } $pattern = preg_quote($pattern, '#'); // Asterisks are translated into zero-or-more regular expression wildcards // to make it convenient to check if the strings starts with the given // pattern such as "library/*", making any string check convenient. $pattern = str_replace('\*', '.*', $pattern); if (preg_match('#^'.$pattern.'\z#u', $value) === 1) { return true; } } return false; } /** * Determine if a given string is 7 bit ASCII. * * @param string $value * @return bool */ public static function isAscii($value) { return ASCII::is_ascii((string) $value); } /** * Determine if a given value is valid JSON. * * @param mixed $value * @return bool */ public static function isJson($value) { if (! is_string($value)) { return false; } if (function_exists('json_validate')) { return json_validate($value, 512); } try { json_decode($value, true, 512, JSON_THROW_ON_ERROR); } catch (JsonException) { return false; } return true; } /** * Determine if a given value is a valid URL. * * @param mixed $value * @param array $protocols * @return bool */ public static function isUrl($value, array $protocols = []) { if (! is_string($value)) { return false; } $protocolList = empty($protocols) ? 'aaa|aaas|about|acap|acct|acd|acr|adiumxtra|adt|afp|afs|aim|amss|android|appdata|apt|ark|attachment|aw|barion|beshare|bitcoin|bitcoincash|blob|bolo|browserext|calculator|callto|cap|cast|casts|chrome|chrome-extension|cid|coap|coap\+tcp|coap\+ws|coaps|coaps\+tcp|coaps\+ws|com-eventbrite-attendee|content|conti|crid|cvs|dab|data|dav|diaspora|dict|did|dis|dlna-playcontainer|dlna-playsingle|dns|dntp|dpp|drm|drop|dtn|dvb|ed2k|elsi|example|facetime|fax|feed|feedready|file|filesystem|finger|first-run-pen-experience|fish|fm|ftp|fuchsia-pkg|geo|gg|git|gizmoproject|go|gopher|graph|gtalk|h323|ham|hcap|hcp|http|https|hxxp|hxxps|hydrazone|iax|icap|icon|im|imap|info|iotdisco|ipn|ipp|ipps|irc|irc6|ircs|iris|iris\.beep|iris\.lwz|iris\.xpc|iris\.xpcs|isostore|itms|jabber|jar|jms|keyparc|lastfm|ldap|ldaps|leaptofrogans|lorawan|lvlt|magnet|mailserver|mailto|maps|market|message|mid|mms|modem|mongodb|moz|ms-access|ms-browser-extension|ms-calculator|ms-drive-to|ms-enrollment|ms-excel|ms-eyecontrolspeech|ms-gamebarservices|ms-gamingoverlay|ms-getoffice|ms-help|ms-infopath|ms-inputapp|ms-lockscreencomponent-config|ms-media-stream-id|ms-mixedrealitycapture|ms-mobileplans|ms-officeapp|ms-people|ms-project|ms-powerpoint|ms-publisher|ms-restoretabcompanion|ms-screenclip|ms-screensketch|ms-search|ms-search-repair|ms-secondary-screen-controller|ms-secondary-screen-setup|ms-settings|ms-settings-airplanemode|ms-settings-bluetooth|ms-settings-camera|ms-settings-cellular|ms-settings-cloudstorage|ms-settings-connectabledevices|ms-settings-displays-topology|ms-settings-emailandaccounts|ms-settings-language|ms-settings-location|ms-settings-lock|ms-settings-nfctransactions|ms-settings-notifications|ms-settings-power|ms-settings-privacy|ms-settings-proximity|ms-settings-screenrotation|ms-settings-wifi|ms-settings-workplace|ms-spd|ms-sttoverlay|ms-transit-to|ms-useractivityset|ms-virtualtouchpad|ms-visio|ms-walk-to|ms-whiteboard|ms-whiteboard-cmd|ms-word|msnim|msrp|msrps|mss|mtqp|mumble|mupdate|mvn|news|nfs|ni|nih|nntp|notes|ocf|oid|onenote|onenote-cmd|opaquelocktoken|openpgp4fpr|pack|palm|paparazzi|payto|pkcs11|platform|pop|pres|prospero|proxy|pwid|psyc|pttp|qb|query|redis|rediss|reload|res|resource|rmi|rsync|rtmfp|rtmp|rtsp|rtsps|rtspu|s3|secondlife|service|session|sftp|sgn|shttp|sieve|simpleledger|sip|sips|skype|smb|sms|smtp|snews|snmp|soap\.beep|soap\.beeps|soldat|spiffe|spotify|ssh|steam|stun|stuns|submit|svn|tag|teamspeak|tel|teliaeid|telnet|tftp|tg|things|thismessage|tip|tn3270|tool|ts3server|turn|turns|tv|udp|unreal|urn|ut2004|v-event|vemmi|ventrilo|videotex|vnc|view-source|wais|webcal|wpid|ws|wss|wtai|wyciwyg|xcon|xcon-userid|xfire|xmlrpc\.beep|xmlrpc\.beeps|xmpp|xri|ymsgr|z39\.50|z39\.50r|z39\.50s' : implode('|', $protocols); /* * This pattern is derived from Symfony\Component\Validator\Constraints\UrlValidator (5.0.7). * * (c) Fabien Potencier <fabien@symfony.com> http://symfony.com */ $pattern = '~^ (LARAVEL_PROTOCOLS):// # protocol (((?:[\_\.\pL\pN-]|%[0-9A-Fa-f]{2})+:)?((?:[\_\.\pL\pN-]|%[0-9A-Fa-f]{2})+)@)? # basic auth ( ([\pL\pN\pS\-\_\.])+(\.?([\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name | # or \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # an IP address | # or \[ (?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,1}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){3})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,2}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){2})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,3}(?:(?:[0-9a-f]{1,4})))?::(?:(?:[0-9a-f]{1,4})):)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,4}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::)))) \] # an IPv6 address ) (:[0-9]+)? # a port (optional) (?:/ (?:[\pL\pN\-._\~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})* )* # a path (?:\? (?:[\pL\pN\-._\~!$&\'\[\]()*+,;=:@/?]|%[0-9A-Fa-f]{2})* )? # a query (optional) (?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%[0-9A-Fa-f]{2})* )? # a fragment (optional) $~ixu'; return preg_match(str_replace('LARAVEL_PROTOCOLS', $protocolList, $pattern), $value) > 0; } /** * Determine if a given value is a valid UUID. * * @param mixed $value * @return bool */ public static function isUuid($value) { if (! is_string($value)) { return false; } return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $value) > 0; } /** * Determine if a given value is a valid ULID. * * @param mixed $value * @return bool */ public static function isUlid($value) { if (! is_string($value)) { return false; } return Ulid::isValid($value); } /** * Convert a string to kebab case. * * @param string $value * @return string */ public static function kebab($value) { return static::snake($value, '-'); } /** * Return the length of the given string. * * @param string $value * @param string|null $encoding * @return int */ public static function length($value, $encoding = null) { return mb_strlen($value, $encoding); } /** * Limit the number of characters in a string. * * @param string $value * @param int $limit * @param string $end * @return string */ public static function limit($value, $limit = 100, $end = '...') { if (mb_strwidth($value, 'UTF-8') <= $limit) { return $value; } return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end; } /** * Convert the given string to lower-case. * * @param string $value * @return string */ public static function lower($value) { return mb_strtolower($value, 'UTF-8'); } /** * Limit the number of words in a string. * * @param string $value * @param int $words * @param string $end * @return string */ public static function words($value, $words = 100, $end = '...') { preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches); if (! isset($matches[0]) || static::length($value) === static::length($matches[0])) { return $value; } return rtrim($matches[0]).$end; } /** * Converts GitHub flavored Markdown into HTML. * * @param string $string * @param array $options * @return string */ public static function markdown($string, array $options = []) { $converter = new GithubFlavoredMarkdownConverter($options); return (string) $converter->convert($string); } /** * Converts inline Markdown into HTML. * * @param string $string * @param array $options * @return string */ public static function inlineMarkdown($string, array $options = []) { $environment = new Environment($options); $environment->addExtension(new GithubFlavoredMarkdownExtension()); $environment->addExtension(new InlinesOnlyExtension()); $converter = new MarkdownConverter($environment); return (string) $converter->convert($string); } /** * Masks a portion of a string with a repeated character. * * @param string $string * @param string $character * @param int $index * @param int|null $length * @param string $encoding * @return string */ public static function mask($string, $character, $index, $length = null, $encoding = 'UTF-8') { if ($character === '') { return $string; } $segment = mb_substr($string, $index, $length, $encoding); if ($segment === '') { return $string; } $strlen = mb_strlen($string, $encoding); $startIndex = $index; if ($index < 0) { $startIndex = $index < -$strlen ? 0 : $strlen + $index; } $start = mb_substr($string, 0, $startIndex, $encoding); $segmentLen = mb_strlen($segment, $encoding); $end = mb_substr($string, $startIndex + $segmentLen); return $start.str_repeat(mb_substr($character, 0, 1, $encoding), $segmentLen).$end; } /** * Get the string matching the given pattern. * * @param string $pattern * @param string $subject * @return string */ public static function match($pattern, $subject) { preg_match($pattern, $subject, $matches); if (! $matches) { return ''; } return $matches[1] ?? $matches[0]; } /** * Determine if a given string matches a given pattern. * * @param string|iterable<string> $pattern * @param string $value * @return bool */ public static function isMatch($pattern, $value) { $value = (string) $value; if (! is_iterable($pattern)) { $pattern = [$pattern]; } foreach ($pattern as $pattern) { $pattern = (string) $pattern; if (preg_match($pattern, $value) === 1) { return true; } } return false; } /** * Get the string matching the given pattern. * * @param string $pattern * @param string $subject * @return \Illuminate\Support\Collection */ public static function matchAll($pattern, $subject) { preg_match_all($pattern, $subject, $matches); if (empty($matches[0])) { return collect(); } return collect($matches[1] ?? $matches[0]); } /** * Pad both sides of a string with another. * * @param string $value * @param int $length * @param string $pad * @return string */ public static function padBoth($value, $length, $pad = ' ') { $short = max(0, $length - mb_strlen($value)); $shortLeft = floor($short / 2); $shortRight = ceil($short / 2); return mb_substr(str_repeat($pad, $shortLeft), 0, $shortLeft). $value. mb_substr(str_repeat($pad, $shortRight), 0, $shortRight); } /** * Pad the left side of a string with another. * * @param string $value * @param int $length * @param string $pad * @return string */ public static function padLeft($value, $length, $pad = ' ') { $short = max(0, $length - mb_strlen($value)); return mb_substr(str_repeat($pad, $short), 0, $short).$value; } /** * Pad the right side of a string with another. * * @param string $value * @param int $length * @param string $pad * @return string */ public static function padRight($value, $length, $pad = ' ') { $short = max(0, $length - mb_strlen($value)); return $value.mb_substr(str_repeat($pad, $short), 0, $short); } /** * Parse a Class[@]method style callback into class and method. * * @param string $callback * @param string|null $default * @return array<int, string|null> */ public static function parseCallback($callback, $default = null) { return static::contains($callback, '@') ? explode('@', $callback, 2) : [$callback, $default]; } /** * Get the plural form of an English word. * * @param string $value * @param int|array|\Countable $count * @return string */ public static function plural($value, $count = 2) { return Pluralizer::plural($value, $count); } /** * Pluralize the last word of an English, studly caps case string. * * @param string $value * @param int|array|\Countable $count * @return string */ public static function pluralStudly($value, $count = 2) { $parts = preg_split('/(.)(?=[A-Z])/u', $value, -1, PREG_SPLIT_DELIM_CAPTURE); $lastWord = array_pop($parts); return implode('', $parts).self::plural($lastWord, $count); } /** * Generate a random, secure password. * * @param int $length * @param bool $letters * @param bool $numbers * @param bool $symbols * @param bool $spaces * @return string */ public static function password($length = 32, $letters = true, $numbers = true, $symbols = true, $spaces = false) { $password = new Collection(); $options = (new Collection([ 'letters' => $letters === true ? [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ] : null, 'numbers' => $numbers === true ? [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ] : null, 'symbols' => $symbols === true ? [ '~', '!', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '.', ',', '<', '>', '?', '/', '\\', '{', '}', '[', ']', '|', ':', ';', ] : null, 'spaces' => $spaces === true ? [' '] : null, ]))->filter()->each(fn ($c) => $password->push($c[random_int(0, count($c) - 1)]) )->flatten(); $length = $length - $password->count(); return $password->merge($options->pipe( fn ($c) => Collection::times($length, fn () => $c[random_int(0, $c->count() - 1)]) ))->shuffle()->implode(''); } /** * Find the multi-byte safe position of the first occurrence of a given substring in a string. * * @param string $haystack * @param string $needle * @param int $offset * @param string|null $encoding * @return int|false */ public static function position($haystack, $needle, $offset = 0, $encoding = null) { return mb_strpos($haystack, (string) $needle, $offset, $encoding); } /** * Generate a more truly "random" alpha-numeric string. * * @param int $length * @return string */ public static function random($length = 16) { return (static::$randomStringFactory ?? function ($length) { $string = ''; while (($len = strlen($string)) < $length) { $size = $length - $len; $bytesSize = (int) ceil($size / 3) * 3; $bytes = random_bytes($bytesSize); $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size); } return $string; })($length); } /** * Set the callable that will be used to generate random strings. * * @param callable|null $factory * @return void */ public static function createRandomStringsUsing(callable $factory = null) { static::$randomStringFactory = $factory; } /** * Set the sequence that will be used to generate random strings. * * @param array $sequence * @param callable|null $whenMissing * @return void */ public static function createRandomStringsUsingSequence(array $sequence, $whenMissing = null) { $next = 0; $whenMissing ??= function ($length) use (&$next) { $factoryCache = static::$randomStringFactory; static::$randomStringFactory = null; $randomString = static::random($length); static::$randomStringFactory = $factoryCache; $next++; return $randomString; }; static::createRandomStringsUsing(function ($length) use (&$next, $sequence, $whenMissing) { if (array_key_exists($next, $sequence)) { return $sequence[$next++]; } return $whenMissing($length); }); } /** * Indicate that random strings should be created normally and not using a custom factory. * * @return void */ public static function createRandomStringsNormally() { static::$randomStringFactory = null; } /** * Repeat the given string. * * @param string $string * @param int $times * @return string */ public static function repeat(string $string, int $times) { return str_repeat($string, $times); } /** * Replace a given value in the string sequentially with an array. * * @param string $search * @param iterable<string> $replace * @param string $subject * @return string */ public static function replaceArray($search, $replace, $subject) { if ($replace instanceof Traversable) { $replace = collect($replace)->all(); } $segments = explode($search, $subject); $result = array_shift($segments); foreach ($segments as $segment) { $result .= self::toStringOr(array_shift($replace) ?? $search, $search).$segment; } return $result; } /** * Convert the given value to a string or return the given fallback on failure. * * @param mixed $value * @param string $fallback * @return string */ private static function toStringOr($value, $fallback) { try { return (string) $value; } catch (Throwable $e) { return $fallback; } } /** * Replace the given value in the given string. * * @param string|iterable<string> $search * @param string|iterable<string> $replace * @param string|iterable<string> $subject * @param bool $caseSensitive * @return string|string[] */ public static function replace($search, $replace, $subject, $caseSensitive = true) { if ($search instanceof Traversable) { $search = collect($search)->all(); } if ($replace instanceof Traversable) { $replace = collect($replace)->all(); } if ($subject instanceof Traversable) { $subject = collect($subject)->all(); } return $caseSensitive ? str_replace($search, $replace, $subject) : str_ireplace($search, $replace, $subject); } /** * Replace the first occurrence of a given value in the string. * * @param string $search * @param string $replace * @param string $subject * @return string */ public static function replaceFirst($search, $replace, $subject) { $search = (string) $search; if ($search === '') { return $subject; } $position = strpos($subject, $search); if ($position !== false) { return substr_replace($subject, $replace, $position, strlen($search)); } return $subject; } /** * Replace the first occurrence of the given value if it appears at the start of the string. * * @param string $search * @param string $replace * @param string $subject * @return string */ public static function replaceStart($search, $replace, $subject) { $search = (string) $search; if ($search === '') { return $subject; } if (static::startsWith($subject, $search)) { return static::replaceFirst($search, $replace, $subject); } return $subject; } /** * Replace the last occurrence of a given value in the string. * * @param string $search * @param string $replace * @param string $subject * @return string */ public static function replaceLast($search, $replace, $subject) { $search = (string) $search; if ($search === '') { return $subject; } $position = strrpos($subject, $search); if ($position !== false) { return substr_replace($subject, $replace, $position, strlen($search)); } return $subject; } /** * Replace the last occurrence of a given value if it appears at the end of the string. * * @param string $search * @param string $replace * @param string $subject * @return string */ public static function replaceEnd($search, $replace, $subject) { $search = (string) $search; if ($search === '') { return $subject; } if (static::endsWith($subject, $search)) { return static::replaceLast($search, $replace, $subject); } return $subject; } /** * Replace the patterns matching the given regular expression. * * @param string $pattern * @param \Closure|string $replace * @param array|string $subject * @param int $limit * @return string|string[]|null */ public static function replaceMatches($pattern, $replace, $subject, $limit = -1) { if ($replace instanceof Closure) { return preg_replace_callback($pattern, $replace, $subject, $limit); } return preg_replace($pattern, $replace, $subject, $limit); } /** * Remove any occurrence of the given string in the subject. * * @param string|iterable<string> $search * @param string|iterable<string> $subject * @param bool $caseSensitive * @return string */ public static function remove($search, $subject, $caseSensitive = true) { if ($search instanceof Traversable) { $search = collect($search)->all(); } return $caseSensitive ? str_replace($search, '', $subject) : str_ireplace($search, '', $subject); } /** * Reverse the given string. * * @param string $value * @return string */ public static function reverse(string $value) { return implode(array_reverse(mb_str_split($value))); } /** * Begin a string with a single instance of a given value. * * @param string $value * @param string $prefix * @return string */ public static function start($value, $prefix) { $quoted = preg_quote($prefix, '/'); return $prefix.preg_replace('/^(?:'.$quoted.')+/u', '', $value); } /** * Convert the given string to upper-case. * * @param string $value * @return string */ public static function upper($value) { return mb_strtoupper($value, 'UTF-8'); } /** * Convert the given string to title case. * * @param string $value * @return string */ public static function title($value) { return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'); } /** * Convert the given string to title case for each word. * * @param string $value * @return string */ public static function headline($value) { $parts = explode(' ', $value); $parts = count($parts) > 1 ? array_map([static::class, 'title'], $parts) : array_map([static::class, 'title'], static::ucsplit(implode('_', $parts))); $collapsed = static::replace(['-', '_', ' '], '_', implode('_', $parts)); return implode(' ', array_filter(explode('_', $collapsed))); } /** * Get the singular form of an English word. * * @param string $value * @return string */ public static function singular($value) { return Pluralizer::singular($value); } /** * Generate a URL friendly "slug" from a given string. * * @param string $title * @param string $separator * @param string|null $language * @param array<string, string> $dictionary * @return string */ public static function slug($title, $separator = '-', $language = 'en', $dictionary = ['@' => 'at']) { $title = $language ? static::ascii($title, $language) : $title; // Convert all dashes/underscores into separator $flip = $separator === '-' ? '_' : '-'; $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title); // Replace dictionary words foreach ($dictionary as $key => $value) { $dictionary[$key] = $separator.$value.$separator; } $title = str_replace(array_keys($dictionary), array_values($dictionary), $title); // Remove all characters that are not the separator, letters, numbers, or whitespace $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', static::lower($title)); // Replace all separator characters and whitespace by a single separator $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title); return trim($title, $separator); } /** * Convert a string to snake case. * * @param string $value * @param string $delimiter * @return string */ public static function snake($value, $delimiter = '_') { $key = $value; if (isset(static::$snakeCache[$key][$delimiter])) { return static::$snakeCache[$key][$delimiter]; } if (! ctype_lower($value)) { $value = preg_replace('/\s+/u', '', ucwords($value)); $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value)); } return static::$snakeCache[$key][$delimiter] = $value; } /** * Remove all "extra" blank space from the given string. * * @param string $value * @return string */ public static function squish($value) { return preg_replace('~(\s|\x{3164}|\x{1160})+~u', ' ', preg_replace('~^[\s\x{FEFF}]+|[\s\x{FEFF}]+$~u', '', $value)); } /** * Determine if a given string starts with a given substring. * * @param string $haystack * @param string|iterable<string> $needles * @return bool */ public static function startsWith($haystack, $needles) { if (! is_iterable($needles)) { $needles = [$needles]; } foreach ($needles as $needle) { if ((string) $needle !== '' && str_starts_with($haystack, $needle)) { return true; } } return false; } /** * Convert a value to studly caps case. * * @param string $value * @return string */ public static function studly($value) { $key = $value; if (isset(static::$studlyCache[$key])) { return static::$studlyCache[$key]; } $words = explode(' ', static::replace(['-', '_'], ' ', $value)); $studlyWords = array_map(fn ($word) => static::ucfirst($word), $words); return static::$studlyCache[$key] = implode($studlyWords); } /** * Returns the portion of the string specified by the start and length parameters. * * @param string $string * @param int $start * @param int|null $length * @param string $encoding * @return string */ public static function substr($string, $start, $length = null, $encoding = 'UTF-8') { return mb_substr($string, $start, $length, $encoding); } /** * Returns the number of substring occurrences. * * @param string $haystack * @param string $needle * @param int $offset * @param int|null $length * @return int */ public static function substrCount($haystack, $needle, $offset = 0, $length = null) { if (! is_null($length)) { return substr_count($haystack, $needle, $offset, $length); } return substr_count($haystack, $needle, $offset); } /** * Replace text within a portion of a string. * * @param string|string[] $string * @param string|string[] $replace * @param int|int[] $offset * @param int|int[]|null $length * @return string|string[] */ public static function substrReplace($string, $replace, $offset = 0, $length = null) { if ($length === null) { $length = strlen($string); } return substr_replace($string, $replace, $offset, $length); } /** * Swap multiple keywords in a string with other keywords. * * @param array $map * @param string $subject * @return string */ public static function swap(array $map, $subject) { return strtr($subject, $map); } /** * Take the first or last {$limit} characters of a string. * * @param string $string * @param int $limit * @return string */ public static function take($string, int $limit): string { if ($limit < 0) { return static::substr($string, $limit); } return static::substr($string, 0, $limit); } /** * Make a string's first character lowercase. * * @param string $string * @return string */ public static function lcfirst($string) { return static::lower(static::substr($string, 0, 1)).static::substr($string, 1); } /** * Make a string's first character uppercase. * * @param string $string * @return string */ public static function ucfirst($string) { return static::upper(static::substr($string, 0, 1)).static::substr($string, 1); } /** * Split a string into pieces by uppercase characters. * * @param string $string * @return string[] */ public static function ucsplit($string) { return preg_split('/(?=\p{Lu})/u', $string, -1, PREG_SPLIT_NO_EMPTY); } /** * Get the number of words a string contains. * * @param string $string * @param string|null $characters * @return int */ public static function wordCount($string, $characters = null) { return str_word_count($string, 0, $characters); } /** * Wrap a string to a given number of characters. * * @param string $string * @param int $characters * @param string $break * @param bool $cutLongWords * @return string */ public static function wordWrap($string, $characters = 75, $break = "\n", $cutLongWords = false) { return wordwrap($string, $characters, $break, $cutLongWords); } /** * Generate a UUID (version 4). * * @return \Ramsey\Uuid\UuidInterface */ public static function uuid() { return static::$uuidFactory ? call_user_func(static::$uuidFactory) : Uuid::uuid4(); } /** * Generate a time-ordered UUID (version 4). * * @return \Ramsey\Uuid\UuidInterface */ public static function orderedUuid() { if (static::$uuidFactory) { return call_user_func(static::$uuidFactory); } $factory = new UuidFactory; $factory->setRandomGenerator(new CombGenerator( $factory->getRandomGenerator(), $factory->getNumberConverter() )); $factory->setCodec(new TimestampFirstCombCodec( $factory->getUuidBuilder() )); return $factory->uuid4(); } /** * Set the callable that will be used to generate UUIDs. * * @param callable|null $factory * @return void */ public static function createUuidsUsing(callable $factory = null) { static::$uuidFactory = $factory; } /** * Set the sequence that will be used to generate UUIDs. * * @param array $sequence * @param callable|null $whenMissing * @return void */ public static function createUuidsUsingSequence(array $sequence, $whenMissing = null) { $next = 0; $whenMissing ??= function () use (&$next) { $factoryCache = static::$uuidFactory; static::$uuidFactory = null; $uuid = static::uuid(); static::$uuidFactory = $factoryCache; $next++; return $uuid; }; static::createUuidsUsing(function () use (&$next, $sequence, $whenMissing) { if (array_key_exists($next, $sequence)) { return $sequence[$next++]; } return $whenMissing(); }); } /** * Always return the same UUID when generating new UUIDs. * * @param \Closure|null $callback * @return \Ramsey\Uuid\UuidInterface */ public static function freezeUuids(Closure $callback = null) { $uuid = Str::uuid(); Str::createUuidsUsing(fn () => $uuid); if ($callback !== null) { try { $callback($uuid); } finally { Str::createUuidsNormally(); } } return $uuid; } /** * Indicate that UUIDs should be created normally and not using a custom factory. * * @return void */ public static function createUuidsNormally() { static::$uuidFactory = null; } /** * Generate a ULID. * * @param \DateTimeInterface|null $time * @return \Symfony\Component\Uid\Ulid */ public static function ulid($time = null) { if (static::$ulidFactory) { return call_user_func(static::$ulidFactory); } if ($time === null) { return new Ulid(); } return new Ulid(Ulid::generate($time)); } /** * Indicate that ULIDs should be created normally and not using a custom factory. * * @return void */ public static function createUlidsNormally() { static::$ulidFactory = null; } /** * Set the callable that will be used to generate ULIDs. * * @param callable|null $factory * @return void */ public static function createUlidsUsing(callable $factory = null) { static::$ulidFactory = $factory; } /** * Set the sequence that will be used to generate ULIDs. * * @param array $sequence * @param callable|null $whenMissing * @return void */ public static function createUlidsUsingSequence(array $sequence, $whenMissing = null) { $next = 0; $whenMissing ??= function () use (&$next) { $factoryCache = static::$ulidFactory; static::$ulidFactory = null; $ulid = static::ulid(); static::$ulidFactory = $factoryCache; $next++; return $ulid; }; static::createUlidsUsing(function () use (&$next, $sequence, $whenMissing) { if (array_key_exists($next, $sequence)) { return $sequence[$next++]; } return $whenMissing(); }); } /** * Always return the same ULID when generating new ULIDs. * * @param Closure|null $callback * @return Ulid */ public static function freezeUlids(Closure $callback = null) { $ulid = Str::ulid(); Str::createUlidsUsing(fn () => $ulid); if ($callback !== null) { try { $callback($ulid); } finally { Str::createUlidsNormally(); } } return $ulid; } /** * Remove all strings from the casing caches. * * @return void */ public static function flushCache() { static::$snakeCache = []; static::$camelCache = []; static::$studlyCache = []; } } src/Illuminate/Support/Stringable.php 0000644 00000100101 15107327040 0013705 0 ustar 00 <?php namespace Illuminate\Support; use ArrayAccess; use Closure; use Illuminate\Support\Facades\Date; use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\Macroable; use Illuminate\Support\Traits\Tappable; use JsonSerializable; use Symfony\Component\VarDumper\VarDumper; class Stringable implements JsonSerializable, ArrayAccess { use Conditionable, Macroable, Tappable; /** * The underlying string value. * * @var string */ protected $value; /** * Create a new instance of the class. * * @param string $value * @return void */ public function __construct($value = '') { $this->value = (string) $value; } /** * Return the remainder of a string after the first occurrence of a given value. * * @param string $search * @return static */ public function after($search) { return new static(Str::after($this->value, $search)); } /** * Return the remainder of a string after the last occurrence of a given value. * * @param string $search * @return static */ public function afterLast($search) { return new static(Str::afterLast($this->value, $search)); } /** * Append the given values to the string. * * @param array|string ...$values * @return static */ public function append(...$values) { return new static($this->value.implode('', $values)); } /** * Append a new line to the string. * * @param int $count * @return $this */ public function newLine($count = 1) { return $this->append(str_repeat(PHP_EOL, $count)); } /** * Transliterate a UTF-8 value to ASCII. * * @param string $language * @return static */ public function ascii($language = 'en') { return new static(Str::ascii($this->value, $language)); } /** * Get the trailing name component of the path. * * @param string $suffix * @return static */ public function basename($suffix = '') { return new static(basename($this->value, $suffix)); } /** * Get the character at the specified index. * * @param int $index * @return string|false */ public function charAt($index) { return Str::charAt($this->value, $index); } /** * Get the basename of the class path. * * @return static */ public function classBasename() { return new static(class_basename($this->value)); } /** * Get the portion of a string before the first occurrence of a given value. * * @param string $search * @return static */ public function before($search) { return new static(Str::before($this->value, $search)); } /** * Get the portion of a string before the last occurrence of a given value. * * @param string $search * @return static */ public function beforeLast($search) { return new static(Str::beforeLast($this->value, $search)); } /** * Get the portion of a string between two given values. * * @param string $from * @param string $to * @return static */ public function between($from, $to) { return new static(Str::between($this->value, $from, $to)); } /** * Get the smallest possible portion of a string between two given values. * * @param string $from * @param string $to * @return static */ public function betweenFirst($from, $to) { return new static(Str::betweenFirst($this->value, $from, $to)); } /** * Convert a value to camel case. * * @return static */ public function camel() { return new static(Str::camel($this->value)); } /** * Determine if a given string contains a given substring. * * @param string|iterable<string> $needles * @param bool $ignoreCase * @return bool */ public function contains($needles, $ignoreCase = false) { return Str::contains($this->value, $needles, $ignoreCase); } /** * Determine if a given string contains all array values. * * @param iterable<string> $needles * @param bool $ignoreCase * @return bool */ public function containsAll($needles, $ignoreCase = false) { return Str::containsAll($this->value, $needles, $ignoreCase); } /** * Convert the case of a string. * * @param int $mode * @param string $encoding * @return string */ public function convertCase(int $mode = MB_CASE_FOLD, ?string $encoding = 'UTF-8') { return new static(Str::convertCase($this->value, $mode, $encoding)); } /** * Get the parent directory's path. * * @param int $levels * @return static */ public function dirname($levels = 1) { return new static(dirname($this->value, $levels)); } /** * Determine if a given string ends with a given substring. * * @param string|iterable<string> $needles * @return bool */ public function endsWith($needles) { return Str::endsWith($this->value, $needles); } /** * Determine if the string is an exact match with the given value. * * @param \Illuminate\Support\Stringable|string $value * @return bool */ public function exactly($value) { if ($value instanceof Stringable) { $value = $value->toString(); } return $this->value === $value; } /** * Extracts an excerpt from text that matches the first instance of a phrase. * * @param string $phrase * @param array $options * @return string|null */ public function excerpt($phrase = '', $options = []) { return Str::excerpt($this->value, $phrase, $options); } /** * Explode the string into an array. * * @param string $delimiter * @param int $limit * @return \Illuminate\Support\Collection<int, string> */ public function explode($delimiter, $limit = PHP_INT_MAX) { return collect(explode($delimiter, $this->value, $limit)); } /** * Split a string using a regular expression or by length. * * @param string|int $pattern * @param int $limit * @param int $flags * @return \Illuminate\Support\Collection<int, string> */ public function split($pattern, $limit = -1, $flags = 0) { if (filter_var($pattern, FILTER_VALIDATE_INT) !== false) { return collect(mb_str_split($this->value, $pattern)); } $segments = preg_split($pattern, $this->value, $limit, $flags); return ! empty($segments) ? collect($segments) : collect(); } /** * Cap a string with a single instance of a given value. * * @param string $cap * @return static */ public function finish($cap) { return new static(Str::finish($this->value, $cap)); } /** * Determine if a given string matches a given pattern. * * @param string|iterable<string> $pattern * @return bool */ public function is($pattern) { return Str::is($pattern, $this->value); } /** * Determine if a given string is 7 bit ASCII. * * @return bool */ public function isAscii() { return Str::isAscii($this->value); } /** * Determine if a given string is valid JSON. * * @return bool */ public function isJson() { return Str::isJson($this->value); } /** * Determine if a given value is a valid URL. * * @return bool */ public function isUrl() { return Str::isUrl($this->value); } /** * Determine if a given string is a valid UUID. * * @return bool */ public function isUuid() { return Str::isUuid($this->value); } /** * Determine if a given string is a valid ULID. * * @return bool */ public function isUlid() { return Str::isUlid($this->value); } /** * Determine if the given string is empty. * * @return bool */ public function isEmpty() { return $this->value === ''; } /** * Determine if the given string is not empty. * * @return bool */ public function isNotEmpty() { return ! $this->isEmpty(); } /** * Convert a string to kebab case. * * @return static */ public function kebab() { return new static(Str::kebab($this->value)); } /** * Return the length of the given string. * * @param string|null $encoding * @return int */ public function length($encoding = null) { return Str::length($this->value, $encoding); } /** * Limit the number of characters in a string. * * @param int $limit * @param string $end * @return static */ public function limit($limit = 100, $end = '...') { return new static(Str::limit($this->value, $limit, $end)); } /** * Convert the given string to lower-case. * * @return static */ public function lower() { return new static(Str::lower($this->value)); } /** * Convert GitHub flavored Markdown into HTML. * * @param array $options * @return static */ public function markdown(array $options = []) { return new static(Str::markdown($this->value, $options)); } /** * Convert inline Markdown into HTML. * * @param array $options * @return static */ public function inlineMarkdown(array $options = []) { return new static(Str::inlineMarkdown($this->value, $options)); } /** * Masks a portion of a string with a repeated character. * * @param string $character * @param int $index * @param int|null $length * @param string $encoding * @return static */ public function mask($character, $index, $length = null, $encoding = 'UTF-8') { return new static(Str::mask($this->value, $character, $index, $length, $encoding)); } /** * Get the string matching the given pattern. * * @param string $pattern * @return static */ public function match($pattern) { return new static(Str::match($pattern, $this->value)); } /** * Determine if a given string matches a given pattern. * * @param string|iterable<string> $pattern * @return bool */ public function isMatch($pattern) { return Str::isMatch($pattern, $this->value); } /** * Get the string matching the given pattern. * * @param string $pattern * @return \Illuminate\Support\Collection */ public function matchAll($pattern) { return Str::matchAll($pattern, $this->value); } /** * Determine if the string matches the given pattern. * * @param string $pattern * @return bool */ public function test($pattern) { return $this->isMatch($pattern); } /** * Pad both sides of the string with another. * * @param int $length * @param string $pad * @return static */ public function padBoth($length, $pad = ' ') { return new static(Str::padBoth($this->value, $length, $pad)); } /** * Pad the left side of the string with another. * * @param int $length * @param string $pad * @return static */ public function padLeft($length, $pad = ' ') { return new static(Str::padLeft($this->value, $length, $pad)); } /** * Pad the right side of the string with another. * * @param int $length * @param string $pad * @return static */ public function padRight($length, $pad = ' ') { return new static(Str::padRight($this->value, $length, $pad)); } /** * Parse a Class@method style callback into class and method. * * @param string|null $default * @return array<int, string|null> */ public function parseCallback($default = null) { return Str::parseCallback($this->value, $default); } /** * Call the given callback and return a new string. * * @param callable $callback * @return static */ public function pipe(callable $callback) { return new static($callback($this)); } /** * Get the plural form of an English word. * * @param int|array|\Countable $count * @return static */ public function plural($count = 2) { return new static(Str::plural($this->value, $count)); } /** * Pluralize the last word of an English, studly caps case string. * * @param int|array|\Countable $count * @return static */ public function pluralStudly($count = 2) { return new static(Str::pluralStudly($this->value, $count)); } /** * Find the multi-byte safe position of the first occurrence of the given substring. * * @param string $needle * @param int $offset * @param string|null $encoding * @return int|false */ public function position($needle, $offset = 0, $encoding = null) { return Str::position($this->value, $needle, $offset, $encoding); } /** * Prepend the given values to the string. * * @param string ...$values * @return static */ public function prepend(...$values) { return new static(implode('', $values).$this->value); } /** * Remove any occurrence of the given string in the subject. * * @param string|iterable<string> $search * @param bool $caseSensitive * @return static */ public function remove($search, $caseSensitive = true) { return new static(Str::remove($search, $this->value, $caseSensitive)); } /** * Reverse the string. * * @return static */ public function reverse() { return new static(Str::reverse($this->value)); } /** * Repeat the string. * * @param int $times * @return static */ public function repeat(int $times) { return new static(str_repeat($this->value, $times)); } /** * Replace the given value in the given string. * * @param string|iterable<string> $search * @param string|iterable<string> $replace * @param bool $caseSensitive * @return static */ public function replace($search, $replace, $caseSensitive = true) { return new static(Str::replace($search, $replace, $this->value, $caseSensitive)); } /** * Replace a given value in the string sequentially with an array. * * @param string $search * @param iterable<string> $replace * @return static */ public function replaceArray($search, $replace) { return new static(Str::replaceArray($search, $replace, $this->value)); } /** * Replace the first occurrence of a given value in the string. * * @param string $search * @param string $replace * @return static */ public function replaceFirst($search, $replace) { return new static(Str::replaceFirst($search, $replace, $this->value)); } /** * Replace the first occurrence of the given value if it appears at the start of the string. * * @param string $search * @param string $replace * @return static */ public function replaceStart($search, $replace) { return new static(Str::replaceStart($search, $replace, $this->value)); } /** * Replace the last occurrence of a given value in the string. * * @param string $search * @param string $replace * @return static */ public function replaceLast($search, $replace) { return new static(Str::replaceLast($search, $replace, $this->value)); } /** * Replace the last occurrence of a given value if it appears at the end of the string. * * @param string $search * @param string $replace * @return static */ public function replaceEnd($search, $replace) { return new static(Str::replaceEnd($search, $replace, $this->value)); } /** * Replace the patterns matching the given regular expression. * * @param string $pattern * @param \Closure|string $replace * @param int $limit * @return static */ public function replaceMatches($pattern, $replace, $limit = -1) { if ($replace instanceof Closure) { return new static(preg_replace_callback($pattern, $replace, $this->value, $limit)); } return new static(preg_replace($pattern, $replace, $this->value, $limit)); } /** * Parse input from a string to a collection, according to a format. * * @param string $format * @return \Illuminate\Support\Collection */ public function scan($format) { return collect(sscanf($this->value, $format)); } /** * Remove all "extra" blank space from the given string. * * @return static */ public function squish() { return new static(Str::squish($this->value)); } /** * Begin a string with a single instance of a given value. * * @param string $prefix * @return static */ public function start($prefix) { return new static(Str::start($this->value, $prefix)); } /** * Strip HTML and PHP tags from the given string. * * @param string $allowedTags * @return static */ public function stripTags($allowedTags = null) { return new static(strip_tags($this->value, $allowedTags)); } /** * Convert the given string to upper-case. * * @return static */ public function upper() { return new static(Str::upper($this->value)); } /** * Convert the given string to title case. * * @return static */ public function title() { return new static(Str::title($this->value)); } /** * Convert the given string to title case for each word. * * @return static */ public function headline() { return new static(Str::headline($this->value)); } /** * Get the singular form of an English word. * * @return static */ public function singular() { return new static(Str::singular($this->value)); } /** * Generate a URL friendly "slug" from a given string. * * @param string $separator * @param string|null $language * @param array<string, string> $dictionary * @return static */ public function slug($separator = '-', $language = 'en', $dictionary = ['@' => 'at']) { return new static(Str::slug($this->value, $separator, $language, $dictionary)); } /** * Convert a string to snake case. * * @param string $delimiter * @return static */ public function snake($delimiter = '_') { return new static(Str::snake($this->value, $delimiter)); } /** * Determine if a given string starts with a given substring. * * @param string|iterable<string> $needles * @return bool */ public function startsWith($needles) { return Str::startsWith($this->value, $needles); } /** * Convert a value to studly caps case. * * @return static */ public function studly() { return new static(Str::studly($this->value)); } /** * Returns the portion of the string specified by the start and length parameters. * * @param int $start * @param int|null $length * @param string $encoding * @return static */ public function substr($start, $length = null, $encoding = 'UTF-8') { return new static(Str::substr($this->value, $start, $length, $encoding)); } /** * Returns the number of substring occurrences. * * @param string $needle * @param int $offset * @param int|null $length * @return int */ public function substrCount($needle, $offset = 0, $length = null) { return Str::substrCount($this->value, $needle, $offset, $length); } /** * Replace text within a portion of a string. * * @param string|string[] $replace * @param int|int[] $offset * @param int|int[]|null $length * @return static */ public function substrReplace($replace, $offset = 0, $length = null) { return new static(Str::substrReplace($this->value, $replace, $offset, $length)); } /** * Swap multiple keywords in a string with other keywords. * * @param array $map * @return static */ public function swap(array $map) { return new static(strtr($this->value, $map)); } /** * Take the first or last {$limit} characters. * * @param int $limit * @return static */ public function take(int $limit) { if ($limit < 0) { return $this->substr($limit); } return $this->substr(0, $limit); } /** * Trim the string of the given characters. * * @param string $characters * @return static */ public function trim($characters = null) { return new static(trim(...array_merge([$this->value], func_get_args()))); } /** * Left trim the string of the given characters. * * @param string $characters * @return static */ public function ltrim($characters = null) { return new static(ltrim(...array_merge([$this->value], func_get_args()))); } /** * Right trim the string of the given characters. * * @param string $characters * @return static */ public function rtrim($characters = null) { return new static(rtrim(...array_merge([$this->value], func_get_args()))); } /** * Make a string's first character lowercase. * * @return static */ public function lcfirst() { return new static(Str::lcfirst($this->value)); } /** * Make a string's first character uppercase. * * @return static */ public function ucfirst() { return new static(Str::ucfirst($this->value)); } /** * Split a string by uppercase characters. * * @return \Illuminate\Support\Collection<int, string> */ public function ucsplit() { return collect(Str::ucsplit($this->value)); } /** * Execute the given callback if the string contains a given substring. * * @param string|iterable<string> $needles * @param callable $callback * @param callable|null $default * @return static */ public function whenContains($needles, $callback, $default = null) { return $this->when($this->contains($needles), $callback, $default); } /** * Execute the given callback if the string contains all array values. * * @param iterable<string> $needles * @param callable $callback * @param callable|null $default * @return static */ public function whenContainsAll(array $needles, $callback, $default = null) { return $this->when($this->containsAll($needles), $callback, $default); } /** * Execute the given callback if the string is empty. * * @param callable $callback * @param callable|null $default * @return static */ public function whenEmpty($callback, $default = null) { return $this->when($this->isEmpty(), $callback, $default); } /** * Execute the given callback if the string is not empty. * * @param callable $callback * @param callable|null $default * @return static */ public function whenNotEmpty($callback, $default = null) { return $this->when($this->isNotEmpty(), $callback, $default); } /** * Execute the given callback if the string ends with a given substring. * * @param string|iterable<string> $needles * @param callable $callback * @param callable|null $default * @return static */ public function whenEndsWith($needles, $callback, $default = null) { return $this->when($this->endsWith($needles), $callback, $default); } /** * Execute the given callback if the string is an exact match with the given value. * * @param string $value * @param callable $callback * @param callable|null $default * @return static */ public function whenExactly($value, $callback, $default = null) { return $this->when($this->exactly($value), $callback, $default); } /** * Execute the given callback if the string is not an exact match with the given value. * * @param string $value * @param callable $callback * @param callable|null $default * @return static */ public function whenNotExactly($value, $callback, $default = null) { return $this->when(! $this->exactly($value), $callback, $default); } /** * Execute the given callback if the string matches a given pattern. * * @param string|iterable<string> $pattern * @param callable $callback * @param callable|null $default * @return static */ public function whenIs($pattern, $callback, $default = null) { return $this->when($this->is($pattern), $callback, $default); } /** * Execute the given callback if the string is 7 bit ASCII. * * @param callable $callback * @param callable|null $default * @return static */ public function whenIsAscii($callback, $default = null) { return $this->when($this->isAscii(), $callback, $default); } /** * Execute the given callback if the string is a valid UUID. * * @param callable $callback * @param callable|null $default * @return static */ public function whenIsUuid($callback, $default = null) { return $this->when($this->isUuid(), $callback, $default); } /** * Execute the given callback if the string is a valid ULID. * * @param callable $callback * @param callable|null $default * @return static */ public function whenIsUlid($callback, $default = null) { return $this->when($this->isUlid(), $callback, $default); } /** * Execute the given callback if the string starts with a given substring. * * @param string|iterable<string> $needles * @param callable $callback * @param callable|null $default * @return static */ public function whenStartsWith($needles, $callback, $default = null) { return $this->when($this->startsWith($needles), $callback, $default); } /** * Execute the given callback if the string matches the given pattern. * * @param string $pattern * @param callable $callback * @param callable|null $default * @return static */ public function whenTest($pattern, $callback, $default = null) { return $this->when($this->test($pattern), $callback, $default); } /** * Limit the number of words in a string. * * @param int $words * @param string $end * @return static */ public function words($words = 100, $end = '...') { return new static(Str::words($this->value, $words, $end)); } /** * Get the number of words a string contains. * * @param string|null $characters * @return int */ public function wordCount($characters = null) { return Str::wordCount($this->value, $characters); } /** * Wrap a string to a given number of characters. * * @param int $characters * @param string $break * @param bool $cutLongWords * @return static */ public function wordWrap($characters = 75, $break = "\n", $cutLongWords = false) { return new static(Str::wordWrap($this->value, $characters, $break, $cutLongWords)); } /** * Wrap the string with the given strings. * * @param string $before * @param string|null $after * @return static */ public function wrap($before, $after = null) { return new static(Str::wrap($this->value, $before, $after)); } /** * Convert the string into a `HtmlString` instance. * * @return \Illuminate\Support\HtmlString */ public function toHtmlString() { return new HtmlString($this->value); } /** * Dump the string. * * @return $this */ public function dump() { VarDumper::dump($this->value); return $this; } /** * Dump the string and end the script. * * @return never */ public function dd() { $this->dump(); exit(1); } /** * Get the underlying string value. * * @return string */ public function value() { return $this->toString(); } /** * Get the underlying string value. * * @return string */ public function toString() { return $this->value; } /** * Get the underlying string value as an integer. * * @return int */ public function toInteger() { return intval($this->value); } /** * Get the underlying string value as a float. * * @return float */ public function toFloat() { return floatval($this->value); } /** * Get the underlying string value as a boolean. * * Returns true when value is "1", "true", "on", and "yes". Otherwise, returns false. * * @return bool */ public function toBoolean() { return filter_var($this->value, FILTER_VALIDATE_BOOLEAN); } /** * Get the underlying string value as a Carbon instance. * * @param string|null $format * @param string|null $tz * @return \Illuminate\Support\Carbon * * @throws \Carbon\Exceptions\InvalidFormatException */ public function toDate($format = null, $tz = null) { if (is_null($format)) { return Date::parse($this->value, $tz); } return Date::createFromFormat($format, $this->value, $tz); } /** * Convert the object to a string when JSON encoded. * * @return string */ public function jsonSerialize(): string { return $this->__toString(); } /** * Determine if the given offset exists. * * @param mixed $offset * @return bool */ public function offsetExists(mixed $offset): bool { return isset($this->value[$offset]); } /** * Get the value at the given offset. * * @param mixed $offset * @return string */ public function offsetGet(mixed $offset): string { return $this->value[$offset]; } /** * Set the value at the given offset. * * @param mixed $offset * @return void */ public function offsetSet(mixed $offset, mixed $value): void { $this->value[$offset] = $value; } /** * Unset the value at the given offset. * * @param mixed $offset * @return void */ public function offsetUnset(mixed $offset): void { unset($this->value[$offset]); } /** * Proxy dynamic properties onto methods. * * @param string $key * @return mixed */ public function __get($key) { return $this->{$key}(); } /** * Get the raw string value. * * @return string */ public function __toString() { return (string) $this->value; } } src/Illuminate/Support/ValidatedInput.php 0000644 00000011164 15107327040 0014542 0 ustar 00 <?php namespace Illuminate\Support; use ArrayIterator; use Illuminate\Contracts\Support\ValidatedData; use stdClass; use Traversable; class ValidatedInput implements ValidatedData { /** * The underlying input. * * @var array */ protected $input; /** * Create a new validated input container. * * @param array $input * @return void */ public function __construct(array $input) { $this->input = $input; } /** * Determine if the validated input has one or more keys. * * @param mixed $keys * @return bool */ public function has($keys) { $keys = is_array($keys) ? $keys : func_get_args(); foreach ($keys as $key) { if (! Arr::has($this->input, $key)) { return false; } } return true; } /** * Determine if the validated input is missing one or more keys. * * @param mixed $keys * @return bool */ public function missing($keys) { return ! $this->has($keys); } /** * Get a subset containing the provided keys with values from the input data. * * @param mixed $keys * @return array */ public function only($keys) { $results = []; $input = $this->input; $placeholder = new stdClass; foreach (is_array($keys) ? $keys : func_get_args() as $key) { $value = data_get($input, $key, $placeholder); if ($value !== $placeholder) { Arr::set($results, $key, $value); } } return $results; } /** * Get all of the input except for a specified array of items. * * @param mixed $keys * @return array */ public function except($keys) { $keys = is_array($keys) ? $keys : func_get_args(); $results = $this->input; Arr::forget($results, $keys); return $results; } /** * Merge the validated input with the given array of additional data. * * @param array $items * @return static */ public function merge(array $items) { return new static(array_merge($this->input, $items)); } /** * Get the input as a collection. * * @return \Illuminate\Support\Collection */ public function collect() { return new Collection($this->input); } /** * Get the raw, underlying input array. * * @return array */ public function all() { return $this->input; } /** * Get the instance as an array. * * @return array */ public function toArray() { return $this->all(); } /** * Dynamically access input data. * * @param string $name * @return mixed */ public function __get($name) { return $this->input[$name]; } /** * Dynamically set input data. * * @param string $name * @param mixed $value * @return mixed */ public function __set($name, $value) { $this->input[$name] = $value; } /** * Determine if an input key is set. * * @return bool */ public function __isset($name) { return isset($this->input[$name]); } /** * Remove an input key. * * @param string $name * @return void */ public function __unset($name) { unset($this->input[$name]); } /** * Determine if an item exists at an offset. * * @param mixed $key * @return bool */ public function offsetExists($key): bool { return isset($this->input[$key]); } /** * Get an item at a given offset. * * @param mixed $key * @return mixed */ public function offsetGet($key): mixed { return $this->input[$key]; } /** * Set the item at a given offset. * * @param mixed $key * @param mixed $value * @return void */ public function offsetSet($key, $value): void { if (is_null($key)) { $this->input[] = $value; } else { $this->input[$key] = $value; } } /** * Unset the item at a given offset. * * @param string $key * @return void */ public function offsetUnset($key): void { unset($this->input[$key]); } /** * Get an iterator for the input. * * @return \ArrayIterator */ public function getIterator(): Traversable { return new ArrayIterator($this->input); } } src/Illuminate/Support/InteractsWithTime.php 0000644 00000003064 15107327040 0015234 0 ustar 00 <?php namespace Illuminate\Support; use DateInterval; use DateTimeInterface; trait InteractsWithTime { /** * Get the number of seconds until the given DateTime. * * @param \DateTimeInterface|\DateInterval|int $delay * @return int */ protected function secondsUntil($delay) { $delay = $this->parseDateInterval($delay); return $delay instanceof DateTimeInterface ? max(0, $delay->getTimestamp() - $this->currentTime()) : (int) $delay; } /** * Get the "available at" UNIX timestamp. * * @param \DateTimeInterface|\DateInterval|int $delay * @return int */ protected function availableAt($delay = 0) { $delay = $this->parseDateInterval($delay); return $delay instanceof DateTimeInterface ? $delay->getTimestamp() : Carbon::now()->addRealSeconds($delay)->getTimestamp(); } /** * If the given value is an interval, convert it to a DateTime instance. * * @param \DateTimeInterface|\DateInterval|int $delay * @return \DateTimeInterface|int */ protected function parseDateInterval($delay) { if ($delay instanceof DateInterval) { $delay = Carbon::now()->add($delay); } return $delay; } /** * Get the current system time as a UNIX timestamp. * * @return int */ protected function currentTime() { return Carbon::now()->getTimestamp(); } } src/Illuminate/Support/DateFactory.php 0000644 00000017410 15107327040 0014032 0 ustar 00 <?php namespace Illuminate\Support; use Carbon\Factory; use InvalidArgumentException; /** * @see https://carbon.nesbot.com/docs/ * @see https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Factory.php * * @method \Illuminate\Support\Carbon create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null) * @method \Illuminate\Support\Carbon createFromDate($year = null, $month = null, $day = null, $tz = null) * @method \Illuminate\Support\Carbon|false createFromFormat($format, $time, $tz = null) * @method \Illuminate\Support\Carbon createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) * @method \Illuminate\Support\Carbon createFromTimeString($time, $tz = null) * @method \Illuminate\Support\Carbon createFromTimestamp($timestamp, $tz = null) * @method \Illuminate\Support\Carbon createFromTimestampMs($timestamp, $tz = null) * @method \Illuminate\Support\Carbon createFromTimestampUTC($timestamp) * @method \Illuminate\Support\Carbon createMidnightDate($year = null, $month = null, $day = null, $tz = null) * @method \Illuminate\Support\Carbon|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) * @method void disableHumanDiffOption($humanDiffOption) * @method void enableHumanDiffOption($humanDiffOption) * @method mixed executeWithLocale($locale, $func) * @method \Illuminate\Support\Carbon fromSerialized($value) * @method array getAvailableLocales() * @method array getDays() * @method int getHumanDiffOptions() * @method array getIsoUnits() * @method array getLastErrors() * @method string getLocale() * @method int getMidDayAt() * @method \Illuminate\Support\Carbon|null getTestNow() * @method \Symfony\Component\Translation\TranslatorInterface getTranslator() * @method int getWeekEndsAt() * @method int getWeekStartsAt() * @method array getWeekendDays() * @method bool hasFormat($date, $format) * @method bool hasMacro($name) * @method bool hasRelativeKeywords($time) * @method bool hasTestNow() * @method \Illuminate\Support\Carbon instance($date) * @method bool isImmutable() * @method bool isModifiableUnit($unit) * @method bool isMutable() * @method bool isStrictModeEnabled() * @method bool localeHasDiffOneDayWords($locale) * @method bool localeHasDiffSyntax($locale) * @method bool localeHasDiffTwoDayWords($locale) * @method bool localeHasPeriodSyntax($locale) * @method bool localeHasShortUnits($locale) * @method void macro($name, $macro) * @method \Illuminate\Support\Carbon|null make($var) * @method \Illuminate\Support\Carbon maxValue() * @method \Illuminate\Support\Carbon minValue() * @method void mixin($mixin) * @method \Illuminate\Support\Carbon now($tz = null) * @method \Illuminate\Support\Carbon parse($time = null, $tz = null) * @method string pluralUnit(string $unit) * @method void resetMonthsOverflow() * @method void resetToStringFormat() * @method void resetYearsOverflow() * @method void serializeUsing($callback) * @method void setHumanDiffOptions($humanDiffOptions) * @method bool setLocale($locale) * @method void setMidDayAt($hour) * @method void setTestNow($testNow = null) * @method void setToStringFormat($format) * @method void setTranslator(\Symfony\Component\Translation\TranslatorInterface $translator) * @method void setUtf8($utf8) * @method void setWeekEndsAt($day) * @method void setWeekStartsAt($day) * @method void setWeekendDays($days) * @method bool shouldOverflowMonths() * @method bool shouldOverflowYears() * @method string singularUnit(string $unit) * @method \Illuminate\Support\Carbon today($tz = null) * @method \Illuminate\Support\Carbon tomorrow($tz = null) * @method void useMonthsOverflow($monthsOverflow = true) * @method void useStrictMode($strictModeEnabled = true) * @method void useYearsOverflow($yearsOverflow = true) * @method \Illuminate\Support\Carbon yesterday($tz = null) */ class DateFactory { /** * The default class that will be used for all created dates. * * @var string */ const DEFAULT_CLASS_NAME = Carbon::class; /** * The type (class) of dates that should be created. * * @var string */ protected static $dateClass; /** * This callable may be used to intercept date creation. * * @var callable */ protected static $callable; /** * The Carbon factory that should be used when creating dates. * * @var object */ protected static $factory; /** * Use the given handler when generating dates (class name, callable, or factory). * * @param mixed $handler * @return mixed * * @throws \InvalidArgumentException */ public static function use($handler) { if (is_callable($handler) && is_object($handler)) { return static::useCallable($handler); } elseif (is_string($handler)) { return static::useClass($handler); } elseif ($handler instanceof Factory) { return static::useFactory($handler); } throw new InvalidArgumentException('Invalid date creation handler. Please provide a class name, callable, or Carbon factory.'); } /** * Use the default date class when generating dates. * * @return void */ public static function useDefault() { static::$dateClass = null; static::$callable = null; static::$factory = null; } /** * Execute the given callable on each date creation. * * @param callable $callable * @return void */ public static function useCallable(callable $callable) { static::$callable = $callable; static::$dateClass = null; static::$factory = null; } /** * Use the given date type (class) when generating dates. * * @param string $dateClass * @return void */ public static function useClass($dateClass) { static::$dateClass = $dateClass; static::$factory = null; static::$callable = null; } /** * Use the given Carbon factory when generating dates. * * @param object $factory * @return void */ public static function useFactory($factory) { static::$factory = $factory; static::$dateClass = null; static::$callable = null; } /** * Handle dynamic calls to generate dates. * * @param string $method * @param array $parameters * @return mixed * * @throws \RuntimeException */ public function __call($method, $parameters) { $defaultClassName = static::DEFAULT_CLASS_NAME; // Using callable to generate dates... if (static::$callable) { return call_user_func(static::$callable, $defaultClassName::$method(...$parameters)); } // Using Carbon factory to generate dates... if (static::$factory) { return static::$factory->$method(...$parameters); } $dateClass = static::$dateClass ?: $defaultClassName; // Check if the date can be created using the public class method... if (method_exists($dateClass, $method) || method_exists($dateClass, 'hasMacro') && $dateClass::hasMacro($method)) { return $dateClass::$method(...$parameters); } // If that fails, create the date with the default class... $date = $defaultClassName::$method(...$parameters); // If the configured class has an "instance" method, we'll try to pass our date into there... if (method_exists($dateClass, 'instance')) { return $dateClass::instance($date); } // Otherwise, assume the configured class has a DateTime compatible constructor... return new $dateClass($date->format('Y-m-d H:i:s.u'), $date->getTimezone()); } } src/Illuminate/Support/Optional.php 0000644 00000005214 15107327040 0013411 0 ustar 00 <?php namespace Illuminate\Support; use ArrayAccess; use ArrayObject; use Illuminate\Support\Traits\Macroable; class Optional implements ArrayAccess { use Macroable { __call as macroCall; } /** * The underlying object. * * @var mixed */ protected $value; /** * Create a new optional instance. * * @param mixed $value * @return void */ public function __construct($value) { $this->value = $value; } /** * Dynamically access a property on the underlying object. * * @param string $key * @return mixed */ public function __get($key) { if (is_object($this->value)) { return $this->value->{$key} ?? null; } } /** * Dynamically check a property exists on the underlying object. * * @param mixed $name * @return bool */ public function __isset($name) { if (is_object($this->value)) { return isset($this->value->{$name}); } if (is_array($this->value) || $this->value instanceof ArrayObject) { return isset($this->value[$name]); } return false; } /** * Determine if an item exists at an offset. * * @param mixed $key * @return bool */ public function offsetExists($key): bool { return Arr::accessible($this->value) && Arr::exists($this->value, $key); } /** * Get an item at a given offset. * * @param mixed $key * @return mixed */ public function offsetGet($key): mixed { return Arr::get($this->value, $key); } /** * Set the item at a given offset. * * @param mixed $key * @param mixed $value * @return void */ public function offsetSet($key, $value): void { if (Arr::accessible($this->value)) { $this->value[$key] = $value; } } /** * Unset the item at a given offset. * * @param string $key * @return void */ public function offsetUnset($key): void { if (Arr::accessible($this->value)) { unset($this->value[$key]); } } /** * Dynamically pass a method to the underlying object. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } if (is_object($this->value)) { return $this->value->{$method}(...$parameters); } } } src/Illuminate/Support/DefaultProviders.php 0000644 00000006075 15107327040 0015114 0 ustar 00 <?php namespace Illuminate\Support; class DefaultProviders { /** * The current providers. * * @var array */ protected $providers; /** * Create a new default provider collection. * * @return void */ public function __construct(?array $providers = null) { $this->providers = $providers ?: [ \Illuminate\Auth\AuthServiceProvider::class, \Illuminate\Broadcasting\BroadcastServiceProvider::class, \Illuminate\Bus\BusServiceProvider::class, \Illuminate\Cache\CacheServiceProvider::class, \Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class, \Illuminate\Cookie\CookieServiceProvider::class, \Illuminate\Database\DatabaseServiceProvider::class, \Illuminate\Encryption\EncryptionServiceProvider::class, \Illuminate\Filesystem\FilesystemServiceProvider::class, \Illuminate\Foundation\Providers\FoundationServiceProvider::class, \Illuminate\Hashing\HashServiceProvider::class, \Illuminate\Mail\MailServiceProvider::class, \Illuminate\Notifications\NotificationServiceProvider::class, \Illuminate\Pagination\PaginationServiceProvider::class, \Illuminate\Pipeline\PipelineServiceProvider::class, \Illuminate\Queue\QueueServiceProvider::class, \Illuminate\Redis\RedisServiceProvider::class, \Illuminate\Auth\Passwords\PasswordResetServiceProvider::class, \Illuminate\Session\SessionServiceProvider::class, \Illuminate\Translation\TranslationServiceProvider::class, \Illuminate\Validation\ValidationServiceProvider::class, \Illuminate\View\ViewServiceProvider::class, ]; } /** * Merge the given providers into the provider collection. * * @param array $providers * @return static */ public function merge(array $providers) { $this->providers = array_merge($this->providers, $providers); return new static($this->providers); } /** * Replace the given providers with other providers. * * @param array $items * @return static */ public function replace(array $replacements) { $current = collect($this->providers); foreach ($replacements as $from => $to) { $key = $current->search($from); $current = $key ? $current->replace([$key => $to]) : $current; } return new static($current->values()->toArray()); } /** * Disable the given providers. * * @param array $providers * @return static */ public function except(array $providers) { return new static(collect($this->providers) ->reject(fn ($p) => in_array($p, $providers)) ->values() ->toArray()); } /** * Convert the provider collection to an array. * * @return array */ public function toArray() { return $this->providers; } } src/Illuminate/Support/Facades/Notification.php 0000644 00000006503 15107327040 0015602 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Notifications\ChannelManager; use Illuminate\Support\Testing\Fakes\NotificationFake; /** * @method static void send(\Illuminate\Support\Collection|array|mixed $notifiables, mixed $notification) * @method static void sendNow(\Illuminate\Support\Collection|array|mixed $notifiables, mixed $notification, array|null $channels = null) * @method static mixed channel(string|null $name = null) * @method static string getDefaultDriver() * @method static string deliversVia() * @method static void deliverVia(string $channel) * @method static \Illuminate\Notifications\ChannelManager locale(string $locale) * @method static mixed driver(string|null $driver = null) * @method static \Illuminate\Notifications\ChannelManager extend(string $driver, \Closure $callback) * @method static array getDrivers() * @method static \Illuminate\Contracts\Container\Container getContainer() * @method static \Illuminate\Notifications\ChannelManager setContainer(\Illuminate\Contracts\Container\Container $container) * @method static \Illuminate\Notifications\ChannelManager forgetDrivers() * @method static void assertSentOnDemand(string|\Closure $notification, callable|null $callback = null) * @method static void assertSentTo(mixed $notifiable, string|\Closure $notification, callable|null $callback = null) * @method static void assertSentOnDemandTimes(string $notification, int $times = 1) * @method static void assertSentToTimes(mixed $notifiable, string $notification, int $times = 1) * @method static void assertNotSentTo(mixed $notifiable, string|\Closure $notification, callable|null $callback = null) * @method static void assertNothingSent() * @method static void assertNothingSentTo(mixed $notifiable) * @method static void assertSentTimes(string $notification, int $expectedCount) * @method static void assertCount(int $expectedCount) * @method static \Illuminate\Support\Collection sent(mixed $notifiable, string $notification, callable|null $callback = null) * @method static bool hasSent(mixed $notifiable, string $notification) * @method static array sentNotifications() * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * * @see \Illuminate\Notifications\ChannelManager * @see \Illuminate\Support\Testing\Fakes\NotificationFake */ class Notification extends Facade { /** * Replace the bound instance with a fake. * * @return \Illuminate\Support\Testing\Fakes\NotificationFake */ public static function fake() { return tap(new NotificationFake, function ($fake) { static::swap($fake); }); } /** * Begin sending a notification to an anonymous notifiable. * * @param string $channel * @param mixed $route * @return \Illuminate\Notifications\AnonymousNotifiable */ public static function route($channel, $route) { return (new AnonymousNotifiable)->route($channel, $route); } /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return ChannelManager::class; } } src/Illuminate/Support/Facades/Queue.php 0000644 00000011010 15107327040 0014225 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Queue\Worker; use Illuminate\Support\Testing\Fakes\QueueFake; /** * @method static void before(mixed $callback) * @method static void after(mixed $callback) * @method static void exceptionOccurred(mixed $callback) * @method static void looping(mixed $callback) * @method static void failing(mixed $callback) * @method static void stopping(mixed $callback) * @method static bool connected(string|null $name = null) * @method static \Illuminate\Contracts\Queue\Queue connection(string|null $name = null) * @method static void extend(string $driver, \Closure $resolver) * @method static void addConnector(string $driver, \Closure $resolver) * @method static string getDefaultDriver() * @method static void setDefaultDriver(string $name) * @method static string getName(string|null $connection = null) * @method static \Illuminate\Contracts\Foundation\Application getApplication() * @method static \Illuminate\Queue\QueueManager setApplication(\Illuminate\Contracts\Foundation\Application $app) * @method static int size(string|null $queue = null) * @method static mixed push(string|object $job, mixed $data = '', string|null $queue = null) * @method static mixed pushOn(string $queue, string|object $job, mixed $data = '') * @method static mixed pushRaw(string $payload, string|null $queue = null, array $options = []) * @method static mixed later(\DateTimeInterface|\DateInterval|int $delay, string|object $job, mixed $data = '', string|null $queue = null) * @method static mixed laterOn(string $queue, \DateTimeInterface|\DateInterval|int $delay, string|object $job, mixed $data = '') * @method static mixed bulk(array $jobs, mixed $data = '', string|null $queue = null) * @method static \Illuminate\Contracts\Queue\Job|null pop(string|null $queue = null) * @method static string getConnectionName() * @method static \Illuminate\Contracts\Queue\Queue setConnectionName(string $name) * @method static mixed getJobBackoff(mixed $job) * @method static mixed getJobExpiration(mixed $job) * @method static void createPayloadUsing(callable|null $callback) * @method static \Illuminate\Container\Container getContainer() * @method static void setContainer(\Illuminate\Container\Container $container) * @method static \Illuminate\Support\Testing\Fakes\QueueFake except(array|string $jobsToBeQueued) * @method static void assertPushed(string|\Closure $job, callable|int|null $callback = null) * @method static void assertPushedOn(string $queue, string|\Closure $job, callable|null $callback = null) * @method static void assertPushedWithChain(string $job, array $expectedChain = [], callable|null $callback = null) * @method static void assertPushedWithoutChain(string $job, callable|null $callback = null) * @method static void assertClosurePushed(callable|int|null $callback = null) * @method static void assertClosureNotPushed(callable|null $callback = null) * @method static void assertNotPushed(string|\Closure $job, callable|null $callback = null) * @method static void assertNothingPushed() * @method static \Illuminate\Support\Collection pushed(string $job, callable|null $callback = null) * @method static bool hasPushed(string $job) * @method static bool shouldFakeJob(object $job) * @method static array pushedJobs() * @method static \Illuminate\Support\Testing\Fakes\QueueFake serializeAndRestore(bool $serializeAndRestore = true) * * @see \Illuminate\Queue\QueueManager * @see \Illuminate\Queue\Queue * @see \Illuminate\Support\Testing\Fakes\QueueFake */ class Queue extends Facade { /** * Register a callback to be executed to pick jobs. * * @param string $workerName * @param callable $callback * @return void */ public static function popUsing($workerName, $callback) { return Worker::popUsing($workerName, $callback); } /** * Replace the bound instance with a fake. * * @param array|string $jobsToFake * @return \Illuminate\Support\Testing\Fakes\QueueFake */ public static function fake($jobsToFake = []) { $actualQueueManager = static::isFake() ? static::getFacadeRoot()->queue : static::getFacadeRoot(); return tap(new QueueFake(static::getFacadeApplication(), $jobsToFake, $actualQueueManager), function ($fake) { static::swap($fake); }); } /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'queue'; } } src/Illuminate/Support/Facades/Vite.php 0000644 00000003417 15107327040 0014064 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static array preloadedAssets() * @method static string|null cspNonce() * @method static string useCspNonce(string|null $nonce = null) * @method static \Illuminate\Foundation\Vite useIntegrityKey(string|false $key) * @method static \Illuminate\Foundation\Vite withEntryPoints(array $entryPoints) * @method static \Illuminate\Foundation\Vite useManifestFilename(string $filename) * @method static string hotFile() * @method static \Illuminate\Foundation\Vite useHotFile(string $path) * @method static \Illuminate\Foundation\Vite useBuildDirectory(string $path) * @method static \Illuminate\Foundation\Vite useScriptTagAttributes(callable|array $attributes) * @method static \Illuminate\Foundation\Vite useStyleTagAttributes(callable|array $attributes) * @method static \Illuminate\Foundation\Vite usePreloadTagAttributes(callable|array|false $attributes) * @method static \Illuminate\Support\HtmlString|void reactRefresh() * @method static string asset(string $asset, string|null $buildDirectory = null) * @method static string content(string $asset, string|null $buildDirectory = null) * @method static string|null manifestHash(string|null $buildDirectory = null) * @method static bool isRunningHot() * @method static string toHtml() * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * * @see \Illuminate\Foundation\Vite */ class Vite extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return \Illuminate\Foundation\Vite::class; } } src/Illuminate/Support/Facades/Blade.php 0000644 00000005617 15107327040 0014170 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static void compile(string|null $path = null) * @method static string getPath() * @method static void setPath(string $path) * @method static string compileString(string $value) * @method static string render(string $string, array $data = [], bool $deleteCachedView = false) * @method static string renderComponent(\Illuminate\View\Component $component) * @method static string stripParentheses(string $expression) * @method static void extend(callable $compiler) * @method static array getExtensions() * @method static void if(string $name, callable $callback) * @method static bool check(string $name, mixed ...$parameters) * @method static void component(string $class, string|null $alias = null, string $prefix = '') * @method static void components(array $components, string $prefix = '') * @method static array getClassComponentAliases() * @method static void anonymousComponentPath(string $path, string|null $prefix = null) * @method static void anonymousComponentNamespace(string $directory, string|null $prefix = null) * @method static void componentNamespace(string $namespace, string $prefix) * @method static array getAnonymousComponentPaths() * @method static array getAnonymousComponentNamespaces() * @method static array getClassComponentNamespaces() * @method static void aliasComponent(string $path, string|null $alias = null) * @method static void include(string $path, string|null $alias = null) * @method static void aliasInclude(string $path, string|null $alias = null) * @method static void directive(string $name, callable $handler) * @method static array getCustomDirectives() * @method static \Illuminate\View\Compilers\BladeCompiler prepareStringsForCompilationUsing(callable $callback) * @method static void precompiler(callable $precompiler) * @method static void setEchoFormat(string $format) * @method static void withDoubleEncoding() * @method static void withoutDoubleEncoding() * @method static void withoutComponentTags() * @method static string getCompiledPath(string $path) * @method static bool isExpired(string $path) * @method static string newComponentHash(string $component) * @method static string compileClassComponentOpening(string $component, string $alias, string $data, string $hash) * @method static string compileEndComponentClass() * @method static mixed sanitizeComponentAttribute(mixed $value) * @method static string compileEndOnce() * @method static void stringable(string|callable $class, callable|null $handler = null) * @method static string compileEchos(string $value) * @method static string applyEchoHandler(string $value) * * @see \Illuminate\View\Compilers\BladeCompiler */ class Blade extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'blade.compiler'; } } src/Illuminate/Support/Facades/Hash.php 0000644 00000002550 15107327040 0014035 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static \Illuminate\Hashing\BcryptHasher createBcryptDriver() * @method static \Illuminate\Hashing\ArgonHasher createArgonDriver() * @method static \Illuminate\Hashing\Argon2IdHasher createArgon2idDriver() * @method static array info(string $hashedValue) * @method static string make(string $value, array $options = []) * @method static bool check(string $value, string $hashedValue, array $options = []) * @method static bool needsRehash(string $hashedValue, array $options = []) * @method static bool isHashed(string $value) * @method static string getDefaultDriver() * @method static mixed driver(string|null $driver = null) * @method static \Illuminate\Hashing\HashManager extend(string $driver, \Closure $callback) * @method static array getDrivers() * @method static \Illuminate\Contracts\Container\Container getContainer() * @method static \Illuminate\Hashing\HashManager setContainer(\Illuminate\Contracts\Container\Container $container) * @method static \Illuminate\Hashing\HashManager forgetDrivers() * * @see \Illuminate\Hashing\HashManager * @see \Illuminate\Hashing\AbstractHasher */ class Hash extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'hash'; } } src/Illuminate/Support/Facades/Session.php 0000644 00000006752 15107327040 0014605 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static bool shouldBlock() * @method static string|null blockDriver() * @method static int defaultRouteBlockLockSeconds() * @method static int defaultRouteBlockWaitSeconds() * @method static array getSessionConfig() * @method static string getDefaultDriver() * @method static void setDefaultDriver(string $name) * @method static mixed driver(string|null $driver = null) * @method static \Illuminate\Session\SessionManager extend(string $driver, \Closure $callback) * @method static array getDrivers() * @method static \Illuminate\Contracts\Container\Container getContainer() * @method static \Illuminate\Session\SessionManager setContainer(\Illuminate\Contracts\Container\Container $container) * @method static \Illuminate\Session\SessionManager forgetDrivers() * @method static bool start() * @method static void save() * @method static void ageFlashData() * @method static array all() * @method static array only(array $keys) * @method static bool exists(string|array $key) * @method static bool missing(string|array $key) * @method static bool has(string|array $key) * @method static mixed get(string $key, mixed $default = null) * @method static mixed pull(string $key, mixed $default = null) * @method static bool hasOldInput(string|null $key = null) * @method static mixed getOldInput(string|null $key = null, mixed $default = null) * @method static void replace(array $attributes) * @method static void put(string|array $key, mixed $value = null) * @method static mixed remember(string $key, \Closure $callback) * @method static void push(string $key, mixed $value) * @method static mixed increment(string $key, int $amount = 1) * @method static int decrement(string $key, int $amount = 1) * @method static void flash(string $key, mixed $value = true) * @method static void now(string $key, mixed $value) * @method static void reflash() * @method static void keep(array|mixed $keys = null) * @method static void flashInput(array $value) * @method static mixed remove(string $key) * @method static void forget(string|array $keys) * @method static void flush() * @method static bool invalidate() * @method static bool regenerate(bool $destroy = false) * @method static bool migrate(bool $destroy = false) * @method static bool isStarted() * @method static string getName() * @method static void setName(string $name) * @method static string getId() * @method static void setId(string|null $id) * @method static bool isValidId(string|null $id) * @method static void setExists(bool $value) * @method static string token() * @method static void regenerateToken() * @method static string|null previousUrl() * @method static void setPreviousUrl(string $url) * @method static void passwordConfirmed() * @method static \SessionHandlerInterface getHandler() * @method static \SessionHandlerInterface setHandler(\SessionHandlerInterface $handler) * @method static bool handlerNeedsRequest() * @method static void setRequestOnHandler(\Illuminate\Http\Request $request) * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * * @see \Illuminate\Session\SessionManager */ class Session extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'session'; } } src/Illuminate/Support/Facades/Route.php 0000644 00000017576 15107327040 0014266 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static \Illuminate\Routing\Route get(string $uri, array|string|callable|null $action = null) * @method static \Illuminate\Routing\Route post(string $uri, array|string|callable|null $action = null) * @method static \Illuminate\Routing\Route put(string $uri, array|string|callable|null $action = null) * @method static \Illuminate\Routing\Route patch(string $uri, array|string|callable|null $action = null) * @method static \Illuminate\Routing\Route delete(string $uri, array|string|callable|null $action = null) * @method static \Illuminate\Routing\Route options(string $uri, array|string|callable|null $action = null) * @method static \Illuminate\Routing\Route any(string $uri, array|string|callable|null $action = null) * @method static \Illuminate\Routing\Route fallback(array|string|callable|null $action) * @method static \Illuminate\Routing\Route redirect(string $uri, string $destination, int $status = 302) * @method static \Illuminate\Routing\Route permanentRedirect(string $uri, string $destination) * @method static \Illuminate\Routing\Route view(string $uri, string $view, array $data = [], int|array $status = 200, array $headers = []) * @method static \Illuminate\Routing\Route match(array|string $methods, string $uri, array|string|callable|null $action = null) * @method static void resources(array $resources, array $options = []) * @method static \Illuminate\Routing\PendingResourceRegistration resource(string $name, string $controller, array $options = []) * @method static void apiResources(array $resources, array $options = []) * @method static \Illuminate\Routing\PendingResourceRegistration apiResource(string $name, string $controller, array $options = []) * @method static void singletons(array $singletons, array $options = []) * @method static \Illuminate\Routing\PendingSingletonResourceRegistration singleton(string $name, string $controller, array $options = []) * @method static void apiSingletons(array $singletons, array $options = []) * @method static \Illuminate\Routing\PendingSingletonResourceRegistration apiSingleton(string $name, string $controller, array $options = []) * @method static \Illuminate\Routing\Router group(array $attributes, \Closure|array|string $routes) * @method static array mergeWithLastGroup(array $new, bool $prependExistingPrefix = true) * @method static string getLastGroupPrefix() * @method static \Illuminate\Routing\Route addRoute(array|string $methods, string $uri, array|string|callable|null $action) * @method static \Illuminate\Routing\Route newRoute(array|string $methods, string $uri, mixed $action) * @method static \Symfony\Component\HttpFoundation\Response respondWithRoute(string $name) * @method static \Symfony\Component\HttpFoundation\Response dispatch(\Illuminate\Http\Request $request) * @method static \Symfony\Component\HttpFoundation\Response dispatchToRoute(\Illuminate\Http\Request $request) * @method static array gatherRouteMiddleware(\Illuminate\Routing\Route $route) * @method static array resolveMiddleware(array $middleware, array $excluded = []) * @method static \Symfony\Component\HttpFoundation\Response prepareResponse(\Symfony\Component\HttpFoundation\Request $request, mixed $response) * @method static \Symfony\Component\HttpFoundation\Response toResponse(\Symfony\Component\HttpFoundation\Request $request, mixed $response) * @method static \Illuminate\Routing\Route substituteBindings(\Illuminate\Routing\Route $route) * @method static void substituteImplicitBindings(\Illuminate\Routing\Route $route) * @method static void matched(string|callable $callback) * @method static array getMiddleware() * @method static \Illuminate\Routing\Router aliasMiddleware(string $name, string $class) * @method static bool hasMiddlewareGroup(string $name) * @method static array getMiddlewareGroups() * @method static \Illuminate\Routing\Router middlewareGroup(string $name, array $middleware) * @method static \Illuminate\Routing\Router prependMiddlewareToGroup(string $group, string $middleware) * @method static \Illuminate\Routing\Router pushMiddlewareToGroup(string $group, string $middleware) * @method static \Illuminate\Routing\Router removeMiddlewareFromGroup(string $group, string $middleware) * @method static \Illuminate\Routing\Router flushMiddlewareGroups() * @method static void bind(string $key, string|callable $binder) * @method static void model(string $key, string $class, \Closure|null $callback = null) * @method static \Closure|null getBindingCallback(string $key) * @method static array getPatterns() * @method static void pattern(string $key, string $pattern) * @method static void patterns(array $patterns) * @method static bool hasGroupStack() * @method static array getGroupStack() * @method static mixed input(string $key, string|null $default = null) * @method static \Illuminate\Http\Request getCurrentRequest() * @method static \Illuminate\Routing\Route|null getCurrentRoute() * @method static \Illuminate\Routing\Route|null current() * @method static bool has(string|array $name) * @method static string|null currentRouteName() * @method static bool is(mixed ...$patterns) * @method static bool currentRouteNamed(mixed ...$patterns) * @method static string|null currentRouteAction() * @method static bool uses(array ...$patterns) * @method static bool currentRouteUses(string $action) * @method static void singularResourceParameters(bool $singular = true) * @method static void resourceParameters(array $parameters = []) * @method static array|null resourceVerbs(array $verbs = []) * @method static \Illuminate\Routing\RouteCollectionInterface getRoutes() * @method static void setRoutes(\Illuminate\Routing\RouteCollection $routes) * @method static void setCompiledRoutes(array $routes) * @method static array uniqueMiddleware(array $middleware) * @method static \Illuminate\Routing\Router setContainer(\Illuminate\Container\Container $container) * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * @method static mixed macroCall(string $method, array $parameters) * @method static \Illuminate\Routing\RouteRegistrar attribute(string $key, mixed $value) * @method static \Illuminate\Routing\RouteRegistrar whereAlpha(array|string $parameters) * @method static \Illuminate\Routing\RouteRegistrar whereAlphaNumeric(array|string $parameters) * @method static \Illuminate\Routing\RouteRegistrar whereNumber(array|string $parameters) * @method static \Illuminate\Routing\RouteRegistrar whereUlid(array|string $parameters) * @method static \Illuminate\Routing\RouteRegistrar whereUuid(array|string $parameters) * @method static \Illuminate\Routing\RouteRegistrar whereIn(array|string $parameters, array $values) * @method static \Illuminate\Routing\RouteRegistrar as(string $value) * @method static \Illuminate\Routing\RouteRegistrar controller(string $controller) * @method static \Illuminate\Routing\RouteRegistrar domain(string $value) * @method static \Illuminate\Routing\RouteRegistrar middleware(array|string|null $middleware) * @method static \Illuminate\Routing\RouteRegistrar name(string $value) * @method static \Illuminate\Routing\RouteRegistrar namespace(string|null $value) * @method static \Illuminate\Routing\RouteRegistrar prefix(string $prefix) * @method static \Illuminate\Routing\RouteRegistrar scopeBindings() * @method static \Illuminate\Routing\RouteRegistrar where(array $where) * @method static \Illuminate\Routing\RouteRegistrar withoutMiddleware(array|string $middleware) * @method static \Illuminate\Routing\RouteRegistrar withoutScopedBindings() * * @see \Illuminate\Routing\Router */ class Route extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'router'; } } src/Illuminate/Support/Facades/Date.php 0000644 00000012534 15107327040 0014032 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Support\DateFactory; /** * @see https://carbon.nesbot.com/docs/ * @see https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Factory.php * * @method static mixed use(mixed $handler) * @method static void useDefault() * @method static void useCallable(callable $callable) * @method static void useClass(string $dateClass) * @method static void useFactory(object $factory) * @method static \Illuminate\Support\Carbon create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null) * @method static \Illuminate\Support\Carbon createFromDate($year = null, $month = null, $day = null, $tz = null) * @method static \Illuminate\Support\Carbon|false createFromFormat($format, $time, $tz = null) * @method static \Illuminate\Support\Carbon createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) * @method static \Illuminate\Support\Carbon createFromTimeString($time, $tz = null) * @method static \Illuminate\Support\Carbon createFromTimestamp($timestamp, $tz = null) * @method static \Illuminate\Support\Carbon createFromTimestampMs($timestamp, $tz = null) * @method static \Illuminate\Support\Carbon createFromTimestampUTC($timestamp) * @method static \Illuminate\Support\Carbon createMidnightDate($year = null, $month = null, $day = null, $tz = null) * @method static \Illuminate\Support\Carbon|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) * @method static void disableHumanDiffOption($humanDiffOption) * @method static void enableHumanDiffOption($humanDiffOption) * @method static mixed executeWithLocale($locale, $func) * @method static \Illuminate\Support\Carbon fromSerialized($value) * @method static array getAvailableLocales() * @method static array getDays() * @method static int getHumanDiffOptions() * @method static array getIsoUnits() * @method static array getLastErrors() * @method static string getLocale() * @method static int getMidDayAt() * @method static \Illuminate\Support\Carbon|null getTestNow() * @method static \Symfony\Component\Translation\TranslatorInterface getTranslator() * @method static int getWeekEndsAt() * @method static int getWeekStartsAt() * @method static array getWeekendDays() * @method static bool hasFormat($date, $format) * @method static bool hasMacro($name) * @method static bool hasRelativeKeywords($time) * @method static bool hasTestNow() * @method static \Illuminate\Support\Carbon instance($date) * @method static bool isImmutable() * @method static bool isModifiableUnit($unit) * @method static bool isMutable() * @method static bool isStrictModeEnabled() * @method static bool localeHasDiffOneDayWords($locale) * @method static bool localeHasDiffSyntax($locale) * @method static bool localeHasDiffTwoDayWords($locale) * @method static bool localeHasPeriodSyntax($locale) * @method static bool localeHasShortUnits($locale) * @method static void macro($name, $macro) * @method static \Illuminate\Support\Carbon|null make($var) * @method static \Illuminate\Support\Carbon maxValue() * @method static \Illuminate\Support\Carbon minValue() * @method static void mixin($mixin) * @method static \Illuminate\Support\Carbon now($tz = null) * @method static \Illuminate\Support\Carbon parse($time = null, $tz = null) * @method static string pluralUnit(string $unit) * @method static void resetMonthsOverflow() * @method static void resetToStringFormat() * @method static void resetYearsOverflow() * @method static void serializeUsing($callback) * @method static void setHumanDiffOptions($humanDiffOptions) * @method static bool setLocale($locale) * @method static void setMidDayAt($hour) * @method static void setTestNow($testNow = null) * @method static void setToStringFormat($format) * @method static void setTranslator(\Symfony\Component\Translation\TranslatorInterface $translator) * @method static void setUtf8($utf8) * @method static void setWeekEndsAt($day) * @method static void setWeekStartsAt($day) * @method static void setWeekendDays($days) * @method static bool shouldOverflowMonths() * @method static bool shouldOverflowYears() * @method static string singularUnit(string $unit) * @method static \Illuminate\Support\Carbon today($tz = null) * @method static \Illuminate\Support\Carbon tomorrow($tz = null) * @method static void useMonthsOverflow($monthsOverflow = true) * @method static void useStrictMode($strictModeEnabled = true) * @method static void useYearsOverflow($yearsOverflow = true) * @method static \Illuminate\Support\Carbon yesterday($tz = null) * * @see \Illuminate\Support\DateFactory */ class Date extends Facade { const DEFAULT_FACADE = DateFactory::class; /** * Get the registered name of the component. * * @return string * * @throws \RuntimeException */ protected static function getFacadeAccessor() { return 'date'; } /** * Resolve the facade root instance from the container. * * @param string $name * @return mixed */ protected static function resolveFacadeInstance($name) { if (! isset(static::$resolvedInstance[$name]) && ! isset(static::$app, static::$app[$name])) { $class = static::DEFAULT_FACADE; static::swap(new $class); } return parent::resolveFacadeInstance($name); } } src/Illuminate/Support/Facades/Mail.php 0000644 00000011543 15107327040 0014036 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Support\Testing\Fakes\MailFake; /** * @method static \Illuminate\Contracts\Mail\Mailer mailer(string|null $name = null) * @method static \Illuminate\Mail\Mailer driver(string|null $driver = null) * @method static \Symfony\Component\Mailer\Transport\TransportInterface createSymfonyTransport(array $config) * @method static string getDefaultDriver() * @method static void setDefaultDriver(string $name) * @method static void purge(string|null $name = null) * @method static \Illuminate\Mail\MailManager extend(string $driver, \Closure $callback) * @method static \Illuminate\Contracts\Foundation\Application getApplication() * @method static \Illuminate\Mail\MailManager setApplication(\Illuminate\Contracts\Foundation\Application $app) * @method static \Illuminate\Mail\MailManager forgetMailers() * @method static void alwaysFrom(string $address, string|null $name = null) * @method static void alwaysReplyTo(string $address, string|null $name = null) * @method static void alwaysReturnPath(string $address) * @method static void alwaysTo(string $address, string|null $name = null) * @method static \Illuminate\Mail\PendingMail to(mixed $users, string|null $name = null) * @method static \Illuminate\Mail\PendingMail cc(mixed $users, string|null $name = null) * @method static \Illuminate\Mail\PendingMail bcc(mixed $users, string|null $name = null) * @method static \Illuminate\Mail\SentMessage|null html(string $html, mixed $callback) * @method static \Illuminate\Mail\SentMessage|null raw(string $text, mixed $callback) * @method static \Illuminate\Mail\SentMessage|null plain(string $view, array $data, mixed $callback) * @method static string render(string|array $view, array $data = []) * @method static \Illuminate\Mail\SentMessage|null send(\Illuminate\Contracts\Mail\Mailable|string|array $view, array $data = [], \Closure|string|null $callback = null) * @method static mixed queue(\Illuminate\Contracts\Mail\Mailable|string|array $view, string|null $queue = null) * @method static mixed onQueue(string $queue, \Illuminate\Contracts\Mail\Mailable $view) * @method static mixed queueOn(string $queue, \Illuminate\Contracts\Mail\Mailable $view) * @method static mixed later(\DateTimeInterface|\DateInterval|int $delay, \Illuminate\Contracts\Mail\Mailable $view, string|null $queue = null) * @method static mixed laterOn(string $queue, \DateTimeInterface|\DateInterval|int $delay, \Illuminate\Contracts\Mail\Mailable $view) * @method static \Symfony\Component\Mailer\Transport\TransportInterface getSymfonyTransport() * @method static \Illuminate\Contracts\View\Factory getViewFactory() * @method static void setSymfonyTransport(\Symfony\Component\Mailer\Transport\TransportInterface $transport) * @method static \Illuminate\Mail\Mailer setQueue(\Illuminate\Contracts\Queue\Factory $queue) * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * @method static void assertSent(string|\Closure $mailable, callable|int|null $callback = null) * @method static void assertNotOutgoing(string|\Closure $mailable, callable|null $callback = null) * @method static void assertNotSent(string|\Closure $mailable, callable|null $callback = null) * @method static void assertNothingOutgoing() * @method static void assertNothingSent() * @method static void assertQueued(string|\Closure $mailable, callable|int|null $callback = null) * @method static void assertNotQueued(string|\Closure $mailable, callable|null $callback = null) * @method static void assertNothingQueued() * @method static void assertSentCount(int $count) * @method static void assertQueuedCount(int $count) * @method static void assertOutgoingCount(int $count) * @method static \Illuminate\Support\Collection sent(string|\Closure $mailable, callable|null $callback = null) * @method static bool hasSent(string $mailable) * @method static \Illuminate\Support\Collection queued(string|\Closure $mailable, callable|null $callback = null) * @method static bool hasQueued(string $mailable) * * @see \Illuminate\Mail\MailManager * @see \Illuminate\Support\Testing\Fakes\MailFake */ class Mail extends Facade { /** * Replace the bound instance with a fake. * * @return \Illuminate\Support\Testing\Fakes\MailFake */ public static function fake() { $actualMailManager = static::isFake() ? static::getFacadeRoot()->manager : static::getFacadeRoot(); return tap(new MailFake($actualMailManager), function ($fake) { static::swap($fake); }); } /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'mail.manager'; } } src/Illuminate/Support/Facades/RateLimiter.php 0000644 00000002105 15107327040 0015367 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static \Illuminate\Cache\RateLimiter for(string $name, \Closure $callback) * @method static \Closure|null limiter(string $name) * @method static mixed attempt(string $key, int $maxAttempts, \Closure $callback, int $decaySeconds = 60) * @method static bool tooManyAttempts(string $key, int $maxAttempts) * @method static int hit(string $key, int $decaySeconds = 60) * @method static mixed attempts(string $key) * @method static mixed resetAttempts(string $key) * @method static int remaining(string $key, int $maxAttempts) * @method static int retriesLeft(string $key, int $maxAttempts) * @method static void clear(string $key) * @method static int availableIn(string $key) * @method static string cleanRateLimiterKey(string $key) * * @see \Illuminate\Cache\RateLimiter */ class RateLimiter extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return \Illuminate\Cache\RateLimiter::class; } } src/Illuminate/Support/Facades/Auth.php 0000644 00000011740 15107327040 0014054 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Laravel\Ui\UiServiceProvider; use RuntimeException; /** * @method static \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard guard(string|null $name = null) * @method static \Illuminate\Auth\SessionGuard createSessionDriver(string $name, array $config) * @method static \Illuminate\Auth\TokenGuard createTokenDriver(string $name, array $config) * @method static string getDefaultDriver() * @method static void shouldUse(string $name) * @method static void setDefaultDriver(string $name) * @method static \Illuminate\Auth\AuthManager viaRequest(string $driver, callable $callback) * @method static \Closure userResolver() * @method static \Illuminate\Auth\AuthManager resolveUsersUsing(\Closure $userResolver) * @method static \Illuminate\Auth\AuthManager extend(string $driver, \Closure $callback) * @method static \Illuminate\Auth\AuthManager provider(string $name, \Closure $callback) * @method static bool hasResolvedGuards() * @method static \Illuminate\Auth\AuthManager forgetGuards() * @method static \Illuminate\Auth\AuthManager setApplication(\Illuminate\Contracts\Foundation\Application $app) * @method static \Illuminate\Contracts\Auth\UserProvider|null createUserProvider(string|null $provider = null) * @method static string getDefaultUserProvider() * @method static bool check() * @method static bool guest() * @method static \Illuminate\Contracts\Auth\Authenticatable|null user() * @method static int|string|null id() * @method static bool validate(array $credentials = []) * @method static bool hasUser() * @method static void setUser(\Illuminate\Contracts\Auth\Authenticatable $user) * @method static bool attempt(array $credentials = [], bool $remember = false) * @method static bool once(array $credentials = []) * @method static void login(\Illuminate\Contracts\Auth\Authenticatable $user, bool $remember = false) * @method static \Illuminate\Contracts\Auth\Authenticatable|bool loginUsingId(mixed $id, bool $remember = false) * @method static \Illuminate\Contracts\Auth\Authenticatable|bool onceUsingId(mixed $id) * @method static bool viaRemember() * @method static void logout() * @method static \Symfony\Component\HttpFoundation\Response|null basic(string $field = 'email', array $extraConditions = []) * @method static \Symfony\Component\HttpFoundation\Response|null onceBasic(string $field = 'email', array $extraConditions = []) * @method static bool attemptWhen(array $credentials = [], array|callable|null $callbacks = null, bool $remember = false) * @method static void logoutCurrentDevice() * @method static \Illuminate\Contracts\Auth\Authenticatable|null logoutOtherDevices(string $password, string $attribute = 'password') * @method static void attempting(mixed $callback) * @method static \Illuminate\Contracts\Auth\Authenticatable getLastAttempted() * @method static string getName() * @method static string getRecallerName() * @method static \Illuminate\Auth\SessionGuard setRememberDuration(int $minutes) * @method static \Illuminate\Contracts\Cookie\QueueingFactory getCookieJar() * @method static void setCookieJar(\Illuminate\Contracts\Cookie\QueueingFactory $cookie) * @method static \Illuminate\Contracts\Events\Dispatcher getDispatcher() * @method static void setDispatcher(\Illuminate\Contracts\Events\Dispatcher $events) * @method static \Illuminate\Contracts\Session\Session getSession() * @method static \Illuminate\Contracts\Auth\Authenticatable|null getUser() * @method static \Symfony\Component\HttpFoundation\Request getRequest() * @method static \Illuminate\Auth\SessionGuard setRequest(\Symfony\Component\HttpFoundation\Request $request) * @method static \Illuminate\Support\Timebox getTimebox() * @method static \Illuminate\Contracts\Auth\Authenticatable authenticate() * @method static \Illuminate\Auth\SessionGuard forgetUser() * @method static \Illuminate\Contracts\Auth\UserProvider getProvider() * @method static void setProvider(\Illuminate\Contracts\Auth\UserProvider $provider) * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * * @see \Illuminate\Auth\AuthManager * @see \Illuminate\Auth\SessionGuard */ class Auth extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'auth'; } /** * Register the typical authentication routes for an application. * * @param array $options * @return void * * @throws \RuntimeException */ public static function routes(array $options = []) { if (! static::$app->providerIsLoaded(UiServiceProvider::class)) { throw new RuntimeException('In order to use the Auth::routes() method, please install the laravel/ui package.'); } static::$app->make('router')->auth($options); } } src/Illuminate/Support/Facades/Facade.php 0000644 00000021276 15107327040 0014323 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Closure; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Illuminate\Support\Js; use Illuminate\Support\Str; use Illuminate\Support\Testing\Fakes\Fake; use Mockery; use Mockery\LegacyMockInterface; use RuntimeException; abstract class Facade { /** * The application instance being facaded. * * @var \Illuminate\Contracts\Foundation\Application */ protected static $app; /** * The resolved object instances. * * @var array */ protected static $resolvedInstance; /** * Indicates if the resolved instance should be cached. * * @var bool */ protected static $cached = true; /** * Run a Closure when the facade has been resolved. * * @param \Closure $callback * @return void */ public static function resolved(Closure $callback) { $accessor = static::getFacadeAccessor(); if (static::$app->resolved($accessor) === true) { $callback(static::getFacadeRoot(), static::$app); } static::$app->afterResolving($accessor, function ($service, $app) use ($callback) { $callback($service, $app); }); } /** * Convert the facade into a Mockery spy. * * @return \Mockery\MockInterface */ public static function spy() { if (! static::isMock()) { $class = static::getMockableClass(); return tap($class ? Mockery::spy($class) : Mockery::spy(), function ($spy) { static::swap($spy); }); } } /** * Initiate a partial mock on the facade. * * @return \Mockery\MockInterface */ public static function partialMock() { $name = static::getFacadeAccessor(); $mock = static::isMock() ? static::$resolvedInstance[$name] : static::createFreshMockInstance(); return $mock->makePartial(); } /** * Initiate a mock expectation on the facade. * * @return \Mockery\Expectation */ public static function shouldReceive() { $name = static::getFacadeAccessor(); $mock = static::isMock() ? static::$resolvedInstance[$name] : static::createFreshMockInstance(); return $mock->shouldReceive(...func_get_args()); } /** * Initiate a mock expectation on the facade. * * @return \Mockery\Expectation */ public static function expects() { $name = static::getFacadeAccessor(); $mock = static::isMock() ? static::$resolvedInstance[$name] : static::createFreshMockInstance(); return $mock->expects(...func_get_args()); } /** * Create a fresh mock instance for the given class. * * @return \Mockery\MockInterface */ protected static function createFreshMockInstance() { return tap(static::createMock(), function ($mock) { static::swap($mock); $mock->shouldAllowMockingProtectedMethods(); }); } /** * Create a fresh mock instance for the given class. * * @return \Mockery\MockInterface */ protected static function createMock() { $class = static::getMockableClass(); return $class ? Mockery::mock($class) : Mockery::mock(); } /** * Determines whether a mock is set as the instance of the facade. * * @return bool */ protected static function isMock() { $name = static::getFacadeAccessor(); return isset(static::$resolvedInstance[$name]) && static::$resolvedInstance[$name] instanceof LegacyMockInterface; } /** * Get the mockable class for the bound instance. * * @return string|null */ protected static function getMockableClass() { if ($root = static::getFacadeRoot()) { return get_class($root); } } /** * Hotswap the underlying instance behind the facade. * * @param mixed $instance * @return void */ public static function swap($instance) { static::$resolvedInstance[static::getFacadeAccessor()] = $instance; if (isset(static::$app)) { static::$app->instance(static::getFacadeAccessor(), $instance); } } /** * Determines whether a "fake" has been set as the facade instance. * * @return bool */ protected static function isFake() { $name = static::getFacadeAccessor(); return isset(static::$resolvedInstance[$name]) && static::$resolvedInstance[$name] instanceof Fake; } /** * Get the root object behind the facade. * * @return mixed */ public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); } /** * Get the registered name of the component. * * @return string * * @throws \RuntimeException */ protected static function getFacadeAccessor() { throw new RuntimeException('Facade does not implement getFacadeAccessor method.'); } /** * Resolve the facade root instance from the container. * * @param string $name * @return mixed */ protected static function resolveFacadeInstance($name) { if (isset(static::$resolvedInstance[$name])) { return static::$resolvedInstance[$name]; } if (static::$app) { if (static::$cached) { return static::$resolvedInstance[$name] = static::$app[$name]; } return static::$app[$name]; } } /** * Clear a resolved facade instance. * * @param string $name * @return void */ public static function clearResolvedInstance($name) { unset(static::$resolvedInstance[$name]); } /** * Clear all of the resolved instances. * * @return void */ public static function clearResolvedInstances() { static::$resolvedInstance = []; } /** * Get the application default aliases. * * @return \Illuminate\Support\Collection */ public static function defaultAliases() { return collect([ 'App' => App::class, 'Arr' => Arr::class, 'Artisan' => Artisan::class, 'Auth' => Auth::class, 'Blade' => Blade::class, 'Broadcast' => Broadcast::class, 'Bus' => Bus::class, 'Cache' => Cache::class, 'Config' => Config::class, 'Cookie' => Cookie::class, 'Crypt' => Crypt::class, 'Date' => Date::class, 'DB' => DB::class, 'Eloquent' => Model::class, 'Event' => Event::class, 'File' => File::class, 'Gate' => Gate::class, 'Hash' => Hash::class, 'Http' => Http::class, 'Js' => Js::class, 'Lang' => Lang::class, 'Log' => Log::class, 'Mail' => Mail::class, 'Notification' => Notification::class, 'Password' => Password::class, 'Process' => Process::class, 'Queue' => Queue::class, 'RateLimiter' => RateLimiter::class, 'Redirect' => Redirect::class, 'Request' => Request::class, 'Response' => Response::class, 'Route' => Route::class, 'Schema' => Schema::class, 'Session' => Session::class, 'Storage' => Storage::class, 'Str' => Str::class, 'URL' => URL::class, 'Validator' => Validator::class, 'View' => View::class, 'Vite' => Vite::class, ]); } /** * Get the application instance behind the facade. * * @return \Illuminate\Contracts\Foundation\Application */ public static function getFacadeApplication() { return static::$app; } /** * Set the application instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public static function setFacadeApplication($app) { static::$app = $app; } /** * Handle dynamic, static calls to the object. * * @param string $method * @param array $args * @return mixed * * @throws \RuntimeException */ public static function __callStatic($method, $args) { $instance = static::getFacadeRoot(); if (! $instance) { throw new RuntimeException('A facade root has not been set.'); } return $instance->$method(...$args); } } src/Illuminate/Support/Facades/Config.php 0000644 00000001571 15107327040 0014361 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static bool has(string $key) * @method static mixed get(array|string $key, mixed $default = null) * @method static array getMany(array $keys) * @method static void set(array|string $key, mixed $value = null) * @method static void prepend(string $key, mixed $value) * @method static void push(string $key, mixed $value) * @method static array all() * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * * @see \Illuminate\Config\Repository */ class Config extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'config'; } } src/Illuminate/Support/Facades/Schema.php 0000644 00000005112 15107327040 0014347 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static void defaultStringLength(int $length) * @method static void defaultMorphKeyType(string $type) * @method static void morphUsingUuids() * @method static void morphUsingUlids() * @method static void useNativeSchemaOperationsIfPossible(bool $value = true) * @method static bool createDatabase(string $name) * @method static bool dropDatabaseIfExists(string $name) * @method static bool hasTable(string $table) * @method static bool hasColumn(string $table, string $column) * @method static bool hasColumns(string $table, array $columns) * @method static void whenTableHasColumn(string $table, string $column, \Closure $callback) * @method static void whenTableDoesntHaveColumn(string $table, string $column, \Closure $callback) * @method static string getColumnType(string $table, string $column, bool $fullDefinition = false) * @method static array getColumnListing(string $table) * @method static array getColumns(string $table) * @method static void table(string $table, \Closure $callback) * @method static void create(string $table, \Closure $callback) * @method static void drop(string $table) * @method static void dropIfExists(string $table) * @method static void dropColumns(string $table, string|array $columns) * @method static void dropAllTables() * @method static void dropAllViews() * @method static void dropAllTypes() * @method static array getAllTables() * @method static void rename(string $from, string $to) * @method static bool enableForeignKeyConstraints() * @method static bool disableForeignKeyConstraints() * @method static mixed withoutForeignKeyConstraints(\Closure $callback) * @method static \Illuminate\Database\Connection getConnection() * @method static \Illuminate\Database\Schema\Builder setConnection(\Illuminate\Database\Connection $connection) * @method static void blueprintResolver(\Closure $resolver) * * @see \Illuminate\Database\Schema\Builder */ class Schema extends Facade { /** * Indicates if the resolved facade should be cached. * * @var bool */ protected static $cached = false; /** * Get a schema builder instance for a connection. * * @param string|null $name * @return \Illuminate\Database\Schema\Builder */ public static function connection($name) { return static::$app['db']->connection($name)->getSchemaBuilder(); } /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'db.schema'; } } src/Illuminate/Support/Facades/Lang.php 0000644 00000003744 15107327040 0014041 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static bool hasForLocale(string $key, string|null $locale = null) * @method static bool has(string $key, string|null $locale = null, bool $fallback = true) * @method static string|array get(string $key, array $replace = [], string|null $locale = null, bool $fallback = true) * @method static string choice(string $key, \Countable|int|array $number, array $replace = [], string|null $locale = null) * @method static void addLines(array $lines, string $locale, string $namespace = '*') * @method static void load(string $namespace, string $group, string $locale) * @method static void addNamespace(string $namespace, string $hint) * @method static void addJsonPath(string $path) * @method static array parseKey(string $key) * @method static void determineLocalesUsing(callable $callback) * @method static \Illuminate\Translation\MessageSelector getSelector() * @method static void setSelector(\Illuminate\Translation\MessageSelector $selector) * @method static \Illuminate\Contracts\Translation\Loader getLoader() * @method static string locale() * @method static string getLocale() * @method static void setLocale(string $locale) * @method static string getFallback() * @method static void setFallback(string $fallback) * @method static void setLoaded(array $loaded) * @method static void stringable(callable|string $class, callable|null $handler = null) * @method static void setParsedKey(string $key, array $parsed) * @method static void flushParsedKeys() * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * * @see \Illuminate\Translation\Translator */ class Lang extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'translator'; } } src/Illuminate/Support/Facades/View.php 0000644 00000012633 15107327040 0014067 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static \Illuminate\Contracts\View\View file(string $path, \Illuminate\Contracts\Support\Arrayable|array $data = [], array $mergeData = []) * @method static \Illuminate\Contracts\View\View make(string $view, \Illuminate\Contracts\Support\Arrayable|array $data = [], array $mergeData = []) * @method static \Illuminate\Contracts\View\View first(array $views, \Illuminate\Contracts\Support\Arrayable|array $data = [], array $mergeData = []) * @method static string renderWhen(bool $condition, string $view, \Illuminate\Contracts\Support\Arrayable|array $data = [], array $mergeData = []) * @method static string renderUnless(bool $condition, string $view, \Illuminate\Contracts\Support\Arrayable|array $data = [], array $mergeData = []) * @method static string renderEach(string $view, array $data, string $iterator, string $empty = 'raw|') * @method static bool exists(string $view) * @method static \Illuminate\Contracts\View\Engine getEngineFromPath(string $path) * @method static mixed share(array|string $key, mixed|null $value = null) * @method static void incrementRender() * @method static void decrementRender() * @method static bool doneRendering() * @method static bool hasRenderedOnce(string $id) * @method static void markAsRenderedOnce(string $id) * @method static void addLocation(string $location) * @method static \Illuminate\View\Factory addNamespace(string $namespace, string|array $hints) * @method static \Illuminate\View\Factory prependNamespace(string $namespace, string|array $hints) * @method static \Illuminate\View\Factory replaceNamespace(string $namespace, string|array $hints) * @method static void addExtension(string $extension, string $engine, \Closure|null $resolver = null) * @method static void flushState() * @method static void flushStateIfDoneRendering() * @method static array getExtensions() * @method static \Illuminate\View\Engines\EngineResolver getEngineResolver() * @method static \Illuminate\View\ViewFinderInterface getFinder() * @method static void setFinder(\Illuminate\View\ViewFinderInterface $finder) * @method static void flushFinderCache() * @method static \Illuminate\Contracts\Events\Dispatcher getDispatcher() * @method static void setDispatcher(\Illuminate\Contracts\Events\Dispatcher $events) * @method static \Illuminate\Contracts\Container\Container getContainer() * @method static void setContainer(\Illuminate\Contracts\Container\Container $container) * @method static mixed shared(string $key, mixed $default = null) * @method static array getShared() * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * @method static void startComponent(\Illuminate\Contracts\View\View|\Illuminate\Contracts\Support\Htmlable|\Closure|string $view, array $data = []) * @method static void startComponentFirst(array $names, array $data = []) * @method static string renderComponent() * @method static mixed|null getConsumableComponentData(string $key, mixed $default = null) * @method static void slot(string $name, string|null $content = null, array $attributes = []) * @method static void endSlot() * @method static array creator(array|string $views, \Closure|string $callback) * @method static array composers(array $composers) * @method static array composer(array|string $views, \Closure|string $callback) * @method static void callComposer(\Illuminate\Contracts\View\View $view) * @method static void callCreator(\Illuminate\Contracts\View\View $view) * @method static void startFragment(string $fragment) * @method static string stopFragment() * @method static mixed getFragment(string $name, string|null $default = null) * @method static array getFragments() * @method static void flushFragments() * @method static void startSection(string $section, string|null $content = null) * @method static void inject(string $section, string $content) * @method static string yieldSection() * @method static string stopSection(bool $overwrite = false) * @method static string appendSection() * @method static string yieldContent(string $section, string $default = '') * @method static string parentPlaceholder(string $section = '') * @method static bool hasSection(string $name) * @method static bool sectionMissing(string $name) * @method static mixed getSection(string $name, string|null $default = null) * @method static array getSections() * @method static void flushSections() * @method static void addLoop(\Countable|array $data) * @method static void incrementLoopIndices() * @method static void popLoop() * @method static \stdClass|null getLastLoop() * @method static array getLoopStack() * @method static void startPush(string $section, string $content = '') * @method static string stopPush() * @method static void startPrepend(string $section, string $content = '') * @method static string stopPrepend() * @method static string yieldPushContent(string $section, string $default = '') * @method static void flushStacks() * @method static void startTranslation(array $replacements = []) * @method static string renderTranslation() * * @see \Illuminate\View\Factory */ class View extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'view'; } } src/Illuminate/Support/Facades/Http.php 0000644 00000021540 15107327040 0014071 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Http\Client\Factory; /** * @method static \Illuminate\Http\Client\Factory globalMiddleware(callable $middleware) * @method static \Illuminate\Http\Client\Factory globalRequestMiddleware(callable $middleware) * @method static \Illuminate\Http\Client\Factory globalResponseMiddleware(callable $middleware) * @method static \GuzzleHttp\Promise\PromiseInterface response(array|string|null $body = null, int $status = 200, array $headers = []) * @method static \Illuminate\Http\Client\ResponseSequence sequence(array $responses = []) * @method static \Illuminate\Http\Client\Factory allowStrayRequests() * @method static void recordRequestResponsePair(\Illuminate\Http\Client\Request $request, \Illuminate\Http\Client\Response $response) * @method static void assertSent(callable $callback) * @method static void assertSentInOrder(array $callbacks) * @method static void assertNotSent(callable $callback) * @method static void assertNothingSent() * @method static void assertSentCount(int $count) * @method static void assertSequencesAreEmpty() * @method static \Illuminate\Support\Collection recorded(callable $callback = null) * @method static \Illuminate\Contracts\Events\Dispatcher|null getDispatcher() * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * @method static mixed macroCall(string $method, array $parameters) * @method static \Illuminate\Http\Client\PendingRequest baseUrl(string $url) * @method static \Illuminate\Http\Client\PendingRequest withBody(string $content, string $contentType = 'application/json') * @method static \Illuminate\Http\Client\PendingRequest asJson() * @method static \Illuminate\Http\Client\PendingRequest asForm() * @method static \Illuminate\Http\Client\PendingRequest attach(string|array $name, string|resource $contents = '', string|null $filename = null, array $headers = []) * @method static \Illuminate\Http\Client\PendingRequest asMultipart() * @method static \Illuminate\Http\Client\PendingRequest bodyFormat(string $format) * @method static \Illuminate\Http\Client\PendingRequest withQueryParameters(array $parameters) * @method static \Illuminate\Http\Client\PendingRequest contentType(string $contentType) * @method static \Illuminate\Http\Client\PendingRequest acceptJson() * @method static \Illuminate\Http\Client\PendingRequest accept(string $contentType) * @method static \Illuminate\Http\Client\PendingRequest withHeaders(array $headers) * @method static \Illuminate\Http\Client\PendingRequest withHeader(string $name, mixed $value) * @method static \Illuminate\Http\Client\PendingRequest replaceHeaders(array $headers) * @method static \Illuminate\Http\Client\PendingRequest withBasicAuth(string $username, string $password) * @method static \Illuminate\Http\Client\PendingRequest withDigestAuth(string $username, string $password) * @method static \Illuminate\Http\Client\PendingRequest withToken(string $token, string $type = 'Bearer') * @method static \Illuminate\Http\Client\PendingRequest withUserAgent(string|bool $userAgent) * @method static \Illuminate\Http\Client\PendingRequest withUrlParameters(array $parameters = []) * @method static \Illuminate\Http\Client\PendingRequest withCookies(array $cookies, string $domain) * @method static \Illuminate\Http\Client\PendingRequest maxRedirects(int $max) * @method static \Illuminate\Http\Client\PendingRequest withoutRedirecting() * @method static \Illuminate\Http\Client\PendingRequest withoutVerifying() * @method static \Illuminate\Http\Client\PendingRequest sink(string|resource $to) * @method static \Illuminate\Http\Client\PendingRequest timeout(int $seconds) * @method static \Illuminate\Http\Client\PendingRequest connectTimeout(int $seconds) * @method static \Illuminate\Http\Client\PendingRequest retry(int $times, \Closure|int $sleepMilliseconds = 0, callable|null $when = null, bool $throw = true) * @method static \Illuminate\Http\Client\PendingRequest withOptions(array $options) * @method static \Illuminate\Http\Client\PendingRequest withMiddleware(callable $middleware) * @method static \Illuminate\Http\Client\PendingRequest withRequestMiddleware(callable $middleware) * @method static \Illuminate\Http\Client\PendingRequest withResponseMiddleware(callable $middleware) * @method static \Illuminate\Http\Client\PendingRequest beforeSending(callable $callback) * @method static \Illuminate\Http\Client\PendingRequest throw(callable|null $callback = null) * @method static \Illuminate\Http\Client\PendingRequest throwIf(callable|bool $condition, callable|null $throwCallback = null) * @method static \Illuminate\Http\Client\PendingRequest throwUnless(bool $condition) * @method static \Illuminate\Http\Client\PendingRequest dump() * @method static \Illuminate\Http\Client\PendingRequest dd() * @method static \Illuminate\Http\Client\Response get(string $url, array|string|null $query = null) * @method static \Illuminate\Http\Client\Response head(string $url, array|string|null $query = null) * @method static \Illuminate\Http\Client\Response post(string $url, array $data = []) * @method static \Illuminate\Http\Client\Response patch(string $url, array $data = []) * @method static \Illuminate\Http\Client\Response put(string $url, array $data = []) * @method static \Illuminate\Http\Client\Response delete(string $url, array $data = []) * @method static array pool(callable $callback) * @method static \Illuminate\Http\Client\Response send(string $method, string $url, array $options = []) * @method static \GuzzleHttp\Client buildClient() * @method static \GuzzleHttp\Client createClient(\GuzzleHttp\HandlerStack $handlerStack) * @method static \GuzzleHttp\HandlerStack buildHandlerStack() * @method static \GuzzleHttp\HandlerStack pushHandlers(\GuzzleHttp\HandlerStack $handlerStack) * @method static \Closure buildBeforeSendingHandler() * @method static \Closure buildRecorderHandler() * @method static \Closure buildStubHandler() * @method static \GuzzleHttp\Psr7\RequestInterface runBeforeSendingCallbacks(\GuzzleHttp\Psr7\RequestInterface $request, array $options) * @method static array mergeOptions(array ...$options) * @method static \Illuminate\Http\Client\PendingRequest stub(callable $callback) * @method static \Illuminate\Http\Client\PendingRequest async(bool $async = true) * @method static \GuzzleHttp\Promise\PromiseInterface|null getPromise() * @method static \Illuminate\Http\Client\PendingRequest setClient(\GuzzleHttp\Client $client) * @method static \Illuminate\Http\Client\PendingRequest setHandler(callable $handler) * @method static array getOptions() * @method static \Illuminate\Http\Client\PendingRequest|mixed when(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null) * @method static \Illuminate\Http\Client\PendingRequest|mixed unless(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null) * * @see \Illuminate\Http\Client\Factory */ class Http extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return Factory::class; } /** * Register a stub callable that will intercept requests and be able to return stub responses. * * @param \Closure|array $callback * @return \Illuminate\Http\Client\Factory */ public static function fake($callback = null) { return tap(static::getFacadeRoot(), function ($fake) use ($callback) { static::swap($fake->fake($callback)); }); } /** * Register a response sequence for the given URL pattern. * * @param string $urlPattern * @return \Illuminate\Http\Client\ResponseSequence */ public static function fakeSequence(string $urlPattern = '*') { $fake = tap(static::getFacadeRoot(), function ($fake) { static::swap($fake); }); return $fake->fakeSequence($urlPattern); } /** * Indicate that an exception should be thrown if any request is not faked. * * @return \Illuminate\Http\Client\Factory */ public static function preventStrayRequests() { return tap(static::getFacadeRoot(), function ($fake) { static::swap($fake->preventStrayRequests()); }); } /** * Stub the given URL using the given callback. * * @param string $url * @param \Illuminate\Http\Client\Response|\GuzzleHttp\Promise\PromiseInterface|callable $callback * @return \Illuminate\Http\Client\Factory */ public static function stubUrl($url, $callback) { return tap(static::getFacadeRoot(), function ($fake) use ($url, $callback) { static::swap($fake->stubUrl($url, $callback)); }); } } src/Illuminate/Support/Facades/File.php 0000644 00000007441 15107327040 0014035 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static bool exists(string $path) * @method static bool missing(string $path) * @method static string get(string $path, bool $lock = false) * @method static array json(string $path, int $flags = 0, bool $lock = false) * @method static string sharedGet(string $path) * @method static mixed getRequire(string $path, array $data = []) * @method static mixed requireOnce(string $path, array $data = []) * @method static \Illuminate\Support\LazyCollection lines(string $path) * @method static string hash(string $path, string $algorithm = 'md5') * @method static int|bool put(string $path, string $contents, bool $lock = false) * @method static void replace(string $path, string $content, int|null $mode = null) * @method static void replaceInFile(array|string $search, array|string $replace, string $path) * @method static int prepend(string $path, string $data) * @method static int append(string $path, string $data) * @method static mixed chmod(string $path, int|null $mode = null) * @method static bool delete(string|array $paths) * @method static bool move(string $path, string $target) * @method static bool copy(string $path, string $target) * @method static void link(string $target, string $link) * @method static void relativeLink(string $target, string $link) * @method static string name(string $path) * @method static string basename(string $path) * @method static string dirname(string $path) * @method static string extension(string $path) * @method static string|null guessExtension(string $path) * @method static string type(string $path) * @method static string|false mimeType(string $path) * @method static int size(string $path) * @method static int lastModified(string $path) * @method static bool isDirectory(string $directory) * @method static bool isEmptyDirectory(string $directory, bool $ignoreDotFiles = false) * @method static bool isReadable(string $path) * @method static bool isWritable(string $path) * @method static bool hasSameHash(string $firstFile, string $secondFile) * @method static bool isFile(string $file) * @method static array glob(string $pattern, int $flags = 0) * @method static \Symfony\Component\Finder\SplFileInfo[] files(string $directory, bool $hidden = false) * @method static \Symfony\Component\Finder\SplFileInfo[] allFiles(string $directory, bool $hidden = false) * @method static array directories(string $directory) * @method static void ensureDirectoryExists(string $path, int $mode = 0755, bool $recursive = true) * @method static bool makeDirectory(string $path, int $mode = 0755, bool $recursive = false, bool $force = false) * @method static bool moveDirectory(string $from, string $to, bool $overwrite = false) * @method static bool copyDirectory(string $directory, string $destination, int|null $options = null) * @method static bool deleteDirectory(string $directory, bool $preserve = false) * @method static bool deleteDirectories(string $directory) * @method static bool cleanDirectory(string $directory) * @method static \Illuminate\Filesystem\Filesystem|mixed when(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null) * @method static \Illuminate\Filesystem\Filesystem|mixed unless(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null) * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * * @see \Illuminate\Filesystem\Filesystem */ class File extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'files'; } } src/Illuminate/Support/Facades/Process.php 0000644 00000010044 15107327040 0014565 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Closure; use Illuminate\Process\Factory; /** * @method static \Illuminate\Process\PendingProcess command(array|string $command) * @method static \Illuminate\Process\PendingProcess path(string $path) * @method static \Illuminate\Process\PendingProcess timeout(int $timeout) * @method static \Illuminate\Process\PendingProcess idleTimeout(int $timeout) * @method static \Illuminate\Process\PendingProcess forever() * @method static \Illuminate\Process\PendingProcess env(array $environment) * @method static \Illuminate\Process\PendingProcess input(\Traversable|resource|string|int|float|bool|null $input) * @method static \Illuminate\Process\PendingProcess quietly() * @method static \Illuminate\Process\PendingProcess tty(bool $tty = true) * @method static \Illuminate\Process\PendingProcess options(array $options) * @method static \Illuminate\Contracts\Process\ProcessResult run(array|string|null $command = null, callable|null $output = null) * @method static \Illuminate\Process\InvokedProcess start(array|string|null $command = null, callable $output = null) * @method static \Illuminate\Process\PendingProcess withFakeHandlers(array $fakeHandlers) * @method static \Illuminate\Process\PendingProcess|mixed when(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null) * @method static \Illuminate\Process\PendingProcess|mixed unless(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null) * @method static \Illuminate\Process\FakeProcessResult result(array|string $output = '', array|string $errorOutput = '', int $exitCode = 0) * @method static \Illuminate\Process\FakeProcessDescription describe() * @method static \Illuminate\Process\FakeProcessSequence sequence(array $processes = []) * @method static bool isRecording() * @method static \Illuminate\Process\Factory recordIfRecording(\Illuminate\Process\PendingProcess $process, \Illuminate\Contracts\Process\ProcessResult $result) * @method static \Illuminate\Process\Factory record(\Illuminate\Process\PendingProcess $process, \Illuminate\Contracts\Process\ProcessResult $result) * @method static \Illuminate\Process\Factory preventStrayProcesses(bool $prevent = true) * @method static bool preventingStrayProcesses() * @method static \Illuminate\Process\Factory assertRan(\Closure|string $callback) * @method static \Illuminate\Process\Factory assertRanTimes(\Closure|string $callback, int $times = 1) * @method static \Illuminate\Process\Factory assertNotRan(\Closure|string $callback) * @method static \Illuminate\Process\Factory assertDidntRun(\Closure|string $callback) * @method static \Illuminate\Process\Factory assertNothingRan() * @method static \Illuminate\Process\Pool pool(callable $callback) * @method static \Illuminate\Contracts\Process\ProcessResult pipe(callable|array $callback, callable|null $output = null) * @method static \Illuminate\Process\ProcessPoolResults concurrently(callable $callback, callable|null $output = null) * @method static \Illuminate\Process\PendingProcess newPendingProcess() * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * @method static mixed macroCall(string $method, array $parameters) * * @see \Illuminate\Process\PendingProcess * @see \Illuminate\Process\Factory */ class Process extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return Factory::class; } /** * Indicate that the process factory should fake processes. * * @param \Closure|array|null $callback * @return \Illuminate\Process\Factory */ public static function fake(Closure|array $callback = null) { return tap(static::getFacadeRoot(), function ($fake) use ($callback) { static::swap($fake->fake($callback)); }); } } src/Illuminate/Support/Facades/Artisan.php 0000644 00000003034 15107327040 0014551 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract; /** * @method static int handle(\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface|null $output = null) * @method static void terminate(\Symfony\Component\Console\Input\InputInterface $input, int $status) * @method static void whenCommandLifecycleIsLongerThan(\DateTimeInterface|\Carbon\CarbonInterval|float|int $threshold, callable $handler) * @method static \Illuminate\Support\Carbon|null commandStartedAt() * @method static \Illuminate\Foundation\Console\ClosureCommand command(string $signature, \Closure $callback) * @method static void registerCommand(\Symfony\Component\Console\Command\Command $command) * @method static int call(string $command, array $parameters = [], \Symfony\Component\Console\Output\OutputInterface|null $outputBuffer = null) * @method static \Illuminate\Foundation\Bus\PendingDispatch queue(string $command, array $parameters = []) * @method static array all() * @method static string output() * @method static void bootstrap() * @method static void bootstrapWithoutBootingProviders() * @method static void setArtisan(\Illuminate\Console\Application|null $artisan) * * @see \Illuminate\Foundation\Console\Kernel */ class Artisan extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return ConsoleKernelContract::class; } } src/Illuminate/Support/Facades/Crypt.php 0000644 00000001316 15107327040 0014252 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static bool supported(string $key, string $cipher) * @method static string generateKey(string $cipher) * @method static string encrypt(mixed $value, bool $serialize = true) * @method static string encryptString(string $value) * @method static mixed decrypt(string $payload, bool $unserialize = true) * @method static string decryptString(string $payload) * @method static string getKey() * * @see \Illuminate\Encryption\Encrypter */ class Crypt extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'encrypter'; } } src/Illuminate/Support/Facades/Cookie.php 0000644 00000004621 15107327040 0014364 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static \Symfony\Component\HttpFoundation\Cookie make(string $name, string $value, int $minutes = 0, string|null $path = null, string|null $domain = null, bool|null $secure = null, bool $httpOnly = true, bool $raw = false, string|null $sameSite = null) * @method static \Symfony\Component\HttpFoundation\Cookie forever(string $name, string $value, string|null $path = null, string|null $domain = null, bool|null $secure = null, bool $httpOnly = true, bool $raw = false, string|null $sameSite = null) * @method static \Symfony\Component\HttpFoundation\Cookie forget(string $name, string|null $path = null, string|null $domain = null) * @method static bool hasQueued(string $key, string|null $path = null) * @method static \Symfony\Component\HttpFoundation\Cookie|null queued(string $key, mixed $default = null, string|null $path = null) * @method static void queue(mixed ...$parameters) * @method static void expire(string $name, string|null $path = null, string|null $domain = null) * @method static void unqueue(string $name, string|null $path = null) * @method static \Illuminate\Cookie\CookieJar setDefaultPathAndDomain(string $path, string|null $domain, bool|null $secure = false, string|null $sameSite = null) * @method static \Symfony\Component\HttpFoundation\Cookie[] getQueuedCookies() * @method static \Illuminate\Cookie\CookieJar flushQueuedCookies() * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * * @see \Illuminate\Cookie\CookieJar */ class Cookie extends Facade { /** * Determine if a cookie exists on the request. * * @param string $key * @return bool */ public static function has($key) { return ! is_null(static::$app['request']->cookie($key, null)); } /** * Retrieve a cookie from the request. * * @param string|null $key * @param mixed $default * @return string|array|null */ public static function get($key = null, $default = null) { return static::$app['request']->cookie($key, $default); } /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'cookie'; } } src/Illuminate/Support/Facades/Request.php 0000644 00000025174 15107327040 0014611 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static \Illuminate\Http\Request capture() * @method static \Illuminate\Http\Request instance() * @method static string method() * @method static string root() * @method static string url() * @method static string fullUrl() * @method static string fullUrlWithQuery(array $query) * @method static string fullUrlWithoutQuery(array|string $keys) * @method static string path() * @method static string decodedPath() * @method static string|null segment(int $index, string|null $default = null) * @method static array segments() * @method static bool is(mixed ...$patterns) * @method static bool routeIs(mixed ...$patterns) * @method static bool fullUrlIs(mixed ...$patterns) * @method static string host() * @method static string httpHost() * @method static string schemeAndHttpHost() * @method static bool ajax() * @method static bool pjax() * @method static bool prefetch() * @method static bool secure() * @method static string|null ip() * @method static array ips() * @method static string|null userAgent() * @method static \Illuminate\Http\Request merge(array $input) * @method static \Illuminate\Http\Request mergeIfMissing(array $input) * @method static \Illuminate\Http\Request replace(array $input) * @method static mixed get(string $key, mixed $default = null) * @method static \Symfony\Component\HttpFoundation\InputBag|mixed json(string|null $key = null, mixed $default = null) * @method static \Illuminate\Http\Request createFrom(\Illuminate\Http\Request $from, \Illuminate\Http\Request|null $to = null) * @method static \Illuminate\Http\Request createFromBase(\Symfony\Component\HttpFoundation\Request $request) * @method static \Illuminate\Http\Request duplicate(array|null $query = null, array|null $request = null, array|null $attributes = null, array|null $cookies = null, array|null $files = null, array|null $server = null) * @method static bool hasSession(bool $skipIfUninitialized = false) * @method static \Symfony\Component\HttpFoundation\Session\SessionInterface getSession() * @method static \Illuminate\Contracts\Session\Session session() * @method static void setLaravelSession(\Illuminate\Contracts\Session\Session $session) * @method static void setRequestLocale(string $locale) * @method static void setDefaultRequestLocale(string $locale) * @method static mixed user(string|null $guard = null) * @method static \Illuminate\Routing\Route|object|string|null route(string|null $param = null, mixed $default = null) * @method static string fingerprint() * @method static \Illuminate\Http\Request setJson(\Symfony\Component\HttpFoundation\InputBag $json) * @method static \Closure getUserResolver() * @method static \Illuminate\Http\Request setUserResolver(\Closure $callback) * @method static \Closure getRouteResolver() * @method static \Illuminate\Http\Request setRouteResolver(\Closure $callback) * @method static array toArray() * @method static void initialize(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], string|resource|null $content = null) * @method static \Illuminate\Http\Request createFromGlobals() * @method static \Illuminate\Http\Request create(string $uri, string $method = 'GET', array $parameters = [], array $cookies = [], array $files = [], array $server = [], string|resource|null $content = null) * @method static void setFactory(callable|null $callable) * @method static void overrideGlobals() * @method static void setTrustedProxies(array $proxies, int $trustedHeaderSet) * @method static string[] getTrustedProxies() * @method static int getTrustedHeaderSet() * @method static void setTrustedHosts(array $hostPatterns) * @method static string[] getTrustedHosts() * @method static string normalizeQueryString(string|null $qs) * @method static void enableHttpMethodParameterOverride() * @method static bool getHttpMethodParameterOverride() * @method static bool hasPreviousSession() * @method static void setSession(\Symfony\Component\HttpFoundation\Session\SessionInterface $session) * @method static array getClientIps() * @method static string|null getClientIp() * @method static string getScriptName() * @method static string getPathInfo() * @method static string getBasePath() * @method static string getBaseUrl() * @method static string getScheme() * @method static int|string|null getPort() * @method static string|null getUser() * @method static string|null getPassword() * @method static string|null getUserInfo() * @method static string getHttpHost() * @method static string getRequestUri() * @method static string getSchemeAndHttpHost() * @method static string getUri() * @method static string getUriForPath(string $path) * @method static string getRelativeUriForPath(string $path) * @method static string|null getQueryString() * @method static bool isSecure() * @method static string getHost() * @method static void setMethod(string $method) * @method static string getMethod() * @method static string getRealMethod() * @method static string|null getMimeType(string $format) * @method static string[] getMimeTypes(string $format) * @method static string|null getFormat(string|null $mimeType) * @method static void setFormat(string|null $format, string|string[] $mimeTypes) * @method static string|null getRequestFormat(string|null $default = 'html') * @method static void setRequestFormat(string|null $format) * @method static string|null getContentTypeFormat() * @method static void setDefaultLocale(string $locale) * @method static string getDefaultLocale() * @method static void setLocale(string $locale) * @method static string getLocale() * @method static bool isMethod(string $method) * @method static bool isMethodSafe() * @method static bool isMethodIdempotent() * @method static bool isMethodCacheable() * @method static string|null getProtocolVersion() * @method static string|resource getContent(bool $asResource = false) * @method static \Symfony\Component\HttpFoundation\InputBag getPayload() * @method static array getETags() * @method static bool isNoCache() * @method static string|null getPreferredFormat(string|null $default = 'html') * @method static string|null getPreferredLanguage(string[] $locales = null) * @method static string[] getLanguages() * @method static string[] getCharsets() * @method static string[] getEncodings() * @method static string[] getAcceptableContentTypes() * @method static bool isXmlHttpRequest() * @method static bool preferSafeContent() * @method static bool isFromTrustedProxy() * @method static array filterPrecognitiveRules(array $rules) * @method static bool isAttemptingPrecognition() * @method static bool isPrecognitive() * @method static bool isJson() * @method static bool expectsJson() * @method static bool wantsJson() * @method static bool accepts(string|array $contentTypes) * @method static string|null prefers(string|array $contentTypes) * @method static bool acceptsAnyContentType() * @method static bool acceptsJson() * @method static bool acceptsHtml() * @method static bool matchesType(string $actual, string $type) * @method static string format(string $default = 'html') * @method static string|array|null old(string|null $key = null, \Illuminate\Database\Eloquent\Model|string|array|null $default = null) * @method static void flash() * @method static void flashOnly(array|mixed $keys) * @method static void flashExcept(array|mixed $keys) * @method static void flush() * @method static string|array|null server(string|null $key = null, string|array|null $default = null) * @method static bool hasHeader(string $key) * @method static string|array|null header(string|null $key = null, string|array|null $default = null) * @method static string|null bearerToken() * @method static bool exists(string|array $key) * @method static bool has(string|array $key) * @method static bool hasAny(string|array $keys) * @method static \Illuminate\Http\Request|mixed whenHas(string $key, callable $callback, callable|null $default = null) * @method static bool filled(string|array $key) * @method static bool isNotFilled(string|array $key) * @method static bool anyFilled(string|array $keys) * @method static \Illuminate\Http\Request|mixed whenFilled(string $key, callable $callback, callable|null $default = null) * @method static bool missing(string|array $key) * @method static \Illuminate\Http\Request|mixed whenMissing(string $key, callable $callback, callable|null $default = null) * @method static array keys() * @method static array all(array|mixed|null $keys = null) * @method static mixed input(string|null $key = null, mixed $default = null) * @method static \Illuminate\Support\Stringable str(string $key, mixed $default = null) * @method static \Illuminate\Support\Stringable string(string $key, mixed $default = null) * @method static bool boolean(string|null $key = null, bool $default = false) * @method static int integer(string $key, int $default = 0) * @method static float float(string $key, float $default = 0) * @method static \Illuminate\Support\Carbon|null date(string $key, string|null $format = null, string|null $tz = null) * @method static object|null enum(string $key, string $enumClass) * @method static \Illuminate\Support\Collection collect(array|string|null $key = null) * @method static array only(array|mixed $keys) * @method static array except(array|mixed $keys) * @method static string|array|null query(string|null $key = null, string|array|null $default = null) * @method static string|array|null post(string|null $key = null, string|array|null $default = null) * @method static bool hasCookie(string $key) * @method static string|array|null cookie(string|null $key = null, string|array|null $default = null) * @method static array allFiles() * @method static bool hasFile(string $key) * @method static \Illuminate\Http\UploadedFile|\Illuminate\Http\UploadedFile[]|array|null file(string|null $key = null, mixed $default = null) * @method static never dd(mixed ...$keys) * @method static \Illuminate\Http\Request dump(mixed $keys = []) * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * @method static array validate(array $rules, ...$params) * @method static array validateWithBag(string $errorBag, array $rules, ...$params) * @method static bool hasValidSignature(bool $absolute = true) * * @see \Illuminate\Http\Request */ class Request extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'request'; } } src/Illuminate/Support/Facades/Bus.php 0000644 00000011170 15107327040 0013701 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Bus\BatchRepository; use Illuminate\Contracts\Bus\Dispatcher as BusDispatcherContract; use Illuminate\Foundation\Bus\PendingChain; use Illuminate\Support\Testing\Fakes\BusFake; /** * @method static mixed dispatch(mixed $command) * @method static mixed dispatchSync(mixed $command, mixed $handler = null) * @method static mixed dispatchNow(mixed $command, mixed $handler = null) * @method static \Illuminate\Bus\Batch|null findBatch(string $batchId) * @method static \Illuminate\Bus\PendingBatch batch(\Illuminate\Support\Collection|array|mixed $jobs) * @method static \Illuminate\Foundation\Bus\PendingChain chain(\Illuminate\Support\Collection|array $jobs) * @method static bool hasCommandHandler(mixed $command) * @method static bool|mixed getCommandHandler(mixed $command) * @method static mixed dispatchToQueue(mixed $command) * @method static void dispatchAfterResponse(mixed $command, mixed $handler = null) * @method static \Illuminate\Bus\Dispatcher pipeThrough(array $pipes) * @method static \Illuminate\Bus\Dispatcher map(array $map) * @method static \Illuminate\Support\Testing\Fakes\BusFake except(array|string $jobsToDispatch) * @method static void assertDispatched(string|\Closure $command, callable|int|null $callback = null) * @method static void assertDispatchedTimes(string|\Closure $command, int $times = 1) * @method static void assertNotDispatched(string|\Closure $command, callable|null $callback = null) * @method static void assertNothingDispatched() * @method static void assertDispatchedSync(string|\Closure $command, callable|int|null $callback = null) * @method static void assertDispatchedSyncTimes(string|\Closure $command, int $times = 1) * @method static void assertNotDispatchedSync(string|\Closure $command, callable|null $callback = null) * @method static void assertDispatchedAfterResponse(string|\Closure $command, callable|int|null $callback = null) * @method static void assertDispatchedAfterResponseTimes(string|\Closure $command, int $times = 1) * @method static void assertNotDispatchedAfterResponse(string|\Closure $command, callable|null $callback = null) * @method static void assertChained(array $expectedChain) * @method static void assertDispatchedWithoutChain(string|\Closure $command, callable|null $callback = null) * @method static void assertBatched(callable $callback) * @method static void assertBatchCount(int $count) * @method static void assertNothingBatched() * @method static \Illuminate\Support\Collection dispatched(string $command, callable|null $callback = null) * @method static \Illuminate\Support\Collection dispatchedSync(string $command, callable|null $callback = null) * @method static \Illuminate\Support\Collection dispatchedAfterResponse(string $command, callable|null $callback = null) * @method static \Illuminate\Support\Collection batched(callable $callback) * @method static bool hasDispatched(string $command) * @method static bool hasDispatchedSync(string $command) * @method static bool hasDispatchedAfterResponse(string $command) * @method static \Illuminate\Bus\Batch dispatchFakeBatch(string $name = '') * @method static \Illuminate\Bus\Batch recordPendingBatch(\Illuminate\Bus\PendingBatch $pendingBatch) * @method static \Illuminate\Support\Testing\Fakes\BusFake serializeAndRestore(bool $serializeAndRestore = true) * * @see \Illuminate\Bus\Dispatcher * @see \Illuminate\Support\Testing\Fakes\BusFake */ class Bus extends Facade { /** * Replace the bound instance with a fake. * * @param array|string $jobsToFake * @param \Illuminate\Bus\BatchRepository|null $batchRepository * @return \Illuminate\Support\Testing\Fakes\BusFake */ public static function fake($jobsToFake = [], BatchRepository $batchRepository = null) { $actualDispatcher = static::isFake() ? static::getFacadeRoot()->dispatcher : static::getFacadeRoot(); return tap(new BusFake($actualDispatcher, $jobsToFake, $batchRepository), function ($fake) { static::swap($fake); }); } /** * Dispatch the given chain of jobs. * * @param array|mixed $jobs * @return \Illuminate\Foundation\Bus\PendingDispatch */ public static function dispatchChain($jobs) { $jobs = is_array($jobs) ? $jobs : func_get_args(); return (new PendingChain(array_shift($jobs), $jobs)) ->dispatch(); } /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return BusDispatcherContract::class; } } src/Illuminate/Support/Facades/Log.php 0000644 00000005101 15107327040 0013666 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static \Psr\Log\LoggerInterface build(array $config) * @method static \Psr\Log\LoggerInterface stack(array $channels, string|null $channel = null) * @method static \Psr\Log\LoggerInterface channel(string|null $channel = null) * @method static \Psr\Log\LoggerInterface driver(string|null $driver = null) * @method static \Illuminate\Log\LogManager shareContext(array $context) * @method static array sharedContext() * @method static \Illuminate\Log\LogManager flushSharedContext() * @method static string|null getDefaultDriver() * @method static void setDefaultDriver(string $name) * @method static \Illuminate\Log\LogManager extend(string $driver, \Closure $callback) * @method static void forgetChannel(string|null $driver = null) * @method static array getChannels() * @method static void emergency(string $message, array $context = []) * @method static void alert(string $message, array $context = []) * @method static void critical(string $message, array $context = []) * @method static void error(string $message, array $context = []) * @method static void warning(string $message, array $context = []) * @method static void notice(string $message, array $context = []) * @method static void info(string $message, array $context = []) * @method static void debug(string $message, array $context = []) * @method static void log(mixed $level, string $message, array $context = []) * @method static void write(string $level, \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message, array $context = []) * @method static \Illuminate\Log\Logger withContext(array $context = []) * @method static \Illuminate\Log\Logger withoutContext() * @method static void listen(\Closure $callback) * @method static \Psr\Log\LoggerInterface getLogger() * @method static \Illuminate\Contracts\Events\Dispatcher getEventDispatcher() * @method static void setEventDispatcher(\Illuminate\Contracts\Events\Dispatcher $dispatcher) * @method static \Illuminate\Log\Logger|mixed when(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null) * @method static \Illuminate\Log\Logger|mixed unless(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null) * * @see \Illuminate\Log\LogManager */ class Log extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'log'; } } src/Illuminate/Support/Facades/Password.php 0000644 00000003755 15107327040 0014764 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Contracts\Auth\PasswordBroker; /** * @method static \Illuminate\Contracts\Auth\PasswordBroker broker(string|null $name = null) * @method static string getDefaultDriver() * @method static void setDefaultDriver(string $name) * @method static string sendResetLink(array $credentials, \Closure|null $callback = null) * @method static mixed reset(array $credentials, \Closure $callback) * @method static \Illuminate\Contracts\Auth\CanResetPassword|null getUser(array $credentials) * @method static string createToken(\Illuminate\Contracts\Auth\CanResetPassword $user) * @method static void deleteToken(\Illuminate\Contracts\Auth\CanResetPassword $user) * @method static bool tokenExists(\Illuminate\Contracts\Auth\CanResetPassword $user, string $token) * @method static \Illuminate\Auth\Passwords\TokenRepositoryInterface getRepository() * * @see \Illuminate\Auth\Passwords\PasswordBrokerManager * @see \Illuminate\Auth\Passwords\PasswordBroker */ class Password extends Facade { /** * Constant representing a successfully sent reminder. * * @var string */ const RESET_LINK_SENT = PasswordBroker::RESET_LINK_SENT; /** * Constant representing a successfully reset password. * * @var string */ const PASSWORD_RESET = PasswordBroker::PASSWORD_RESET; /** * Constant representing the user not found response. * * @var string */ const INVALID_USER = PasswordBroker::INVALID_USER; /** * Constant representing an invalid token. * * @var string */ const INVALID_TOKEN = PasswordBroker::INVALID_TOKEN; /** * Constant representing a throttled reset attempt. * * @var string */ const RESET_THROTTLED = PasswordBroker::RESET_THROTTLED; /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'auth.password'; } } src/Illuminate/Support/Facades/App.php 0000644 00000020475 15107327040 0013700 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static string version() * @method static void bootstrapWith(string[] $bootstrappers) * @method static void afterLoadingEnvironment(\Closure $callback) * @method static void beforeBootstrapping(string $bootstrapper, \Closure $callback) * @method static void afterBootstrapping(string $bootstrapper, \Closure $callback) * @method static bool hasBeenBootstrapped() * @method static \Illuminate\Foundation\Application setBasePath(string $basePath) * @method static string path(string $path = '') * @method static \Illuminate\Foundation\Application useAppPath(string $path) * @method static string basePath(string $path = '') * @method static string bootstrapPath(string $path = '') * @method static \Illuminate\Foundation\Application useBootstrapPath(string $path) * @method static string configPath(string $path = '') * @method static \Illuminate\Foundation\Application useConfigPath(string $path) * @method static string databasePath(string $path = '') * @method static \Illuminate\Foundation\Application useDatabasePath(string $path) * @method static string langPath(string $path = '') * @method static \Illuminate\Foundation\Application useLangPath(string $path) * @method static string publicPath(string $path = '') * @method static \Illuminate\Foundation\Application usePublicPath(string $path) * @method static string storagePath(string $path = '') * @method static \Illuminate\Foundation\Application useStoragePath(string $path) * @method static string resourcePath(string $path = '') * @method static string viewPath(string $path = '') * @method static string joinPaths(string $basePath, string $path = '') * @method static string environmentPath() * @method static \Illuminate\Foundation\Application useEnvironmentPath(string $path) * @method static \Illuminate\Foundation\Application loadEnvironmentFrom(string $file) * @method static string environmentFile() * @method static string environmentFilePath() * @method static string|bool environment(string|array ...$environments) * @method static bool isLocal() * @method static bool isProduction() * @method static string detectEnvironment(\Closure $callback) * @method static bool runningInConsole() * @method static bool runningConsoleCommand(string|array ...$commands) * @method static bool runningUnitTests() * @method static bool hasDebugModeEnabled() * @method static void registerConfiguredProviders() * @method static \Illuminate\Support\ServiceProvider register(\Illuminate\Support\ServiceProvider|string $provider, bool $force = false) * @method static \Illuminate\Support\ServiceProvider|null getProvider(\Illuminate\Support\ServiceProvider|string $provider) * @method static array getProviders(\Illuminate\Support\ServiceProvider|string $provider) * @method static \Illuminate\Support\ServiceProvider resolveProvider(string $provider) * @method static void loadDeferredProviders() * @method static void loadDeferredProvider(string $service) * @method static void registerDeferredProvider(string $provider, string|null $service = null) * @method static mixed make(string $abstract, array $parameters = []) * @method static bool bound(string $abstract) * @method static bool isBooted() * @method static void boot() * @method static void booting(callable $callback) * @method static void booted(callable $callback) * @method static \Symfony\Component\HttpFoundation\Response handle(\Symfony\Component\HttpFoundation\Request $request, int $type = 1, bool $catch = true) * @method static bool shouldSkipMiddleware() * @method static string getCachedServicesPath() * @method static string getCachedPackagesPath() * @method static bool configurationIsCached() * @method static string getCachedConfigPath() * @method static bool routesAreCached() * @method static string getCachedRoutesPath() * @method static bool eventsAreCached() * @method static string getCachedEventsPath() * @method static \Illuminate\Foundation\Application addAbsoluteCachePathPrefix(string $prefix) * @method static \Illuminate\Contracts\Foundation\MaintenanceMode maintenanceMode() * @method static bool isDownForMaintenance() * @method static never abort(int $code, string $message = '', array $headers = []) * @method static \Illuminate\Foundation\Application terminating(callable|string $callback) * @method static void terminate() * @method static array getLoadedProviders() * @method static bool providerIsLoaded(string $provider) * @method static array getDeferredServices() * @method static void setDeferredServices(array $services) * @method static void addDeferredServices(array $services) * @method static bool isDeferredService(string $service) * @method static void provideFacades(string $namespace) * @method static string getLocale() * @method static string currentLocale() * @method static string getFallbackLocale() * @method static void setLocale(string $locale) * @method static void setFallbackLocale(string $fallbackLocale) * @method static bool isLocale(string $locale) * @method static void registerCoreContainerAliases() * @method static void flush() * @method static string getNamespace() * @method static \Illuminate\Contracts\Container\ContextualBindingBuilder when(array|string $concrete) * @method static bool has(string $id) * @method static bool isShared(string $abstract) * @method static bool isAlias(string $name) * @method static void bind(string $abstract, \Closure|string|null $concrete = null, bool $shared = false) * @method static bool hasMethodBinding(string $method) * @method static void bindMethod(array|string $method, \Closure $callback) * @method static mixed callMethodBinding(string $method, mixed $instance) * @method static void addContextualBinding(string $concrete, string $abstract, \Closure|string $implementation) * @method static void bindIf(string $abstract, \Closure|string|null $concrete = null, bool $shared = false) * @method static void singleton(string $abstract, \Closure|string|null $concrete = null) * @method static void singletonIf(string $abstract, \Closure|string|null $concrete = null) * @method static void scoped(string $abstract, \Closure|string|null $concrete = null) * @method static void scopedIf(string $abstract, \Closure|string|null $concrete = null) * @method static void extend(string $abstract, \Closure $closure) * @method static mixed instance(string $abstract, mixed $instance) * @method static void tag(array|string $abstracts, array|mixed $tags) * @method static iterable tagged(string $tag) * @method static void alias(string $abstract, string $alias) * @method static mixed rebinding(string $abstract, \Closure $callback) * @method static mixed refresh(string $abstract, mixed $target, string $method) * @method static \Closure wrap(\Closure $callback, array $parameters = []) * @method static mixed call(callable|string $callback, array $parameters = [], string|null $defaultMethod = null) * @method static \Closure factory(string $abstract) * @method static mixed makeWith(string|callable $abstract, array $parameters = []) * @method static mixed get(string $id) * @method static mixed build(\Closure|string $concrete) * @method static void beforeResolving(\Closure|string $abstract, \Closure|null $callback = null) * @method static void resolving(\Closure|string $abstract, \Closure|null $callback = null) * @method static void afterResolving(\Closure|string $abstract, \Closure|null $callback = null) * @method static array getBindings() * @method static string getAlias(string $abstract) * @method static void forgetExtenders(string $abstract) * @method static void forgetInstance(string $abstract) * @method static void forgetInstances() * @method static void forgetScopedInstances() * @method static \Illuminate\Foundation\Application getInstance() * @method static \Illuminate\Contracts\Container\Container|\Illuminate\Foundation\Application setInstance(\Illuminate\Contracts\Container\Container|null $container = null) * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * * @see \Illuminate\Foundation\Application */ class App extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'app'; } } src/Illuminate/Support/Facades/DB.php 0000644 00000016404 15107327040 0013442 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static \Illuminate\Database\Connection connection(string|null $name = null) * @method static void registerDoctrineType(string $class, string $name, string $type) * @method static void purge(string|null $name = null) * @method static void disconnect(string|null $name = null) * @method static \Illuminate\Database\Connection reconnect(string|null $name = null) * @method static mixed usingConnection(string $name, callable $callback) * @method static string getDefaultConnection() * @method static void setDefaultConnection(string $name) * @method static string[] supportedDrivers() * @method static string[] availableDrivers() * @method static void extend(string $name, callable $resolver) * @method static void forgetExtension(string $name) * @method static array getConnections() * @method static void setReconnector(callable $reconnector) * @method static \Illuminate\Database\DatabaseManager setApplication(\Illuminate\Contracts\Foundation\Application $app) * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * @method static mixed macroCall(string $method, array $parameters) * @method static void useDefaultQueryGrammar() * @method static void useDefaultSchemaGrammar() * @method static void useDefaultPostProcessor() * @method static \Illuminate\Database\Schema\Builder getSchemaBuilder() * @method static \Illuminate\Database\Query\Builder table(\Closure|\Illuminate\Database\Query\Builder|\Illuminate\Contracts\Database\Query\Expression|string $table, string|null $as = null) * @method static \Illuminate\Database\Query\Builder query() * @method static mixed selectOne(string $query, array $bindings = [], bool $useReadPdo = true) * @method static mixed scalar(string $query, array $bindings = [], bool $useReadPdo = true) * @method static array selectFromWriteConnection(string $query, array $bindings = []) * @method static array select(string $query, array $bindings = [], bool $useReadPdo = true) * @method static array selectResultSets(string $query, array $bindings = [], bool $useReadPdo = true) * @method static \Generator cursor(string $query, array $bindings = [], bool $useReadPdo = true) * @method static bool insert(string $query, array $bindings = []) * @method static int update(string $query, array $bindings = []) * @method static int delete(string $query, array $bindings = []) * @method static bool statement(string $query, array $bindings = []) * @method static int affectingStatement(string $query, array $bindings = []) * @method static bool unprepared(string $query) * @method static array pretend(\Closure $callback) * @method static mixed withoutPretending(\Closure $callback) * @method static void bindValues(\PDOStatement $statement, array $bindings) * @method static array prepareBindings(array $bindings) * @method static void logQuery(string $query, array $bindings, float|null $time = null) * @method static void whenQueryingForLongerThan(\DateTimeInterface|\Carbon\CarbonInterval|float|int $threshold, callable $handler) * @method static void allowQueryDurationHandlersToRunAgain() * @method static float totalQueryDuration() * @method static void resetTotalQueryDuration() * @method static void reconnectIfMissingConnection() * @method static \Illuminate\Database\Connection beforeExecuting(\Closure $callback) * @method static void listen(\Closure $callback) * @method static \Illuminate\Contracts\Database\Query\Expression raw(mixed $value) * @method static string escape(string|float|int|bool|null $value, bool $binary = false) * @method static bool hasModifiedRecords() * @method static void recordsHaveBeenModified(bool $value = true) * @method static \Illuminate\Database\Connection setRecordModificationState(bool $value) * @method static void forgetRecordModificationState() * @method static \Illuminate\Database\Connection useWriteConnectionWhenReading(bool $value = true) * @method static bool isDoctrineAvailable() * @method static bool usingNativeSchemaOperations() * @method static \Doctrine\DBAL\Schema\Column getDoctrineColumn(string $table, string $column) * @method static \Doctrine\DBAL\Schema\AbstractSchemaManager getDoctrineSchemaManager() * @method static \Doctrine\DBAL\Connection getDoctrineConnection() * @method static \PDO getPdo() * @method static \PDO|\Closure|null getRawPdo() * @method static \PDO getReadPdo() * @method static \PDO|\Closure|null getRawReadPdo() * @method static \Illuminate\Database\Connection setPdo(\PDO|\Closure|null $pdo) * @method static \Illuminate\Database\Connection setReadPdo(\PDO|\Closure|null $pdo) * @method static string|null getName() * @method static string|null getNameWithReadWriteType() * @method static mixed getConfig(string|null $option = null) * @method static string getDriverName() * @method static \Illuminate\Database\Query\Grammars\Grammar getQueryGrammar() * @method static \Illuminate\Database\Connection setQueryGrammar(\Illuminate\Database\Query\Grammars\Grammar $grammar) * @method static \Illuminate\Database\Schema\Grammars\Grammar getSchemaGrammar() * @method static \Illuminate\Database\Connection setSchemaGrammar(\Illuminate\Database\Schema\Grammars\Grammar $grammar) * @method static \Illuminate\Database\Query\Processors\Processor getPostProcessor() * @method static \Illuminate\Database\Connection setPostProcessor(\Illuminate\Database\Query\Processors\Processor $processor) * @method static \Illuminate\Contracts\Events\Dispatcher getEventDispatcher() * @method static \Illuminate\Database\Connection setEventDispatcher(\Illuminate\Contracts\Events\Dispatcher $events) * @method static void unsetEventDispatcher() * @method static \Illuminate\Database\Connection setTransactionManager(\Illuminate\Database\DatabaseTransactionsManager $manager) * @method static void unsetTransactionManager() * @method static bool pretending() * @method static array getQueryLog() * @method static array getRawQueryLog() * @method static void flushQueryLog() * @method static void enableQueryLog() * @method static void disableQueryLog() * @method static bool logging() * @method static string getDatabaseName() * @method static \Illuminate\Database\Connection setDatabaseName(string $database) * @method static \Illuminate\Database\Connection setReadWriteType(string|null $readWriteType) * @method static string getTablePrefix() * @method static \Illuminate\Database\Connection setTablePrefix(string $prefix) * @method static \Illuminate\Database\Grammar withTablePrefix(\Illuminate\Database\Grammar $grammar) * @method static void resolverFor(string $driver, \Closure $callback) * @method static mixed getResolver(string $driver) * @method static mixed transaction(\Closure $callback, int $attempts = 1) * @method static void beginTransaction() * @method static void commit() * @method static void rollBack(int|null $toLevel = null) * @method static int transactionLevel() * @method static void afterCommit(callable $callback) * * @see \Illuminate\Database\DatabaseManager */ class DB extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'db'; } } src/Illuminate/Support/Facades/Validator.php 0000644 00000003050 15107327040 0015073 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static \Illuminate\Validation\Validator make(array $data, array $rules, array $messages = [], array $attributes = []) * @method static array validate(array $data, array $rules, array $messages = [], array $attributes = []) * @method static void extend(string $rule, \Closure|string $extension, string|null $message = null) * @method static void extendImplicit(string $rule, \Closure|string $extension, string|null $message = null) * @method static void extendDependent(string $rule, \Closure|string $extension, string|null $message = null) * @method static void replacer(string $rule, \Closure|string $replacer) * @method static void includeUnvalidatedArrayKeys() * @method static void excludeUnvalidatedArrayKeys() * @method static void resolver(\Closure $resolver) * @method static \Illuminate\Contracts\Translation\Translator getTranslator() * @method static \Illuminate\Validation\PresenceVerifierInterface getPresenceVerifier() * @method static void setPresenceVerifier(\Illuminate\Validation\PresenceVerifierInterface $presenceVerifier) * @method static \Illuminate\Contracts\Container\Container|null getContainer() * @method static \Illuminate\Validation\Factory setContainer(\Illuminate\Contracts\Container\Container $container) * * @see \Illuminate\Validation\Factory */ class Validator extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'validator'; } } src/Illuminate/Support/Facades/Gate.php 0000644 00000005335 15107327040 0014036 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Contracts\Auth\Access\Gate as GateContract; /** * @method static bool has(string|array $ability) * @method static \Illuminate\Auth\Access\Response allowIf(\Illuminate\Auth\Access\Response|\Closure|bool $condition, string|null $message = null, string|null $code = null) * @method static \Illuminate\Auth\Access\Response denyIf(\Illuminate\Auth\Access\Response|\Closure|bool $condition, string|null $message = null, string|null $code = null) * @method static \Illuminate\Auth\Access\Gate define(string $ability, callable|array|string $callback) * @method static \Illuminate\Auth\Access\Gate resource(string $name, string $class, array|null $abilities = null) * @method static \Illuminate\Auth\Access\Gate policy(string $class, string $policy) * @method static \Illuminate\Auth\Access\Gate before(callable $callback) * @method static \Illuminate\Auth\Access\Gate after(callable $callback) * @method static bool allows(string $ability, array|mixed $arguments = []) * @method static bool denies(string $ability, array|mixed $arguments = []) * @method static bool check(iterable|string $abilities, array|mixed $arguments = []) * @method static bool any(iterable|string $abilities, array|mixed $arguments = []) * @method static bool none(iterable|string $abilities, array|mixed $arguments = []) * @method static \Illuminate\Auth\Access\Response authorize(string $ability, array|mixed $arguments = []) * @method static \Illuminate\Auth\Access\Response inspect(string $ability, array|mixed $arguments = []) * @method static mixed raw(string $ability, array|mixed $arguments = []) * @method static mixed getPolicyFor(object|string $class) * @method static \Illuminate\Auth\Access\Gate guessPolicyNamesUsing(callable $callback) * @method static mixed resolvePolicy(object|string $class) * @method static \Illuminate\Auth\Access\Gate forUser(\Illuminate\Contracts\Auth\Authenticatable|mixed $user) * @method static array abilities() * @method static array policies() * @method static \Illuminate\Auth\Access\Gate defaultDenialResponse(\Illuminate\Auth\Access\Response $response) * @method static \Illuminate\Auth\Access\Gate setContainer(\Illuminate\Contracts\Container\Container $container) * @method static \Illuminate\Auth\Access\Response denyWithStatus(int $status, string|null $message = null, int|null $code = null) * @method static \Illuminate\Auth\Access\Response denyAsNotFound(string|null $message = null, int|null $code = null) * * @see \Illuminate\Auth\Access\Gate */ class Gate extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return GateContract::class; } } src/Illuminate/Support/Facades/Response.php 0000644 00000005116 15107327040 0014751 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract; /** * @method static \Illuminate\Http\Response make(mixed $content = '', int $status = 200, array $headers = []) * @method static \Illuminate\Http\Response noContent(int $status = 204, array $headers = []) * @method static \Illuminate\Http\Response view(string|array $view, array $data = [], int $status = 200, array $headers = []) * @method static \Illuminate\Http\JsonResponse json(mixed $data = [], int $status = 200, array $headers = [], int $options = 0) * @method static \Illuminate\Http\JsonResponse jsonp(string $callback, mixed $data = [], int $status = 200, array $headers = [], int $options = 0) * @method static \Symfony\Component\HttpFoundation\StreamedResponse stream(callable $callback, int $status = 200, array $headers = []) * @method static \Symfony\Component\HttpFoundation\StreamedResponse streamDownload(callable $callback, string|null $name = null, array $headers = [], string|null $disposition = 'attachment') * @method static \Symfony\Component\HttpFoundation\BinaryFileResponse download(\SplFileInfo|string $file, string|null $name = null, array $headers = [], string|null $disposition = 'attachment') * @method static \Symfony\Component\HttpFoundation\BinaryFileResponse file(\SplFileInfo|string $file, array $headers = []) * @method static \Illuminate\Http\RedirectResponse redirectTo(string $path, int $status = 302, array $headers = [], bool|null $secure = null) * @method static \Illuminate\Http\RedirectResponse redirectToRoute(string $route, mixed $parameters = [], int $status = 302, array $headers = []) * @method static \Illuminate\Http\RedirectResponse redirectToAction(array|string $action, mixed $parameters = [], int $status = 302, array $headers = []) * @method static \Illuminate\Http\RedirectResponse redirectGuest(string $path, int $status = 302, array $headers = [], bool|null $secure = null) * @method static \Illuminate\Http\RedirectResponse redirectToIntended(string $default = '/', int $status = 302, array $headers = [], bool|null $secure = null) * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * * @see \Illuminate\Routing\ResponseFactory */ class Response extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return ResponseFactoryContract::class; } } src/Illuminate/Support/Facades/Pipeline.php 0000644 00000001656 15107327040 0014725 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static \Illuminate\Pipeline\Pipeline send(mixed $passable) * @method static \Illuminate\Pipeline\Pipeline through(array|mixed $pipes) * @method static \Illuminate\Pipeline\Pipeline pipe(array|mixed $pipes) * @method static \Illuminate\Pipeline\Pipeline via(string $method) * @method static mixed then(\Closure $destination) * @method static mixed thenReturn() * @method static \Illuminate\Pipeline\Pipeline setContainer(\Illuminate\Contracts\Container\Container $container) * * @see \Illuminate\Pipeline\Pipeline */ class Pipeline extends Facade { /** * Indicates if the resolved instance should be cached. * * @var bool */ protected static $cached = false; /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'pipeline'; } } src/Illuminate/Support/Facades/Redis.php 0000644 00000003766 15107327040 0014232 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static \Illuminate\Redis\Connections\Connection connection(string|null $name = null) * @method static \Illuminate\Redis\Connections\Connection resolve(string|null $name = null) * @method static array connections() * @method static void enableEvents() * @method static void disableEvents() * @method static void setDriver(string $driver) * @method static void purge(string|null $name = null) * @method static \Illuminate\Redis\RedisManager extend(string $driver, \Closure $callback) * @method static void createSubscription(array|string $channels, \Closure $callback, string $method = 'subscribe') * @method static \Illuminate\Redis\Limiters\ConcurrencyLimiterBuilder funnel(string $name) * @method static \Illuminate\Redis\Limiters\DurationLimiterBuilder throttle(string $name) * @method static mixed client() * @method static void subscribe(array|string $channels, \Closure $callback) * @method static void psubscribe(array|string $channels, \Closure $callback) * @method static mixed command(string $method, array $parameters = []) * @method static void listen(\Closure $callback) * @method static string|null getName() * @method static \Illuminate\Redis\Connections\Connection setName(string $name) * @method static \Illuminate\Contracts\Events\Dispatcher getEventDispatcher() * @method static void setEventDispatcher(\Illuminate\Contracts\Events\Dispatcher $events) * @method static void unsetEventDispatcher() * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * @method static mixed macroCall(string $method, array $parameters) * * @see \Illuminate\Redis\RedisManager */ class Redis extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'redis'; } } src/Illuminate/Support/Facades/Redirect.php 0000644 00000004462 15107327040 0014717 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static \Illuminate\Http\RedirectResponse back(int $status = 302, array $headers = [], mixed $fallback = false) * @method static \Illuminate\Http\RedirectResponse refresh(int $status = 302, array $headers = []) * @method static \Illuminate\Http\RedirectResponse guest(string $path, int $status = 302, array $headers = [], bool|null $secure = null) * @method static \Illuminate\Http\RedirectResponse intended(mixed $default = '/', int $status = 302, array $headers = [], bool|null $secure = null) * @method static \Illuminate\Http\RedirectResponse to(string $path, int $status = 302, array $headers = [], bool|null $secure = null) * @method static \Illuminate\Http\RedirectResponse away(string $path, int $status = 302, array $headers = []) * @method static \Illuminate\Http\RedirectResponse secure(string $path, int $status = 302, array $headers = []) * @method static \Illuminate\Http\RedirectResponse route(string $route, mixed $parameters = [], int $status = 302, array $headers = []) * @method static \Illuminate\Http\RedirectResponse signedRoute(string $route, mixed $parameters = [], \DateTimeInterface|\DateInterval|int|null $expiration = null, int $status = 302, array $headers = []) * @method static \Illuminate\Http\RedirectResponse temporarySignedRoute(string $route, \DateTimeInterface|\DateInterval|int|null $expiration, mixed $parameters = [], int $status = 302, array $headers = []) * @method static \Illuminate\Http\RedirectResponse action(string|array $action, mixed $parameters = [], int $status = 302, array $headers = []) * @method static \Illuminate\Routing\UrlGenerator getUrlGenerator() * @method static void setSession(\Illuminate\Session\Store $session) * @method static string|null getIntendedUrl() * @method static \Illuminate\Routing\Redirector setIntendedUrl(string $url) * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * * @see \Illuminate\Routing\Redirector */ class Redirect extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'redirect'; } } src/Illuminate/Support/Facades/ParallelTesting.php 0000644 00000002344 15107327040 0016245 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static void resolveOptionsUsing(\Closure|null $resolver) * @method static void resolveTokenUsing(\Closure|null $resolver) * @method static void setUpProcess(callable $callback) * @method static void setUpTestCase(callable $callback) * @method static void setUpTestDatabase(callable $callback) * @method static void tearDownProcess(callable $callback) * @method static void tearDownTestCase(callable $callback) * @method static void callSetUpProcessCallbacks() * @method static void callSetUpTestCaseCallbacks(\Illuminate\Foundation\Testing\TestCase $testCase) * @method static void callSetUpTestDatabaseCallbacks(string $database) * @method static void callTearDownProcessCallbacks() * @method static void callTearDownTestCaseCallbacks(\Illuminate\Foundation\Testing\TestCase $testCase) * @method static mixed option(string $option) * @method static string|false token() * * @see \Illuminate\Testing\ParallelTesting */ class ParallelTesting extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return \Illuminate\Testing\ParallelTesting::class; } } src/Illuminate/Support/Facades/Event.php 0000644 00000011310 15107327040 0014225 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Testing\Fakes\EventFake; /** * @method static void listen(\Closure|string|array $events, \Closure|string|array|null $listener = null) * @method static bool hasListeners(string $eventName) * @method static bool hasWildcardListeners(string $eventName) * @method static void push(string $event, object|array $payload = []) * @method static void flush(string $event) * @method static void subscribe(object|string $subscriber) * @method static mixed until(string|object $event, mixed $payload = []) * @method static array|null dispatch(string|object $event, mixed $payload = [], bool $halt = false) * @method static array getListeners(string $eventName) * @method static \Closure makeListener(\Closure|string|array $listener, bool $wildcard = false) * @method static \Closure createClassListener(string $listener, bool $wildcard = false) * @method static void forget(string $event) * @method static void forgetPushed() * @method static \Illuminate\Events\Dispatcher setQueueResolver(callable $resolver) * @method static \Illuminate\Events\Dispatcher setTransactionManagerResolver(callable $resolver) * @method static array getRawListeners() * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * @method static \Illuminate\Support\Testing\Fakes\EventFake except(array|string $eventsToDispatch) * @method static void assertListening(string $expectedEvent, string|array $expectedListener) * @method static void assertDispatched(string|\Closure $event, callable|int|null $callback = null) * @method static void assertDispatchedTimes(string $event, int $times = 1) * @method static void assertNotDispatched(string|\Closure $event, callable|null $callback = null) * @method static void assertNothingDispatched() * @method static \Illuminate\Support\Collection dispatched(string $event, callable|null $callback = null) * @method static bool hasDispatched(string $event) * * @see \Illuminate\Events\Dispatcher * @see \Illuminate\Support\Testing\Fakes\EventFake */ class Event extends Facade { /** * Replace the bound instance with a fake. * * @param array|string $eventsToFake * @return \Illuminate\Support\Testing\Fakes\EventFake */ public static function fake($eventsToFake = []) { $actualDispatcher = static::isFake() ? static::getFacadeRoot()->dispatcher : static::getFacadeRoot(); return tap(new EventFake($actualDispatcher, $eventsToFake), function ($fake) { static::swap($fake); Model::setEventDispatcher($fake); Cache::refreshEventDispatcher(); }); } /** * Replace the bound instance with a fake that fakes all events except the given events. * * @param string[]|string $eventsToAllow * @return \Illuminate\Support\Testing\Fakes\EventFake */ public static function fakeExcept($eventsToAllow) { return static::fake([ function ($eventName) use ($eventsToAllow) { return ! in_array($eventName, (array) $eventsToAllow); }, ]); } /** * Replace the bound instance with a fake during the given callable's execution. * * @param callable $callable * @param array $eventsToFake * @return mixed */ public static function fakeFor(callable $callable, array $eventsToFake = []) { $originalDispatcher = static::getFacadeRoot(); static::fake($eventsToFake); return tap($callable(), function () use ($originalDispatcher) { static::swap($originalDispatcher); Model::setEventDispatcher($originalDispatcher); Cache::refreshEventDispatcher(); }); } /** * Replace the bound instance with a fake during the given callable's execution. * * @param callable $callable * @param array $eventsToAllow * @return mixed */ public static function fakeExceptFor(callable $callable, array $eventsToAllow = []) { $originalDispatcher = static::getFacadeRoot(); static::fakeExcept($eventsToAllow); return tap($callable(), function () use ($originalDispatcher) { static::swap($originalDispatcher); Model::setEventDispatcher($originalDispatcher); Cache::refreshEventDispatcher(); }); } /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'events'; } } src/Illuminate/Support/Facades/Cache.php 0000644 00000007404 15107327040 0014160 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static \Illuminate\Contracts\Cache\Repository store(string|null $name = null) * @method static \Illuminate\Contracts\Cache\Repository driver(string|null $driver = null) * @method static \Illuminate\Contracts\Cache\Repository resolve(string $name) * @method static \Illuminate\Cache\Repository repository(\Illuminate\Contracts\Cache\Store $store) * @method static void refreshEventDispatcher() * @method static string getDefaultDriver() * @method static void setDefaultDriver(string $name) * @method static \Illuminate\Cache\CacheManager forgetDriver(array|string|null $name = null) * @method static void purge(string|null $name = null) * @method static \Illuminate\Cache\CacheManager extend(string $driver, \Closure $callback) * @method static \Illuminate\Cache\CacheManager setApplication(\Illuminate\Contracts\Foundation\Application $app) * @method static bool has(array|string $key) * @method static bool missing(string $key) * @method static mixed get(array|string $key, mixed|\Closure $default = null) * @method static array many(array $keys) * @method static iterable getMultiple(iterable $keys, mixed $default = null) * @method static mixed pull(array|string $key, mixed|\Closure $default = null) * @method static bool put(array|string $key, mixed $value, \DateTimeInterface|\DateInterval|int|null $ttl = null) * @method static bool set(string $key, mixed $value, null|int|\DateInterval $ttl = null) * @method static bool putMany(array $values, \DateTimeInterface|\DateInterval|int|null $ttl = null) * @method static bool setMultiple(iterable $values, null|int|\DateInterval $ttl = null) * @method static bool add(string $key, mixed $value, \DateTimeInterface|\DateInterval|int|null $ttl = null) * @method static int|bool increment(string $key, mixed $value = 1) * @method static int|bool decrement(string $key, mixed $value = 1) * @method static bool forever(string $key, mixed $value) * @method static mixed remember(string $key, \Closure|\DateTimeInterface|\DateInterval|int|null $ttl, \Closure $callback) * @method static mixed sear(string $key, \Closure $callback) * @method static mixed rememberForever(string $key, \Closure $callback) * @method static bool forget(string $key) * @method static bool delete(string $key) * @method static bool deleteMultiple(iterable $keys) * @method static bool clear() * @method static \Illuminate\Cache\TaggedCache tags(array|mixed $names) * @method static bool supportsTags() * @method static int|null getDefaultCacheTime() * @method static \Illuminate\Cache\Repository setDefaultCacheTime(int|null $seconds) * @method static \Illuminate\Contracts\Cache\Store getStore() * @method static \Illuminate\Cache\Repository setStore(\Illuminate\Contracts\Cache\Store $store) * @method static \Illuminate\Contracts\Events\Dispatcher getEventDispatcher() * @method static void setEventDispatcher(\Illuminate\Contracts\Events\Dispatcher $events) * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * @method static mixed macroCall(string $method, array $parameters) * @method static bool flush() * @method static string getPrefix() * @method static \Illuminate\Contracts\Cache\Lock lock(string $name, int $seconds = 0, string|null $owner = null) * @method static \Illuminate\Contracts\Cache\Lock restoreLock(string $name, string $owner) * * @see \Illuminate\Cache\CacheManager * * @mixin \Illuminate\Cache\Repository */ class Cache extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'cache'; } } src/Illuminate/Support/Facades/Broadcast.php 0000644 00000004442 15107327040 0015056 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactoryContract; /** * @method static void routes(array|null $attributes = null) * @method static void userRoutes(array|null $attributes = null) * @method static void channelRoutes(array|null $attributes = null) * @method static string|null socket(\Illuminate\Http\Request|null $request = null) * @method static \Illuminate\Broadcasting\PendingBroadcast event(mixed|null $event = null) * @method static void queue(mixed $event) * @method static mixed connection(string|null $driver = null) * @method static mixed driver(string|null $name = null) * @method static \Pusher\Pusher pusher(array $config) * @method static \Ably\AblyRest ably(array $config) * @method static string getDefaultDriver() * @method static void setDefaultDriver(string $name) * @method static void purge(string|null $name = null) * @method static \Illuminate\Broadcasting\BroadcastManager extend(string $driver, \Closure $callback) * @method static \Illuminate\Contracts\Foundation\Application getApplication() * @method static \Illuminate\Broadcasting\BroadcastManager setApplication(\Illuminate\Contracts\Foundation\Application $app) * @method static \Illuminate\Broadcasting\BroadcastManager forgetDrivers() * @method static mixed auth(\Illuminate\Http\Request $request) * @method static mixed validAuthenticationResponse(\Illuminate\Http\Request $request, mixed $result) * @method static void broadcast(array $channels, string $event, array $payload = []) * @method static array|null resolveAuthenticatedUser(\Illuminate\Http\Request $request) * @method static void resolveAuthenticatedUserUsing(\Closure $callback) * @method static \Illuminate\Broadcasting\Broadcasters\Broadcaster channel(\Illuminate\Contracts\Broadcasting\HasBroadcastChannel|string $channel, callable|string $callback, array $options = []) * @method static \Illuminate\Support\Collection getChannels() * * @see \Illuminate\Broadcasting\BroadcastManager * @see \Illuminate\Broadcasting\Broadcasters\Broadcaster */ class Broadcast extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return BroadcastingFactoryContract::class; } } src/Illuminate/Support/Facades/URL.php 0000644 00000007172 15107327040 0013621 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static string full() * @method static string current() * @method static string previous(mixed $fallback = false) * @method static string previousPath(mixed $fallback = false) * @method static string to(string $path, mixed $extra = [], bool|null $secure = null) * @method static string secure(string $path, array $parameters = []) * @method static string asset(string $path, bool|null $secure = null) * @method static string secureAsset(string $path) * @method static string assetFrom(string $root, string $path, bool|null $secure = null) * @method static string formatScheme(bool|null $secure = null) * @method static string signedRoute(string $name, mixed $parameters = [], \DateTimeInterface|\DateInterval|int|null $expiration = null, bool $absolute = true) * @method static string temporarySignedRoute(string $name, \DateTimeInterface|\DateInterval|int $expiration, array $parameters = [], bool $absolute = true) * @method static bool hasValidSignature(\Illuminate\Http\Request $request, bool $absolute = true, array $ignoreQuery = []) * @method static bool hasValidRelativeSignature(\Illuminate\Http\Request $request, array $ignoreQuery = []) * @method static bool hasCorrectSignature(\Illuminate\Http\Request $request, bool $absolute = true, array $ignoreQuery = []) * @method static bool signatureHasNotExpired(\Illuminate\Http\Request $request) * @method static string route(string $name, mixed $parameters = [], bool $absolute = true) * @method static string toRoute(\Illuminate\Routing\Route $route, mixed $parameters, bool $absolute) * @method static string action(string|array $action, mixed $parameters = [], bool $absolute = true) * @method static array formatParameters(mixed|array $parameters) * @method static string formatRoot(string $scheme, string|null $root = null) * @method static string format(string $root, string $path, \Illuminate\Routing\Route|null $route = null) * @method static bool isValidUrl(string $path) * @method static void defaults(array $defaults) * @method static array getDefaultParameters() * @method static void forceScheme(string|null $scheme) * @method static void forceRootUrl(string|null $root) * @method static \Illuminate\Routing\UrlGenerator formatHostUsing(\Closure $callback) * @method static \Illuminate\Routing\UrlGenerator formatPathUsing(\Closure $callback) * @method static \Closure pathFormatter() * @method static \Illuminate\Http\Request getRequest() * @method static void setRequest(\Illuminate\Http\Request $request) * @method static \Illuminate\Routing\UrlGenerator setRoutes(\Illuminate\Routing\RouteCollectionInterface $routes) * @method static \Illuminate\Routing\UrlGenerator setSessionResolver(callable $sessionResolver) * @method static \Illuminate\Routing\UrlGenerator setKeyResolver(callable $keyResolver) * @method static \Illuminate\Routing\UrlGenerator withKeyResolver(callable $keyResolver) * @method static \Illuminate\Routing\UrlGenerator resolveMissingNamedRoutesUsing(callable $missingNamedRouteResolver) * @method static string getRootControllerNamespace() * @method static \Illuminate\Routing\UrlGenerator setRootControllerNamespace(string $rootNamespace) * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * * @see \Illuminate\Routing\UrlGenerator */ class URL extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'url'; } } src/Illuminate/Support/Facades/Storage.php 0000644 00000016776 15107327040 0014575 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Filesystem\Filesystem; /** * @method static \Illuminate\Contracts\Filesystem\Filesystem drive(string|null $name = null) * @method static \Illuminate\Contracts\Filesystem\Filesystem disk(string|null $name = null) * @method static \Illuminate\Contracts\Filesystem\Cloud cloud() * @method static \Illuminate\Contracts\Filesystem\Filesystem build(string|array $config) * @method static \Illuminate\Contracts\Filesystem\Filesystem createLocalDriver(array $config) * @method static \Illuminate\Contracts\Filesystem\Filesystem createFtpDriver(array $config) * @method static \Illuminate\Contracts\Filesystem\Filesystem createSftpDriver(array $config) * @method static \Illuminate\Contracts\Filesystem\Cloud createS3Driver(array $config) * @method static \Illuminate\Contracts\Filesystem\Filesystem createScopedDriver(array $config) * @method static \Illuminate\Filesystem\FilesystemManager set(string $name, mixed $disk) * @method static string getDefaultDriver() * @method static string getDefaultCloudDriver() * @method static \Illuminate\Filesystem\FilesystemManager forgetDisk(array|string $disk) * @method static void purge(string|null $name = null) * @method static \Illuminate\Filesystem\FilesystemManager extend(string $driver, \Closure $callback) * @method static \Illuminate\Filesystem\FilesystemManager setApplication(\Illuminate\Contracts\Foundation\Application $app) * @method static bool exists(string $path) * @method static string|null get(string $path) * @method static resource|null readStream(string $path) * @method static bool put(string $path, \Psr\Http\Message\StreamInterface|\Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|resource $contents, mixed $options = []) * @method static bool writeStream(string $path, resource $resource, array $options = []) * @method static string getVisibility(string $path) * @method static bool setVisibility(string $path, string $visibility) * @method static bool prepend(string $path, string $data) * @method static bool append(string $path, string $data) * @method static bool delete(string|array $paths) * @method static bool copy(string $from, string $to) * @method static bool move(string $from, string $to) * @method static int size(string $path) * @method static int lastModified(string $path) * @method static array files(string|null $directory = null, bool $recursive = false) * @method static array allFiles(string|null $directory = null) * @method static array directories(string|null $directory = null, bool $recursive = false) * @method static array allDirectories(string|null $directory = null) * @method static bool makeDirectory(string $path) * @method static bool deleteDirectory(string $directory) * @method static \Illuminate\Filesystem\FilesystemAdapter assertExists(string|array $path, string|null $content = null) * @method static \Illuminate\Filesystem\FilesystemAdapter assertMissing(string|array $path) * @method static \Illuminate\Filesystem\FilesystemAdapter assertDirectoryEmpty(string $path) * @method static bool missing(string $path) * @method static bool fileExists(string $path) * @method static bool fileMissing(string $path) * @method static bool directoryExists(string $path) * @method static bool directoryMissing(string $path) * @method static string path(string $path) * @method static array|null json(string $path, int $flags = 0) * @method static \Symfony\Component\HttpFoundation\StreamedResponse response(string $path, string|null $name = null, array $headers = [], string|null $disposition = 'inline') * @method static \Symfony\Component\HttpFoundation\StreamedResponse download(string $path, string|null $name = null, array $headers = []) * @method static string|false putFile(\Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $path, \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|array|null $file = null, mixed $options = []) * @method static string|false putFileAs(\Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $path, \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|array|null $file, string|array|null $name = null, mixed $options = []) * @method static string|false checksum(string $path, array $options = []) * @method static string|false mimeType(string $path) * @method static string url(string $path) * @method static bool providesTemporaryUrls() * @method static string temporaryUrl(string $path, \DateTimeInterface $expiration, array $options = []) * @method static array temporaryUploadUrl(string $path, \DateTimeInterface $expiration, array $options = []) * @method static \League\Flysystem\FilesystemOperator getDriver() * @method static \League\Flysystem\FilesystemAdapter getAdapter() * @method static array getConfig() * @method static void buildTemporaryUrlsUsing(\Closure $callback) * @method static \Illuminate\Filesystem\FilesystemAdapter|mixed when(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null) * @method static \Illuminate\Filesystem\FilesystemAdapter|mixed unless(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null) * @method static void macro(string $name, object|callable $macro) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * @method static mixed macroCall(string $method, array $parameters) * @method static bool has(string $location) * @method static string read(string $location) * @method static \League\Flysystem\DirectoryListing listContents(string $location, bool $deep = false) * @method static int fileSize(string $path) * @method static string visibility(string $path) * @method static void write(string $location, string $contents, array $config = []) * @method static void createDirectory(string $location, array $config = []) * * @see \Illuminate\Filesystem\FilesystemManager */ class Storage extends Facade { /** * Replace the given disk with a local testing disk. * * @param string|null $disk * @param array $config * @return \Illuminate\Contracts\Filesystem\Filesystem */ public static function fake($disk = null, array $config = []) { $disk = $disk ?: static::$app['config']->get('filesystems.default'); $root = storage_path('framework/testing/disks/'.$disk); if ($token = ParallelTesting::token()) { $root = "{$root}_test_{$token}"; } (new Filesystem)->cleanDirectory($root); static::set($disk, $fake = static::createLocalDriver(array_merge($config, [ 'root' => $root, ]))); return tap($fake)->buildTemporaryUrlsUsing(function ($path, $expiration) { return URL::to($path.'?expiration='.$expiration->getTimestamp()); }); } /** * Replace the given disk with a persistent local testing disk. * * @param string|null $disk * @param array $config * @return \Illuminate\Contracts\Filesystem\Filesystem */ public static function persistentFake($disk = null, array $config = []) { $disk = $disk ?: static::$app['config']->get('filesystems.default'); static::set($disk, $fake = static::createLocalDriver(array_merge($config, [ 'root' => storage_path('framework/testing/disks/'.$disk), ]))); return $fake; } /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'filesystem'; } } src/Illuminate/Support/HigherOrderTapProxy.php 0000644 00000001231 15107327040 0015530 0 ustar 00 <?php namespace Illuminate\Support; class HigherOrderTapProxy { /** * The target being tapped. * * @var mixed */ public $target; /** * Create a new tap proxy instance. * * @param mixed $target * @return void */ public function __construct($target) { $this->target = $target; } /** * Dynamically pass method calls to the target. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { $this->target->{$method}(...$parameters); return $this->target; } } src/Illuminate/Support/composer.json 0000644 00000003370 15107327040 0013636 0 ustar 00 { "name": "illuminate/support", "description": "The Illuminate Support package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "ext-ctype": "*", "ext-filter": "*", "ext-mbstring": "*", "doctrine/inflector": "^2.0", "illuminate/collections": "^10.0", "illuminate/conditionable": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0", "nesbot/carbon": "^2.67", "voku/portable-ascii": "^2.0" }, "conflict": { "tightenco/collect": "<5.5.33" }, "autoload": { "psr-4": { "Illuminate\\Support\\": "" }, "files": [ "helpers.php" ] }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "illuminate/filesystem": "Required to use the composer class (^10.0).", "league/commonmark": "Required to use Str::markdown() and Stringable::markdown() (^2.0.2).", "ramsey/uuid": "Required to use Str::uuid() (^4.7).", "symfony/process": "Required to use the composer class (^6.2).", "symfony/uid": "Required to use Str::ulid() (^6.2).", "symfony/var-dumper": "Required to use the dd function (^6.2).", "vlucas/phpdotenv": "Required to use the Env class and env helper (^5.4.1)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Support/Composer.php 0000644 00000015542 15107327040 0013420 0 ustar 00 <?php namespace Illuminate\Support; use Closure; use Illuminate\Filesystem\Filesystem; use RuntimeException; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Process\PhpExecutableFinder; use Symfony\Component\Process\Process; class Composer { /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * The working path to regenerate from. * * @var string|null */ protected $workingPath; /** * Create a new Composer manager instance. * * @param \Illuminate\Filesystem\Filesystem $files * @param string|null $workingPath * @return void */ public function __construct(Filesystem $files, $workingPath = null) { $this->files = $files; $this->workingPath = $workingPath; } /** * Determine if the given Composer package is installed. * * @param string $package * @return bool * * @throw \RuntimeException */ protected function hasPackage($package) { $composer = json_decode(file_get_contents($this->findComposerFile()), true); return array_key_exists($package, $composer['require'] ?? []) || array_key_exists($package, $composer['require-dev'] ?? []); } /** * Install the given Composer packages into the application. * * @param array<int, string> $packages * @param bool $dev * @param \Closure|\Symfony\Component\Console\Output\OutputInterface|null $output * @param string|null $composerBinary * @return bool */ public function requirePackages(array $packages, bool $dev = false, Closure|OutputInterface $output = null, $composerBinary = null) { $command = collect([ ...$this->findComposer($composerBinary), 'require', ...$packages, ]) ->when($dev, function ($command) { $command->push('--dev'); })->all(); return 0 === $this->getProcess($command, ['COMPOSER_MEMORY_LIMIT' => '-1']) ->run( $output instanceof OutputInterface ? function ($type, $line) use ($output) { $output->write(' '.$line); } : $output ); } /** * Remove the given Composer packages from the application. * * @param array<int, string> $packages * @param bool $dev * @param \Closure|\Symfony\Component\Console\Output\OutputInterface|null $output * @param string|null $composerBinary * @return bool */ public function removePackages(array $packages, bool $dev = false, Closure|OutputInterface $output = null, $composerBinary = null) { $command = collect([ ...$this->findComposer($composerBinary), 'remove', ...$packages, ]) ->when($dev, function ($command) { $command->push('--dev'); })->all(); return 0 === $this->getProcess($command, ['COMPOSER_MEMORY_LIMIT' => '-1']) ->run( $output instanceof OutputInterface ? function ($type, $line) use ($output) { $output->write(' '.$line); } : $output ); } /** * Modify the "composer.json" file contents using the given callback. * * @param callable(array):array $callback * @return void * * @throw \RuntimeException */ public function modify(callable $callback) { $composerFile = $this->findComposerFile(); $composer = json_decode(file_get_contents($composerFile), true, 512, JSON_THROW_ON_ERROR); file_put_contents( $composerFile, json_encode( call_user_func($callback, $composer), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ); } /** * Regenerate the Composer autoloader files. * * @param string|array $extra * @param string|null $composerBinary * @return int */ public function dumpAutoloads($extra = '', $composerBinary = null) { $extra = $extra ? (array) $extra : []; $command = array_merge($this->findComposer($composerBinary), ['dump-autoload'], $extra); return $this->getProcess($command)->run(); } /** * Regenerate the optimized Composer autoloader files. * * @param string|null $composerBinary * @return int */ public function dumpOptimized($composerBinary = null) { return $this->dumpAutoloads('--optimize', $composerBinary); } /** * Get the Composer binary / command for the environment. * * @param string|null $composerBinary * @return array */ public function findComposer($composerBinary = null) { if (! is_null($composerBinary) && $this->files->exists($composerBinary)) { return [$this->phpBinary(), $composerBinary]; } elseif ($this->files->exists($this->workingPath.'/composer.phar')) { return [$this->phpBinary(), 'composer.phar']; } return ['composer']; } /** * Get the path to the "composer.json" file. * * @return string * * @throw \RuntimeException */ protected function findComposerFile() { $composerFile = "{$this->workingPath}/composer.json"; if (! file_exists($composerFile)) { throw new RuntimeException("Unable to locate `composer.json` file at [{$this->workingPath}]."); } return $composerFile; } /** * Get the PHP binary. * * @return string */ protected function phpBinary() { return ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false)); } /** * Get a new Symfony process instance. * * @param array $command * @param array $env * @return \Symfony\Component\Process\Process */ protected function getProcess(array $command, array $env = []) { return (new Process($command, $this->workingPath, $env))->setTimeout(null); } /** * Set the working path used by the class. * * @param string $path * @return $this */ public function setWorkingPath($path) { $this->workingPath = realpath($path); return $this; } /** * Get the version of Composer. * * @return string|null */ public function getVersion() { $command = array_merge($this->findComposer(), ['-V', '--no-ansi']); $process = $this->getProcess($command); $process->run(); $output = $process->getOutput(); if (preg_match('/(\d+(\.\d+){2})/', $output, $version)) { return $version[1]; } return explode(' ', $output)[2] ?? null; } } src/Illuminate/Support/ViewErrorBag.php 0000644 00000005103 15107327040 0014157 0 ustar 00 <?php namespace Illuminate\Support; use Countable; use Illuminate\Contracts\Support\MessageBag as MessageBagContract; /** * @mixin \Illuminate\Contracts\Support\MessageBag */ class ViewErrorBag implements Countable { /** * The array of the view error bags. * * @var array */ protected $bags = []; /** * Checks if a named MessageBag exists in the bags. * * @param string $key * @return bool */ public function hasBag($key = 'default') { return isset($this->bags[$key]); } /** * Get a MessageBag instance from the bags. * * @param string $key * @return \Illuminate\Contracts\Support\MessageBag */ public function getBag($key) { return Arr::get($this->bags, $key) ?: new MessageBag; } /** * Get all the bags. * * @return array */ public function getBags() { return $this->bags; } /** * Add a new MessageBag instance to the bags. * * @param string $key * @param \Illuminate\Contracts\Support\MessageBag $bag * @return $this */ public function put($key, MessageBagContract $bag) { $this->bags[$key] = $bag; return $this; } /** * Determine if the default message bag has any messages. * * @return bool */ public function any() { return $this->count() > 0; } /** * Get the number of messages in the default bag. * * @return int */ public function count(): int { return $this->getBag('default')->count(); } /** * Dynamically call methods on the default bag. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->getBag('default')->$method(...$parameters); } /** * Dynamically access a view error bag. * * @param string $key * @return \Illuminate\Contracts\Support\MessageBag */ public function __get($key) { return $this->getBag($key); } /** * Dynamically set a view error bag. * * @param string $key * @param \Illuminate\Contracts\Support\MessageBag $value * @return void */ public function __set($key, $value) { $this->put($key, $value); } /** * Convert the default bag to its string representation. * * @return string */ public function __toString() { return (string) $this->getBag('default'); } } src/Illuminate/Support/ServiceProvider.php 0000644 00000025763 15107327040 0014752 0 ustar 00 <?php namespace Illuminate\Support; use Closure; use Illuminate\Console\Application as Artisan; use Illuminate\Contracts\Foundation\CachesConfiguration; use Illuminate\Contracts\Foundation\CachesRoutes; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Database\Eloquent\Factory as ModelFactory; use Illuminate\View\Compilers\BladeCompiler; abstract class ServiceProvider { /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * All of the registered booting callbacks. * * @var array */ protected $bootingCallbacks = []; /** * All of the registered booted callbacks. * * @var array */ protected $bootedCallbacks = []; /** * The paths that should be published. * * @var array */ public static $publishes = []; /** * The paths that should be published by group. * * @var array */ public static $publishGroups = []; /** * Create a new service provider instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function __construct($app) { $this->app = $app; } /** * Register any application services. * * @return void */ public function register() { // } /** * Register a booting callback to be run before the "boot" method is called. * * @param \Closure $callback * @return void */ public function booting(Closure $callback) { $this->bootingCallbacks[] = $callback; } /** * Register a booted callback to be run after the "boot" method is called. * * @param \Closure $callback * @return void */ public function booted(Closure $callback) { $this->bootedCallbacks[] = $callback; } /** * Call the registered booting callbacks. * * @return void */ public function callBootingCallbacks() { $index = 0; while ($index < count($this->bootingCallbacks)) { $this->app->call($this->bootingCallbacks[$index]); $index++; } } /** * Call the registered booted callbacks. * * @return void */ public function callBootedCallbacks() { $index = 0; while ($index < count($this->bootedCallbacks)) { $this->app->call($this->bootedCallbacks[$index]); $index++; } } /** * Merge the given configuration with the existing configuration. * * @param string $path * @param string $key * @return void */ protected function mergeConfigFrom($path, $key) { if (! ($this->app instanceof CachesConfiguration && $this->app->configurationIsCached())) { $config = $this->app->make('config'); $config->set($key, array_merge( require $path, $config->get($key, []) )); } } /** * Load the given routes file if routes are not already cached. * * @param string $path * @return void */ protected function loadRoutesFrom($path) { if (! ($this->app instanceof CachesRoutes && $this->app->routesAreCached())) { require $path; } } /** * Register a view file namespace. * * @param string|array $path * @param string $namespace * @return void */ protected function loadViewsFrom($path, $namespace) { $this->callAfterResolving('view', function ($view) use ($path, $namespace) { if (isset($this->app->config['view']['paths']) && is_array($this->app->config['view']['paths'])) { foreach ($this->app->config['view']['paths'] as $viewPath) { if (is_dir($appPath = $viewPath.'/vendor/'.$namespace)) { $view->addNamespace($namespace, $appPath); } } } $view->addNamespace($namespace, $path); }); } /** * Register the given view components with a custom prefix. * * @param string $prefix * @param array $components * @return void */ protected function loadViewComponentsAs($prefix, array $components) { $this->callAfterResolving(BladeCompiler::class, function ($blade) use ($prefix, $components) { foreach ($components as $alias => $component) { $blade->component($component, is_string($alias) ? $alias : null, $prefix); } }); } /** * Register a translation file namespace. * * @param string $path * @param string $namespace * @return void */ protected function loadTranslationsFrom($path, $namespace) { $this->callAfterResolving('translator', function ($translator) use ($path, $namespace) { $translator->addNamespace($namespace, $path); }); } /** * Register a JSON translation file path. * * @param string $path * @return void */ protected function loadJsonTranslationsFrom($path) { $this->callAfterResolving('translator', function ($translator) use ($path) { $translator->addJsonPath($path); }); } /** * Register database migration paths. * * @param array|string $paths * @return void */ protected function loadMigrationsFrom($paths) { $this->callAfterResolving('migrator', function ($migrator) use ($paths) { foreach ((array) $paths as $path) { $migrator->path($path); } }); } /** * Register Eloquent model factory paths. * * @deprecated Will be removed in a future Laravel version. * * @param array|string $paths * @return void */ protected function loadFactoriesFrom($paths) { $this->callAfterResolving(ModelFactory::class, function ($factory) use ($paths) { foreach ((array) $paths as $path) { $factory->load($path); } }); } /** * Setup an after resolving listener, or fire immediately if already resolved. * * @param string $name * @param callable $callback * @return void */ protected function callAfterResolving($name, $callback) { $this->app->afterResolving($name, $callback); if ($this->app->resolved($name)) { $callback($this->app->make($name), $this->app); } } /** * Register paths to be published by the publish command. * * @param array $paths * @param mixed $groups * @return void */ protected function publishes(array $paths, $groups = null) { $this->ensurePublishArrayInitialized($class = static::class); static::$publishes[$class] = array_merge(static::$publishes[$class], $paths); foreach ((array) $groups as $group) { $this->addPublishGroup($group, $paths); } } /** * Ensure the publish array for the service provider is initialized. * * @param string $class * @return void */ protected function ensurePublishArrayInitialized($class) { if (! array_key_exists($class, static::$publishes)) { static::$publishes[$class] = []; } } /** * Add a publish group / tag to the service provider. * * @param string $group * @param array $paths * @return void */ protected function addPublishGroup($group, $paths) { if (! array_key_exists($group, static::$publishGroups)) { static::$publishGroups[$group] = []; } static::$publishGroups[$group] = array_merge( static::$publishGroups[$group], $paths ); } /** * Get the paths to publish. * * @param string|null $provider * @param string|null $group * @return array */ public static function pathsToPublish($provider = null, $group = null) { if (! is_null($paths = static::pathsForProviderOrGroup($provider, $group))) { return $paths; } return collect(static::$publishes)->reduce(function ($paths, $p) { return array_merge($paths, $p); }, []); } /** * Get the paths for the provider or group (or both). * * @param string|null $provider * @param string|null $group * @return array */ protected static function pathsForProviderOrGroup($provider, $group) { if ($provider && $group) { return static::pathsForProviderAndGroup($provider, $group); } elseif ($group && array_key_exists($group, static::$publishGroups)) { return static::$publishGroups[$group]; } elseif ($provider && array_key_exists($provider, static::$publishes)) { return static::$publishes[$provider]; } elseif ($group || $provider) { return []; } } /** * Get the paths for the provider and group. * * @param string $provider * @param string $group * @return array */ protected static function pathsForProviderAndGroup($provider, $group) { if (! empty(static::$publishes[$provider]) && ! empty(static::$publishGroups[$group])) { return array_intersect_key(static::$publishes[$provider], static::$publishGroups[$group]); } return []; } /** * Get the service providers available for publishing. * * @return array */ public static function publishableProviders() { return array_keys(static::$publishes); } /** * Get the groups available for publishing. * * @return array */ public static function publishableGroups() { return array_keys(static::$publishGroups); } /** * Register the package's custom Artisan commands. * * @param array|mixed $commands * @return void */ public function commands($commands) { $commands = is_array($commands) ? $commands : func_get_args(); Artisan::starting(function ($artisan) use ($commands) { $artisan->resolveCommands($commands); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return []; } /** * Get the events that trigger this service provider to register. * * @return array */ public function when() { return []; } /** * Determine if the provider is deferred. * * @return bool */ public function isDeferred() { return $this instanceof DeferrableProvider; } /** * Get the default providers for a Laravel application. * * @return \Illuminate\Support\DefaultProviders */ public static function defaultProviders() { return new DefaultProviders; } } src/Illuminate/Support/Traits/ForwardsCalls.php 0000644 00000003521 15107327040 0015637 0 ustar 00 <?php namespace Illuminate\Support\Traits; use BadMethodCallException; use Error; trait ForwardsCalls { /** * Forward a method call to the given object. * * @param mixed $object * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ protected function forwardCallTo($object, $method, $parameters) { try { return $object->{$method}(...$parameters); } catch (Error|BadMethodCallException $e) { $pattern = '~^Call to undefined method (?P<class>[^:]+)::(?P<method>[^\(]+)\(\)$~'; if (! preg_match($pattern, $e->getMessage(), $matches)) { throw $e; } if ($matches['class'] != get_class($object) || $matches['method'] != $method) { throw $e; } static::throwBadMethodCallException($method); } } /** * Forward a method call to the given object, returning $this if the forwarded call returned itself. * * @param mixed $object * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ protected function forwardDecoratedCallTo($object, $method, $parameters) { $result = $this->forwardCallTo($object, $method, $parameters); return $result === $object ? $this : $result; } /** * Throw a bad method call exception for the given method. * * @param string $method * @return void * * @throws \BadMethodCallException */ protected static function throwBadMethodCallException($method) { throw new BadMethodCallException(sprintf( 'Call to undefined method %s::%s()', static::class, $method )); } } src/Illuminate/Support/Traits/CapsuleManagerTrait.php 0000644 00000002627 15107327040 0016772 0 ustar 00 <?php namespace Illuminate\Support\Traits; use Illuminate\Contracts\Container\Container; use Illuminate\Support\Fluent; trait CapsuleManagerTrait { /** * The current globally used instance. * * @var object */ protected static $instance; /** * The container instance. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * Setup the IoC container instance. * * @param \Illuminate\Contracts\Container\Container $container * @return void */ protected function setupContainer(Container $container) { $this->container = $container; if (! $this->container->bound('config')) { $this->container->instance('config', new Fluent); } } /** * Make this capsule instance available globally. * * @return void */ public function setAsGlobal() { static::$instance = $this; } /** * Get the IoC container instance. * * @return \Illuminate\Contracts\Container\Container */ public function getContainer() { return $this->container; } /** * Set the IoC container instance. * * @param \Illuminate\Contracts\Container\Container $container * @return void */ public function setContainer(Container $container) { $this->container = $container; } } src/Illuminate/Support/Traits/Tappable.php 0000644 00000000543 15107327040 0014622 0 ustar 00 <?php namespace Illuminate\Support\Traits; trait Tappable { /** * Call the given Closure with this instance then return the instance. * * @param callable|null $callback * @return $this|\Illuminate\Support\HigherOrderTapProxy */ public function tap($callback = null) { return tap($this, $callback); } } src/Illuminate/Support/Traits/ReflectsClosures.php 0000644 00000004736 15107327040 0016371 0 ustar 00 <?php namespace Illuminate\Support\Traits; use Closure; use Illuminate\Support\Reflector; use ReflectionFunction; use RuntimeException; trait ReflectsClosures { /** * Get the class name of the first parameter of the given Closure. * * @param \Closure $closure * @return string * * @throws \ReflectionException * @throws \RuntimeException */ protected function firstClosureParameterType(Closure $closure) { $types = array_values($this->closureParameterTypes($closure)); if (! $types) { throw new RuntimeException('The given Closure has no parameters.'); } if ($types[0] === null) { throw new RuntimeException('The first parameter of the given Closure is missing a type hint.'); } return $types[0]; } /** * Get the class names of the first parameter of the given Closure, including union types. * * @param \Closure $closure * @return array * * @throws \ReflectionException * @throws \RuntimeException */ protected function firstClosureParameterTypes(Closure $closure) { $reflection = new ReflectionFunction($closure); $types = collect($reflection->getParameters())->mapWithKeys(function ($parameter) { if ($parameter->isVariadic()) { return [$parameter->getName() => null]; } return [$parameter->getName() => Reflector::getParameterClassNames($parameter)]; })->filter()->values()->all(); if (empty($types)) { throw new RuntimeException('The given Closure has no parameters.'); } if (isset($types[0]) && empty($types[0])) { throw new RuntimeException('The first parameter of the given Closure is missing a type hint.'); } return $types[0]; } /** * Get the class names / types of the parameters of the given Closure. * * @param \Closure $closure * @return array * * @throws \ReflectionException */ protected function closureParameterTypes(Closure $closure) { $reflection = new ReflectionFunction($closure); return collect($reflection->getParameters())->mapWithKeys(function ($parameter) { if ($parameter->isVariadic()) { return [$parameter->getName() => null]; } return [$parameter->getName() => Reflector::getParameterClassName($parameter)]; })->all(); } } src/Illuminate/Support/Traits/Localizable.php 0000644 00000001165 15107327040 0015314 0 ustar 00 <?php namespace Illuminate\Support\Traits; use Illuminate\Container\Container; trait Localizable { /** * Run the callback with the given locale. * * @param string $locale * @param \Closure $callback * @return mixed */ public function withLocale($locale, $callback) { if (! $locale) { return $callback(); } $app = Container::getInstance(); $original = $app->getLocale(); try { $app->setLocale($locale); return $callback(); } finally { $app->setLocale($original); } } } src/Illuminate/Support/LICENSE.md 0000644 00000002063 15107327040 0012516 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Support/HtmlString.php 0000644 00000002041 15107327040 0013712 0 ustar 00 <?php namespace Illuminate\Support; use Illuminate\Contracts\Support\Htmlable; class HtmlString implements Htmlable { /** * The HTML string. * * @var string */ protected $html; /** * Create a new HTML string instance. * * @param string $html * @return void */ public function __construct($html = '') { $this->html = $html; } /** * Get the HTML string. * * @return string */ public function toHtml() { return $this->html; } /** * Determine if the given HTML string is empty. * * @return bool */ public function isEmpty() { return $this->html === ''; } /** * Determine if the given HTML string is not empty. * * @return bool */ public function isNotEmpty() { return ! $this->isEmpty(); } /** * Get the HTML string. * * @return string */ public function __toString() { return $this->toHtml(); } } src/Illuminate/Support/Pluralizer.php 0000644 00000005534 15107327040 0013762 0 ustar 00 <?php namespace Illuminate\Support; use Doctrine\Inflector\InflectorFactory; class Pluralizer { /** * The cached inflector instance. * * @var static */ protected static $inflector; /** * The language that should be used by the inflector. * * @var string */ protected static $language = 'english'; /** * Uncountable non-nouns word forms. * * Contains words supported by Doctrine/Inflector/Rules/English/Uninflected.php * * @var string[] */ public static $uncountable = [ 'recommended', 'related', ]; /** * Get the plural form of an English word. * * @param string $value * @param int|array|\Countable $count * @return string */ public static function plural($value, $count = 2) { if (is_countable($count)) { $count = count($count); } if ((int) abs($count) === 1 || static::uncountable($value) || preg_match('/^(.*)[A-Za-z0-9\x{0080}-\x{FFFF}]$/u', $value) == 0) { return $value; } $plural = static::inflector()->pluralize($value); return static::matchCase($plural, $value); } /** * Get the singular form of an English word. * * @param string $value * @return string */ public static function singular($value) { $singular = static::inflector()->singularize($value); return static::matchCase($singular, $value); } /** * Determine if the given value is uncountable. * * @param string $value * @return bool */ protected static function uncountable($value) { return in_array(strtolower($value), static::$uncountable); } /** * Attempt to match the case on two strings. * * @param string $value * @param string $comparison * @return string */ protected static function matchCase($value, $comparison) { $functions = ['mb_strtolower', 'mb_strtoupper', 'ucfirst', 'ucwords']; foreach ($functions as $function) { if ($function($comparison) === $comparison) { return $function($value); } } return $value; } /** * Get the inflector instance. * * @return \Doctrine\Inflector\Inflector */ public static function inflector() { if (is_null(static::$inflector)) { static::$inflector = InflectorFactory::createForLanguage(static::$language)->build(); } return static::$inflector; } /** * Specify the language that should be used by the inflector. * * @param string $language * @return void */ public static function useLanguage(string $language) { static::$language = $language; static::$inflector = null; } } src/Illuminate/Support/MessageBag.php 0000644 00000024136 15107327040 0013626 0 ustar 00 <?php namespace Illuminate\Support; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Contracts\Support\MessageBag as MessageBagContract; use Illuminate\Contracts\Support\MessageProvider; use JsonSerializable; class MessageBag implements Jsonable, JsonSerializable, MessageBagContract, MessageProvider { /** * All of the registered messages. * * @var array */ protected $messages = []; /** * Default format for message output. * * @var string */ protected $format = ':message'; /** * Create a new message bag instance. * * @param array $messages * @return void */ public function __construct(array $messages = []) { foreach ($messages as $key => $value) { $value = $value instanceof Arrayable ? $value->toArray() : (array) $value; $this->messages[$key] = array_unique($value); } } /** * Get the keys present in the message bag. * * @return array */ public function keys() { return array_keys($this->messages); } /** * Add a message to the message bag. * * @param string $key * @param string $message * @return $this */ public function add($key, $message) { if ($this->isUnique($key, $message)) { $this->messages[$key][] = $message; } return $this; } /** * Add a message to the message bag if the given conditional is "true". * * @param bool $boolean * @param string $key * @param string $message * @return $this */ public function addIf($boolean, $key, $message) { return $boolean ? $this->add($key, $message) : $this; } /** * Determine if a key and message combination already exists. * * @param string $key * @param string $message * @return bool */ protected function isUnique($key, $message) { $messages = (array) $this->messages; return ! isset($messages[$key]) || ! in_array($message, $messages[$key]); } /** * Merge a new array of messages into the message bag. * * @param \Illuminate\Contracts\Support\MessageProvider|array $messages * @return $this */ public function merge($messages) { if ($messages instanceof MessageProvider) { $messages = $messages->getMessageBag()->getMessages(); } $this->messages = array_merge_recursive($this->messages, $messages); return $this; } /** * Determine if messages exist for all of the given keys. * * @param array|string|null $key * @return bool */ public function has($key) { if ($this->isEmpty()) { return false; } if (is_null($key)) { return $this->any(); } $keys = is_array($key) ? $key : func_get_args(); foreach ($keys as $key) { if ($this->first($key) === '') { return false; } } return true; } /** * Determine if messages exist for any of the given keys. * * @param array|string|null $keys * @return bool */ public function hasAny($keys = []) { if ($this->isEmpty()) { return false; } $keys = is_array($keys) ? $keys : func_get_args(); foreach ($keys as $key) { if ($this->has($key)) { return true; } } return false; } /** * Determine if messages don't exist for all of the given keys. * * @param array|string|null $key * @return bool */ public function missing($key) { $keys = is_array($key) ? $key : func_get_args(); return ! $this->hasAny($keys); } /** * Get the first message from the message bag for a given key. * * @param string|null $key * @param string|null $format * @return string */ public function first($key = null, $format = null) { $messages = is_null($key) ? $this->all($format) : $this->get($key, $format); $firstMessage = Arr::first($messages, null, ''); return is_array($firstMessage) ? Arr::first($firstMessage) : $firstMessage; } /** * Get all of the messages from the message bag for a given key. * * @param string $key * @param string|null $format * @return array */ public function get($key, $format = null) { // If the message exists in the message bag, we will transform it and return // the message. Otherwise, we will check if the key is implicit & collect // all the messages that match the given key and output it as an array. if (array_key_exists($key, $this->messages)) { return $this->transform( $this->messages[$key], $this->checkFormat($format), $key ); } if (str_contains($key, '*')) { return $this->getMessagesForWildcardKey($key, $format); } return []; } /** * Get the messages for a wildcard key. * * @param string $key * @param string|null $format * @return array */ protected function getMessagesForWildcardKey($key, $format) { return collect($this->messages) ->filter(function ($messages, $messageKey) use ($key) { return Str::is($key, $messageKey); }) ->map(function ($messages, $messageKey) use ($format) { return $this->transform( $messages, $this->checkFormat($format), $messageKey ); })->all(); } /** * Get all of the messages for every key in the message bag. * * @param string|null $format * @return array */ public function all($format = null) { $format = $this->checkFormat($format); $all = []; foreach ($this->messages as $key => $messages) { $all = array_merge($all, $this->transform($messages, $format, $key)); } return $all; } /** * Get all of the unique messages for every key in the message bag. * * @param string|null $format * @return array */ public function unique($format = null) { return array_unique($this->all($format)); } /** * Remove a message from the message bag. * * @param string $key * @return $this */ public function forget($key) { unset($this->messages[$key]); return $this; } /** * Format an array of messages. * * @param array $messages * @param string $format * @param string $messageKey * @return array */ protected function transform($messages, $format, $messageKey) { if ($format == ':message') { return (array) $messages; } return collect((array) $messages) ->map(function ($message) use ($format, $messageKey) { // We will simply spin through the given messages and transform each one // replacing the :message place holder with the real message allowing // the messages to be easily formatted to each developer's desires. return str_replace([':message', ':key'], [$message, $messageKey], $format); })->all(); } /** * Get the appropriate format based on the given format. * * @param string $format * @return string */ protected function checkFormat($format) { return $format ?: $this->format; } /** * Get the raw messages in the message bag. * * @return array */ public function messages() { return $this->messages; } /** * Get the raw messages in the message bag. * * @return array */ public function getMessages() { return $this->messages(); } /** * Get the messages for the instance. * * @return \Illuminate\Support\MessageBag */ public function getMessageBag() { return $this; } /** * Get the default message format. * * @return string */ public function getFormat() { return $this->format; } /** * Set the default message format. * * @param string $format * @return \Illuminate\Support\MessageBag */ public function setFormat($format = ':message') { $this->format = $format; return $this; } /** * Determine if the message bag has any messages. * * @return bool */ public function isEmpty() { return ! $this->any(); } /** * Determine if the message bag has any messages. * * @return bool */ public function isNotEmpty() { return $this->any(); } /** * Determine if the message bag has any messages. * * @return bool */ public function any() { return $this->count() > 0; } /** * Get the number of messages in the message bag. * * @return int */ public function count(): int { return count($this->messages, COUNT_RECURSIVE) - count($this->messages); } /** * Get the instance as an array. * * @return array */ public function toArray() { return $this->getMessages(); } /** * Convert the object into something JSON serializable. * * @return array */ public function jsonSerialize(): array { return $this->toArray(); } /** * Convert the object to its JSON representation. * * @param int $options * @return string */ public function toJson($options = 0) { return json_encode($this->jsonSerialize(), $options); } /** * Convert the message bag to its string representation. * * @return string */ public function __toString() { return $this->toJson(); } } src/Illuminate/Support/ProcessUtils.php 0000644 00000004002 15107327040 0014255 0 ustar 00 <?php namespace Illuminate\Support; /** * ProcessUtils is a bunch of utility methods. * * This class was originally copied from Symfony 3. */ class ProcessUtils { /** * Escapes a string to be used as a shell argument. * * @param string $argument * @return string */ public static function escapeArgument($argument) { // Fix for PHP bug #43784 escapeshellarg removes % from given string // Fix for PHP bug #49446 escapeshellarg doesn't work on Windows // @see https://bugs.php.net/bug.php?id=43784 // @see https://bugs.php.net/bug.php?id=49446 if ('\\' === DIRECTORY_SEPARATOR) { if ($argument === '') { return '""'; } $escapedArgument = ''; $quote = false; foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) { if ($part === '"') { $escapedArgument .= '\\"'; } elseif (self::isSurroundedBy($part, '%')) { // Avoid environment variable expansion $escapedArgument .= '^%"'.substr($part, 1, -1).'"^%'; } else { // escape trailing backslash if (str_ends_with($part, '\\')) { $part .= '\\'; } $quote = true; $escapedArgument .= $part; } } if ($quote) { $escapedArgument = '"'.$escapedArgument.'"'; } return $escapedArgument; } return "'".str_replace("'", "'\\''", $argument)."'"; } /** * Is the given string surrounded by the given character? * * @param string $arg * @param string $char * @return bool */ protected static function isSurroundedBy($arg, $char) { return strlen($arg) > 2 && $char === $arg[0] && $char === $arg[strlen($arg) - 1]; } } src/Illuminate/Support/Fluent.php 0000644 00000010221 15107327040 0013053 0 ustar 00 <?php namespace Illuminate\Support; use ArrayAccess; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use JsonSerializable; /** * @template TKey of array-key * @template TValue * * @implements \Illuminate\Contracts\Support\Arrayable<TKey, TValue> * @implements \ArrayAccess<TKey, TValue> */ class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable { /** * All of the attributes set on the fluent instance. * * @var array<TKey, TValue> */ protected $attributes = []; /** * Create a new fluent instance. * * @param iterable<TKey, TValue> $attributes * @return void */ public function __construct($attributes = []) { foreach ($attributes as $key => $value) { $this->attributes[$key] = $value; } } /** * Get an attribute from the fluent instance. * * @template TGetDefault * * @param TKey $key * @param TGetDefault|(\Closure(): TGetDefault) $default * @return TValue|TGetDefault */ public function get($key, $default = null) { if (array_key_exists($key, $this->attributes)) { return $this->attributes[$key]; } return value($default); } /** * Get the attributes from the fluent instance. * * @return array<TKey, TValue> */ public function getAttributes() { return $this->attributes; } /** * Convert the fluent instance to an array. * * @return array<TKey, TValue> */ public function toArray() { return $this->attributes; } /** * Convert the object into something JSON serializable. * * @return array<TKey, TValue> */ public function jsonSerialize(): array { return $this->toArray(); } /** * Convert the fluent instance to JSON. * * @param int $options * @return string */ public function toJson($options = 0) { return json_encode($this->jsonSerialize(), $options); } /** * Determine if the given offset exists. * * @param TKey $offset * @return bool */ public function offsetExists($offset): bool { return isset($this->attributes[$offset]); } /** * Get the value for a given offset. * * @param TKey $offset * @return TValue|null */ public function offsetGet($offset): mixed { return $this->get($offset); } /** * Set the value at the given offset. * * @param TKey $offset * @param TValue $value * @return void */ public function offsetSet($offset, $value): void { $this->attributes[$offset] = $value; } /** * Unset the value at the given offset. * * @param TKey $offset * @return void */ public function offsetUnset($offset): void { unset($this->attributes[$offset]); } /** * Handle dynamic calls to the fluent instance to set attributes. * * @param TKey $method * @param array{0: ?TValue} $parameters * @return $this */ public function __call($method, $parameters) { $this->attributes[$method] = count($parameters) > 0 ? reset($parameters) : true; return $this; } /** * Dynamically retrieve the value of an attribute. * * @param TKey $key * @return TValue|null */ public function __get($key) { return $this->get($key); } /** * Dynamically set the value of an attribute. * * @param TKey $key * @param TValue $value * @return void */ public function __set($key, $value) { $this->offsetSet($key, $value); } /** * Dynamically check if an attribute is set. * * @param TKey $key * @return bool */ public function __isset($key) { return $this->offsetExists($key); } /** * Dynamically unset an attribute. * * @param TKey $key * @return void */ public function __unset($key) { $this->offsetUnset($key); } } src/Illuminate/Support/Benchmark.php 0000644 00000003666 15107327040 0013527 0 ustar 00 <?php namespace Illuminate\Support; use Closure; class Benchmark { /** * Measure a callable or array of callables over the given number of iterations. * * @param \Closure|array $benchmarkables * @param int $iterations * @return array|float */ public static function measure(Closure|array $benchmarkables, int $iterations = 1): array|float { return collect(Arr::wrap($benchmarkables))->map(function ($callback) use ($iterations) { return collect(range(1, $iterations))->map(function () use ($callback) { gc_collect_cycles(); $start = hrtime(true); $callback(); return (hrtime(true) - $start) / 1000000; })->average(); })->when( $benchmarkables instanceof Closure, fn ($c) => $c->first(), fn ($c) => $c->all(), ); } /** * Measure a callable once and return the duration and result. * * @template TReturn of mixed * * @param (callable(): TReturn) $callback * @return array{0: TReturn, 1: float} */ public static function value(callable $callback): array { gc_collect_cycles(); $start = hrtime(true); $result = $callback(); return [$result, (hrtime(true) - $start) / 1000000]; } /** * Measure a callable or array of callables over the given number of iterations, then dump and die. * * @param \Closure|array $benchmarkables * @param int $iterations * @return never */ public static function dd(Closure|array $benchmarkables, int $iterations = 1): void { $result = collect(static::measure(Arr::wrap($benchmarkables), $iterations)) ->map(fn ($average) => number_format($average, 3).'ms') ->when($benchmarkables instanceof Closure, fn ($c) => $c->first(), fn ($c) => $c->all()); dd($result); } } src/Illuminate/Support/Timebox.php 0000644 00000003177 15107327040 0013241 0 ustar 00 <?php namespace Illuminate\Support; use Throwable; class Timebox { /** * Indicates if the timebox is allowed to return early. * * @var bool */ public $earlyReturn = false; /** * Invoke the given callback within the specified timebox minimum. * * @template TCallReturnType * * @param (callable($this): TCallReturnType) $callback * @param int $microseconds * @return TCallReturnType */ public function call(callable $callback, int $microseconds) { $exception = null; $start = microtime(true); try { $result = $callback($this); } catch (Throwable $caught) { $exception = $caught; } $remainder = intval($microseconds - ((microtime(true) - $start) * 1000000)); if (! $this->earlyReturn && $remainder > 0) { $this->usleep($remainder); } if ($exception) { throw $exception; } return $result; } /** * Indicate that the timebox can return early. * * @return $this */ public function returnEarly() { $this->earlyReturn = true; return $this; } /** * Indicate that the timebox cannot return early. * * @return $this */ public function dontReturnEarly() { $this->earlyReturn = false; return $this; } /** * Sleep for the specified number of microseconds. * * @param int $microseconds * @return void */ protected function usleep(int $microseconds) { Sleep::usleep($microseconds); } } src/Illuminate/Support/AggregateServiceProvider.php 0000644 00000001743 15107327040 0016551 0 ustar 00 <?php namespace Illuminate\Support; class AggregateServiceProvider extends ServiceProvider { /** * The provider class names. * * @var array */ protected $providers = []; /** * An array of the service provider instances. * * @var array */ protected $instances = []; /** * Register the service provider. * * @return void */ public function register() { $this->instances = []; foreach ($this->providers as $provider) { $this->instances[] = $this->app->register($provider); } } /** * Get the services provided by the provider. * * @return array */ public function provides() { $provides = []; foreach ($this->providers as $provider) { $instance = $this->app->resolveProvider($provider); $provides = array_merge($provides, $instance->provides()); } return $provides; } } src/Illuminate/Support/Testing/Fakes/PendingChainFake.php 0000644 00000002632 15107327040 0017411 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; use Closure; use Illuminate\Foundation\Bus\PendingChain; use Illuminate\Queue\CallQueuedClosure; class PendingChainFake extends PendingChain { /** * The fake bus instance. * * @var \Illuminate\Support\Testing\Fakes\BusFake */ protected $bus; /** * Create a new pending chain instance. * * @param \Illuminate\Support\Testing\Fakes\BusFake $bus * @param mixed $job * @param array $chain * @return void */ public function __construct(BusFake $bus, $job, $chain) { $this->bus = $bus; $this->job = $job; $this->chain = $chain; } /** * Dispatch the job with the given arguments. * * @return \Illuminate\Foundation\Bus\PendingDispatch */ public function dispatch() { if (is_string($this->job)) { $firstJob = new $this->job(...func_get_args()); } elseif ($this->job instanceof Closure) { $firstJob = CallQueuedClosure::create($this->job); } else { $firstJob = $this->job; } $firstJob->allOnConnection($this->connection); $firstJob->allOnQueue($this->queue); $firstJob->chain($this->chain); $firstJob->delay($this->delay); $firstJob->chainCatchCallbacks = $this->catchCallbacks(); return $this->bus->dispatch($firstJob); } } src/Illuminate/Support/Testing/Fakes/PendingBatchFake.php 0000644 00000002041 15107327040 0017402 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; use Illuminate\Bus\PendingBatch; use Illuminate\Support\Collection; class PendingBatchFake extends PendingBatch { /** * The fake bus instance. * * @var \Illuminate\Support\Testing\Fakes\BusFake */ protected $bus; /** * Create a new pending batch instance. * * @param \Illuminate\Support\Testing\Fakes\BusFake $bus * @param \Illuminate\Support\Collection $jobs * @return void */ public function __construct(BusFake $bus, Collection $jobs) { $this->bus = $bus; $this->jobs = $jobs; } /** * Dispatch the batch. * * @return \Illuminate\Bus\Batch */ public function dispatch() { return $this->bus->recordPendingBatch($this); } /** * Dispatch the batch after the response is sent to the browser. * * @return \Illuminate\Bus\Batch */ public function dispatchAfterResponse() { return $this->bus->recordPendingBatch($this); } } src/Illuminate/Support/Testing/Fakes/BatchFake.php 0000644 00000007502 15107327040 0016104 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; use Carbon\CarbonImmutable; use Illuminate\Bus\Batch; use Illuminate\Bus\UpdatedBatchJobCounts; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; class BatchFake extends Batch { /** * The jobs that have been added to the batch. * * @var array */ public $added = []; /** * Indicates if the batch has been deleted. * * @var bool */ public $deleted = false; /** * Create a new batch instance. * * @param string $id * @param string $name * @param int $totalJobs * @param int $pendingJobs * @param int $failedJobs * @param array $failedJobIds * @param array $options * @param \Carbon\CarbonImmutable $createdAt * @param \Carbon\CarbonImmutable|null $cancelledAt * @param \Carbon\CarbonImmutable|null $finishedAt * @return void */ public function __construct(string $id, string $name, int $totalJobs, int $pendingJobs, int $failedJobs, array $failedJobIds, array $options, CarbonImmutable $createdAt, ?CarbonImmutable $cancelledAt = null, ?CarbonImmutable $finishedAt = null) { $this->id = $id; $this->name = $name; $this->totalJobs = $totalJobs; $this->pendingJobs = $pendingJobs; $this->failedJobs = $failedJobs; $this->failedJobIds = $failedJobIds; $this->options = $options; $this->createdAt = $createdAt; $this->cancelledAt = $cancelledAt; $this->finishedAt = $finishedAt; } /** * Get a fresh instance of the batch represented by this ID. * * @return self */ public function fresh() { return $this; } /** * Add additional jobs to the batch. * * @param \Illuminate\Support\Enumerable|object|array $jobs * @return self */ public function add($jobs) { $jobs = Collection::wrap($jobs); foreach ($jobs as $job) { $this->added[] = $job; } return $this; } /** * Record that a job within the batch finished successfully, executing any callbacks if necessary. * * @param string $jobId * @return void */ public function recordSuccessfulJob(string $jobId) { // } /** * Decrement the pending jobs for the batch. * * @param string $jobId * @return \Illuminate\Bus\UpdatedBatchJobCounts */ public function decrementPendingJobs(string $jobId) { // } /** * Record that a job within the batch failed to finish successfully, executing any callbacks if necessary. * * @param string $jobId * @param \Throwable $e * @return void */ public function recordFailedJob(string $jobId, $e) { // } /** * Increment the failed jobs for the batch. * * @param string $jobId * @return \Illuminate\Bus\UpdatedBatchJobCounts */ public function incrementFailedJobs(string $jobId) { return new UpdatedBatchJobCounts; } /** * Cancel the batch. * * @return void */ public function cancel() { $this->cancelledAt = Carbon::now(); } /** * Delete the batch from storage. * * @return void */ public function delete() { $this->deleted = true; } /** * Determine if the batch has been deleted. * * @return bool */ public function deleted() { return $this->deleted; } } src/Illuminate/Support/Testing/Fakes/BusFake.php 0000644 00000057162 15107327040 0015623 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; use Closure; use Illuminate\Bus\BatchRepository; use Illuminate\Bus\PendingBatch; use Illuminate\Contracts\Bus\QueueingDispatcher; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Traits\ReflectsClosures; use PHPUnit\Framework\Assert as PHPUnit; class BusFake implements Fake, QueueingDispatcher { use ReflectsClosures; /** * The original Bus dispatcher implementation. * * @var \Illuminate\Contracts\Bus\QueueingDispatcher */ public $dispatcher; /** * The job types that should be intercepted instead of dispatched. * * @var array */ protected $jobsToFake = []; /** * The job types that should be dispatched instead of faked. * * @var array */ protected $jobsToDispatch = []; /** * The fake repository to track batched jobs. * * @var \Illuminate\Bus\BatchRepository */ protected $batchRepository; /** * The commands that have been dispatched. * * @var array */ protected $commands = []; /** * The commands that have been dispatched synchronously. * * @var array */ protected $commandsSync = []; /** * The commands that have been dispatched after the response has been sent. * * @var array */ protected $commandsAfterResponse = []; /** * The batches that have been dispatched. * * @var array */ protected $batches = []; /** * Indicates if commands should be serialized and restored when pushed to the Bus. * * @var bool */ protected bool $serializeAndRestore = false; /** * Create a new bus fake instance. * * @param \Illuminate\Contracts\Bus\QueueingDispatcher $dispatcher * @param array|string $jobsToFake * @param \Illuminate\Bus\BatchRepository|null $batchRepository * @return void */ public function __construct(QueueingDispatcher $dispatcher, $jobsToFake = [], BatchRepository $batchRepository = null) { $this->dispatcher = $dispatcher; $this->jobsToFake = Arr::wrap($jobsToFake); $this->batchRepository = $batchRepository ?: new BatchRepositoryFake; } /** * Specify the jobs that should be dispatched instead of faked. * * @param array|string $jobsToDispatch * @return $this */ public function except($jobsToDispatch) { $this->jobsToDispatch = array_merge($this->jobsToDispatch, Arr::wrap($jobsToDispatch)); return $this; } /** * Assert if a job was dispatched based on a truth-test callback. * * @param string|\Closure $command * @param callable|int|null $callback * @return void */ public function assertDispatched($command, $callback = null) { if ($command instanceof Closure) { [$command, $callback] = [$this->firstClosureParameterType($command), $command]; } if (is_numeric($callback)) { return $this->assertDispatchedTimes($command, $callback); } PHPUnit::assertTrue( $this->dispatched($command, $callback)->count() > 0 || $this->dispatchedAfterResponse($command, $callback)->count() > 0 || $this->dispatchedSync($command, $callback)->count() > 0, "The expected [{$command}] job was not dispatched." ); } /** * Assert if a job was pushed a number of times. * * @param string|\Closure $command * @param int $times * @return void */ public function assertDispatchedTimes($command, $times = 1) { $callback = null; if ($command instanceof Closure) { [$command, $callback] = [$this->firstClosureParameterType($command), $command]; } $count = $this->dispatched($command, $callback)->count() + $this->dispatchedAfterResponse($command, $callback)->count() + $this->dispatchedSync($command, $callback)->count(); PHPUnit::assertSame( $times, $count, "The expected [{$command}] job was pushed {$count} times instead of {$times} times." ); } /** * Determine if a job was dispatched based on a truth-test callback. * * @param string|\Closure $command * @param callable|null $callback * @return void */ public function assertNotDispatched($command, $callback = null) { if ($command instanceof Closure) { [$command, $callback] = [$this->firstClosureParameterType($command), $command]; } PHPUnit::assertTrue( $this->dispatched($command, $callback)->count() === 0 && $this->dispatchedAfterResponse($command, $callback)->count() === 0 && $this->dispatchedSync($command, $callback)->count() === 0, "The unexpected [{$command}] job was dispatched." ); } /** * Assert that no jobs were dispatched. * * @return void */ public function assertNothingDispatched() { PHPUnit::assertEmpty($this->commands, 'Jobs were dispatched unexpectedly.'); } /** * Assert if a job was explicitly dispatched synchronously based on a truth-test callback. * * @param string|\Closure $command * @param callable|int|null $callback * @return void */ public function assertDispatchedSync($command, $callback = null) { if ($command instanceof Closure) { [$command, $callback] = [$this->firstClosureParameterType($command), $command]; } if (is_numeric($callback)) { return $this->assertDispatchedSyncTimes($command, $callback); } PHPUnit::assertTrue( $this->dispatchedSync($command, $callback)->count() > 0, "The expected [{$command}] job was not dispatched synchronously." ); } /** * Assert if a job was pushed synchronously a number of times. * * @param string|\Closure $command * @param int $times * @return void */ public function assertDispatchedSyncTimes($command, $times = 1) { $callback = null; if ($command instanceof Closure) { [$command, $callback] = [$this->firstClosureParameterType($command), $command]; } $count = $this->dispatchedSync($command, $callback)->count(); PHPUnit::assertSame( $times, $count, "The expected [{$command}] job was synchronously pushed {$count} times instead of {$times} times." ); } /** * Determine if a job was dispatched based on a truth-test callback. * * @param string|\Closure $command * @param callable|null $callback * @return void */ public function assertNotDispatchedSync($command, $callback = null) { if ($command instanceof Closure) { [$command, $callback] = [$this->firstClosureParameterType($command), $command]; } PHPUnit::assertCount( 0, $this->dispatchedSync($command, $callback), "The unexpected [{$command}] job was dispatched synchronously." ); } /** * Assert if a job was dispatched after the response was sent based on a truth-test callback. * * @param string|\Closure $command * @param callable|int|null $callback * @return void */ public function assertDispatchedAfterResponse($command, $callback = null) { if ($command instanceof Closure) { [$command, $callback] = [$this->firstClosureParameterType($command), $command]; } if (is_numeric($callback)) { return $this->assertDispatchedAfterResponseTimes($command, $callback); } PHPUnit::assertTrue( $this->dispatchedAfterResponse($command, $callback)->count() > 0, "The expected [{$command}] job was not dispatched after sending the response." ); } /** * Assert if a job was pushed after the response was sent a number of times. * * @param string|\Closure $command * @param int $times * @return void */ public function assertDispatchedAfterResponseTimes($command, $times = 1) { $callback = null; if ($command instanceof Closure) { [$command, $callback] = [$this->firstClosureParameterType($command), $command]; } $count = $this->dispatchedAfterResponse($command, $callback)->count(); PHPUnit::assertSame( $times, $count, "The expected [{$command}] job was pushed {$count} times instead of {$times} times." ); } /** * Determine if a job was dispatched based on a truth-test callback. * * @param string|\Closure $command * @param callable|null $callback * @return void */ public function assertNotDispatchedAfterResponse($command, $callback = null) { if ($command instanceof Closure) { [$command, $callback] = [$this->firstClosureParameterType($command), $command]; } PHPUnit::assertCount( 0, $this->dispatchedAfterResponse($command, $callback), "The unexpected [{$command}] job was dispatched after sending the response." ); } /** * Assert if a chain of jobs was dispatched. * * @param array $expectedChain * @return void */ public function assertChained(array $expectedChain) { $command = $expectedChain[0]; $expectedChain = array_slice($expectedChain, 1); $callback = null; if ($command instanceof Closure) { [$command, $callback] = [$this->firstClosureParameterType($command), $command]; } elseif (! is_string($command)) { $instance = $command; $command = get_class($instance); $callback = function ($job) use ($instance) { return serialize($this->resetChainPropertiesToDefaults($job)) === serialize($instance); }; } PHPUnit::assertTrue( $this->dispatched($command, $callback)->isNotEmpty(), "The expected [{$command}] job was not dispatched." ); $this->isChainOfObjects($expectedChain) ? $this->assertDispatchedWithChainOfObjects($command, $expectedChain, $callback) : $this->assertDispatchedWithChainOfClasses($command, $expectedChain, $callback); } /** * Reset the chain properties to their default values on the job. * * @param mixed $job * @return mixed */ protected function resetChainPropertiesToDefaults($job) { return tap(clone $job, function ($job) { $job->chainConnection = null; $job->chainQueue = null; $job->chainCatchCallbacks = null; $job->chained = []; }); } /** * Assert if a job was dispatched with an empty chain based on a truth-test callback. * * @param string|\Closure $command * @param callable|null $callback * @return void */ public function assertDispatchedWithoutChain($command, $callback = null) { if ($command instanceof Closure) { [$command, $callback] = [$this->firstClosureParameterType($command), $command]; } PHPUnit::assertTrue( $this->dispatched($command, $callback)->isNotEmpty(), "The expected [{$command}] job was not dispatched." ); $this->assertDispatchedWithChainOfClasses($command, [], $callback); } /** * Assert if a job was dispatched with chained jobs based on a truth-test callback. * * @param string $command * @param array $expectedChain * @param callable|null $callback * @return void */ protected function assertDispatchedWithChainOfObjects($command, $expectedChain, $callback) { $chain = collect($expectedChain)->map(fn ($job) => serialize($job))->all(); PHPUnit::assertTrue( $this->dispatched($command, $callback)->filter( fn ($job) => $job->chained == $chain )->isNotEmpty(), 'The expected chain was not dispatched.' ); } /** * Assert if a job was dispatched with chained jobs based on a truth-test callback. * * @param string $command * @param array $expectedChain * @param callable|null $callback * @return void */ protected function assertDispatchedWithChainOfClasses($command, $expectedChain, $callback) { $matching = $this->dispatched($command, $callback)->map->chained->map(function ($chain) { return collect($chain)->map( fn ($job) => get_class(unserialize($job)) ); })->filter( fn ($chain) => $chain->all() === $expectedChain ); PHPUnit::assertTrue( $matching->isNotEmpty(), 'The expected chain was not dispatched.' ); } /** * Determine if the given chain is entirely composed of objects. * * @param array $chain * @return bool */ protected function isChainOfObjects($chain) { return ! collect($chain)->contains(fn ($job) => ! is_object($job)); } /** * Assert if a batch was dispatched based on a truth-test callback. * * @param callable $callback * @return void */ public function assertBatched(callable $callback) { PHPUnit::assertTrue( $this->batched($callback)->count() > 0, 'The expected batch was not dispatched.' ); } /** * Assert the number of batches that have been dispatched. * * @param int $count * @return void */ public function assertBatchCount($count) { PHPUnit::assertCount( $count, $this->batches, ); } /** * Assert that no batched jobs were dispatched. * * @return void */ public function assertNothingBatched() { PHPUnit::assertEmpty($this->batches, 'Batched jobs were dispatched unexpectedly.'); } /** * Get all of the jobs matching a truth-test callback. * * @param string $command * @param callable|null $callback * @return \Illuminate\Support\Collection */ public function dispatched($command, $callback = null) { if (! $this->hasDispatched($command)) { return collect(); } $callback = $callback ?: fn () => true; return collect($this->commands[$command])->filter(fn ($command) => $callback($command)); } /** * Get all of the jobs dispatched synchronously matching a truth-test callback. * * @param string $command * @param callable|null $callback * @return \Illuminate\Support\Collection */ public function dispatchedSync(string $command, $callback = null) { if (! $this->hasDispatchedSync($command)) { return collect(); } $callback = $callback ?: fn () => true; return collect($this->commandsSync[$command])->filter(fn ($command) => $callback($command)); } /** * Get all of the jobs dispatched after the response was sent matching a truth-test callback. * * @param string $command * @param callable|null $callback * @return \Illuminate\Support\Collection */ public function dispatchedAfterResponse(string $command, $callback = null) { if (! $this->hasDispatchedAfterResponse($command)) { return collect(); } $callback = $callback ?: fn () => true; return collect($this->commandsAfterResponse[$command])->filter(fn ($command) => $callback($command)); } /** * Get all of the pending batches matching a truth-test callback. * * @param callable $callback * @return \Illuminate\Support\Collection */ public function batched(callable $callback) { if (empty($this->batches)) { return collect(); } return collect($this->batches)->filter(fn ($batch) => $callback($batch)); } /** * Determine if there are any stored commands for a given class. * * @param string $command * @return bool */ public function hasDispatched($command) { return isset($this->commands[$command]) && ! empty($this->commands[$command]); } /** * Determine if there are any stored commands for a given class. * * @param string $command * @return bool */ public function hasDispatchedSync($command) { return isset($this->commandsSync[$command]) && ! empty($this->commandsSync[$command]); } /** * Determine if there are any stored commands for a given class. * * @param string $command * @return bool */ public function hasDispatchedAfterResponse($command) { return isset($this->commandsAfterResponse[$command]) && ! empty($this->commandsAfterResponse[$command]); } /** * Dispatch a command to its appropriate handler. * * @param mixed $command * @return mixed */ public function dispatch($command) { if ($this->shouldFakeJob($command)) { $this->commands[get_class($command)][] = $this->getCommandRepresentation($command); } else { return $this->dispatcher->dispatch($command); } } /** * Dispatch a command to its appropriate handler in the current process. * * Queueable jobs will be dispatched to the "sync" queue. * * @param mixed $command * @param mixed $handler * @return mixed */ public function dispatchSync($command, $handler = null) { if ($this->shouldFakeJob($command)) { $this->commandsSync[get_class($command)][] = $this->getCommandRepresentation($command); } else { return $this->dispatcher->dispatchSync($command, $handler); } } /** * Dispatch a command to its appropriate handler in the current process. * * @param mixed $command * @param mixed $handler * @return mixed */ public function dispatchNow($command, $handler = null) { if ($this->shouldFakeJob($command)) { $this->commands[get_class($command)][] = $this->getCommandRepresentation($command); } else { return $this->dispatcher->dispatchNow($command, $handler); } } /** * Dispatch a command to its appropriate handler behind a queue. * * @param mixed $command * @return mixed */ public function dispatchToQueue($command) { if ($this->shouldFakeJob($command)) { $this->commands[get_class($command)][] = $this->getCommandRepresentation($command); } else { return $this->dispatcher->dispatchToQueue($command); } } /** * Dispatch a command to its appropriate handler. * * @param mixed $command * @return mixed */ public function dispatchAfterResponse($command) { if ($this->shouldFakeJob($command)) { $this->commandsAfterResponse[get_class($command)][] = $this->getCommandRepresentation($command); } else { return $this->dispatcher->dispatch($command); } } /** * Create a new chain of queueable jobs. * * @param \Illuminate\Support\Collection|array $jobs * @return \Illuminate\Foundation\Bus\PendingChain */ public function chain($jobs) { $jobs = Collection::wrap($jobs); return new PendingChainFake($this, $jobs->shift(), $jobs->toArray()); } /** * Attempt to find the batch with the given ID. * * @param string $batchId * @return \Illuminate\Bus\Batch|null */ public function findBatch(string $batchId) { return $this->batchRepository->find($batchId); } /** * Create a new batch of queueable jobs. * * @param \Illuminate\Support\Collection|array $jobs * @return \Illuminate\Bus\PendingBatch */ public function batch($jobs) { return new PendingBatchFake($this, Collection::wrap($jobs)); } /** * Dispatch an empty job batch for testing. * * @param string $name * @return \Illuminate\Bus\Batch */ public function dispatchFakeBatch($name = '') { return $this->batch([])->name($name)->dispatch(); } /** * Record the fake pending batch dispatch. * * @param \Illuminate\Bus\PendingBatch $pendingBatch * @return \Illuminate\Bus\Batch */ public function recordPendingBatch(PendingBatch $pendingBatch) { $this->batches[] = $pendingBatch; return $this->batchRepository->store($pendingBatch); } /** * Determine if a command should be faked or actually dispatched. * * @param mixed $command * @return bool */ protected function shouldFakeJob($command) { if ($this->shouldDispatchCommand($command)) { return false; } if (empty($this->jobsToFake)) { return true; } return collect($this->jobsToFake) ->filter(function ($job) use ($command) { return $job instanceof Closure ? $job($command) : $job === get_class($command); })->isNotEmpty(); } /** * Determine if a command should be dispatched or not. * * @param mixed $command * @return bool */ protected function shouldDispatchCommand($command) { return collect($this->jobsToDispatch) ->filter(function ($job) use ($command) { return $job instanceof Closure ? $job($command) : $job === get_class($command); })->isNotEmpty(); } /** * Specify if commands should be serialized and restored when being batched. * * @param bool $serializeAndRestore * @return $this */ public function serializeAndRestore(bool $serializeAndRestore = true) { $this->serializeAndRestore = $serializeAndRestore; return $this; } /** * Serialize and unserialize the command to simulate the queueing process. * * @param mixed $command * @return mixed */ protected function serializeAndRestoreCommand($command) { return unserialize(serialize($command)); } /** * Return the command representation that should be stored. * * @param mixed $command * @return mixed */ protected function getCommandRepresentation($command) { return $this->serializeAndRestore ? $this->serializeAndRestoreCommand($command) : $command; } /** * Set the pipes commands should be piped through before dispatching. * * @param array $pipes * @return $this */ public function pipeThrough(array $pipes) { $this->dispatcher->pipeThrough($pipes); return $this; } /** * Determine if the given command has a handler. * * @param mixed $command * @return bool */ public function hasCommandHandler($command) { return $this->dispatcher->hasCommandHandler($command); } /** * Retrieve the handler for a command. * * @param mixed $command * @return mixed */ public function getCommandHandler($command) { return $this->dispatcher->getCommandHandler($command); } /** * Map a command to a handler. * * @param array $map * @return $this */ public function map(array $map) { $this->dispatcher->map($map); return $this; } } src/Illuminate/Support/Testing/Fakes/MailFake.php 0000644 00000032100 15107327040 0015735 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; use Closure; use Illuminate\Contracts\Mail\Factory; use Illuminate\Contracts\Mail\Mailable; use Illuminate\Contracts\Mail\Mailer; use Illuminate\Contracts\Mail\MailQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\MailManager; use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\ReflectsClosures; use PHPUnit\Framework\Assert as PHPUnit; class MailFake implements Factory, Fake, Mailer, MailQueue { use ForwardsCalls, ReflectsClosures; /** * The mailer instance. * * @var MailManager */ public $manager; /** * The mailer currently being used to send a message. * * @var string */ protected $currentMailer; /** * All of the mailables that have been sent. * * @var array */ protected $mailables = []; /** * All of the mailables that have been queued. * * @var array */ protected $queuedMailables = []; /** * Create a new mail fake. * * @param MailManager $manager * @return void */ public function __construct(MailManager $manager) { $this->manager = $manager; } /** * Assert if a mailable was sent based on a truth-test callback. * * @param string|\Closure $mailable * @param callable|int|null $callback * @return void */ public function assertSent($mailable, $callback = null) { [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback); if (is_numeric($callback)) { return $this->assertSentTimes($mailable, $callback); } $message = "The expected [{$mailable}] mailable was not sent."; if (count($this->queuedMailables) > 0) { $message .= ' Did you mean to use assertQueued() instead?'; } PHPUnit::assertTrue( $this->sent($mailable, $callback)->count() > 0, $message ); } /** * Assert if a mailable was sent a number of times. * * @param string $mailable * @param int $times * @return void */ protected function assertSentTimes($mailable, $times = 1) { $count = $this->sent($mailable)->count(); PHPUnit::assertSame( $times, $count, "The expected [{$mailable}] mailable was sent {$count} times instead of {$times} times." ); } /** * Determine if a mailable was not sent or queued to be sent based on a truth-test callback. * * @param string|\Closure $mailable * @param callable|null $callback * @return void */ public function assertNotOutgoing($mailable, $callback = null) { $this->assertNotSent($mailable, $callback); $this->assertNotQueued($mailable, $callback); } /** * Determine if a mailable was not sent based on a truth-test callback. * * @param string|\Closure $mailable * @param callable|null $callback * @return void */ public function assertNotSent($mailable, $callback = null) { [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback); PHPUnit::assertCount( 0, $this->sent($mailable, $callback), "The unexpected [{$mailable}] mailable was sent." ); } /** * Assert that no mailables were sent or queued to be sent. * * @return void */ public function assertNothingOutgoing() { $this->assertNothingSent(); $this->assertNothingQueued(); } /** * Assert that no mailables were sent. * * @return void */ public function assertNothingSent() { $mailableNames = collect($this->mailables)->map( fn ($mailable) => get_class($mailable) )->join(', '); PHPUnit::assertEmpty($this->mailables, 'The following mailables were sent unexpectedly: '.$mailableNames); } /** * Assert if a mailable was queued based on a truth-test callback. * * @param string|\Closure $mailable * @param callable|int|null $callback * @return void */ public function assertQueued($mailable, $callback = null) { [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback); if (is_numeric($callback)) { return $this->assertQueuedTimes($mailable, $callback); } PHPUnit::assertTrue( $this->queued($mailable, $callback)->count() > 0, "The expected [{$mailable}] mailable was not queued." ); } /** * Assert if a mailable was queued a number of times. * * @param string $mailable * @param int $times * @return void */ protected function assertQueuedTimes($mailable, $times = 1) { $count = $this->queued($mailable)->count(); PHPUnit::assertSame( $times, $count, "The expected [{$mailable}] mailable was queued {$count} times instead of {$times} times." ); } /** * Determine if a mailable was not queued based on a truth-test callback. * * @param string|\Closure $mailable * @param callable|null $callback * @return void */ public function assertNotQueued($mailable, $callback = null) { [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback); PHPUnit::assertCount( 0, $this->queued($mailable, $callback), "The unexpected [{$mailable}] mailable was queued." ); } /** * Assert that no mailables were queued. * * @return void */ public function assertNothingQueued() { $mailableNames = collect($this->queuedMailables)->map( fn ($mailable) => get_class($mailable) )->join(', '); PHPUnit::assertEmpty($this->queuedMailables, 'The following mailables were queued unexpectedly: '.$mailableNames); } /** * Assert the total number of mailables that were sent. * * @param int $count * @return void */ public function assertSentCount($count) { $total = collect($this->mailables)->count(); PHPUnit::assertSame( $count, $total, "The total number of mailables sent was {$total} instead of {$count}." ); } /** * Assert the total number of mailables that were queued. * * @param int $count * @return void */ public function assertQueuedCount($count) { $total = collect($this->queuedMailables)->count(); PHPUnit::assertSame( $count, $total, "The total number of mailables queued was {$total} instead of {$count}." ); } /** * Assert the total number of mailables that were sent or queued. * * @param int $count * @return void */ public function assertOutgoingCount($count) { $total = collect($this->mailables) ->concat($this->queuedMailables) ->count(); PHPUnit::assertSame( $count, $total, "The total number of outgoing mailables was {$total} instead of {$count}." ); } /** * Get all of the mailables matching a truth-test callback. * * @param string|\Closure $mailable * @param callable|null $callback * @return \Illuminate\Support\Collection */ public function sent($mailable, $callback = null) { [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback); if (! $this->hasSent($mailable)) { return collect(); } $callback = $callback ?: fn () => true; return $this->mailablesOf($mailable)->filter(fn ($mailable) => $callback($mailable)); } /** * Determine if the given mailable has been sent. * * @param string $mailable * @return bool */ public function hasSent($mailable) { return $this->mailablesOf($mailable)->count() > 0; } /** * Get all of the queued mailables matching a truth-test callback. * * @param string|\Closure $mailable * @param callable|null $callback * @return \Illuminate\Support\Collection */ public function queued($mailable, $callback = null) { [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback); if (! $this->hasQueued($mailable)) { return collect(); } $callback = $callback ?: fn () => true; return $this->queuedMailablesOf($mailable)->filter(fn ($mailable) => $callback($mailable)); } /** * Determine if the given mailable has been queued. * * @param string $mailable * @return bool */ public function hasQueued($mailable) { return $this->queuedMailablesOf($mailable)->count() > 0; } /** * Get all of the mailed mailables for a given type. * * @param string $type * @return \Illuminate\Support\Collection */ protected function mailablesOf($type) { return collect($this->mailables)->filter(fn ($mailable) => $mailable instanceof $type); } /** * Get all of the mailed mailables for a given type. * * @param string $type * @return \Illuminate\Support\Collection */ protected function queuedMailablesOf($type) { return collect($this->queuedMailables)->filter(fn ($mailable) => $mailable instanceof $type); } /** * Get a mailer instance by name. * * @param string|null $name * @return \Illuminate\Contracts\Mail\Mailer */ public function mailer($name = null) { $this->currentMailer = $name; return $this; } /** * Begin the process of mailing a mailable class instance. * * @param mixed $users * @return \Illuminate\Mail\PendingMail */ public function to($users) { return (new PendingMailFake($this))->to($users); } /** * Begin the process of mailing a mailable class instance. * * @param mixed $users * @return \Illuminate\Mail\PendingMail */ public function cc($users) { return (new PendingMailFake($this))->cc($users); } /** * Begin the process of mailing a mailable class instance. * * @param mixed $users * @return \Illuminate\Mail\PendingMail */ public function bcc($users) { return (new PendingMailFake($this))->bcc($users); } /** * Send a new message with only a raw text part. * * @param string $text * @param \Closure|string $callback * @return void */ public function raw($text, $callback) { // } /** * Send a new message using a view. * * @param \Illuminate\Contracts\Mail\Mailable|string|array $view * @param array $data * @param \Closure|string|null $callback * @return void */ public function send($view, array $data = [], $callback = null) { if (! $view instanceof Mailable) { return; } $view->mailer($this->currentMailer); if ($view instanceof ShouldQueue) { return $this->queue($view, $data); } $this->currentMailer = null; $this->mailables[] = $view; } /** * Queue a new e-mail message for sending. * * @param \Illuminate\Contracts\Mail\Mailable|string|array $view * @param string|null $queue * @return mixed */ public function queue($view, $queue = null) { if (! $view instanceof Mailable) { return; } $view->mailer($this->currentMailer); $this->currentMailer = null; $this->queuedMailables[] = $view; } /** * Queue a new e-mail message for sending after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param \Illuminate\Contracts\Mail\Mailable|string|array $view * @param string|null $queue * @return mixed */ public function later($delay, $view, $queue = null) { $this->queue($view, $queue); } /** * Infer mailable class using reflection if a typehinted closure is passed to assertion. * * @param string|\Closure $mailable * @param callable|null $callback * @return array */ protected function prepareMailableAndCallback($mailable, $callback) { if ($mailable instanceof Closure) { return [$this->firstClosureParameterType($mailable), $mailable]; } return [$mailable, $callback]; } /** * Forget all of the resolved mailer instances. * * @return $this */ public function forgetMailers() { $this->currentMailer = null; return $this; } /** * Handle dynamic method calls to the mailer. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->forwardCallTo($this->manager, $method, $parameters); } } src/Illuminate/Support/Testing/Fakes/QueueFake.php 0000644 00000034626 15107327040 0016156 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; use BadMethodCallException; use Closure; use Illuminate\Contracts\Queue\Queue; use Illuminate\Queue\CallQueuedClosure; use Illuminate\Queue\QueueManager; use Illuminate\Support\Collection; use Illuminate\Support\Traits\ReflectsClosures; use PHPUnit\Framework\Assert as PHPUnit; class QueueFake extends QueueManager implements Fake, Queue { use ReflectsClosures; /** * The original queue manager. * * @var \Illuminate\Contracts\Queue\Queue */ public $queue; /** * The job types that should be intercepted instead of pushed to the queue. * * @var \Illuminate\Support\Collection */ protected $jobsToFake; /** * The job types that should be pushed to the queue and not intercepted. * * @var \Illuminate\Support\Collection */ protected $jobsToBeQueued; /** * All of the jobs that have been pushed. * * @var array */ protected $jobs = []; /** * Indicates if items should be serialized and restored when pushed to the queue. * * @var bool */ protected bool $serializeAndRestore = false; /** * Create a new fake queue instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @param array $jobsToFake * @param \Illuminate\Queue\QueueManager|null $queue * @return void */ public function __construct($app, $jobsToFake = [], $queue = null) { parent::__construct($app); $this->jobsToFake = Collection::wrap($jobsToFake); $this->jobsToBeQueued = Collection::make(); $this->queue = $queue; } /** * Specify the jobs that should be queued instead of faked. * * @param array|string $jobsToBeQueued * @return $this */ public function except($jobsToBeQueued) { $this->jobsToBeQueued = Collection::wrap($jobsToBeQueued)->merge($this->jobsToBeQueued); return $this; } /** * Assert if a job was pushed based on a truth-test callback. * * @param string|\Closure $job * @param callable|int|null $callback * @return void */ public function assertPushed($job, $callback = null) { if ($job instanceof Closure) { [$job, $callback] = [$this->firstClosureParameterType($job), $job]; } if (is_numeric($callback)) { return $this->assertPushedTimes($job, $callback); } PHPUnit::assertTrue( $this->pushed($job, $callback)->count() > 0, "The expected [{$job}] job was not pushed." ); } /** * Assert if a job was pushed a number of times. * * @param string $job * @param int $times * @return void */ protected function assertPushedTimes($job, $times = 1) { $count = $this->pushed($job)->count(); PHPUnit::assertSame( $times, $count, "The expected [{$job}] job was pushed {$count} times instead of {$times} times." ); } /** * Assert if a job was pushed based on a truth-test callback. * * @param string $queue * @param string|\Closure $job * @param callable|null $callback * @return void */ public function assertPushedOn($queue, $job, $callback = null) { if ($job instanceof Closure) { [$job, $callback] = [$this->firstClosureParameterType($job), $job]; } $this->assertPushed($job, function ($job, $pushedQueue) use ($callback, $queue) { if ($pushedQueue !== $queue) { return false; } return $callback ? $callback(...func_get_args()) : true; }); } /** * Assert if a job was pushed with chained jobs based on a truth-test callback. * * @param string $job * @param array $expectedChain * @param callable|null $callback * @return void */ public function assertPushedWithChain($job, $expectedChain = [], $callback = null) { PHPUnit::assertTrue( $this->pushed($job, $callback)->isNotEmpty(), "The expected [{$job}] job was not pushed." ); PHPUnit::assertTrue( collect($expectedChain)->isNotEmpty(), 'The expected chain can not be empty.' ); $this->isChainOfObjects($expectedChain) ? $this->assertPushedWithChainOfObjects($job, $expectedChain, $callback) : $this->assertPushedWithChainOfClasses($job, $expectedChain, $callback); } /** * Assert if a job was pushed with an empty chain based on a truth-test callback. * * @param string $job * @param callable|null $callback * @return void */ public function assertPushedWithoutChain($job, $callback = null) { PHPUnit::assertTrue( $this->pushed($job, $callback)->isNotEmpty(), "The expected [{$job}] job was not pushed." ); $this->assertPushedWithChainOfClasses($job, [], $callback); } /** * Assert if a job was pushed with chained jobs based on a truth-test callback. * * @param string $job * @param array $expectedChain * @param callable|null $callback * @return void */ protected function assertPushedWithChainOfObjects($job, $expectedChain, $callback) { $chain = collect($expectedChain)->map(fn ($job) => serialize($job))->all(); PHPUnit::assertTrue( $this->pushed($job, $callback)->filter(fn ($job) => $job->chained == $chain)->isNotEmpty(), 'The expected chain was not pushed.' ); } /** * Assert if a job was pushed with chained jobs based on a truth-test callback. * * @param string $job * @param array $expectedChain * @param callable|null $callback * @return void */ protected function assertPushedWithChainOfClasses($job, $expectedChain, $callback) { $matching = $this->pushed($job, $callback)->map->chained->map(function ($chain) { return collect($chain)->map(function ($job) { return get_class(unserialize($job)); }); })->filter(function ($chain) use ($expectedChain) { return $chain->all() === $expectedChain; }); PHPUnit::assertTrue( $matching->isNotEmpty(), 'The expected chain was not pushed.' ); } /** * Assert if a closure was pushed based on a truth-test callback. * * @param callable|int|null $callback * @return void */ public function assertClosurePushed($callback = null) { $this->assertPushed(CallQueuedClosure::class, $callback); } /** * Assert that a closure was not pushed based on a truth-test callback. * * @param callable|null $callback * @return void */ public function assertClosureNotPushed($callback = null) { $this->assertNotPushed(CallQueuedClosure::class, $callback); } /** * Determine if the given chain is entirely composed of objects. * * @param array $chain * @return bool */ protected function isChainOfObjects($chain) { return ! collect($chain)->contains(fn ($job) => ! is_object($job)); } /** * Determine if a job was pushed based on a truth-test callback. * * @param string|\Closure $job * @param callable|null $callback * @return void */ public function assertNotPushed($job, $callback = null) { if ($job instanceof Closure) { [$job, $callback] = [$this->firstClosureParameterType($job), $job]; } PHPUnit::assertCount( 0, $this->pushed($job, $callback), "The unexpected [{$job}] job was pushed." ); } /** * Assert that no jobs were pushed. * * @return void */ public function assertNothingPushed() { PHPUnit::assertEmpty($this->jobs, 'Jobs were pushed unexpectedly.'); } /** * Get all of the jobs matching a truth-test callback. * * @param string $job * @param callable|null $callback * @return \Illuminate\Support\Collection */ public function pushed($job, $callback = null) { if (! $this->hasPushed($job)) { return collect(); } $callback = $callback ?: fn () => true; return collect($this->jobs[$job])->filter( fn ($data) => $callback($data['job'], $data['queue'], $data['data']) )->pluck('job'); } /** * Determine if there are any stored jobs for a given class. * * @param string $job * @return bool */ public function hasPushed($job) { return isset($this->jobs[$job]) && ! empty($this->jobs[$job]); } /** * Resolve a queue connection instance. * * @param mixed $value * @return \Illuminate\Contracts\Queue\Queue */ public function connection($value = null) { return $this; } /** * Get the size of the queue. * * @param string|null $queue * @return int */ public function size($queue = null) { return collect($this->jobs)->flatten(1)->filter( fn ($job) => $job['queue'] === $queue )->count(); } /** * Push a new job onto the queue. * * @param string|object $job * @param mixed $data * @param string|null $queue * @return mixed */ public function push($job, $data = '', $queue = null) { if ($this->shouldFakeJob($job)) { if ($job instanceof Closure) { $job = CallQueuedClosure::create($job); } $this->jobs[is_object($job) ? get_class($job) : $job][] = [ 'job' => $this->serializeAndRestore ? $this->serializeAndRestoreJob($job) : $job, 'queue' => $queue, 'data' => $data, ]; } else { is_object($job) && isset($job->connection) ? $this->queue->connection($job->connection)->push($job, $data, $queue) : $this->queue->push($job, $data, $queue); } } /** * Determine if a job should be faked or actually dispatched. * * @param object $job * @return bool */ public function shouldFakeJob($job) { if ($this->shouldDispatchJob($job)) { return false; } if ($this->jobsToFake->isEmpty()) { return true; } return $this->jobsToFake->contains( fn ($jobToFake) => $job instanceof ((string) $jobToFake) || $job === (string) $jobToFake ); } /** * Determine if a job should be pushed to the queue instead of faked. * * @param object $job * @return bool */ protected function shouldDispatchJob($job) { if ($this->jobsToBeQueued->isEmpty()) { return false; } return $this->jobsToBeQueued->contains( fn ($jobToQueue) => $job instanceof ((string) $jobToQueue) ); } /** * Push a raw payload onto the queue. * * @param string $payload * @param string|null $queue * @param array $options * @return mixed */ public function pushRaw($payload, $queue = null, array $options = []) { // } /** * Push a new job onto the queue after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @param string|object $job * @param mixed $data * @param string|null $queue * @return mixed */ public function later($delay, $job, $data = '', $queue = null) { return $this->push($job, $data, $queue); } /** * Push a new job onto the queue. * * @param string $queue * @param string|object $job * @param mixed $data * @return mixed */ public function pushOn($queue, $job, $data = '') { return $this->push($job, $data, $queue); } /** * Push a new job onto a specific queue after (n) seconds. * * @param string $queue * @param \DateTimeInterface|\DateInterval|int $delay * @param string|object $job * @param mixed $data * @return mixed */ public function laterOn($queue, $delay, $job, $data = '') { return $this->push($job, $data, $queue); } /** * Pop the next job off of the queue. * * @param string|null $queue * @return \Illuminate\Contracts\Queue\Job|null */ public function pop($queue = null) { // } /** * Push an array of jobs onto the queue. * * @param array $jobs * @param mixed $data * @param string|null $queue * @return mixed */ public function bulk($jobs, $data = '', $queue = null) { foreach ($jobs as $job) { $this->push($job, $data, $queue); } } /** * Get the jobs that have been pushed. * * @return array */ public function pushedJobs() { return $this->jobs; } /** * Specify if jobs should be serialized and restored when being "pushed" to the queue. * * @param bool $serializeAndRestore * @return $this */ public function serializeAndRestore(bool $serializeAndRestore = true) { $this->serializeAndRestore = $serializeAndRestore; return $this; } /** * Serialize and unserialize the job to simulate the queueing process. * * @param mixed $job * @return mixed */ protected function serializeAndRestoreJob($job) { return unserialize(serialize($job)); } /** * Get the connection name for the queue. * * @return string */ public function getConnectionName() { // } /** * Set the connection name for the queue. * * @param string $name * @return $this */ public function setConnectionName($name) { return $this; } /** * Override the QueueManager to prevent circular dependency. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public function __call($method, $parameters) { throw new BadMethodCallException(sprintf( 'Call to undefined method %s::%s()', static::class, $method )); } } src/Illuminate/Support/Testing/Fakes/PendingMailFake.php 0000644 00000001644 15107327040 0017253 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; use Illuminate\Contracts\Mail\Mailable; use Illuminate\Mail\PendingMail; class PendingMailFake extends PendingMail { /** * Create a new instance. * * @param \Illuminate\Support\Testing\Fakes\MailFake $mailer * @return void */ public function __construct($mailer) { $this->mailer = $mailer; } /** * Send a new mailable message instance. * * @param \Illuminate\Contracts\Mail\Mailable $mailable * @return void */ public function send(Mailable $mailable) { $this->mailer->send($this->fill($mailable)); } /** * Push the given mailable onto the queue. * * @param \Illuminate\Contracts\Mail\Mailable $mailable * @return mixed */ public function queue(Mailable $mailable) { return $this->mailer->queue($this->fill($mailable)); } } src/Illuminate/Support/Testing/Fakes/Fake.php 0000644 00000000116 15107327040 0015134 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; interface Fake { // } src/Illuminate/Support/Testing/Fakes/EventFake.php 0000644 00000025520 15107327040 0016144 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; use Closure; use Illuminate\Container\Container; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Events\ShouldDispatchAfterCommit; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\ReflectsClosures; use PHPUnit\Framework\Assert as PHPUnit; use ReflectionFunction; class EventFake implements Dispatcher, Fake { use ForwardsCalls, ReflectsClosures; /** * The original event dispatcher. * * @var \Illuminate\Contracts\Events\Dispatcher */ public $dispatcher; /** * The event types that should be intercepted instead of dispatched. * * @var array */ protected $eventsToFake = []; /** * The event types that should be dispatched instead of intercepted. * * @var array */ protected $eventsToDispatch = []; /** * All of the events that have been intercepted keyed by type. * * @var array */ protected $events = []; /** * Create a new event fake instance. * * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher * @param array|string $eventsToFake * @return void */ public function __construct(Dispatcher $dispatcher, $eventsToFake = []) { $this->dispatcher = $dispatcher; $this->eventsToFake = Arr::wrap($eventsToFake); } /** * Specify the events that should be dispatched instead of faked. * * @param array|string $eventsToDispatch * @return $this */ public function except($eventsToDispatch) { $this->eventsToDispatch = array_merge( $this->eventsToDispatch, Arr::wrap($eventsToDispatch) ); return $this; } /** * Assert if an event has a listener attached to it. * * @param string $expectedEvent * @param string|array $expectedListener * @return void */ public function assertListening($expectedEvent, $expectedListener) { foreach ($this->dispatcher->getListeners($expectedEvent) as $listenerClosure) { $actualListener = (new ReflectionFunction($listenerClosure)) ->getStaticVariables()['listener']; $normalizedListener = $expectedListener; if (is_string($actualListener) && Str::contains($actualListener, '@')) { $actualListener = Str::parseCallback($actualListener); if (is_string($expectedListener)) { if (Str::contains($expectedListener, '@')) { $normalizedListener = Str::parseCallback($expectedListener); } else { $normalizedListener = [ $expectedListener, method_exists($expectedListener, 'handle') ? 'handle' : '__invoke', ]; } } } if ($actualListener === $normalizedListener || ($actualListener instanceof Closure && $normalizedListener === Closure::class)) { PHPUnit::assertTrue(true); return; } } PHPUnit::assertTrue( false, sprintf( 'Event [%s] does not have the [%s] listener attached to it', $expectedEvent, print_r($expectedListener, true) ) ); } /** * Assert if an event was dispatched based on a truth-test callback. * * @param string|\Closure $event * @param callable|int|null $callback * @return void */ public function assertDispatched($event, $callback = null) { if ($event instanceof Closure) { [$event, $callback] = [$this->firstClosureParameterType($event), $event]; } if (is_int($callback)) { return $this->assertDispatchedTimes($event, $callback); } PHPUnit::assertTrue( $this->dispatched($event, $callback)->count() > 0, "The expected [{$event}] event was not dispatched." ); } /** * Assert if an event was dispatched a number of times. * * @param string $event * @param int $times * @return void */ public function assertDispatchedTimes($event, $times = 1) { $count = $this->dispatched($event)->count(); PHPUnit::assertSame( $times, $count, "The expected [{$event}] event was dispatched {$count} times instead of {$times} times." ); } /** * Determine if an event was dispatched based on a truth-test callback. * * @param string|\Closure $event * @param callable|null $callback * @return void */ public function assertNotDispatched($event, $callback = null) { if ($event instanceof Closure) { [$event, $callback] = [$this->firstClosureParameterType($event), $event]; } PHPUnit::assertCount( 0, $this->dispatched($event, $callback), "The unexpected [{$event}] event was dispatched." ); } /** * Assert that no events were dispatched. * * @return void */ public function assertNothingDispatched() { $count = count(Arr::flatten($this->events)); PHPUnit::assertSame( 0, $count, "{$count} unexpected events were dispatched." ); } /** * Get all of the events matching a truth-test callback. * * @param string $event * @param callable|null $callback * @return \Illuminate\Support\Collection */ public function dispatched($event, $callback = null) { if (! $this->hasDispatched($event)) { return collect(); } $callback = $callback ?: fn () => true; return collect($this->events[$event])->filter( fn ($arguments) => $callback(...$arguments) ); } /** * Determine if the given event has been dispatched. * * @param string $event * @return bool */ public function hasDispatched($event) { return isset($this->events[$event]) && ! empty($this->events[$event]); } /** * Register an event listener with the dispatcher. * * @param \Closure|string|array $events * @param mixed $listener * @return void */ public function listen($events, $listener = null) { $this->dispatcher->listen($events, $listener); } /** * Determine if a given event has listeners. * * @param string $eventName * @return bool */ public function hasListeners($eventName) { return $this->dispatcher->hasListeners($eventName); } /** * Register an event and payload to be dispatched later. * * @param string $event * @param array $payload * @return void */ public function push($event, $payload = []) { // } /** * Register an event subscriber with the dispatcher. * * @param object|string $subscriber * @return void */ public function subscribe($subscriber) { $this->dispatcher->subscribe($subscriber); } /** * Flush a set of pushed events. * * @param string $event * @return void */ public function flush($event) { // } /** * Fire an event and call the listeners. * * @param string|object $event * @param mixed $payload * @param bool $halt * @return array|null */ public function dispatch($event, $payload = [], $halt = false) { $name = is_object($event) ? get_class($event) : (string) $event; if ($this->shouldFakeEvent($name, $payload)) { $this->fakeEvent($event, $name, func_get_args()); } else { return $this->dispatcher->dispatch($event, $payload, $halt); } } /** * Determine if an event should be faked or actually dispatched. * * @param string $eventName * @param mixed $payload * @return bool */ protected function shouldFakeEvent($eventName, $payload) { if ($this->shouldDispatchEvent($eventName, $payload)) { return false; } if (empty($this->eventsToFake)) { return true; } return collect($this->eventsToFake) ->filter(function ($event) use ($eventName, $payload) { return $event instanceof Closure ? $event($eventName, $payload) : $event === $eventName; }) ->isNotEmpty(); } /** * Push the event onto the fake events array immediately or after the next database transaction. * * @param string|object $event * @param string $name * @param array $arguments * @return void */ protected function fakeEvent($event, $name, $arguments) { if ($event instanceof ShouldDispatchAfterCommit && Container::getInstance()->bound('db.transactions')) { return Container::getInstance()->make('db.transactions') ->addCallback(fn () => $this->events[$name][] = $arguments); } $this->events[$name][] = $arguments; } /** * Determine whether an event should be dispatched or not. * * @param string $eventName * @param mixed $payload * @return bool */ protected function shouldDispatchEvent($eventName, $payload) { if (empty($this->eventsToDispatch)) { return false; } return collect($this->eventsToDispatch) ->filter(function ($event) use ($eventName, $payload) { return $event instanceof Closure ? $event($eventName, $payload) : $event === $eventName; }) ->isNotEmpty(); } /** * Remove a set of listeners from the dispatcher. * * @param string $event * @return void */ public function forget($event) { // } /** * Forget all of the queued listeners. * * @return void */ public function forgetPushed() { // } /** * Dispatch an event and call the listeners. * * @param string|object $event * @param mixed $payload * @return mixed */ public function until($event, $payload = []) { return $this->dispatch($event, $payload, true); } /** * Handle dynamic method calls to the dispatcher. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->forwardCallTo($this->dispatcher, $method, $parameters); } } src/Illuminate/Support/Testing/Fakes/NotificationFake.php 0000644 00000024550 15107327040 0017513 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; use Closure; use Exception; use Illuminate\Contracts\Notifications\Dispatcher as NotificationDispatcher; use Illuminate\Contracts\Notifications\Factory as NotificationFactory; use Illuminate\Contracts\Translation\HasLocalePreference; use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use Illuminate\Support\Traits\ReflectsClosures; use PHPUnit\Framework\Assert as PHPUnit; class NotificationFake implements Fake, NotificationDispatcher, NotificationFactory { use Macroable, ReflectsClosures; /** * All of the notifications that have been sent. * * @var array */ protected $notifications = []; /** * Locale used when sending notifications. * * @var string|null */ public $locale; /** * Assert if a notification was sent on-demand based on a truth-test callback. * * @param string|\Closure $notification * @param callable|null $callback * @return void * * @throws \Exception */ public function assertSentOnDemand($notification, $callback = null) { $this->assertSentTo(new AnonymousNotifiable, $notification, $callback); } /** * Assert if a notification was sent based on a truth-test callback. * * @param mixed $notifiable * @param string|\Closure $notification * @param callable|null $callback * @return void * * @throws \Exception */ public function assertSentTo($notifiable, $notification, $callback = null) { if (is_array($notifiable) || $notifiable instanceof Collection) { if (count($notifiable) === 0) { throw new Exception('No notifiable given.'); } foreach ($notifiable as $singleNotifiable) { $this->assertSentTo($singleNotifiable, $notification, $callback); } return; } if ($notification instanceof Closure) { [$notification, $callback] = [$this->firstClosureParameterType($notification), $notification]; } if (is_numeric($callback)) { return $this->assertSentToTimes($notifiable, $notification, $callback); } PHPUnit::assertTrue( $this->sent($notifiable, $notification, $callback)->count() > 0, "The expected [{$notification}] notification was not sent." ); } /** * Assert if a notification was sent on-demand a number of times. * * @param string $notification * @param int $times * @return void */ public function assertSentOnDemandTimes($notification, $times = 1) { return $this->assertSentToTimes(new AnonymousNotifiable, $notification, $times); } /** * Assert if a notification was sent a number of times. * * @param mixed $notifiable * @param string $notification * @param int $times * @return void */ public function assertSentToTimes($notifiable, $notification, $times = 1) { $count = $this->sent($notifiable, $notification)->count(); PHPUnit::assertSame( $times, $count, "Expected [{$notification}] to be sent {$times} times, but was sent {$count} times." ); } /** * Determine if a notification was sent based on a truth-test callback. * * @param mixed $notifiable * @param string|\Closure $notification * @param callable|null $callback * @return void * * @throws \Exception */ public function assertNotSentTo($notifiable, $notification, $callback = null) { if (is_array($notifiable) || $notifiable instanceof Collection) { if (count($notifiable) === 0) { throw new Exception('No notifiable given.'); } foreach ($notifiable as $singleNotifiable) { $this->assertNotSentTo($singleNotifiable, $notification, $callback); } return; } if ($notification instanceof Closure) { [$notification, $callback] = [$this->firstClosureParameterType($notification), $notification]; } PHPUnit::assertCount( 0, $this->sent($notifiable, $notification, $callback), "The unexpected [{$notification}] notification was sent." ); } /** * Assert that no notifications were sent. * * @return void */ public function assertNothingSent() { PHPUnit::assertEmpty($this->notifications, 'Notifications were sent unexpectedly.'); } /** * Assert that no notifications were sent to the given notifiable. * * @param mixed $notifiable * @return void * * @throws \Exception */ public function assertNothingSentTo($notifiable) { if (is_array($notifiable) || $notifiable instanceof Collection) { if (count($notifiable) === 0) { throw new Exception('No notifiable given.'); } foreach ($notifiable as $singleNotifiable) { $this->assertNothingSentTo($singleNotifiable); } return; } PHPUnit::assertEmpty( $this->notifications[get_class($notifiable)][$notifiable->getKey()] ?? [], 'Notifications were sent unexpectedly.', ); } /** * Assert the total amount of times a notification was sent. * * @param string $notification * @param int $expectedCount * @return void */ public function assertSentTimes($notification, $expectedCount) { $actualCount = collect($this->notifications) ->flatten(1) ->reduce(fn ($count, $sent) => $count + count($sent[$notification] ?? []), 0); PHPUnit::assertSame( $expectedCount, $actualCount, "Expected [{$notification}] to be sent {$expectedCount} times, but was sent {$actualCount} times." ); } /** * Assert the total count of notification that were sent. * * @param int $expectedCount * @return void */ public function assertCount($expectedCount) { $actualCount = collect($this->notifications)->flatten(3)->count(); PHPUnit::assertSame( $expectedCount, $actualCount, "Expected {$expectedCount} notifications to be sent, but {$actualCount} were sent." ); } /** * Get all of the notifications matching a truth-test callback. * * @param mixed $notifiable * @param string $notification * @param callable|null $callback * @return \Illuminate\Support\Collection */ public function sent($notifiable, $notification, $callback = null) { if (! $this->hasSent($notifiable, $notification)) { return collect(); } $callback = $callback ?: fn () => true; $notifications = collect($this->notificationsFor($notifiable, $notification)); return $notifications->filter( fn ($arguments) => $callback(...array_values($arguments)) )->pluck('notification'); } /** * Determine if there are more notifications left to inspect. * * @param mixed $notifiable * @param string $notification * @return bool */ public function hasSent($notifiable, $notification) { return ! empty($this->notificationsFor($notifiable, $notification)); } /** * Get all of the notifications for a notifiable entity by type. * * @param mixed $notifiable * @param string $notification * @return array */ protected function notificationsFor($notifiable, $notification) { return $this->notifications[get_class($notifiable)][$notifiable->getKey()][$notification] ?? []; } /** * Send the given notification to the given notifiable entities. * * @param \Illuminate\Support\Collection|array|mixed $notifiables * @param mixed $notification * @return void */ public function send($notifiables, $notification) { $this->sendNow($notifiables, $notification); } /** * Send the given notification immediately. * * @param \Illuminate\Support\Collection|array|mixed $notifiables * @param mixed $notification * @param array|null $channels * @return void */ public function sendNow($notifiables, $notification, array $channels = null) { if (! $notifiables instanceof Collection && ! is_array($notifiables)) { $notifiables = [$notifiables]; } foreach ($notifiables as $notifiable) { if (! $notification->id) { $notification->id = Str::uuid()->toString(); } $notifiableChannels = $channels ?: $notification->via($notifiable); if (method_exists($notification, 'shouldSend')) { $notifiableChannels = array_filter( $notifiableChannels, fn ($channel) => $notification->shouldSend($notifiable, $channel) !== false ); if (empty($notifiableChannels)) { continue; } } $this->notifications[get_class($notifiable)][$notifiable->getKey()][get_class($notification)][] = [ 'notification' => $notification, 'channels' => $notifiableChannels, 'notifiable' => $notifiable, 'locale' => $notification->locale ?? $this->locale ?? value(function () use ($notifiable) { if ($notifiable instanceof HasLocalePreference) { return $notifiable->preferredLocale(); } }), ]; } } /** * Get a channel instance by name. * * @param string|null $name * @return mixed */ public function channel($name = null) { // } /** * Set the locale of notifications. * * @param string $locale * @return $this */ public function locale($locale) { $this->locale = $locale; return $this; } /** * Get the notifications that have been sent. * * @return array */ public function sentNotifications() { return $this->notifications; } } src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php 0000644 00000006622 15107327040 0020206 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; use Carbon\CarbonImmutable; use Closure; use Illuminate\Bus\BatchRepository; use Illuminate\Bus\PendingBatch; use Illuminate\Bus\UpdatedBatchJobCounts; use Illuminate\Support\Str; class BatchRepositoryFake implements BatchRepository { /** * The batches stored in the repository. * * @var \Illuminate\Bus\Batch[] */ protected $batches = []; /** * Retrieve a list of batches. * * @param int $limit * @param mixed $before * @return \Illuminate\Bus\Batch[] */ public function get($limit, $before) { return $this->batches; } /** * Retrieve information about an existing batch. * * @param string $batchId * @return \Illuminate\Bus\Batch|null */ public function find(string $batchId) { return $this->batches[$batchId] ?? null; } /** * Store a new pending batch. * * @param \Illuminate\Bus\PendingBatch $batch * @return \Illuminate\Bus\Batch */ public function store(PendingBatch $batch) { $id = (string) Str::orderedUuid(); $this->batches[$id] = new BatchFake( $id, $batch->name, count($batch->jobs), count($batch->jobs), 0, [], $batch->options, CarbonImmutable::now(), null, null ); return $this->batches[$id]; } /** * Increment the total number of jobs within the batch. * * @param string $batchId * @param int $amount * @return void */ public function incrementTotalJobs(string $batchId, int $amount) { // } /** * Decrement the total number of pending jobs for the batch. * * @param string $batchId * @param string $jobId * @return \Illuminate\Bus\UpdatedBatchJobCounts */ public function decrementPendingJobs(string $batchId, string $jobId) { return new UpdatedBatchJobCounts; } /** * Increment the total number of failed jobs for the batch. * * @param string $batchId * @param string $jobId * @return \Illuminate\Bus\UpdatedBatchJobCounts */ public function incrementFailedJobs(string $batchId, string $jobId) { return new UpdatedBatchJobCounts; } /** * Mark the batch that has the given ID as finished. * * @param string $batchId * @return void */ public function markAsFinished(string $batchId) { if (isset($this->batches[$batchId])) { $this->batches[$batchId]->finishedAt = now(); } } /** * Cancel the batch that has the given ID. * * @param string $batchId * @return void */ public function cancel(string $batchId) { if (isset($this->batches[$batchId])) { $this->batches[$batchId]->cancel(); } } /** * Delete the batch that has the given ID. * * @param string $batchId * @return void */ public function delete(string $batchId) { unset($this->batches[$batchId]); } /** * Execute the given Closure within a storage specific transaction. * * @param \Closure $callback * @return mixed */ public function transaction(Closure $callback) { return $callback(); } } src/Illuminate/Support/Env.php 0000644 00000005726 15107327040 0012364 0 ustar 00 <?php namespace Illuminate\Support; use Dotenv\Repository\Adapter\PutenvAdapter; use Dotenv\Repository\RepositoryBuilder; use PhpOption\Option; use RuntimeException; class Env { /** * Indicates if the putenv adapter is enabled. * * @var bool */ protected static $putenv = true; /** * The environment repository instance. * * @var \Dotenv\Repository\RepositoryInterface|null */ protected static $repository; /** * Enable the putenv adapter. * * @return void */ public static function enablePutenv() { static::$putenv = true; static::$repository = null; } /** * Disable the putenv adapter. * * @return void */ public static function disablePutenv() { static::$putenv = false; static::$repository = null; } /** * Get the environment repository instance. * * @return \Dotenv\Repository\RepositoryInterface */ public static function getRepository() { if (static::$repository === null) { $builder = RepositoryBuilder::createWithDefaultAdapters(); if (static::$putenv) { $builder = $builder->addAdapter(PutenvAdapter::class); } static::$repository = $builder->immutable()->make(); } return static::$repository; } /** * Get the value of an environment variable. * * @param string $key * @param mixed $default * @return mixed */ public static function get($key, $default = null) { return self::getOption($key)->getOrCall(fn () => value($default)); } /** * Get the value of a required environment variable. * * @param string $key * @return mixed * * @throws \RuntimeException */ public static function getOrFail($key) { return self::getOption($key)->getOrThrow(new RuntimeException("Environment variable [$key] has no value.")); } /** * Get the possible option for this environment variable. * * @param string $key * @return \PhpOption\Option|\PhpOption\Some */ protected static function getOption($key) { return Option::fromValue(static::getRepository()->get($key)) ->map(function ($value) { switch (strtolower($value)) { case 'true': case '(true)': return true; case 'false': case '(false)': return false; case 'empty': case '(empty)': return ''; case 'null': case '(null)': return; } if (preg_match('/\A([\'"])(.*)\1\z/', $value, $matches)) { return $matches[2]; } return $value; }); } } src/Illuminate/Support/Lottery.php 0000644 00000013237 15107327040 0013272 0 ustar 00 <?php namespace Illuminate\Support; use RuntimeException; class Lottery { /** * The number of expected wins. * * @var int|float */ protected $chances; /** * The number of potential opportunities to win. * * @var int|null */ protected $outOf; /** * The winning callback. * * @var null|callable */ protected $winner; /** * The losing callback. * * @var null|callable */ protected $loser; /** * The factory that should be used to generate results. * * @var callable|null */ protected static $resultFactory; /** * Create a new Lottery instance. * * @param int|float $chances * @param int|null $outOf * @return void */ public function __construct($chances, $outOf = null) { if ($outOf === null && is_float($chances) && $chances > 1) { throw new RuntimeException('Float must not be greater than 1.'); } $this->chances = $chances; $this->outOf = $outOf; } /** * Create a new Lottery instance. * * @param int|float $chances * @param int|null $outOf * @return static */ public static function odds($chances, $outOf = null) { return new static($chances, $outOf); } /** * Set the winner callback. * * @param callable $callback * @return $this */ public function winner($callback) { $this->winner = $callback; return $this; } /** * Set the loser callback. * * @param callable $callback * @return $this */ public function loser($callback) { $this->loser = $callback; return $this; } /** * Run the lottery. * * @param mixed ...$args * @return mixed */ public function __invoke(...$args) { return $this->runCallback(...$args); } /** * Run the lottery. * * @param null|int $times * @return mixed */ public function choose($times = null) { if ($times === null) { return $this->runCallback(); } $results = []; for ($i = 0; $i < $times; $i++) { $results[] = $this->runCallback(); } return $results; } /** * Run the winner or loser callback, randomly. * * @param mixed ...$args * @return callable */ protected function runCallback(...$args) { return $this->wins() ? ($this->winner ?? fn () => true)(...$args) : ($this->loser ?? fn () => false)(...$args); } /** * Determine if the lottery "wins" or "loses". * * @return bool */ protected function wins() { return static::resultFactory()($this->chances, $this->outOf); } /** * The factory that determines the lottery result. * * @return callable */ protected static function resultFactory() { return static::$resultFactory ?? fn ($chances, $outOf) => $outOf === null ? random_int(0, PHP_INT_MAX) / PHP_INT_MAX <= $chances : random_int(1, $outOf) <= $chances; } /** * Force the lottery to always result in a win. * * @param callable|null $callback * @return void */ public static function alwaysWin($callback = null) { self::setResultFactory(fn () => true); if ($callback === null) { return; } $callback(); static::determineResultNormally(); } /** * Force the lottery to always result in a lose. * * @param callable|null $callback * @return void */ public static function alwaysLose($callback = null) { self::setResultFactory(fn () => false); if ($callback === null) { return; } $callback(); static::determineResultNormally(); } /** * Set the sequence that will be used to determine lottery results. * * @param array $sequence * @param callable|null $whenMissing * @return void */ public static function fix($sequence, $whenMissing = null) { return static::forceResultWithSequence($sequence, $whenMissing); } /** * Set the sequence that will be used to determine lottery results. * * @param array $sequence * @param callable|null $whenMissing * @return void */ public static function forceResultWithSequence($sequence, $whenMissing = null) { $next = 0; $whenMissing ??= function ($chances, $outOf) use (&$next) { $factoryCache = static::$resultFactory; static::$resultFactory = null; $result = static::resultFactory()($chances, $outOf); static::$resultFactory = $factoryCache; $next++; return $result; }; static::setResultFactory(function ($chances, $outOf) use (&$next, $sequence, $whenMissing) { if (array_key_exists($next, $sequence)) { return $sequence[$next++]; } return $whenMissing($chances, $outOf); }); } /** * Indicate that the lottery results should be determined normally. * * @return void */ public static function determineResultNormally() { static::$resultFactory = null; } /** * Set the factory that should be used to determine the lottery results. * * @param callable $factory * @return void */ public static function setResultFactory($factory) { self::$resultFactory = $factory; } } src/Illuminate/Support/Manager.php 0000644 00000010662 15107327040 0013201 0 ustar 00 <?php namespace Illuminate\Support; use Closure; use Illuminate\Contracts\Container\Container; use InvalidArgumentException; abstract class Manager { /** * The container instance. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * The configuration repository instance. * * @var \Illuminate\Contracts\Config\Repository */ protected $config; /** * The registered custom driver creators. * * @var array */ protected $customCreators = []; /** * The array of created "drivers". * * @var array */ protected $drivers = []; /** * Create a new manager instance. * * @param \Illuminate\Contracts\Container\Container $container * @return void */ public function __construct(Container $container) { $this->container = $container; $this->config = $container->make('config'); } /** * Get the default driver name. * * @return string */ abstract public function getDefaultDriver(); /** * Get a driver instance. * * @param string|null $driver * @return mixed * * @throws \InvalidArgumentException */ public function driver($driver = null) { $driver = $driver ?: $this->getDefaultDriver(); if (is_null($driver)) { throw new InvalidArgumentException(sprintf( 'Unable to resolve NULL driver for [%s].', static::class )); } // If the given driver has not been created before, we will create the instances // here and cache it so we can return it next time very quickly. If there is // already a driver created by this name, we'll just return that instance. if (! isset($this->drivers[$driver])) { $this->drivers[$driver] = $this->createDriver($driver); } return $this->drivers[$driver]; } /** * Create a new driver instance. * * @param string $driver * @return mixed * * @throws \InvalidArgumentException */ protected function createDriver($driver) { // First, we will determine if a custom driver creator exists for the given driver and // if it does not we will check for a creator method for the driver. Custom creator // callbacks allow developers to build their own "drivers" easily using Closures. if (isset($this->customCreators[$driver])) { return $this->callCustomCreator($driver); } $method = 'create'.Str::studly($driver).'Driver'; if (method_exists($this, $method)) { return $this->$method(); } throw new InvalidArgumentException("Driver [$driver] not supported."); } /** * Call a custom driver creator. * * @param string $driver * @return mixed */ protected function callCustomCreator($driver) { return $this->customCreators[$driver]($this->container); } /** * Register a custom driver creator Closure. * * @param string $driver * @param \Closure $callback * @return $this */ public function extend($driver, Closure $callback) { $this->customCreators[$driver] = $callback; return $this; } /** * Get all of the created "drivers". * * @return array */ public function getDrivers() { return $this->drivers; } /** * Get the container instance used by the manager. * * @return \Illuminate\Contracts\Container\Container */ public function getContainer() { return $this->container; } /** * Set the container instance used by the manager. * * @param \Illuminate\Contracts\Container\Container $container * @return $this */ public function setContainer(Container $container) { $this->container = $container; return $this; } /** * Forget all of the resolved driver instances. * * @return $this */ public function forgetDrivers() { $this->drivers = []; return $this; } /** * Dynamically call the default driver instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->driver()->$method(...$parameters); } } src/Illuminate/Log/ParsesLogConfiguration.php 0000644 00000003426 15107327040 0015323 0 ustar 00 <?php namespace Illuminate\Log; use InvalidArgumentException; use Monolog\Level; trait ParsesLogConfiguration { /** * The Log levels. * * @var array */ protected $levels = [ 'debug' => Level::Debug, 'info' => Level::Info, 'notice' => Level::Notice, 'warning' => Level::Warning, 'error' => Level::Error, 'critical' => Level::Critical, 'alert' => Level::Alert, 'emergency' => Level::Emergency, ]; /** * Get fallback log channel name. * * @return string */ abstract protected function getFallbackChannelName(); /** * Parse the string level into a Monolog constant. * * @param array $config * @return int * * @throws \InvalidArgumentException */ protected function level(array $config) { $level = $config['level'] ?? 'debug'; if (isset($this->levels[$level])) { return $this->levels[$level]; } throw new InvalidArgumentException('Invalid log level.'); } /** * Parse the action level from the given configuration. * * @param array $config * @return int * * @throws \InvalidArgumentException */ protected function actionLevel(array $config) { $level = $config['action_level'] ?? 'debug'; if (isset($this->levels[$level])) { return $this->levels[$level]; } throw new InvalidArgumentException('Invalid log action level.'); } /** * Extract the log channel from the given configuration. * * @param array $config * @return string */ protected function parseChannel(array $config) { return $config['name'] ?? $this->getFallbackChannelName(); } } src/Illuminate/Log/composer.json 0000644 00000001514 15107327040 0012701 0 ustar 00 { "name": "illuminate/log", "description": "The Illuminate Log package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/contracts": "^10.0", "illuminate/support": "^10.0", "monolog/monolog": "^3.0" }, "autoload": { "psr-4": { "Illuminate\\Log\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Log/Events/MessageLogged.php 0000644 00000001252 15107327040 0014641 0 ustar 00 <?php namespace Illuminate\Log\Events; class MessageLogged { /** * The log "level". * * @var string */ public $level; /** * The log message. * * @var string */ public $message; /** * The log context. * * @var array */ public $context; /** * Create a new event instance. * * @param string $level * @param string $message * @param array $context * @return void */ public function __construct($level, $message, array $context = []) { $this->level = $level; $this->message = $message; $this->context = $context; } } src/Illuminate/Log/LogManager.php 0000644 00000047176 15107327040 0012722 0 ustar 00 <?php namespace Illuminate\Log; use Closure; use Illuminate\Support\Str; use InvalidArgumentException; use Monolog\Formatter\LineFormatter; use Monolog\Handler\ErrorLogHandler; use Monolog\Handler\FingersCrossedHandler; use Monolog\Handler\FormattableHandlerInterface; use Monolog\Handler\HandlerInterface; use Monolog\Handler\RotatingFileHandler; use Monolog\Handler\SlackWebhookHandler; use Monolog\Handler\StreamHandler; use Monolog\Handler\SyslogHandler; use Monolog\Handler\WhatFailureGroupHandler; use Monolog\Logger as Monolog; use Monolog\Processor\ProcessorInterface; use Monolog\Processor\PsrLogMessageProcessor; use Psr\Log\LoggerInterface; use Throwable; /** * @mixin \Illuminate\Log\Logger */ class LogManager implements LoggerInterface { use ParsesLogConfiguration; /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The array of resolved channels. * * @var array */ protected $channels = []; /** * The context shared across channels and stacks. * * @var array */ protected $sharedContext = []; /** * The registered custom driver creators. * * @var array */ protected $customCreators = []; /** * The standard date format to use when writing logs. * * @var string */ protected $dateFormat = 'Y-m-d H:i:s'; /** * Create a new Log manager instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function __construct($app) { $this->app = $app; } /** * Build an on-demand log channel. * * @param array $config * @return \Psr\Log\LoggerInterface */ public function build(array $config) { unset($this->channels['ondemand']); return $this->get('ondemand', $config); } /** * Create a new, on-demand aggregate logger instance. * * @param array $channels * @param string|null $channel * @return \Psr\Log\LoggerInterface */ public function stack(array $channels, $channel = null) { return (new Logger( $this->createStackDriver(compact('channels', 'channel')), $this->app['events'] ))->withContext($this->sharedContext); } /** * Get a log channel instance. * * @param string|null $channel * @return \Psr\Log\LoggerInterface */ public function channel($channel = null) { return $this->driver($channel); } /** * Get a log driver instance. * * @param string|null $driver * @return \Psr\Log\LoggerInterface */ public function driver($driver = null) { return $this->get($this->parseDriver($driver)); } /** * Attempt to get the log from the local cache. * * @param string $name * @param array|null $config * @return \Psr\Log\LoggerInterface */ protected function get($name, ?array $config = null) { try { return $this->channels[$name] ?? with($this->resolve($name, $config), function ($logger) use ($name) { return $this->channels[$name] = $this->tap($name, new Logger($logger, $this->app['events']))->withContext($this->sharedContext); }); } catch (Throwable $e) { return tap($this->createEmergencyLogger(), function ($logger) use ($e) { $logger->emergency('Unable to create configured logger. Using emergency logger.', [ 'exception' => $e, ]); }); } } /** * Apply the configured taps for the logger. * * @param string $name * @param \Illuminate\Log\Logger $logger * @return \Illuminate\Log\Logger */ protected function tap($name, Logger $logger) { foreach ($this->configurationFor($name)['tap'] ?? [] as $tap) { [$class, $arguments] = $this->parseTap($tap); $this->app->make($class)->__invoke($logger, ...explode(',', $arguments)); } return $logger; } /** * Parse the given tap class string into a class name and arguments string. * * @param string $tap * @return array */ protected function parseTap($tap) { return str_contains($tap, ':') ? explode(':', $tap, 2) : [$tap, '']; } /** * Create an emergency log handler to avoid white screens of death. * * @return \Psr\Log\LoggerInterface */ protected function createEmergencyLogger() { $config = $this->configurationFor('emergency'); $handler = new StreamHandler( $config['path'] ?? $this->app->storagePath().'/logs/laravel.log', $this->level(['level' => 'debug']) ); return new Logger( new Monolog('laravel', $this->prepareHandlers([$handler])), $this->app['events'] ); } /** * Resolve the given log instance by name. * * @param string $name * @param array|null $config * @return \Psr\Log\LoggerInterface * * @throws \InvalidArgumentException */ protected function resolve($name, ?array $config = null) { $config ??= $this->configurationFor($name); if (is_null($config)) { throw new InvalidArgumentException("Log [{$name}] is not defined."); } if (isset($this->customCreators[$config['driver']])) { return $this->callCustomCreator($config); } $driverMethod = 'create'.ucfirst($config['driver']).'Driver'; if (method_exists($this, $driverMethod)) { return $this->{$driverMethod}($config); } throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported."); } /** * Call a custom driver creator. * * @param array $config * @return mixed */ protected function callCustomCreator(array $config) { return $this->customCreators[$config['driver']]($this->app, $config); } /** * Create a custom log driver instance. * * @param array $config * @return \Psr\Log\LoggerInterface */ protected function createCustomDriver(array $config) { $factory = is_callable($via = $config['via']) ? $via : $this->app->make($via); return $factory($config); } /** * Create an aggregate log driver instance. * * @param array $config * @return \Psr\Log\LoggerInterface */ protected function createStackDriver(array $config) { if (is_string($config['channels'])) { $config['channels'] = explode(',', $config['channels']); } $handlers = collect($config['channels'])->flatMap(function ($channel) { return $channel instanceof LoggerInterface ? $channel->getHandlers() : $this->channel($channel)->getHandlers(); })->all(); $processors = collect($config['channels'])->flatMap(function ($channel) { return $channel instanceof LoggerInterface ? $channel->getProcessors() : $this->channel($channel)->getProcessors(); })->all(); if ($config['ignore_exceptions'] ?? false) { $handlers = [new WhatFailureGroupHandler($handlers)]; } return new Monolog($this->parseChannel($config), $handlers, $processors); } /** * Create an instance of the single file log driver. * * @param array $config * @return \Psr\Log\LoggerInterface */ protected function createSingleDriver(array $config) { return new Monolog($this->parseChannel($config), [ $this->prepareHandler( new StreamHandler( $config['path'], $this->level($config), $config['bubble'] ?? true, $config['permission'] ?? null, $config['locking'] ?? false ), $config ), ], $config['replace_placeholders'] ?? false ? [new PsrLogMessageProcessor()] : []); } /** * Create an instance of the daily file log driver. * * @param array $config * @return \Psr\Log\LoggerInterface */ protected function createDailyDriver(array $config) { return new Monolog($this->parseChannel($config), [ $this->prepareHandler(new RotatingFileHandler( $config['path'], $config['days'] ?? 7, $this->level($config), $config['bubble'] ?? true, $config['permission'] ?? null, $config['locking'] ?? false ), $config), ], $config['replace_placeholders'] ?? false ? [new PsrLogMessageProcessor()] : []); } /** * Create an instance of the Slack log driver. * * @param array $config * @return \Psr\Log\LoggerInterface */ protected function createSlackDriver(array $config) { return new Monolog($this->parseChannel($config), [ $this->prepareHandler(new SlackWebhookHandler( $config['url'], $config['channel'] ?? null, $config['username'] ?? 'Laravel', $config['attachment'] ?? true, $config['emoji'] ?? ':boom:', $config['short'] ?? false, $config['context'] ?? true, $this->level($config), $config['bubble'] ?? true, $config['exclude_fields'] ?? [] ), $config), ], $config['replace_placeholders'] ?? false ? [new PsrLogMessageProcessor()] : []); } /** * Create an instance of the syslog log driver. * * @param array $config * @return \Psr\Log\LoggerInterface */ protected function createSyslogDriver(array $config) { return new Monolog($this->parseChannel($config), [ $this->prepareHandler(new SyslogHandler( Str::snake($this->app['config']['app.name'], '-'), $config['facility'] ?? LOG_USER, $this->level($config) ), $config), ], $config['replace_placeholders'] ?? false ? [new PsrLogMessageProcessor()] : []); } /** * Create an instance of the "error log" log driver. * * @param array $config * @return \Psr\Log\LoggerInterface */ protected function createErrorlogDriver(array $config) { return new Monolog($this->parseChannel($config), [ $this->prepareHandler(new ErrorLogHandler( $config['type'] ?? ErrorLogHandler::OPERATING_SYSTEM, $this->level($config) )), ], $config['replace_placeholders'] ?? false ? [new PsrLogMessageProcessor()] : []); } /** * Create an instance of any handler available in Monolog. * * @param array $config * @return \Psr\Log\LoggerInterface * * @throws \InvalidArgumentException * @throws \Illuminate\Contracts\Container\BindingResolutionException */ protected function createMonologDriver(array $config) { if (! is_a($config['handler'], HandlerInterface::class, true)) { throw new InvalidArgumentException( $config['handler'].' must be an instance of '.HandlerInterface::class ); } collect($config['processors'] ?? [])->each(function ($processor) { $processor = $processor['processor'] ?? $processor; if (! is_a($processor, ProcessorInterface::class, true)) { throw new InvalidArgumentException( $processor.' must be an instance of '.ProcessorInterface::class ); } }); $with = array_merge( ['level' => $this->level($config)], $config['with'] ?? [], $config['handler_with'] ?? [] ); $handler = $this->prepareHandler( $this->app->make($config['handler'], $with), $config ); $processors = collect($config['processors'] ?? []) ->map(fn ($processor) => $this->app->make($processor['processor'] ?? $processor, $processor['with'] ?? [])) ->toArray(); return new Monolog( $this->parseChannel($config), [$handler], $processors, ); } /** * Prepare the handlers for usage by Monolog. * * @param array $handlers * @return array */ protected function prepareHandlers(array $handlers) { foreach ($handlers as $key => $handler) { $handlers[$key] = $this->prepareHandler($handler); } return $handlers; } /** * Prepare the handler for usage by Monolog. * * @param \Monolog\Handler\HandlerInterface $handler * @param array $config * @return \Monolog\Handler\HandlerInterface */ protected function prepareHandler(HandlerInterface $handler, array $config = []) { if (isset($config['action_level'])) { $handler = new FingersCrossedHandler( $handler, $this->actionLevel($config), 0, true, $config['stop_buffering'] ?? true ); } if (! $handler instanceof FormattableHandlerInterface) { return $handler; } if (! isset($config['formatter'])) { $handler->setFormatter($this->formatter()); } elseif ($config['formatter'] !== 'default') { $handler->setFormatter($this->app->make($config['formatter'], $config['formatter_with'] ?? [])); } return $handler; } /** * Get a Monolog formatter instance. * * @return \Monolog\Formatter\FormatterInterface */ protected function formatter() { return new LineFormatter(null, $this->dateFormat, true, true, true); } /** * Share context across channels and stacks. * * @param array $context * @return $this */ public function shareContext(array $context) { foreach ($this->channels as $channel) { $channel->withContext($context); } $this->sharedContext = array_merge($this->sharedContext, $context); return $this; } /** * The context shared across channels and stacks. * * @return array */ public function sharedContext() { return $this->sharedContext; } /** * Flush the shared context. * * @return $this */ public function flushSharedContext() { $this->sharedContext = []; return $this; } /** * Get fallback log channel name. * * @return string */ protected function getFallbackChannelName() { return $this->app->bound('env') ? $this->app->environment() : 'production'; } /** * Get the log connection configuration. * * @param string $name * @return array */ protected function configurationFor($name) { return $this->app['config']["logging.channels.{$name}"]; } /** * Get the default log driver name. * * @return string|null */ public function getDefaultDriver() { return $this->app['config']['logging.default']; } /** * Set the default log driver name. * * @param string $name * @return void */ public function setDefaultDriver($name) { $this->app['config']['logging.default'] = $name; } /** * Register a custom driver creator Closure. * * @param string $driver * @param \Closure $callback * @return $this */ public function extend($driver, Closure $callback) { $this->customCreators[$driver] = $callback->bindTo($this, $this); return $this; } /** * Unset the given channel instance. * * @param string|null $driver * @return void */ public function forgetChannel($driver = null) { $driver = $this->parseDriver($driver); if (isset($this->channels[$driver])) { unset($this->channels[$driver]); } } /** * Parse the driver name. * * @param string|null $driver * @return string|null */ protected function parseDriver($driver) { $driver ??= $this->getDefaultDriver(); if ($this->app->runningUnitTests()) { $driver ??= 'null'; } return $driver; } /** * Get all of the resolved log channels. * * @return array */ public function getChannels() { return $this->channels; } /** * System is unusable. * * @param string $message * @param array $context * @return void */ public function emergency($message, array $context = []): void { $this->driver()->emergency($message, $context); } /** * Action must be taken immediately. * * Example: Entire website down, database unavailable, etc. This should * trigger the SMS alerts and wake you up. * * @param string $message * @param array $context * @return void */ public function alert($message, array $context = []): void { $this->driver()->alert($message, $context); } /** * Critical conditions. * * Example: Application component unavailable, unexpected exception. * * @param string $message * @param array $context * @return void */ public function critical($message, array $context = []): void { $this->driver()->critical($message, $context); } /** * Runtime errors that do not require immediate action but should typically * be logged and monitored. * * @param string $message * @param array $context * @return void */ public function error($message, array $context = []): void { $this->driver()->error($message, $context); } /** * Exceptional occurrences that are not errors. * * Example: Use of deprecated APIs, poor use of an API, undesirable things * that are not necessarily wrong. * * @param string $message * @param array $context * @return void */ public function warning($message, array $context = []): void { $this->driver()->warning($message, $context); } /** * Normal but significant events. * * @param string $message * @param array $context * @return void */ public function notice($message, array $context = []): void { $this->driver()->notice($message, $context); } /** * Interesting events. * * Example: User logs in, SQL logs. * * @param string $message * @param array $context * @return void */ public function info($message, array $context = []): void { $this->driver()->info($message, $context); } /** * Detailed debug information. * * @param string $message * @param array $context * @return void */ public function debug($message, array $context = []): void { $this->driver()->debug($message, $context); } /** * Logs with an arbitrary level. * * @param mixed $level * @param string $message * @param array $context * @return void */ public function log($level, $message, array $context = []): void { $this->driver()->log($level, $message, $context); } /** * Dynamically call the default driver instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->driver()->$method(...$parameters); } } src/Illuminate/Log/Logger.php 0000644 00000021316 15107327040 0012111 0 ustar 00 <?php namespace Illuminate\Log; use Closure; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Log\Events\MessageLogged; use Illuminate\Support\Traits\Conditionable; use Psr\Log\LoggerInterface; use RuntimeException; class Logger implements LoggerInterface { use Conditionable; /** * The underlying logger implementation. * * @var \Psr\Log\LoggerInterface */ protected $logger; /** * The event dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher|null */ protected $dispatcher; /** * Any context to be added to logs. * * @var array */ protected $context = []; /** * Create a new log writer instance. * * @param \Psr\Log\LoggerInterface $logger * @param \Illuminate\Contracts\Events\Dispatcher|null $dispatcher * @return void */ public function __construct(LoggerInterface $logger, Dispatcher $dispatcher = null) { $this->logger = $logger; $this->dispatcher = $dispatcher; } /** * Log an emergency message to the logs. * * @param \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message * @param array $context * @return void */ public function emergency($message, array $context = []): void { $this->writeLog(__FUNCTION__, $message, $context); } /** * Log an alert message to the logs. * * @param \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message * @param array $context * @return void */ public function alert($message, array $context = []): void { $this->writeLog(__FUNCTION__, $message, $context); } /** * Log a critical message to the logs. * * @param \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message * @param array $context * @return void */ public function critical($message, array $context = []): void { $this->writeLog(__FUNCTION__, $message, $context); } /** * Log an error message to the logs. * * @param \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message * @param array $context * @return void */ public function error($message, array $context = []): void { $this->writeLog(__FUNCTION__, $message, $context); } /** * Log a warning message to the logs. * * @param \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message * @param array $context * @return void */ public function warning($message, array $context = []): void { $this->writeLog(__FUNCTION__, $message, $context); } /** * Log a notice to the logs. * * @param \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message * @param array $context * @return void */ public function notice($message, array $context = []): void { $this->writeLog(__FUNCTION__, $message, $context); } /** * Log an informational message to the logs. * * @param \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message * @param array $context * @return void */ public function info($message, array $context = []): void { $this->writeLog(__FUNCTION__, $message, $context); } /** * Log a debug message to the logs. * * @param \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message * @param array $context * @return void */ public function debug($message, array $context = []): void { $this->writeLog(__FUNCTION__, $message, $context); } /** * Log a message to the logs. * * @param string $level * @param \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message * @param array $context * @return void */ public function log($level, $message, array $context = []): void { $this->writeLog($level, $message, $context); } /** * Dynamically pass log calls into the writer. * * @param string $level * @param \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message * @param array $context * @return void */ public function write($level, $message, array $context = []): void { $this->writeLog($level, $message, $context); } /** * Write a message to the log. * * @param string $level * @param \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message * @param array $context * @return void */ protected function writeLog($level, $message, $context): void { $this->logger->{$level}( $message = $this->formatMessage($message), $context = array_merge($this->context, $context) ); $this->fireLogEvent($level, $message, $context); } /** * Add context to all future logs. * * @param array $context * @return $this */ public function withContext(array $context = []) { $this->context = array_merge($this->context, $context); return $this; } /** * Flush the existing context array. * * @return $this */ public function withoutContext() { $this->context = []; return $this; } /** * Register a new callback handler for when a log event is triggered. * * @param \Closure $callback * @return void * * @throws \RuntimeException */ public function listen(Closure $callback) { if (! isset($this->dispatcher)) { throw new RuntimeException('Events dispatcher has not been set.'); } $this->dispatcher->listen(MessageLogged::class, $callback); } /** * Fires a log event. * * @param string $level * @param string $message * @param array $context * @return void */ protected function fireLogEvent($level, $message, array $context = []) { // If the event dispatcher is set, we will pass along the parameters to the // log listeners. These are useful for building profilers or other tools // that aggregate all of the log messages for a given "request" cycle. if (isset($this->dispatcher)) { $this->dispatcher->dispatch(new MessageLogged($level, $message, $context)); } } /** * Format the parameters for the logger. * * @param \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message * @return string */ protected function formatMessage($message) { if (is_array($message)) { return var_export($message, true); } elseif ($message instanceof Jsonable) { return $message->toJson(); } elseif ($message instanceof Arrayable) { return var_export($message->toArray(), true); } return (string) $message; } /** * Get the underlying logger implementation. * * @return \Psr\Log\LoggerInterface */ public function getLogger() { return $this->logger; } /** * Get the event dispatcher instance. * * @return \Illuminate\Contracts\Events\Dispatcher */ public function getEventDispatcher() { return $this->dispatcher; } /** * Set the event dispatcher instance. * * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher * @return void */ public function setEventDispatcher(Dispatcher $dispatcher) { $this->dispatcher = $dispatcher; } /** * Dynamically proxy method calls to the underlying logger. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->logger->{$method}(...$parameters); } } src/Illuminate/Log/LogServiceProvider.php 0000644 00000000505 15107327040 0014444 0 ustar 00 <?php namespace Illuminate\Log; use Illuminate\Support\ServiceProvider; class LogServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton('log', fn ($app) => new LogManager($app)); } } src/Illuminate/Log/LICENSE.md 0000644 00000002063 15107327040 0011563 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Pagination/PaginationState.php 0000644 00000002013 15107327040 0015325 0 ustar 00 <?php namespace Illuminate\Pagination; class PaginationState { /** * Bind the pagination state resolvers using the given application container as a base. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public static function resolveUsing($app) { Paginator::viewFactoryResolver(fn () => $app['view']); Paginator::currentPathResolver(fn () => $app['request']->url()); Paginator::currentPageResolver(function ($pageName = 'page') use ($app) { $page = $app['request']->input($pageName); if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) { return (int) $page; } return 1; }); Paginator::queryStringResolver(fn () => $app['request']->query()); CursorPaginator::currentCursorResolver(function ($cursorName = 'cursor') use ($app) { return Cursor::fromEncoded($app['request']->input($cursorName)); }); } } src/Illuminate/Pagination/UrlWindow.php 0000644 00000013155 15107327040 0014176 0 ustar 00 <?php namespace Illuminate\Pagination; use Illuminate\Contracts\Pagination\LengthAwarePaginator as PaginatorContract; class UrlWindow { /** * The paginator implementation. * * @var \Illuminate\Contracts\Pagination\LengthAwarePaginator */ protected $paginator; /** * Create a new URL window instance. * * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator * @return void */ public function __construct(PaginatorContract $paginator) { $this->paginator = $paginator; } /** * Create a new URL window instance. * * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator * @return array */ public static function make(PaginatorContract $paginator) { return (new static($paginator))->get(); } /** * Get the window of URLs to be shown. * * @return array */ public function get() { $onEachSide = $this->paginator->onEachSide; if ($this->paginator->lastPage() < ($onEachSide * 2) + 8) { return $this->getSmallSlider(); } return $this->getUrlSlider($onEachSide); } /** * Get the slider of URLs there are not enough pages to slide. * * @return array */ protected function getSmallSlider() { return [ 'first' => $this->paginator->getUrlRange(1, $this->lastPage()), 'slider' => null, 'last' => null, ]; } /** * Create a URL slider links. * * @param int $onEachSide * @return array */ protected function getUrlSlider($onEachSide) { $window = $onEachSide + 4; if (! $this->hasPages()) { return ['first' => null, 'slider' => null, 'last' => null]; } // If the current page is very close to the beginning of the page range, we will // just render the beginning of the page range, followed by the last 2 of the // links in this list, since we will not have room to create a full slider. if ($this->currentPage() <= $window) { return $this->getSliderTooCloseToBeginning($window, $onEachSide); } // If the current page is close to the ending of the page range we will just get // this first couple pages, followed by a larger window of these ending pages // since we're too close to the end of the list to create a full on slider. elseif ($this->currentPage() > ($this->lastPage() - $window)) { return $this->getSliderTooCloseToEnding($window, $onEachSide); } // If we have enough room on both sides of the current page to build a slider we // will surround it with both the beginning and ending caps, with this window // of pages in the middle providing a Google style sliding paginator setup. return $this->getFullSlider($onEachSide); } /** * Get the slider of URLs when too close to the beginning of the window. * * @param int $window * @param int $onEachSide * @return array */ protected function getSliderTooCloseToBeginning($window, $onEachSide) { return [ 'first' => $this->paginator->getUrlRange(1, $window + $onEachSide), 'slider' => null, 'last' => $this->getFinish(), ]; } /** * Get the slider of URLs when too close to the ending of the window. * * @param int $window * @param int $onEachSide * @return array */ protected function getSliderTooCloseToEnding($window, $onEachSide) { $last = $this->paginator->getUrlRange( $this->lastPage() - ($window + ($onEachSide - 1)), $this->lastPage() ); return [ 'first' => $this->getStart(), 'slider' => null, 'last' => $last, ]; } /** * Get the slider of URLs when a full slider can be made. * * @param int $onEachSide * @return array */ protected function getFullSlider($onEachSide) { return [ 'first' => $this->getStart(), 'slider' => $this->getAdjacentUrlRange($onEachSide), 'last' => $this->getFinish(), ]; } /** * Get the page range for the current page window. * * @param int $onEachSide * @return array */ public function getAdjacentUrlRange($onEachSide) { return $this->paginator->getUrlRange( $this->currentPage() - $onEachSide, $this->currentPage() + $onEachSide ); } /** * Get the starting URLs of a pagination slider. * * @return array */ public function getStart() { return $this->paginator->getUrlRange(1, 2); } /** * Get the ending URLs of a pagination slider. * * @return array */ public function getFinish() { return $this->paginator->getUrlRange( $this->lastPage() - 1, $this->lastPage() ); } /** * Determine if the underlying paginator being presented has pages to show. * * @return bool */ public function hasPages() { return $this->paginator->lastPage() > 1; } /** * Get the current page from the paginator. * * @return int */ protected function currentPage() { return $this->paginator->currentPage(); } /** * Get the last page from the paginator. * * @return int */ protected function lastPage() { return $this->paginator->lastPage(); } } src/Illuminate/Pagination/AbstractPaginator.php 0000644 00000041050 15107327040 0015647 0 ustar 00 <?php namespace Illuminate\Pagination; use Closure; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\Tappable; use Traversable; /** * @mixin \Illuminate\Support\Collection */ abstract class AbstractPaginator implements Htmlable { use ForwardsCalls, Tappable; /** * All of the items being paginated. * * @var \Illuminate\Support\Collection */ protected $items; /** * The number of items to be shown per page. * * @var int */ protected $perPage; /** * The current page being "viewed". * * @var int */ protected $currentPage; /** * The base path to assign to all URLs. * * @var string */ protected $path = '/'; /** * The query parameters to add to all URLs. * * @var array */ protected $query = []; /** * The URL fragment to add to all URLs. * * @var string|null */ protected $fragment; /** * The query string variable used to store the page. * * @var string */ protected $pageName = 'page'; /** * The number of links to display on each side of current page link. * * @var int */ public $onEachSide = 3; /** * The paginator options. * * @var array */ protected $options; /** * The current path resolver callback. * * @var \Closure */ protected static $currentPathResolver; /** * The current page resolver callback. * * @var \Closure */ protected static $currentPageResolver; /** * The query string resolver callback. * * @var \Closure */ protected static $queryStringResolver; /** * The view factory resolver callback. * * @var \Closure */ protected static $viewFactoryResolver; /** * The default pagination view. * * @var string */ public static $defaultView = 'pagination::tailwind'; /** * The default "simple" pagination view. * * @var string */ public static $defaultSimpleView = 'pagination::simple-tailwind'; /** * Determine if the given value is a valid page number. * * @param int $page * @return bool */ protected function isValidPageNumber($page) { return $page >= 1 && filter_var($page, FILTER_VALIDATE_INT) !== false; } /** * Get the URL for the previous page. * * @return string|null */ public function previousPageUrl() { if ($this->currentPage() > 1) { return $this->url($this->currentPage() - 1); } } /** * Create a range of pagination URLs. * * @param int $start * @param int $end * @return array */ public function getUrlRange($start, $end) { return collect(range($start, $end))->mapWithKeys(function ($page) { return [$page => $this->url($page)]; })->all(); } /** * Get the URL for a given page number. * * @param int $page * @return string */ public function url($page) { if ($page <= 0) { $page = 1; } // If we have any extra query string key / value pairs that need to be added // onto the URL, we will put them in query string form and then attach it // to the URL. This allows for extra information like sortings storage. $parameters = [$this->pageName => $page]; if (count($this->query) > 0) { $parameters = array_merge($this->query, $parameters); } return $this->path() .(str_contains($this->path(), '?') ? '&' : '?') .Arr::query($parameters) .$this->buildFragment(); } /** * Get / set the URL fragment to be appended to URLs. * * @param string|null $fragment * @return $this|string|null */ public function fragment($fragment = null) { if (is_null($fragment)) { return $this->fragment; } $this->fragment = $fragment; return $this; } /** * Add a set of query string values to the paginator. * * @param array|string|null $key * @param string|null $value * @return $this */ public function appends($key, $value = null) { if (is_null($key)) { return $this; } if (is_array($key)) { return $this->appendArray($key); } return $this->addQuery($key, $value); } /** * Add an array of query string values. * * @param array $keys * @return $this */ protected function appendArray(array $keys) { foreach ($keys as $key => $value) { $this->addQuery($key, $value); } return $this; } /** * Add all current query string values to the paginator. * * @return $this */ public function withQueryString() { if (isset(static::$queryStringResolver)) { return $this->appends(call_user_func(static::$queryStringResolver)); } return $this; } /** * Add a query string value to the paginator. * * @param string $key * @param string $value * @return $this */ protected function addQuery($key, $value) { if ($key !== $this->pageName) { $this->query[$key] = $value; } return $this; } /** * Build the full fragment portion of a URL. * * @return string */ protected function buildFragment() { return $this->fragment ? '#'.$this->fragment : ''; } /** * Load a set of relationships onto the mixed relationship collection. * * @param string $relation * @param array $relations * @return $this */ public function loadMorph($relation, $relations) { $this->getCollection()->loadMorph($relation, $relations); return $this; } /** * Load a set of relationship counts onto the mixed relationship collection. * * @param string $relation * @param array $relations * @return $this */ public function loadMorphCount($relation, $relations) { $this->getCollection()->loadMorphCount($relation, $relations); return $this; } /** * Get the slice of items being paginated. * * @return array */ public function items() { return $this->items->all(); } /** * Get the number of the first item in the slice. * * @return int|null */ public function firstItem() { return count($this->items) > 0 ? ($this->currentPage - 1) * $this->perPage + 1 : null; } /** * Get the number of the last item in the slice. * * @return int|null */ public function lastItem() { return count($this->items) > 0 ? $this->firstItem() + $this->count() - 1 : null; } /** * Transform each item in the slice of items using a callback. * * @param callable $callback * @return $this */ public function through(callable $callback) { $this->items->transform($callback); return $this; } /** * Get the number of items shown per page. * * @return int */ public function perPage() { return $this->perPage; } /** * Determine if there are enough items to split into multiple pages. * * @return bool */ public function hasPages() { return $this->currentPage() != 1 || $this->hasMorePages(); } /** * Determine if the paginator is on the first page. * * @return bool */ public function onFirstPage() { return $this->currentPage() <= 1; } /** * Determine if the paginator is on the last page. * * @return bool */ public function onLastPage() { return ! $this->hasMorePages(); } /** * Get the current page. * * @return int */ public function currentPage() { return $this->currentPage; } /** * Get the query string variable used to store the page. * * @return string */ public function getPageName() { return $this->pageName; } /** * Set the query string variable used to store the page. * * @param string $name * @return $this */ public function setPageName($name) { $this->pageName = $name; return $this; } /** * Set the base path to assign to all URLs. * * @param string $path * @return $this */ public function withPath($path) { return $this->setPath($path); } /** * Set the base path to assign to all URLs. * * @param string $path * @return $this */ public function setPath($path) { $this->path = $path; return $this; } /** * Set the number of links to display on each side of current page link. * * @param int $count * @return $this */ public function onEachSide($count) { $this->onEachSide = $count; return $this; } /** * Get the base path for paginator generated URLs. * * @return string|null */ public function path() { return $this->path; } /** * Resolve the current request path or return the default value. * * @param string $default * @return string */ public static function resolveCurrentPath($default = '/') { if (isset(static::$currentPathResolver)) { return call_user_func(static::$currentPathResolver); } return $default; } /** * Set the current request path resolver callback. * * @param \Closure $resolver * @return void */ public static function currentPathResolver(Closure $resolver) { static::$currentPathResolver = $resolver; } /** * Resolve the current page or return the default value. * * @param string $pageName * @param int $default * @return int */ public static function resolveCurrentPage($pageName = 'page', $default = 1) { if (isset(static::$currentPageResolver)) { return (int) call_user_func(static::$currentPageResolver, $pageName); } return $default; } /** * Set the current page resolver callback. * * @param \Closure $resolver * @return void */ public static function currentPageResolver(Closure $resolver) { static::$currentPageResolver = $resolver; } /** * Resolve the query string or return the default value. * * @param string|array|null $default * @return string */ public static function resolveQueryString($default = null) { if (isset(static::$queryStringResolver)) { return (static::$queryStringResolver)(); } return $default; } /** * Set with query string resolver callback. * * @param \Closure $resolver * @return void */ public static function queryStringResolver(Closure $resolver) { static::$queryStringResolver = $resolver; } /** * Get an instance of the view factory from the resolver. * * @return \Illuminate\Contracts\View\Factory */ public static function viewFactory() { return call_user_func(static::$viewFactoryResolver); } /** * Set the view factory resolver callback. * * @param \Closure $resolver * @return void */ public static function viewFactoryResolver(Closure $resolver) { static::$viewFactoryResolver = $resolver; } /** * Set the default pagination view. * * @param string $view * @return void */ public static function defaultView($view) { static::$defaultView = $view; } /** * Set the default "simple" pagination view. * * @param string $view * @return void */ public static function defaultSimpleView($view) { static::$defaultSimpleView = $view; } /** * Indicate that Tailwind styling should be used for generated links. * * @return void */ public static function useTailwind() { static::defaultView('pagination::tailwind'); static::defaultSimpleView('pagination::simple-tailwind'); } /** * Indicate that Bootstrap 4 styling should be used for generated links. * * @return void */ public static function useBootstrap() { static::useBootstrapFour(); } /** * Indicate that Bootstrap 3 styling should be used for generated links. * * @return void */ public static function useBootstrapThree() { static::defaultView('pagination::default'); static::defaultSimpleView('pagination::simple-default'); } /** * Indicate that Bootstrap 4 styling should be used for generated links. * * @return void */ public static function useBootstrapFour() { static::defaultView('pagination::bootstrap-4'); static::defaultSimpleView('pagination::simple-bootstrap-4'); } /** * Indicate that Bootstrap 5 styling should be used for generated links. * * @return void */ public static function useBootstrapFive() { static::defaultView('pagination::bootstrap-5'); static::defaultSimpleView('pagination::simple-bootstrap-5'); } /** * Get an iterator for the items. * * @return \ArrayIterator */ public function getIterator(): Traversable { return $this->items->getIterator(); } /** * Determine if the list of items is empty. * * @return bool */ public function isEmpty() { return $this->items->isEmpty(); } /** * Determine if the list of items is not empty. * * @return bool */ public function isNotEmpty() { return $this->items->isNotEmpty(); } /** * Get the number of items for the current page. * * @return int */ public function count(): int { return $this->items->count(); } /** * Get the paginator's underlying collection. * * @return \Illuminate\Support\Collection */ public function getCollection() { return $this->items; } /** * Set the paginator's underlying collection. * * @param \Illuminate\Support\Collection $collection * @return $this */ public function setCollection(Collection $collection) { $this->items = $collection; return $this; } /** * Get the paginator options. * * @return array */ public function getOptions() { return $this->options; } /** * Determine if the given item exists. * * @param mixed $key * @return bool */ public function offsetExists($key): bool { return $this->items->has($key); } /** * Get the item at the given offset. * * @param mixed $key * @return mixed */ public function offsetGet($key): mixed { return $this->items->get($key); } /** * Set the item at the given offset. * * @param mixed $key * @param mixed $value * @return void */ public function offsetSet($key, $value): void { $this->items->put($key, $value); } /** * Unset the item at the given key. * * @param mixed $key * @return void */ public function offsetUnset($key): void { $this->items->forget($key); } /** * Render the contents of the paginator to HTML. * * @return string */ public function toHtml() { return (string) $this->render(); } /** * Make dynamic calls into the collection. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->forwardCallTo($this->getCollection(), $method, $parameters); } /** * Render the contents of the paginator when casting to a string. * * @return string */ public function __toString() { return (string) $this->render(); } } src/Illuminate/Pagination/resources/views/semantic-ui.blade.php 0000644 00000003222 15107327040 0020671 0 ustar 00 @if ($paginator->hasPages()) <div class="ui pagination menu" role="navigation"> {{-- Previous Page Link --}} @if ($paginator->onFirstPage()) <a class="icon item disabled" aria-disabled="true" aria-label="@lang('pagination.previous')"> <i class="left chevron icon"></i> </a> @else <a class="icon item" href="{{ $paginator->previousPageUrl() }}" rel="prev" aria-label="@lang('pagination.previous')"> <i class="left chevron icon"></i> </a> @endif {{-- Pagination Elements --}} @foreach ($elements as $element) {{-- "Three Dots" Separator --}} @if (is_string($element)) <a class="icon item disabled" aria-disabled="true">{{ $element }}</a> @endif {{-- Array Of Links --}} @if (is_array($element)) @foreach ($element as $page => $url) @if ($page == $paginator->currentPage()) <a class="item active" href="{{ $url }}" aria-current="page">{{ $page }}</a> @else <a class="item" href="{{ $url }}">{{ $page }}</a> @endif @endforeach @endif @endforeach {{-- Next Page Link --}} @if ($paginator->hasMorePages()) <a class="icon item" href="{{ $paginator->nextPageUrl() }}" rel="next" aria-label="@lang('pagination.next')"> <i class="right chevron icon"></i> </a> @else <a class="icon item disabled" aria-disabled="true" aria-label="@lang('pagination.next')"> <i class="right chevron icon"></i> </a> @endif </div> @endif src/Illuminate/Pagination/resources/views/bootstrap-5.blade.php 0000644 00000010300 15107327040 0020625 0 ustar 00 @if ($paginator->hasPages()) <nav class="d-flex justify-items-center justify-content-between"> <div class="d-flex justify-content-between flex-fill d-sm-none"> <ul class="pagination"> {{-- Previous Page Link --}} @if ($paginator->onFirstPage()) <li class="page-item disabled" aria-disabled="true"> <span class="page-link">@lang('pagination.previous')</span> </li> @else <li class="page-item"> <a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev">@lang('pagination.previous')</a> </li> @endif {{-- Next Page Link --}} @if ($paginator->hasMorePages()) <li class="page-item"> <a class="page-link" href="{{ $paginator->nextPageUrl() }}" rel="next">@lang('pagination.next')</a> </li> @else <li class="page-item disabled" aria-disabled="true"> <span class="page-link">@lang('pagination.next')</span> </li> @endif </ul> </div> <div class="d-none flex-sm-fill d-sm-flex align-items-sm-center justify-content-sm-between"> <div> <p class="small text-muted"> {!! __('Showing') !!} <span class="fw-semibold">{{ $paginator->firstItem() }}</span> {!! __('to') !!} <span class="fw-semibold">{{ $paginator->lastItem() }}</span> {!! __('of') !!} <span class="fw-semibold">{{ $paginator->total() }}</span> {!! __('results') !!} </p> </div> <div> <ul class="pagination"> {{-- Previous Page Link --}} @if ($paginator->onFirstPage()) <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.previous')"> <span class="page-link" aria-hidden="true">‹</span> </li> @else <li class="page-item"> <a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev" aria-label="@lang('pagination.previous')">‹</a> </li> @endif {{-- Pagination Elements --}} @foreach ($elements as $element) {{-- "Three Dots" Separator --}} @if (is_string($element)) <li class="page-item disabled" aria-disabled="true"><span class="page-link">{{ $element }}</span></li> @endif {{-- Array Of Links --}} @if (is_array($element)) @foreach ($element as $page => $url) @if ($page == $paginator->currentPage()) <li class="page-item active" aria-current="page"><span class="page-link">{{ $page }}</span></li> @else <li class="page-item"><a class="page-link" href="{{ $url }}">{{ $page }}</a></li> @endif @endforeach @endif @endforeach {{-- Next Page Link --}} @if ($paginator->hasMorePages()) <li class="page-item"> <a class="page-link" href="{{ $paginator->nextPageUrl() }}" rel="next" aria-label="@lang('pagination.next')">›</a> </li> @else <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.next')"> <span class="page-link" aria-hidden="true">›</span> </li> @endif </ul> </div> </div> </nav> @endif src/Illuminate/Pagination/resources/views/tailwind.blade.php 0000644 00000017232 15107327040 0020274 0 ustar 00 @if ($paginator->hasPages()) <nav role="navigation" aria-label="{{ __('Pagination Navigation') }}" class="flex items-center justify-between"> <div class="flex justify-between flex-1 sm:hidden"> @if ($paginator->onFirstPage()) <span class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5 rounded-md"> {!! __('pagination.previous') !!} </span> @else <a href="{{ $paginator->previousPageUrl() }}" class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 leading-5 rounded-md hover:text-gray-500 focus:outline-none focus:ring ring-gray-300 focus:border-blue-300 active:bg-gray-100 active:text-gray-700 transition ease-in-out duration-150"> {!! __('pagination.previous') !!} </a> @endif @if ($paginator->hasMorePages()) <a href="{{ $paginator->nextPageUrl() }}" class="relative inline-flex items-center px-4 py-2 ml-3 text-sm font-medium text-gray-700 bg-white border border-gray-300 leading-5 rounded-md hover:text-gray-500 focus:outline-none focus:ring ring-gray-300 focus:border-blue-300 active:bg-gray-100 active:text-gray-700 transition ease-in-out duration-150"> {!! __('pagination.next') !!} </a> @else <span class="relative inline-flex items-center px-4 py-2 ml-3 text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5 rounded-md"> {!! __('pagination.next') !!} </span> @endif </div> <div class="hidden sm:flex-1 sm:flex sm:items-center sm:justify-between"> <div> <p class="text-sm text-gray-700 leading-5"> {!! __('Showing') !!} @if ($paginator->firstItem()) <span class="font-medium">{{ $paginator->firstItem() }}</span> {!! __('to') !!} <span class="font-medium">{{ $paginator->lastItem() }}</span> @else {{ $paginator->count() }} @endif {!! __('of') !!} <span class="font-medium">{{ $paginator->total() }}</span> {!! __('results') !!} </p> </div> <div> <span class="relative z-0 inline-flex shadow-sm rounded-md"> {{-- Previous Page Link --}} @if ($paginator->onFirstPage()) <span aria-disabled="true" aria-label="{{ __('pagination.previous') }}"> <span class="relative inline-flex items-center px-2 py-2 text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default rounded-l-md leading-5" aria-hidden="true"> <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20"> <path fill-rule="evenodd" d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd" /> </svg> </span> </span> @else <a href="{{ $paginator->previousPageUrl() }}" rel="prev" class="relative inline-flex items-center px-2 py-2 text-sm font-medium text-gray-500 bg-white border border-gray-300 rounded-l-md leading-5 hover:text-gray-400 focus:z-10 focus:outline-none focus:ring ring-gray-300 focus:border-blue-300 active:bg-gray-100 active:text-gray-500 transition ease-in-out duration-150" aria-label="{{ __('pagination.previous') }}"> <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20"> <path fill-rule="evenodd" d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd" /> </svg> </a> @endif {{-- Pagination Elements --}} @foreach ($elements as $element) {{-- "Three Dots" Separator --}} @if (is_string($element)) <span aria-disabled="true"> <span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-700 bg-white border border-gray-300 cursor-default leading-5">{{ $element }}</span> </span> @endif {{-- Array Of Links --}} @if (is_array($element)) @foreach ($element as $page => $url) @if ($page == $paginator->currentPage()) <span aria-current="page"> <span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">{{ $page }}</span> </span> @else <a href="{{ $url }}" class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-700 bg-white border border-gray-300 leading-5 hover:text-gray-500 focus:z-10 focus:outline-none focus:ring ring-gray-300 focus:border-blue-300 active:bg-gray-100 active:text-gray-700 transition ease-in-out duration-150" aria-label="{{ __('Go to page :page', ['page' => $page]) }}"> {{ $page }} </a> @endif @endforeach @endif @endforeach {{-- Next Page Link --}} @if ($paginator->hasMorePages()) <a href="{{ $paginator->nextPageUrl() }}" rel="next" class="relative inline-flex items-center px-2 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 rounded-r-md leading-5 hover:text-gray-400 focus:z-10 focus:outline-none focus:ring ring-gray-300 focus:border-blue-300 active:bg-gray-100 active:text-gray-500 transition ease-in-out duration-150" aria-label="{{ __('pagination.next') }}"> <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20"> <path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd" /> </svg> </a> @else <span aria-disabled="true" aria-label="{{ __('pagination.next') }}"> <span class="relative inline-flex items-center px-2 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default rounded-r-md leading-5" aria-hidden="true"> <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20"> <path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd" /> </svg> </span> </span> @endif </span> </div> </div> </nav> @endif src/Illuminate/Pagination/resources/views/bootstrap-4.blade.php 0000644 00000004044 15107327040 0020634 0 ustar 00 @if ($paginator->hasPages()) <nav> <ul class="pagination"> {{-- Previous Page Link --}} @if ($paginator->onFirstPage()) <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.previous')"> <span class="page-link" aria-hidden="true">‹</span> </li> @else <li class="page-item"> <a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev" aria-label="@lang('pagination.previous')">‹</a> </li> @endif {{-- Pagination Elements --}} @foreach ($elements as $element) {{-- "Three Dots" Separator --}} @if (is_string($element)) <li class="page-item disabled" aria-disabled="true"><span class="page-link">{{ $element }}</span></li> @endif {{-- Array Of Links --}} @if (is_array($element)) @foreach ($element as $page => $url) @if ($page == $paginator->currentPage()) <li class="page-item active" aria-current="page"><span class="page-link">{{ $page }}</span></li> @else <li class="page-item"><a class="page-link" href="{{ $url }}">{{ $page }}</a></li> @endif @endforeach @endif @endforeach {{-- Next Page Link --}} @if ($paginator->hasMorePages()) <li class="page-item"> <a class="page-link" href="{{ $paginator->nextPageUrl() }}" rel="next" aria-label="@lang('pagination.next')">›</a> </li> @else <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.next')"> <span class="page-link" aria-hidden="true">›</span> </li> @endif </ul> </nav> @endif src/Illuminate/Pagination/resources/views/default.blade.php 0000644 00000003510 15107327040 0020077 0 ustar 00 @if ($paginator->hasPages()) <nav> <ul class="pagination"> {{-- Previous Page Link --}} @if ($paginator->onFirstPage()) <li class="disabled" aria-disabled="true" aria-label="@lang('pagination.previous')"> <span aria-hidden="true">‹</span> </li> @else <li> <a href="{{ $paginator->previousPageUrl() }}" rel="prev" aria-label="@lang('pagination.previous')">‹</a> </li> @endif {{-- Pagination Elements --}} @foreach ($elements as $element) {{-- "Three Dots" Separator --}} @if (is_string($element)) <li class="disabled" aria-disabled="true"><span>{{ $element }}</span></li> @endif {{-- Array Of Links --}} @if (is_array($element)) @foreach ($element as $page => $url) @if ($page == $paginator->currentPage()) <li class="active" aria-current="page"><span>{{ $page }}</span></li> @else <li><a href="{{ $url }}">{{ $page }}</a></li> @endif @endforeach @endif @endforeach {{-- Next Page Link --}} @if ($paginator->hasMorePages()) <li> <a href="{{ $paginator->nextPageUrl() }}" rel="next" aria-label="@lang('pagination.next')">›</a> </li> @else <li class="disabled" aria-disabled="true" aria-label="@lang('pagination.next')"> <span aria-hidden="true">›</span> </li> @endif </ul> </nav> @endif src/Illuminate/Pagination/resources/views/simple-default.blade.php 0000644 00000001405 15107327040 0021367 0 ustar 00 @if ($paginator->hasPages()) <nav> <ul class="pagination"> {{-- Previous Page Link --}} @if ($paginator->onFirstPage()) <li class="disabled" aria-disabled="true"><span>@lang('pagination.previous')</span></li> @else <li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">@lang('pagination.previous')</a></li> @endif {{-- Next Page Link --}} @if ($paginator->hasMorePages()) <li><a href="{{ $paginator->nextPageUrl() }}" rel="next">@lang('pagination.next')</a></li> @else <li class="disabled" aria-disabled="true"><span>@lang('pagination.next')</span></li> @endif </ul> </nav> @endif src/Illuminate/Pagination/resources/views/simple-tailwind.blade.php 0000644 00000003243 15107327040 0021560 0 ustar 00 @if ($paginator->hasPages()) <nav role="navigation" aria-label="Pagination Navigation" class="flex justify-between"> {{-- Previous Page Link --}} @if ($paginator->onFirstPage()) <span class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5 rounded-md"> {!! __('pagination.previous') !!} </span> @else <a href="{{ $paginator->previousPageUrl() }}" rel="prev" class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 leading-5 rounded-md hover:text-gray-500 focus:outline-none focus:ring ring-gray-300 focus:border-blue-300 active:bg-gray-100 active:text-gray-700 transition ease-in-out duration-150"> {!! __('pagination.previous') !!} </a> @endif {{-- Next Page Link --}} @if ($paginator->hasMorePages()) <a href="{{ $paginator->nextPageUrl() }}" rel="next" class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 leading-5 rounded-md hover:text-gray-500 focus:outline-none focus:ring ring-gray-300 focus:border-blue-300 active:bg-gray-100 active:text-gray-700 transition ease-in-out duration-150"> {!! __('pagination.next') !!} </a> @else <span class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5 rounded-md"> {!! __('pagination.next') !!} </span> @endif </nav> @endif src/Illuminate/Pagination/resources/views/simple-bootstrap-5.blade.php 0000644 00000002224 15107327040 0022122 0 ustar 00 @if ($paginator->hasPages()) <nav role="navigation" aria-label="Pagination Navigation"> <ul class="pagination"> {{-- Previous Page Link --}} @if ($paginator->onFirstPage()) <li class="page-item disabled" aria-disabled="true"> <span class="page-link">{!! __('pagination.previous') !!}</span> </li> @else <li class="page-item"> <a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev"> {!! __('pagination.previous') !!} </a> </li> @endif {{-- Next Page Link --}} @if ($paginator->hasMorePages()) <li class="page-item"> <a class="page-link" href="{{ $paginator->nextPageUrl() }}" rel="next">{!! __('pagination.next') !!}</a> </li> @else <li class="page-item disabled" aria-disabled="true"> <span class="page-link">{!! __('pagination.next') !!}</span> </li> @endif </ul> </nav> @endif src/Illuminate/Pagination/resources/views/simple-bootstrap-4.blade.php 0000644 00000002035 15107327040 0022121 0 ustar 00 @if ($paginator->hasPages()) <nav> <ul class="pagination"> {{-- Previous Page Link --}} @if ($paginator->onFirstPage()) <li class="page-item disabled" aria-disabled="true"> <span class="page-link">@lang('pagination.previous')</span> </li> @else <li class="page-item"> <a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev">@lang('pagination.previous')</a> </li> @endif {{-- Next Page Link --}} @if ($paginator->hasMorePages()) <li class="page-item"> <a class="page-link" href="{{ $paginator->nextPageUrl() }}" rel="next">@lang('pagination.next')</a> </li> @else <li class="page-item disabled" aria-disabled="true"> <span class="page-link">@lang('pagination.next')</span> </li> @endif </ul> </nav> @endif src/Illuminate/Pagination/composer.json 0000644 00000001604 15107327040 0014251 0 ustar 00 { "name": "illuminate/pagination", "description": "The Illuminate Pagination package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "ext-filter": "*", "illuminate/collections": "^10.0", "illuminate/contracts": "^10.0", "illuminate/support": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Pagination\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Pagination/PaginationServiceProvider.php 0000644 00000001344 15107327040 0017366 0 ustar 00 <?php namespace Illuminate\Pagination; use Illuminate\Support\ServiceProvider; class PaginationServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { $this->loadViewsFrom(__DIR__.'/resources/views', 'pagination'); if ($this->app->runningInConsole()) { $this->publishes([ __DIR__.'/resources/views' => $this->app->resourcePath('views/vendor/pagination'), ], 'laravel-pagination'); } } /** * Register the service provider. * * @return void */ public function register() { PaginationState::resolveUsing($this->app); } } src/Illuminate/Pagination/LICENSE.md 0000644 00000002063 15107327040 0013133 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Pagination/LengthAwarePaginator.php 0000644 00000014047 15107327040 0016313 0 ustar 00 <?php namespace Illuminate\Pagination; use ArrayAccess; use Countable; use Illuminate\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Support\Collection; use IteratorAggregate; use JsonSerializable; class LengthAwarePaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable, JsonSerializable, LengthAwarePaginatorContract { /** * The total number of items before slicing. * * @var int */ protected $total; /** * The last available page. * * @var int */ protected $lastPage; /** * Create a new paginator instance. * * @param mixed $items * @param int $total * @param int $perPage * @param int|null $currentPage * @param array $options (path, query, fragment, pageName) * @return void */ public function __construct($items, $total, $perPage, $currentPage = null, array $options = []) { $this->options = $options; foreach ($options as $key => $value) { $this->{$key} = $value; } $this->total = $total; $this->perPage = (int) $perPage; $this->lastPage = max((int) ceil($total / $perPage), 1); $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path; $this->currentPage = $this->setCurrentPage($currentPage, $this->pageName); $this->items = $items instanceof Collection ? $items : Collection::make($items); } /** * Get the current page for the request. * * @param int $currentPage * @param string $pageName * @return int */ protected function setCurrentPage($currentPage, $pageName) { $currentPage = $currentPage ?: static::resolveCurrentPage($pageName); return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1; } /** * Render the paginator using the given view. * * @param string|null $view * @param array $data * @return \Illuminate\Contracts\Support\Htmlable */ public function links($view = null, $data = []) { return $this->render($view, $data); } /** * Render the paginator using the given view. * * @param string|null $view * @param array $data * @return \Illuminate\Contracts\Support\Htmlable */ public function render($view = null, $data = []) { return static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [ 'paginator' => $this, 'elements' => $this->elements(), ])); } /** * Get the paginator links as a collection (for JSON responses). * * @return \Illuminate\Support\Collection */ public function linkCollection() { return collect($this->elements())->flatMap(function ($item) { if (! is_array($item)) { return [['url' => null, 'label' => '...', 'active' => false]]; } return collect($item)->map(function ($url, $page) { return [ 'url' => $url, 'label' => (string) $page, 'active' => $this->currentPage() === $page, ]; }); })->prepend([ 'url' => $this->previousPageUrl(), 'label' => function_exists('__') ? __('pagination.previous') : 'Previous', 'active' => false, ])->push([ 'url' => $this->nextPageUrl(), 'label' => function_exists('__') ? __('pagination.next') : 'Next', 'active' => false, ]); } /** * Get the array of elements to pass to the view. * * @return array */ protected function elements() { $window = UrlWindow::make($this); return array_filter([ $window['first'], is_array($window['slider']) ? '...' : null, $window['slider'], is_array($window['last']) ? '...' : null, $window['last'], ]); } /** * Get the total number of items being paginated. * * @return int */ public function total() { return $this->total; } /** * Determine if there are more items in the data source. * * @return bool */ public function hasMorePages() { return $this->currentPage() < $this->lastPage(); } /** * Get the URL for the next page. * * @return string|null */ public function nextPageUrl() { if ($this->hasMorePages()) { return $this->url($this->currentPage() + 1); } } /** * Get the last page. * * @return int */ public function lastPage() { return $this->lastPage; } /** * Get the instance as an array. * * @return array */ public function toArray() { return [ 'current_page' => $this->currentPage(), 'data' => $this->items->toArray(), 'first_page_url' => $this->url(1), 'from' => $this->firstItem(), 'last_page' => $this->lastPage(), 'last_page_url' => $this->url($this->lastPage()), 'links' => $this->linkCollection()->toArray(), 'next_page_url' => $this->nextPageUrl(), 'path' => $this->path(), 'per_page' => $this->perPage(), 'prev_page_url' => $this->previousPageUrl(), 'to' => $this->lastItem(), 'total' => $this->total(), ]; } /** * Convert the object into something JSON serializable. * * @return array */ public function jsonSerialize(): array { return $this->toArray(); } /** * Convert the object to its JSON representation. * * @param int $options * @return string */ public function toJson($options = 0) { return json_encode($this->jsonSerialize(), $options); } } src/Illuminate/Pagination/CursorPaginator.php 0000644 00000010706 15107327040 0015365 0 ustar 00 <?php namespace Illuminate\Pagination; use ArrayAccess; use Countable; use Illuminate\Contracts\Pagination\CursorPaginator as PaginatorContract; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Support\Collection; use IteratorAggregate; use JsonSerializable; class CursorPaginator extends AbstractCursorPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable, JsonSerializable, PaginatorContract { /** * Indicates whether there are more items in the data source. * * @return bool */ protected $hasMore; /** * Create a new paginator instance. * * @param mixed $items * @param int $perPage * @param \Illuminate\Pagination\Cursor|null $cursor * @param array $options (path, query, fragment, pageName) * @return void */ public function __construct($items, $perPage, $cursor = null, array $options = []) { $this->options = $options; foreach ($options as $key => $value) { $this->{$key} = $value; } $this->perPage = (int) $perPage; $this->cursor = $cursor; $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path; $this->setItems($items); } /** * Set the items for the paginator. * * @param mixed $items * @return void */ protected function setItems($items) { $this->items = $items instanceof Collection ? $items : Collection::make($items); $this->hasMore = $this->items->count() > $this->perPage; $this->items = $this->items->slice(0, $this->perPage); if (! is_null($this->cursor) && $this->cursor->pointsToPreviousItems()) { $this->items = $this->items->reverse()->values(); } } /** * Render the paginator using the given view. * * @param string|null $view * @param array $data * @return \Illuminate\Contracts\Support\Htmlable */ public function links($view = null, $data = []) { return $this->render($view, $data); } /** * Render the paginator using the given view. * * @param string|null $view * @param array $data * @return \Illuminate\Contracts\Support\Htmlable */ public function render($view = null, $data = []) { return static::viewFactory()->make($view ?: Paginator::$defaultSimpleView, array_merge($data, [ 'paginator' => $this, ])); } /** * Determine if there are more items in the data source. * * @return bool */ public function hasMorePages() { return (is_null($this->cursor) && $this->hasMore) || (! is_null($this->cursor) && $this->cursor->pointsToNextItems() && $this->hasMore) || (! is_null($this->cursor) && $this->cursor->pointsToPreviousItems()); } /** * Determine if there are enough items to split into multiple pages. * * @return bool */ public function hasPages() { return ! $this->onFirstPage() || $this->hasMorePages(); } /** * Determine if the paginator is on the first page. * * @return bool */ public function onFirstPage() { return is_null($this->cursor) || ($this->cursor->pointsToPreviousItems() && ! $this->hasMore); } /** * Determine if the paginator is on the last page. * * @return bool */ public function onLastPage() { return ! $this->hasMorePages(); } /** * Get the instance as an array. * * @return array */ public function toArray() { return [ 'data' => $this->items->toArray(), 'path' => $this->path(), 'per_page' => $this->perPage(), 'next_cursor' => $this->nextCursor()?->encode(), 'next_page_url' => $this->nextPageUrl(), 'prev_cursor' => $this->previousCursor()?->encode(), 'prev_page_url' => $this->previousPageUrl(), ]; } /** * Convert the object into something JSON serializable. * * @return array */ public function jsonSerialize(): array { return $this->toArray(); } /** * Convert the object to its JSON representation. * * @param int $options * @return string */ public function toJson($options = 0) { return json_encode($this->jsonSerialize(), $options); } } src/Illuminate/Pagination/Paginator.php 0000644 00000010426 15107327040 0014166 0 ustar 00 <?php namespace Illuminate\Pagination; use ArrayAccess; use Countable; use Illuminate\Contracts\Pagination\Paginator as PaginatorContract; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Support\Collection; use IteratorAggregate; use JsonSerializable; class Paginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable, JsonSerializable, PaginatorContract { /** * Determine if there are more items in the data source. * * @return bool */ protected $hasMore; /** * Create a new paginator instance. * * @param mixed $items * @param int $perPage * @param int|null $currentPage * @param array $options (path, query, fragment, pageName) * @return void */ public function __construct($items, $perPage, $currentPage = null, array $options = []) { $this->options = $options; foreach ($options as $key => $value) { $this->{$key} = $value; } $this->perPage = $perPage; $this->currentPage = $this->setCurrentPage($currentPage); $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path; $this->setItems($items); } /** * Get the current page for the request. * * @param int $currentPage * @return int */ protected function setCurrentPage($currentPage) { $currentPage = $currentPage ?: static::resolveCurrentPage(); return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1; } /** * Set the items for the paginator. * * @param mixed $items * @return void */ protected function setItems($items) { $this->items = $items instanceof Collection ? $items : Collection::make($items); $this->hasMore = $this->items->count() > $this->perPage; $this->items = $this->items->slice(0, $this->perPage); } /** * Get the URL for the next page. * * @return string|null */ public function nextPageUrl() { if ($this->hasMorePages()) { return $this->url($this->currentPage() + 1); } } /** * Render the paginator using the given view. * * @param string|null $view * @param array $data * @return string */ public function links($view = null, $data = []) { return $this->render($view, $data); } /** * Render the paginator using the given view. * * @param string|null $view * @param array $data * @return \Illuminate\Contracts\Support\Htmlable */ public function render($view = null, $data = []) { return static::viewFactory()->make($view ?: static::$defaultSimpleView, array_merge($data, [ 'paginator' => $this, ])); } /** * Manually indicate that the paginator does have more pages. * * @param bool $hasMore * @return $this */ public function hasMorePagesWhen($hasMore = true) { $this->hasMore = $hasMore; return $this; } /** * Determine if there are more items in the data source. * * @return bool */ public function hasMorePages() { return $this->hasMore; } /** * Get the instance as an array. * * @return array */ public function toArray() { return [ 'current_page' => $this->currentPage(), 'data' => $this->items->toArray(), 'first_page_url' => $this->url(1), 'from' => $this->firstItem(), 'next_page_url' => $this->nextPageUrl(), 'path' => $this->path(), 'per_page' => $this->perPage(), 'prev_page_url' => $this->previousPageUrl(), 'to' => $this->lastItem(), ]; } /** * Convert the object into something JSON serializable. * * @return array */ public function jsonSerialize(): array { return $this->toArray(); } /** * Convert the object to its JSON representation. * * @param int $options * @return string */ public function toJson($options = 0) { return json_encode($this->jsonSerialize(), $options); } } src/Illuminate/Pagination/AbstractCursorPaginator.php 0000644 00000035613 15107327040 0017055 0 ustar 00 <?php namespace Illuminate\Pagination; use ArrayAccess; use Closure; use Exception; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\Tappable; use Traversable; /** * @mixin \Illuminate\Support\Collection */ abstract class AbstractCursorPaginator implements Htmlable { use ForwardsCalls, Tappable; /** * All of the items being paginated. * * @var \Illuminate\Support\Collection */ protected $items; /** * The number of items to be shown per page. * * @var int */ protected $perPage; /** * The base path to assign to all URLs. * * @var string */ protected $path = '/'; /** * The query parameters to add to all URLs. * * @var array */ protected $query = []; /** * The URL fragment to add to all URLs. * * @var string|null */ protected $fragment; /** * The cursor string variable used to store the page. * * @var string */ protected $cursorName = 'cursor'; /** * The current cursor. * * @var \Illuminate\Pagination\Cursor|null */ protected $cursor; /** * The paginator parameters for the cursor. * * @var array */ protected $parameters; /** * The paginator options. * * @var array */ protected $options; /** * The current cursor resolver callback. * * @var \Closure */ protected static $currentCursorResolver; /** * Get the URL for a given cursor. * * @param \Illuminate\Pagination\Cursor|null $cursor * @return string */ public function url($cursor) { // If we have any extra query string key / value pairs that need to be added // onto the URL, we will put them in query string form and then attach it // to the URL. This allows for extra information like sortings storage. $parameters = is_null($cursor) ? [] : [$this->cursorName => $cursor->encode()]; if (count($this->query) > 0) { $parameters = array_merge($this->query, $parameters); } return $this->path() .(str_contains($this->path(), '?') ? '&' : '?') .Arr::query($parameters) .$this->buildFragment(); } /** * Get the URL for the previous page. * * @return string|null */ public function previousPageUrl() { if (is_null($previousCursor = $this->previousCursor())) { return null; } return $this->url($previousCursor); } /** * The URL for the next page, or null. * * @return string|null */ public function nextPageUrl() { if (is_null($nextCursor = $this->nextCursor())) { return null; } return $this->url($nextCursor); } /** * Get the "cursor" that points to the previous set of items. * * @return \Illuminate\Pagination\Cursor|null */ public function previousCursor() { if (is_null($this->cursor) || ($this->cursor->pointsToPreviousItems() && ! $this->hasMore)) { return null; } if ($this->items->isEmpty()) { return null; } return $this->getCursorForItem($this->items->first(), false); } /** * Get the "cursor" that points to the next set of items. * * @return \Illuminate\Pagination\Cursor|null */ public function nextCursor() { if ((is_null($this->cursor) && ! $this->hasMore) || (! is_null($this->cursor) && $this->cursor->pointsToNextItems() && ! $this->hasMore)) { return null; } if ($this->items->isEmpty()) { return null; } return $this->getCursorForItem($this->items->last(), true); } /** * Get a cursor instance for the given item. * * @param \ArrayAccess|\stdClass $item * @param bool $isNext * @return \Illuminate\Pagination\Cursor */ public function getCursorForItem($item, $isNext = true) { return new Cursor($this->getParametersForItem($item), $isNext); } /** * Get the cursor parameters for a given object. * * @param \ArrayAccess|\stdClass $item * @return array * * @throws \Exception */ public function getParametersForItem($item) { return collect($this->parameters) ->flip() ->map(function ($_, $parameterName) use ($item) { if ($item instanceof JsonResource) { $item = $item->resource; } if ($item instanceof Model && ! is_null($parameter = $this->getPivotParameterForItem($item, $parameterName))) { return $parameter; } elseif ($item instanceof ArrayAccess || is_array($item)) { return $this->ensureParameterIsPrimitive( $item[$parameterName] ?? $item[Str::afterLast($parameterName, '.')] ); } elseif (is_object($item)) { return $this->ensureParameterIsPrimitive( $item->{$parameterName} ?? $item->{Str::afterLast($parameterName, '.')} ); } throw new Exception('Only arrays and objects are supported when cursor paginating items.'); })->toArray(); } /** * Get the cursor parameter value from a pivot model if applicable. * * @param \ArrayAccess|\stdClass $item * @param string $parameterName * @return string|null */ protected function getPivotParameterForItem($item, $parameterName) { $table = Str::beforeLast($parameterName, '.'); foreach ($item->getRelations() as $relation) { if ($relation instanceof Pivot && $relation->getTable() === $table) { return $this->ensureParameterIsPrimitive( $relation->getAttribute(Str::afterLast($parameterName, '.')) ); } } } /** * Ensure the parameter is a primitive type. * * This can resolve issues that arise the developer uses a value object for an attribute. * * @param mixed $parameter * @return mixed */ protected function ensureParameterIsPrimitive($parameter) { return is_object($parameter) && method_exists($parameter, '__toString') ? (string) $parameter : $parameter; } /** * Get / set the URL fragment to be appended to URLs. * * @param string|null $fragment * @return $this|string|null */ public function fragment($fragment = null) { if (is_null($fragment)) { return $this->fragment; } $this->fragment = $fragment; return $this; } /** * Add a set of query string values to the paginator. * * @param array|string|null $key * @param string|null $value * @return $this */ public function appends($key, $value = null) { if (is_null($key)) { return $this; } if (is_array($key)) { return $this->appendArray($key); } return $this->addQuery($key, $value); } /** * Add an array of query string values. * * @param array $keys * @return $this */ protected function appendArray(array $keys) { foreach ($keys as $key => $value) { $this->addQuery($key, $value); } return $this; } /** * Add all current query string values to the paginator. * * @return $this */ public function withQueryString() { if (! is_null($query = Paginator::resolveQueryString())) { return $this->appends($query); } return $this; } /** * Add a query string value to the paginator. * * @param string $key * @param string $value * @return $this */ protected function addQuery($key, $value) { if ($key !== $this->cursorName) { $this->query[$key] = $value; } return $this; } /** * Build the full fragment portion of a URL. * * @return string */ protected function buildFragment() { return $this->fragment ? '#'.$this->fragment : ''; } /** * Load a set of relationships onto the mixed relationship collection. * * @param string $relation * @param array $relations * @return $this */ public function loadMorph($relation, $relations) { $this->getCollection()->loadMorph($relation, $relations); return $this; } /** * Load a set of relationship counts onto the mixed relationship collection. * * @param string $relation * @param array $relations * @return $this */ public function loadMorphCount($relation, $relations) { $this->getCollection()->loadMorphCount($relation, $relations); return $this; } /** * Get the slice of items being paginated. * * @return array */ public function items() { return $this->items->all(); } /** * Transform each item in the slice of items using a callback. * * @param callable $callback * @return $this */ public function through(callable $callback) { $this->items->transform($callback); return $this; } /** * Get the number of items shown per page. * * @return int */ public function perPage() { return $this->perPage; } /** * Get the current cursor being paginated. * * @return \Illuminate\Pagination\Cursor|null */ public function cursor() { return $this->cursor; } /** * Get the query string variable used to store the cursor. * * @return string */ public function getCursorName() { return $this->cursorName; } /** * Set the query string variable used to store the cursor. * * @param string $name * @return $this */ public function setCursorName($name) { $this->cursorName = $name; return $this; } /** * Set the base path to assign to all URLs. * * @param string $path * @return $this */ public function withPath($path) { return $this->setPath($path); } /** * Set the base path to assign to all URLs. * * @param string $path * @return $this */ public function setPath($path) { $this->path = $path; return $this; } /** * Get the base path for paginator generated URLs. * * @return string|null */ public function path() { return $this->path; } /** * Resolve the current cursor or return the default value. * * @param string $cursorName * @return \Illuminate\Pagination\Cursor|null */ public static function resolveCurrentCursor($cursorName = 'cursor', $default = null) { if (isset(static::$currentCursorResolver)) { return call_user_func(static::$currentCursorResolver, $cursorName); } return $default; } /** * Set the current cursor resolver callback. * * @param \Closure $resolver * @return void */ public static function currentCursorResolver(Closure $resolver) { static::$currentCursorResolver = $resolver; } /** * Get an instance of the view factory from the resolver. * * @return \Illuminate\Contracts\View\Factory */ public static function viewFactory() { return Paginator::viewFactory(); } /** * Get an iterator for the items. * * @return \ArrayIterator */ public function getIterator(): Traversable { return $this->items->getIterator(); } /** * Determine if the list of items is empty. * * @return bool */ public function isEmpty() { return $this->items->isEmpty(); } /** * Determine if the list of items is not empty. * * @return bool */ public function isNotEmpty() { return $this->items->isNotEmpty(); } /** * Get the number of items for the current page. * * @return int */ public function count(): int { return $this->items->count(); } /** * Get the paginator's underlying collection. * * @return \Illuminate\Support\Collection */ public function getCollection() { return $this->items; } /** * Set the paginator's underlying collection. * * @param \Illuminate\Support\Collection $collection * @return $this */ public function setCollection(Collection $collection) { $this->items = $collection; return $this; } /** * Get the paginator options. * * @return array */ public function getOptions() { return $this->options; } /** * Determine if the given item exists. * * @param mixed $key * @return bool */ public function offsetExists($key): bool { return $this->items->has($key); } /** * Get the item at the given offset. * * @param mixed $key * @return mixed */ public function offsetGet($key): mixed { return $this->items->get($key); } /** * Set the item at the given offset. * * @param mixed $key * @param mixed $value * @return void */ public function offsetSet($key, $value): void { $this->items->put($key, $value); } /** * Unset the item at the given key. * * @param mixed $key * @return void */ public function offsetUnset($key): void { $this->items->forget($key); } /** * Render the contents of the paginator to HTML. * * @return string */ public function toHtml() { return (string) $this->render(); } /** * Make dynamic calls into the collection. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->forwardCallTo($this->getCollection(), $method, $parameters); } /** * Render the contents of the paginator when casting to a string. * * @return string */ public function __toString() { return (string) $this->render(); } } src/Illuminate/Pagination/Cursor.php 0000644 00000006236 15107327040 0013523 0 ustar 00 <?php namespace Illuminate\Pagination; use Illuminate\Contracts\Support\Arrayable; use UnexpectedValueException; class Cursor implements Arrayable { /** * The parameters associated with the cursor. * * @var array */ protected $parameters; /** * Determine whether the cursor points to the next or previous set of items. * * @var bool */ protected $pointsToNextItems; /** * Create a new cursor instance. * * @param array $parameters * @param bool $pointsToNextItems */ public function __construct(array $parameters, $pointsToNextItems = true) { $this->parameters = $parameters; $this->pointsToNextItems = $pointsToNextItems; } /** * Get the given parameter from the cursor. * * @param string $parameterName * @return string|null * * @throws \UnexpectedValueException */ public function parameter(string $parameterName) { if (! array_key_exists($parameterName, $this->parameters)) { throw new UnexpectedValueException("Unable to find parameter [{$parameterName}] in pagination item."); } return $this->parameters[$parameterName]; } /** * Get the given parameters from the cursor. * * @param array $parameterNames * @return array */ public function parameters(array $parameterNames) { return collect($parameterNames)->map(function ($parameterName) { return $this->parameter($parameterName); })->toArray(); } /** * Determine whether the cursor points to the next set of items. * * @return bool */ public function pointsToNextItems() { return $this->pointsToNextItems; } /** * Determine whether the cursor points to the previous set of items. * * @return bool */ public function pointsToPreviousItems() { return ! $this->pointsToNextItems; } /** * Get the array representation of the cursor. * * @return array */ public function toArray() { return array_merge($this->parameters, [ '_pointsToNextItems' => $this->pointsToNextItems, ]); } /** * Get the encoded string representation of the cursor to construct a URL. * * @return string */ public function encode() { return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(json_encode($this->toArray()))); } /** * Get a cursor instance from the encoded string representation. * * @param string|null $encodedString * @return static|null */ public static function fromEncoded($encodedString) { if (! is_string($encodedString)) { return null; } $parameters = json_decode(base64_decode(str_replace(['-', '_'], ['+', '/'], $encodedString)), true); if (json_last_error() !== JSON_ERROR_NONE) { return null; } $pointsToNextItems = $parameters['_pointsToNextItems']; unset($parameters['_pointsToNextItems']); return new static($parameters, $pointsToNextItems); } } src/Illuminate/Notifications/RoutesNotifications.php 0000644 00000002420 15107327040 0016770 0 ustar 00 <?php namespace Illuminate\Notifications; use Illuminate\Contracts\Notifications\Dispatcher; use Illuminate\Support\Str; trait RoutesNotifications { /** * Send the given notification. * * @param mixed $instance * @return void */ public function notify($instance) { app(Dispatcher::class)->send($this, $instance); } /** * Send the given notification immediately. * * @param mixed $instance * @param array|null $channels * @return void */ public function notifyNow($instance, array $channels = null) { app(Dispatcher::class)->sendNow($this, $instance, $channels); } /** * Get the notification routing information for the given driver. * * @param string $driver * @param \Illuminate\Notifications\Notification|null $notification * @return mixed */ public function routeNotificationFor($driver, $notification = null) { if (method_exists($this, $method = 'routeNotificationFor'.Str::studly($driver))) { return $this->{$method}($notification); } return match ($driver) { 'database' => $this->notifications(), 'mail' => $this->email, default => null, }; } } src/Illuminate/Notifications/Notification.php 0000644 00000001400 15107327040 0015400 0 ustar 00 <?php namespace Illuminate\Notifications; use Illuminate\Queue\SerializesModels; class Notification { use SerializesModels; /** * The unique identifier for the notification. * * @var string */ public $id; /** * The locale to be used when sending the notification. * * @var string|null */ public $locale; /** * Get the channels the event should broadcast on. * * @return array */ public function broadcastOn() { return []; } /** * Set the locale to send this notification in. * * @param string $locale * @return $this */ public function locale($locale) { $this->locale = $locale; return $this; } } src/Illuminate/Notifications/NotificationServiceProvider.php 0000644 00000002150 15107327040 0020437 0 ustar 00 <?php namespace Illuminate\Notifications; use Illuminate\Contracts\Notifications\Dispatcher as DispatcherContract; use Illuminate\Contracts\Notifications\Factory as FactoryContract; use Illuminate\Support\ServiceProvider; class NotificationServiceProvider extends ServiceProvider { /** * Boot the application services. * * @return void */ public function boot() { $this->loadViewsFrom(__DIR__.'/resources/views', 'notifications'); if ($this->app->runningInConsole()) { $this->publishes([ __DIR__.'/resources/views' => $this->app->resourcePath('views/vendor/notifications'), ], 'laravel-notifications'); } } /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton(ChannelManager::class, fn ($app) => new ChannelManager($app)); $this->app->alias( ChannelManager::class, DispatcherContract::class ); $this->app->alias( ChannelManager::class, FactoryContract::class ); } } src/Illuminate/Notifications/Console/NotificationTableCommand.php 0000644 00000001627 15107327040 0021264 0 ustar 00 <?php namespace Illuminate\Notifications\Console; use Illuminate\Console\MigrationGeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'notifications:table')] class NotificationTableCommand extends MigrationGeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'notifications:table'; /** * The console command description. * * @var string */ protected $description = 'Create a migration for the notifications table'; /** * Get the migration table name. * * @return string */ protected function migrationTableName() { return 'notifications'; } /** * Get the path to the migration stub file. * * @return string */ protected function migrationStubFile() { return __DIR__.'/stubs/notifications.stub'; } } src/Illuminate/Notifications/Console/stubs/notifications.stub 0000644 00000001326 15107327040 0020562 0 ustar 00 <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('notifications', function (Blueprint $table) { $table->uuid('id')->primary(); $table->string('type'); $table->morphs('notifiable'); $table->text('data'); $table->timestamp('read_at')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('notifications'); } }; src/Illuminate/Notifications/SendQueuedNotifications.php 0000644 00000011535 15107327040 0017560 0 ustar 00 <?php namespace Illuminate\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeEncrypted; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueueAfterCommit; use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Collection; class SendQueuedNotifications implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; /** * The notifiable entities that should receive the notification. * * @var \Illuminate\Support\Collection */ public $notifiables; /** * The notification to be sent. * * @var \Illuminate\Notifications\Notification */ public $notification; /** * All of the channels to send the notification to. * * @var array */ public $channels; /** * The number of times the job may be attempted. * * @var int */ public $tries; /** * The number of seconds the job can run before timing out. * * @var int */ public $timeout; /** * The maximum number of unhandled exceptions to allow before failing. * * @var int */ public $maxExceptions; /** * Indicates if the job should be encrypted. * * @var bool */ public $shouldBeEncrypted = false; /** * Create a new job instance. * * @param \Illuminate\Notifications\Notifiable|\Illuminate\Support\Collection $notifiables * @param \Illuminate\Notifications\Notification $notification * @param array|null $channels * @return void */ public function __construct($notifiables, $notification, array $channels = null) { $this->channels = $channels; $this->notification = $notification; $this->notifiables = $this->wrapNotifiables($notifiables); $this->tries = property_exists($notification, 'tries') ? $notification->tries : null; $this->timeout = property_exists($notification, 'timeout') ? $notification->timeout : null; $this->maxExceptions = property_exists($notification, 'maxExceptions') ? $notification->maxExceptions : null; if ($notification instanceof ShouldQueueAfterCommit) { $this->afterCommit = true; } else { $this->afterCommit = property_exists($notification, 'afterCommit') ? $notification->afterCommit : null; } $this->shouldBeEncrypted = $notification instanceof ShouldBeEncrypted; } /** * Wrap the notifiable(s) in a collection. * * @param \Illuminate\Notifications\Notifiable|\Illuminate\Support\Collection $notifiables * @return \Illuminate\Support\Collection */ protected function wrapNotifiables($notifiables) { if ($notifiables instanceof Collection) { return $notifiables; } elseif ($notifiables instanceof Model) { return EloquentCollection::wrap($notifiables); } return Collection::wrap($notifiables); } /** * Send the notifications. * * @param \Illuminate\Notifications\ChannelManager $manager * @return void */ public function handle(ChannelManager $manager) { $manager->sendNow($this->notifiables, $this->notification, $this->channels); } /** * Get the display name for the queued job. * * @return string */ public function displayName() { return get_class($this->notification); } /** * Call the failed method on the notification instance. * * @param \Throwable $e * @return void */ public function failed($e) { if (method_exists($this->notification, 'failed')) { $this->notification->failed($e); } } /** * Get the number of seconds before a released notification will be available. * * @return mixed */ public function backoff() { if (! method_exists($this->notification, 'backoff') && ! isset($this->notification->backoff)) { return; } return $this->notification->backoff ?? $this->notification->backoff(); } /** * Determine the time at which the job should timeout. * * @return \DateTime|null */ public function retryUntil() { if (! method_exists($this->notification, 'retryUntil') && ! isset($this->notification->retryUntil)) { return; } return $this->notification->retryUntil ?? $this->notification->retryUntil(); } /** * Prepare the instance for cloning. * * @return void */ public function __clone() { $this->notifiables = clone $this->notifiables; $this->notification = clone $this->notification; } } src/Illuminate/Notifications/HasDatabaseNotifications.php 0000644 00000001414 15107327040 0017651 0 ustar 00 <?php namespace Illuminate\Notifications; trait HasDatabaseNotifications { /** * Get the entity's notifications. * * @return \Illuminate\Database\Eloquent\Relations\MorphMany */ public function notifications() { return $this->morphMany(DatabaseNotification::class, 'notifiable')->latest(); } /** * Get the entity's read notifications. * * @return \Illuminate\Database\Query\Builder */ public function readNotifications() { return $this->notifications()->read(); } /** * Get the entity's unread notifications. * * @return \Illuminate\Database\Query\Builder */ public function unreadNotifications() { return $this->notifications()->unread(); } } src/Illuminate/Notifications/DatabaseNotification.php 0000644 00000005440 15107327040 0017035 0 ustar 00 <?php namespace Illuminate\Notifications; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; class DatabaseNotification extends Model { /** * The "type" of the primary key ID. * * @var string */ protected $keyType = 'string'; /** * Indicates if the IDs are auto-incrementing. * * @var bool */ public $incrementing = false; /** * The table associated with the model. * * @var string */ protected $table = 'notifications'; /** * The guarded attributes on the model. * * @var array */ protected $guarded = []; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'data' => 'array', 'read_at' => 'datetime', ]; /** * Get the notifiable entity that the notification belongs to. * * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function notifiable() { return $this->morphTo(); } /** * Mark the notification as read. * * @return void */ public function markAsRead() { if (is_null($this->read_at)) { $this->forceFill(['read_at' => $this->freshTimestamp()])->save(); } } /** * Mark the notification as unread. * * @return void */ public function markAsUnread() { if (! is_null($this->read_at)) { $this->forceFill(['read_at' => null])->save(); } } /** * Determine if a notification has been read. * * @return bool */ public function read() { return $this->read_at !== null; } /** * Determine if a notification has not been read. * * @return bool */ public function unread() { return $this->read_at === null; } /** * Scope a query to only include read notifications. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeRead(Builder $query) { return $query->whereNotNull('read_at'); } /** * Scope a query to only include unread notifications. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeUnread(Builder $query) { return $query->whereNull('read_at'); } /** * Create a new database notification collection instance. * * @param array $models * @return \Illuminate\Notifications\DatabaseNotificationCollection */ public function newCollection(array $models = []) { return new DatabaseNotificationCollection($models); } } src/Illuminate/Notifications/DatabaseNotificationCollection.php 0000644 00000001172 15107327040 0021047 0 ustar 00 <?php namespace Illuminate\Notifications; use Illuminate\Database\Eloquent\Collection; /** * @template TKey of array-key * @template TModel of DatabaseNotification * * @extends \Illuminate\Database\Eloquent\Collection<TKey, TModel> */ class DatabaseNotificationCollection extends Collection { /** * Mark all notifications as read. * * @return void */ public function markAsRead() { $this->each->markAsRead(); } /** * Mark all notifications as unread. * * @return void */ public function markAsUnread() { $this->each->markAsUnread(); } } src/Illuminate/Notifications/Channels/BroadcastChannel.php 0000644 00000003745 15107327040 0017716 0 ustar 00 <?php namespace Illuminate\Notifications\Channels; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Notifications\Events\BroadcastNotificationCreated; use Illuminate\Notifications\Messages\BroadcastMessage; use Illuminate\Notifications\Notification; use RuntimeException; class BroadcastChannel { /** * The event dispatcher. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * Create a new broadcast channel. * * @param \Illuminate\Contracts\Events\Dispatcher $events * @return void */ public function __construct(Dispatcher $events) { $this->events = $events; } /** * Send the given notification. * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @return array|null */ public function send($notifiable, Notification $notification) { $message = $this->getData($notifiable, $notification); $event = new BroadcastNotificationCreated( $notifiable, $notification, is_array($message) ? $message : $message->data ); if ($message instanceof BroadcastMessage) { $event->onConnection($message->connection) ->onQueue($message->queue); } return $this->events->dispatch($event); } /** * Get the data for the notification. * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @return mixed * * @throws \RuntimeException */ protected function getData($notifiable, Notification $notification) { if (method_exists($notification, 'toBroadcast')) { return $notification->toBroadcast($notifiable); } if (method_exists($notification, 'toArray')) { return $notification->toArray($notifiable); } throw new RuntimeException('Notification is missing toArray method.'); } } src/Illuminate/Notifications/Channels/MailChannel.php 0000644 00000022156 15107327040 0016673 0 ustar 00 <?php namespace Illuminate\Notifications\Channels; use Illuminate\Config\Repository as ConfigRepository; use Illuminate\Container\Container; use Illuminate\Contracts\Mail\Factory as MailFactory; use Illuminate\Contracts\Mail\Mailable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Markdown; use Illuminate\Notifications\Notification; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Symfony\Component\Mailer\Header\MetadataHeader; use Symfony\Component\Mailer\Header\TagHeader; class MailChannel { /** * The mailer implementation. * * @var \Illuminate\Contracts\Mail\Factory */ protected $mailer; /** * The markdown implementation. * * @var \Illuminate\Mail\Markdown */ protected $markdown; /** * Create a new mail channel instance. * * @param \Illuminate\Contracts\Mail\Factory $mailer * @param \Illuminate\Mail\Markdown $markdown * @return void */ public function __construct(MailFactory $mailer, Markdown $markdown) { $this->mailer = $mailer; $this->markdown = $markdown; } /** * Send the given notification. * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @return \Illuminate\Mail\SentMessage|null */ public function send($notifiable, Notification $notification) { $message = $notification->toMail($notifiable); if (! $notifiable->routeNotificationFor('mail', $notification) && ! $message instanceof Mailable) { return; } if ($message instanceof Mailable) { return $message->send($this->mailer); } return $this->mailer->mailer($message->mailer ?? null)->send( $this->buildView($message), array_merge($message->data(), $this->additionalMessageData($notification)), $this->messageBuilder($notifiable, $notification, $message) ); } /** * Get the mailer Closure for the message. * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @param \Illuminate\Notifications\Messages\MailMessage $message * @return \Closure */ protected function messageBuilder($notifiable, $notification, $message) { return function ($mailMessage) use ($notifiable, $notification, $message) { $this->buildMessage($mailMessage, $notifiable, $notification, $message); }; } /** * Build the notification's view. * * @param \Illuminate\Notifications\Messages\MailMessage $message * @return string|array */ protected function buildView($message) { if ($message->view) { return $message->view; } return [ 'html' => $this->buildMarkdownHtml($message), 'text' => $this->buildMarkdownText($message), ]; } /** * Build the HTML view for a Markdown message. * * @param \Illuminate\Notifications\Messages\MailMessage $message * @return \Closure */ protected function buildMarkdownHtml($message) { return fn ($data) => $this->markdownRenderer($message)->render( $message->markdown, array_merge($data, $message->data()), ); } /** * Build the text view for a Markdown message. * * @param \Illuminate\Notifications\Messages\MailMessage $message * @return \Closure */ protected function buildMarkdownText($message) { return fn ($data) => $this->markdownRenderer($message)->renderText( $message->markdown, array_merge($data, $message->data()), ); } /** * Get the Markdown implementation. * * @param \Illuminate\Notifications\Messages\MailMessage $message * @return \Illuminate\Mail\Markdown */ protected function markdownRenderer($message) { $config = Container::getInstance()->get(ConfigRepository::class); $theme = $message->theme ?? $config->get('mail.markdown.theme', 'default'); return $this->markdown->theme($theme); } /** * Get additional meta-data to pass along with the view data. * * @param \Illuminate\Notifications\Notification $notification * @return array */ protected function additionalMessageData($notification) { return [ '__laravel_notification_id' => $notification->id, '__laravel_notification' => get_class($notification), '__laravel_notification_queued' => in_array( ShouldQueue::class, class_implements($notification) ), ]; } /** * Build the mail message. * * @param \Illuminate\Mail\Message $mailMessage * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @param \Illuminate\Notifications\Messages\MailMessage $message * @return void */ protected function buildMessage($mailMessage, $notifiable, $notification, $message) { $this->addressMessage($mailMessage, $notifiable, $notification, $message); $mailMessage->subject($message->subject ?: Str::title( Str::snake(class_basename($notification), ' ') )); $this->addAttachments($mailMessage, $message); if (! is_null($message->priority)) { $mailMessage->priority($message->priority); } if ($message->tags) { foreach ($message->tags as $tag) { $mailMessage->getHeaders()->add(new TagHeader($tag)); } } if ($message->metadata) { foreach ($message->metadata as $key => $value) { $mailMessage->getHeaders()->add(new MetadataHeader($key, $value)); } } $this->runCallbacks($mailMessage, $message); } /** * Address the mail message. * * @param \Illuminate\Mail\Message $mailMessage * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @param \Illuminate\Notifications\Messages\MailMessage $message * @return void */ protected function addressMessage($mailMessage, $notifiable, $notification, $message) { $this->addSender($mailMessage, $message); $mailMessage->to($this->getRecipients($notifiable, $notification, $message)); if (! empty($message->cc)) { foreach ($message->cc as $cc) { $mailMessage->cc($cc[0], Arr::get($cc, 1)); } } if (! empty($message->bcc)) { foreach ($message->bcc as $bcc) { $mailMessage->bcc($bcc[0], Arr::get($bcc, 1)); } } } /** * Add the "from" and "reply to" addresses to the message. * * @param \Illuminate\Mail\Message $mailMessage * @param \Illuminate\Notifications\Messages\MailMessage $message * @return void */ protected function addSender($mailMessage, $message) { if (! empty($message->from)) { $mailMessage->from($message->from[0], Arr::get($message->from, 1)); } if (! empty($message->replyTo)) { foreach ($message->replyTo as $replyTo) { $mailMessage->replyTo($replyTo[0], Arr::get($replyTo, 1)); } } } /** * Get the recipients of the given message. * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @param \Illuminate\Notifications\Messages\MailMessage $message * @return mixed */ protected function getRecipients($notifiable, $notification, $message) { if (is_string($recipients = $notifiable->routeNotificationFor('mail', $notification))) { $recipients = [$recipients]; } return collect($recipients)->mapWithKeys(function ($recipient, $email) { return is_numeric($email) ? [$email => (is_string($recipient) ? $recipient : $recipient->email)] : [$email => $recipient]; })->all(); } /** * Add the attachments to the message. * * @param \Illuminate\Mail\Message $mailMessage * @param \Illuminate\Notifications\Messages\MailMessage $message * @return void */ protected function addAttachments($mailMessage, $message) { foreach ($message->attachments as $attachment) { $mailMessage->attach($attachment['file'], $attachment['options']); } foreach ($message->rawAttachments as $attachment) { $mailMessage->attachData($attachment['data'], $attachment['name'], $attachment['options']); } } /** * Run the callbacks for the message. * * @param \Illuminate\Mail\Message $mailMessage * @param \Illuminate\Notifications\Messages\MailMessage $message * @return $this */ protected function runCallbacks($mailMessage, $message) { foreach ($message->callbacks as $callback) { $callback($mailMessage->getSymfonyMessage()); } return $this; } } src/Illuminate/Notifications/Channels/DatabaseChannel.php 0000644 00000003665 15107327040 0017521 0 ustar 00 <?php namespace Illuminate\Notifications\Channels; use Illuminate\Notifications\Notification; use RuntimeException; class DatabaseChannel { /** * Send the given notification. * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @return \Illuminate\Database\Eloquent\Model */ public function send($notifiable, Notification $notification) { return $notifiable->routeNotificationFor('database', $notification)->create( $this->buildPayload($notifiable, $notification) ); } /** * Build an array payload for the DatabaseNotification Model. * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @return array */ protected function buildPayload($notifiable, Notification $notification) { return [ 'id' => $notification->id, 'type' => method_exists($notification, 'databaseType') ? $notification->databaseType($notifiable) : get_class($notification), 'data' => $this->getData($notifiable, $notification), 'read_at' => null, ]; } /** * Get the data for the notification. * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @return array * * @throws \RuntimeException */ protected function getData($notifiable, Notification $notification) { if (method_exists($notification, 'toDatabase')) { return is_array($data = $notification->toDatabase($notifiable)) ? $data : $data->data; } if (method_exists($notification, 'toArray')) { return $notification->toArray($notifiable); } throw new RuntimeException('Notification is missing toDatabase / toArray method.'); } } src/Illuminate/Notifications/resources/views/email.blade.php 0000644 00000002071 15107327040 0020263 0 ustar 00 <x-mail::message> {{-- Greeting --}} @if (! empty($greeting)) # {{ $greeting }} @else @if ($level === 'error') # @lang('Whoops!') @else # @lang('Hello!') @endif @endif {{-- Intro Lines --}} @foreach ($introLines as $line) {{ $line }} @endforeach {{-- Action Button --}} @isset($actionText) <?php $color = match ($level) { 'success', 'error' => $level, default => 'primary', }; ?> <x-mail::button :url="$actionUrl" :color="$color"> {{ $actionText }} </x-mail::button> @endisset {{-- Outro Lines --}} @foreach ($outroLines as $line) {{ $line }} @endforeach {{-- Salutation --}} @if (! empty($salutation)) {{ $salutation }} @else @lang('Regards'),<br> {{ config('app.name') }} @endif {{-- Subcopy --}} @isset($actionText) <x-slot:subcopy> @lang( "If you're having trouble clicking the \":actionText\" button, copy and paste the URL below\n". 'into your web browser:', [ 'actionText' => $actionText, ] ) <span class="break-all">[{{ $displayableActionUrl }}]({{ $actionUrl }})</span> </x-slot:subcopy> @endisset </x-mail::message> src/Illuminate/Notifications/NotificationSender.php 0000644 00000017314 15107327040 0016554 0 ustar 00 <?php namespace Illuminate\Notifications; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Translation\HasLocalePreference; use Illuminate\Database\Eloquent\Collection as ModelCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\Events\NotificationSending; use Illuminate\Notifications\Events\NotificationSent; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Illuminate\Support\Traits\Localizable; class NotificationSender { use Localizable; /** * The notification manager instance. * * @var \Illuminate\Notifications\ChannelManager */ protected $manager; /** * The Bus dispatcher instance. * * @var \Illuminate\Contracts\Bus\Dispatcher */ protected $bus; /** * The event dispatcher. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * The locale to be used when sending notifications. * * @var string|null */ protected $locale; /** * Create a new notification sender instance. * * @param \Illuminate\Notifications\ChannelManager $manager * @param \Illuminate\Contracts\Bus\Dispatcher $bus * @param \Illuminate\Contracts\Events\Dispatcher $events * @param string|null $locale * @return void */ public function __construct($manager, $bus, $events, $locale = null) { $this->bus = $bus; $this->events = $events; $this->locale = $locale; $this->manager = $manager; } /** * Send the given notification to the given notifiable entities. * * @param \Illuminate\Support\Collection|array|mixed $notifiables * @param mixed $notification * @return void */ public function send($notifiables, $notification) { $notifiables = $this->formatNotifiables($notifiables); if ($notification instanceof ShouldQueue) { return $this->queueNotification($notifiables, $notification); } $this->sendNow($notifiables, $notification); } /** * Send the given notification immediately. * * @param \Illuminate\Support\Collection|array|mixed $notifiables * @param mixed $notification * @param array|null $channels * @return void */ public function sendNow($notifiables, $notification, array $channels = null) { $notifiables = $this->formatNotifiables($notifiables); $original = clone $notification; foreach ($notifiables as $notifiable) { if (empty($viaChannels = $channels ?: $notification->via($notifiable))) { continue; } $this->withLocale($this->preferredLocale($notifiable, $notification), function () use ($viaChannels, $notifiable, $original) { $notificationId = Str::uuid()->toString(); foreach ((array) $viaChannels as $channel) { if (! ($notifiable instanceof AnonymousNotifiable && $channel === 'database')) { $this->sendToNotifiable($notifiable, $notificationId, clone $original, $channel); } } }); } } /** * Get the notifiable's preferred locale for the notification. * * @param mixed $notifiable * @param mixed $notification * @return string|null */ protected function preferredLocale($notifiable, $notification) { return $notification->locale ?? $this->locale ?? value(function () use ($notifiable) { if ($notifiable instanceof HasLocalePreference) { return $notifiable->preferredLocale(); } }); } /** * Send the given notification to the given notifiable via a channel. * * @param mixed $notifiable * @param string $id * @param mixed $notification * @param string $channel * @return void */ protected function sendToNotifiable($notifiable, $id, $notification, $channel) { if (! $notification->id) { $notification->id = $id; } if (! $this->shouldSendNotification($notifiable, $notification, $channel)) { return; } $response = $this->manager->driver($channel)->send($notifiable, $notification); $this->events->dispatch( new NotificationSent($notifiable, $notification, $channel, $response) ); } /** * Determines if the notification can be sent. * * @param mixed $notifiable * @param mixed $notification * @param string $channel * @return bool */ protected function shouldSendNotification($notifiable, $notification, $channel) { if (method_exists($notification, 'shouldSend') && $notification->shouldSend($notifiable, $channel) === false) { return false; } return $this->events->until( new NotificationSending($notifiable, $notification, $channel) ) !== false; } /** * Queue the given notification instances. * * @param mixed $notifiables * @param \Illuminate\Notifications\Notification $notification * @return void */ protected function queueNotification($notifiables, $notification) { $notifiables = $this->formatNotifiables($notifiables); $original = clone $notification; foreach ($notifiables as $notifiable) { $notificationId = Str::uuid()->toString(); foreach ((array) $original->via($notifiable) as $channel) { $notification = clone $original; if (! $notification->id) { $notification->id = $notificationId; } if (! is_null($this->locale)) { $notification->locale = $this->locale; } $connection = $notification->connection; if (method_exists($notification, 'viaConnections')) { $connection = $notification->viaConnections()[$channel] ?? null; } $queue = $notification->queue; if (method_exists($notification, 'viaQueues')) { $queue = $notification->viaQueues()[$channel] ?? null; } $delay = $notification->delay; if (method_exists($notification, 'withDelay')) { $delay = $notification->withDelay($notifiable, $channel) ?? null; } $middleware = $notification->middleware ?? []; if (method_exists($notification, 'middleware')) { $middleware = array_merge( $notification->middleware($notifiable, $channel), $middleware ); } $this->bus->dispatch( (new SendQueuedNotifications($notifiable, $notification, [$channel])) ->onConnection($connection) ->onQueue($queue) ->delay(is_array($delay) ? ($delay[$channel] ?? null) : $delay) ->through($middleware) ); } } } /** * Format the notifiables into a Collection / array if necessary. * * @param mixed $notifiables * @return \Illuminate\Database\Eloquent\Collection|array */ protected function formatNotifiables($notifiables) { if (! $notifiables instanceof Collection && ! is_array($notifiables)) { return $notifiables instanceof Model ? new ModelCollection([$notifiables]) : [$notifiables]; } return $notifiables; } } src/Illuminate/Notifications/composer.json 0000644 00000002306 15107327040 0014771 0 ustar 00 { "name": "illuminate/notifications", "description": "The Illuminate Notifications package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/broadcasting": "^10.0", "illuminate/bus": "^10.0", "illuminate/collections": "^10.0", "illuminate/container": "^10.0", "illuminate/contracts": "^10.0", "illuminate/filesystem": "^10.0", "illuminate/mail": "^10.0", "illuminate/queue": "^10.0", "illuminate/support": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Notifications\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "illuminate/database": "Required to use the database transport (^10.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Notifications/Events/NotificationFailed.php 0000644 00000002161 15107327040 0017756 0 ustar 00 <?php namespace Illuminate\Notifications\Events; use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; class NotificationFailed { use Queueable, SerializesModels; /** * The notifiable entity who received the notification. * * @var mixed */ public $notifiable; /** * The notification instance. * * @var \Illuminate\Notifications\Notification */ public $notification; /** * The channel name. * * @var string */ public $channel; /** * The data needed to process this failure. * * @var array */ public $data = []; /** * Create a new event instance. * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @param string $channel * @param array $data * @return void */ public function __construct($notifiable, $notification, $channel, $data = []) { $this->data = $data; $this->channel = $channel; $this->notifiable = $notifiable; $this->notification = $notification; } } src/Illuminate/Notifications/Events/NotificationSent.php 0000644 00000002157 15107327040 0017510 0 ustar 00 <?php namespace Illuminate\Notifications\Events; use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; class NotificationSent { use Queueable, SerializesModels; /** * The notifiable entity who received the notification. * * @var mixed */ public $notifiable; /** * The notification instance. * * @var \Illuminate\Notifications\Notification */ public $notification; /** * The channel name. * * @var string */ public $channel; /** * The channel's response. * * @var mixed */ public $response; /** * Create a new event instance. * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @param string $channel * @param mixed $response * @return void */ public function __construct($notifiable, $notification, $channel, $response = null) { $this->channel = $channel; $this->response = $response; $this->notifiable = $notifiable; $this->notification = $notification; } } src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php 0000644 00000006636 15107327040 0021777 0 ustar 00 <?php namespace Illuminate\Notifications\Events; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Arr; class BroadcastNotificationCreated implements ShouldBroadcast { use Queueable, SerializesModels; /** * The notifiable entity who received the notification. * * @var mixed */ public $notifiable; /** * The notification instance. * * @var \Illuminate\Notifications\Notification */ public $notification; /** * The notification data. * * @var array */ public $data = []; /** * Create a new event instance. * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @param array $data * @return void */ public function __construct($notifiable, $notification, $data) { $this->data = $data; $this->notifiable = $notifiable; $this->notification = $notification; } /** * Get the channels the event should broadcast on. * * @return array */ public function broadcastOn() { if ($this->notifiable instanceof AnonymousNotifiable && $this->notifiable->routeNotificationFor('broadcast')) { $channels = Arr::wrap($this->notifiable->routeNotificationFor('broadcast')); } else { $channels = $this->notification->broadcastOn(); } if (! empty($channels)) { return $channels; } if (is_string($channels = $this->channelName())) { return [new PrivateChannel($channels)]; } return collect($channels)->map(function ($channel) { return new PrivateChannel($channel); })->all(); } /** * Get the broadcast channel name for the event. * * @return array|string */ protected function channelName() { if (method_exists($this->notifiable, 'receivesBroadcastNotificationsOn')) { return $this->notifiable->receivesBroadcastNotificationsOn($this->notification); } $class = str_replace('\\', '.', get_class($this->notifiable)); return $class.'.'.$this->notifiable->getKey(); } /** * Get the data that should be sent with the broadcasted event. * * @return array */ public function broadcastWith() { if (method_exists($this->notification, 'broadcastWith')) { return $this->notification->broadcastWith(); } return array_merge($this->data, [ 'id' => $this->notification->id, 'type' => $this->broadcastType(), ]); } /** * Get the type of the notification being broadcast. * * @return string */ public function broadcastType() { return method_exists($this->notification, 'broadcastType') ? $this->notification->broadcastType() : get_class($this->notification); } /** * Get the event name of the notification being broadcast. * * @return string */ public function broadcastAs() { return method_exists($this->notification, 'broadcastAs') ? $this->notification->broadcastAs() : __CLASS__; } } src/Illuminate/Notifications/Events/NotificationSending.php 0000644 00000001674 15107327040 0020171 0 ustar 00 <?php namespace Illuminate\Notifications\Events; use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; class NotificationSending { use Queueable, SerializesModels; /** * The notifiable entity who received the notification. * * @var mixed */ public $notifiable; /** * The notification instance. * * @var \Illuminate\Notifications\Notification */ public $notification; /** * The channel name. * * @var string */ public $channel; /** * Create a new event instance. * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification * @param string $channel * @return void */ public function __construct($notifiable, $notification, $channel) { $this->channel = $channel; $this->notifiable = $notifiable; $this->notification = $notification; } } src/Illuminate/Notifications/LICENSE.md 0000644 00000002063 15107327040 0013653 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Notifications/Action.php 0000644 00000000737 15107327040 0014203 0 ustar 00 <?php namespace Illuminate\Notifications; class Action { /** * The action text. * * @var string */ public $text; /** * The action URL. * * @var string */ public $url; /** * Create a new action instance. * * @param string $text * @param string $url * @return void */ public function __construct($text, $url) { $this->url = $url; $this->text = $text; } } src/Illuminate/Notifications/AnonymousNotifiable.php 0000644 00000003153 15107327040 0016746 0 ustar 00 <?php namespace Illuminate\Notifications; use Illuminate\Contracts\Notifications\Dispatcher; use InvalidArgumentException; class AnonymousNotifiable { /** * All of the notification routing information. * * @var array */ public $routes = []; /** * Add routing information to the target. * * @param string $channel * @param mixed $route * @return $this * * @throws \InvalidArgumentException */ public function route($channel, $route) { if ($channel === 'database') { throw new InvalidArgumentException('The database channel does not support on-demand notifications.'); } $this->routes[$channel] = $route; return $this; } /** * Send the given notification. * * @param mixed $notification * @return void */ public function notify($notification) { app(Dispatcher::class)->send($this, $notification); } /** * Send the given notification immediately. * * @param mixed $notification * @return void */ public function notifyNow($notification) { app(Dispatcher::class)->sendNow($this, $notification); } /** * Get the notification routing information for the given driver. * * @param string $driver * @return mixed */ public function routeNotificationFor($driver) { return $this->routes[$driver] ?? null; } /** * Get the value of the notifiable's primary key. * * @return mixed */ public function getKey() { // } } src/Illuminate/Notifications/Notifiable.php 0000644 00000000170 15107327040 0015031 0 ustar 00 <?php namespace Illuminate\Notifications; trait Notifiable { use HasDatabaseNotifications, RoutesNotifications; } src/Illuminate/Notifications/ChannelManager.php 0000644 00000007574 15107327040 0015637 0 ustar 00 <?php namespace Illuminate\Notifications; use Illuminate\Contracts\Bus\Dispatcher as Bus; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Notifications\Dispatcher as DispatcherContract; use Illuminate\Contracts\Notifications\Factory as FactoryContract; use Illuminate\Support\Manager; use InvalidArgumentException; class ChannelManager extends Manager implements DispatcherContract, FactoryContract { /** * The default channel used to deliver messages. * * @var string */ protected $defaultChannel = 'mail'; /** * The locale used when sending notifications. * * @var string|null */ protected $locale; /** * Send the given notification to the given notifiable entities. * * @param \Illuminate\Support\Collection|array|mixed $notifiables * @param mixed $notification * @return void */ public function send($notifiables, $notification) { (new NotificationSender( $this, $this->container->make(Bus::class), $this->container->make(Dispatcher::class), $this->locale) )->send($notifiables, $notification); } /** * Send the given notification immediately. * * @param \Illuminate\Support\Collection|array|mixed $notifiables * @param mixed $notification * @param array|null $channels * @return void */ public function sendNow($notifiables, $notification, array $channels = null) { (new NotificationSender( $this, $this->container->make(Bus::class), $this->container->make(Dispatcher::class), $this->locale) )->sendNow($notifiables, $notification, $channels); } /** * Get a channel instance. * * @param string|null $name * @return mixed */ public function channel($name = null) { return $this->driver($name); } /** * Create an instance of the database driver. * * @return \Illuminate\Notifications\Channels\DatabaseChannel */ protected function createDatabaseDriver() { return $this->container->make(Channels\DatabaseChannel::class); } /** * Create an instance of the broadcast driver. * * @return \Illuminate\Notifications\Channels\BroadcastChannel */ protected function createBroadcastDriver() { return $this->container->make(Channels\BroadcastChannel::class); } /** * Create an instance of the mail driver. * * @return \Illuminate\Notifications\Channels\MailChannel */ protected function createMailDriver() { return $this->container->make(Channels\MailChannel::class); } /** * Create a new driver instance. * * @param string $driver * @return mixed * * @throws \InvalidArgumentException */ protected function createDriver($driver) { try { return parent::createDriver($driver); } catch (InvalidArgumentException $e) { if (class_exists($driver)) { return $this->container->make($driver); } throw $e; } } /** * Get the default channel driver name. * * @return string */ public function getDefaultDriver() { return $this->defaultChannel; } /** * Get the default channel driver name. * * @return string */ public function deliversVia() { return $this->getDefaultDriver(); } /** * Set the default channel driver name. * * @param string $channel * @return void */ public function deliverVia($channel) { $this->defaultChannel = $channel; } /** * Set the locale of notifications. * * @param string $locale * @return $this */ public function locale($locale) { $this->locale = $locale; return $this; } } src/Illuminate/Notifications/Messages/BroadcastMessage.php 0000644 00000001156 15107327040 0017740 0 ustar 00 <?php namespace Illuminate\Notifications\Messages; use Illuminate\Bus\Queueable; class BroadcastMessage { use Queueable; /** * The data for the notification. * * @var array */ public $data; /** * Create a new message instance. * * @param array $data * @return void */ public function __construct(array $data) { $this->data = $data; } /** * Set the message data. * * @param array $data * @return $this */ public function data($data) { $this->data = $data; return $this; } } src/Illuminate/Notifications/Messages/SimpleMessage.php 0000644 00000013332 15107327040 0017266 0 ustar 00 <?php namespace Illuminate\Notifications\Messages; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Notifications\Action; class SimpleMessage { /** * The "level" of the notification (info, success, error). * * @var string */ public $level = 'info'; /** * The subject of the notification. * * @var string */ public $subject; /** * The notification's greeting. * * @var string */ public $greeting; /** * The notification's salutation. * * @var string */ public $salutation; /** * The "intro" lines of the notification. * * @var array */ public $introLines = []; /** * The "outro" lines of the notification. * * @var array */ public $outroLines = []; /** * The text / label for the action. * * @var string */ public $actionText; /** * The action URL. * * @var string */ public $actionUrl; /** * The name of the mailer that should send the notification. * * @var string */ public $mailer; /** * Indicate that the notification gives information about a successful operation. * * @return $this */ public function success() { $this->level = 'success'; return $this; } /** * Indicate that the notification gives information about an error. * * @return $this */ public function error() { $this->level = 'error'; return $this; } /** * Set the "level" of the notification (success, error, etc.). * * @param string $level * @return $this */ public function level($level) { $this->level = $level; return $this; } /** * Set the subject of the notification. * * @param string $subject * @return $this */ public function subject($subject) { $this->subject = $subject; return $this; } /** * Set the greeting of the notification. * * @param string $greeting * @return $this */ public function greeting($greeting) { $this->greeting = $greeting; return $this; } /** * Set the salutation of the notification. * * @param string $salutation * @return $this */ public function salutation($salutation) { $this->salutation = $salutation; return $this; } /** * Add a line of text to the notification. * * @param mixed $line * @return $this */ public function line($line) { return $this->with($line); } /** * Add a line of text to the notification if the given condition is true. * * @param bool $boolean * @param mixed $line * @return $this */ public function lineIf($boolean, $line) { if ($boolean) { return $this->line($line); } return $this; } /** * Add lines of text to the notification. * * @param iterable $lines * @return $this */ public function lines($lines) { foreach ($lines as $line) { $this->line($line); } return $this; } /** * Add lines of text to the notification if the given condition is true. * * @param bool $boolean * @param iterable $lines * @return $this */ public function linesIf($boolean, $lines) { if ($boolean) { return $this->lines($lines); } return $this; } /** * Add a line of text to the notification. * * @param mixed $line * @return $this */ public function with($line) { if ($line instanceof Action) { $this->action($line->text, $line->url); } elseif (! $this->actionText) { $this->introLines[] = $this->formatLine($line); } else { $this->outroLines[] = $this->formatLine($line); } return $this; } /** * Format the given line of text. * * @param \Illuminate\Contracts\Support\Htmlable|string|array $line * @return \Illuminate\Contracts\Support\Htmlable|string */ protected function formatLine($line) { if ($line instanceof Htmlable) { return $line; } if (is_array($line)) { return implode(' ', array_map('trim', $line)); } return trim(implode(' ', array_map('trim', preg_split('/\\r\\n|\\r|\\n/', $line ?? '')))); } /** * Configure the "call to action" button. * * @param string $text * @param string $url * @return $this */ public function action($text, $url) { $this->actionText = $text; $this->actionUrl = $url; return $this; } /** * Set the name of the mailer that should send the notification. * * @param string $mailer * @return $this */ public function mailer($mailer) { $this->mailer = $mailer; return $this; } /** * Get an array representation of the message. * * @return array */ public function toArray() { return [ 'level' => $this->level, 'subject' => $this->subject, 'greeting' => $this->greeting, 'salutation' => $this->salutation, 'introLines' => $this->introLines, 'outroLines' => $this->outroLines, 'actionText' => $this->actionText, 'actionUrl' => $this->actionUrl, 'displayableActionUrl' => str_replace(['mailto:', 'tel:'], '', $this->actionUrl ?? ''), ]; } } src/Illuminate/Notifications/Messages/DatabaseMessage.php 0000644 00000000625 15107327040 0017542 0 ustar 00 <?php namespace Illuminate\Notifications\Messages; class DatabaseMessage { /** * The data that should be stored with the notification. * * @var array */ public $data = []; /** * Create a new database message. * * @param array $data * @return void */ public function __construct(array $data = []) { $this->data = $data; } } src/Illuminate/Notifications/Messages/MailMessage.php 0000644 00000020714 15107327040 0016721 0 ustar 00 <?php namespace Illuminate\Notifications\Messages; use Illuminate\Container\Container; use Illuminate\Contracts\Mail\Attachable; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Renderable; use Illuminate\Mail\Attachment; use Illuminate\Mail\Markdown; use Illuminate\Support\Traits\Conditionable; class MailMessage extends SimpleMessage implements Renderable { use Conditionable; /** * The view to be rendered. * * @var array|string */ public $view; /** * The view data for the message. * * @var array */ public $viewData = []; /** * The Markdown template to render (if applicable). * * @var string|null */ public $markdown = 'notifications::email'; /** * The current theme being used when generating emails. * * @var string|null */ public $theme; /** * The "from" information for the message. * * @var array */ public $from = []; /** * The "reply to" information for the message. * * @var array */ public $replyTo = []; /** * The "cc" information for the message. * * @var array */ public $cc = []; /** * The "bcc" information for the message. * * @var array */ public $bcc = []; /** * The attachments for the message. * * @var array */ public $attachments = []; /** * The raw attachments for the message. * * @var array */ public $rawAttachments = []; /** * The tags for the message. * * @var array */ public $tags = []; /** * The metadata for the message. * * @var array */ public $metadata = []; /** * Priority level of the message. * * @var int */ public $priority; /** * The callbacks for the message. * * @var array */ public $callbacks = []; /** * Set the view for the mail message. * * @param array|string $view * @param array $data * @return $this */ public function view($view, array $data = []) { $this->view = $view; $this->viewData = $data; $this->markdown = null; return $this; } /** * Set the Markdown template for the notification. * * @param string $view * @param array $data * @return $this */ public function markdown($view, array $data = []) { $this->markdown = $view; $this->viewData = $data; $this->view = null; return $this; } /** * Set the default markdown template. * * @param string $template * @return $this */ public function template($template) { $this->markdown = $template; return $this; } /** * Set the theme to use with the Markdown template. * * @param string $theme * @return $this */ public function theme($theme) { $this->theme = $theme; return $this; } /** * Set the from address for the mail message. * * @param string $address * @param string|null $name * @return $this */ public function from($address, $name = null) { $this->from = [$address, $name]; return $this; } /** * Set the "reply to" address of the message. * * @param array|string $address * @param string|null $name * @return $this */ public function replyTo($address, $name = null) { if ($this->arrayOfAddresses($address)) { $this->replyTo += $this->parseAddresses($address); } else { $this->replyTo[] = [$address, $name]; } return $this; } /** * Set the cc address for the mail message. * * @param array|string $address * @param string|null $name * @return $this */ public function cc($address, $name = null) { if ($this->arrayOfAddresses($address)) { $this->cc += $this->parseAddresses($address); } else { $this->cc[] = [$address, $name]; } return $this; } /** * Set the bcc address for the mail message. * * @param array|string $address * @param string|null $name * @return $this */ public function bcc($address, $name = null) { if ($this->arrayOfAddresses($address)) { $this->bcc += $this->parseAddresses($address); } else { $this->bcc[] = [$address, $name]; } return $this; } /** * Attach a file to the message. * * @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file * @param array $options * @return $this */ public function attach($file, array $options = []) { if ($file instanceof Attachable) { $file = $file->toMailAttachment(); } if ($file instanceof Attachment) { return $file->attachTo($this); } $this->attachments[] = compact('file', 'options'); return $this; } /** * Attach multiple files to the message. * * @param array<string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment|array> $files * @return $this */ public function attachMany($files) { foreach ($files as $file => $options) { if (is_int($file)) { $this->attach($options); } else { $this->attach($file, $options); } } return $this; } /** * Attach in-memory data as an attachment. * * @param string $data * @param string $name * @param array $options * @return $this */ public function attachData($data, $name, array $options = []) { $this->rawAttachments[] = compact('data', 'name', 'options'); return $this; } /** * Add a tag header to the message when supported by the underlying transport. * * @param string $value * @return $this */ public function tag($value) { array_push($this->tags, $value); return $this; } /** * Add a metadata header to the message when supported by the underlying transport. * * @param string $key * @param string $value * @return $this */ public function metadata($key, $value) { $this->metadata[$key] = $value; return $this; } /** * Set the priority of this message. * * The value is an integer where 1 is the highest priority and 5 is the lowest. * * @param int $level * @return $this */ public function priority($level) { $this->priority = $level; return $this; } /** * Get the data array for the mail message. * * @return array */ public function data() { return array_merge($this->toArray(), $this->viewData); } /** * Parse the multi-address array into the necessary format. * * @param array $value * @return array */ protected function parseAddresses($value) { return collect($value)->map(function ($address, $name) { return [$address, is_numeric($name) ? null : $name]; })->values()->all(); } /** * Determine if the given "address" is actually an array of addresses. * * @param mixed $address * @return bool */ protected function arrayOfAddresses($address) { return is_iterable($address) || $address instanceof Arrayable; } /** * Render the mail notification message into an HTML string. * * @return \Illuminate\Support\HtmlString */ public function render() { if (isset($this->view)) { return Container::getInstance()->make('mailer')->render( $this->view, $this->data() ); } $markdown = Container::getInstance()->make(Markdown::class); return $markdown->theme($this->theme ?: $markdown->getTheme()) ->render($this->markdown, $this->data()); } /** * Register a callback to be called with the Symfony message instance. * * @param callable $callback * @return $this */ public function withSymfonyMessage($callback) { $this->callbacks[] = $callback; return $this; } } src/Illuminate/Redis/Limiters/DurationLimiter.php 0000644 00000011154 15107327040 0016121 0 ustar 00 <?php namespace Illuminate\Redis\Limiters; use Illuminate\Contracts\Redis\LimiterTimeoutException; use Illuminate\Support\Sleep; class DurationLimiter { /** * The Redis factory implementation. * * @var \Illuminate\Redis\Connections\Connection */ private $redis; /** * The unique name of the lock. * * @var string */ private $name; /** * The allowed number of concurrent tasks. * * @var int */ private $maxLocks; /** * The number of seconds a slot should be maintained. * * @var int */ private $decay; /** * The timestamp of the end of the current duration. * * @var int */ public $decaysAt; /** * The number of remaining slots. * * @var int */ public $remaining; /** * Create a new duration limiter instance. * * @param \Illuminate\Redis\Connections\Connection $redis * @param string $name * @param int $maxLocks * @param int $decay * @return void */ public function __construct($redis, $name, $maxLocks, $decay) { $this->name = $name; $this->decay = $decay; $this->redis = $redis; $this->maxLocks = $maxLocks; } /** * Attempt to acquire the lock for the given number of seconds. * * @param int $timeout * @param callable|null $callback * @param int $sleep * @return mixed * * @throws \Illuminate\Contracts\Redis\LimiterTimeoutException */ public function block($timeout, $callback = null, $sleep = 750) { $starting = time(); while (! $this->acquire()) { if (time() - $timeout >= $starting) { throw new LimiterTimeoutException; } Sleep::usleep($sleep * 1000); } if (is_callable($callback)) { return $callback(); } return true; } /** * Attempt to acquire the lock. * * @return bool */ public function acquire() { $results = $this->redis->eval( $this->luaScript(), 1, $this->name, microtime(true), time(), $this->decay, $this->maxLocks ); $this->decaysAt = $results[1]; $this->remaining = max(0, $results[2]); return (bool) $results[0]; } /** * Determine if the key has been "accessed" too many times. * * @return bool */ public function tooManyAttempts() { [$this->decaysAt, $this->remaining] = $this->redis->eval( $this->tooManyAttemptsLuaScript(), 1, $this->name, microtime(true), time(), $this->decay, $this->maxLocks ); return $this->remaining <= 0; } /** * Clear the limiter. * * @return void */ public function clear() { $this->redis->del($this->name); } /** * Get the Lua script for acquiring a lock. * * KEYS[1] - The limiter name * ARGV[1] - Current time in microseconds * ARGV[2] - Current time in seconds * ARGV[3] - Duration of the bucket * ARGV[4] - Allowed number of tasks * * @return string */ protected function luaScript() { return <<<'LUA' local function reset() redis.call('HMSET', KEYS[1], 'start', ARGV[2], 'end', ARGV[2] + ARGV[3], 'count', 1) return redis.call('EXPIRE', KEYS[1], ARGV[3] * 2) end if redis.call('EXISTS', KEYS[1]) == 0 then return {reset(), ARGV[2] + ARGV[3], ARGV[4] - 1} end if ARGV[1] >= redis.call('HGET', KEYS[1], 'start') and ARGV[1] <= redis.call('HGET', KEYS[1], 'end') then return { tonumber(redis.call('HINCRBY', KEYS[1], 'count', 1)) <= tonumber(ARGV[4]), redis.call('HGET', KEYS[1], 'end'), ARGV[4] - redis.call('HGET', KEYS[1], 'count') } end return {reset(), ARGV[2] + ARGV[3], ARGV[4] - 1} LUA; } /** * Get the Lua script to determine if the key has been "accessed" too many times. * * KEYS[1] - The limiter name * ARGV[1] - Current time in microseconds * ARGV[2] - Current time in seconds * ARGV[3] - Duration of the bucket * ARGV[4] - Allowed number of tasks * * @return string */ protected function tooManyAttemptsLuaScript() { return <<<'LUA' if redis.call('EXISTS', KEYS[1]) == 0 then return {0, ARGV[2] + ARGV[3]} end if ARGV[1] >= redis.call('HGET', KEYS[1], 'start') and ARGV[1] <= redis.call('HGET', KEYS[1], 'end') then return { redis.call('HGET', KEYS[1], 'end'), ARGV[4] - redis.call('HGET', KEYS[1], 'count') } end return {0, ARGV[2] + ARGV[3]} LUA; } } src/Illuminate/Redis/Limiters/ConcurrencyLimiterBuilder.php 0000644 00000006031 15107327040 0020133 0 ustar 00 <?php namespace Illuminate\Redis\Limiters; use Illuminate\Contracts\Redis\LimiterTimeoutException; use Illuminate\Support\InteractsWithTime; class ConcurrencyLimiterBuilder { use InteractsWithTime; /** * The Redis connection. * * @var \Illuminate\Redis\Connections\Connection */ public $connection; /** * The name of the lock. * * @var string */ public $name; /** * The maximum number of entities that can hold the lock at the same time. * * @var int */ public $maxLocks; /** * The number of seconds to maintain the lock until it is automatically released. * * @var int */ public $releaseAfter = 60; /** * The amount of time to block until a lock is available. * * @var int */ public $timeout = 3; /** * The number of milliseconds to wait between attempts to acquire the lock. * * @var int */ public $sleep = 250; /** * Create a new builder instance. * * @param \Illuminate\Redis\Connections\Connection $connection * @param string $name * @return void */ public function __construct($connection, $name) { $this->name = $name; $this->connection = $connection; } /** * Set the maximum number of locks that can be obtained per time window. * * @param int $maxLocks * @return $this */ public function limit($maxLocks) { $this->maxLocks = $maxLocks; return $this; } /** * Set the number of seconds until the lock will be released. * * @param int $releaseAfter * @return $this */ public function releaseAfter($releaseAfter) { $this->releaseAfter = $this->secondsUntil($releaseAfter); return $this; } /** * Set the amount of time to block until a lock is available. * * @param int $timeout * @return $this */ public function block($timeout) { $this->timeout = $timeout; return $this; } /** * The number of milliseconds to wait between lock acquisition attempts. * * @param int $sleep * @return $this */ public function sleep($sleep) { $this->sleep = $sleep; return $this; } /** * Execute the given callback if a lock is obtained, otherwise call the failure callback. * * @param callable $callback * @param callable|null $failure * @return mixed * * @throws \Illuminate\Contracts\Redis\LimiterTimeoutException */ public function then(callable $callback, callable $failure = null) { try { return (new ConcurrencyLimiter( $this->connection, $this->name, $this->maxLocks, $this->releaseAfter ))->block($this->timeout, $callback, $this->sleep); } catch (LimiterTimeoutException $e) { if ($failure) { return $failure($e); } throw $e; } } } src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php 0000644 00000007463 15107327040 0016636 0 ustar 00 <?php namespace Illuminate\Redis\Limiters; use Illuminate\Contracts\Redis\LimiterTimeoutException; use Illuminate\Support\Sleep; use Illuminate\Support\Str; use Throwable; class ConcurrencyLimiter { /** * The Redis factory implementation. * * @var \Illuminate\Redis\Connections\Connection */ protected $redis; /** * The name of the limiter. * * @var string */ protected $name; /** * The allowed number of concurrent tasks. * * @var int */ protected $maxLocks; /** * The number of seconds a slot should be maintained. * * @var int */ protected $releaseAfter; /** * Create a new concurrency limiter instance. * * @param \Illuminate\Redis\Connections\Connection $redis * @param string $name * @param int $maxLocks * @param int $releaseAfter * @return void */ public function __construct($redis, $name, $maxLocks, $releaseAfter) { $this->name = $name; $this->redis = $redis; $this->maxLocks = $maxLocks; $this->releaseAfter = $releaseAfter; } /** * Attempt to acquire the lock for the given number of seconds. * * @param int $timeout * @param callable|null $callback * @param int $sleep * @return mixed * * @throws \Illuminate\Contracts\Redis\LimiterTimeoutException * @throws \Throwable */ public function block($timeout, $callback = null, $sleep = 250) { $starting = time(); $id = Str::random(20); while (! $slot = $this->acquire($id)) { if (time() - $timeout >= $starting) { throw new LimiterTimeoutException; } Sleep::usleep($sleep * 1000); } if (is_callable($callback)) { try { return tap($callback(), function () use ($slot, $id) { $this->release($slot, $id); }); } catch (Throwable $exception) { $this->release($slot, $id); throw $exception; } } return true; } /** * Attempt to acquire the lock. * * @param string $id A unique identifier for this lock * @return mixed */ protected function acquire($id) { $slots = array_map(function ($i) { return $this->name.$i; }, range(1, $this->maxLocks)); return $this->redis->eval(...array_merge( [$this->lockScript(), count($slots)], array_merge($slots, [$this->name, $this->releaseAfter, $id]) )); } /** * Get the Lua script for acquiring a lock. * * KEYS - The keys that represent available slots * ARGV[1] - The limiter name * ARGV[2] - The number of seconds the slot should be reserved * ARGV[3] - The unique identifier for this lock * * @return string */ protected function lockScript() { return <<<'LUA' for index, value in pairs(redis.call('mget', unpack(KEYS))) do if not value then redis.call('set', KEYS[index], ARGV[3], "EX", ARGV[2]) return ARGV[1]..index end end LUA; } /** * Release the lock. * * @param string $key * @param string $id * @return void */ protected function release($key, $id) { $this->redis->eval($this->releaseScript(), 1, $key, $id); } /** * Get the Lua script to atomically release a lock. * * KEYS[1] - The name of the lock * ARGV[1] - The unique identifier for this lock * * @return string */ protected function releaseScript() { return <<<'LUA' if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end LUA; } } src/Illuminate/Redis/Limiters/DurationLimiterBuilder.php 0000644 00000005726 15107327040 0017440 0 ustar 00 <?php namespace Illuminate\Redis\Limiters; use Illuminate\Contracts\Redis\LimiterTimeoutException; use Illuminate\Support\InteractsWithTime; class DurationLimiterBuilder { use InteractsWithTime; /** * The Redis connection. * * @var \Illuminate\Redis\Connections\Connection */ public $connection; /** * The name of the lock. * * @var string */ public $name; /** * The maximum number of locks that can be obtained per time window. * * @var int */ public $maxLocks; /** * The amount of time the lock window is maintained. * * @var int */ public $decay; /** * The amount of time to block until a lock is available. * * @var int */ public $timeout = 3; /** * The number of milliseconds to wait between attempts to acquire the lock. * * @var int */ public $sleep = 750; /** * Create a new builder instance. * * @param \Illuminate\Redis\Connections\Connection $connection * @param string $name * @return void */ public function __construct($connection, $name) { $this->name = $name; $this->connection = $connection; } /** * Set the maximum number of locks that can be obtained per time window. * * @param int $maxLocks * @return $this */ public function allow($maxLocks) { $this->maxLocks = $maxLocks; return $this; } /** * Set the amount of time the lock window is maintained. * * @param \DateTimeInterface|\DateInterval|int $decay * @return $this */ public function every($decay) { $this->decay = $this->secondsUntil($decay); return $this; } /** * Set the amount of time to block until a lock is available. * * @param int $timeout * @return $this */ public function block($timeout) { $this->timeout = $timeout; return $this; } /** * The number of milliseconds to wait between lock acquisition attempts. * * @param int $sleep * @return $this */ public function sleep($sleep) { $this->sleep = $sleep; return $this; } /** * Execute the given callback if a lock is obtained, otherwise call the failure callback. * * @param callable $callback * @param callable|null $failure * @return mixed * * @throws \Illuminate\Contracts\Redis\LimiterTimeoutException */ public function then(callable $callback, callable $failure = null) { try { return (new DurationLimiter( $this->connection, $this->name, $this->maxLocks, $this->decay ))->block($this->timeout, $callback, $this->sleep); } catch (LimiterTimeoutException $e) { if ($failure) { return $failure($e); } throw $e; } } } src/Illuminate/Redis/RedisManager.php 0000644 00000015002 15107327040 0013553 0 ustar 00 <?php namespace Illuminate\Redis; use Closure; use Illuminate\Contracts\Redis\Factory; use Illuminate\Redis\Connections\Connection; use Illuminate\Redis\Connectors\PhpRedisConnector; use Illuminate\Redis\Connectors\PredisConnector; use Illuminate\Support\Arr; use Illuminate\Support\ConfigurationUrlParser; use InvalidArgumentException; /** * @mixin \Illuminate\Redis\Connections\Connection */ class RedisManager implements Factory { /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The name of the default driver. * * @var string */ protected $driver; /** * The registered custom driver creators. * * @var array */ protected $customCreators = []; /** * The Redis server configurations. * * @var array */ protected $config; /** * The Redis connections. * * @var mixed */ protected $connections; /** * Indicates whether event dispatcher is set on connections. * * @var bool */ protected $events = false; /** * Create a new Redis manager instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @param string $driver * @param array $config * @return void */ public function __construct($app, $driver, array $config) { $this->app = $app; $this->driver = $driver; $this->config = $config; } /** * Get a Redis connection by name. * * @param string|null $name * @return \Illuminate\Redis\Connections\Connection */ public function connection($name = null) { $name = $name ?: 'default'; if (isset($this->connections[$name])) { return $this->connections[$name]; } return $this->connections[$name] = $this->configure( $this->resolve($name), $name ); } /** * Resolve the given connection by name. * * @param string|null $name * @return \Illuminate\Redis\Connections\Connection * * @throws \InvalidArgumentException */ public function resolve($name = null) { $name = $name ?: 'default'; $options = $this->config['options'] ?? []; if (isset($this->config[$name])) { return $this->connector()->connect( $this->parseConnectionConfiguration($this->config[$name]), array_merge(Arr::except($options, 'parameters'), ['parameters' => Arr::get($options, 'parameters.'.$name, Arr::get($options, 'parameters', []))]) ); } if (isset($this->config['clusters'][$name])) { return $this->resolveCluster($name); } throw new InvalidArgumentException("Redis connection [{$name}] not configured."); } /** * Resolve the given cluster connection by name. * * @param string $name * @return \Illuminate\Redis\Connections\Connection */ protected function resolveCluster($name) { return $this->connector()->connectToCluster( array_map(function ($config) { return $this->parseConnectionConfiguration($config); }, $this->config['clusters'][$name]), $this->config['clusters']['options'] ?? [], $this->config['options'] ?? [] ); } /** * Configure the given connection to prepare it for commands. * * @param \Illuminate\Redis\Connections\Connection $connection * @param string $name * @return \Illuminate\Redis\Connections\Connection */ protected function configure(Connection $connection, $name) { $connection->setName($name); if ($this->events && $this->app->bound('events')) { $connection->setEventDispatcher($this->app->make('events')); } return $connection; } /** * Get the connector instance for the current driver. * * @return \Illuminate\Contracts\Redis\Connector|null */ protected function connector() { $customCreator = $this->customCreators[$this->driver] ?? null; if ($customCreator) { return $customCreator(); } return match ($this->driver) { 'predis' => new PredisConnector, 'phpredis' => new PhpRedisConnector, default => null, }; } /** * Parse the Redis connection configuration. * * @param mixed $config * @return array */ protected function parseConnectionConfiguration($config) { $parsed = (new ConfigurationUrlParser)->parseConfiguration($config); $driver = strtolower($parsed['driver'] ?? ''); if (in_array($driver, ['tcp', 'tls'])) { $parsed['scheme'] = $driver; } return array_filter($parsed, function ($key) { return ! in_array($key, ['driver'], true); }, ARRAY_FILTER_USE_KEY); } /** * Return all of the created connections. * * @return array */ public function connections() { return $this->connections; } /** * Enable the firing of Redis command events. * * @return void */ public function enableEvents() { $this->events = true; } /** * Disable the firing of Redis command events. * * @return void */ public function disableEvents() { $this->events = false; } /** * Set the default driver. * * @param string $driver * @return void */ public function setDriver($driver) { $this->driver = $driver; } /** * Disconnect the given connection and remove from local cache. * * @param string|null $name * @return void */ public function purge($name = null) { $name = $name ?: 'default'; unset($this->connections[$name]); } /** * Register a custom driver creator Closure. * * @param string $driver * @param \Closure $callback * @return $this */ public function extend($driver, Closure $callback) { $this->customCreators[$driver] = $callback->bindTo($this, $this); return $this; } /** * Pass methods onto the default Redis connection. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->connection()->{$method}(...$parameters); } } src/Illuminate/Redis/composer.json 0000644 00000002061 15107327040 0013224 0 ustar 00 { "name": "illuminate/redis", "description": "The Illuminate Redis package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/collections": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0", "illuminate/support": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Redis\\": "" } }, "suggest": { "ext-redis": "Required to use the phpredis connector (^4.0|^5.0).", "predis/predis": "Required to use the predis connector (^2.0.2)." }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Redis/Events/CommandExecuted.php 0000644 00000002245 15107327040 0015530 0 ustar 00 <?php namespace Illuminate\Redis\Events; class CommandExecuted { /** * The Redis command that was executed. * * @var string */ public $command; /** * The array of command parameters. * * @var array */ public $parameters; /** * The number of milliseconds it took to execute the command. * * @var float */ public $time; /** * The Redis connection instance. * * @var \Illuminate\Redis\Connections\Connection */ public $connection; /** * The Redis connection name. * * @var string */ public $connectionName; /** * Create a new event instance. * * @param string $command * @param array $parameters * @param float|null $time * @param \Illuminate\Redis\Connections\Connection $connection * @return void */ public function __construct($command, $parameters, $time, $connection) { $this->time = $time; $this->command = $command; $this->parameters = $parameters; $this->connection = $connection; $this->connectionName = $connection->getName(); } } src/Illuminate/Redis/Connections/PredisConnection.php 0000644 00000003243 15107327040 0016746 0 ustar 00 <?php namespace Illuminate\Redis\Connections; use Closure; use Illuminate\Contracts\Redis\Connection as ConnectionContract; use Predis\Command\Argument\ArrayableArgument; /** * @mixin \Predis\Client */ class PredisConnection extends Connection implements ConnectionContract { /** * The Predis client. * * @var \Predis\Client */ protected $client; /** * Create a new Predis connection. * * @param \Predis\Client $client * @return void */ public function __construct($client) { $this->client = $client; } /** * Subscribe to a set of given channels for messages. * * @param array|string $channels * @param \Closure $callback * @param string $method * @return void */ public function createSubscription($channels, Closure $callback, $method = 'subscribe') { $loop = $this->pubSubLoop(); $loop->{$method}(...array_values((array) $channels)); foreach ($loop as $message) { if ($message->kind === 'message' || $message->kind === 'pmessage') { $callback($message->payload, $message->channel); } } unset($loop); } /** * Parse the command's parameters for event dispatching. * * @param array $parameters * @return array */ protected function parseParametersForEvent(array $parameters) { return collect($parameters) ->transform(function ($parameter) { return $parameter instanceof ArrayableArgument ? $parameter->toArray() : $parameter; })->all(); } } src/Illuminate/Redis/Connections/PacksPhpRedisValues.php 0000644 00000011766 15107327040 0017371 0 ustar 00 <?php namespace Illuminate\Redis\Connections; use Redis; use RuntimeException; use UnexpectedValueException; trait PacksPhpRedisValues { /** * Indicates if Redis supports packing. * * @var bool|null */ protected $supportsPacking; /** * Indicates if Redis supports LZF compression. * * @var bool|null */ protected $supportsLzf; /** * Indicates if Redis supports Zstd compression. * * @var bool|null */ protected $supportsZstd; /** * Prepares the given values to be used with the `eval` command, including serialization and compression. * * @param array<int|string,string> $values * @return array<int|string,string> */ public function pack(array $values): array { if (empty($values)) { return $values; } if ($this->supportsPacking()) { return array_map([$this->client, '_pack'], $values); } if ($this->compressed()) { if ($this->supportsLzf() && $this->lzfCompressed()) { if (! function_exists('lzf_compress')) { throw new RuntimeException("'lzf' extension required to call 'lzf_compress'."); } $processor = function ($value) { return \lzf_compress($this->client->_serialize($value)); }; } elseif ($this->supportsZstd() && $this->zstdCompressed()) { if (! function_exists('zstd_compress')) { throw new RuntimeException("'zstd' extension required to call 'zstd_compress'."); } $compressionLevel = $this->client->getOption(Redis::OPT_COMPRESSION_LEVEL); $processor = function ($value) use ($compressionLevel) { return \zstd_compress( $this->client->_serialize($value), $compressionLevel === 0 ? Redis::COMPRESSION_ZSTD_DEFAULT : $compressionLevel ); }; } else { throw new UnexpectedValueException(sprintf( 'Unsupported phpredis compression in use [%d].', $this->client->getOption(Redis::OPT_COMPRESSION) )); } } else { $processor = function ($value) { return $this->client->_serialize($value); }; } return array_map($processor, $values); } /** * Determine if compression is enabled. * * @return bool */ public function compressed(): bool { return defined('Redis::OPT_COMPRESSION') && $this->client->getOption(Redis::OPT_COMPRESSION) !== Redis::COMPRESSION_NONE; } /** * Determine if LZF compression is enabled. * * @return bool */ public function lzfCompressed(): bool { return defined('Redis::COMPRESSION_LZF') && $this->client->getOption(Redis::OPT_COMPRESSION) === Redis::COMPRESSION_LZF; } /** * Determine if ZSTD compression is enabled. * * @return bool */ public function zstdCompressed(): bool { return defined('Redis::COMPRESSION_ZSTD') && $this->client->getOption(Redis::OPT_COMPRESSION) === Redis::COMPRESSION_ZSTD; } /** * Determine if LZ4 compression is enabled. * * @return bool */ public function lz4Compressed(): bool { return defined('Redis::COMPRESSION_LZ4') && $this->client->getOption(Redis::OPT_COMPRESSION) === Redis::COMPRESSION_LZ4; } /** * Determine if the current PhpRedis extension version supports packing. * * @return bool */ protected function supportsPacking(): bool { if ($this->supportsPacking === null) { $this->supportsPacking = $this->phpRedisVersionAtLeast('5.3.5'); } return $this->supportsPacking; } /** * Determine if the current PhpRedis extension version supports LZF compression. * * @return bool */ protected function supportsLzf(): bool { if ($this->supportsLzf === null) { $this->supportsLzf = $this->phpRedisVersionAtLeast('4.3.0'); } return $this->supportsLzf; } /** * Determine if the current PhpRedis extension version supports Zstd compression. * * @return bool */ protected function supportsZstd(): bool { if ($this->supportsZstd === null) { $this->supportsZstd = $this->phpRedisVersionAtLeast('5.1.0'); } return $this->supportsZstd; } /** * Determine if the PhpRedis extension version is at least the given version. * * @param string $version * @return bool */ protected function phpRedisVersionAtLeast(string $version): bool { $phpredisVersion = phpversion('redis'); return $phpredisVersion !== false && version_compare($phpredisVersion, $version, '>='); } } src/Illuminate/Redis/Connections/PhpRedisConnection.php 0000644 00000033776 15107327040 0017254 0 ustar 00 <?php namespace Illuminate\Redis\Connections; use Closure; use Illuminate\Contracts\Redis\Connection as ConnectionContract; use Redis; use RedisException; /** * @mixin \Redis */ class PhpRedisConnection extends Connection implements ConnectionContract { use PacksPhpRedisValues; /** * The connection creation callback. * * @var callable */ protected $connector; /** * The connection configuration array. * * @var array */ protected $config; /** * Create a new PhpRedis connection. * * @param \Redis $client * @param callable|null $connector * @param array $config * @return void */ public function __construct($client, callable $connector = null, array $config = []) { $this->client = $client; $this->config = $config; $this->connector = $connector; } /** * Returns the value of the given key. * * @param string $key * @return string|null */ public function get($key) { $result = $this->command('get', [$key]); return $result !== false ? $result : null; } /** * Get the values of all the given keys. * * @param array $keys * @return array */ public function mget(array $keys) { return array_map(function ($value) { return $value !== false ? $value : null; }, $this->command('mget', [$keys])); } /** * Set the string value in the argument as the value of the key. * * @param string $key * @param mixed $value * @param string|null $expireResolution * @param int|null $expireTTL * @param string|null $flag * @return bool */ public function set($key, $value, $expireResolution = null, $expireTTL = null, $flag = null) { return $this->command('set', [ $key, $value, $expireResolution ? [$flag, $expireResolution => $expireTTL] : null, ]); } /** * Set the given key if it doesn't exist. * * @param string $key * @param string $value * @return int */ public function setnx($key, $value) { return (int) $this->command('setnx', [$key, $value]); } /** * Get the value of the given hash fields. * * @param string $key * @param mixed ...$dictionary * @return array */ public function hmget($key, ...$dictionary) { if (count($dictionary) === 1) { $dictionary = $dictionary[0]; } return array_values($this->command('hmget', [$key, $dictionary])); } /** * Set the given hash fields to their respective values. * * @param string $key * @param mixed ...$dictionary * @return int */ public function hmset($key, ...$dictionary) { if (count($dictionary) === 1) { $dictionary = $dictionary[0]; } else { $input = collect($dictionary); $dictionary = $input->nth(2)->combine($input->nth(2, 1))->toArray(); } return $this->command('hmset', [$key, $dictionary]); } /** * Set the given hash field if it doesn't exist. * * @param string $hash * @param string $key * @param string $value * @return int */ public function hsetnx($hash, $key, $value) { return (int) $this->command('hsetnx', [$hash, $key, $value]); } /** * Removes the first count occurrences of the value element from the list. * * @param string $key * @param int $count * @param mixed $value * @return int|false */ public function lrem($key, $count, $value) { return $this->command('lrem', [$key, $value, $count]); } /** * Removes and returns the first element of the list stored at key. * * @param mixed ...$arguments * @return array|null */ public function blpop(...$arguments) { $result = $this->command('blpop', $arguments); return empty($result) ? null : $result; } /** * Removes and returns the last element of the list stored at key. * * @param mixed ...$arguments * @return array|null */ public function brpop(...$arguments) { $result = $this->command('brpop', $arguments); return empty($result) ? null : $result; } /** * Removes and returns a random element from the set value at key. * * @param string $key * @param int|null $count * @return mixed|false */ public function spop($key, $count = 1) { return $this->command('spop', func_get_args()); } /** * Add one or more members to a sorted set or update its score if it already exists. * * @param string $key * @param mixed ...$dictionary * @return int */ public function zadd($key, ...$dictionary) { if (is_array(end($dictionary))) { foreach (array_pop($dictionary) as $member => $score) { $dictionary[] = $score; $dictionary[] = $member; } } $options = []; foreach (array_slice($dictionary, 0, 3) as $i => $value) { if (in_array($value, ['nx', 'xx', 'ch', 'incr', 'gt', 'lt', 'NX', 'XX', 'CH', 'INCR', 'GT', 'LT'], true)) { $options[] = $value; unset($dictionary[$i]); } } return $this->command('zadd', array_merge([$key], [$options], array_values($dictionary))); } /** * Return elements with score between $min and $max. * * @param string $key * @param mixed $min * @param mixed $max * @param array $options * @return array */ public function zrangebyscore($key, $min, $max, $options = []) { if (isset($options['limit']) && ! array_is_list($options['limit'])) { $options['limit'] = [ $options['limit']['offset'], $options['limit']['count'], ]; } return $this->command('zRangeByScore', [$key, $min, $max, $options]); } /** * Return elements with score between $min and $max. * * @param string $key * @param mixed $min * @param mixed $max * @param array $options * @return array */ public function zrevrangebyscore($key, $min, $max, $options = []) { if (isset($options['limit']) && ! array_is_list($options['limit'])) { $options['limit'] = [ $options['limit']['offset'], $options['limit']['count'], ]; } return $this->command('zRevRangeByScore', [$key, $min, $max, $options]); } /** * Find the intersection between sets and store in a new set. * * @param string $output * @param array $keys * @param array $options * @return int */ public function zinterstore($output, $keys, $options = []) { return $this->command('zinterstore', [$output, $keys, $options['weights'] ?? null, $options['aggregate'] ?? 'sum', ]); } /** * Find the union between sets and store in a new set. * * @param string $output * @param array $keys * @param array $options * @return int */ public function zunionstore($output, $keys, $options = []) { return $this->command('zunionstore', [$output, $keys, $options['weights'] ?? null, $options['aggregate'] ?? 'sum', ]); } /** * Scans all keys based on options. * * @param mixed $cursor * @param array $options * @return mixed */ public function scan($cursor, $options = []) { $result = $this->client->scan($cursor, $options['match'] ?? '*', $options['count'] ?? 10 ); if ($result === false) { $result = []; } return $cursor === 0 && empty($result) ? false : [$cursor, $result]; } /** * Scans the given set for all values based on options. * * @param string $key * @param mixed $cursor * @param array $options * @return mixed */ public function zscan($key, $cursor, $options = []) { $result = $this->client->zscan($key, $cursor, $options['match'] ?? '*', $options['count'] ?? 10 ); if ($result === false) { $result = []; } return $cursor === 0 && empty($result) ? false : [$cursor, $result]; } /** * Scans the given hash for all values based on options. * * @param string $key * @param mixed $cursor * @param array $options * @return mixed */ public function hscan($key, $cursor, $options = []) { $result = $this->client->hscan($key, $cursor, $options['match'] ?? '*', $options['count'] ?? 10 ); if ($result === false) { $result = []; } return $cursor === 0 && empty($result) ? false : [$cursor, $result]; } /** * Scans the given set for all values based on options. * * @param string $key * @param mixed $cursor * @param array $options * @return mixed */ public function sscan($key, $cursor, $options = []) { $result = $this->client->sscan($key, $cursor, $options['match'] ?? '*', $options['count'] ?? 10 ); if ($result === false) { $result = []; } return $cursor === 0 && empty($result) ? false : [$cursor, $result]; } /** * Execute commands in a pipeline. * * @param callable|null $callback * @return \Redis|array */ public function pipeline(callable $callback = null) { $pipeline = $this->client()->pipeline(); return is_null($callback) ? $pipeline : tap($pipeline, $callback)->exec(); } /** * Execute commands in a transaction. * * @param callable|null $callback * @return \Redis|array */ public function transaction(callable $callback = null) { $transaction = $this->client()->multi(); return is_null($callback) ? $transaction : tap($transaction, $callback)->exec(); } /** * Evaluate a LUA script serverside, from the SHA1 hash of the script instead of the script itself. * * @param string $script * @param int $numkeys * @param mixed ...$arguments * @return mixed */ public function evalsha($script, $numkeys, ...$arguments) { return $this->command('evalsha', [ $this->script('load', $script), $arguments, $numkeys, ]); } /** * Evaluate a script and return its result. * * @param string $script * @param int $numberOfKeys * @param mixed ...$arguments * @return mixed */ public function eval($script, $numberOfKeys, ...$arguments) { return $this->command('eval', [$script, $arguments, $numberOfKeys]); } /** * Subscribe to a set of given channels for messages. * * @param array|string $channels * @param \Closure $callback * @return void */ public function subscribe($channels, Closure $callback) { $this->client->subscribe((array) $channels, function ($redis, $channel, $message) use ($callback) { $callback($message, $channel); }); } /** * Subscribe to a set of given channels with wildcards. * * @param array|string $channels * @param \Closure $callback * @return void */ public function psubscribe($channels, Closure $callback) { $this->client->psubscribe((array) $channels, function ($redis, $pattern, $channel, $message) use ($callback) { $callback($message, $channel); }); } /** * Subscribe to a set of given channels for messages. * * @param array|string $channels * @param \Closure $callback * @param string $method * @return void */ public function createSubscription($channels, Closure $callback, $method = 'subscribe') { // } /** * Flush the selected Redis database. * * @return mixed */ public function flushdb() { $arguments = func_get_args(); if (strtoupper((string) ($arguments[0] ?? null)) === 'ASYNC') { return $this->command('flushdb', [true]); } return $this->command('flushdb'); } /** * Execute a raw command. * * @param array $parameters * @return mixed */ public function executeRaw(array $parameters) { return $this->command('rawCommand', $parameters); } /** * Run a command against the Redis database. * * @param string $method * @param array $parameters * @return mixed * * @throws \RedisException */ public function command($method, array $parameters = []) { try { return parent::command($method, $parameters); } catch (RedisException $e) { foreach (['went away', 'socket', 'read error on connection', 'Connection lost'] as $errorMessage) { if (str_contains($e->getMessage(), $errorMessage)) { $this->client = $this->connector ? call_user_func($this->connector) : $this->client; break; } } throw $e; } } /** * Disconnects from the Redis instance. * * @return void */ public function disconnect() { $this->client->close(); } /** * Pass other method calls down to the underlying client. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return parent::__call(strtolower($method), $parameters); } } src/Illuminate/Redis/Connections/Connection.php 0000644 00000012130 15107327040 0015572 0 ustar 00 <?php namespace Illuminate\Redis\Connections; use Closure; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Redis\Events\CommandExecuted; use Illuminate\Redis\Limiters\ConcurrencyLimiterBuilder; use Illuminate\Redis\Limiters\DurationLimiterBuilder; use Illuminate\Support\Traits\Macroable; abstract class Connection { use Macroable { __call as macroCall; } /** * The Redis client. * * @var \Redis */ protected $client; /** * The Redis connection name. * * @var string|null */ protected $name; /** * The event dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * Subscribe to a set of given channels for messages. * * @param array|string $channels * @param \Closure $callback * @param string $method * @return void */ abstract public function createSubscription($channels, Closure $callback, $method = 'subscribe'); /** * Funnel a callback for a maximum number of simultaneous executions. * * @param string $name * @return \Illuminate\Redis\Limiters\ConcurrencyLimiterBuilder */ public function funnel($name) { return new ConcurrencyLimiterBuilder($this, $name); } /** * Throttle a callback for a maximum number of executions over a given duration. * * @param string $name * @return \Illuminate\Redis\Limiters\DurationLimiterBuilder */ public function throttle($name) { return new DurationLimiterBuilder($this, $name); } /** * Get the underlying Redis client. * * @return mixed */ public function client() { return $this->client; } /** * Subscribe to a set of given channels for messages. * * @param array|string $channels * @param \Closure $callback * @return void */ public function subscribe($channels, Closure $callback) { return $this->createSubscription($channels, $callback, __FUNCTION__); } /** * Subscribe to a set of given channels with wildcards. * * @param array|string $channels * @param \Closure $callback * @return void */ public function psubscribe($channels, Closure $callback) { return $this->createSubscription($channels, $callback, __FUNCTION__); } /** * Run a command against the Redis database. * * @param string $method * @param array $parameters * @return mixed */ public function command($method, array $parameters = []) { $start = microtime(true); $result = $this->client->{$method}(...$parameters); $time = round((microtime(true) - $start) * 1000, 2); if (isset($this->events)) { $this->event(new CommandExecuted( $method, $this->parseParametersForEvent($parameters), $time, $this )); } return $result; } /** * Parse the command's parameters for event dispatching. * * @param array $parameters * @return array */ protected function parseParametersForEvent(array $parameters) { return $parameters; } /** * Fire the given event if possible. * * @param mixed $event * @return void */ protected function event($event) { $this->events?->dispatch($event); } /** * Register a Redis command listener with the connection. * * @param \Closure $callback * @return void */ public function listen(Closure $callback) { $this->events?->listen(CommandExecuted::class, $callback); } /** * Get the connection name. * * @return string|null */ public function getName() { return $this->name; } /** * Set the connections name. * * @param string $name * @return $this */ public function setName($name) { $this->name = $name; return $this; } /** * Get the event dispatcher used by the connection. * * @return \Illuminate\Contracts\Events\Dispatcher */ public function getEventDispatcher() { return $this->events; } /** * Set the event dispatcher instance on the connection. * * @param \Illuminate\Contracts\Events\Dispatcher $events * @return void */ public function setEventDispatcher(Dispatcher $events) { $this->events = $events; } /** * Unset the event dispatcher instance on the connection. * * @return void */ public function unsetEventDispatcher() { $this->events = null; } /** * Pass other method calls down to the underlying client. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } return $this->command($method, $parameters); } } src/Illuminate/Redis/Connections/PredisClusterConnection.php 0000644 00000001130 15107327040 0020301 0 ustar 00 <?php namespace Illuminate\Redis\Connections; use Predis\Command\Redis\FLUSHDB; use Predis\Command\ServerFlushDatabase; class PredisClusterConnection extends PredisConnection { /** * Flush the selected Redis database on all cluster nodes. * * @return void */ public function flushdb() { $command = class_exists(ServerFlushDatabase::class) ? ServerFlushDatabase::class : FLUSHDB::class; foreach ($this->client as $node) { $node->executeCommand(tap(new $command)->setArguments(func_get_args())); } } } src/Illuminate/Redis/Connections/PhpRedisClusterConnection.php 0000644 00000001122 15107327040 0020572 0 ustar 00 <?php namespace Illuminate\Redis\Connections; class PhpRedisClusterConnection extends PhpRedisConnection { /** * Flush the selected Redis database on all master nodes. * * @return mixed */ public function flushdb() { $arguments = func_get_args(); $async = strtoupper((string) ($arguments[0] ?? null)) === 'ASYNC'; foreach ($this->client->_masters() as $master) { $async ? $this->command('rawCommand', [$master, 'flushdb', 'async']) : $this->command('flushdb', [$master]); } } } src/Illuminate/Redis/LICENSE.md 0000644 00000002063 15107327040 0012110 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Redis/Connectors/PredisConnector.php 0000644 00000003027 15107327040 0016434 0 ustar 00 <?php namespace Illuminate\Redis\Connectors; use Illuminate\Contracts\Redis\Connector; use Illuminate\Redis\Connections\PredisClusterConnection; use Illuminate\Redis\Connections\PredisConnection; use Illuminate\Support\Arr; use Predis\Client; class PredisConnector implements Connector { /** * Create a new connection. * * @param array $config * @param array $options * @return \Illuminate\Redis\Connections\PredisConnection */ public function connect(array $config, array $options) { $formattedOptions = array_merge( ['timeout' => 10.0], $options, Arr::pull($config, 'options', []) ); if (isset($config['prefix'])) { $formattedOptions['prefix'] = $config['prefix']; } return new PredisConnection(new Client($config, $formattedOptions)); } /** * Create a new clustered Predis connection. * * @param array $config * @param array $clusterOptions * @param array $options * @return \Illuminate\Redis\Connections\PredisClusterConnection */ public function connectToCluster(array $config, array $clusterOptions, array $options) { $clusterSpecificOptions = Arr::pull($config, 'options', []); if (isset($config['prefix'])) { $clusterSpecificOptions['prefix'] = $config['prefix']; } return new PredisClusterConnection(new Client(array_values($config), array_merge( $options, $clusterOptions, $clusterSpecificOptions ))); } } src/Illuminate/Redis/Connectors/PhpRedisConnector.php 0000644 00000016767 15107327040 0016743 0 ustar 00 <?php namespace Illuminate\Redis\Connectors; use Illuminate\Contracts\Redis\Connector; use Illuminate\Redis\Connections\PhpRedisClusterConnection; use Illuminate\Redis\Connections\PhpRedisConnection; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Redis as RedisFacade; use Illuminate\Support\Str; use LogicException; use Redis; use RedisCluster; class PhpRedisConnector implements Connector { /** * Create a new connection. * * @param array $config * @param array $options * @return \Illuminate\Redis\Connections\PhpRedisConnection */ public function connect(array $config, array $options) { $formattedOptions = Arr::pull($config, 'options', []); if (isset($config['prefix'])) { $formattedOptions['prefix'] = $config['prefix']; } $connector = function () use ($config, $options, $formattedOptions) { return $this->createClient(array_merge( $config, $options, $formattedOptions )); }; return new PhpRedisConnection($connector(), $connector, $config); } /** * Create a new clustered PhpRedis connection. * * @param array $config * @param array $clusterOptions * @param array $options * @return \Illuminate\Redis\Connections\PhpRedisClusterConnection */ public function connectToCluster(array $config, array $clusterOptions, array $options) { $options = array_merge($options, $clusterOptions, Arr::pull($config, 'options', [])); return new PhpRedisClusterConnection($this->createRedisClusterInstance( array_map([$this, 'buildClusterConnectionString'], $config), $options )); } /** * Build a single cluster seed string from an array. * * @param array $server * @return string */ protected function buildClusterConnectionString(array $server) { return $this->formatHost($server).':'.$server['port'].'?'.Arr::query(Arr::only($server, [ 'database', 'password', 'prefix', 'read_timeout', ])); } /** * Create the Redis client instance. * * @param array $config * @return \Redis * * @throws \LogicException */ protected function createClient(array $config) { return tap(new Redis, function ($client) use ($config) { if ($client instanceof RedisFacade) { throw new LogicException( extension_loaded('redis') ? 'Please remove or rename the Redis facade alias in your "app" configuration file in order to avoid collision with the PHP Redis extension.' : 'Please make sure the PHP Redis extension is installed and enabled.' ); } $this->establishConnection($client, $config); if (! empty($config['password'])) { if (isset($config['username']) && $config['username'] !== '' && is_string($config['password'])) { $client->auth([$config['username'], $config['password']]); } else { $client->auth($config['password']); } } if (isset($config['database'])) { $client->select((int) $config['database']); } if (! empty($config['prefix'])) { $client->setOption(Redis::OPT_PREFIX, $config['prefix']); } if (! empty($config['read_timeout'])) { $client->setOption(Redis::OPT_READ_TIMEOUT, $config['read_timeout']); } if (! empty($config['scan'])) { $client->setOption(Redis::OPT_SCAN, $config['scan']); } if (! empty($config['name'])) { $client->client('SETNAME', $config['name']); } if (array_key_exists('serializer', $config)) { $client->setOption(Redis::OPT_SERIALIZER, $config['serializer']); } if (array_key_exists('compression', $config)) { $client->setOption(Redis::OPT_COMPRESSION, $config['compression']); } if (array_key_exists('compression_level', $config)) { $client->setOption(Redis::OPT_COMPRESSION_LEVEL, $config['compression_level']); } }); } /** * Establish a connection with the Redis host. * * @param \Redis $client * @param array $config * @return void */ protected function establishConnection($client, array $config) { $persistent = $config['persistent'] ?? false; $parameters = [ $this->formatHost($config), $config['port'], Arr::get($config, 'timeout', 0.0), $persistent ? Arr::get($config, 'persistent_id', null) : null, Arr::get($config, 'retry_interval', 0), ]; if (version_compare(phpversion('redis'), '3.1.3', '>=')) { $parameters[] = Arr::get($config, 'read_timeout', 0.0); } if (version_compare(phpversion('redis'), '5.3.0', '>=') && ! is_null($context = Arr::get($config, 'context'))) { $parameters[] = $context; } $client->{$persistent ? 'pconnect' : 'connect'}(...$parameters); } /** * Create a new redis cluster instance. * * @param array $servers * @param array $options * @return \RedisCluster */ protected function createRedisClusterInstance(array $servers, array $options) { $parameters = [ null, array_values($servers), $options['timeout'] ?? 0, $options['read_timeout'] ?? 0, isset($options['persistent']) && $options['persistent'], ]; if (version_compare(phpversion('redis'), '4.3.0', '>=')) { $parameters[] = $options['password'] ?? null; } if (version_compare(phpversion('redis'), '5.3.2', '>=') && ! is_null($context = Arr::get($options, 'context'))) { $parameters[] = $context; } return tap(new RedisCluster(...$parameters), function ($client) use ($options) { if (! empty($options['prefix'])) { $client->setOption(Redis::OPT_PREFIX, $options['prefix']); } if (! empty($options['scan'])) { $client->setOption(Redis::OPT_SCAN, $options['scan']); } if (! empty($options['failover'])) { $client->setOption(RedisCluster::OPT_SLAVE_FAILOVER, $options['failover']); } if (! empty($options['name'])) { $client->client('SETNAME', $options['name']); } if (array_key_exists('serializer', $options)) { $client->setOption(Redis::OPT_SERIALIZER, $options['serializer']); } if (array_key_exists('compression', $options)) { $client->setOption(Redis::OPT_COMPRESSION, $options['compression']); } if (array_key_exists('compression_level', $options)) { $client->setOption(Redis::OPT_COMPRESSION_LEVEL, $options['compression_level']); } }); } /** * Format the host using the scheme if available. * * @param array $options * @return string */ protected function formatHost(array $options) { if (isset($options['scheme'])) { return Str::start($options['host'], "{$options['scheme']}://"); } return $options['host']; } } src/Illuminate/Redis/RedisServiceProvider.php 0000644 00000001616 15107327040 0015322 0 ustar 00 <?php namespace Illuminate\Redis; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\Arr; use Illuminate\Support\ServiceProvider; class RedisServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton('redis', function ($app) { $config = $app->make('config')->get('database.redis', []); return new RedisManager($app, Arr::pull($config, 'client', 'phpredis'), $config); }); $this->app->bind('redis.connection', function ($app) { return $app['redis']->connection(); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return ['redis', 'redis.connection']; } } src/Illuminate/Bus/Batch.php 0000644 00000027600 15107327040 0011725 0 ustar 00 <?php namespace Illuminate\Bus; use Carbon\CarbonImmutable; use Closure; use Illuminate\Contracts\Queue\Factory as QueueFactory; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Queue\CallQueuedClosure; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use JsonSerializable; use Throwable; class Batch implements Arrayable, JsonSerializable { /** * The queue factory implementation. * * @var \Illuminate\Contracts\Queue\Factory */ protected $queue; /** * The repository implementation. * * @var \Illuminate\Bus\BatchRepository */ protected $repository; /** * The batch ID. * * @var string */ public $id; /** * The batch name. * * @var string */ public $name; /** * The total number of jobs that belong to the batch. * * @var int */ public $totalJobs; /** * The total number of jobs that are still pending. * * @var int */ public $pendingJobs; /** * The total number of jobs that have failed. * * @var int */ public $failedJobs; /** * The IDs of the jobs that have failed. * * @var array */ public $failedJobIds; /** * The batch options. * * @var array */ public $options; /** * The date indicating when the batch was created. * * @var \Carbon\CarbonImmutable */ public $createdAt; /** * The date indicating when the batch was cancelled. * * @var \Carbon\CarbonImmutable|null */ public $cancelledAt; /** * The date indicating when the batch was finished. * * @var \Carbon\CarbonImmutable|null */ public $finishedAt; /** * Create a new batch instance. * * @param \Illuminate\Contracts\Queue\Factory $queue * @param \Illuminate\Bus\BatchRepository $repository * @param string $id * @param string $name * @param int $totalJobs * @param int $pendingJobs * @param int $failedJobs * @param array $failedJobIds * @param array $options * @param \Carbon\CarbonImmutable $createdAt * @param \Carbon\CarbonImmutable|null $cancelledAt * @param \Carbon\CarbonImmutable|null $finishedAt * @return void */ public function __construct(QueueFactory $queue, BatchRepository $repository, string $id, string $name, int $totalJobs, int $pendingJobs, int $failedJobs, array $failedJobIds, array $options, CarbonImmutable $createdAt, ?CarbonImmutable $cancelledAt = null, ?CarbonImmutable $finishedAt = null) { $this->queue = $queue; $this->repository = $repository; $this->id = $id; $this->name = $name; $this->totalJobs = $totalJobs; $this->pendingJobs = $pendingJobs; $this->failedJobs = $failedJobs; $this->failedJobIds = $failedJobIds; $this->options = $options; $this->createdAt = $createdAt; $this->cancelledAt = $cancelledAt; $this->finishedAt = $finishedAt; } /** * Get a fresh instance of the batch represented by this ID. * * @return self */ public function fresh() { return $this->repository->find($this->id); } /** * Add additional jobs to the batch. * * @param \Illuminate\Support\Enumerable|object|array $jobs * @return self */ public function add($jobs) { $count = 0; $jobs = Collection::wrap($jobs)->map(function ($job) use (&$count) { $job = $job instanceof Closure ? CallQueuedClosure::create($job) : $job; if (is_array($job)) { $count += count($job); return with($this->prepareBatchedChain($job), function ($chain) { return $chain->first() ->allOnQueue($this->options['queue'] ?? null) ->allOnConnection($this->options['connection'] ?? null) ->chain($chain->slice(1)->values()->all()); }); } else { $job->withBatchId($this->id); $count++; } return $job; }); $this->repository->transaction(function () use ($jobs, $count) { $this->repository->incrementTotalJobs($this->id, $count); $this->queue->connection($this->options['connection'] ?? null)->bulk( $jobs->all(), $data = '', $this->options['queue'] ?? null ); }); return $this->fresh(); } /** * Prepare a chain that exists within the jobs being added. * * @param array $chain * @return \Illuminate\Support\Collection */ protected function prepareBatchedChain(array $chain) { return collect($chain)->map(function ($job) { $job = $job instanceof Closure ? CallQueuedClosure::create($job) : $job; return $job->withBatchId($this->id); }); } /** * Get the total number of jobs that have been processed by the batch thus far. * * @return int */ public function processedJobs() { return $this->totalJobs - $this->pendingJobs; } /** * Get the percentage of jobs that have been processed (between 0-100). * * @return int */ public function progress() { return $this->totalJobs > 0 ? round(($this->processedJobs() / $this->totalJobs) * 100) : 0; } /** * Record that a job within the batch finished successfully, executing any callbacks if necessary. * * @param string $jobId * @return void */ public function recordSuccessfulJob(string $jobId) { $counts = $this->decrementPendingJobs($jobId); if ($counts->pendingJobs === 0) { $this->repository->markAsFinished($this->id); } if ($counts->pendingJobs === 0 && $this->hasThenCallbacks()) { $batch = $this->fresh(); collect($this->options['then'])->each(function ($handler) use ($batch) { $this->invokeHandlerCallback($handler, $batch); }); } if ($counts->allJobsHaveRanExactlyOnce() && $this->hasFinallyCallbacks()) { $batch = $this->fresh(); collect($this->options['finally'])->each(function ($handler) use ($batch) { $this->invokeHandlerCallback($handler, $batch); }); } } /** * Decrement the pending jobs for the batch. * * @param string $jobId * @return \Illuminate\Bus\UpdatedBatchJobCounts */ public function decrementPendingJobs(string $jobId) { return $this->repository->decrementPendingJobs($this->id, $jobId); } /** * Determine if the batch has finished executing. * * @return bool */ public function finished() { return ! is_null($this->finishedAt); } /** * Determine if the batch has "success" callbacks. * * @return bool */ public function hasThenCallbacks() { return isset($this->options['then']) && ! empty($this->options['then']); } /** * Determine if the batch allows jobs to fail without cancelling the batch. * * @return bool */ public function allowsFailures() { return Arr::get($this->options, 'allowFailures', false) === true; } /** * Determine if the batch has job failures. * * @return bool */ public function hasFailures() { return $this->failedJobs > 0; } /** * Record that a job within the batch failed to finish successfully, executing any callbacks if necessary. * * @param string $jobId * @param \Throwable $e * @return void */ public function recordFailedJob(string $jobId, $e) { $counts = $this->incrementFailedJobs($jobId); if ($counts->failedJobs === 1 && ! $this->allowsFailures()) { $this->cancel(); } if ($counts->failedJobs === 1 && $this->hasCatchCallbacks()) { $batch = $this->fresh(); collect($this->options['catch'])->each(function ($handler) use ($batch, $e) { $this->invokeHandlerCallback($handler, $batch, $e); }); } if ($counts->allJobsHaveRanExactlyOnce() && $this->hasFinallyCallbacks()) { $batch = $this->fresh(); collect($this->options['finally'])->each(function ($handler) use ($batch, $e) { $this->invokeHandlerCallback($handler, $batch, $e); }); } } /** * Increment the failed jobs for the batch. * * @param string $jobId * @return \Illuminate\Bus\UpdatedBatchJobCounts */ public function incrementFailedJobs(string $jobId) { return $this->repository->incrementFailedJobs($this->id, $jobId); } /** * Determine if the batch has "catch" callbacks. * * @return bool */ public function hasCatchCallbacks() { return isset($this->options['catch']) && ! empty($this->options['catch']); } /** * Determine if the batch has "finally" callbacks. * * @return bool */ public function hasFinallyCallbacks() { return isset($this->options['finally']) && ! empty($this->options['finally']); } /** * Cancel the batch. * * @return void */ public function cancel() { $this->repository->cancel($this->id); } /** * Determine if the batch has been cancelled. * * @return bool */ public function canceled() { return $this->cancelled(); } /** * Determine if the batch has been cancelled. * * @return bool */ public function cancelled() { return ! is_null($this->cancelledAt); } /** * Delete the batch from storage. * * @return void */ public function delete() { $this->repository->delete($this->id); } /** * Invoke a batch callback handler. * * @param callable $handler * @param \Illuminate\Bus\Batch $batch * @param \Throwable|null $e * @return void */ protected function invokeHandlerCallback($handler, Batch $batch, Throwable $e = null) { try { return $handler($batch, $e); } catch (Throwable $e) { if (function_exists('report')) { report($e); } } } /** * Convert the batch to an array. * * @return array */ public function toArray() { return [ 'id' => $this->id, 'name' => $this->name, 'totalJobs' => $this->totalJobs, 'pendingJobs' => $this->pendingJobs, 'processedJobs' => $this->processedJobs(), 'progress' => $this->progress(), 'failedJobs' => $this->failedJobs, 'options' => $this->options, 'createdAt' => $this->createdAt, 'cancelledAt' => $this->cancelledAt, 'finishedAt' => $this->finishedAt, ]; } /** * Get the JSON serializable representation of the object. * * @return array */ public function jsonSerialize(): array { return $this->toArray(); } /** * Dynamically access the batch's "options" via properties. * * @param string $key * @return mixed */ public function __get($key) { return $this->options[$key] ?? null; } } src/Illuminate/Bus/DatabaseBatchRepository.php 0000644 00000025401 15107327040 0015447 0 ustar 00 <?php namespace Illuminate\Bus; use Carbon\CarbonImmutable; use Closure; use DateTimeInterface; use Illuminate\Database\Connection; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\PostgresConnection; use Illuminate\Database\Query\Expression; use Illuminate\Support\Str; class DatabaseBatchRepository implements PrunableBatchRepository { /** * The batch factory instance. * * @var \Illuminate\Bus\BatchFactory */ protected $factory; /** * The database connection instance. * * @var \Illuminate\Database\Connection */ protected $connection; /** * The database table to use to store batch information. * * @var string */ protected $table; /** * Create a new batch repository instance. * * @param \Illuminate\Bus\BatchFactory $factory * @param \Illuminate\Database\Connection $connection * @param string $table */ public function __construct(BatchFactory $factory, Connection $connection, string $table) { $this->factory = $factory; $this->connection = $connection; $this->table = $table; } /** * Retrieve a list of batches. * * @param int $limit * @param mixed $before * @return \Illuminate\Bus\Batch[] */ public function get($limit = 50, $before = null) { return $this->connection->table($this->table) ->orderByDesc('id') ->take($limit) ->when($before, fn ($q) => $q->where('id', '<', $before)) ->get() ->map(function ($batch) { return $this->toBatch($batch); }) ->all(); } /** * Retrieve information about an existing batch. * * @param string $batchId * @return \Illuminate\Bus\Batch|null */ public function find(string $batchId) { $batch = $this->connection->table($this->table) ->useWritePdo() ->where('id', $batchId) ->first(); if ($batch) { return $this->toBatch($batch); } } /** * Store a new pending batch. * * @param \Illuminate\Bus\PendingBatch $batch * @return \Illuminate\Bus\Batch */ public function store(PendingBatch $batch) { $id = (string) Str::orderedUuid(); $this->connection->table($this->table)->insert([ 'id' => $id, 'name' => $batch->name, 'total_jobs' => 0, 'pending_jobs' => 0, 'failed_jobs' => 0, 'failed_job_ids' => '[]', 'options' => $this->serialize($batch->options), 'created_at' => time(), 'cancelled_at' => null, 'finished_at' => null, ]); return $this->find($id); } /** * Increment the total number of jobs within the batch. * * @param string $batchId * @param int $amount * @return void */ public function incrementTotalJobs(string $batchId, int $amount) { $this->connection->table($this->table)->where('id', $batchId)->update([ 'total_jobs' => new Expression('total_jobs + '.$amount), 'pending_jobs' => new Expression('pending_jobs + '.$amount), 'finished_at' => null, ]); } /** * Decrement the total number of pending jobs for the batch. * * @param string $batchId * @param string $jobId * @return \Illuminate\Bus\UpdatedBatchJobCounts */ public function decrementPendingJobs(string $batchId, string $jobId) { $values = $this->updateAtomicValues($batchId, function ($batch) use ($jobId) { return [ 'pending_jobs' => $batch->pending_jobs - 1, 'failed_jobs' => $batch->failed_jobs, 'failed_job_ids' => json_encode(array_values(array_diff((array) json_decode($batch->failed_job_ids, true), [$jobId]))), ]; }); return new UpdatedBatchJobCounts( $values['pending_jobs'], $values['failed_jobs'] ); } /** * Increment the total number of failed jobs for the batch. * * @param string $batchId * @param string $jobId * @return \Illuminate\Bus\UpdatedBatchJobCounts */ public function incrementFailedJobs(string $batchId, string $jobId) { $values = $this->updateAtomicValues($batchId, function ($batch) use ($jobId) { return [ 'pending_jobs' => $batch->pending_jobs, 'failed_jobs' => $batch->failed_jobs + 1, 'failed_job_ids' => json_encode(array_values(array_unique(array_merge((array) json_decode($batch->failed_job_ids, true), [$jobId])))), ]; }); return new UpdatedBatchJobCounts( $values['pending_jobs'], $values['failed_jobs'] ); } /** * Update an atomic value within the batch. * * @param string $batchId * @param \Closure $callback * @return int|null */ protected function updateAtomicValues(string $batchId, Closure $callback) { return $this->connection->transaction(function () use ($batchId, $callback) { $batch = $this->connection->table($this->table)->where('id', $batchId) ->lockForUpdate() ->first(); return is_null($batch) ? [] : tap($callback($batch), function ($values) use ($batchId) { $this->connection->table($this->table)->where('id', $batchId)->update($values); }); }); } /** * Mark the batch that has the given ID as finished. * * @param string $batchId * @return void */ public function markAsFinished(string $batchId) { $this->connection->table($this->table)->where('id', $batchId)->update([ 'finished_at' => time(), ]); } /** * Cancel the batch that has the given ID. * * @param string $batchId * @return void */ public function cancel(string $batchId) { $this->connection->table($this->table)->where('id', $batchId)->update([ 'cancelled_at' => time(), 'finished_at' => time(), ]); } /** * Delete the batch that has the given ID. * * @param string $batchId * @return void */ public function delete(string $batchId) { $this->connection->table($this->table)->where('id', $batchId)->delete(); } /** * Prune all of the entries older than the given date. * * @param \DateTimeInterface $before * @return int */ public function prune(DateTimeInterface $before) { $query = $this->connection->table($this->table) ->whereNotNull('finished_at') ->where('finished_at', '<', $before->getTimestamp()); $totalDeleted = 0; do { $deleted = $query->take(1000)->delete(); $totalDeleted += $deleted; } while ($deleted !== 0); return $totalDeleted; } /** * Prune all of the unfinished entries older than the given date. * * @param \DateTimeInterface $before * @return int */ public function pruneUnfinished(DateTimeInterface $before) { $query = $this->connection->table($this->table) ->whereNull('finished_at') ->where('created_at', '<', $before->getTimestamp()); $totalDeleted = 0; do { $deleted = $query->take(1000)->delete(); $totalDeleted += $deleted; } while ($deleted !== 0); return $totalDeleted; } /** * Prune all of the cancelled entries older than the given date. * * @param \DateTimeInterface $before * @return int */ public function pruneCancelled(DateTimeInterface $before) { $query = $this->connection->table($this->table) ->whereNotNull('cancelled_at') ->where('created_at', '<', $before->getTimestamp()); $totalDeleted = 0; do { $deleted = $query->take(1000)->delete(); $totalDeleted += $deleted; } while ($deleted !== 0); return $totalDeleted; } /** * Execute the given Closure within a storage specific transaction. * * @param \Closure $callback * @return mixed */ public function transaction(Closure $callback) { return $this->connection->transaction(fn () => $callback()); } /** * Serialize the given value. * * @param mixed $value * @return string */ protected function serialize($value) { $serialized = serialize($value); return $this->connection instanceof PostgresConnection ? base64_encode($serialized) : $serialized; } /** * Unserialize the given value. * * @param string $serialized * @return mixed */ protected function unserialize($serialized) { if ($this->connection instanceof PostgresConnection && ! Str::contains($serialized, [':', ';'])) { $serialized = base64_decode($serialized); } try { return unserialize($serialized); } catch (ModelNotFoundException) { return []; } } /** * Convert the given raw batch to a Batch object. * * @param object $batch * @return \Illuminate\Bus\Batch */ protected function toBatch($batch) { return $this->factory->make( $this, $batch->id, $batch->name, (int) $batch->total_jobs, (int) $batch->pending_jobs, (int) $batch->failed_jobs, (array) json_decode($batch->failed_job_ids, true), $this->unserialize($batch->options), CarbonImmutable::createFromTimestamp($batch->created_at), $batch->cancelled_at ? CarbonImmutable::createFromTimestamp($batch->cancelled_at) : $batch->cancelled_at, $batch->finished_at ? CarbonImmutable::createFromTimestamp($batch->finished_at) : $batch->finished_at ); } /** * Get the underlying database connection. * * @return \Illuminate\Database\Connection */ public function getConnection() { return $this->connection; } /** * Set the underlying database connection. * * @param \Illuminate\Database\Connection $connection * @return void */ public function setConnection(Connection $connection) { $this->connection = $connection; } } src/Illuminate/Bus/BatchRepository.php 0000644 00000004201 15107327040 0014015 0 ustar 00 <?php namespace Illuminate\Bus; use Closure; interface BatchRepository { /** * Retrieve a list of batches. * * @param int $limit * @param mixed $before * @return \Illuminate\Bus\Batch[] */ public function get($limit, $before); /** * Retrieve information about an existing batch. * * @param string $batchId * @return \Illuminate\Bus\Batch|null */ public function find(string $batchId); /** * Store a new pending batch. * * @param \Illuminate\Bus\PendingBatch $batch * @return \Illuminate\Bus\Batch */ public function store(PendingBatch $batch); /** * Increment the total number of jobs within the batch. * * @param string $batchId * @param int $amount * @return void */ public function incrementTotalJobs(string $batchId, int $amount); /** * Decrement the total number of pending jobs for the batch. * * @param string $batchId * @param string $jobId * @return \Illuminate\Bus\UpdatedBatchJobCounts */ public function decrementPendingJobs(string $batchId, string $jobId); /** * Increment the total number of failed jobs for the batch. * * @param string $batchId * @param string $jobId * @return \Illuminate\Bus\UpdatedBatchJobCounts */ public function incrementFailedJobs(string $batchId, string $jobId); /** * Mark the batch that has the given ID as finished. * * @param string $batchId * @return void */ public function markAsFinished(string $batchId); /** * Cancel the batch that has the given ID. * * @param string $batchId * @return void */ public function cancel(string $batchId); /** * Delete the batch that has the given ID. * * @param string $batchId * @return void */ public function delete(string $batchId); /** * Execute the given Closure within a storage specific transaction. * * @param \Closure $callback * @return mixed */ public function transaction(Closure $callback); } src/Illuminate/Bus/PendingBatch.php 0000644 00000015616 15107327040 0013236 0 ustar 00 <?php namespace Illuminate\Bus; use Closure; use Illuminate\Bus\Events\BatchDispatched; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher as EventDispatcher; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Laravel\SerializableClosure\SerializableClosure; use Throwable; class PendingBatch { /** * The IoC container instance. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * The batch name. * * @var string */ public $name = ''; /** * The jobs that belong to the batch. * * @var \Illuminate\Support\Collection */ public $jobs; /** * The batch options. * * @var array */ public $options = []; /** * Create a new pending batch instance. * * @param \Illuminate\Contracts\Container\Container $container * @param \Illuminate\Support\Collection $jobs * @return void */ public function __construct(Container $container, Collection $jobs) { $this->container = $container; $this->jobs = $jobs; } /** * Add jobs to the batch. * * @param iterable|object|array $jobs * @return $this */ public function add($jobs) { $jobs = is_iterable($jobs) ? $jobs : Arr::wrap($jobs); foreach ($jobs as $job) { $this->jobs->push($job); } return $this; } /** * Add a callback to be executed after all jobs in the batch have executed successfully. * * @param callable $callback * @return $this */ public function then($callback) { $this->options['then'][] = $callback instanceof Closure ? new SerializableClosure($callback) : $callback; return $this; } /** * Get the "then" callbacks that have been registered with the pending batch. * * @return array */ public function thenCallbacks() { return $this->options['then'] ?? []; } /** * Add a callback to be executed after the first failing job in the batch. * * @param callable $callback * @return $this */ public function catch($callback) { $this->options['catch'][] = $callback instanceof Closure ? new SerializableClosure($callback) : $callback; return $this; } /** * Get the "catch" callbacks that have been registered with the pending batch. * * @return array */ public function catchCallbacks() { return $this->options['catch'] ?? []; } /** * Add a callback to be executed after the batch has finished executing. * * @param callable $callback * @return $this */ public function finally($callback) { $this->options['finally'][] = $callback instanceof Closure ? new SerializableClosure($callback) : $callback; return $this; } /** * Get the "finally" callbacks that have been registered with the pending batch. * * @return array */ public function finallyCallbacks() { return $this->options['finally'] ?? []; } /** * Indicate that the batch should not be cancelled when a job within the batch fails. * * @param bool $allowFailures * @return $this */ public function allowFailures($allowFailures = true) { $this->options['allowFailures'] = $allowFailures; return $this; } /** * Determine if the pending batch allows jobs to fail without cancelling the batch. * * @return bool */ public function allowsFailures() { return Arr::get($this->options, 'allowFailures', false) === true; } /** * Set the name for the batch. * * @param string $name * @return $this */ public function name(string $name) { $this->name = $name; return $this; } /** * Specify the queue connection that the batched jobs should run on. * * @param string $connection * @return $this */ public function onConnection(string $connection) { $this->options['connection'] = $connection; return $this; } /** * Get the connection used by the pending batch. * * @return string|null */ public function connection() { return $this->options['connection'] ?? null; } /** * Specify the queue that the batched jobs should run on. * * @param string $queue * @return $this */ public function onQueue(string $queue) { $this->options['queue'] = $queue; return $this; } /** * Get the queue used by the pending batch. * * @return string|null */ public function queue() { return $this->options['queue'] ?? null; } /** * Add additional data into the batch's options array. * * @param string $key * @param mixed $value * @return $this */ public function withOption(string $key, $value) { $this->options[$key] = $value; return $this; } /** * Dispatch the batch. * * @return \Illuminate\Bus\Batch * * @throws \Throwable */ public function dispatch() { $repository = $this->container->make(BatchRepository::class); try { $batch = $repository->store($this); $batch = $batch->add($this->jobs); } catch (Throwable $e) { if (isset($batch)) { $repository->delete($batch->id); } throw $e; } $this->container->make(EventDispatcher::class)->dispatch( new BatchDispatched($batch) ); return $batch; } /** * Dispatch the batch after the response is sent to the browser. * * @return \Illuminate\Bus\Batch */ public function dispatchAfterResponse() { $repository = $this->container->make(BatchRepository::class); $batch = $repository->store($this); if ($batch) { $this->container->terminating(function () use ($batch) { $this->dispatchExistingBatch($batch); }); } return $batch; } /** * Dispatch an existing batch. * * @param \Illuminate\Bus\Batch $batch * @return void * * @throws \Throwable */ protected function dispatchExistingBatch($batch) { try { $batch = $batch->add($this->jobs); } catch (Throwable $e) { if (isset($batch)) { $batch->delete(); } throw $e; } $this->container->make(EventDispatcher::class)->dispatch( new BatchDispatched($batch) ); } } src/Illuminate/Bus/Dispatcher.php 0000644 00000017216 15107327040 0012774 0 ustar 00 <?php namespace Illuminate\Bus; use Closure; use Illuminate\Contracts\Bus\QueueingDispatcher; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Queue\Queue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\PendingChain; use Illuminate\Pipeline\Pipeline; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\Jobs\SyncJob; use Illuminate\Support\Collection; use RuntimeException; class Dispatcher implements QueueingDispatcher { /** * The container implementation. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * The pipeline instance for the bus. * * @var \Illuminate\Pipeline\Pipeline */ protected $pipeline; /** * The pipes to send commands through before dispatching. * * @var array */ protected $pipes = []; /** * The command to handler mapping for non-self-handling events. * * @var array */ protected $handlers = []; /** * The queue resolver callback. * * @var \Closure|null */ protected $queueResolver; /** * Create a new command dispatcher instance. * * @param \Illuminate\Contracts\Container\Container $container * @param \Closure|null $queueResolver * @return void */ public function __construct(Container $container, Closure $queueResolver = null) { $this->container = $container; $this->queueResolver = $queueResolver; $this->pipeline = new Pipeline($container); } /** * Dispatch a command to its appropriate handler. * * @param mixed $command * @return mixed */ public function dispatch($command) { return $this->queueResolver && $this->commandShouldBeQueued($command) ? $this->dispatchToQueue($command) : $this->dispatchNow($command); } /** * Dispatch a command to its appropriate handler in the current process. * * Queueable jobs will be dispatched to the "sync" queue. * * @param mixed $command * @param mixed $handler * @return mixed */ public function dispatchSync($command, $handler = null) { if ($this->queueResolver && $this->commandShouldBeQueued($command) && method_exists($command, 'onConnection')) { return $this->dispatchToQueue($command->onConnection('sync')); } return $this->dispatchNow($command, $handler); } /** * Dispatch a command to its appropriate handler in the current process without using the synchronous queue. * * @param mixed $command * @param mixed $handler * @return mixed */ public function dispatchNow($command, $handler = null) { $uses = class_uses_recursive($command); if (in_array(InteractsWithQueue::class, $uses) && in_array(Queueable::class, $uses) && ! $command->job) { $command->setJob(new SyncJob($this->container, json_encode([]), 'sync', 'sync')); } if ($handler || $handler = $this->getCommandHandler($command)) { $callback = function ($command) use ($handler) { $method = method_exists($handler, 'handle') ? 'handle' : '__invoke'; return $handler->{$method}($command); }; } else { $callback = function ($command) { $method = method_exists($command, 'handle') ? 'handle' : '__invoke'; return $this->container->call([$command, $method]); }; } return $this->pipeline->send($command)->through($this->pipes)->then($callback); } /** * Attempt to find the batch with the given ID. * * @param string $batchId * @return \Illuminate\Bus\Batch|null */ public function findBatch(string $batchId) { return $this->container->make(BatchRepository::class)->find($batchId); } /** * Create a new batch of queueable jobs. * * @param \Illuminate\Support\Collection|array|mixed $jobs * @return \Illuminate\Bus\PendingBatch */ public function batch($jobs) { return new PendingBatch($this->container, Collection::wrap($jobs)); } /** * Create a new chain of queueable jobs. * * @param \Illuminate\Support\Collection|array $jobs * @return \Illuminate\Foundation\Bus\PendingChain */ public function chain($jobs) { $jobs = Collection::wrap($jobs); return new PendingChain($jobs->shift(), $jobs->toArray()); } /** * Determine if the given command has a handler. * * @param mixed $command * @return bool */ public function hasCommandHandler($command) { return array_key_exists(get_class($command), $this->handlers); } /** * Retrieve the handler for a command. * * @param mixed $command * @return bool|mixed */ public function getCommandHandler($command) { if ($this->hasCommandHandler($command)) { return $this->container->make($this->handlers[get_class($command)]); } return false; } /** * Determine if the given command should be queued. * * @param mixed $command * @return bool */ protected function commandShouldBeQueued($command) { return $command instanceof ShouldQueue; } /** * Dispatch a command to its appropriate handler behind a queue. * * @param mixed $command * @return mixed * * @throws \RuntimeException */ public function dispatchToQueue($command) { $connection = $command->connection ?? null; $queue = call_user_func($this->queueResolver, $connection); if (! $queue instanceof Queue) { throw new RuntimeException('Queue resolver did not return a Queue implementation.'); } if (method_exists($command, 'queue')) { return $command->queue($queue, $command); } return $this->pushCommandToQueue($queue, $command); } /** * Push the command onto the given queue instance. * * @param \Illuminate\Contracts\Queue\Queue $queue * @param mixed $command * @return mixed */ protected function pushCommandToQueue($queue, $command) { if (isset($command->queue, $command->delay)) { return $queue->laterOn($command->queue, $command->delay, $command); } if (isset($command->queue)) { return $queue->pushOn($command->queue, $command); } if (isset($command->delay)) { return $queue->later($command->delay, $command); } return $queue->push($command); } /** * Dispatch a command to its appropriate handler after the current process. * * @param mixed $command * @param mixed $handler * @return void */ public function dispatchAfterResponse($command, $handler = null) { $this->container->terminating(function () use ($command, $handler) { $this->dispatchSync($command, $handler); }); } /** * Set the pipes through which commands should be piped before dispatching. * * @param array $pipes * @return $this */ public function pipeThrough(array $pipes) { $this->pipes = $pipes; return $this; } /** * Map a command to a handler. * * @param array $map * @return $this */ public function map(array $map) { $this->handlers = array_merge($this->handlers, $map); return $this; } } src/Illuminate/Bus/BatchFactory.php 0000644 00000003324 15107327040 0013252 0 ustar 00 <?php namespace Illuminate\Bus; use Carbon\CarbonImmutable; use Illuminate\Contracts\Queue\Factory as QueueFactory; class BatchFactory { /** * The queue factory implementation. * * @var \Illuminate\Contracts\Queue\Factory */ protected $queue; /** * Create a new batch factory instance. * * @param \Illuminate\Contracts\Queue\Factory $queue * @return void */ public function __construct(QueueFactory $queue) { $this->queue = $queue; } /** * Create a new batch instance. * * @param \Illuminate\Bus\BatchRepository $repository * @param string $id * @param string $name * @param int $totalJobs * @param int $pendingJobs * @param int $failedJobs * @param array $failedJobIds * @param array $options * @param \Carbon\CarbonImmutable $createdAt * @param \Carbon\CarbonImmutable|null $cancelledAt * @param \Carbon\CarbonImmutable|null $finishedAt * @return \Illuminate\Bus\Batch */ public function make(BatchRepository $repository, string $id, string $name, int $totalJobs, int $pendingJobs, int $failedJobs, array $failedJobIds, array $options, CarbonImmutable $createdAt, ?CarbonImmutable $cancelledAt, ?CarbonImmutable $finishedAt) { return new Batch($this->queue, $repository, $id, $name, $totalJobs, $pendingJobs, $failedJobs, $failedJobIds, $options, $createdAt, $cancelledAt, $finishedAt); } } src/Illuminate/Bus/UniqueLock.php 0000644 00000003273 15107327040 0012763 0 ustar 00 <?php namespace Illuminate\Bus; use Illuminate\Contracts\Cache\Repository as Cache; class UniqueLock { /** * The cache repository implementation. * * @var \Illuminate\Contracts\Cache\Repository */ protected $cache; /** * Create a new unique lock manager instance. * * @param \Illuminate\Contracts\Cache\Repository $cache * @return void */ public function __construct(Cache $cache) { $this->cache = $cache; } /** * Attempt to acquire a lock for the given job. * * @param mixed $job * @return bool */ public function acquire($job) { $uniqueFor = method_exists($job, 'uniqueFor') ? $job->uniqueFor() : ($job->uniqueFor ?? 0); $cache = method_exists($job, 'uniqueVia') ? $job->uniqueVia() : $this->cache; return (bool) $cache->lock($this->getKey($job), $uniqueFor)->get(); } /** * Release the lock for the given job. * * @param mixed $job * @return void */ public function release($job) { $cache = method_exists($job, 'uniqueVia') ? $job->uniqueVia() : $this->cache; $cache->lock($this->getKey($job))->forceRelease(); } /** * Generate the lock key for the given job. * * @param mixed $job * @return string */ protected function getKey($job) { $uniqueId = method_exists($job, 'uniqueId') ? $job->uniqueId() : ($job->uniqueId ?? ''); return 'laravel_unique_job:'.get_class($job).$uniqueId; } } src/Illuminate/Bus/Batchable.php 0000644 00000005410 15107327040 0012544 0 ustar 00 <?php namespace Illuminate\Bus; use Carbon\CarbonImmutable; use Illuminate\Container\Container; use Illuminate\Support\Str; use Illuminate\Support\Testing\Fakes\BatchFake; trait Batchable { /** * The batch ID (if applicable). * * @var string */ public $batchId; /** * The fake batch, if applicable. * * @var \Illuminate\Support\Testing\Fakes\BatchFake */ private $fakeBatch; /** * Get the batch instance for the job, if applicable. * * @return \Illuminate\Bus\Batch|null */ public function batch() { if ($this->fakeBatch) { return $this->fakeBatch; } if ($this->batchId) { return Container::getInstance()->make(BatchRepository::class)->find($this->batchId); } } /** * Determine if the batch is still active and processing. * * @return bool */ public function batching() { $batch = $this->batch(); return $batch && ! $batch->cancelled(); } /** * Set the batch ID on the job. * * @param string $batchId * @return $this */ public function withBatchId(string $batchId) { $this->batchId = $batchId; return $this; } /** * Indicate that the job should use a fake batch. * * @param string $id * @param string $name * @param int $totalJobs * @param int $pendingJobs * @param int $failedJobs * @param array $failedJobIds * @param array $options * @param \Carbon\CarbonImmutable $createdAt * @param \Carbon\CarbonImmutable|null $cancelledAt * @param \Carbon\CarbonImmutable|null $finishedAt * @return array{0: $this, 1: \Illuminate\Support\Testing\Fakes\BatchFake} */ public function withFakeBatch(string $id = '', string $name = '', int $totalJobs = 0, int $pendingJobs = 0, int $failedJobs = 0, array $failedJobIds = [], array $options = [], CarbonImmutable $createdAt = null, ?CarbonImmutable $cancelledAt = null, ?CarbonImmutable $finishedAt = null) { $this->fakeBatch = new BatchFake( empty($id) ? (string) Str::uuid() : $id, $name, $totalJobs, $pendingJobs, $failedJobs, $failedJobIds, $options, $createdAt ?? CarbonImmutable::now(), $cancelledAt, $finishedAt, ); return [$this, $this->fakeBatch]; } } src/Illuminate/Bus/PrunableBatchRepository.php 0000644 00000000476 15107327040 0015520 0 ustar 00 <?php namespace Illuminate\Bus; use DateTimeInterface; interface PrunableBatchRepository extends BatchRepository { /** * Prune all of the entries older than the given date. * * @param \DateTimeInterface $before * @return int */ public function prune(DateTimeInterface $before); } src/Illuminate/Bus/composer.json 0000644 00000001746 15107327040 0012720 0 ustar 00 { "name": "illuminate/bus", "description": "The Illuminate Bus package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/collections": "^10.0", "illuminate/contracts": "^10.0", "illuminate/pipeline": "^10.0", "illuminate/support": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Bus\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "illuminate/queue": "Required to use closures when chaining jobs (^7.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Bus/Events/BatchDispatched.php 0000644 00000000633 15107327040 0015157 0 ustar 00 <?php namespace Illuminate\Bus\Events; use Illuminate\Bus\Batch; class BatchDispatched { /** * The batch instance. * * @var \Illuminate\Bus\Batch */ public $batch; /** * Create a new event instance. * * @param \Illuminate\Bus\Batch $batch * @return void */ public function __construct(Batch $batch) { $this->batch = $batch; } } src/Illuminate/Bus/Queueable.php 0000644 00000013643 15107327040 0012616 0 ustar 00 <?php namespace Illuminate\Bus; use Closure; use Illuminate\Queue\CallQueuedClosure; use Illuminate\Support\Arr; use RuntimeException; trait Queueable { /** * The name of the connection the job should be sent to. * * @var string|null */ public $connection; /** * The name of the queue the job should be sent to. * * @var string|null */ public $queue; /** * The name of the connection the chain should be sent to. * * @var string|null */ public $chainConnection; /** * The name of the queue the chain should be sent to. * * @var string|null */ public $chainQueue; /** * The callbacks to be executed on chain failure. * * @var array|null */ public $chainCatchCallbacks; /** * The number of seconds before the job should be made available. * * @var \DateTimeInterface|\DateInterval|array|int|null */ public $delay; /** * Indicates whether the job should be dispatched after all database transactions have committed. * * @var bool|null */ public $afterCommit; /** * The middleware the job should be dispatched through. * * @var array */ public $middleware = []; /** * The jobs that should run if this job is successful. * * @var array */ public $chained = []; /** * Set the desired connection for the job. * * @param string|null $connection * @return $this */ public function onConnection($connection) { $this->connection = $connection; return $this; } /** * Set the desired queue for the job. * * @param string|null $queue * @return $this */ public function onQueue($queue) { $this->queue = $queue; return $this; } /** * Set the desired connection for the chain. * * @param string|null $connection * @return $this */ public function allOnConnection($connection) { $this->chainConnection = $connection; $this->connection = $connection; return $this; } /** * Set the desired queue for the chain. * * @param string|null $queue * @return $this */ public function allOnQueue($queue) { $this->chainQueue = $queue; $this->queue = $queue; return $this; } /** * Set the desired delay in seconds for the job. * * @param \DateTimeInterface|\DateInterval|array|int|null $delay * @return $this */ public function delay($delay) { $this->delay = $delay; return $this; } /** * Indicate that the job should be dispatched after all database transactions have committed. * * @return $this */ public function afterCommit() { $this->afterCommit = true; return $this; } /** * Indicate that the job should not wait until database transactions have been committed before dispatching. * * @return $this */ public function beforeCommit() { $this->afterCommit = false; return $this; } /** * Specify the middleware the job should be dispatched through. * * @param array|object $middleware * @return $this */ public function through($middleware) { $this->middleware = Arr::wrap($middleware); return $this; } /** * Set the jobs that should run if this job is successful. * * @param array $chain * @return $this */ public function chain($chain) { $this->chained = collect($chain)->map(function ($job) { return $this->serializeJob($job); })->all(); return $this; } /** * Prepend a job to the current chain so that it is run after the currently running job. * * @param mixed $job * @return $this */ public function prependToChain($job) { $this->chained = Arr::prepend($this->chained, $this->serializeJob($job)); return $this; } /** * Append a job to the end of the current chain. * * @param mixed $job * @return $this */ public function appendToChain($job) { $this->chained = array_merge($this->chained, [$this->serializeJob($job)]); return $this; } /** * Serialize a job for queuing. * * @param mixed $job * @return string * * @throws \RuntimeException */ protected function serializeJob($job) { if ($job instanceof Closure) { if (! class_exists(CallQueuedClosure::class)) { throw new RuntimeException( 'To enable support for closure jobs, please install the illuminate/queue package.' ); } $job = CallQueuedClosure::create($job); } return serialize($job); } /** * Dispatch the next job on the chain. * * @return void */ public function dispatchNextJobInChain() { if (! empty($this->chained)) { dispatch(tap(unserialize(array_shift($this->chained)), function ($next) { $next->chained = $this->chained; $next->onConnection($next->connection ?: $this->chainConnection); $next->onQueue($next->queue ?: $this->chainQueue); $next->chainConnection = $this->chainConnection; $next->chainQueue = $this->chainQueue; $next->chainCatchCallbacks = $this->chainCatchCallbacks; })); } } /** * Invoke all of the chain's failed job callbacks. * * @param \Throwable $e * @return void */ public function invokeChainCatchCallbacks($e) { collect($this->chainCatchCallbacks)->each(function ($callback) use ($e) { $callback($e); }); } } src/Illuminate/Bus/BusServiceProvider.php 0000644 00000003733 15107327040 0014472 0 ustar 00 <?php namespace Illuminate\Bus; use Illuminate\Contracts\Bus\Dispatcher as DispatcherContract; use Illuminate\Contracts\Bus\QueueingDispatcher as QueueingDispatcherContract; use Illuminate\Contracts\Queue\Factory as QueueFactoryContract; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; class BusServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton(Dispatcher::class, function ($app) { return new Dispatcher($app, function ($connection = null) use ($app) { return $app[QueueFactoryContract::class]->connection($connection); }); }); $this->registerBatchServices(); $this->app->alias( Dispatcher::class, DispatcherContract::class ); $this->app->alias( Dispatcher::class, QueueingDispatcherContract::class ); } /** * Register the batch handling services. * * @return void */ protected function registerBatchServices() { $this->app->singleton(BatchRepository::class, DatabaseBatchRepository::class); $this->app->singleton(DatabaseBatchRepository::class, function ($app) { return new DatabaseBatchRepository( $app->make(BatchFactory::class), $app->make('db')->connection($app->config->get('queue.batching.database')), $app->config->get('queue.batching.table', 'job_batches') ); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return [ Dispatcher::class, DispatcherContract::class, QueueingDispatcherContract::class, BatchRepository::class, DatabaseBatchRepository::class, ]; } } src/Illuminate/Bus/LICENSE.md 0000644 00000002063 15107327040 0011573 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Bus/UpdatedBatchJobCounts.php 0000644 00000001530 15107327040 0015055 0 ustar 00 <?php namespace Illuminate\Bus; class UpdatedBatchJobCounts { /** * The number of pending jobs remaining for the batch. * * @var int */ public $pendingJobs; /** * The number of failed jobs that belong to the batch. * * @var int */ public $failedJobs; /** * Create a new batch job counts object. * * @param int $pendingJobs * @param int $failedJobs * @return void */ public function __construct(int $pendingJobs = 0, int $failedJobs = 0) { $this->pendingJobs = $pendingJobs; $this->failedJobs = $failedJobs; } /** * Determine if all jobs have run exactly once. * * @return bool */ public function allJobsHaveRanExactlyOnce() { return ($this->pendingJobs - $this->failedJobs) === 0; } } src/Illuminate/Macroable/composer.json 0000644 00000001351 15107327040 0014044 0 ustar 00 { "name": "illuminate/macroable", "description": "The Illuminate Macroable package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1" }, "autoload": { "psr-4": { "Illuminate\\Support\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Macroable/Traits/Macroable.php 0000644 00000005365 15107327040 0015177 0 ustar 00 <?php namespace Illuminate\Support\Traits; use BadMethodCallException; use Closure; use ReflectionClass; use ReflectionMethod; trait Macroable { /** * The registered string macros. * * @var array */ protected static $macros = []; /** * Register a custom macro. * * @param string $name * @param object|callable $macro * @return void */ public static function macro($name, $macro) { static::$macros[$name] = $macro; } /** * Mix another object into the class. * * @param object $mixin * @param bool $replace * @return void * * @throws \ReflectionException */ public static function mixin($mixin, $replace = true) { $methods = (new ReflectionClass($mixin))->getMethods( ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED ); foreach ($methods as $method) { if ($replace || ! static::hasMacro($method->name)) { static::macro($method->name, $method->invoke($mixin)); } } } /** * Checks if macro is registered. * * @param string $name * @return bool */ public static function hasMacro($name) { return isset(static::$macros[$name]); } /** * Flush the existing macros. * * @return void */ public static function flushMacros() { static::$macros = []; } /** * Dynamically handle calls to the class. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public static function __callStatic($method, $parameters) { if (! static::hasMacro($method)) { throw new BadMethodCallException(sprintf( 'Method %s::%s does not exist.', static::class, $method )); } $macro = static::$macros[$method]; if ($macro instanceof Closure) { $macro = $macro->bindTo(null, static::class); } return $macro(...$parameters); } /** * Dynamically handle calls to the class. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public function __call($method, $parameters) { if (! static::hasMacro($method)) { throw new BadMethodCallException(sprintf( 'Method %s::%s does not exist.', static::class, $method )); } $macro = static::$macros[$method]; if ($macro instanceof Closure) { $macro = $macro->bindTo($this, static::class); } return $macro(...$parameters); } } src/Illuminate/Macroable/LICENSE.md 0000644 00000002063 15107327040 0012727 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Encryption/EncryptionServiceProvider.php 0000644 00000003654 15107327040 0017476 0 ustar 00 <?php namespace Illuminate\Encryption; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; use Laravel\SerializableClosure\SerializableClosure; class EncryptionServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { $this->registerEncrypter(); $this->registerSerializableClosureSecurityKey(); } /** * Register the encrypter. * * @return void */ protected function registerEncrypter() { $this->app->singleton('encrypter', function ($app) { $config = $app->make('config')->get('app'); return new Encrypter($this->parseKey($config), $config['cipher']); }); } /** * Configure Serializable Closure signing for security. * * @return void */ protected function registerSerializableClosureSecurityKey() { $config = $this->app->make('config')->get('app'); if (! class_exists(SerializableClosure::class) || empty($config['key'])) { return; } SerializableClosure::setSecretKey($this->parseKey($config)); } /** * Parse the encryption key. * * @param array $config * @return string */ protected function parseKey(array $config) { if (Str::startsWith($key = $this->key($config), $prefix = 'base64:')) { $key = base64_decode(Str::after($key, $prefix)); } return $key; } /** * Extract the encryption key from the given configuration. * * @param array $config * @return string * * @throws \Illuminate\Encryption\MissingAppKeyException */ protected function key(array $config) { return tap($config['key'], function ($key) { if (empty($key)) { throw new MissingAppKeyException; } }); } } src/Illuminate/Encryption/MissingAppKeyException.php 0000644 00000000600 15107327040 0016676 0 ustar 00 <?php namespace Illuminate\Encryption; use RuntimeException; class MissingAppKeyException extends RuntimeException { /** * Create a new exception instance. * * @param string $message * @return void */ public function __construct($message = 'No application encryption key has been specified.') { parent::__construct($message); } } src/Illuminate/Encryption/composer.json 0000644 00000001620 15107327040 0014310 0 ustar 00 { "name": "illuminate/encryption", "description": "The Illuminate Encryption package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "ext-hash": "*", "ext-mbstring": "*", "ext-openssl": "*", "illuminate/contracts": "^10.0", "illuminate/support": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Encryption\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Encryption/Encrypter.php 0000644 00000017577 15107327040 0014274 0 ustar 00 <?php namespace Illuminate\Encryption; use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract; use Illuminate\Contracts\Encryption\EncryptException; use Illuminate\Contracts\Encryption\StringEncrypter; use RuntimeException; class Encrypter implements EncrypterContract, StringEncrypter { /** * The encryption key. * * @var string */ protected $key; /** * The algorithm used for encryption. * * @var string */ protected $cipher; /** * The supported cipher algorithms and their properties. * * @var array */ private static $supportedCiphers = [ 'aes-128-cbc' => ['size' => 16, 'aead' => false], 'aes-256-cbc' => ['size' => 32, 'aead' => false], 'aes-128-gcm' => ['size' => 16, 'aead' => true], 'aes-256-gcm' => ['size' => 32, 'aead' => true], ]; /** * Create a new encrypter instance. * * @param string $key * @param string $cipher * @return void * * @throws \RuntimeException */ public function __construct($key, $cipher = 'aes-128-cbc') { $key = (string) $key; if (! static::supported($key, $cipher)) { $ciphers = implode(', ', array_keys(self::$supportedCiphers)); throw new RuntimeException("Unsupported cipher or incorrect key length. Supported ciphers are: {$ciphers}."); } $this->key = $key; $this->cipher = $cipher; } /** * Determine if the given key and cipher combination is valid. * * @param string $key * @param string $cipher * @return bool */ public static function supported($key, $cipher) { if (! isset(self::$supportedCiphers[strtolower($cipher)])) { return false; } return mb_strlen($key, '8bit') === self::$supportedCiphers[strtolower($cipher)]['size']; } /** * Create a new encryption key for the given cipher. * * @param string $cipher * @return string */ public static function generateKey($cipher) { return random_bytes(self::$supportedCiphers[strtolower($cipher)]['size'] ?? 32); } /** * Encrypt the given value. * * @param mixed $value * @param bool $serialize * @return string * * @throws \Illuminate\Contracts\Encryption\EncryptException */ public function encrypt($value, $serialize = true) { $iv = random_bytes(openssl_cipher_iv_length(strtolower($this->cipher))); $value = \openssl_encrypt( $serialize ? serialize($value) : $value, strtolower($this->cipher), $this->key, 0, $iv, $tag ); if ($value === false) { throw new EncryptException('Could not encrypt the data.'); } $iv = base64_encode($iv); $tag = base64_encode($tag ?? ''); $mac = self::$supportedCiphers[strtolower($this->cipher)]['aead'] ? '' // For AEAD-algorithms, the tag / MAC is returned by openssl_encrypt... : $this->hash($iv, $value); $json = json_encode(compact('iv', 'value', 'mac', 'tag'), JSON_UNESCAPED_SLASHES); if (json_last_error() !== JSON_ERROR_NONE) { throw new EncryptException('Could not encrypt the data.'); } return base64_encode($json); } /** * Encrypt a string without serialization. * * @param string $value * @return string * * @throws \Illuminate\Contracts\Encryption\EncryptException */ public function encryptString($value) { return $this->encrypt($value, false); } /** * Decrypt the given value. * * @param string $payload * @param bool $unserialize * @return mixed * * @throws \Illuminate\Contracts\Encryption\DecryptException */ public function decrypt($payload, $unserialize = true) { $payload = $this->getJsonPayload($payload); $iv = base64_decode($payload['iv']); $this->ensureTagIsValid( $tag = empty($payload['tag']) ? null : base64_decode($payload['tag']) ); // Here we will decrypt the value. If we are able to successfully decrypt it // we will then unserialize it and return it out to the caller. If we are // unable to decrypt this value we will throw out an exception message. $decrypted = \openssl_decrypt( $payload['value'], strtolower($this->cipher), $this->key, 0, $iv, $tag ?? '' ); if ($decrypted === false) { throw new DecryptException('Could not decrypt the data.'); } return $unserialize ? unserialize($decrypted) : $decrypted; } /** * Decrypt the given string without unserialization. * * @param string $payload * @return string * * @throws \Illuminate\Contracts\Encryption\DecryptException */ public function decryptString($payload) { return $this->decrypt($payload, false); } /** * Create a MAC for the given value. * * @param string $iv * @param mixed $value * @return string */ protected function hash($iv, $value) { return hash_hmac('sha256', $iv.$value, $this->key); } /** * Get the JSON array from the given payload. * * @param string $payload * @return array * * @throws \Illuminate\Contracts\Encryption\DecryptException */ protected function getJsonPayload($payload) { $payload = json_decode(base64_decode($payload), true); // If the payload is not valid JSON or does not have the proper keys set we will // assume it is invalid and bail out of the routine since we will not be able // to decrypt the given value. We'll also check the MAC for this encryption. if (! $this->validPayload($payload)) { throw new DecryptException('The payload is invalid.'); } if (! self::$supportedCiphers[strtolower($this->cipher)]['aead'] && ! $this->validMac($payload)) { throw new DecryptException('The MAC is invalid.'); } return $payload; } /** * Verify that the encryption payload is valid. * * @param mixed $payload * @return bool */ protected function validPayload($payload) { if (! is_array($payload)) { return false; } foreach (['iv', 'value', 'mac'] as $item) { if (! isset($payload[$item]) || ! is_string($payload[$item])) { return false; } } if (isset($payload['tag']) && ! is_string($payload['tag'])) { return false; } return strlen(base64_decode($payload['iv'], true)) === openssl_cipher_iv_length(strtolower($this->cipher)); } /** * Determine if the MAC for the given payload is valid. * * @param array $payload * @return bool */ protected function validMac(array $payload) { return hash_equals( $this->hash($payload['iv'], $payload['value']), $payload['mac'] ); } /** * Ensure the given tag is a valid tag given the selected cipher. * * @param string $tag * @return void */ protected function ensureTagIsValid($tag) { if (self::$supportedCiphers[strtolower($this->cipher)]['aead'] && strlen($tag) !== 16) { throw new DecryptException('Could not decrypt the data.'); } if (! self::$supportedCiphers[strtolower($this->cipher)]['aead'] && is_string($tag)) { throw new DecryptException('Unable to use tag because the cipher algorithm does not support AEAD.'); } } /** * Get the encryption key that the encrypter is currently using. * * @return string */ public function getKey() { return $this->key; } } src/Illuminate/Encryption/LICENSE.md 0000644 00000002063 15107327040 0013174 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/View/ViewFinderInterface.php 0000644 00000002653 15107327040 0014751 0 ustar 00 <?php namespace Illuminate\View; interface ViewFinderInterface { /** * Hint path delimiter value. * * @var string */ const HINT_PATH_DELIMITER = '::'; /** * Get the fully qualified location of the view. * * @param string $view * @return string */ public function find($view); /** * Add a location to the finder. * * @param string $location * @return void */ public function addLocation($location); /** * Add a namespace hint to the finder. * * @param string $namespace * @param string|array $hints * @return void */ public function addNamespace($namespace, $hints); /** * Prepend a namespace hint to the finder. * * @param string $namespace * @param string|array $hints * @return void */ public function prependNamespace($namespace, $hints); /** * Replace the namespace hints for the given namespace. * * @param string $namespace * @param string|array $hints * @return void */ public function replaceNamespace($namespace, $hints); /** * Add a valid view extension to the finder. * * @param string $extension * @return void */ public function addExtension($extension); /** * Flush the cache of located views. * * @return void */ public function flush(); } src/Illuminate/View/FileViewFinder.php 0000644 00000016135 15107327040 0013730 0 ustar 00 <?php namespace Illuminate\View; use Illuminate\Filesystem\Filesystem; use InvalidArgumentException; class FileViewFinder implements ViewFinderInterface { /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * The array of active view paths. * * @var array */ protected $paths; /** * The array of views that have been located. * * @var array */ protected $views = []; /** * The namespace to file path hints. * * @var array */ protected $hints = []; /** * Register a view extension with the finder. * * @var string[] */ protected $extensions = ['blade.php', 'php', 'css', 'html']; /** * Create a new file view loader instance. * * @param \Illuminate\Filesystem\Filesystem $files * @param array $paths * @param array|null $extensions * @return void */ public function __construct(Filesystem $files, array $paths, array $extensions = null) { $this->files = $files; $this->paths = array_map([$this, 'resolvePath'], $paths); if (isset($extensions)) { $this->extensions = $extensions; } } /** * Get the fully qualified location of the view. * * @param string $name * @return string */ public function find($name) { if (isset($this->views[$name])) { return $this->views[$name]; } if ($this->hasHintInformation($name = trim($name))) { return $this->views[$name] = $this->findNamespacedView($name); } return $this->views[$name] = $this->findInPaths($name, $this->paths); } /** * Get the path to a template with a named path. * * @param string $name * @return string */ protected function findNamespacedView($name) { [$namespace, $view] = $this->parseNamespaceSegments($name); return $this->findInPaths($view, $this->hints[$namespace]); } /** * Get the segments of a template with a named path. * * @param string $name * @return array * * @throws \InvalidArgumentException */ protected function parseNamespaceSegments($name) { $segments = explode(static::HINT_PATH_DELIMITER, $name); if (count($segments) !== 2) { throw new InvalidArgumentException("View [{$name}] has an invalid name."); } if (! isset($this->hints[$segments[0]])) { throw new InvalidArgumentException("No hint path defined for [{$segments[0]}]."); } return $segments; } /** * Find the given view in the list of paths. * * @param string $name * @param array $paths * @return string * * @throws \InvalidArgumentException */ protected function findInPaths($name, $paths) { foreach ((array) $paths as $path) { foreach ($this->getPossibleViewFiles($name) as $file) { if ($this->files->exists($viewPath = $path.'/'.$file)) { return $viewPath; } } } throw new InvalidArgumentException("View [{$name}] not found."); } /** * Get an array of possible view files. * * @param string $name * @return array */ protected function getPossibleViewFiles($name) { return array_map(fn ($extension) => str_replace('.', '/', $name).'.'.$extension, $this->extensions); } /** * Add a location to the finder. * * @param string $location * @return void */ public function addLocation($location) { $this->paths[] = $this->resolvePath($location); } /** * Prepend a location to the finder. * * @param string $location * @return void */ public function prependLocation($location) { array_unshift($this->paths, $this->resolvePath($location)); } /** * Resolve the path. * * @param string $path * @return string */ protected function resolvePath($path) { return realpath($path) ?: $path; } /** * Add a namespace hint to the finder. * * @param string $namespace * @param string|array $hints * @return void */ public function addNamespace($namespace, $hints) { $hints = (array) $hints; if (isset($this->hints[$namespace])) { $hints = array_merge($this->hints[$namespace], $hints); } $this->hints[$namespace] = $hints; } /** * Prepend a namespace hint to the finder. * * @param string $namespace * @param string|array $hints * @return void */ public function prependNamespace($namespace, $hints) { $hints = (array) $hints; if (isset($this->hints[$namespace])) { $hints = array_merge($hints, $this->hints[$namespace]); } $this->hints[$namespace] = $hints; } /** * Replace the namespace hints for the given namespace. * * @param string $namespace * @param string|array $hints * @return void */ public function replaceNamespace($namespace, $hints) { $this->hints[$namespace] = (array) $hints; } /** * Register an extension with the view finder. * * @param string $extension * @return void */ public function addExtension($extension) { if (($index = array_search($extension, $this->extensions)) !== false) { unset($this->extensions[$index]); } array_unshift($this->extensions, $extension); } /** * Returns whether or not the view name has any hint information. * * @param string $name * @return bool */ public function hasHintInformation($name) { return strpos($name, static::HINT_PATH_DELIMITER) > 0; } /** * Flush the cache of located views. * * @return void */ public function flush() { $this->views = []; } /** * Get the filesystem instance. * * @return \Illuminate\Filesystem\Filesystem */ public function getFilesystem() { return $this->files; } /** * Set the active view paths. * * @param array $paths * @return $this */ public function setPaths($paths) { $this->paths = $paths; return $this; } /** * Get the active view paths. * * @return array */ public function getPaths() { return $this->paths; } /** * Get the views that have been located. * * @return array */ public function getViews() { return $this->views; } /** * Get the namespace to file path hints. * * @return array */ public function getHints() { return $this->hints; } /** * Get registered extensions. * * @return array */ public function getExtensions() { return $this->extensions; } } src/Illuminate/View/ViewException.php 0000644 00000001607 15107327040 0013655 0 ustar 00 <?php namespace Illuminate\View; use ErrorException; use Illuminate\Container\Container; use Illuminate\Support\Reflector; class ViewException extends ErrorException { /** * Report the exception. * * @return bool|null */ public function report() { $exception = $this->getPrevious(); if (Reflector::isCallable($reportCallable = [$exception, 'report'])) { return Container::getInstance()->call($reportCallable); } return false; } /** * Render the exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response|null */ public function render($request) { $exception = $this->getPrevious(); if ($exception && method_exists($exception, 'render')) { return $exception->render($request); } } } src/Illuminate/View/Engines/EngineResolver.php 0000644 00000002702 15107327040 0015400 0 ustar 00 <?php namespace Illuminate\View\Engines; use Closure; use InvalidArgumentException; class EngineResolver { /** * The array of engine resolvers. * * @var array */ protected $resolvers = []; /** * The resolved engine instances. * * @var array */ protected $resolved = []; /** * Register a new engine resolver. * * The engine string typically corresponds to a file extension. * * @param string $engine * @param \Closure $resolver * @return void */ public function register($engine, Closure $resolver) { $this->forget($engine); $this->resolvers[$engine] = $resolver; } /** * Resolve an engine instance by name. * * @param string $engine * @return \Illuminate\Contracts\View\Engine * * @throws \InvalidArgumentException */ public function resolve($engine) { if (isset($this->resolved[$engine])) { return $this->resolved[$engine]; } if (isset($this->resolvers[$engine])) { return $this->resolved[$engine] = call_user_func($this->resolvers[$engine]); } throw new InvalidArgumentException("Engine [{$engine}] not found."); } /** * Remove a resolved engine. * * @param string $engine * @return void */ public function forget($engine) { unset($this->resolved[$engine]); } } src/Illuminate/View/Engines/PhpEngine.php 0000644 00000003531 15107327040 0014327 0 ustar 00 <?php namespace Illuminate\View\Engines; use Illuminate\Contracts\View\Engine; use Illuminate\Filesystem\Filesystem; use Throwable; class PhpEngine implements Engine { /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * Create a new file engine instance. * * @param \Illuminate\Filesystem\Filesystem $files * @return void */ public function __construct(Filesystem $files) { $this->files = $files; } /** * Get the evaluated contents of the view. * * @param string $path * @param array $data * @return string */ public function get($path, array $data = []) { return $this->evaluatePath($path, $data); } /** * Get the evaluated contents of the view at the given path. * * @param string $path * @param array $data * @return string */ protected function evaluatePath($path, $data) { $obLevel = ob_get_level(); ob_start(); // We'll evaluate the contents of the view inside a try/catch block so we can // flush out any stray output that might get out before an error occurs or // an exception is thrown. This prevents any partial views from leaking. try { $this->files->getRequire($path, $data); } catch (Throwable $e) { $this->handleViewException($e, $obLevel); } return ltrim(ob_get_clean()); } /** * Handle a view exception. * * @param \Throwable $e * @param int $obLevel * @return void * * @throws \Throwable */ protected function handleViewException(Throwable $e, $obLevel) { while (ob_get_level() > $obLevel) { ob_end_clean(); } throw $e; } } src/Illuminate/View/Engines/Engine.php 0000644 00000000552 15107327040 0013657 0 ustar 00 <?php namespace Illuminate\View\Engines; abstract class Engine { /** * The view that was last to be rendered. * * @var string */ protected $lastRendered; /** * Get the last view that was rendered. * * @return string */ public function getLastRendered() { return $this->lastRendered; } } src/Illuminate/View/Engines/CompilerEngine.php 0000644 00000007640 15107327040 0015357 0 ustar 00 <?php namespace Illuminate\View\Engines; use Illuminate\Filesystem\Filesystem; use Illuminate\Http\Exceptions\HttpResponseException; use Illuminate\View\Compilers\CompilerInterface; use Illuminate\View\ViewException; use Symfony\Component\HttpKernel\Exception\HttpException; use Throwable; class CompilerEngine extends PhpEngine { /** * The Blade compiler instance. * * @var \Illuminate\View\Compilers\CompilerInterface */ protected $compiler; /** * A stack of the last compiled templates. * * @var array */ protected $lastCompiled = []; /** * The view paths that were compiled or are not expired, keyed by the path. * * @var array<string, true> */ protected $compiledOrNotExpired = []; /** * Create a new compiler engine instance. * * @param \Illuminate\View\Compilers\CompilerInterface $compiler * @param \Illuminate\Filesystem\Filesystem|null $files * @return void */ public function __construct(CompilerInterface $compiler, Filesystem $files = null) { parent::__construct($files ?: new Filesystem); $this->compiler = $compiler; } /** * Get the evaluated contents of the view. * * @param string $path * @param array $data * @return string */ public function get($path, array $data = []) { $this->lastCompiled[] = $path; // If this given view has expired, which means it has simply been edited since // it was last compiled, we will re-compile the views so we can evaluate a // fresh copy of the view. We'll pass the compiler the path of the view. if (! isset($this->compiledOrNotExpired[$path]) && $this->compiler->isExpired($path)) { $this->compiler->compile($path); } // Once we have the path to the compiled file, we will evaluate the paths with // typical PHP just like any other templates. We also keep a stack of views // which have been rendered for right exception messages to be generated. try { $results = $this->evaluatePath($this->compiler->getCompiledPath($path), $data); } catch (ViewException $e) { if (! str($e->getMessage())->contains(['No such file or directory', 'File does not exist at path'])) { throw $e; } if (! isset($this->compiledOrNotExpired[$path])) { throw $e; } $this->compiler->compile($path); $results = $this->evaluatePath($this->compiler->getCompiledPath($path), $data); } $this->compiledOrNotExpired[$path] = true; array_pop($this->lastCompiled); return $results; } /** * Handle a view exception. * * @param \Throwable $e * @param int $obLevel * @return void * * @throws \Throwable */ protected function handleViewException(Throwable $e, $obLevel) { if ($e instanceof HttpException || $e instanceof HttpResponseException) { parent::handleViewException($e, $obLevel); } $e = new ViewException($this->getMessage($e), 0, 1, $e->getFile(), $e->getLine(), $e); parent::handleViewException($e, $obLevel); } /** * Get the exception message for an exception. * * @param \Throwable $e * @return string */ protected function getMessage(Throwable $e) { return $e->getMessage().' (View: '.realpath(last($this->lastCompiled)).')'; } /** * Get the compiler implementation. * * @return \Illuminate\View\Compilers\CompilerInterface */ public function getCompiler() { return $this->compiler; } /** * Clear the cache of views that were compiled or not expired. * * @return void */ public function forgetCompiledOrNotExpired() { $this->compiledOrNotExpired = []; } } src/Illuminate/View/Engines/FileEngine.php 0000644 00000001403 15107327040 0014453 0 ustar 00 <?php namespace Illuminate\View\Engines; use Illuminate\Contracts\View\Engine; use Illuminate\Filesystem\Filesystem; class FileEngine implements Engine { /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * Create a new file engine instance. * * @param \Illuminate\Filesystem\Filesystem $files * @return void */ public function __construct(Filesystem $files) { $this->files = $files; } /** * Get the evaluated contents of the view. * * @param string $path * @param array $data * @return string */ public function get($path, array $data = []) { return $this->files->get($path); } } src/Illuminate/View/AnonymousComponent.php 0000644 00000002215 15107327040 0014733 0 ustar 00 <?php namespace Illuminate\View; class AnonymousComponent extends Component { /** * The component view. * * @var string */ protected $view; /** * The component data. * * @var array */ protected $data = []; /** * Create a new anonymous component instance. * * @param string $view * @param array $data * @return void */ public function __construct($view, $data) { $this->view = $view; $this->data = $data; } /** * Get the view / view contents that represent the component. * * @return string */ public function render() { return $this->view; } /** * Get the data that should be supplied to the view. * * @return array */ public function data() { $this->attributes = $this->attributes ?: $this->newAttributeBag(); return array_merge( ($this->data['attributes'] ?? null)?->getAttributes() ?: [], $this->attributes->getAttributes(), $this->data, ['attributes' => $this->attributes] ); } } src/Illuminate/View/Concerns/ManagesFragments.php 0000644 00000003320 15107327040 0016052 0 ustar 00 <?php namespace Illuminate\View\Concerns; use InvalidArgumentException; trait ManagesFragments { /** * All of the captured, rendered fragments. * * @var array */ protected $fragments = []; /** * The stack of in-progress fragment renders. * * @var array */ protected $fragmentStack = []; /** * Start injecting content into a fragment. * * @param string $fragment * @return void */ public function startFragment($fragment) { if (ob_start()) { $this->fragmentStack[] = $fragment; } } /** * Stop injecting content into a fragment. * * @return string * * @throws \InvalidArgumentException */ public function stopFragment() { if (empty($this->fragmentStack)) { throw new InvalidArgumentException('Cannot end a fragment without first starting one.'); } $last = array_pop($this->fragmentStack); $this->fragments[$last] = ob_get_clean(); return $this->fragments[$last]; } /** * Get the contents of a fragment. * * @param string $name * @param string|null $default * @return mixed */ public function getFragment($name, $default = null) { return $this->getFragments()[$name] ?? $default; } /** * Get the entire array of rendered fragments. * * @return array */ public function getFragments() { return $this->fragments; } /** * Flush all of the fragments. * * @return void */ public function flushFragments() { $this->fragments = []; $this->fragmentStack = []; } } src/Illuminate/View/Concerns/ManagesEvents.php 0000644 00000011542 15107327040 0015375 0 ustar 00 <?php namespace Illuminate\View\Concerns; use Closure; use Illuminate\Contracts\View\View as ViewContract; use Illuminate\Support\Str; trait ManagesEvents { /** * Register a view creator event. * * @param array|string $views * @param \Closure|string $callback * @return array */ public function creator($views, $callback) { $creators = []; foreach ((array) $views as $view) { $creators[] = $this->addViewEvent($view, $callback, 'creating: '); } return $creators; } /** * Register multiple view composers via an array. * * @param array $composers * @return array */ public function composers(array $composers) { $registered = []; foreach ($composers as $callback => $views) { $registered = array_merge($registered, $this->composer($views, $callback)); } return $registered; } /** * Register a view composer event. * * @param array|string $views * @param \Closure|string $callback * @return array */ public function composer($views, $callback) { $composers = []; foreach ((array) $views as $view) { $composers[] = $this->addViewEvent($view, $callback); } return $composers; } /** * Add an event for a given view. * * @param string $view * @param \Closure|string $callback * @param string $prefix * @return \Closure|null */ protected function addViewEvent($view, $callback, $prefix = 'composing: ') { $view = $this->normalizeName($view); if ($callback instanceof Closure) { $this->addEventListener($prefix.$view, $callback); return $callback; } elseif (is_string($callback)) { return $this->addClassEvent($view, $callback, $prefix); } } /** * Register a class based view composer. * * @param string $view * @param string $class * @param string $prefix * @return \Closure */ protected function addClassEvent($view, $class, $prefix) { $name = $prefix.$view; // When registering a class based view "composer", we will simply resolve the // classes from the application IoC container then call the compose method // on the instance. This allows for convenient, testable view composers. $callback = $this->buildClassEventCallback( $class, $prefix ); $this->addEventListener($name, $callback); return $callback; } /** * Build a class based container callback Closure. * * @param string $class * @param string $prefix * @return \Closure */ protected function buildClassEventCallback($class, $prefix) { [$class, $method] = $this->parseClassEvent($class, $prefix); // Once we have the class and method name, we can build the Closure to resolve // the instance out of the IoC container and call the method on it with the // given arguments that are passed to the Closure as the composer's data. return function () use ($class, $method) { return $this->container->make($class)->{$method}(...func_get_args()); }; } /** * Parse a class based composer name. * * @param string $class * @param string $prefix * @return array */ protected function parseClassEvent($class, $prefix) { return Str::parseCallback($class, $this->classEventMethodForPrefix($prefix)); } /** * Determine the class event method based on the given prefix. * * @param string $prefix * @return string */ protected function classEventMethodForPrefix($prefix) { return str_contains($prefix, 'composing') ? 'compose' : 'create'; } /** * Add a listener to the event dispatcher. * * @param string $name * @param \Closure $callback * @return void */ protected function addEventListener($name, $callback) { if (str_contains($name, '*')) { $callback = function ($name, array $data) use ($callback) { return $callback($data[0]); }; } $this->events->listen($name, $callback); } /** * Call the composer for a given view. * * @param \Illuminate\Contracts\View\View $view * @return void */ public function callComposer(ViewContract $view) { $this->events->dispatch('composing: '.$view->name(), [$view]); } /** * Call the creator for a given view. * * @param \Illuminate\Contracts\View\View $view * @return void */ public function callCreator(ViewContract $view) { $this->events->dispatch('creating: '.$view->name(), [$view]); } } src/Illuminate/View/Concerns/ManagesStacks.php 0000644 00000010333 15107327040 0015356 0 ustar 00 <?php namespace Illuminate\View\Concerns; use InvalidArgumentException; trait ManagesStacks { /** * All of the finished, captured push sections. * * @var array */ protected $pushes = []; /** * All of the finished, captured prepend sections. * * @var array */ protected $prepends = []; /** * The stack of in-progress push sections. * * @var array */ protected $pushStack = []; /** * Start injecting content into a push section. * * @param string $section * @param string $content * @return void */ public function startPush($section, $content = '') { if ($content === '') { if (ob_start()) { $this->pushStack[] = $section; } } else { $this->extendPush($section, $content); } } /** * Stop injecting content into a push section. * * @return string * * @throws \InvalidArgumentException */ public function stopPush() { if (empty($this->pushStack)) { throw new InvalidArgumentException('Cannot end a push stack without first starting one.'); } return tap(array_pop($this->pushStack), function ($last) { $this->extendPush($last, ob_get_clean()); }); } /** * Append content to a given push section. * * @param string $section * @param string $content * @return void */ protected function extendPush($section, $content) { if (! isset($this->pushes[$section])) { $this->pushes[$section] = []; } if (! isset($this->pushes[$section][$this->renderCount])) { $this->pushes[$section][$this->renderCount] = $content; } else { $this->pushes[$section][$this->renderCount] .= $content; } } /** * Start prepending content into a push section. * * @param string $section * @param string $content * @return void */ public function startPrepend($section, $content = '') { if ($content === '') { if (ob_start()) { $this->pushStack[] = $section; } } else { $this->extendPrepend($section, $content); } } /** * Stop prepending content into a push section. * * @return string * * @throws \InvalidArgumentException */ public function stopPrepend() { if (empty($this->pushStack)) { throw new InvalidArgumentException('Cannot end a prepend operation without first starting one.'); } return tap(array_pop($this->pushStack), function ($last) { $this->extendPrepend($last, ob_get_clean()); }); } /** * Prepend content to a given stack. * * @param string $section * @param string $content * @return void */ protected function extendPrepend($section, $content) { if (! isset($this->prepends[$section])) { $this->prepends[$section] = []; } if (! isset($this->prepends[$section][$this->renderCount])) { $this->prepends[$section][$this->renderCount] = $content; } else { $this->prepends[$section][$this->renderCount] = $content.$this->prepends[$section][$this->renderCount]; } } /** * Get the string contents of a push section. * * @param string $section * @param string $default * @return string */ public function yieldPushContent($section, $default = '') { if (! isset($this->pushes[$section]) && ! isset($this->prepends[$section])) { return $default; } $output = ''; if (isset($this->prepends[$section])) { $output .= implode(array_reverse($this->prepends[$section])); } if (isset($this->pushes[$section])) { $output .= implode($this->pushes[$section]); } return $output; } /** * Flush all of the stacks. * * @return void */ public function flushStacks() { $this->pushes = []; $this->prepends = []; $this->pushStack = []; } } src/Illuminate/View/Concerns/ManagesLoops.php 0000644 00000004433 15107327040 0015226 0 ustar 00 <?php namespace Illuminate\View\Concerns; use Illuminate\Support\Arr; use Illuminate\Support\LazyCollection; trait ManagesLoops { /** * The stack of in-progress loops. * * @var array */ protected $loopsStack = []; /** * Add new loop to the stack. * * @param \Countable|array $data * @return void */ public function addLoop($data) { $length = is_countable($data) && ! $data instanceof LazyCollection ? count($data) : null; $parent = Arr::last($this->loopsStack); $this->loopsStack[] = [ 'iteration' => 0, 'index' => 0, 'remaining' => $length ?? null, 'count' => $length, 'first' => true, 'last' => isset($length) ? $length == 1 : null, 'odd' => false, 'even' => true, 'depth' => count($this->loopsStack) + 1, 'parent' => $parent ? (object) $parent : null, ]; } /** * Increment the top loop's indices. * * @return void */ public function incrementLoopIndices() { $loop = $this->loopsStack[$index = count($this->loopsStack) - 1]; $this->loopsStack[$index] = array_merge($this->loopsStack[$index], [ 'iteration' => $loop['iteration'] + 1, 'index' => $loop['iteration'], 'first' => $loop['iteration'] == 0, 'odd' => ! $loop['odd'], 'even' => ! $loop['even'], 'remaining' => isset($loop['count']) ? $loop['remaining'] - 1 : null, 'last' => isset($loop['count']) ? $loop['iteration'] == $loop['count'] - 1 : null, ]); } /** * Pop a loop from the top of the loop stack. * * @return void */ public function popLoop() { array_pop($this->loopsStack); } /** * Get an instance of the last loop in the stack. * * @return \stdClass|null */ public function getLastLoop() { if ($last = Arr::last($this->loopsStack)) { return (object) $last; } } /** * Get the entire loop stack. * * @return array */ public function getLoopStack() { return $this->loopsStack; } } src/Illuminate/View/Concerns/ManagesLayouts.php 0000644 00000013435 15107327040 0015574 0 ustar 00 <?php namespace Illuminate\View\Concerns; use Illuminate\Contracts\View\View; use Illuminate\Support\Str; use InvalidArgumentException; trait ManagesLayouts { /** * All of the finished, captured sections. * * @var array */ protected $sections = []; /** * The stack of in-progress sections. * * @var array */ protected $sectionStack = []; /** * The parent placeholder for the request. * * @var mixed */ protected static $parentPlaceholder = []; /** * The parent placeholder salt for the request. * * @var string */ protected static $parentPlaceholderSalt; /** * Start injecting content into a section. * * @param string $section * @param string|null $content * @return void */ public function startSection($section, $content = null) { if ($content === null) { if (ob_start()) { $this->sectionStack[] = $section; } } else { $this->extendSection($section, $content instanceof View ? $content : e($content)); } } /** * Inject inline content into a section. * * @param string $section * @param string $content * @return void */ public function inject($section, $content) { $this->startSection($section, $content); } /** * Stop injecting content into a section and return its contents. * * @return string */ public function yieldSection() { if (empty($this->sectionStack)) { return ''; } return $this->yieldContent($this->stopSection()); } /** * Stop injecting content into a section. * * @param bool $overwrite * @return string * * @throws \InvalidArgumentException */ public function stopSection($overwrite = false) { if (empty($this->sectionStack)) { throw new InvalidArgumentException('Cannot end a section without first starting one.'); } $last = array_pop($this->sectionStack); if ($overwrite) { $this->sections[$last] = ob_get_clean(); } else { $this->extendSection($last, ob_get_clean()); } return $last; } /** * Stop injecting content into a section and append it. * * @return string * * @throws \InvalidArgumentException */ public function appendSection() { if (empty($this->sectionStack)) { throw new InvalidArgumentException('Cannot end a section without first starting one.'); } $last = array_pop($this->sectionStack); if (isset($this->sections[$last])) { $this->sections[$last] .= ob_get_clean(); } else { $this->sections[$last] = ob_get_clean(); } return $last; } /** * Append content to a given section. * * @param string $section * @param string $content * @return void */ protected function extendSection($section, $content) { if (isset($this->sections[$section])) { $content = str_replace(static::parentPlaceholder($section), $content, $this->sections[$section]); } $this->sections[$section] = $content; } /** * Get the string contents of a section. * * @param string $section * @param string $default * @return string */ public function yieldContent($section, $default = '') { $sectionContent = $default instanceof View ? $default : e($default); if (isset($this->sections[$section])) { $sectionContent = $this->sections[$section]; } $sectionContent = str_replace('@@parent', '--parent--holder--', $sectionContent); return str_replace( '--parent--holder--', '@parent', str_replace(static::parentPlaceholder($section), '', $sectionContent) ); } /** * Get the parent placeholder for the current request. * * @param string $section * @return string */ public static function parentPlaceholder($section = '') { if (! isset(static::$parentPlaceholder[$section])) { $salt = static::parentPlaceholderSalt(); static::$parentPlaceholder[$section] = '##parent-placeholder-'.hash('xxh128', $salt.$section).'##'; } return static::$parentPlaceholder[$section]; } /** * Get the parent placeholder salt. * * @return string */ protected static function parentPlaceholderSalt() { if (! static::$parentPlaceholderSalt) { return static::$parentPlaceholderSalt = Str::random(40); } return static::$parentPlaceholderSalt; } /** * Check if section exists. * * @param string $name * @return bool */ public function hasSection($name) { return array_key_exists($name, $this->sections); } /** * Check if section does not exist. * * @param string $name * @return bool */ public function sectionMissing($name) { return ! $this->hasSection($name); } /** * Get the contents of a section. * * @param string $name * @param string|null $default * @return mixed */ public function getSection($name, $default = null) { return $this->getSections()[$name] ?? $default; } /** * Get the entire array of sections. * * @return array */ public function getSections() { return $this->sections; } /** * Flush all of the sections. * * @return void */ public function flushSections() { $this->sections = []; $this->sectionStack = []; } } src/Illuminate/View/Concerns/ManagesTranslations.php 0000644 00000001373 15107327040 0016613 0 ustar 00 <?php namespace Illuminate\View\Concerns; trait ManagesTranslations { /** * The translation replacements for the translation being rendered. * * @var array */ protected $translationReplacements = []; /** * Start a translation block. * * @param array $replacements * @return void */ public function startTranslation($replacements = []) { ob_start(); $this->translationReplacements = $replacements; } /** * Render the current translation. * * @return string */ public function renderTranslation() { return $this->container->make('translator')->get( trim(ob_get_clean()), $this->translationReplacements ); } } src/Illuminate/View/Concerns/ManagesComponents.php 0000644 00000012523 15107327040 0016256 0 ustar 00 <?php namespace Illuminate\View\Concerns; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Contracts\View\View; use Illuminate\Support\Arr; use Illuminate\View\ComponentSlot; trait ManagesComponents { /** * The components being rendered. * * @var array */ protected $componentStack = []; /** * The original data passed to the component. * * @var array */ protected $componentData = []; /** * The component data for the component that is currently being rendered. * * @var array */ protected $currentComponentData = []; /** * The slot contents for the component. * * @var array */ protected $slots = []; /** * The names of the slots being rendered. * * @var array */ protected $slotStack = []; /** * Start a component rendering process. * * @param \Illuminate\Contracts\View\View|\Illuminate\Contracts\Support\Htmlable|\Closure|string $view * @param array $data * @return void */ public function startComponent($view, array $data = []) { if (ob_start()) { $this->componentStack[] = $view; $this->componentData[$this->currentComponent()] = $data; $this->slots[$this->currentComponent()] = []; } } /** * Get the first view that actually exists from the given list, and start a component. * * @param array $names * @param array $data * @return void */ public function startComponentFirst(array $names, array $data = []) { $name = Arr::first($names, function ($item) { return $this->exists($item); }); $this->startComponent($name, $data); } /** * Render the current component. * * @return string */ public function renderComponent() { $view = array_pop($this->componentStack); $this->currentComponentData = array_merge( $previousComponentData = $this->currentComponentData, $data = $this->componentData() ); try { $view = value($view, $data); if ($view instanceof View) { return $view->with($data)->render(); } elseif ($view instanceof Htmlable) { return $view->toHtml(); } else { return $this->make($view, $data)->render(); } } finally { $this->currentComponentData = $previousComponentData; } } /** * Get the data for the given component. * * @return array */ protected function componentData() { $defaultSlot = new ComponentSlot(trim(ob_get_clean())); $slots = array_merge([ '__default' => $defaultSlot, ], $this->slots[count($this->componentStack)]); return array_merge( $this->componentData[count($this->componentStack)], ['slot' => $defaultSlot], $this->slots[count($this->componentStack)], ['__laravel_slots' => $slots] ); } /** * Get an item from the component data that exists above the current component. * * @param string $key * @param mixed $default * @return mixed|null */ public function getConsumableComponentData($key, $default = null) { if (array_key_exists($key, $this->currentComponentData)) { return $this->currentComponentData[$key]; } $currentComponent = count($this->componentStack); if ($currentComponent === 0) { return value($default); } for ($i = $currentComponent - 1; $i >= 0; $i--) { $data = $this->componentData[$i] ?? []; if (array_key_exists($key, $data)) { return $data[$key]; } } return value($default); } /** * Start the slot rendering process. * * @param string $name * @param string|null $content * @param array $attributes * @return void */ public function slot($name, $content = null, $attributes = []) { if (func_num_args() === 2 || $content !== null) { $this->slots[$this->currentComponent()][$name] = $content; } elseif (ob_start()) { $this->slots[$this->currentComponent()][$name] = ''; $this->slotStack[$this->currentComponent()][] = [$name, $attributes]; } } /** * Save the slot content for rendering. * * @return void */ public function endSlot() { last($this->componentStack); $currentSlot = array_pop( $this->slotStack[$this->currentComponent()] ); [$currentName, $currentAttributes] = $currentSlot; $this->slots[$this->currentComponent()][$currentName] = new ComponentSlot( trim(ob_get_clean()), $currentAttributes ); } /** * Get the index for the current component. * * @return int */ protected function currentComponent() { return count($this->componentStack) - 1; } /** * Flush all of the component state. * * @return void */ protected function flushComponents() { $this->componentStack = []; $this->componentData = []; $this->currentComponentData = []; } } src/Illuminate/View/View.php 0000644 00000025610 15107327040 0011776 0 ustar 00 <?php namespace Illuminate\View; use ArrayAccess; use BadMethodCallException; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Contracts\Support\MessageProvider; use Illuminate\Contracts\Support\Renderable; use Illuminate\Contracts\View\Engine; use Illuminate\Contracts\View\View as ViewContract; use Illuminate\Support\MessageBag; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use Illuminate\Support\ViewErrorBag; use Throwable; class View implements ArrayAccess, Htmlable, ViewContract { use Macroable { __call as macroCall; } /** * The view factory instance. * * @var \Illuminate\View\Factory */ protected $factory; /** * The engine implementation. * * @var \Illuminate\Contracts\View\Engine */ protected $engine; /** * The name of the view. * * @var string */ protected $view; /** * The array of view data. * * @var array */ protected $data; /** * The path to the view file. * * @var string */ protected $path; /** * Create a new view instance. * * @param \Illuminate\View\Factory $factory * @param \Illuminate\Contracts\View\Engine $engine * @param string $view * @param string $path * @param mixed $data * @return void */ public function __construct(Factory $factory, Engine $engine, $view, $path, $data = []) { $this->view = $view; $this->path = $path; $this->engine = $engine; $this->factory = $factory; $this->data = $data instanceof Arrayable ? $data->toArray() : (array) $data; } /** * Get the evaluated contents of a given fragment. * * @param string $fragment * @return string */ public function fragment($fragment) { return $this->render(function () use ($fragment) { return $this->factory->getFragment($fragment); }); } /** * Get the evaluated contents for a given array of fragments. * * @param array $fragments * @return string */ public function fragments(array $fragments) { return collect($fragments)->map(fn ($f) => $this->fragment($f))->implode(''); } /** * Get the evaluated contents of a given fragment if the given condition is true. * * @param bool $boolean * @param string $fragment * @return string */ public function fragmentIf($boolean, $fragment) { if (value($boolean)) { return $this->fragment($fragment); } return $this->render(); } /** * Get the evaluated contents for a given array of fragments if the given condition is true. * * @param bool $boolean * @param array $fragments * @return string */ public function fragmentsIf($boolean, array $fragments) { if (value($boolean)) { return $this->fragments($fragments); } return $this->render(); } /** * Get the string contents of the view. * * @param callable|null $callback * @return string * * @throws \Throwable */ public function render(callable $callback = null) { try { $contents = $this->renderContents(); $response = isset($callback) ? $callback($this, $contents) : null; // Once we have the contents of the view, we will flush the sections if we are // done rendering all views so that there is nothing left hanging over when // another view gets rendered in the future by the application developer. $this->factory->flushStateIfDoneRendering(); return ! is_null($response) ? $response : $contents; } catch (Throwable $e) { $this->factory->flushState(); throw $e; } } /** * Get the contents of the view instance. * * @return string */ protected function renderContents() { // We will keep track of the number of views being rendered so we can flush // the section after the complete rendering operation is done. This will // clear out the sections for any separate views that may be rendered. $this->factory->incrementRender(); $this->factory->callComposer($this); $contents = $this->getContents(); // Once we've finished rendering the view, we'll decrement the render count // so that each section gets flushed out next time a view is created and // no old sections are staying around in the memory of an environment. $this->factory->decrementRender(); return $contents; } /** * Get the evaluated contents of the view. * * @return string */ protected function getContents() { return $this->engine->get($this->path, $this->gatherData()); } /** * Get the data bound to the view instance. * * @return array */ public function gatherData() { $data = array_merge($this->factory->getShared(), $this->data); foreach ($data as $key => $value) { if ($value instanceof Renderable) { $data[$key] = $value->render(); } } return $data; } /** * Get the sections of the rendered view. * * @return array * * @throws \Throwable */ public function renderSections() { return $this->render(function () { return $this->factory->getSections(); }); } /** * Add a piece of data to the view. * * @param string|array $key * @param mixed $value * @return $this */ public function with($key, $value = null) { if (is_array($key)) { $this->data = array_merge($this->data, $key); } else { $this->data[$key] = $value; } return $this; } /** * Add a view instance to the view data. * * @param string $key * @param string $view * @param array $data * @return $this */ public function nest($key, $view, array $data = []) { return $this->with($key, $this->factory->make($view, $data)); } /** * Add validation errors to the view. * * @param \Illuminate\Contracts\Support\MessageProvider|array $provider * @param string $bag * @return $this */ public function withErrors($provider, $bag = 'default') { return $this->with('errors', (new ViewErrorBag)->put( $bag, $this->formatErrors($provider) )); } /** * Parse the given errors into an appropriate value. * * @param \Illuminate\Contracts\Support\MessageProvider|array|string $provider * @return \Illuminate\Support\MessageBag */ protected function formatErrors($provider) { return $provider instanceof MessageProvider ? $provider->getMessageBag() : new MessageBag((array) $provider); } /** * Get the name of the view. * * @return string */ public function name() { return $this->getName(); } /** * Get the name of the view. * * @return string */ public function getName() { return $this->view; } /** * Get the array of view data. * * @return array */ public function getData() { return $this->data; } /** * Get the path to the view file. * * @return string */ public function getPath() { return $this->path; } /** * Set the path to the view. * * @param string $path * @return void */ public function setPath($path) { $this->path = $path; } /** * Get the view factory instance. * * @return \Illuminate\View\Factory */ public function getFactory() { return $this->factory; } /** * Get the view's rendering engine. * * @return \Illuminate\Contracts\View\Engine */ public function getEngine() { return $this->engine; } /** * Determine if a piece of data is bound. * * @param string $key * @return bool */ public function offsetExists($key): bool { return array_key_exists($key, $this->data); } /** * Get a piece of bound data to the view. * * @param string $key * @return mixed */ public function offsetGet($key): mixed { return $this->data[$key]; } /** * Set a piece of data on the view. * * @param string $key * @param mixed $value * @return void */ public function offsetSet($key, $value): void { $this->with($key, $value); } /** * Unset a piece of data from the view. * * @param string $key * @return void */ public function offsetUnset($key): void { unset($this->data[$key]); } /** * Get a piece of data from the view. * * @param string $key * @return mixed */ public function &__get($key) { return $this->data[$key]; } /** * Set a piece of data on the view. * * @param string $key * @param mixed $value * @return void */ public function __set($key, $value) { $this->with($key, $value); } /** * Check if a piece of data is bound to the view. * * @param string $key * @return bool */ public function __isset($key) { return isset($this->data[$key]); } /** * Remove a piece of bound data from the view. * * @param string $key * @return void */ public function __unset($key) { unset($this->data[$key]); } /** * Dynamically bind parameters to the view. * * @param string $method * @param array $parameters * @return \Illuminate\View\View * * @throws \BadMethodCallException */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } if (! str_starts_with($method, 'with')) { throw new BadMethodCallException(sprintf( 'Method %s::%s does not exist.', static::class, $method )); } return $this->with(Str::camel(substr($method, 4)), $parameters[0]); } /** * Get content as a string of HTML. * * @return string */ public function toHtml() { return $this->render(); } /** * Get the string contents of the view. * * @return string * * @throws \Throwable */ public function __toString() { return $this->render(); } } src/Illuminate/View/Component.php 0000644 00000027627 15107327040 0013040 0 ustar 00 <?php namespace Illuminate\View; use Closure; use Illuminate\Container\Container; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Contracts\View\View as ViewContract; use ReflectionClass; use ReflectionMethod; use ReflectionProperty; abstract class Component { /** * The properties / methods that should not be exposed to the component. * * @var array */ protected $except = []; /** * The component alias name. * * @var string */ public $componentName; /** * The component attributes. * * @var \Illuminate\View\ComponentAttributeBag */ public $attributes; /** * The view factory instance, if any. * * @var \Illuminate\Contracts\View\Factory|null */ protected static $factory; /** * The component resolver callback. * * @var (\Closure(string, array): Component)|null */ protected static $componentsResolver; /** * The cache of blade view names, keyed by contents. * * @var array<string, string> */ protected static $bladeViewCache = []; /** * The cache of public property names, keyed by class. * * @var array */ protected static $propertyCache = []; /** * The cache of public method names, keyed by class. * * @var array */ protected static $methodCache = []; /** * The cache of constructor parameters, keyed by class. * * @var array<class-string, array<int, string>> */ protected static $constructorParametersCache = []; /** * Get the view / view contents that represent the component. * * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\Support\Htmlable|\Closure|string */ abstract public function render(); /** * Resolve the component instance with the given data. * * @param array $data * @return static */ public static function resolve($data) { if (static::$componentsResolver) { return call_user_func(static::$componentsResolver, static::class, $data); } $parameters = static::extractConstructorParameters(); $dataKeys = array_keys($data); if (empty(array_diff($parameters, $dataKeys))) { return new static(...array_intersect_key($data, array_flip($parameters))); } return Container::getInstance()->make(static::class, $data); } /** * Extract the constructor parameters for the component. * * @return array */ protected static function extractConstructorParameters() { if (! isset(static::$constructorParametersCache[static::class])) { $class = new ReflectionClass(static::class); $constructor = $class->getConstructor(); static::$constructorParametersCache[static::class] = $constructor ? collect($constructor->getParameters())->map->getName()->all() : []; } return static::$constructorParametersCache[static::class]; } /** * Resolve the Blade view or view file that should be used when rendering the component. * * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\Support\Htmlable|\Closure|string */ public function resolveView() { $view = $this->render(); if ($view instanceof ViewContract) { return $view; } if ($view instanceof Htmlable) { return $view; } $resolver = function ($view) { if ($view instanceof ViewContract) { return $view; } return $this->extractBladeViewFromString($view); }; return $view instanceof Closure ? function (array $data = []) use ($view, $resolver) { return $resolver($view($data)); } : $resolver($view); } /** * Create a Blade view with the raw component string content. * * @param string $contents * @return string */ protected function extractBladeViewFromString($contents) { $key = sprintf('%s::%s', static::class, $contents); if (isset(static::$bladeViewCache[$key])) { return static::$bladeViewCache[$key]; } if (strlen($contents) <= PHP_MAXPATHLEN && $this->factory()->exists($contents)) { return static::$bladeViewCache[$key] = $contents; } return static::$bladeViewCache[$key] = $this->createBladeViewFromString($this->factory(), $contents); } /** * Create a Blade view with the raw component string content. * * @param \Illuminate\Contracts\View\Factory $factory * @param string $contents * @return string */ protected function createBladeViewFromString($factory, $contents) { $factory->addNamespace( '__components', $directory = Container::getInstance()['config']->get('view.compiled') ); if (! is_file($viewFile = $directory.'/'.hash('xxh128', $contents).'.blade.php')) { if (! is_dir($directory)) { mkdir($directory, 0755, true); } file_put_contents($viewFile, $contents); } return '__components::'.basename($viewFile, '.blade.php'); } /** * Get the data that should be supplied to the view. * * @author Freek Van der Herten * @author Brent Roose * * @return array */ public function data() { $this->attributes = $this->attributes ?: $this->newAttributeBag(); return array_merge($this->extractPublicProperties(), $this->extractPublicMethods()); } /** * Extract the public properties for the component. * * @return array */ protected function extractPublicProperties() { $class = get_class($this); if (! isset(static::$propertyCache[$class])) { $reflection = new ReflectionClass($this); static::$propertyCache[$class] = collect($reflection->getProperties(ReflectionProperty::IS_PUBLIC)) ->reject(function (ReflectionProperty $property) { return $property->isStatic(); }) ->reject(function (ReflectionProperty $property) { return $this->shouldIgnore($property->getName()); }) ->map(function (ReflectionProperty $property) { return $property->getName(); })->all(); } $values = []; foreach (static::$propertyCache[$class] as $property) { $values[$property] = $this->{$property}; } return $values; } /** * Extract the public methods for the component. * * @return array */ protected function extractPublicMethods() { $class = get_class($this); if (! isset(static::$methodCache[$class])) { $reflection = new ReflectionClass($this); static::$methodCache[$class] = collect($reflection->getMethods(ReflectionMethod::IS_PUBLIC)) ->reject(function (ReflectionMethod $method) { return $this->shouldIgnore($method->getName()); }) ->map(function (ReflectionMethod $method) { return $method->getName(); }); } $values = []; foreach (static::$methodCache[$class] as $method) { $values[$method] = $this->createVariableFromMethod(new ReflectionMethod($this, $method)); } return $values; } /** * Create a callable variable from the given method. * * @param \ReflectionMethod $method * @return mixed */ protected function createVariableFromMethod(ReflectionMethod $method) { return $method->getNumberOfParameters() === 0 ? $this->createInvokableVariable($method->getName()) : Closure::fromCallable([$this, $method->getName()]); } /** * Create an invokable, toStringable variable for the given component method. * * @param string $method * @return \Illuminate\View\InvokableComponentVariable */ protected function createInvokableVariable(string $method) { return new InvokableComponentVariable(function () use ($method) { return $this->{$method}(); }); } /** * Determine if the given property / method should be ignored. * * @param string $name * @return bool */ protected function shouldIgnore($name) { return str_starts_with($name, '__') || in_array($name, $this->ignoredMethods()); } /** * Get the methods that should be ignored. * * @return array */ protected function ignoredMethods() { return array_merge([ 'data', 'render', 'resolve', 'resolveView', 'shouldRender', 'view', 'withName', 'withAttributes', 'flushCache', 'forgetFactory', 'forgetComponentsResolver', 'resolveComponentsUsing', ], $this->except); } /** * Set the component alias name. * * @param string $name * @return $this */ public function withName($name) { $this->componentName = $name; return $this; } /** * Set the extra attributes that the component should make available. * * @param array $attributes * @return $this */ public function withAttributes(array $attributes) { $this->attributes = $this->attributes ?: $this->newAttributeBag(); $this->attributes->setAttributes($attributes); return $this; } /** * Get a new attribute bag instance. * * @param array $attributes * @return \Illuminate\View\ComponentAttributeBag */ protected function newAttributeBag(array $attributes = []) { return new ComponentAttributeBag($attributes); } /** * Determine if the component should be rendered. * * @return bool */ public function shouldRender() { return true; } /** * Get the evaluated view contents for the given view. * * @param string|null $view * @param \Illuminate\Contracts\Support\Arrayable|array $data * @param array $mergeData * @return \Illuminate\Contracts\View\View */ public function view($view, $data = [], $mergeData = []) { return $this->factory()->make($view, $data, $mergeData); } /** * Get the view factory instance. * * @return \Illuminate\Contracts\View\Factory */ protected function factory() { if (is_null(static::$factory)) { static::$factory = Container::getInstance()->make('view'); } return static::$factory; } /** * Flush the component's cached state. * * @return void */ public static function flushCache() { static::$bladeViewCache = []; static::$constructorParametersCache = []; static::$methodCache = []; static::$propertyCache = []; } /** * Forget the component's factory instance. * * @return void */ public static function forgetFactory() { static::$factory = null; } /** * Forget the component's resolver callback. * * @return void * * @internal */ public static function forgetComponentsResolver() { static::$componentsResolver = null; } /** * Set the callback that should be used to resolve components within views. * * @param \Closure(string $component, array $data): Component $resolver * @return void * * @internal */ public static function resolveComponentsUsing($resolver) { static::$componentsResolver = $resolver; } } src/Illuminate/View/AppendableAttributeValue.php 0000644 00000001010 15107327040 0015764 0 ustar 00 <?php namespace Illuminate\View; class AppendableAttributeValue { /** * The attribute value. * * @var mixed */ public $value; /** * Create a new appendable attribute value. * * @param mixed $value * @return void */ public function __construct($value) { $this->value = $value; } /** * Get the string value. * * @return string */ public function __toString() { return (string) $this->value; } } src/Illuminate/View/ComponentSlot.php 0000644 00000003100 15107327040 0013656 0 ustar 00 <?php namespace Illuminate\View; use Illuminate\Contracts\Support\Htmlable; class ComponentSlot implements Htmlable { /** * The slot attribute bag. * * @var \Illuminate\View\ComponentAttributeBag */ public $attributes; /** * The slot contents. * * @var string */ protected $contents; /** * Create a new slot instance. * * @param string $contents * @param array $attributes * @return void */ public function __construct($contents = '', $attributes = []) { $this->contents = $contents; $this->withAttributes($attributes); } /** * Set the extra attributes that the slot should make available. * * @param array $attributes * @return $this */ public function withAttributes(array $attributes) { $this->attributes = new ComponentAttributeBag($attributes); return $this; } /** * Get the slot's HTML string. * * @return string */ public function toHtml() { return $this->contents; } /** * Determine if the slot is empty. * * @return bool */ public function isEmpty() { return $this->contents === ''; } /** * Determine if the slot is not empty. * * @return bool */ public function isNotEmpty() { return ! $this->isEmpty(); } /** * Get the slot's HTML string. * * @return string */ public function __toString() { return $this->toHtml(); } } src/Illuminate/View/composer.json 0000644 00000002027 15107327040 0013072 0 ustar 00 { "name": "illuminate/view", "description": "The Illuminate View package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "ext-tokenizer": "*", "illuminate/collections": "^10.0", "illuminate/container": "^10.0", "illuminate/contracts": "^10.0", "illuminate/events": "^10.0", "illuminate/filesystem": "^10.0", "illuminate/macroable": "^10.0", "illuminate/support": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\View\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/View/DynamicComponent.php 0000644 00000011240 15107327040 0014325 0 ustar 00 <?php namespace Illuminate\View; use Illuminate\Container\Container; use Illuminate\Support\Str; use Illuminate\View\Compilers\ComponentTagCompiler; class DynamicComponent extends Component { /** * The name of the component. * * @var string */ public $component; /** * The component tag compiler instance. * * @var \Illuminate\View\Compilers\BladeTagCompiler */ protected static $compiler; /** * The cached component classes. * * @var array */ protected static $componentClasses = []; /** * Create a new component instance. * * @param string $component * @return void */ public function __construct(string $component) { $this->component = $component; } /** * Get the view / contents that represent the component. * * @return \Illuminate\Contracts\View\View|string */ public function render() { $template = <<<'EOF' <?php extract(collect($attributes->getAttributes())->mapWithKeys(function ($value, $key) { return [Illuminate\Support\Str::camel(str_replace([':', '.'], ' ', $key)) => $value]; })->all(), EXTR_SKIP); ?> {{ props }} <x-{{ component }} {{ bindings }} {{ attributes }}> {{ slots }} {{ defaultSlot }} </x-{{ component }}> EOF; return function ($data) use ($template) { $bindings = $this->bindings($class = $this->classForComponent()); return str_replace( [ '{{ component }}', '{{ props }}', '{{ bindings }}', '{{ attributes }}', '{{ slots }}', '{{ defaultSlot }}', ], [ $this->component, $this->compileProps($bindings), $this->compileBindings($bindings), class_exists($class) ? '{{ $attributes }}' : '', $this->compileSlots($data['__laravel_slots']), '{{ $slot ?? "" }}', ], $template ); }; } /** * Compile the @props directive for the component. * * @param array $bindings * @return string */ protected function compileProps(array $bindings) { if (empty($bindings)) { return ''; } return '@props('.'[\''.implode('\',\'', collect($bindings)->map(function ($dataKey) { return Str::camel($dataKey); })->all()).'\']'.')'; } /** * Compile the bindings for the component. * * @param array $bindings * @return string */ protected function compileBindings(array $bindings) { return collect($bindings)->map(function ($key) { return ':'.$key.'="$'.Str::camel(str_replace([':', '.'], ' ', $key)).'"'; })->implode(' '); } /** * Compile the slots for the component. * * @param array $slots * @return string */ protected function compileSlots(array $slots) { return collect($slots)->map(function ($slot, $name) { return $name === '__default' ? null : '<x-slot name="'.$name.'" '.((string) $slot->attributes).'>{{ $'.$name.' }}</x-slot>'; })->filter()->implode(PHP_EOL); } /** * Get the class for the current component. * * @return string */ protected function classForComponent() { if (isset(static::$componentClasses[$this->component])) { return static::$componentClasses[$this->component]; } return static::$componentClasses[$this->component] = $this->compiler()->componentClass($this->component); } /** * Get the names of the variables that should be bound to the component. * * @param string $class * @return array */ protected function bindings(string $class) { [$data, $attributes] = $this->compiler()->partitionDataAndAttributes($class, $this->attributes->getAttributes()); return array_keys($data->all()); } /** * Get an instance of the Blade tag compiler. * * @return \Illuminate\View\Compilers\ComponentTagCompiler */ protected function compiler() { if (! static::$compiler) { static::$compiler = new ComponentTagCompiler( Container::getInstance()->make('blade.compiler')->getClassComponentAliases(), Container::getInstance()->make('blade.compiler')->getClassComponentNamespaces(), Container::getInstance()->make('blade.compiler') ); } return static::$compiler; } } src/Illuminate/View/ComponentAttributeBag.php 0000644 00000027544 15107327040 0015334 0 ustar 00 <?php namespace Illuminate\View; use ArrayAccess; use ArrayIterator; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\Arr; use Illuminate\Support\HtmlString; use Illuminate\Support\Str; use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\Macroable; use IteratorAggregate; use JsonSerializable; use Traversable; class ComponentAttributeBag implements ArrayAccess, IteratorAggregate, JsonSerializable, Htmlable { use Conditionable, Macroable; /** * The raw array of attributes. * * @var array */ protected $attributes = []; /** * Create a new component attribute bag instance. * * @param array $attributes * @return void */ public function __construct(array $attributes = []) { $this->attributes = $attributes; } /** * Get the first attribute's value. * * @param mixed $default * @return mixed */ public function first($default = null) { return $this->getIterator()->current() ?? value($default); } /** * Get a given attribute from the attribute array. * * @param string $key * @param mixed $default * @return mixed */ public function get($key, $default = null) { return $this->attributes[$key] ?? value($default); } /** * Determine if a given attribute exists in the attribute array. * * @param array|string $key * @return bool */ public function has($key) { $keys = is_array($key) ? $key : func_get_args(); foreach ($keys as $value) { if (! array_key_exists($value, $this->attributes)) { return false; } } return true; } /** * Determine if any of the keys exist in the attribute array. * * @param array|string $key * @return bool */ public function hasAny($key) { if (! count($this->attributes)) { return false; } $keys = is_array($key) ? $key : func_get_args(); foreach ($keys as $value) { if ($this->has($value)) { return true; } } return false; } /** * Determine if a given attribute is missing from the attribute array. * * @param string $key * @return bool */ public function missing($key) { return ! $this->has($key); } /** * Only include the given attribute from the attribute array. * * @param mixed $keys * @return static */ public function only($keys) { if (is_null($keys)) { $values = $this->attributes; } else { $keys = Arr::wrap($keys); $values = Arr::only($this->attributes, $keys); } return new static($values); } /** * Exclude the given attribute from the attribute array. * * @param mixed|array $keys * @return static */ public function except($keys) { if (is_null($keys)) { $values = $this->attributes; } else { $keys = Arr::wrap($keys); $values = Arr::except($this->attributes, $keys); } return new static($values); } /** * Filter the attributes, returning a bag of attributes that pass the filter. * * @param callable $callback * @return static */ public function filter($callback) { return new static(collect($this->attributes)->filter($callback)->all()); } /** * Return a bag of attributes that have keys starting with the given value / pattern. * * @param string|string[] $needles * @return static */ public function whereStartsWith($needles) { return $this->filter(function ($value, $key) use ($needles) { return Str::startsWith($key, $needles); }); } /** * Return a bag of attributes with keys that do not start with the given value / pattern. * * @param string|string[] $needles * @return static */ public function whereDoesntStartWith($needles) { return $this->filter(function ($value, $key) use ($needles) { return ! Str::startsWith($key, $needles); }); } /** * Return a bag of attributes that have keys starting with the given value / pattern. * * @param string|string[] $needles * @return static */ public function thatStartWith($needles) { return $this->whereStartsWith($needles); } /** * Only include the given attribute from the attribute array. * * @param mixed|array $keys * @return static */ public function onlyProps($keys) { return $this->only($this->extractPropNames($keys)); } /** * Exclude the given attribute from the attribute array. * * @param mixed|array $keys * @return static */ public function exceptProps($keys) { return $this->except($this->extractPropNames($keys)); } /** * Extract prop names from given keys. * * @param mixed|array $keys * @return array */ protected function extractPropNames($keys) { $props = []; foreach ($keys as $key => $defaultValue) { $key = is_numeric($key) ? $defaultValue : $key; $props[] = $key; $props[] = Str::kebab($key); } return $props; } /** * Conditionally merge classes into the attribute bag. * * @param mixed|array $classList * @return static */ public function class($classList) { $classList = Arr::wrap($classList); return $this->merge(['class' => Arr::toCssClasses($classList)]); } /** * Conditionally merge styles into the attribute bag. * * @param mixed|array $styleList * @return static */ public function style($styleList) { $styleList = Arr::wrap($styleList); return $this->merge(['style' => Arr::toCssStyles($styleList)]); } /** * Merge additional attributes / values into the attribute bag. * * @param array $attributeDefaults * @param bool $escape * @return static */ public function merge(array $attributeDefaults = [], $escape = true) { $attributeDefaults = array_map(function ($value) use ($escape) { return $this->shouldEscapeAttributeValue($escape, $value) ? e($value) : $value; }, $attributeDefaults); [$appendableAttributes, $nonAppendableAttributes] = collect($this->attributes) ->partition(function ($value, $key) use ($attributeDefaults) { return $key === 'class' || $key === 'style' || ( isset($attributeDefaults[$key]) && $attributeDefaults[$key] instanceof AppendableAttributeValue ); }); $attributes = $appendableAttributes->mapWithKeys(function ($value, $key) use ($attributeDefaults, $escape) { $defaultsValue = isset($attributeDefaults[$key]) && $attributeDefaults[$key] instanceof AppendableAttributeValue ? $this->resolveAppendableAttributeDefault($attributeDefaults, $key, $escape) : ($attributeDefaults[$key] ?? ''); if ($key === 'style') { $value = Str::finish($value, ';'); } return [$key => implode(' ', array_unique(array_filter([$defaultsValue, $value])))]; })->merge($nonAppendableAttributes)->all(); return new static(array_merge($attributeDefaults, $attributes)); } /** * Determine if the specific attribute value should be escaped. * * @param bool $escape * @param mixed $value * @return bool */ protected function shouldEscapeAttributeValue($escape, $value) { if (! $escape) { return false; } return ! is_object($value) && ! is_null($value) && ! is_bool($value); } /** * Create a new appendable attribute value. * * @param mixed $value * @return \Illuminate\View\AppendableAttributeValue */ public function prepends($value) { return new AppendableAttributeValue($value); } /** * Resolve an appendable attribute value default value. * * @param array $attributeDefaults * @param string $key * @param bool $escape * @return mixed */ protected function resolveAppendableAttributeDefault($attributeDefaults, $key, $escape) { if ($this->shouldEscapeAttributeValue($escape, $value = $attributeDefaults[$key]->value)) { $value = e($value); } return $value; } /** * Get all of the raw attributes. * * @return array */ public function getAttributes() { return $this->attributes; } /** * Set the underlying attributes. * * @param array $attributes * @return void */ public function setAttributes(array $attributes) { if (isset($attributes['attributes']) && $attributes['attributes'] instanceof self) { $parentBag = $attributes['attributes']; unset($attributes['attributes']); $attributes = $parentBag->merge($attributes, $escape = false)->getAttributes(); } $this->attributes = $attributes; } /** * Get content as a string of HTML. * * @return string */ public function toHtml() { return (string) $this; } /** * Merge additional attributes / values into the attribute bag. * * @param array $attributeDefaults * @return \Illuminate\Support\HtmlString */ public function __invoke(array $attributeDefaults = []) { return new HtmlString((string) $this->merge($attributeDefaults)); } /** * Determine if the given offset exists. * * @param string $offset * @return bool */ public function offsetExists($offset): bool { return isset($this->attributes[$offset]); } /** * Get the value at the given offset. * * @param string $offset * @return mixed */ public function offsetGet($offset): mixed { return $this->get($offset); } /** * Set the value at a given offset. * * @param string $offset * @param mixed $value * @return void */ public function offsetSet($offset, $value): void { $this->attributes[$offset] = $value; } /** * Remove the value at the given offset. * * @param string $offset * @return void */ public function offsetUnset($offset): void { unset($this->attributes[$offset]); } /** * Get an iterator for the items. * * @return \ArrayIterator */ public function getIterator(): Traversable { return new ArrayIterator($this->attributes); } /** * Convert the object into a JSON serializable form. * * @return mixed */ public function jsonSerialize(): mixed { return $this->attributes; } /** * Implode the attributes into a single HTML ready string. * * @return string */ public function __toString() { $string = ''; foreach ($this->attributes as $key => $value) { if ($value === false || is_null($value)) { continue; } if ($value === true) { // Exception for Alpine... $value = $key === 'x-data' ? '' : $key; } $string .= ' '.$key.'="'.str_replace('"', '\\"', trim($value)).'"'; } return trim($string); } } src/Illuminate/View/Factory.php 0000644 00000035166 15107327040 0012502 0 ustar 00 <?php namespace Illuminate\View; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\View\Factory as FactoryContract; use Illuminate\Support\Arr; use Illuminate\Support\Traits\Macroable; use Illuminate\View\Engines\EngineResolver; use InvalidArgumentException; class Factory implements FactoryContract { use Macroable, Concerns\ManagesComponents, Concerns\ManagesEvents, Concerns\ManagesFragments, Concerns\ManagesLayouts, Concerns\ManagesLoops, Concerns\ManagesStacks, Concerns\ManagesTranslations; /** * The engine implementation. * * @var \Illuminate\View\Engines\EngineResolver */ protected $engines; /** * The view finder implementation. * * @var \Illuminate\View\ViewFinderInterface */ protected $finder; /** * The event dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * The IoC container instance. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * Data that should be available to all templates. * * @var array */ protected $shared = []; /** * The extension to engine bindings. * * @var array */ protected $extensions = [ 'blade.php' => 'blade', 'php' => 'php', 'css' => 'file', 'html' => 'file', ]; /** * The view composer events. * * @var array */ protected $composers = []; /** * The number of active rendering operations. * * @var int */ protected $renderCount = 0; /** * The "once" block IDs that have been rendered. * * @var array */ protected $renderedOnce = []; /** * Create a new view factory instance. * * @param \Illuminate\View\Engines\EngineResolver $engines * @param \Illuminate\View\ViewFinderInterface $finder * @param \Illuminate\Contracts\Events\Dispatcher $events * @return void */ public function __construct(EngineResolver $engines, ViewFinderInterface $finder, Dispatcher $events) { $this->finder = $finder; $this->events = $events; $this->engines = $engines; $this->share('__env', $this); } /** * Get the evaluated view contents for the given view. * * @param string $path * @param \Illuminate\Contracts\Support\Arrayable|array $data * @param array $mergeData * @return \Illuminate\Contracts\View\View */ public function file($path, $data = [], $mergeData = []) { $data = array_merge($mergeData, $this->parseData($data)); return tap($this->viewInstance($path, $path, $data), function ($view) { $this->callCreator($view); }); } /** * Get the evaluated view contents for the given view. * * @param string $view * @param \Illuminate\Contracts\Support\Arrayable|array $data * @param array $mergeData * @return \Illuminate\Contracts\View\View */ public function make($view, $data = [], $mergeData = []) { $path = $this->finder->find( $view = $this->normalizeName($view) ); // Next, we will create the view instance and call the view creator for the view // which can set any data, etc. Then we will return the view instance back to // the caller for rendering or performing other view manipulations on this. $data = array_merge($mergeData, $this->parseData($data)); return tap($this->viewInstance($view, $path, $data), function ($view) { $this->callCreator($view); }); } /** * Get the first view that actually exists from the given list. * * @param array $views * @param \Illuminate\Contracts\Support\Arrayable|array $data * @param array $mergeData * @return \Illuminate\Contracts\View\View * * @throws \InvalidArgumentException */ public function first(array $views, $data = [], $mergeData = []) { $view = Arr::first($views, function ($view) { return $this->exists($view); }); if (! $view) { throw new InvalidArgumentException('None of the views in the given array exist.'); } return $this->make($view, $data, $mergeData); } /** * Get the rendered content of the view based on a given condition. * * @param bool $condition * @param string $view * @param \Illuminate\Contracts\Support\Arrayable|array $data * @param array $mergeData * @return string */ public function renderWhen($condition, $view, $data = [], $mergeData = []) { if (! $condition) { return ''; } return $this->make($view, $this->parseData($data), $mergeData)->render(); } /** * Get the rendered content of the view based on the negation of a given condition. * * @param bool $condition * @param string $view * @param \Illuminate\Contracts\Support\Arrayable|array $data * @param array $mergeData * @return string */ public function renderUnless($condition, $view, $data = [], $mergeData = []) { return $this->renderWhen(! $condition, $view, $data, $mergeData); } /** * Get the rendered contents of a partial from a loop. * * @param string $view * @param array $data * @param string $iterator * @param string $empty * @return string */ public function renderEach($view, $data, $iterator, $empty = 'raw|') { $result = ''; // If is actually data in the array, we will loop through the data and append // an instance of the partial view to the final result HTML passing in the // iterated value of this data array, allowing the views to access them. if (count($data) > 0) { foreach ($data as $key => $value) { $result .= $this->make( $view, ['key' => $key, $iterator => $value] )->render(); } } // If there is no data in the array, we will render the contents of the empty // view. Alternatively, the "empty view" could be a raw string that begins // with "raw|" for convenience and to let this know that it is a string. else { $result = str_starts_with($empty, 'raw|') ? substr($empty, 4) : $this->make($empty)->render(); } return $result; } /** * Normalize a view name. * * @param string $name * @return string */ protected function normalizeName($name) { return ViewName::normalize($name); } /** * Parse the given data into a raw array. * * @param mixed $data * @return array */ protected function parseData($data) { return $data instanceof Arrayable ? $data->toArray() : $data; } /** * Create a new view instance from the given arguments. * * @param string $view * @param string $path * @param \Illuminate\Contracts\Support\Arrayable|array $data * @return \Illuminate\Contracts\View\View */ protected function viewInstance($view, $path, $data) { return new View($this, $this->getEngineFromPath($path), $view, $path, $data); } /** * Determine if a given view exists. * * @param string $view * @return bool */ public function exists($view) { try { $this->finder->find($view); } catch (InvalidArgumentException) { return false; } return true; } /** * Get the appropriate view engine for the given path. * * @param string $path * @return \Illuminate\Contracts\View\Engine * * @throws \InvalidArgumentException */ public function getEngineFromPath($path) { if (! $extension = $this->getExtension($path)) { throw new InvalidArgumentException("Unrecognized extension in file: {$path}."); } $engine = $this->extensions[$extension]; return $this->engines->resolve($engine); } /** * Get the extension used by the view file. * * @param string $path * @return string|null */ protected function getExtension($path) { $extensions = array_keys($this->extensions); return Arr::first($extensions, function ($value) use ($path) { return str_ends_with($path, '.'.$value); }); } /** * Add a piece of shared data to the environment. * * @param array|string $key * @param mixed|null $value * @return mixed */ public function share($key, $value = null) { $keys = is_array($key) ? $key : [$key => $value]; foreach ($keys as $key => $value) { $this->shared[$key] = $value; } return $value; } /** * Increment the rendering counter. * * @return void */ public function incrementRender() { $this->renderCount++; } /** * Decrement the rendering counter. * * @return void */ public function decrementRender() { $this->renderCount--; } /** * Check if there are no active render operations. * * @return bool */ public function doneRendering() { return $this->renderCount == 0; } /** * Determine if the given once token has been rendered. * * @param string $id * @return bool */ public function hasRenderedOnce(string $id) { return isset($this->renderedOnce[$id]); } /** * Mark the given once token as having been rendered. * * @param string $id * @return void */ public function markAsRenderedOnce(string $id) { $this->renderedOnce[$id] = true; } /** * Add a location to the array of view locations. * * @param string $location * @return void */ public function addLocation($location) { $this->finder->addLocation($location); } /** * Add a new namespace to the loader. * * @param string $namespace * @param string|array $hints * @return $this */ public function addNamespace($namespace, $hints) { $this->finder->addNamespace($namespace, $hints); return $this; } /** * Prepend a new namespace to the loader. * * @param string $namespace * @param string|array $hints * @return $this */ public function prependNamespace($namespace, $hints) { $this->finder->prependNamespace($namespace, $hints); return $this; } /** * Replace the namespace hints for the given namespace. * * @param string $namespace * @param string|array $hints * @return $this */ public function replaceNamespace($namespace, $hints) { $this->finder->replaceNamespace($namespace, $hints); return $this; } /** * Register a valid view extension and its engine. * * @param string $extension * @param string $engine * @param \Closure|null $resolver * @return void */ public function addExtension($extension, $engine, $resolver = null) { $this->finder->addExtension($extension); if (isset($resolver)) { $this->engines->register($engine, $resolver); } unset($this->extensions[$extension]); $this->extensions = array_merge([$extension => $engine], $this->extensions); } /** * Flush all of the factory state like sections and stacks. * * @return void */ public function flushState() { $this->renderCount = 0; $this->renderedOnce = []; $this->flushSections(); $this->flushStacks(); $this->flushComponents(); $this->flushFragments(); } /** * Flush all of the section contents if done rendering. * * @return void */ public function flushStateIfDoneRendering() { if ($this->doneRendering()) { $this->flushState(); } } /** * Get the extension to engine bindings. * * @return array */ public function getExtensions() { return $this->extensions; } /** * Get the engine resolver instance. * * @return \Illuminate\View\Engines\EngineResolver */ public function getEngineResolver() { return $this->engines; } /** * Get the view finder instance. * * @return \Illuminate\View\ViewFinderInterface */ public function getFinder() { return $this->finder; } /** * Set the view finder instance. * * @param \Illuminate\View\ViewFinderInterface $finder * @return void */ public function setFinder(ViewFinderInterface $finder) { $this->finder = $finder; } /** * Flush the cache of views located by the finder. * * @return void */ public function flushFinderCache() { $this->getFinder()->flush(); } /** * Get the event dispatcher instance. * * @return \Illuminate\Contracts\Events\Dispatcher */ public function getDispatcher() { return $this->events; } /** * Set the event dispatcher instance. * * @param \Illuminate\Contracts\Events\Dispatcher $events * @return void */ public function setDispatcher(Dispatcher $events) { $this->events = $events; } /** * Get the IoC container instance. * * @return \Illuminate\Contracts\Container\Container */ public function getContainer() { return $this->container; } /** * Set the IoC container instance. * * @param \Illuminate\Contracts\Container\Container $container * @return void */ public function setContainer(Container $container) { $this->container = $container; } /** * Get an item from the shared data. * * @param string $key * @param mixed $default * @return mixed */ public function shared($key, $default = null) { return Arr::get($this->shared, $key, $default); } /** * Get all of the shared data for the environment. * * @return array */ public function getShared() { return $this->shared; } } src/Illuminate/View/LICENSE.md 0000644 00000002063 15107327040 0011754 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/View/Middleware/ShareErrorsFromSession.php 0000644 00000002622 15107327040 0017566 0 ustar 00 <?php namespace Illuminate\View\Middleware; use Closure; use Illuminate\Contracts\View\Factory as ViewFactory; use Illuminate\Support\ViewErrorBag; class ShareErrorsFromSession { /** * The view factory implementation. * * @var \Illuminate\Contracts\View\Factory */ protected $view; /** * Create a new error binder instance. * * @param \Illuminate\Contracts\View\Factory $view * @return void */ public function __construct(ViewFactory $view) { $this->view = $view; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { // If the current session has an "errors" variable bound to it, we will share // its value with all view instances so the views can easily access errors // without having to bind. An empty bag is set when there aren't errors. $this->view->share( 'errors', $request->session()->get('errors') ?: new ViewErrorBag ); // Putting the errors in the view for every view allows the developer to just // assume that some errors are always available, which is convenient since // they don't have to continually run checks for the presence of errors. return $next($request); } } src/Illuminate/View/Compilers/BladeCompiler.php 0000644 00000064517 15107327040 0015534 0 ustar 00 <?php namespace Illuminate\View\Compilers; use Illuminate\Container\Container; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Contracts\View\Factory as ViewFactory; use Illuminate\Contracts\View\View; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Support\Traits\ReflectsClosures; use Illuminate\View\Component; use InvalidArgumentException; class BladeCompiler extends Compiler implements CompilerInterface { use Concerns\CompilesAuthorizations, Concerns\CompilesClasses, Concerns\CompilesComments, Concerns\CompilesComponents, Concerns\CompilesConditionals, Concerns\CompilesEchos, Concerns\CompilesErrors, Concerns\CompilesFragments, Concerns\CompilesHelpers, Concerns\CompilesIncludes, Concerns\CompilesInjections, Concerns\CompilesJson, Concerns\CompilesJs, Concerns\CompilesLayouts, Concerns\CompilesLoops, Concerns\CompilesRawPhp, Concerns\CompilesStacks, Concerns\CompilesStyles, Concerns\CompilesTranslations, ReflectsClosures; /** * All of the registered extensions. * * @var array */ protected $extensions = []; /** * All custom "directive" handlers. * * @var array */ protected $customDirectives = []; /** * All custom "condition" handlers. * * @var array */ protected $conditions = []; /** * The registered string preparation callbacks. * * @var array */ protected $prepareStringsForCompilationUsing = []; /** * All of the registered precompilers. * * @var array */ protected $precompilers = []; /** * The file currently being compiled. * * @var string */ protected $path; /** * All of the available compiler functions. * * @var string[] */ protected $compilers = [ // 'Comments', 'Extensions', 'Statements', 'Echos', ]; /** * Array of opening and closing tags for raw echos. * * @var string[] */ protected $rawTags = ['{!!', '!!}']; /** * Array of opening and closing tags for regular echos. * * @var string[] */ protected $contentTags = ['{{', '}}']; /** * Array of opening and closing tags for escaped echos. * * @var string[] */ protected $escapedTags = ['{{{', '}}}']; /** * The "regular" / legacy echo string format. * * @var string */ protected $echoFormat = 'e(%s)'; /** * Array of footer lines to be added to the template. * * @var array */ protected $footer = []; /** * Array to temporarily store the raw blocks found in the template. * * @var array */ protected $rawBlocks = []; /** * The array of anonymous component paths to search for components in. * * @var array */ protected $anonymousComponentPaths = []; /** * The array of anonymous component namespaces to autoload from. * * @var array */ protected $anonymousComponentNamespaces = []; /** * The array of class component aliases and their class names. * * @var array */ protected $classComponentAliases = []; /** * The array of class component namespaces to autoload from. * * @var array */ protected $classComponentNamespaces = []; /** * Indicates if component tags should be compiled. * * @var bool */ protected $compilesComponentTags = true; /** * Compile the view at the given path. * * @param string|null $path * @return void */ public function compile($path = null) { if ($path) { $this->setPath($path); } if (! is_null($this->cachePath)) { $contents = $this->compileString($this->files->get($this->getPath())); if (! empty($this->getPath())) { $contents = $this->appendFilePath($contents); } $this->ensureCompiledDirectoryExists( $compiledPath = $this->getCompiledPath($this->getPath()) ); $this->files->put($compiledPath, $contents); } } /** * Append the file path to the compiled string. * * @param string $contents * @return string */ protected function appendFilePath($contents) { $tokens = $this->getOpenAndClosingPhpTokens($contents); if ($tokens->isNotEmpty() && $tokens->last() !== T_CLOSE_TAG) { $contents .= ' ?>'; } return $contents."<?php /**PATH {$this->getPath()} ENDPATH**/ ?>"; } /** * Get the open and closing PHP tag tokens from the given string. * * @param string $contents * @return \Illuminate\Support\Collection */ protected function getOpenAndClosingPhpTokens($contents) { return collect(token_get_all($contents)) ->pluck(0) ->filter(function ($token) { return in_array($token, [T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, T_CLOSE_TAG]); }); } /** * Get the path currently being compiled. * * @return string */ public function getPath() { return $this->path; } /** * Set the path currently being compiled. * * @param string $path * @return void */ public function setPath($path) { $this->path = $path; } /** * Compile the given Blade template contents. * * @param string $value * @return string */ public function compileString($value) { [$this->footer, $result] = [[], '']; foreach ($this->prepareStringsForCompilationUsing as $callback) { $value = $callback($value); } $value = $this->storeUncompiledBlocks($value); // First we will compile the Blade component tags. This is a precompile style // step which compiles the component Blade tags into @component directives // that may be used by Blade. Then we should call any other precompilers. $value = $this->compileComponentTags( $this->compileComments($value) ); foreach ($this->precompilers as $precompiler) { $value = $precompiler($value); } // Here we will loop through all of the tokens returned by the Zend lexer and // parse each one into the corresponding valid PHP. We will then have this // template as the correctly rendered PHP that can be rendered natively. foreach (token_get_all($value) as $token) { $result .= is_array($token) ? $this->parseToken($token) : $token; } if (! empty($this->rawBlocks)) { $result = $this->restoreRawContent($result); } // If there are any footer lines that need to get added to a template we will // add them here at the end of the template. This gets used mainly for the // template inheritance via the extends keyword that should be appended. if (count($this->footer) > 0) { $result = $this->addFooters($result); } if (! empty($this->echoHandlers)) { $result = $this->addBladeCompilerVariable($result); } return str_replace( ['##BEGIN-COMPONENT-CLASS##', '##END-COMPONENT-CLASS##'], '', $result); } /** * Evaluate and render a Blade string to HTML. * * @param string $string * @param array $data * @param bool $deleteCachedView * @return string */ public static function render($string, $data = [], $deleteCachedView = false) { $component = new class($string) extends Component { protected $template; public function __construct($template) { $this->template = $template; } public function render() { return $this->template; } }; $view = Container::getInstance() ->make(ViewFactory::class) ->make($component->resolveView(), $data); return tap($view->render(), function () use ($view, $deleteCachedView) { if ($deleteCachedView) { unlink($view->getPath()); } }); } /** * Render a component instance to HTML. * * @param \Illuminate\View\Component $component * @return string */ public static function renderComponent(Component $component) { $data = $component->data(); $view = value($component->resolveView(), $data); if ($view instanceof View) { return $view->with($data)->render(); } elseif ($view instanceof Htmlable) { return $view->toHtml(); } else { return Container::getInstance() ->make(ViewFactory::class) ->make($view, $data) ->render(); } } /** * Store the blocks that do not receive compilation. * * @param string $value * @return string */ protected function storeUncompiledBlocks($value) { if (str_contains($value, '@verbatim')) { $value = $this->storeVerbatimBlocks($value); } if (str_contains($value, '@php')) { $value = $this->storePhpBlocks($value); } return $value; } /** * Store the verbatim blocks and replace them with a temporary placeholder. * * @param string $value * @return string */ protected function storeVerbatimBlocks($value) { return preg_replace_callback('/(?<!@)@verbatim(.*?)@endverbatim/s', function ($matches) { return $this->storeRawBlock($matches[1]); }, $value); } /** * Store the PHP blocks and replace them with a temporary placeholder. * * @param string $value * @return string */ protected function storePhpBlocks($value) { return preg_replace_callback('/(?<!@)@php(.*?)@endphp/s', function ($matches) { return $this->storeRawBlock("<?php{$matches[1]}?>"); }, $value); } /** * Store a raw block and return a unique raw placeholder. * * @param string $value * @return string */ protected function storeRawBlock($value) { return $this->getRawPlaceholder( array_push($this->rawBlocks, $value) - 1 ); } /** * Compile the component tags. * * @param string $value * @return string */ protected function compileComponentTags($value) { if (! $this->compilesComponentTags) { return $value; } return (new ComponentTagCompiler( $this->classComponentAliases, $this->classComponentNamespaces, $this ))->compile($value); } /** * Replace the raw placeholders with the original code stored in the raw blocks. * * @param string $result * @return string */ protected function restoreRawContent($result) { $result = preg_replace_callback('/'.$this->getRawPlaceholder('(\d+)').'/', function ($matches) { return $this->rawBlocks[$matches[1]]; }, $result); $this->rawBlocks = []; return $result; } /** * Get a placeholder to temporarily mark the position of raw blocks. * * @param int|string $replace * @return string */ protected function getRawPlaceholder($replace) { return str_replace('#', $replace, '@__raw_block_#__@'); } /** * Add the stored footers onto the given content. * * @param string $result * @return string */ protected function addFooters($result) { return ltrim($result, "\n") ."\n".implode("\n", array_reverse($this->footer)); } /** * Parse the tokens from the template. * * @param array $token * @return string */ protected function parseToken($token) { [$id, $content] = $token; if ($id == T_INLINE_HTML) { foreach ($this->compilers as $type) { $content = $this->{"compile{$type}"}($content); } } return $content; } /** * Execute the user defined extensions. * * @param string $value * @return string */ protected function compileExtensions($value) { foreach ($this->extensions as $compiler) { $value = $compiler($value, $this); } return $value; } /** * Compile Blade statements that start with "@". * * @param string $template * @return string */ protected function compileStatements($template) { preg_match_all('/\B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( [\S\s]*? ) \))?/x', $template, $matches); $offset = 0; for ($i = 0; isset($matches[0][$i]); $i++) { $match = [ $matches[0][$i], $matches[1][$i], $matches[2][$i], $matches[3][$i] ?: null, $matches[4][$i] ?: null, ]; // Here we check to see if we have properly found the closing parenthesis by // regex pattern or not, and will recursively continue on to the next ")" // then check again until the tokenizer confirms we find the right one. while (isset($match[4]) && Str::endsWith($match[0], ')') && ! $this->hasEvenNumberOfParentheses($match[0])) { if (($after = Str::after($template, $match[0])) === $template) { break; } $rest = Str::before($after, ')'); if (isset($matches[0][$i + 1]) && Str::contains($rest.')', $matches[0][$i + 1])) { unset($matches[0][$i + 1]); $i++; } $match[0] = $match[0].$rest.')'; $match[3] = $match[3].$rest.')'; $match[4] = $match[4].$rest; } [$template, $offset] = $this->replaceFirstStatement( $match[0], $this->compileStatement($match), $template, $offset ); } return $template; } /** * Replace the first match for a statement compilation operation. * * @param string $search * @param string $replace * @param string $subject * @param int $offset * @return array */ protected function replaceFirstStatement($search, $replace, $subject, $offset) { $search = (string) $search; if ($search === '') { return $subject; } $position = strpos($subject, $search, $offset); if ($position !== false) { return [ substr_replace($subject, $replace, $position, strlen($search)), $position + strlen($replace), ]; } return [$subject, 0]; } /** * Determine if the given expression has the same number of opening and closing parentheses. * * @param string $expression * @return bool */ protected function hasEvenNumberOfParentheses(string $expression) { $tokens = token_get_all('<?php '.$expression); if (Arr::last($tokens) !== ')') { return false; } $opening = 0; $closing = 0; foreach ($tokens as $token) { if ($token == ')') { $closing++; } elseif ($token == '(') { $opening++; } } return $opening === $closing; } /** * Compile a single Blade @ statement. * * @param array $match * @return string */ protected function compileStatement($match) { if (str_contains($match[1], '@')) { $match[0] = isset($match[3]) ? $match[1].$match[3] : $match[1]; } elseif (isset($this->customDirectives[$match[1]])) { $match[0] = $this->callCustomDirective($match[1], Arr::get($match, 3)); } elseif (method_exists($this, $method = 'compile'.ucfirst($match[1]))) { $match[0] = $this->$method(Arr::get($match, 3)); } else { return $match[0]; } return isset($match[3]) ? $match[0] : $match[0].$match[2]; } /** * Call the given directive with the given value. * * @param string $name * @param string|null $value * @return string */ protected function callCustomDirective($name, $value) { $value ??= ''; if (str_starts_with($value, '(') && str_ends_with($value, ')')) { $value = Str::substr($value, 1, -1); } return call_user_func($this->customDirectives[$name], trim($value)); } /** * Strip the parentheses from the given expression. * * @param string $expression * @return string */ public function stripParentheses($expression) { if (Str::startsWith($expression, '(')) { $expression = substr($expression, 1, -1); } return $expression; } /** * Register a custom Blade compiler. * * @param callable $compiler * @return void */ public function extend(callable $compiler) { $this->extensions[] = $compiler; } /** * Get the extensions used by the compiler. * * @return array */ public function getExtensions() { return $this->extensions; } /** * Register an "if" statement directive. * * @param string $name * @param callable $callback * @return void */ public function if($name, callable $callback) { $this->conditions[$name] = $callback; $this->directive($name, function ($expression) use ($name) { return $expression !== '' ? "<?php if (\Illuminate\Support\Facades\Blade::check('{$name}', {$expression})): ?>" : "<?php if (\Illuminate\Support\Facades\Blade::check('{$name}')): ?>"; }); $this->directive('unless'.$name, function ($expression) use ($name) { return $expression !== '' ? "<?php if (! \Illuminate\Support\Facades\Blade::check('{$name}', {$expression})): ?>" : "<?php if (! \Illuminate\Support\Facades\Blade::check('{$name}')): ?>"; }); $this->directive('else'.$name, function ($expression) use ($name) { return $expression !== '' ? "<?php elseif (\Illuminate\Support\Facades\Blade::check('{$name}', {$expression})): ?>" : "<?php elseif (\Illuminate\Support\Facades\Blade::check('{$name}')): ?>"; }); $this->directive('end'.$name, function () { return '<?php endif; ?>'; }); } /** * Check the result of a condition. * * @param string $name * @param mixed ...$parameters * @return bool */ public function check($name, ...$parameters) { return call_user_func($this->conditions[$name], ...$parameters); } /** * Register a class-based component alias directive. * * @param string $class * @param string|null $alias * @param string $prefix * @return void */ public function component($class, $alias = null, $prefix = '') { if (! is_null($alias) && str_contains($alias, '\\')) { [$class, $alias] = [$alias, $class]; } if (is_null($alias)) { $alias = str_contains($class, '\\View\\Components\\') ? collect(explode('\\', Str::after($class, '\\View\\Components\\')))->map(function ($segment) { return Str::kebab($segment); })->implode(':') : Str::kebab(class_basename($class)); } if (! empty($prefix)) { $alias = $prefix.'-'.$alias; } $this->classComponentAliases[$alias] = $class; } /** * Register an array of class-based components. * * @param array $components * @param string $prefix * @return void */ public function components(array $components, $prefix = '') { foreach ($components as $key => $value) { if (is_numeric($key)) { $this->component($value, null, $prefix); } else { $this->component($key, $value, $prefix); } } } /** * Get the registered class component aliases. * * @return array */ public function getClassComponentAliases() { return $this->classComponentAliases; } /** * Register a new anonymous component path. * * @param string $path * @param string|null $prefix * @return void */ public function anonymousComponentPath(string $path, string $prefix = null) { $prefixHash = md5($prefix ?: $path); $this->anonymousComponentPaths[] = [ 'path' => $path, 'prefix' => $prefix, 'prefixHash' => $prefixHash, ]; Container::getInstance() ->make(ViewFactory::class) ->addNamespace($prefixHash, $path); } /** * Register an anonymous component namespace. * * @param string $directory * @param string|null $prefix * @return void */ public function anonymousComponentNamespace(string $directory, string $prefix = null) { $prefix ??= $directory; $this->anonymousComponentNamespaces[$prefix] = Str::of($directory) ->replace('/', '.') ->trim('. ') ->toString(); } /** * Register a class-based component namespace. * * @param string $namespace * @param string $prefix * @return void */ public function componentNamespace($namespace, $prefix) { $this->classComponentNamespaces[$prefix] = $namespace; } /** * Get the registered anonymous component paths. * * @return array */ public function getAnonymousComponentPaths() { return $this->anonymousComponentPaths; } /** * Get the registered anonymous component namespaces. * * @return array */ public function getAnonymousComponentNamespaces() { return $this->anonymousComponentNamespaces; } /** * Get the registered class component namespaces. * * @return array */ public function getClassComponentNamespaces() { return $this->classComponentNamespaces; } /** * Register a component alias directive. * * @param string $path * @param string|null $alias * @return void */ public function aliasComponent($path, $alias = null) { $alias = $alias ?: Arr::last(explode('.', $path)); $this->directive($alias, function ($expression) use ($path) { return $expression ? "<?php \$__env->startComponent('{$path}', {$expression}); ?>" : "<?php \$__env->startComponent('{$path}'); ?>"; }); $this->directive('end'.$alias, function ($expression) { return '<?php echo $__env->renderComponent(); ?>'; }); } /** * Register an include alias directive. * * @param string $path * @param string|null $alias * @return void */ public function include($path, $alias = null) { $this->aliasInclude($path, $alias); } /** * Register an include alias directive. * * @param string $path * @param string|null $alias * @return void */ public function aliasInclude($path, $alias = null) { $alias = $alias ?: Arr::last(explode('.', $path)); $this->directive($alias, function ($expression) use ($path) { $expression = $this->stripParentheses($expression) ?: '[]'; return "<?php echo \$__env->make('{$path}', {$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>"; }); } /** * Register a handler for custom directives. * * @param string $name * @param callable $handler * @return void * * @throws \InvalidArgumentException */ public function directive($name, callable $handler) { if (! preg_match('/^\w+(?:::\w+)?$/x', $name)) { throw new InvalidArgumentException("The directive name [{$name}] is not valid. Directive names must only contain alphanumeric characters and underscores."); } $this->customDirectives[$name] = $handler; } /** * Get the list of custom directives. * * @return array */ public function getCustomDirectives() { return $this->customDirectives; } /** * Indicate that the following callable should be used to prepare strings for compilation. * * @param callable $callback * @return $this */ public function prepareStringsForCompilationUsing(callable $callback) { $this->prepareStringsForCompilationUsing[] = $callback; return $this; } /** * Register a new precompiler. * * @param callable $precompiler * @return void */ public function precompiler(callable $precompiler) { $this->precompilers[] = $precompiler; } /** * Set the echo format to be used by the compiler. * * @param string $format * @return void */ public function setEchoFormat($format) { $this->echoFormat = $format; } /** * Set the "echo" format to double encode entities. * * @return void */ public function withDoubleEncoding() { $this->setEchoFormat('e(%s, true)'); } /** * Set the "echo" format to not double encode entities. * * @return void */ public function withoutDoubleEncoding() { $this->setEchoFormat('e(%s, false)'); } /** * Indicate that component tags should not be compiled. * * @return void */ public function withoutComponentTags() { $this->compilesComponentTags = false; } } src/Illuminate/View/Compilers/Concerns/CompilesLayouts.php 0000644 00000006013 15107327040 0017723 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; trait CompilesLayouts { /** * The name of the last section that was started. * * @var string */ protected $lastSection; /** * Compile the extends statements into valid PHP. * * @param string $expression * @return string */ protected function compileExtends($expression) { $expression = $this->stripParentheses($expression); $echo = "<?php echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>"; $this->footer[] = $echo; return ''; } /** * Compile the extends-first statements into valid PHP. * * @param string $expression * @return string */ protected function compileExtendsFirst($expression) { $expression = $this->stripParentheses($expression); $echo = "<?php echo \$__env->first({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>"; $this->footer[] = $echo; return ''; } /** * Compile the section statements into valid PHP. * * @param string $expression * @return string */ protected function compileSection($expression) { $this->lastSection = trim($expression, "()'\" "); return "<?php \$__env->startSection{$expression}; ?>"; } /** * Replace the @parent directive to a placeholder. * * @return string */ protected function compileParent() { $escapedLastSection = strtr($this->lastSection, ['\\' => '\\\\', "'" => "\\'"]); return "<?php echo \Illuminate\View\Factory::parentPlaceholder('{$escapedLastSection}'); ?>"; } /** * Compile the yield statements into valid PHP. * * @param string $expression * @return string */ protected function compileYield($expression) { return "<?php echo \$__env->yieldContent{$expression}; ?>"; } /** * Compile the show statements into valid PHP. * * @return string */ protected function compileShow() { return '<?php echo $__env->yieldSection(); ?>'; } /** * Compile the append statements into valid PHP. * * @return string */ protected function compileAppend() { return '<?php $__env->appendSection(); ?>'; } /** * Compile the overwrite statements into valid PHP. * * @return string */ protected function compileOverwrite() { return '<?php $__env->stopSection(true); ?>'; } /** * Compile the stop statements into valid PHP. * * @return string */ protected function compileStop() { return '<?php $__env->stopSection(); ?>'; } /** * Compile the end-section statements into valid PHP. * * @return string */ protected function compileEndsection() { return '<?php $__env->stopSection(); ?>'; } } src/Illuminate/View/Compilers/Concerns/CompilesInjections.php 0000644 00000000771 15107327040 0020375 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; trait CompilesInjections { /** * Compile the inject statements into valid PHP. * * @param string $expression * @return string */ protected function compileInject($expression) { $segments = explode(',', preg_replace("/[\(\)]/", '', $expression)); $variable = trim($segments[0], " '\""); $service = trim($segments[1]); return "<?php \${$variable} = app({$service}); ?>"; } } src/Illuminate/View/Compilers/Concerns/CompilesClasses.php 0000644 00000000676 15107327040 0017671 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; trait CompilesClasses { /** * Compile the conditional class statement into valid PHP. * * @param string $expression * @return string */ protected function compileClass($expression) { $expression = is_null($expression) ? '([])' : $expression; return "class=\"<?php echo \Illuminate\Support\Arr::toCssClasses{$expression}; ?>\""; } } src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php 0000644 00000004601 15107327040 0020032 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; trait CompilesIncludes { /** * Compile the each statements into valid PHP. * * @param string $expression * @return string */ protected function compileEach($expression) { return "<?php echo \$__env->renderEach{$expression}; ?>"; } /** * Compile the include statements into valid PHP. * * @param string $expression * @return string */ protected function compileInclude($expression) { $expression = $this->stripParentheses($expression); return "<?php echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>"; } /** * Compile the include-if statements into valid PHP. * * @param string $expression * @return string */ protected function compileIncludeIf($expression) { $expression = $this->stripParentheses($expression); return "<?php if (\$__env->exists({$expression})) echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>"; } /** * Compile the include-when statements into valid PHP. * * @param string $expression * @return string */ protected function compileIncludeWhen($expression) { $expression = $this->stripParentheses($expression); return "<?php echo \$__env->renderWhen($expression, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path'])); ?>"; } /** * Compile the include-unless statements into valid PHP. * * @param string $expression * @return string */ protected function compileIncludeUnless($expression) { $expression = $this->stripParentheses($expression); return "<?php echo \$__env->renderUnless($expression, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path'])); ?>"; } /** * Compile the include-first statements into valid PHP. * * @param string $expression * @return string */ protected function compileIncludeFirst($expression) { $expression = $this->stripParentheses($expression); return "<?php echo \$__env->first({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>"; } } src/Illuminate/View/Compilers/Concerns/CompilesStyles.php 0000644 00000000673 15107327040 0017554 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; trait CompilesStyles { /** * Compile the conditional style statement into valid PHP. * * @param string $expression * @return string */ protected function compileStyle($expression) { $expression = is_null($expression) ? '([])' : $expression; return "style=\"<?php echo \Illuminate\Support\Arr::toCssStyles{$expression} ?>\""; } } src/Illuminate/View/Compilers/Concerns/CompilesErrors.php 0000644 00000001645 15107327040 0017545 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; trait CompilesErrors { /** * Compile the error statements into valid PHP. * * @param string $expression * @return string */ protected function compileError($expression) { $expression = $this->stripParentheses($expression); return '<?php $__errorArgs = ['.$expression.']; $__bag = $errors->getBag($__errorArgs[1] ?? \'default\'); if ($__bag->has($__errorArgs[0])) : if (isset($message)) { $__messageOriginal = $message; } $message = $__bag->first($__errorArgs[0]); ?>'; } /** * Compile the enderror statements into valid PHP. * * @param string $expression * @return string */ protected function compileEnderror($expression) { return '<?php unset($message); if (isset($__messageOriginal)) { $message = $__messageOriginal; } endif; unset($__errorArgs, $__bag); ?>'; } } src/Illuminate/View/Compilers/Concerns/CompilesAuthorizations.php 0000644 00000004706 15107327040 0021315 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; trait CompilesAuthorizations { /** * Compile the can statements into valid PHP. * * @param string $expression * @return string */ protected function compileCan($expression) { return "<?php if (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->check{$expression}): ?>"; } /** * Compile the cannot statements into valid PHP. * * @param string $expression * @return string */ protected function compileCannot($expression) { return "<?php if (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->denies{$expression}): ?>"; } /** * Compile the canany statements into valid PHP. * * @param string $expression * @return string */ protected function compileCanany($expression) { return "<?php if (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->any{$expression}): ?>"; } /** * Compile the else-can statements into valid PHP. * * @param string $expression * @return string */ protected function compileElsecan($expression) { return "<?php elseif (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->check{$expression}): ?>"; } /** * Compile the else-cannot statements into valid PHP. * * @param string $expression * @return string */ protected function compileElsecannot($expression) { return "<?php elseif (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->denies{$expression}): ?>"; } /** * Compile the else-canany statements into valid PHP. * * @param string $expression * @return string */ protected function compileElsecanany($expression) { return "<?php elseif (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->any{$expression}): ?>"; } /** * Compile the end-can statements into valid PHP. * * @return string */ protected function compileEndcan() { return '<?php endif; ?>'; } /** * Compile the end-cannot statements into valid PHP. * * @return string */ protected function compileEndcannot() { return '<?php endif; ?>'; } /** * Compile the end-canany statements into valid PHP. * * @return string */ protected function compileEndcanany() { return '<?php endif; ?>'; } } src/Illuminate/View/Compilers/Concerns/CompilesComponents.php 0000644 00000013436 15107327040 0020417 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString; use Illuminate\Support\Str; use Illuminate\View\ComponentAttributeBag; trait CompilesComponents { /** * The component name hash stack. * * @var array */ protected static $componentHashStack = []; /** * Compile the component statements into valid PHP. * * @param string $expression * @return string */ protected function compileComponent($expression) { [$component, $alias, $data] = str_contains($expression, ',') ? array_map('trim', explode(',', trim($expression, '()'), 3)) + ['', '', ''] : [trim($expression, '()'), '', '']; $component = trim($component, '\'"'); $hash = static::newComponentHash($component); if (Str::contains($component, ['::class', '\\'])) { return static::compileClassComponentOpening($component, $alias, $data, $hash); } return "<?php \$__env->startComponent{$expression}; ?>"; } /** * Get a new component hash for a component name. * * @param string $component * @return string */ public static function newComponentHash(string $component) { static::$componentHashStack[] = $hash = hash('xxh128', $component); return $hash; } /** * Compile a class component opening. * * @param string $component * @param string $alias * @param string $data * @param string $hash * @return string */ public static function compileClassComponentOpening(string $component, string $alias, string $data, string $hash) { return implode("\n", [ '<?php if (isset($component)) { $__componentOriginal'.$hash.' = $component; } ?>', '<?php $component = '.$component.'::resolve('.($data ?: '[]').' + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? (array) $attributes->getIterator() : [])); ?>', '<?php $component->withName('.$alias.'); ?>', '<?php if ($component->shouldRender()): ?>', '<?php $__env->startComponent($component->resolveView(), $component->data()); ?>', ]); } /** * Compile the end-component statements into valid PHP. * * @return string */ protected function compileEndComponent() { return '<?php echo $__env->renderComponent(); ?>'; } /** * Compile the end-component statements into valid PHP. * * @return string */ public function compileEndComponentClass() { $hash = array_pop(static::$componentHashStack); return $this->compileEndComponent()."\n".implode("\n", [ '<?php endif; ?>', '<?php if (isset($__componentOriginal'.$hash.')): ?>', '<?php $component = $__componentOriginal'.$hash.'; ?>', '<?php unset($__componentOriginal'.$hash.'); ?>', '<?php endif; ?>', ]); } /** * Compile the slot statements into valid PHP. * * @param string $expression * @return string */ protected function compileSlot($expression) { return "<?php \$__env->slot{$expression}; ?>"; } /** * Compile the end-slot statements into valid PHP. * * @return string */ protected function compileEndSlot() { return '<?php $__env->endSlot(); ?>'; } /** * Compile the component-first statements into valid PHP. * * @param string $expression * @return string */ protected function compileComponentFirst($expression) { return "<?php \$__env->startComponentFirst{$expression}; ?>"; } /** * Compile the end-component-first statements into valid PHP. * * @return string */ protected function compileEndComponentFirst() { return $this->compileEndComponent(); } /** * Compile the prop statement into valid PHP. * * @param string $expression * @return string */ protected function compileProps($expression) { return "<?php \$attributes ??= new \\Illuminate\\View\\ComponentAttributeBag; ?> <?php foreach(\$attributes->onlyProps{$expression} as \$__key => \$__value) { \$\$__key = \$\$__key ?? \$__value; } ?> <?php \$attributes = \$attributes->exceptProps{$expression}; ?> <?php foreach (array_filter({$expression}, 'is_string', ARRAY_FILTER_USE_KEY) as \$__key => \$__value) { \$\$__key = \$\$__key ?? \$__value; } ?> <?php \$__defined_vars = get_defined_vars(); ?> <?php foreach (\$attributes as \$__key => \$__value) { if (array_key_exists(\$__key, \$__defined_vars)) unset(\$\$__key); } ?> <?php unset(\$__defined_vars); ?>"; } /** * Compile the aware statement into valid PHP. * * @param string $expression * @return string */ protected function compileAware($expression) { return "<?php foreach ({$expression} as \$__key => \$__value) { \$__consumeVariable = is_string(\$__key) ? \$__key : \$__value; \$\$__consumeVariable = is_string(\$__key) ? \$__env->getConsumableComponentData(\$__key, \$__value) : \$__env->getConsumableComponentData(\$__value); } ?>"; } /** * Sanitize the given component attribute value. * * @param mixed $value * @return mixed */ public static function sanitizeComponentAttribute($value) { if ($value instanceof CanBeEscapedWhenCastToString) { return $value->escapeWhenCastingToString(); } return is_string($value) || (is_object($value) && ! $value instanceof ComponentAttributeBag && method_exists($value, '__toString')) ? e($value) : $value; } } src/Illuminate/View/Compilers/Concerns/CompilesRawPhp.php 0000644 00000001154 15107327040 0017465 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; trait CompilesRawPhp { /** * Compile the raw PHP statements into valid PHP. * * @param string $expression * @return string */ protected function compilePhp($expression) { if ($expression) { return "<?php {$expression}; ?>"; } return '@php'; } /** * Compile the unset statements into valid PHP. * * @param string $expression * @return string */ protected function compileUnset($expression) { return "<?php unset{$expression}; ?>"; } } src/Illuminate/View/Compilers/Concerns/CompilesFragments.php 0000644 00000001331 15107327040 0020207 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; trait CompilesFragments { /** * The last compiled fragment. * * @var string */ protected $lastFragment; /** * Compile the fragment statements into valid PHP. * * @param string $expression * @return string */ protected function compileFragment($expression) { $this->lastFragment = trim($expression, "()'\" "); return "<?php \$__env->startFragment{$expression}; ?>"; } /** * Compile the end-fragment statements into valid PHP. * * @return string */ protected function compileEndfragment() { return '<?php echo $__env->stopFragment(); ?>'; } } src/Illuminate/View/Compilers/Concerns/CompilesHelpers.php 0000644 00000003076 15107327040 0017673 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; use Illuminate\Foundation\Vite; trait CompilesHelpers { /** * Compile the CSRF statements into valid PHP. * * @return string */ protected function compileCsrf() { return '<?php echo csrf_field(); ?>'; } /** * Compile the "dd" statements into valid PHP. * * @param string $arguments * @return string */ protected function compileDd($arguments) { return "<?php dd{$arguments}; ?>"; } /** * Compile the "dump" statements into valid PHP. * * @param string $arguments * @return string */ protected function compileDump($arguments) { return "<?php dump{$arguments}; ?>"; } /** * Compile the method statements into valid PHP. * * @param string $method * @return string */ protected function compileMethod($method) { return "<?php echo method_field{$method}; ?>"; } /** * Compile the "vite" statements into valid PHP. * * @param string|null $arguments * @return string */ protected function compileVite($arguments) { $arguments ??= '()'; $class = Vite::class; return "<?php echo app('$class'){$arguments}; ?>"; } /** * Compile the "viteReactRefresh" statements into valid PHP. * * @return string */ protected function compileViteReactRefresh() { $class = Vite::class; return "<?php echo app('$class')->reactRefresh(); ?>"; } } src/Illuminate/View/Compilers/Concerns/CompilesStacks.php 0000644 00000005351 15107327040 0017517 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; use Illuminate\Support\Str; trait CompilesStacks { /** * Compile the stack statements into the content. * * @param string $expression * @return string */ protected function compileStack($expression) { return "<?php echo \$__env->yieldPushContent{$expression}; ?>"; } /** * Compile the push statements into valid PHP. * * @param string $expression * @return string */ protected function compilePush($expression) { return "<?php \$__env->startPush{$expression}; ?>"; } /** * Compile the push-once statements into valid PHP. * * @param string $expression * @return string */ protected function compilePushOnce($expression) { $parts = explode(',', $this->stripParentheses($expression), 2); [$stack, $id] = [$parts[0], $parts[1] ?? '']; $id = trim($id) ?: "'".(string) Str::uuid()."'"; return '<?php if (! $__env->hasRenderedOnce('.$id.')): $__env->markAsRenderedOnce('.$id.'); $__env->startPush('.$stack.'); ?>'; } /** * Compile the end-push statements into valid PHP. * * @return string */ protected function compileEndpush() { return '<?php $__env->stopPush(); ?>'; } /** * Compile the end-push-once statements into valid PHP. * * @return string */ protected function compileEndpushOnce() { return '<?php $__env->stopPush(); endif; ?>'; } /** * Compile the prepend statements into valid PHP. * * @param string $expression * @return string */ protected function compilePrepend($expression) { return "<?php \$__env->startPrepend{$expression}; ?>"; } /** * Compile the prepend-once statements into valid PHP. * * @param string $expression * @return string */ protected function compilePrependOnce($expression) { $parts = explode(',', $this->stripParentheses($expression), 2); [$stack, $id] = [$parts[0], $parts[1] ?? '']; $id = trim($id) ?: "'".(string) Str::uuid()."'"; return '<?php if (! $__env->hasRenderedOnce('.$id.')): $__env->markAsRenderedOnce('.$id.'); $__env->startPrepend('.$stack.'); ?>'; } /** * Compile the end-prepend statements into valid PHP. * * @return string */ protected function compileEndprepend() { return '<?php $__env->stopPrepend(); ?>'; } /** * Compile the end-prepend-once statements into valid PHP. * * @return string */ protected function compileEndprependOnce() { return '<?php $__env->stopPrepend(); endif; ?>'; } } src/Illuminate/View/Compilers/Concerns/CompilesJs.php 0000644 00000000677 15107327040 0016651 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; use Illuminate\Support\Js; trait CompilesJs { /** * Compile the "@js" directive into valid PHP. * * @param string $expression * @return string */ protected function compileJs(string $expression) { return sprintf( "<?php echo \%s::from(%s)->toHtml() ?>", Js::class, $this->stripParentheses($expression) ); } } src/Illuminate/View/Compilers/Concerns/CompilesTranslations.php 0000644 00000002035 15107327040 0020744 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; trait CompilesTranslations { /** * Compile the lang statements into valid PHP. * * @param string|null $expression * @return string */ protected function compileLang($expression) { if (is_null($expression)) { return '<?php $__env->startTranslation(); ?>'; } elseif ($expression[1] === '[') { return "<?php \$__env->startTranslation{$expression}; ?>"; } return "<?php echo app('translator')->get{$expression}; ?>"; } /** * Compile the end-lang statements into valid PHP. * * @return string */ protected function compileEndlang() { return '<?php echo $__env->renderTranslation(); ?>'; } /** * Compile the choice statements into valid PHP. * * @param string $expression * @return string */ protected function compileChoice($expression) { return "<?php echo app('translator')->choice{$expression}; ?>"; } } src/Illuminate/View/Compilers/Concerns/CompilesEchos.php 0000644 00000010534 15107327040 0017327 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; use Closure; use Illuminate\Support\Str; trait CompilesEchos { /** * Custom rendering callbacks for stringable objects. * * @var array */ protected $echoHandlers = []; /** * Add a handler to be executed before echoing a given class. * * @param string|callable $class * @param callable|null $handler * @return void */ public function stringable($class, $handler = null) { if ($class instanceof Closure) { [$class, $handler] = [$this->firstClosureParameterType($class), $class]; } $this->echoHandlers[$class] = $handler; } /** * Compile Blade echos into valid PHP. * * @param string $value * @return string */ public function compileEchos($value) { foreach ($this->getEchoMethods() as $method) { $value = $this->$method($value); } return $value; } /** * Get the echo methods in the proper order for compilation. * * @return array */ protected function getEchoMethods() { return [ 'compileRawEchos', 'compileEscapedEchos', 'compileRegularEchos', ]; } /** * Compile the "raw" echo statements. * * @param string $value * @return string */ protected function compileRawEchos($value) { $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->rawTags[0], $this->rawTags[1]); $callback = function ($matches) { $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3]; return $matches[1] ? substr($matches[0], 1) : "<?php echo {$this->wrapInEchoHandler($matches[2])}; ?>{$whitespace}"; }; return preg_replace_callback($pattern, $callback, $value); } /** * Compile the "regular" echo statements. * * @param string $value * @return string */ protected function compileRegularEchos($value) { $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->contentTags[0], $this->contentTags[1]); $callback = function ($matches) { $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3]; $wrapped = sprintf($this->echoFormat, $this->wrapInEchoHandler($matches[2])); return $matches[1] ? substr($matches[0], 1) : "<?php echo {$wrapped}; ?>{$whitespace}"; }; return preg_replace_callback($pattern, $callback, $value); } /** * Compile the escaped echo statements. * * @param string $value * @return string */ protected function compileEscapedEchos($value) { $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->escapedTags[0], $this->escapedTags[1]); $callback = function ($matches) { $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3]; return $matches[1] ? $matches[0] : "<?php echo e({$this->wrapInEchoHandler($matches[2])}); ?>{$whitespace}"; }; return preg_replace_callback($pattern, $callback, $value); } /** * Add an instance of the blade echo handler to the start of the compiled string. * * @param string $result * @return string */ protected function addBladeCompilerVariable($result) { return "<?php \$__bladeCompiler = app('blade.compiler'); ?>".$result; } /** * Wrap the echoable value in an echo handler if applicable. * * @param string $value * @return string */ protected function wrapInEchoHandler($value) { $value = Str::of($value) ->trim() ->when(str_ends_with($value, ';'), function ($str) { return $str->beforeLast(';'); }); return empty($this->echoHandlers) ? $value : '$__bladeCompiler->applyEchoHandler('.$value.')'; } /** * Apply the echo handler for the value if it exists. * * @param string $value * @return string */ public function applyEchoHandler($value) { if (is_object($value) && isset($this->echoHandlers[get_class($value)])) { return call_user_func($this->echoHandlers[get_class($value)], $value); } return $value; } } src/Illuminate/View/Compilers/Concerns/CompilesJson.php 0000644 00000001325 15107327040 0017175 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; trait CompilesJson { /** * The default JSON encoding options. * * @var int */ private $encodingOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT; /** * Compile the JSON statement into valid PHP. * * @param string $expression * @return string */ protected function compileJson($expression) { $parts = explode(',', $this->stripParentheses($expression)); $options = isset($parts[1]) ? trim($parts[1]) : $this->encodingOptions; $depth = isset($parts[2]) ? trim($parts[2]) : 512; return "<?php echo json_encode($parts[0], $options, $depth) ?>"; } } src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php 0000644 00000020477 15107327040 0020723 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; use Illuminate\Support\Str; trait CompilesConditionals { /** * Identifier for the first case in the switch statement. * * @var bool */ protected $firstCaseInSwitch = true; /** * Compile the if-auth statements into valid PHP. * * @param string|null $guard * @return string */ protected function compileAuth($guard = null) { $guard = is_null($guard) ? '()' : $guard; return "<?php if(auth()->guard{$guard}->check()): ?>"; } /** * Compile the else-auth statements into valid PHP. * * @param string|null $guard * @return string */ protected function compileElseAuth($guard = null) { $guard = is_null($guard) ? '()' : $guard; return "<?php elseif(auth()->guard{$guard}->check()): ?>"; } /** * Compile the end-auth statements into valid PHP. * * @return string */ protected function compileEndAuth() { return '<?php endif; ?>'; } /** * Compile the env statements into valid PHP. * * @param string $environments * @return string */ protected function compileEnv($environments) { return "<?php if(app()->environment{$environments}): ?>"; } /** * Compile the end-env statements into valid PHP. * * @return string */ protected function compileEndEnv() { return '<?php endif; ?>'; } /** * Compile the production statements into valid PHP. * * @return string */ protected function compileProduction() { return "<?php if(app()->environment('production')): ?>"; } /** * Compile the end-production statements into valid PHP. * * @return string */ protected function compileEndProduction() { return '<?php endif; ?>'; } /** * Compile the if-guest statements into valid PHP. * * @param string|null $guard * @return string */ protected function compileGuest($guard = null) { $guard = is_null($guard) ? '()' : $guard; return "<?php if(auth()->guard{$guard}->guest()): ?>"; } /** * Compile the else-guest statements into valid PHP. * * @param string|null $guard * @return string */ protected function compileElseGuest($guard = null) { $guard = is_null($guard) ? '()' : $guard; return "<?php elseif(auth()->guard{$guard}->guest()): ?>"; } /** * Compile the end-guest statements into valid PHP. * * @return string */ protected function compileEndGuest() { return '<?php endif; ?>'; } /** * Compile the has-section statements into valid PHP. * * @param string $expression * @return string */ protected function compileHasSection($expression) { return "<?php if (! empty(trim(\$__env->yieldContent{$expression}))): ?>"; } /** * Compile the section-missing statements into valid PHP. * * @param string $expression * @return string */ protected function compileSectionMissing($expression) { return "<?php if (empty(trim(\$__env->yieldContent{$expression}))): ?>"; } /** * Compile the if statements into valid PHP. * * @param string $expression * @return string */ protected function compileIf($expression) { return "<?php if{$expression}: ?>"; } /** * Compile the unless statements into valid PHP. * * @param string $expression * @return string */ protected function compileUnless($expression) { return "<?php if (! {$expression}): ?>"; } /** * Compile the else-if statements into valid PHP. * * @param string $expression * @return string */ protected function compileElseif($expression) { return "<?php elseif{$expression}: ?>"; } /** * Compile the else statements into valid PHP. * * @return string */ protected function compileElse() { return '<?php else: ?>'; } /** * Compile the end-if statements into valid PHP. * * @return string */ protected function compileEndif() { return '<?php endif; ?>'; } /** * Compile the end-unless statements into valid PHP. * * @return string */ protected function compileEndunless() { return '<?php endif; ?>'; } /** * Compile the if-isset statements into valid PHP. * * @param string $expression * @return string */ protected function compileIsset($expression) { return "<?php if(isset{$expression}): ?>"; } /** * Compile the end-isset statements into valid PHP. * * @return string */ protected function compileEndIsset() { return '<?php endif; ?>'; } /** * Compile the switch statements into valid PHP. * * @param string $expression * @return string */ protected function compileSwitch($expression) { $this->firstCaseInSwitch = true; return "<?php switch{$expression}:"; } /** * Compile the case statements into valid PHP. * * @param string $expression * @return string */ protected function compileCase($expression) { if ($this->firstCaseInSwitch) { $this->firstCaseInSwitch = false; return "case {$expression}: ?>"; } return "<?php case {$expression}: ?>"; } /** * Compile the default statements in switch case into valid PHP. * * @return string */ protected function compileDefault() { return '<?php default: ?>'; } /** * Compile the end switch statements into valid PHP. * * @return string */ protected function compileEndSwitch() { return '<?php endswitch; ?>'; } /** * Compile a once block into valid PHP. * * @param string|null $id * @return string */ protected function compileOnce($id = null) { $id = $id ? $this->stripParentheses($id) : "'".(string) Str::uuid()."'"; return '<?php if (! $__env->hasRenderedOnce('.$id.')): $__env->markAsRenderedOnce('.$id.'); ?>'; } /** * Compile an end-once block into valid PHP. * * @return string */ public function compileEndOnce() { return '<?php endif; ?>'; } /** * Compile a selected block into valid PHP. * * @param string $condition * @return string */ protected function compileSelected($condition) { return "<?php if{$condition}: echo 'selected'; endif; ?>"; } /** * Compile a checked block into valid PHP. * * @param string $condition * @return string */ protected function compileChecked($condition) { return "<?php if{$condition}: echo 'checked'; endif; ?>"; } /** * Compile a disabled block into valid PHP. * * @param string $condition * @return string */ protected function compileDisabled($condition) { return "<?php if{$condition}: echo 'disabled'; endif; ?>"; } /** * Compile a required block into valid PHP. * * @param string $condition * @return string */ protected function compileRequired($condition) { return "<?php if{$condition}: echo 'required'; endif; ?>"; } /** * Compile a readonly block into valid PHP. * * @param string $condition * @return string */ protected function compileReadonly($condition) { return "<?php if{$condition}: echo 'readonly'; endif; ?>"; } /** * Compile the push statements into valid PHP. * * @param string $expression * @return string */ protected function compilePushIf($expression) { $parts = explode(',', $this->stripParentheses($expression), 2); return "<?php if({$parts[0]}): \$__env->startPush({$parts[1]}); ?>"; } /** * Compile the end-push statements into valid PHP. * * @return string */ protected function compileEndPushIf() { return '<?php $__env->stopPush(); endif; ?>'; } } src/Illuminate/View/Compilers/Concerns/CompilesComments.php 0000644 00000000635 15107327040 0020054 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; trait CompilesComments { /** * Compile Blade comments into an empty string. * * @param string $value * @return string */ protected function compileComments($value) { $pattern = sprintf('/%s--(.*?)--%s/s', $this->contentTags[0], $this->contentTags[1]); return preg_replace($pattern, '', $value); } } src/Illuminate/View/Compilers/Concerns/CompilesLoops.php 0000644 00000011471 15107327040 0017363 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; use Illuminate\Contracts\View\ViewCompilationException; trait CompilesLoops { /** * Counter to keep track of nested forelse statements. * * @var int */ protected $forElseCounter = 0; /** * Compile the for-else statements into valid PHP. * * @param string $expression * @return string * * @throws \Illuminate\Contracts\View\ViewCompilationException */ protected function compileForelse($expression) { $empty = '$__empty_'.++$this->forElseCounter; preg_match('/\( *(.+) +as +(.+)\)$/is', $expression ?? '', $matches); if (count($matches) === 0) { throw new ViewCompilationException('Malformed @forelse statement.'); } $iteratee = trim($matches[1]); $iteration = trim($matches[2]); $initLoop = "\$__currentLoopData = {$iteratee}; \$__env->addLoop(\$__currentLoopData);"; $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();'; return "<?php {$empty} = true; {$initLoop} foreach(\$__currentLoopData as {$iteration}): {$iterateLoop} {$empty} = false; ?>"; } /** * Compile the for-else-empty and empty statements into valid PHP. * * @param string $expression * @return string */ protected function compileEmpty($expression) { if ($expression) { return "<?php if(empty{$expression}): ?>"; } $empty = '$__empty_'.$this->forElseCounter--; return "<?php endforeach; \$__env->popLoop(); \$loop = \$__env->getLastLoop(); if ({$empty}): ?>"; } /** * Compile the end-for-else statements into valid PHP. * * @return string */ protected function compileEndforelse() { return '<?php endif; ?>'; } /** * Compile the end-empty statements into valid PHP. * * @return string */ protected function compileEndEmpty() { return '<?php endif; ?>'; } /** * Compile the for statements into valid PHP. * * @param string $expression * @return string */ protected function compileFor($expression) { return "<?php for{$expression}: ?>"; } /** * Compile the for-each statements into valid PHP. * * @param string $expression * @return string * * @throws \Illuminate\Contracts\View\ViewCompilationException */ protected function compileForeach($expression) { preg_match('/\( *(.+) +as +(.*)\)$/is', $expression ?? '', $matches); if (count($matches) === 0) { throw new ViewCompilationException('Malformed @foreach statement.'); } $iteratee = trim($matches[1]); $iteration = trim($matches[2]); $initLoop = "\$__currentLoopData = {$iteratee}; \$__env->addLoop(\$__currentLoopData);"; $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();'; return "<?php {$initLoop} foreach(\$__currentLoopData as {$iteration}): {$iterateLoop} ?>"; } /** * Compile the break statements into valid PHP. * * @param string $expression * @return string */ protected function compileBreak($expression) { if ($expression) { preg_match('/\(\s*(-?\d+)\s*\)$/', $expression, $matches); return $matches ? '<?php break '.max(1, $matches[1]).'; ?>' : "<?php if{$expression} break; ?>"; } return '<?php break; ?>'; } /** * Compile the continue statements into valid PHP. * * @param string $expression * @return string */ protected function compileContinue($expression) { if ($expression) { preg_match('/\(\s*(-?\d+)\s*\)$/', $expression, $matches); return $matches ? '<?php continue '.max(1, $matches[1]).'; ?>' : "<?php if{$expression} continue; ?>"; } return '<?php continue; ?>'; } /** * Compile the end-for statements into valid PHP. * * @return string */ protected function compileEndfor() { return '<?php endfor; ?>'; } /** * Compile the end-for-each statements into valid PHP. * * @return string */ protected function compileEndforeach() { return '<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>'; } /** * Compile the while statements into valid PHP. * * @param string $expression * @return string */ protected function compileWhile($expression) { return "<?php while{$expression}: ?>"; } /** * Compile the end-while statements into valid PHP. * * @return string */ protected function compileEndwhile() { return '<?php endwhile; ?>'; } } src/Illuminate/View/Compilers/CompilerInterface.php 0000644 00000001060 15107327040 0016405 0 ustar 00 <?php namespace Illuminate\View\Compilers; interface CompilerInterface { /** * Get the path to the compiled version of a view. * * @param string $path * @return string */ public function getCompiledPath($path); /** * Determine if the given view is expired. * * @param string $path * @return bool */ public function isExpired($path); /** * Compile the view at the given path. * * @param string $path * @return void */ public function compile($path); } src/Illuminate/View/Compilers/ComponentTagCompiler.php 0000644 00000063263 15107327040 0017120 0 ustar 00 <?php namespace Illuminate\View\Compilers; use Illuminate\Container\Container; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\View\Factory; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Str; use Illuminate\View\AnonymousComponent; use Illuminate\View\DynamicComponent; use Illuminate\View\ViewFinderInterface; use InvalidArgumentException; use ReflectionClass; /** * @author Spatie bvba <info@spatie.be> * @author Taylor Otwell <taylor@laravel.com> */ class ComponentTagCompiler { /** * The Blade compiler instance. * * @var \Illuminate\View\Compilers\BladeCompiler */ protected $blade; /** * The component class aliases. * * @var array */ protected $aliases = []; /** * The component class namespaces. * * @var array */ protected $namespaces = []; /** * The "bind:" attributes that have been compiled for the current component. * * @var array */ protected $boundAttributes = []; /** * Create a new component tag compiler. * * @param array $aliases * @param array $namespaces * @param \Illuminate\View\Compilers\BladeCompiler|null $blade * @return void */ public function __construct(array $aliases = [], array $namespaces = [], ?BladeCompiler $blade = null) { $this->aliases = $aliases; $this->namespaces = $namespaces; $this->blade = $blade ?: new BladeCompiler(new Filesystem, sys_get_temp_dir()); } /** * Compile the component and slot tags within the given string. * * @param string $value * @return string */ public function compile(string $value) { $value = $this->compileSlots($value); return $this->compileTags($value); } /** * Compile the tags within the given string. * * @param string $value * @return string * * @throws \InvalidArgumentException */ public function compileTags(string $value) { $value = $this->compileSelfClosingTags($value); $value = $this->compileOpeningTags($value); $value = $this->compileClosingTags($value); return $value; } /** * Compile the opening tags within the given string. * * @param string $value * @return string * * @throws \InvalidArgumentException */ protected function compileOpeningTags(string $value) { $pattern = "/ < \s* x[-\:]([\w\-\:\.]*) (?<attributes> (?: \s+ (?: (?: @(?:class)(\( (?: (?>[^()]+) | (?-1) )* \)) ) | (?: @(?:style)(\( (?: (?>[^()]+) | (?-1) )* \)) ) | (?: \{\{\s*\\\$attributes(?:[^}]+?)?\s*\}\} ) | (?: (\:\\\$)(\w+) ) | (?: [\w\-:.@%]+ ( = (?: \\\"[^\\\"]*\\\" | \'[^\']*\' | [^\'\\\"=<>]+ ) )? ) ) )* \s* ) (?<![\/=\-]) > /x"; return preg_replace_callback($pattern, function (array $matches) { $this->boundAttributes = []; $attributes = $this->getAttributesFromAttributeString($matches['attributes']); return $this->componentString($matches[1], $attributes); }, $value); } /** * Compile the self-closing tags within the given string. * * @param string $value * @return string * * @throws \InvalidArgumentException */ protected function compileSelfClosingTags(string $value) { $pattern = "/ < \s* x[-\:]([\w\-\:\.]*) \s* (?<attributes> (?: \s+ (?: (?: @(?:class)(\( (?: (?>[^()]+) | (?-1) )* \)) ) | (?: @(?:style)(\( (?: (?>[^()]+) | (?-1) )* \)) ) | (?: \{\{\s*\\\$attributes(?:[^}]+?)?\s*\}\} ) | (?: (\:\\\$)(\w+) ) | (?: [\w\-:.@%]+ ( = (?: \\\"[^\\\"]*\\\" | \'[^\']*\' | [^\'\\\"=<>]+ ) )? ) ) )* \s* ) \/> /x"; return preg_replace_callback($pattern, function (array $matches) { $this->boundAttributes = []; $attributes = $this->getAttributesFromAttributeString($matches['attributes']); return $this->componentString($matches[1], $attributes)."\n@endComponentClass##END-COMPONENT-CLASS##"; }, $value); } /** * Compile the Blade component string for the given component and attributes. * * @param string $component * @param array $attributes * @return string * * @throws \InvalidArgumentException */ protected function componentString(string $component, array $attributes) { $class = $this->componentClass($component); [$data, $attributes] = $this->partitionDataAndAttributes($class, $attributes); $data = $data->mapWithKeys(function ($value, $key) { return [Str::camel($key) => $value]; }); // If the component doesn't exist as a class, we'll assume it's a class-less // component and pass the component as a view parameter to the data so it // can be accessed within the component and we can render out the view. if (! class_exists($class)) { $view = Str::startsWith($component, 'mail::') ? "\$__env->getContainer()->make(Illuminate\\View\\Factory::class)->make('{$component}')" : "'$class'"; $parameters = [ 'view' => $view, 'data' => '['.$this->attributesToString($data->all(), $escapeBound = false).']', ]; $class = AnonymousComponent::class; } else { $parameters = $data->all(); } return "##BEGIN-COMPONENT-CLASS##@component('{$class}', '{$component}', [".$this->attributesToString($parameters, $escapeBound = false).']) <?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag && $constructor = (new ReflectionClass('.$class.'::class))->getConstructor()): ?> <?php $attributes = $attributes->except(collect($constructor->getParameters())->map->getName()->all()); ?> <?php endif; ?> <?php $component->withAttributes(['.$this->attributesToString($attributes->all(), $escapeAttributes = $class !== DynamicComponent::class).']); ?>'; } /** * Get the component class for a given component alias. * * @param string $component * @return string * * @throws \InvalidArgumentException */ public function componentClass(string $component) { $viewFactory = Container::getInstance()->make(Factory::class); if (isset($this->aliases[$component])) { if (class_exists($alias = $this->aliases[$component])) { return $alias; } if ($viewFactory->exists($alias)) { return $alias; } throw new InvalidArgumentException( "Unable to locate class or view [{$alias}] for component [{$component}]." ); } if ($class = $this->findClassByComponent($component)) { return $class; } if (class_exists($class = $this->guessClassName($component))) { return $class; } if (! is_null($guess = $this->guessAnonymousComponentUsingNamespaces($viewFactory, $component)) || ! is_null($guess = $this->guessAnonymousComponentUsingPaths($viewFactory, $component))) { return $guess; } if (Str::startsWith($component, 'mail::')) { return $component; } throw new InvalidArgumentException( "Unable to locate a class or view for component [{$component}]." ); } /** * Attempt to find an anonymous component using the registered anonymous component paths. * * @param \Illuminate\Contracts\View\Factory $viewFactory * @param string $component * @return string|null */ protected function guessAnonymousComponentUsingPaths(Factory $viewFactory, string $component) { $delimiter = ViewFinderInterface::HINT_PATH_DELIMITER; foreach ($this->blade->getAnonymousComponentPaths() as $path) { try { if (str_contains($component, $delimiter) && ! str_starts_with($component, $path['prefix'].$delimiter)) { continue; } $formattedComponent = str_starts_with($component, $path['prefix'].$delimiter) ? Str::after($component, $delimiter) : $component; if (! is_null($guess = match (true) { $viewFactory->exists($guess = $path['prefixHash'].$delimiter.$formattedComponent) => $guess, $viewFactory->exists($guess = $path['prefixHash'].$delimiter.$formattedComponent.'.index') => $guess, default => null, })) { return $guess; } } catch (InvalidArgumentException) { // } } } /** * Attempt to find an anonymous component using the registered anonymous component namespaces. * * @param \Illuminate\Contracts\View\Factory $viewFactory * @param string $component * @return string|null */ protected function guessAnonymousComponentUsingNamespaces(Factory $viewFactory, string $component) { return collect($this->blade->getAnonymousComponentNamespaces()) ->filter(function ($directory, $prefix) use ($component) { return Str::startsWith($component, $prefix.'::'); }) ->prepend('components', $component) ->reduce(function ($carry, $directory, $prefix) use ($component, $viewFactory) { if (! is_null($carry)) { return $carry; } $componentName = Str::after($component, $prefix.'::'); if ($viewFactory->exists($view = $this->guessViewName($componentName, $directory))) { return $view; } if ($viewFactory->exists($view = $this->guessViewName($componentName, $directory).'.index')) { return $view; } }); } /** * Find the class for the given component using the registered namespaces. * * @param string $component * @return string|null */ public function findClassByComponent(string $component) { $segments = explode('::', $component); $prefix = $segments[0]; if (! isset($this->namespaces[$prefix], $segments[1])) { return; } if (class_exists($class = $this->namespaces[$prefix].'\\'.$this->formatClassName($segments[1]))) { return $class; } } /** * Guess the class name for the given component. * * @param string $component * @return string */ public function guessClassName(string $component) { $namespace = Container::getInstance() ->make(Application::class) ->getNamespace(); $class = $this->formatClassName($component); return $namespace.'View\\Components\\'.$class; } /** * Format the class name for the given component. * * @param string $component * @return string */ public function formatClassName(string $component) { $componentPieces = array_map(function ($componentPiece) { return ucfirst(Str::camel($componentPiece)); }, explode('.', $component)); return implode('\\', $componentPieces); } /** * Guess the view name for the given component. * * @param string $name * @param string $prefix * @return string */ public function guessViewName($name, $prefix = 'components.') { if (! Str::endsWith($prefix, '.')) { $prefix .= '.'; } $delimiter = ViewFinderInterface::HINT_PATH_DELIMITER; if (str_contains($name, $delimiter)) { return Str::replaceFirst($delimiter, $delimiter.$prefix, $name); } return $prefix.$name; } /** * Partition the data and extra attributes from the given array of attributes. * * @param string $class * @param array $attributes * @return array */ public function partitionDataAndAttributes($class, array $attributes) { // If the class doesn't exist, we'll assume it is a class-less component and // return all of the attributes as both data and attributes since we have // now way to partition them. The user can exclude attributes manually. if (! class_exists($class)) { return [collect($attributes), collect($attributes)]; } $constructor = (new ReflectionClass($class))->getConstructor(); $parameterNames = $constructor ? collect($constructor->getParameters())->map->getName()->all() : []; return collect($attributes)->partition(function ($value, $key) use ($parameterNames) { return in_array(Str::camel($key), $parameterNames); })->all(); } /** * Compile the closing tags within the given string. * * @param string $value * @return string */ protected function compileClosingTags(string $value) { return preg_replace("/<\/\s*x[-\:][\w\-\:\.]*\s*>/", ' @endComponentClass##END-COMPONENT-CLASS##', $value); } /** * Compile the slot tags within the given string. * * @param string $value * @return string */ public function compileSlots(string $value) { $pattern = "/ < \s* x[\-\:]slot (?:\:(?<inlineName>\w+(?:-\w+)*))? (?:\s+name=(?<name>(\"[^\"]+\"|\\\'[^\\\']+\\\'|[^\s>]+)))? (?:\s+\:name=(?<boundName>(\"[^\"]+\"|\\\'[^\\\']+\\\'|[^\s>]+)))? (?<attributes> (?: \s+ (?: (?: @(?:class)(\( (?: (?>[^()]+) | (?-1) )* \)) ) | (?: @(?:style)(\( (?: (?>[^()]+) | (?-1) )* \)) ) | (?: \{\{\s*\\\$attributes(?:[^}]+?)?\s*\}\} ) | (?: [\w\-:.@]+ ( = (?: \\\"[^\\\"]*\\\" | \'[^\']*\' | [^\'\\\"=<>]+ ) )? ) ) )* \s* ) (?<![\/=\-]) > /x"; $value = preg_replace_callback($pattern, function ($matches) { $name = $this->stripQuotes($matches['inlineName'] ?: $matches['name'] ?: $matches['boundName']); if (Str::contains($name, '-') && ! empty($matches['inlineName'])) { $name = Str::camel($name); } // If the name was given as a simple string, we will wrap it in quotes as if it was bound for convenience... if (! empty($matches['inlineName']) || ! empty($matches['name'])) { $name = "'{$name}'"; } $this->boundAttributes = []; $attributes = $this->getAttributesFromAttributeString($matches['attributes']); // If an inline name was provided and a name or bound name was *also* provided, we will assume the name should be an attribute... if (! empty($matches['inlineName']) && (! empty($matches['name']) || ! empty($matches['boundName']))) { $attributes = ! empty($matches['name']) ? array_merge($attributes, $this->getAttributesFromAttributeString('name='.$matches['name'])) : array_merge($attributes, $this->getAttributesFromAttributeString(':name='.$matches['boundName'])); } return " @slot({$name}, null, [".$this->attributesToString($attributes).']) '; }, $value); return preg_replace('/<\/\s*x[\-\:]slot[^>]*>/', ' @endslot', $value); } /** * Get an array of attributes from the given attribute string. * * @param string $attributeString * @return array */ protected function getAttributesFromAttributeString(string $attributeString) { $attributeString = $this->parseShortAttributeSyntax($attributeString); $attributeString = $this->parseAttributeBag($attributeString); $attributeString = $this->parseComponentTagClassStatements($attributeString); $attributeString = $this->parseComponentTagStyleStatements($attributeString); $attributeString = $this->parseBindAttributes($attributeString); $pattern = '/ (?<attribute>[\w\-:.@%]+) ( = (?<value> ( \"[^\"]+\" | \\\'[^\\\']+\\\' | [^\s>]+ ) ) )? /x'; if (! preg_match_all($pattern, $attributeString, $matches, PREG_SET_ORDER)) { return []; } return collect($matches)->mapWithKeys(function ($match) { $attribute = $match['attribute']; $value = $match['value'] ?? null; if (is_null($value)) { $value = 'true'; $attribute = Str::start($attribute, 'bind:'); } $value = $this->stripQuotes($value); if (str_starts_with($attribute, 'bind:')) { $attribute = Str::after($attribute, 'bind:'); $this->boundAttributes[$attribute] = true; } else { $value = "'".$this->compileAttributeEchos($value)."'"; } if (str_starts_with($attribute, '::')) { $attribute = substr($attribute, 1); } return [$attribute => $value]; })->toArray(); } /** * Parses a short attribute syntax like :$foo into a fully-qualified syntax like :foo="$foo". * * @param string $value * @return string */ protected function parseShortAttributeSyntax(string $value) { $pattern = "/\s\:\\\$(\w+)/x"; return preg_replace_callback($pattern, function (array $matches) { return " :{$matches[1]}=\"\${$matches[1]}\""; }, $value); } /** * Parse the attribute bag in a given attribute string into its fully-qualified syntax. * * @param string $attributeString * @return string */ protected function parseAttributeBag(string $attributeString) { $pattern = "/ (?:^|\s+) # start of the string or whitespace between attributes \{\{\s*(\\\$attributes(?:[^}]+?(?<!\s))?)\s*\}\} # exact match of attributes variable being echoed /x"; return preg_replace($pattern, ' :attributes="$1"', $attributeString); } /** * Parse @class statements in a given attribute string into their fully-qualified syntax. * * @param string $attributeString * @return string */ protected function parseComponentTagClassStatements(string $attributeString) { return preg_replace_callback( '/@(class)(\( ( (?>[^()]+) | (?2) )* \))/x', function ($match) { if ($match[1] === 'class') { $match[2] = str_replace('"', "'", $match[2]); return ":class=\"\Illuminate\Support\Arr::toCssClasses{$match[2]}\""; } return $match[0]; }, $attributeString ); } /** * Parse @style statements in a given attribute string into their fully-qualified syntax. * * @param string $attributeString * @return string */ protected function parseComponentTagStyleStatements(string $attributeString) { return preg_replace_callback( '/@(style)(\( ( (?>[^()]+) | (?2) )* \))/x', function ($match) { if ($match[1] === 'style') { $match[2] = str_replace('"', "'", $match[2]); return ":style=\"\Illuminate\Support\Arr::toCssStyles{$match[2]}\""; } return $match[0]; }, $attributeString ); } /** * Parse the "bind" attributes in a given attribute string into their fully-qualified syntax. * * @param string $attributeString * @return string */ protected function parseBindAttributes(string $attributeString) { $pattern = "/ (?:^|\s+) # start of the string or whitespace between attributes :(?!:) # attribute needs to start with a single colon ([\w\-:.@]+) # match the actual attribute name = # only match attributes that have a value /xm"; return preg_replace($pattern, ' bind:$1=', $attributeString); } /** * Compile any Blade echo statements that are present in the attribute string. * * These echo statements need to be converted to string concatenation statements. * * @param string $attributeString * @return string */ protected function compileAttributeEchos(string $attributeString) { $value = $this->blade->compileEchos($attributeString); $value = $this->escapeSingleQuotesOutsideOfPhpBlocks($value); $value = str_replace('<?php echo ', '\'.', $value); $value = str_replace('; ?>', '.\'', $value); return $value; } /** * Escape the single quotes in the given string that are outside of PHP blocks. * * @param string $value * @return string */ protected function escapeSingleQuotesOutsideOfPhpBlocks(string $value) { return collect(token_get_all($value))->map(function ($token) { if (! is_array($token)) { return $token; } return $token[0] === T_INLINE_HTML ? str_replace("'", "\\'", $token[1]) : $token[1]; })->implode(''); } /** * Convert an array of attributes to a string. * * @param array $attributes * @param bool $escapeBound * @return string */ protected function attributesToString(array $attributes, $escapeBound = true) { return collect($attributes) ->map(function (string $value, string $attribute) use ($escapeBound) { return $escapeBound && isset($this->boundAttributes[$attribute]) && $value !== 'true' && ! is_numeric($value) ? "'{$attribute}' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute({$value})" : "'{$attribute}' => {$value}"; }) ->implode(','); } /** * Strip any quotes from the given string. * * @param string $value * @return string */ public function stripQuotes(string $value) { return Str::startsWith($value, ['"', '\'']) ? substr($value, 1, -1) : $value; } } src/Illuminate/View/Compilers/Compiler.php 0000644 00000006341 15107327040 0014573 0 ustar 00 <?php namespace Illuminate\View\Compilers; use ErrorException; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Str; use InvalidArgumentException; abstract class Compiler { /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * The cache path for the compiled views. * * @var string */ protected $cachePath; /** * The base path that should be removed from paths before hashing. * * @var string */ protected $basePath; /** * Determines if compiled views should be cached. * * @var bool */ protected $shouldCache; /** * The compiled view file extension. * * @var string */ protected $compiledExtension = 'php'; /** * Create a new compiler instance. * * @param \Illuminate\Filesystem\Filesystem $files * @param string $cachePath * @param string $basePath * @param bool $shouldCache * @param string $compiledExtension * @return void * * @throws \InvalidArgumentException */ public function __construct( Filesystem $files, $cachePath, $basePath = '', $shouldCache = true, $compiledExtension = 'php') { if (! $cachePath) { throw new InvalidArgumentException('Please provide a valid cache path.'); } $this->files = $files; $this->cachePath = $cachePath; $this->basePath = $basePath; $this->shouldCache = $shouldCache; $this->compiledExtension = $compiledExtension; } /** * Get the path to the compiled version of a view. * * @param string $path * @return string */ public function getCompiledPath($path) { return $this->cachePath.'/'.hash('xxh128', 'v2'.Str::after($path, $this->basePath)).'.'.$this->compiledExtension; } /** * Determine if the view at the given path is expired. * * @param string $path * @return bool * * @throws \ErrorException */ public function isExpired($path) { if (! $this->shouldCache) { return true; } $compiled = $this->getCompiledPath($path); // If the compiled file doesn't exist we will indicate that the view is expired // so that it can be re-compiled. Else, we will verify the last modification // of the views is less than the modification times of the compiled views. if (! $this->files->exists($compiled)) { return true; } try { return $this->files->lastModified($path) >= $this->files->lastModified($compiled); } catch (ErrorException $exception) { if (! $this->files->exists($compiled)) { return true; } throw $exception; } } /** * Create the compiled file directory if necessary. * * @param string $path * @return void */ protected function ensureCompiledDirectoryExists($path) { if (! $this->files->exists(dirname($path))) { $this->files->makeDirectory(dirname($path), 0777, true, true); } } } src/Illuminate/View/ViewName.php 0000644 00000001011 15107327040 0012564 0 ustar 00 <?php namespace Illuminate\View; class ViewName { /** * Normalize the given view name. * * @param string $name * @return string */ public static function normalize($name) { $delimiter = ViewFinderInterface::HINT_PATH_DELIMITER; if (! str_contains($name, $delimiter)) { return str_replace('/', '.', $name); } [$namespace, $name] = explode($delimiter, $name); return $namespace.$delimiter.str_replace('/', '.', $name); } } src/Illuminate/View/InvokableComponentVariable.php 0000644 00000003750 15107327040 0016330 0 ustar 00 <?php namespace Illuminate\View; use ArrayIterator; use Closure; use Illuminate\Contracts\Support\DeferringDisplayableValue; use Illuminate\Support\Enumerable; use IteratorAggregate; use Traversable; class InvokableComponentVariable implements DeferringDisplayableValue, IteratorAggregate { /** * The callable instance to resolve the variable value. * * @var \Closure */ protected $callable; /** * Create a new variable instance. * * @param \Closure $callable * @return void */ public function __construct(Closure $callable) { $this->callable = $callable; } /** * Resolve the displayable value that the class is deferring. * * @return \Illuminate\Contracts\Support\Htmlable|string */ public function resolveDisplayableValue() { return $this->__invoke(); } /** * Get an iterator instance for the variable. * * @return \ArrayIterator */ public function getIterator(): Traversable { $result = $this->__invoke(); return new ArrayIterator($result instanceof Enumerable ? $result->all() : $result); } /** * Dynamically proxy attribute access to the variable. * * @param string $key * @return mixed */ public function __get($key) { return $this->__invoke()->{$key}; } /** * Dynamically proxy method access to the variable. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->__invoke()->{$method}(...$parameters); } /** * Resolve the variable. * * @return mixed */ public function __invoke() { return call_user_func($this->callable); } /** * Resolve the variable as a string. * * @return mixed */ public function __toString() { return (string) $this->__invoke(); } } src/Illuminate/View/ViewServiceProvider.php 0000644 00000012161 15107327040 0015027 0 ustar 00 <?php namespace Illuminate\View; use Illuminate\Support\ServiceProvider; use Illuminate\View\Compilers\BladeCompiler; use Illuminate\View\Engines\CompilerEngine; use Illuminate\View\Engines\EngineResolver; use Illuminate\View\Engines\FileEngine; use Illuminate\View\Engines\PhpEngine; class ViewServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { $this->registerFactory(); $this->registerViewFinder(); $this->registerBladeCompiler(); $this->registerEngineResolver(); $this->app->terminating(static function () { Component::flushCache(); }); } /** * Register the view environment. * * @return void */ public function registerFactory() { $this->app->singleton('view', function ($app) { // Next we need to grab the engine resolver instance that will be used by the // environment. The resolver will be used by an environment to get each of // the various engine implementations such as plain PHP or Blade engine. $resolver = $app['view.engine.resolver']; $finder = $app['view.finder']; $factory = $this->createFactory($resolver, $finder, $app['events']); // We will also set the container instance on this view environment since the // view composers may be classes registered in the container, which allows // for great testable, flexible composers for the application developer. $factory->setContainer($app); $factory->share('app', $app); $app->terminating(static function () { Component::forgetFactory(); }); return $factory; }); } /** * Create a new Factory Instance. * * @param \Illuminate\View\Engines\EngineResolver $resolver * @param \Illuminate\View\ViewFinderInterface $finder * @param \Illuminate\Contracts\Events\Dispatcher $events * @return \Illuminate\View\Factory */ protected function createFactory($resolver, $finder, $events) { return new Factory($resolver, $finder, $events); } /** * Register the view finder implementation. * * @return void */ public function registerViewFinder() { $this->app->bind('view.finder', function ($app) { return new FileViewFinder($app['files'], $app['config']['view.paths']); }); } /** * Register the Blade compiler implementation. * * @return void */ public function registerBladeCompiler() { $this->app->singleton('blade.compiler', function ($app) { return tap(new BladeCompiler( $app['files'], $app['config']['view.compiled'], $app['config']->get('view.relative_hash', false) ? $app->basePath() : '', $app['config']->get('view.cache', true), $app['config']->get('view.compiled_extension', 'php'), ), function ($blade) { $blade->component('dynamic-component', DynamicComponent::class); }); }); } /** * Register the engine resolver instance. * * @return void */ public function registerEngineResolver() { $this->app->singleton('view.engine.resolver', function () { $resolver = new EngineResolver; // Next, we will register the various view engines with the resolver so that the // environment will resolve the engines needed for various views based on the // extension of view file. We call a method for each of the view's engines. foreach (['file', 'php', 'blade'] as $engine) { $this->{'register'.ucfirst($engine).'Engine'}($resolver); } return $resolver; }); } /** * Register the file engine implementation. * * @param \Illuminate\View\Engines\EngineResolver $resolver * @return void */ public function registerFileEngine($resolver) { $resolver->register('file', function () { return new FileEngine($this->app['files']); }); } /** * Register the PHP engine implementation. * * @param \Illuminate\View\Engines\EngineResolver $resolver * @return void */ public function registerPhpEngine($resolver) { $resolver->register('php', function () { return new PhpEngine($this->app['files']); }); } /** * Register the Blade engine implementation. * * @param \Illuminate\View\Engines\EngineResolver $resolver * @return void */ public function registerBladeEngine($resolver) { $resolver->register('blade', function () { $compiler = new CompilerEngine($this->app['blade.compiler'], $this->app['files']); $this->app->terminating(static function () use ($compiler) { $compiler->forgetCompiledOrNotExpired(); }); return $compiler; }); } } src/Illuminate/Testing/TestResponse.php 0000644 00000136011 15107327040 0014223 0 ustar 00 <?php namespace Illuminate\Testing; use ArrayAccess; use Closure; use Illuminate\Contracts\Support\MessageBag; use Illuminate\Contracts\View\View; use Illuminate\Cookie\CookieValuePrefix; use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use Illuminate\Support\Traits\Tappable; use Illuminate\Support\ViewErrorBag; use Illuminate\Testing\Assert as PHPUnit; use Illuminate\Testing\Constraints\SeeInOrder; use Illuminate\Testing\Fluent\AssertableJson; use LogicException; use PHPUnit\Framework\ExpectationFailedException; use ReflectionProperty; use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpFoundation\StreamedResponse; /** * @mixin \Illuminate\Http\Response */ class TestResponse implements ArrayAccess { use Concerns\AssertsStatusCodes, Tappable, Macroable { __call as macroCall; } /** * The response to delegate to. * * @var \Illuminate\Http\Response */ public $baseResponse; /** * The collection of logged exceptions for the request. * * @var \Illuminate\Support\Collection */ public $exceptions; /** * The streamed content of the response. * * @var string */ protected $streamedContent; /** * Create a new test response instance. * * @param \Illuminate\Http\Response $response * @return void */ public function __construct($response) { $this->baseResponse = $response; $this->exceptions = new Collection; } /** * Create a new TestResponse from another response. * * @param \Illuminate\Http\Response $response * @return static */ public static function fromBaseResponse($response) { return new static($response); } /** * Assert that the response has a successful status code. * * @return $this */ public function assertSuccessful() { PHPUnit::assertTrue( $this->isSuccessful(), $this->statusMessageWithDetails('>=200, <300', $this->getStatusCode()) ); return $this; } /** * Assert that the Precognition request was successful. * * @return $this */ public function assertSuccessfulPrecognition() { $this->assertNoContent(); PHPUnit::assertTrue( $this->headers->has('Precognition-Success'), 'Header [Precognition-Success] not present on response.' ); PHPUnit::assertSame( 'true', $this->headers->get('Precognition-Success'), 'The Precognition-Success header was found, but the value is not `true`.' ); return $this; } /** * Assert that the response is a server error. * * @return $this */ public function assertServerError() { PHPUnit::assertTrue( $this->isServerError(), $this->statusMessageWithDetails('>=500, < 600', $this->getStatusCode()) ); return $this; } /** * Assert that the response has the given status code. * * @param int $status * @return $this */ public function assertStatus($status) { $message = $this->statusMessageWithDetails($status, $actual = $this->getStatusCode()); PHPUnit::assertSame($actual, $status, $message); return $this; } /** * Get an assertion message for a status assertion containing extra details when available. * * @param string|int $expected * @param string|int $actual * @return string */ protected function statusMessageWithDetails($expected, $actual) { return "Expected response status code [{$expected}] but received {$actual}."; } /** * Assert whether the response is redirecting to a given URI. * * @param string|null $uri * @return $this */ public function assertRedirect($uri = null) { PHPUnit::assertTrue( $this->isRedirect(), $this->statusMessageWithDetails('201, 301, 302, 303, 307, 308', $this->getStatusCode()), ); if (! is_null($uri)) { $this->assertLocation($uri); } return $this; } /** * Assert whether the response is redirecting to a URI that contains the given URI. * * @param string $uri * @return $this */ public function assertRedirectContains($uri) { PHPUnit::assertTrue( $this->isRedirect(), $this->statusMessageWithDetails('201, 301, 302, 303, 307, 308', $this->getStatusCode()), ); PHPUnit::assertTrue( Str::contains($this->headers->get('Location'), $uri), 'Redirect location ['.$this->headers->get('Location').'] does not contain ['.$uri.'].' ); return $this; } /** * Assert whether the response is redirecting to a given route. * * @param string $name * @param mixed $parameters * @return $this */ public function assertRedirectToRoute($name, $parameters = []) { $uri = route($name, $parameters); PHPUnit::assertTrue( $this->isRedirect(), $this->statusMessageWithDetails('201, 301, 302, 303, 307, 308', $this->getStatusCode()), ); $this->assertLocation($uri); return $this; } /** * Assert whether the response is redirecting to a given signed route. * * @param string|null $name * @param mixed $parameters * @return $this */ public function assertRedirectToSignedRoute($name = null, $parameters = []) { if (! is_null($name)) { $uri = route($name, $parameters); } PHPUnit::assertTrue( $this->isRedirect(), $this->statusMessageWithDetails('201, 301, 302, 303, 307, 308', $this->getStatusCode()), ); $request = Request::create($this->headers->get('Location')); PHPUnit::assertTrue( $request->hasValidSignature(), 'The response is not a redirect to a signed route.' ); if (! is_null($name)) { $expectedUri = rtrim($request->fullUrlWithQuery([ 'signature' => null, 'expires' => null, ]), '?'); PHPUnit::assertEquals( app('url')->to($uri), $expectedUri ); } return $this; } /** * Asserts that the response contains the given header and equals the optional value. * * @param string $headerName * @param mixed $value * @return $this */ public function assertHeader($headerName, $value = null) { PHPUnit::assertTrue( $this->headers->has($headerName), "Header [{$headerName}] not present on response." ); $actual = $this->headers->get($headerName); if (! is_null($value)) { PHPUnit::assertEquals( $value, $this->headers->get($headerName), "Header [{$headerName}] was found, but value [{$actual}] does not match [{$value}]." ); } return $this; } /** * Asserts that the response does not contain the given header. * * @param string $headerName * @return $this */ public function assertHeaderMissing($headerName) { PHPUnit::assertFalse( $this->headers->has($headerName), "Unexpected header [{$headerName}] is present on response." ); return $this; } /** * Assert that the current location header matches the given URI. * * @param string $uri * @return $this */ public function assertLocation($uri) { PHPUnit::assertEquals( app('url')->to($uri), app('url')->to($this->headers->get('Location')) ); return $this; } /** * Assert that the response offers a file download. * * @param string|null $filename * @return $this */ public function assertDownload($filename = null) { $contentDisposition = explode(';', $this->headers->get('content-disposition')); if (trim($contentDisposition[0]) !== 'attachment') { PHPUnit::fail( 'Response does not offer a file download.'.PHP_EOL. 'Disposition ['.trim($contentDisposition[0]).'] found in header, [attachment] expected.' ); } if (! is_null($filename)) { if (isset($contentDisposition[1]) && trim(explode('=', $contentDisposition[1])[0]) !== 'filename') { PHPUnit::fail( 'Unsupported Content-Disposition header provided.'.PHP_EOL. 'Disposition ['.trim(explode('=', $contentDisposition[1])[0]).'] found in header, [filename] expected.' ); } $message = "Expected file [{$filename}] is not present in Content-Disposition header."; if (! isset($contentDisposition[1])) { PHPUnit::fail($message); } else { PHPUnit::assertSame( $filename, isset(explode('=', $contentDisposition[1])[1]) ? trim(explode('=', $contentDisposition[1])[1], " \"'") : '', $message ); return $this; } } else { PHPUnit::assertTrue(true); return $this; } } /** * Asserts that the response contains the given cookie and equals the optional value. * * @param string $cookieName * @param mixed $value * @return $this */ public function assertPlainCookie($cookieName, $value = null) { $this->assertCookie($cookieName, $value, false); return $this; } /** * Asserts that the response contains the given cookie and equals the optional value. * * @param string $cookieName * @param mixed $value * @param bool $encrypted * @param bool $unserialize * @return $this */ public function assertCookie($cookieName, $value = null, $encrypted = true, $unserialize = false) { PHPUnit::assertNotNull( $cookie = $this->getCookie($cookieName, $encrypted && ! is_null($value), $unserialize), "Cookie [{$cookieName}] not present on response." ); if (! $cookie || is_null($value)) { return $this; } $cookieValue = $cookie->getValue(); PHPUnit::assertEquals( $value, $cookieValue, "Cookie [{$cookieName}] was found, but value [{$cookieValue}] does not match [{$value}]." ); return $this; } /** * Asserts that the response contains the given cookie and is expired. * * @param string $cookieName * @return $this */ public function assertCookieExpired($cookieName) { PHPUnit::assertNotNull( $cookie = $this->getCookie($cookieName, false), "Cookie [{$cookieName}] not present on response." ); $expiresAt = Carbon::createFromTimestamp($cookie->getExpiresTime()); PHPUnit::assertTrue( $cookie->getExpiresTime() !== 0 && $expiresAt->lessThan(Carbon::now()), "Cookie [{$cookieName}] is not expired, it expires at [{$expiresAt}]." ); return $this; } /** * Asserts that the response contains the given cookie and is not expired. * * @param string $cookieName * @return $this */ public function assertCookieNotExpired($cookieName) { PHPUnit::assertNotNull( $cookie = $this->getCookie($cookieName, false), "Cookie [{$cookieName}] not present on response." ); $expiresAt = Carbon::createFromTimestamp($cookie->getExpiresTime()); PHPUnit::assertTrue( $cookie->getExpiresTime() === 0 || $expiresAt->greaterThan(Carbon::now()), "Cookie [{$cookieName}] is expired, it expired at [{$expiresAt}]." ); return $this; } /** * Asserts that the response does not contain the given cookie. * * @param string $cookieName * @return $this */ public function assertCookieMissing($cookieName) { PHPUnit::assertNull( $this->getCookie($cookieName, false), "Cookie [{$cookieName}] is present on response." ); return $this; } /** * Get the given cookie from the response. * * @param string $cookieName * @param bool $decrypt * @param bool $unserialize * @return \Symfony\Component\HttpFoundation\Cookie|null */ public function getCookie($cookieName, $decrypt = true, $unserialize = false) { foreach ($this->headers->getCookies() as $cookie) { if ($cookie->getName() === $cookieName) { if (! $decrypt) { return $cookie; } $decryptedValue = CookieValuePrefix::remove( app('encrypter')->decrypt($cookie->getValue(), $unserialize) ); return new Cookie( $cookie->getName(), $decryptedValue, $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly(), $cookie->isRaw(), $cookie->getSameSite() ); } } } /** * Assert that the given string matches the response content. * * @param string $value * @return $this */ public function assertContent($value) { PHPUnit::assertSame($value, $this->content()); return $this; } /** * Assert that the given string matches the streamed response content. * * @param string $value * @return $this */ public function assertStreamedContent($value) { PHPUnit::assertSame($value, $this->streamedContent()); return $this; } /** * Assert that the given string or array of strings are contained within the response. * * @param string|array $value * @param bool $escape * @return $this */ public function assertSee($value, $escape = true) { $value = Arr::wrap($value); $values = $escape ? array_map('e', $value) : $value; foreach ($values as $value) { PHPUnit::assertStringContainsString((string) $value, $this->getContent()); } return $this; } /** * Assert that the given strings are contained in order within the response. * * @param array $values * @param bool $escape * @return $this */ public function assertSeeInOrder(array $values, $escape = true) { $values = $escape ? array_map('e', $values) : $values; PHPUnit::assertThat($values, new SeeInOrder($this->getContent())); return $this; } /** * Assert that the given string or array of strings are contained within the response text. * * @param string|array $value * @param bool $escape * @return $this */ public function assertSeeText($value, $escape = true) { $value = Arr::wrap($value); $values = $escape ? array_map('e', $value) : $value; $content = strip_tags($this->getContent()); foreach ($values as $value) { PHPUnit::assertStringContainsString((string) $value, $content); } return $this; } /** * Assert that the given strings are contained in order within the response text. * * @param array $values * @param bool $escape * @return $this */ public function assertSeeTextInOrder(array $values, $escape = true) { $values = $escape ? array_map('e', $values) : $values; PHPUnit::assertThat($values, new SeeInOrder(strip_tags($this->getContent()))); return $this; } /** * Assert that the given string or array of strings are not contained within the response. * * @param string|array $value * @param bool $escape * @return $this */ public function assertDontSee($value, $escape = true) { $value = Arr::wrap($value); $values = $escape ? array_map('e', $value) : $value; foreach ($values as $value) { PHPUnit::assertStringNotContainsString((string) $value, $this->getContent()); } return $this; } /** * Assert that the given string or array of strings are not contained within the response text. * * @param string|array $value * @param bool $escape * @return $this */ public function assertDontSeeText($value, $escape = true) { $value = Arr::wrap($value); $values = $escape ? array_map('e', $value) : $value; $content = strip_tags($this->getContent()); foreach ($values as $value) { PHPUnit::assertStringNotContainsString((string) $value, $content); } return $this; } /** * Assert that the response is a superset of the given JSON. * * @param array|callable $value * @param bool $strict * @return $this */ public function assertJson($value, $strict = false) { $json = $this->decodeResponseJson(); if (is_array($value)) { $json->assertSubset($value, $strict); } else { $assert = AssertableJson::fromAssertableJsonString($json); $value($assert); if (Arr::isAssoc($assert->toArray())) { $assert->interacted(); } } return $this; } /** * Assert that the expected value and type exists at the given path in the response. * * @param string $path * @param mixed $expect * @return $this */ public function assertJsonPath($path, $expect) { $this->decodeResponseJson()->assertPath($path, $expect); return $this; } /** * Assert that the given path in the response contains all of the expected values without looking at the order. * * @param string $path * @param array $expect * @return $this */ public function assertJsonPathCanonicalizing($path, array $expect) { $this->decodeResponseJson()->assertPathCanonicalizing($path, $expect); return $this; } /** * Assert that the response has the exact given JSON. * * @param array $data * @return $this */ public function assertExactJson(array $data) { $this->decodeResponseJson()->assertExact($data); return $this; } /** * Assert that the response has the similar JSON as given. * * @param array $data * @return $this */ public function assertSimilarJson(array $data) { $this->decodeResponseJson()->assertSimilar($data); return $this; } /** * Assert that the response contains the given JSON fragment. * * @param array $data * @return $this */ public function assertJsonFragment(array $data) { $this->decodeResponseJson()->assertFragment($data); return $this; } /** * Assert that the response does not contain the given JSON fragment. * * @param array $data * @param bool $exact * @return $this */ public function assertJsonMissing(array $data, $exact = false) { $this->decodeResponseJson()->assertMissing($data, $exact); return $this; } /** * Assert that the response does not contain the exact JSON fragment. * * @param array $data * @return $this */ public function assertJsonMissingExact(array $data) { $this->decodeResponseJson()->assertMissingExact($data); return $this; } /** * Assert that the response does not contain the given path. * * @param string $path * @return $this */ public function assertJsonMissingPath(string $path) { $this->decodeResponseJson()->assertMissingPath($path); return $this; } /** * Assert that the response has a given JSON structure. * * @param array|null $structure * @param array|null $responseData * @return $this */ public function assertJsonStructure(array $structure = null, $responseData = null) { $this->decodeResponseJson()->assertStructure($structure, $responseData); return $this; } /** * Assert that the response JSON has the expected count of items at the given key. * * @param int $count * @param string|null $key * @return $this */ public function assertJsonCount(int $count, $key = null) { $this->decodeResponseJson()->assertCount($count, $key); return $this; } /** * Assert that the response has the given JSON validation errors. * * @param string|array $errors * @param string $responseKey * @return $this */ public function assertJsonValidationErrors($errors, $responseKey = 'errors') { $errors = Arr::wrap($errors); PHPUnit::assertNotEmpty($errors, 'No validation errors were provided.'); $jsonErrors = Arr::get($this->json(), $responseKey) ?? []; $errorMessage = $jsonErrors ? 'Response has the following JSON validation errors:'. PHP_EOL.PHP_EOL.json_encode($jsonErrors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE).PHP_EOL : 'Response does not have JSON validation errors.'; foreach ($errors as $key => $value) { if (is_int($key)) { $this->assertJsonValidationErrorFor($value, $responseKey); continue; } $this->assertJsonValidationErrorFor($key, $responseKey); foreach (Arr::wrap($value) as $expectedMessage) { $errorMissing = true; foreach (Arr::wrap($jsonErrors[$key]) as $jsonErrorMessage) { if (Str::contains($jsonErrorMessage, $expectedMessage)) { $errorMissing = false; break; } } } if ($errorMissing) { PHPUnit::fail( "Failed to find a validation error in the response for key and message: '$key' => '$expectedMessage'".PHP_EOL.PHP_EOL.$errorMessage ); } } return $this; } /** * Assert the response has any JSON validation errors for the given key. * * @param string $key * @param string $responseKey * @return $this */ public function assertJsonValidationErrorFor($key, $responseKey = 'errors') { $jsonErrors = Arr::get($this->json(), $responseKey) ?? []; $errorMessage = $jsonErrors ? 'Response has the following JSON validation errors:'. PHP_EOL.PHP_EOL.json_encode($jsonErrors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE).PHP_EOL : 'Response does not have JSON validation errors.'; PHPUnit::assertArrayHasKey( $key, $jsonErrors, "Failed to find a validation error in the response for key: '{$key}'".PHP_EOL.PHP_EOL.$errorMessage ); return $this; } /** * Assert that the response has no JSON validation errors for the given keys. * * @param string|array|null $keys * @param string $responseKey * @return $this */ public function assertJsonMissingValidationErrors($keys = null, $responseKey = 'errors') { if ($this->getContent() === '') { PHPUnit::assertTrue(true); return $this; } $json = $this->json(); if (! Arr::has($json, $responseKey)) { PHPUnit::assertTrue(true); return $this; } $errors = Arr::get($json, $responseKey, []); if (is_null($keys) && count($errors) > 0) { PHPUnit::fail( 'Response has unexpected validation errors: '.PHP_EOL.PHP_EOL. json_encode($errors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ); } foreach (Arr::wrap($keys) as $key) { PHPUnit::assertFalse( isset($errors[$key]), "Found unexpected validation error for key: '{$key}'" ); } return $this; } /** * Assert that the given key is a JSON array. * * @param string|null $key * @return $this */ public function assertJsonIsArray($key = null) { $data = $this->json($key); $encodedData = json_encode($data); PHPUnit::assertTrue( is_array($data) && str_starts_with($encodedData, '[') && str_ends_with($encodedData, ']') ); return $this; } /** * Assert that the given key is a JSON object. * * @param string|null $key * @return $this */ public function assertJsonIsObject($key = null) { $data = $this->json($key); $encodedData = json_encode($data); PHPUnit::assertTrue( is_array($data) && str_starts_with($encodedData, '{') && str_ends_with($encodedData, '}') ); return $this; } /** * Validate and return the decoded response JSON. * * @return \Illuminate\Testing\AssertableJsonString * * @throws \Throwable */ public function decodeResponseJson() { $testJson = new AssertableJsonString($this->getContent()); $decodedResponse = $testJson->json(); if (is_null($decodedResponse) || $decodedResponse === false) { if ($this->exception) { throw $this->exception; } else { PHPUnit::fail('Invalid JSON was returned from the route.'); } } return $testJson; } /** * Validate and return the decoded response JSON. * * @param string|null $key * @return mixed */ public function json($key = null) { return $this->decodeResponseJson()->json($key); } /** * Get the JSON decoded body of the response as a collection. * * @param string|null $key * @return \Illuminate\Support\Collection */ public function collect($key = null) { return Collection::make($this->json($key)); } /** * Assert that the response view equals the given value. * * @param string $value * @return $this */ public function assertViewIs($value) { $this->ensureResponseHasView(); PHPUnit::assertEquals($value, $this->original->name()); return $this; } /** * Assert that the response view has a given piece of bound data. * * @param string|array $key * @param mixed $value * @return $this */ public function assertViewHas($key, $value = null) { if (is_array($key)) { return $this->assertViewHasAll($key); } $this->ensureResponseHasView(); if (is_null($value)) { PHPUnit::assertTrue(Arr::has($this->original->gatherData(), $key)); } elseif ($value instanceof Closure) { PHPUnit::assertTrue($value(Arr::get($this->original->gatherData(), $key))); } elseif ($value instanceof Model) { PHPUnit::assertTrue($value->is(Arr::get($this->original->gatherData(), $key))); } elseif ($value instanceof EloquentCollection) { $actual = Arr::get($this->original->gatherData(), $key); PHPUnit::assertInstanceOf(EloquentCollection::class, $actual); PHPUnit::assertSameSize($value, $actual); $value->each(fn ($item, $index) => PHPUnit::assertTrue($actual->get($index)->is($item))); } else { PHPUnit::assertEquals($value, Arr::get($this->original->gatherData(), $key)); } return $this; } /** * Assert that the response view has a given list of bound data. * * @param array $bindings * @return $this */ public function assertViewHasAll(array $bindings) { foreach ($bindings as $key => $value) { if (is_int($key)) { $this->assertViewHas($value); } else { $this->assertViewHas($key, $value); } } return $this; } /** * Get a piece of data from the original view. * * @param string $key * @return mixed */ public function viewData($key) { $this->ensureResponseHasView(); return $this->original->gatherData()[$key]; } /** * Assert that the response view is missing a piece of bound data. * * @param string $key * @return $this */ public function assertViewMissing($key) { $this->ensureResponseHasView(); PHPUnit::assertFalse(Arr::has($this->original->gatherData(), $key)); return $this; } /** * Ensure that the response has a view as its original content. * * @return $this */ protected function ensureResponseHasView() { if (! $this->responseHasView()) { return PHPUnit::fail('The response is not a view.'); } return $this; } /** * Determine if the original response is a view. * * @return bool */ protected function responseHasView() { return isset($this->original) && $this->original instanceof View; } /** * Assert that the given keys do not have validation errors. * * @param string|array|null $keys * @param string $errorBag * @param string $responseKey * @return $this */ public function assertValid($keys = null, $errorBag = 'default', $responseKey = 'errors') { if ($this->baseResponse->headers->get('Content-Type') === 'application/json') { return $this->assertJsonMissingValidationErrors($keys, $responseKey); } if ($this->session()->get('errors')) { $errors = $this->session()->get('errors')->getBag($errorBag)->getMessages(); } else { $errors = []; } if (empty($errors)) { PHPUnit::assertTrue(true); return $this; } if (is_null($keys) && count($errors) > 0) { PHPUnit::fail( 'Response has unexpected validation errors: '.PHP_EOL.PHP_EOL. json_encode($errors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ); } foreach (Arr::wrap($keys) as $key) { PHPUnit::assertFalse( isset($errors[$key]), "Found unexpected validation error for key: '{$key}'" ); } return $this; } /** * Assert that the response has the given validation errors. * * @param string|array|null $errors * @param string $errorBag * @param string $responseKey * @return $this */ public function assertInvalid($errors = null, $errorBag = 'default', $responseKey = 'errors') { if ($this->baseResponse->headers->get('Content-Type') === 'application/json') { return $this->assertJsonValidationErrors($errors, $responseKey); } $this->assertSessionHas('errors'); $sessionErrors = $this->session()->get('errors')->getBag($errorBag)->getMessages(); $errorMessage = $sessionErrors ? 'Response has the following validation errors in the session:'. PHP_EOL.PHP_EOL.json_encode($sessionErrors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE).PHP_EOL : 'Response does not have validation errors in the session.'; foreach (Arr::wrap($errors) as $key => $value) { PHPUnit::assertArrayHasKey( (is_int($key)) ? $value : $key, $sessionErrors, "Failed to find a validation error in session for key: '{$value}'".PHP_EOL.PHP_EOL.$errorMessage ); if (! is_int($key)) { $hasError = false; foreach (Arr::wrap($sessionErrors[$key]) as $sessionErrorMessage) { if (Str::contains($sessionErrorMessage, $value)) { $hasError = true; break; } } if (! $hasError) { PHPUnit::fail( "Failed to find a validation error for key and message: '$key' => '$value'".PHP_EOL.PHP_EOL.$errorMessage ); } } } return $this; } /** * Assert that the session has a given value. * * @param string|array $key * @param mixed $value * @return $this */ public function assertSessionHas($key, $value = null) { if (is_array($key)) { return $this->assertSessionHasAll($key); } if (is_null($value)) { PHPUnit::assertTrue( $this->session()->has($key), "Session is missing expected key [{$key}]." ); } elseif ($value instanceof Closure) { PHPUnit::assertTrue($value($this->session()->get($key))); } else { PHPUnit::assertEquals($value, $this->session()->get($key)); } return $this; } /** * Assert that the session has a given list of values. * * @param array $bindings * @return $this */ public function assertSessionHasAll(array $bindings) { foreach ($bindings as $key => $value) { if (is_int($key)) { $this->assertSessionHas($value); } else { $this->assertSessionHas($key, $value); } } return $this; } /** * Assert that the session has a given value in the flashed input array. * * @param string|array $key * @param mixed $value * @return $this */ public function assertSessionHasInput($key, $value = null) { if (is_array($key)) { foreach ($key as $k => $v) { if (is_int($k)) { $this->assertSessionHasInput($v); } else { $this->assertSessionHasInput($k, $v); } } return $this; } if (is_null($value)) { PHPUnit::assertTrue( $this->session()->hasOldInput($key), "Session is missing expected key [{$key}]." ); } elseif ($value instanceof Closure) { PHPUnit::assertTrue($value($this->session()->getOldInput($key))); } else { PHPUnit::assertEquals($value, $this->session()->getOldInput($key)); } return $this; } /** * Assert that the session has the given errors. * * @param string|array $keys * @param mixed $format * @param string $errorBag * @return $this */ public function assertSessionHasErrors($keys = [], $format = null, $errorBag = 'default') { $this->assertSessionHas('errors'); $keys = (array) $keys; $errors = $this->session()->get('errors')->getBag($errorBag); foreach ($keys as $key => $value) { if (is_int($key)) { PHPUnit::assertTrue($errors->has($value), "Session missing error: $value"); } else { PHPUnit::assertContains(is_bool($value) ? (string) $value : $value, $errors->get($key, $format)); } } return $this; } /** * Assert that the session is missing the given errors. * * @param string|array $keys * @param string|null $format * @param string $errorBag * @return $this */ public function assertSessionDoesntHaveErrors($keys = [], $format = null, $errorBag = 'default') { $keys = (array) $keys; if (empty($keys)) { return $this->assertSessionHasNoErrors(); } if (is_null($this->session()->get('errors'))) { PHPUnit::assertTrue(true); return $this; } $errors = $this->session()->get('errors')->getBag($errorBag); foreach ($keys as $key => $value) { if (is_int($key)) { PHPUnit::assertFalse($errors->has($value), "Session has unexpected error: $value"); } else { PHPUnit::assertNotContains($value, $errors->get($key, $format)); } } return $this; } /** * Assert that the session has no errors. * * @return $this */ public function assertSessionHasNoErrors() { $hasErrors = $this->session()->has('errors'); PHPUnit::assertFalse( $hasErrors, 'Session has unexpected errors: '.PHP_EOL.PHP_EOL. json_encode((function () use ($hasErrors) { $errors = []; $sessionErrors = $this->session()->get('errors'); if ($hasErrors && is_a($sessionErrors, ViewErrorBag::class)) { foreach ($sessionErrors->getBags() as $bag => $messages) { if (is_a($messages, MessageBag::class)) { $errors[$bag] = $messages->all(); } } } return $errors; })(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), ); return $this; } /** * Assert that the session has the given errors. * * @param string $errorBag * @param string|array $keys * @param mixed $format * @return $this */ public function assertSessionHasErrorsIn($errorBag, $keys = [], $format = null) { return $this->assertSessionHasErrors($keys, $format, $errorBag); } /** * Assert that the session does not have a given key. * * @param string|array $key * @return $this */ public function assertSessionMissing($key) { if (is_array($key)) { foreach ($key as $value) { $this->assertSessionMissing($value); } } else { PHPUnit::assertFalse( $this->session()->has($key), "Session has unexpected key [{$key}]." ); } return $this; } /** * Get the current session store. * * @return \Illuminate\Session\Store */ protected function session() { $session = app('session.store'); if (! $session->isStarted()) { $session->start(); } return $session; } /** * Dump the content from the response and end the script. * * @return never */ public function dd() { $this->dump(); exit(1); } /** * Dump the headers from the response and end the script. * * @return never */ public function ddHeaders() { $this->dumpHeaders(); exit(1); } /** * Dump the session from the response and end the script. * * @param string|array $keys * @return never */ public function ddSession($keys = []) { $this->dumpSession($keys); exit(1); } /** * Dump the content from the response. * * @param string|null $key * @return $this */ public function dump($key = null) { $content = $this->getContent(); $json = json_decode($content); if (json_last_error() === JSON_ERROR_NONE) { $content = $json; } if (! is_null($key)) { dump(data_get($content, $key)); } else { dump($content); } return $this; } /** * Dump the headers from the response. * * @return $this */ public function dumpHeaders() { dump($this->headers->all()); return $this; } /** * Dump the session from the response. * * @param string|array $keys * @return $this */ public function dumpSession($keys = []) { $keys = (array) $keys; if (empty($keys)) { dump($this->session()->all()); } else { dump($this->session()->only($keys)); } return $this; } /** * Get the streamed content from the response. * * @return string */ public function streamedContent() { if (! is_null($this->streamedContent)) { return $this->streamedContent; } if (! $this->baseResponse instanceof StreamedResponse) { PHPUnit::fail('The response is not a streamed response.'); } ob_start(function (string $buffer): string { $this->streamedContent .= $buffer; return ''; }); $this->sendContent(); ob_end_clean(); return $this->streamedContent; } /** * Set the previous exceptions on the response. * * @param \Illuminate\Support\Collection $exceptions * @return $this */ public function withExceptions(Collection $exceptions) { $this->exceptions = $exceptions; return $this; } /** * This method is called when test method did not execute successfully. * * @param \Throwable $exception * @return \Throwable */ public function transformNotSuccessfulException($exception) { if (! $exception instanceof ExpectationFailedException) { return $exception; } if ($lastException = $this->exceptions->last()) { return $this->appendExceptionToException($lastException, $exception); } if ($this->baseResponse instanceof RedirectResponse) { $session = $this->baseResponse->getSession(); if (! is_null($session) && $session->has('errors')) { return $this->appendErrorsToException($session->get('errors')->all(), $exception); } } if ($this->baseResponse->headers->get('Content-Type') === 'application/json') { $testJson = new AssertableJsonString($this->getContent()); if (isset($testJson['errors'])) { return $this->appendErrorsToException($testJson->json(), $exception, true); } } return $exception; } /** * Append an exception to the message of another exception. * * @param \Throwable $exceptionToAppend * @param \Throwable $exception * @return \Throwable */ protected function appendExceptionToException($exceptionToAppend, $exception) { $exceptionMessage = $exceptionToAppend->getMessage(); $exceptionToAppend = (string) $exceptionToAppend; $message = <<<"EOF" The following exception occurred during the last request: $exceptionToAppend ---------------------------------------------------------------------------------- $exceptionMessage EOF; return $this->appendMessageToException($message, $exception); } /** * Append errors to an exception message. * * @param array $errors * @param \Throwable $exception * @param bool $json * @return \Throwable */ protected function appendErrorsToException($errors, $exception, $json = false) { $errors = $json ? json_encode($errors, JSON_PRETTY_PRINT) : implode(PHP_EOL, Arr::flatten($errors)); // JSON error messages may already contain the errors, so we shouldn't duplicate them... if (str_contains($exception->getMessage(), $errors)) { return $exception; } $message = <<<"EOF" The following errors occurred during the last request: $errors EOF; return $this->appendMessageToException($message, $exception); } /** * Append a message to an exception. * * @param string $message * @param \Throwable $exception * @return \Throwable */ protected function appendMessageToException($message, $exception) { $property = new ReflectionProperty($exception, 'message'); $property->setValue( $exception, $exception->getMessage().PHP_EOL.PHP_EOL.$message.PHP_EOL ); return $exception; } /** * Dynamically access base response parameters. * * @param string $key * @return mixed */ public function __get($key) { return $this->baseResponse->{$key}; } /** * Proxy isset() checks to the underlying base response. * * @param string $key * @return bool */ public function __isset($key) { return isset($this->baseResponse->{$key}); } /** * Determine if the given offset exists. * * @param string $offset * @return bool */ public function offsetExists($offset): bool { return $this->responseHasView() ? isset($this->original->gatherData()[$offset]) : isset($this->json()[$offset]); } /** * Get the value for a given offset. * * @param string $offset * @return mixed */ public function offsetGet($offset): mixed { return $this->responseHasView() ? $this->viewData($offset) : $this->json()[$offset]; } /** * Set the value at the given offset. * * @param string $offset * @param mixed $value * @return void * * @throws \LogicException */ public function offsetSet($offset, $value): void { throw new LogicException('Response data may not be mutated using array access.'); } /** * Unset the value at the given offset. * * @param string $offset * @return void * * @throws \LogicException */ public function offsetUnset($offset): void { throw new LogicException('Response data may not be mutated using array access.'); } /** * Handle dynamic calls into macros or pass missing methods to the base response. * * @param string $method * @param array $args * @return mixed */ public function __call($method, $args) { if (static::hasMacro($method)) { return $this->macroCall($method, $args); } return $this->baseResponse->{$method}(...$args); } } src/Illuminate/Testing/ParallelRunner.php 0000644 00000001341 15107327040 0014510 0 ustar 00 <?php namespace Illuminate\Testing; use Illuminate\Testing\Concerns\RunsInParallel; if (interface_exists(\ParaTest\RunnerInterface::class)) { class ParallelRunner implements \ParaTest\RunnerInterface { use RunsInParallel; /** * Runs the test suite. * * @return int */ public function run(): int { return $this->execute(); } } } else { class ParallelRunner implements \ParaTest\Runners\PHPUnit\RunnerInterface { use RunsInParallel; /** * Runs the test suite. * * @return void */ public function run(): void { $this->execute(); } } } src/Illuminate/Testing/TestComponent.php 0000644 00000007607 15107327040 0014377 0 ustar 00 <?php namespace Illuminate\Testing; use Illuminate\Testing\Assert as PHPUnit; use Illuminate\Testing\Constraints\SeeInOrder; class TestComponent { /** * The original component. * * @var \Illuminate\View\Component */ public $component; /** * The rendered component contents. * * @var string */ protected $rendered; /** * Create a new test component instance. * * @param \Illuminate\View\Component $component * @param \Illuminate\View\View $view * @return void */ public function __construct($component, $view) { $this->component = $component; $this->rendered = $view->render(); } /** * Assert that the given string is contained within the rendered component. * * @param string $value * @param bool $escape * @return $this */ public function assertSee($value, $escape = true) { $value = $escape ? e($value) : $value; PHPUnit::assertStringContainsString((string) $value, $this->rendered); return $this; } /** * Assert that the given strings are contained in order within the rendered component. * * @param array $values * @param bool $escape * @return $this */ public function assertSeeInOrder(array $values, $escape = true) { $values = $escape ? array_map('e', $values) : $values; PHPUnit::assertThat($values, new SeeInOrder($this->rendered)); return $this; } /** * Assert that the given string is contained within the rendered component text. * * @param string $value * @param bool $escape * @return $this */ public function assertSeeText($value, $escape = true) { $value = $escape ? e($value) : $value; PHPUnit::assertStringContainsString((string) $value, strip_tags($this->rendered)); return $this; } /** * Assert that the given strings are contained in order within the rendered component text. * * @param array $values * @param bool $escape * @return $this */ public function assertSeeTextInOrder(array $values, $escape = true) { $values = $escape ? array_map('e', $values) : $values; PHPUnit::assertThat($values, new SeeInOrder(strip_tags($this->rendered))); return $this; } /** * Assert that the given string is not contained within the rendered component. * * @param string $value * @param bool $escape * @return $this */ public function assertDontSee($value, $escape = true) { $value = $escape ? e($value) : $value; PHPUnit::assertStringNotContainsString((string) $value, $this->rendered); return $this; } /** * Assert that the given string is not contained within the rendered component text. * * @param string $value * @param bool $escape * @return $this */ public function assertDontSeeText($value, $escape = true) { $value = $escape ? e($value) : $value; PHPUnit::assertStringNotContainsString((string) $value, strip_tags($this->rendered)); return $this; } /** * Get the string contents of the rendered component. * * @return string */ public function __toString() { return $this->rendered; } /** * Dynamically access properties on the underlying component. * * @param string $attribute * @return mixed */ public function __get($attribute) { return $this->component->{$attribute}; } /** * Dynamically call methods on the underlying component. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->component->{$method}(...$parameters); } } src/Illuminate/Testing/Exceptions/InvalidArgumentException.php 0000644 00000001501 15107327040 0020651 0 ustar 00 <?php namespace Illuminate\Testing\Exceptions; use PHPUnit\Framework\Exception; class InvalidArgumentException extends Exception { /** * Creates a new exception for an invalid argument. * * @return static */ public static function create(int $argument, string $type): static { $stack = debug_backtrace(); $function = $stack[1]['function']; if (isset($stack[1]['class'])) { $function = sprintf('%s::%s', $stack[1]['class'], $stack[1]['function']); } return new static( sprintf( 'Argument #%d of %s() must be %s %s', $argument, $function, in_array(lcfirst($type)[0], ['a', 'e', 'i', 'o', 'u'], true) ? 'an' : 'a', $type ) ); } } src/Illuminate/Testing/Assert.php 0000644 00000002222 15107327040 0013022 0 ustar 00 <?php namespace Illuminate\Testing; use ArrayAccess; use Illuminate\Testing\Constraints\ArraySubset; use Illuminate\Testing\Exceptions\InvalidArgumentException; use PHPUnit\Framework\Assert as PHPUnit; /** * @internal This class is not meant to be used or overwritten outside the framework itself. */ abstract class Assert extends PHPUnit { /** * Asserts that an array has a specified subset. * * @param \ArrayAccess|array $subset * @param \ArrayAccess|array $array * @param bool $checkForIdentity * @param string $msg * @return void */ public static function assertArraySubset($subset, $array, bool $checkForIdentity = false, string $msg = ''): void { if (! (is_array($subset) || $subset instanceof ArrayAccess)) { throw InvalidArgumentException::create(1, 'array or ArrayAccess'); } if (! (is_array($array) || $array instanceof ArrayAccess)) { throw InvalidArgumentException::create(2, 'array or ArrayAccess'); } $constraint = new ArraySubset($subset, $checkForIdentity); PHPUnit::assertThat($array, $constraint, $msg); } } src/Illuminate/Testing/Concerns/AssertsStatusCodes.php 0000644 00000012275 15107327040 0017152 0 ustar 00 <?php namespace Illuminate\Testing\Concerns; use Illuminate\Testing\Assert as PHPUnit; trait AssertsStatusCodes { /** * Assert that the response has a 200 "OK" status code. * * @return $this */ public function assertOk() { return $this->assertStatus(200); } /** * Assert that the response has a 201 "Created" status code. * * @return $this */ public function assertCreated() { return $this->assertStatus(201); } /** * Assert that the response has a 202 "Accepted" status code. * * @return $this */ public function assertAccepted() { return $this->assertStatus(202); } /** * Assert that the response has the given status code and no content. * * @param int $status * @return $this */ public function assertNoContent($status = 204) { $this->assertStatus($status); PHPUnit::assertEmpty($this->getContent(), 'Response content is not empty.'); return $this; } /** * Assert that the response has a 301 "Moved Permanently" status code. * * @return $this */ public function assertMovedPermanently() { return $this->assertStatus(301); } /** * Assert that the response has a 302 "Found" status code. * * @return $this */ public function assertFound() { return $this->assertStatus(302); } /** * Assert that the response has a 304 "Not Modified" status code. * * @return $this */ public function assertNotModified() { return $this->assertStatus(304); } /** * Assert that the response has a 307 "Temporary Redirect" status code. * * @return $this */ public function assertTemporaryRedirect() { return $this->assertStatus(307); } /** * Assert that the response has a 308 "Permanent Redirect" status code. * * @return $this */ public function assertPermanentRedirect() { return $this->assertStatus(308); } /** * Assert that the response has a 400 "Bad Request" status code. * * @return $this */ public function assertBadRequest() { return $this->assertStatus(400); } /** * Assert that the response has a 401 "Unauthorized" status code. * * @return $this */ public function assertUnauthorized() { return $this->assertStatus(401); } /** * Assert that the response has a 402 "Payment Required" status code. * * @return $this */ public function assertPaymentRequired() { return $this->assertStatus(402); } /** * Assert that the response has a 403 "Forbidden" status code. * * @return $this */ public function assertForbidden() { return $this->assertStatus(403); } /** * Assert that the response has a 404 "Not Found" status code. * * @return $this */ public function assertNotFound() { return $this->assertStatus(404); } /** * Assert that the response has a 405 "Method Not Allowed" status code. * * @return $this */ public function assertMethodNotAllowed() { return $this->assertStatus(405); } /** * Assert that the response has a 406 "Not Acceptable" status code. * * @return $this */ public function assertNotAcceptable() { return $this->assertStatus(406); } /** * Assert that the response has a 408 "Request Timeout" status code. * * @return $this */ public function assertRequestTimeout() { return $this->assertStatus(408); } /** * Assert that the response has a 409 "Conflict" status code. * * @return $this */ public function assertConflict() { return $this->assertStatus(409); } /** * Assert that the response has a 410 "Gone" status code. * * @return $this */ public function assertGone() { return $this->assertStatus(410); } /** * Assert that the response has a 415 "Unsupported Media Type" status code. * * @return $this */ public function assertUnsupportedMediaType() { return $this->assertStatus(415); } /** * Assert that the response has a 422 "Unprocessable Entity" status code. * * @return $this */ public function assertUnprocessable() { return $this->assertStatus(422); } /** * Assert that the response has a 429 "Too Many Requests" status code. * * @return $this */ public function assertTooManyRequests() { return $this->assertStatus(429); } /** * Assert that the response has a 500 "Internal Server Error" status code. * * @return $this */ public function assertInternalServerError() { return $this->assertStatus(500); } /** * Assert that the response has a 503 "Service Unavailable" status code. * * @return $this */ public function assertServiceUnavailable() { return $this->assertStatus(503); } } src/Illuminate/Testing/Concerns/TestDatabases.php 0000644 00000012237 15107327040 0016071 0 ustar 00 <?php namespace Illuminate\Testing\Concerns; use Illuminate\Database\QueryException; use Illuminate\Foundation\Testing; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\ParallelTesting; use Illuminate\Support\Facades\Schema; trait TestDatabases { /** * Indicates if the test database schema is up to date. * * @var bool */ protected static $schemaIsUpToDate = false; /** * Boot a test database. * * @return void */ protected function bootTestDatabase() { ParallelTesting::setUpProcess(function () { $this->whenNotUsingInMemoryDatabase(function ($database) { if (ParallelTesting::option('recreate_databases')) { Schema::dropDatabaseIfExists( $this->testDatabase($database) ); } }); }); ParallelTesting::setUpTestCase(function ($testCase) { $uses = array_flip(class_uses_recursive(get_class($testCase))); $databaseTraits = [ Testing\DatabaseMigrations::class, Testing\DatabaseTransactions::class, Testing\DatabaseTruncation::class, Testing\RefreshDatabase::class, ]; if (Arr::hasAny($uses, $databaseTraits) && ! ParallelTesting::option('without_databases')) { $this->whenNotUsingInMemoryDatabase(function ($database) use ($uses) { [$testDatabase, $created] = $this->ensureTestDatabaseExists($database); $this->switchToDatabase($testDatabase); if (isset($uses[Testing\DatabaseTransactions::class])) { $this->ensureSchemaIsUpToDate(); } if ($created) { ParallelTesting::callSetUpTestDatabaseCallbacks($testDatabase); } }); } }); ParallelTesting::tearDownProcess(function () { $this->whenNotUsingInMemoryDatabase(function ($database) { if (ParallelTesting::option('drop_databases')) { Schema::dropDatabaseIfExists( $this->testDatabase($database) ); } }); }); } /** * Ensure a test database exists and returns its name. * * @param string $database * @return array */ protected function ensureTestDatabaseExists($database) { $testDatabase = $this->testDatabase($database); try { $this->usingDatabase($testDatabase, function () { Schema::hasTable('dummy'); }); } catch (QueryException) { $this->usingDatabase($database, function () use ($testDatabase) { Schema::dropDatabaseIfExists($testDatabase); Schema::createDatabase($testDatabase); }); return [$testDatabase, true]; } return [$testDatabase, false]; } /** * Ensure the current database test schema is up to date. * * @return void */ protected function ensureSchemaIsUpToDate() { if (! static::$schemaIsUpToDate) { Artisan::call('migrate'); static::$schemaIsUpToDate = true; } } /** * Runs the given callable using the given database. * * @param string $database * @param callable $callable * @return void */ protected function usingDatabase($database, $callable) { $original = DB::getConfig('database'); try { $this->switchToDatabase($database); $callable(); } finally { $this->switchToDatabase($original); } } /** * Apply the given callback when tests are not using in memory database. * * @param callable $callback * @return void */ protected function whenNotUsingInMemoryDatabase($callback) { if (ParallelTesting::option('without_databases')) { return; } $database = DB::getConfig('database'); if ($database !== ':memory:') { $callback($database); } } /** * Switch to the given database. * * @param string $database * @return void */ protected function switchToDatabase($database) { DB::purge(); $default = config('database.default'); $url = config("database.connections.{$default}.url"); if ($url) { config()->set( "database.connections.{$default}.url", preg_replace('/^(.*)(\/[\w-]*)(\??.*)$/', "$1/{$database}$3", $url), ); } else { config()->set( "database.connections.{$default}.database", $database, ); } } /** * Returns the test database name. * * @return string */ protected function testDatabase($database) { $token = ParallelTesting::token(); return "{$database}_test_{$token}"; } } src/Illuminate/Testing/Concerns/RunsInParallel.php 0000644 00000012341 15107327040 0016231 0 ustar 00 <?php namespace Illuminate\Testing\Concerns; use Illuminate\Contracts\Console\Kernel; use Illuminate\Support\Facades\ParallelTesting; use Illuminate\Testing\ParallelConsoleOutput; use RuntimeException; use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\OutputInterface; trait RunsInParallel { /** * The application resolver callback. * * @var \Closure|null */ protected static $applicationResolver; /** * The runner resolver callback. * * @var \Closure|null */ protected static $runnerResolver; /** * The original test runner options. * * @var \ParaTest\Runners\PHPUnit\Options|\ParaTest\Options */ protected $options; /** * The output instance. * * @var \Symfony\Component\Console\Output\OutputInterface */ protected $output; /** * The original test runner. * * @var \ParaTest\Runners\PHPUnit\RunnerInterface|\ParaTest\RunnerInterface */ protected $runner; /** * Creates a new test runner instance. * * @param \ParaTest\Runners\PHPUnit\Options|\ParaTest\Options $options * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ public function __construct($options, OutputInterface $output) { $this->options = $options; if ($output instanceof ConsoleOutput) { $output = new ParallelConsoleOutput($output); } $runnerResolver = static::$runnerResolver ?: function ($options, OutputInterface $output) { $wrapperRunnerClass = class_exists(\ParaTest\WrapperRunner\WrapperRunner::class) ? \ParaTest\WrapperRunner\WrapperRunner::class : \ParaTest\Runners\PHPUnit\WrapperRunner::class; return new $wrapperRunnerClass($options, $output); }; $this->runner = $runnerResolver($options, $output); } /** * Set the application resolver callback. * * @param \Closure|null $resolver * @return void */ public static function resolveApplicationUsing($resolver) { static::$applicationResolver = $resolver; } /** * Set the runner resolver callback. * * @param \Closure|null $resolver * @return void */ public static function resolveRunnerUsing($resolver) { static::$runnerResolver = $resolver; } /** * Runs the test suite. * * @return int */ public function execute(): int { $phpHandlerClass = class_exists(\PHPUnit\TextUI\Configuration\PhpHandler::class) ? \PHPUnit\TextUI\Configuration\PhpHandler::class : \PHPUnit\TextUI\XmlConfiguration\PhpHandler::class; $configuration = $this->options instanceof \ParaTest\Options ? $this->options->configuration : $this->options->configuration(); (new $phpHandlerClass)->handle($configuration->php()); $this->forEachProcess(function () { ParallelTesting::callSetUpProcessCallbacks(); }); try { $potentialExitCode = $this->runner->run(); } finally { $this->forEachProcess(function () { ParallelTesting::callTearDownProcessCallbacks(); }); } return $potentialExitCode === null ? $this->getExitCode() : $potentialExitCode; } /** * Returns the highest exit code encountered throughout the course of test execution. * * @return int */ public function getExitCode(): int { return $this->runner->getExitCode(); } /** * Apply the given callback for each process. * * @param callable $callback * @return void */ protected function forEachProcess($callback) { $processes = $this->options instanceof \ParaTest\Options ? $this->options->processes : $this->options->processes(); collect(range(1, $processes))->each(function ($token) use ($callback) { tap($this->createApplication(), function ($app) use ($callback, $token) { ParallelTesting::resolveTokenUsing(fn () => $token); $callback($app); })->flush(); }); } /** * Creates the application. * * @return \Illuminate\Contracts\Foundation\Application * * @throws \RuntimeException */ protected function createApplication() { $applicationResolver = static::$applicationResolver ?: function () { if (trait_exists(\Tests\CreatesApplication::class)) { $applicationCreator = new class { use \Tests\CreatesApplication; }; return $applicationCreator->createApplication(); } elseif (file_exists($path = getcwd().'/bootstrap/app.php') || file_exists($path = getcwd().'/.laravel/app.php')) { $app = require $path; $app->make(Kernel::class)->bootstrap(); return $app; } throw new RuntimeException('Parallel Runner unable to resolve application.'); }; return $applicationResolver(); } } src/Illuminate/Testing/PendingCommand.php 0000644 00000031622 15107327040 0014452 0 ustar 00 <?php namespace Illuminate\Testing; use Illuminate\Console\OutputStyle; use Illuminate\Contracts\Console\Kernel; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Arr; use Mockery; use Mockery\Exception\NoMatchingExpectationException; use PHPUnit\Framework\TestCase as PHPUnitTestCase; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\BufferedOutput; class PendingCommand { /** * The test being run. * * @var \Illuminate\Foundation\Testing\TestCase */ public $test; /** * The application instance. * * @var \Illuminate\Contracts\Container\Container */ protected $app; /** * The command to run. * * @var string */ protected $command; /** * The parameters to pass to the command. * * @var array */ protected $parameters; /** * The expected exit code. * * @var int */ protected $expectedExitCode; /** * The unexpected exit code. * * @var int */ protected $unexpectedExitCode; /** * Determine if the command has executed. * * @var bool */ protected $hasExecuted = false; /** * Create a new pending console command run. * * @param \PHPUnit\Framework\TestCase $test * @param \Illuminate\Contracts\Container\Container $app * @param string $command * @param array $parameters * @return void */ public function __construct(PHPUnitTestCase $test, Container $app, $command, $parameters) { $this->app = $app; $this->test = $test; $this->command = $command; $this->parameters = $parameters; } /** * Specify an expected question that will be asked when the command runs. * * @param string $question * @param string|bool $answer * @return $this */ public function expectsQuestion($question, $answer) { $this->test->expectedQuestions[] = [$question, $answer]; return $this; } /** * Specify an expected confirmation question that will be asked when the command runs. * * @param string $question * @param string $answer * @return $this */ public function expectsConfirmation($question, $answer = 'no') { return $this->expectsQuestion($question, strtolower($answer) === 'yes'); } /** * Specify an expected choice question with expected answers that will be asked/shown when the command runs. * * @param string $question * @param string|array $answer * @param array $answers * @param bool $strict * @return $this */ public function expectsChoice($question, $answer, $answers, $strict = false) { $this->test->expectedChoices[$question] = [ 'expected' => $answers, 'strict' => $strict, ]; return $this->expectsQuestion($question, $answer); } /** * Specify output that should be printed when the command runs. * * @param string $output * @return $this */ public function expectsOutput($output) { $this->test->expectedOutput[] = $output; return $this; } /** * Specify output that should never be printed when the command runs. * * @param string $output * @return $this */ public function doesntExpectOutput($output) { $this->test->unexpectedOutput[$output] = false; return $this; } /** * Specify that the given string should be contained in the command output. * * @param string $string * @return $this */ public function expectsOutputToContain($string) { $this->test->expectedOutputSubstrings[] = $string; return $this; } /** * Specify that the given string shouldn't be contained in the command output. * * @param string $string * @return $this */ public function doesntExpectOutputToContain($string) { $this->test->unexpectedOutputSubstrings[$string] = false; return $this; } /** * Specify a table that should be printed when the command runs. * * @param array $headers * @param \Illuminate\Contracts\Support\Arrayable|array $rows * @param string $tableStyle * @param array $columnStyles * @return $this */ public function expectsTable($headers, $rows, $tableStyle = 'default', array $columnStyles = []) { $table = (new Table($output = new BufferedOutput)) ->setHeaders((array) $headers) ->setRows($rows instanceof Arrayable ? $rows->toArray() : $rows) ->setStyle($tableStyle); foreach ($columnStyles as $columnIndex => $columnStyle) { $table->setColumnStyle($columnIndex, $columnStyle); } $table->render(); $lines = array_filter( explode(PHP_EOL, $output->fetch()) ); foreach ($lines as $line) { $this->expectsOutput($line); } return $this; } /** * Assert that the command has the given exit code. * * @param int $exitCode * @return $this */ public function assertExitCode($exitCode) { $this->expectedExitCode = $exitCode; return $this; } /** * Assert that the command does not have the given exit code. * * @param int $exitCode * @return $this */ public function assertNotExitCode($exitCode) { $this->unexpectedExitCode = $exitCode; return $this; } /** * Assert that the command has the success exit code. * * @return $this */ public function assertSuccessful() { return $this->assertExitCode(Command::SUCCESS); } /** * Assert that the command has the success exit code. * * @return $this */ public function assertOk() { return $this->assertSuccessful(); } /** * Assert that the command does not have the success exit code. * * @return $this */ public function assertFailed() { return $this->assertNotExitCode(Command::SUCCESS); } /** * Execute the command. * * @return int */ public function execute() { return $this->run(); } /** * Execute the command. * * @return int * * @throws \Mockery\Exception\NoMatchingExpectationException */ public function run() { $this->hasExecuted = true; $mock = $this->mockConsoleOutput(); try { $exitCode = $this->app->make(Kernel::class)->call($this->command, $this->parameters, $mock); } catch (NoMatchingExpectationException $e) { if ($e->getMethodName() === 'askQuestion') { $this->test->fail('Unexpected question "'.$e->getActualArguments()[0]->getQuestion().'" was asked.'); } throw $e; } if ($this->expectedExitCode !== null) { $this->test->assertEquals( $this->expectedExitCode, $exitCode, "Expected status code {$this->expectedExitCode} but received {$exitCode}." ); } elseif (! is_null($this->unexpectedExitCode)) { $this->test->assertNotEquals( $this->unexpectedExitCode, $exitCode, "Unexpected status code {$this->unexpectedExitCode} was received." ); } $this->verifyExpectations(); $this->flushExpectations(); return $exitCode; } /** * Determine if expected questions / choices / outputs are fulfilled. * * @return void */ protected function verifyExpectations() { if (count($this->test->expectedQuestions)) { $this->test->fail('Question "'.Arr::first($this->test->expectedQuestions)[0].'" was not asked.'); } if (count($this->test->expectedChoices) > 0) { foreach ($this->test->expectedChoices as $question => $answers) { $assertion = $answers['strict'] ? 'assertEquals' : 'assertEqualsCanonicalizing'; $this->test->{$assertion}( $answers['expected'], $answers['actual'], 'Question "'.$question.'" has different options.' ); } } if (count($this->test->expectedOutput)) { $this->test->fail('Output "'.Arr::first($this->test->expectedOutput).'" was not printed.'); } if (count($this->test->expectedOutputSubstrings)) { $this->test->fail('Output does not contain "'.Arr::first($this->test->expectedOutputSubstrings).'".'); } if ($output = array_search(true, $this->test->unexpectedOutput)) { $this->test->fail('Output "'.$output.'" was printed.'); } if ($output = array_search(true, $this->test->unexpectedOutputSubstrings)) { $this->test->fail('Output "'.$output.'" was printed.'); } } /** * Mock the application's console output. * * @return \Mockery\MockInterface */ protected function mockConsoleOutput() { $mock = Mockery::mock(OutputStyle::class.'[askQuestion]', [ new ArrayInput($this->parameters), $this->createABufferedOutputMock(), ]); foreach ($this->test->expectedQuestions as $i => $question) { $mock->shouldReceive('askQuestion') ->once() ->ordered() ->with(Mockery::on(function ($argument) use ($question) { if (isset($this->test->expectedChoices[$question[0]])) { $this->test->expectedChoices[$question[0]]['actual'] = $argument->getAutocompleterValues(); } return $argument->getQuestion() == $question[0]; })) ->andReturnUsing(function () use ($question, $i) { unset($this->test->expectedQuestions[$i]); return $question[1]; }); } $this->app->bind(OutputStyle::class, function () use ($mock) { return $mock; }); return $mock; } /** * Create a mock for the buffered output. * * @return \Mockery\MockInterface */ private function createABufferedOutputMock() { $mock = Mockery::mock(BufferedOutput::class.'[doWrite]') ->shouldAllowMockingProtectedMethods() ->shouldIgnoreMissing(); foreach ($this->test->expectedOutput as $i => $output) { $mock->shouldReceive('doWrite') ->once() ->ordered() ->with($output, Mockery::any()) ->andReturnUsing(function () use ($i) { unset($this->test->expectedOutput[$i]); }); } foreach ($this->test->expectedOutputSubstrings as $i => $text) { $mock->shouldReceive('doWrite') ->atLeast() ->times(0) ->withArgs(fn ($output) => str_contains($output, $text)) ->andReturnUsing(function () use ($i) { unset($this->test->expectedOutputSubstrings[$i]); }); } foreach ($this->test->unexpectedOutput as $output => $displayed) { $mock->shouldReceive('doWrite') ->atLeast() ->times(0) ->ordered() ->with($output, Mockery::any()) ->andReturnUsing(function () use ($output) { $this->test->unexpectedOutput[$output] = true; }); } foreach ($this->test->unexpectedOutputSubstrings as $text => $displayed) { $mock->shouldReceive('doWrite') ->atLeast() ->times(0) ->withArgs(fn ($output) => str_contains($output, $text)) ->andReturnUsing(function () use ($text) { $this->test->unexpectedOutputSubstrings[$text] = true; }); } return $mock; } /** * Flush the expectations from the test case. * * @return void */ protected function flushExpectations() { $this->test->expectedOutput = []; $this->test->expectedOutputSubstrings = []; $this->test->unexpectedOutput = []; $this->test->unexpectedOutputSubstrings = []; $this->test->expectedTables = []; $this->test->expectedQuestions = []; $this->test->expectedChoices = []; } /** * Handle the object's destruction. * * @return void */ public function __destruct() { if ($this->hasExecuted) { return; } $this->run(); } } src/Illuminate/Testing/ParallelConsoleOutput.php 0000644 00000002565 15107327040 0016073 0 ustar 00 <?php namespace Illuminate\Testing; use Illuminate\Support\Str; use Symfony\Component\Console\Output\ConsoleOutput; class ParallelConsoleOutput extends ConsoleOutput { /** * The original output instance. * * @var \Symfony\Component\Console\Output\OutputInterface */ protected $output; /** * The output that should be ignored. * * @var array */ protected $ignore = [ 'Running phpunit in', 'Configuration read from', ]; /** * Create a new Parallel ConsoleOutput instance. * * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ public function __construct($output) { parent::__construct( $output->getVerbosity(), $output->isDecorated(), $output->getFormatter(), ); $this->output = $output; } /** * Writes a message to the output. * * @param string|iterable $messages * @param bool $newline * @param int $options * @return void */ public function write($messages, bool $newline = false, int $options = 0) { $messages = collect($messages)->filter(function ($message) { return ! Str::contains($message, $this->ignore); }); $this->output->write($messages->toArray(), $newline, $options); } } src/Illuminate/Testing/LoggedExceptionCollection.php 0000644 00000000210 15107327040 0016650 0 ustar 00 <?php namespace Illuminate\Testing; use Illuminate\Support\Collection; class LoggedExceptionCollection extends Collection { // } src/Illuminate/Testing/composer.json 0000644 00000002572 15107327040 0013602 0 ustar 00 { "name": "illuminate/testing", "description": "The Illuminate Testing package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "ext-mbstring": "*", "illuminate/collections": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0", "illuminate/support": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Testing\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "brianium/paratest": "Required to run tests in parallel (^6.0).", "illuminate/console": "Required to assert console commands (^10.0).", "illuminate/database": "Required to assert databases (^10.0).", "illuminate/http": "Required to assert responses (^10.0).", "mockery/mockery": "Required to use mocking (^1.5.1).", "phpunit/phpunit": "Required to use assertions and run tests (^9.5.8|^10.0.7)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Testing/AssertableJsonString.php 0000644 00000025512 15107327040 0015676 0 ustar 00 <?php namespace Illuminate\Testing; use ArrayAccess; use Closure; use Countable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Testing\Assert as PHPUnit; use JsonSerializable; class AssertableJsonString implements ArrayAccess, Countable { /** * The original encoded json. * * @var \Illuminate\Contracts\Support\Jsonable|\JsonSerializable|array|string */ public $json; /** * The decoded json contents. * * @var array|null */ protected $decoded; /** * Create a new assertable JSON string instance. * * @param \Illuminate\Contracts\Support\Jsonable|\JsonSerializable|array|string $jsonable * @return void */ public function __construct($jsonable) { $this->json = $jsonable; if ($jsonable instanceof JsonSerializable) { $this->decoded = $jsonable->jsonSerialize(); } elseif ($jsonable instanceof Jsonable) { $this->decoded = json_decode($jsonable->toJson(), true); } elseif (is_array($jsonable)) { $this->decoded = $jsonable; } else { $this->decoded = json_decode($jsonable, true); } } /** * Validate and return the decoded response JSON. * * @param string|null $key * @return mixed */ public function json($key = null) { return data_get($this->decoded, $key); } /** * Assert that the response JSON has the expected count of items at the given key. * * @param int $count * @param string|null $key * @return $this */ public function assertCount(int $count, $key = null) { if (! is_null($key)) { PHPUnit::assertCount( $count, data_get($this->decoded, $key), "Failed to assert that the response count matched the expected {$count}" ); return $this; } PHPUnit::assertCount($count, $this->decoded, "Failed to assert that the response count matched the expected {$count}" ); return $this; } /** * Assert that the response has the exact given JSON. * * @param array $data * @return $this */ public function assertExact(array $data) { $actual = $this->reorderAssocKeys((array) $this->decoded); $expected = $this->reorderAssocKeys($data); PHPUnit::assertEquals( json_encode($expected, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), json_encode($actual, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) ); return $this; } /** * Assert that the response has the similar JSON as given. * * @param array $data * @return $this */ public function assertSimilar(array $data) { $actual = json_encode( Arr::sortRecursive((array) $this->decoded), JSON_UNESCAPED_UNICODE ); PHPUnit::assertEquals(json_encode(Arr::sortRecursive($data), JSON_UNESCAPED_UNICODE), $actual); return $this; } /** * Assert that the response contains the given JSON fragment. * * @param array $data * @return $this */ public function assertFragment(array $data) { $actual = json_encode( Arr::sortRecursive((array) $this->decoded), JSON_UNESCAPED_UNICODE ); foreach (Arr::sortRecursive($data) as $key => $value) { $expected = $this->jsonSearchStrings($key, $value); PHPUnit::assertTrue( Str::contains($actual, $expected), 'Unable to find JSON fragment: '.PHP_EOL.PHP_EOL. '['.json_encode([$key => $value], JSON_UNESCAPED_UNICODE).']'.PHP_EOL.PHP_EOL. 'within'.PHP_EOL.PHP_EOL. "[{$actual}]." ); } return $this; } /** * Assert that the response does not contain the given JSON fragment. * * @param array $data * @param bool $exact * @return $this */ public function assertMissing(array $data, $exact = false) { if ($exact) { return $this->assertMissingExact($data); } $actual = json_encode( Arr::sortRecursive((array) $this->decoded), JSON_UNESCAPED_UNICODE ); foreach (Arr::sortRecursive($data) as $key => $value) { $unexpected = $this->jsonSearchStrings($key, $value); PHPUnit::assertFalse( Str::contains($actual, $unexpected), 'Found unexpected JSON fragment: '.PHP_EOL.PHP_EOL. '['.json_encode([$key => $value], JSON_UNESCAPED_UNICODE).']'.PHP_EOL.PHP_EOL. 'within'.PHP_EOL.PHP_EOL. "[{$actual}]." ); } return $this; } /** * Assert that the response does not contain the exact JSON fragment. * * @param array $data * @return $this */ public function assertMissingExact(array $data) { $actual = json_encode( Arr::sortRecursive((array) $this->decoded), JSON_UNESCAPED_UNICODE ); foreach (Arr::sortRecursive($data) as $key => $value) { $unexpected = $this->jsonSearchStrings($key, $value); if (! Str::contains($actual, $unexpected)) { return $this; } } PHPUnit::fail( 'Found unexpected JSON fragment: '.PHP_EOL.PHP_EOL. '['.json_encode($data, JSON_UNESCAPED_UNICODE).']'.PHP_EOL.PHP_EOL. 'within'.PHP_EOL.PHP_EOL. "[{$actual}]." ); return $this; } /** * Assert that the response does not contain the given path. * * @param string $path * @return $this */ public function assertMissingPath($path) { PHPUnit::assertFalse(Arr::has($this->json(), $path)); return $this; } /** * Assert that the expected value and type exists at the given path in the response. * * @param string $path * @param mixed $expect * @return $this */ public function assertPath($path, $expect) { if ($expect instanceof Closure) { PHPUnit::assertTrue($expect($this->json($path))); } else { PHPUnit::assertSame($expect, $this->json($path)); } return $this; } /** * Assert that the given path in the response contains all of the expected values without looking at the order. * * @param string $path * @param array $expect * @return $this */ public function assertPathCanonicalizing($path, $expect) { PHPUnit::assertEqualsCanonicalizing($expect, $this->json($path)); return $this; } /** * Assert that the response has a given JSON structure. * * @param array|null $structure * @param array|null $responseData * @return $this */ public function assertStructure(array $structure = null, $responseData = null) { if (is_null($structure)) { return $this->assertSimilar($this->decoded); } if (! is_null($responseData)) { return (new static($responseData))->assertStructure($structure); } foreach ($structure as $key => $value) { if (is_array($value) && $key === '*') { PHPUnit::assertIsArray($this->decoded); foreach ($this->decoded as $responseDataItem) { $this->assertStructure($structure['*'], $responseDataItem); } } elseif (is_array($value)) { PHPUnit::assertArrayHasKey($key, $this->decoded); $this->assertStructure($structure[$key], $this->decoded[$key]); } else { PHPUnit::assertArrayHasKey($value, $this->decoded); } } return $this; } /** * Assert that the response is a superset of the given JSON. * * @param array $data * @param bool $strict * @return $this */ public function assertSubset(array $data, $strict = false) { PHPUnit::assertArraySubset( $data, $this->decoded, $strict, $this->assertJsonMessage($data) ); return $this; } /** * Reorder associative array keys to make it easy to compare arrays. * * @param array $data * @return array */ protected function reorderAssocKeys(array $data) { $data = Arr::dot($data); ksort($data); $result = []; foreach ($data as $key => $value) { Arr::set($result, $key, $value); } return $result; } /** * Get the assertion message for assertJson. * * @param array $data * @return string */ protected function assertJsonMessage(array $data) { $expected = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $actual = json_encode($this->decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); return 'Unable to find JSON: '.PHP_EOL.PHP_EOL. "[{$expected}]".PHP_EOL.PHP_EOL. 'within response JSON:'.PHP_EOL.PHP_EOL. "[{$actual}].".PHP_EOL.PHP_EOL; } /** * Get the strings we need to search for when examining the JSON. * * @param string $key * @param string $value * @return array */ protected function jsonSearchStrings($key, $value) { $needle = Str::substr(json_encode([$key => $value], JSON_UNESCAPED_UNICODE), 1, -1); return [ $needle.']', $needle.'}', $needle.',', ]; } /** * Get the total number of items in the underlying JSON array. * * @return int */ public function count(): int { return count($this->decoded); } /** * Determine whether an offset exists. * * @param mixed $offset * @return bool */ public function offsetExists($offset): bool { return isset($this->decoded[$offset]); } /** * Get the value at the given offset. * * @param string $offset * @return mixed */ public function offsetGet($offset): mixed { return $this->decoded[$offset]; } /** * Set the value at the given offset. * * @param string $offset * @param mixed $value * @return void */ public function offsetSet($offset, $value): void { $this->decoded[$offset] = $value; } /** * Unset the value at the given offset. * * @param string $offset * @return void */ public function offsetUnset($offset): void { unset($this->decoded[$offset]); } } src/Illuminate/Testing/LICENSE.md 0000644 00000002063 15107327040 0012457 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Testing/ParallelTestingServiceProvider.php 0000644 00000001521 15107327040 0017710 0 ustar 00 <?php namespace Illuminate\Testing; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; use Illuminate\Testing\Concerns\TestDatabases; class ParallelTestingServiceProvider extends ServiceProvider implements DeferrableProvider { use TestDatabases; /** * Boot the application's service providers. * * @return void */ public function boot() { if ($this->app->runningInConsole()) { $this->bootTestDatabase(); } } /** * Register the service provider. * * @return void */ public function register() { if ($this->app->runningInConsole()) { $this->app->singleton(ParallelTesting::class, function () { return new ParallelTesting($this->app); }); } } } src/Illuminate/Testing/TestView.php 0000644 00000012433 15107327040 0013340 0 ustar 00 <?php namespace Illuminate\Testing; use Closure; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Illuminate\Support\Traits\Macroable; use Illuminate\Testing\Assert as PHPUnit; use Illuminate\Testing\Constraints\SeeInOrder; use Illuminate\View\View; class TestView { use Macroable; /** * The original view. * * @var \Illuminate\View\View */ protected $view; /** * The rendered view contents. * * @var string */ protected $rendered; /** * Create a new test view instance. * * @param \Illuminate\View\View $view * @return void */ public function __construct(View $view) { $this->view = $view; $this->rendered = $view->render(); } /** * Assert that the response view has a given piece of bound data. * * @param string|array $key * @param mixed $value * @return $this */ public function assertViewHas($key, $value = null) { if (is_array($key)) { return $this->assertViewHasAll($key); } if (is_null($value)) { PHPUnit::assertTrue(Arr::has($this->view->gatherData(), $key)); } elseif ($value instanceof Closure) { PHPUnit::assertTrue($value(Arr::get($this->view->gatherData(), $key))); } elseif ($value instanceof Model) { PHPUnit::assertTrue($value->is(Arr::get($this->view->gatherData(), $key))); } elseif ($value instanceof Collection) { $actual = Arr::get($this->view->gatherData(), $key); PHPUnit::assertInstanceOf(Collection::class, $actual); PHPUnit::assertSameSize($value, $actual); $value->each(fn ($item, $index) => PHPUnit::assertTrue($actual->get($index)->is($item))); } else { PHPUnit::assertEquals($value, Arr::get($this->view->gatherData(), $key)); } return $this; } /** * Assert that the response view has a given list of bound data. * * @param array $bindings * @return $this */ public function assertViewHasAll(array $bindings) { foreach ($bindings as $key => $value) { if (is_int($key)) { $this->assertViewHas($value); } else { $this->assertViewHas($key, $value); } } return $this; } /** * Assert that the response view is missing a piece of bound data. * * @param string $key * @return $this */ public function assertViewMissing($key) { PHPUnit::assertFalse(Arr::has($this->view->gatherData(), $key)); return $this; } /** * Assert that the given string is contained within the view. * * @param string $value * @param bool $escape * @return $this */ public function assertSee($value, $escape = true) { $value = $escape ? e($value) : $value; PHPUnit::assertStringContainsString((string) $value, $this->rendered); return $this; } /** * Assert that the given strings are contained in order within the view. * * @param array $values * @param bool $escape * @return $this */ public function assertSeeInOrder(array $values, $escape = true) { $values = $escape ? array_map('e', $values) : $values; PHPUnit::assertThat($values, new SeeInOrder($this->rendered)); return $this; } /** * Assert that the given string is contained within the view text. * * @param string $value * @param bool $escape * @return $this */ public function assertSeeText($value, $escape = true) { $value = $escape ? e($value) : $value; PHPUnit::assertStringContainsString((string) $value, strip_tags($this->rendered)); return $this; } /** * Assert that the given strings are contained in order within the view text. * * @param array $values * @param bool $escape * @return $this */ public function assertSeeTextInOrder(array $values, $escape = true) { $values = $escape ? array_map('e', $values) : $values; PHPUnit::assertThat($values, new SeeInOrder(strip_tags($this->rendered))); return $this; } /** * Assert that the given string is not contained within the view. * * @param string $value * @param bool $escape * @return $this */ public function assertDontSee($value, $escape = true) { $value = $escape ? e($value) : $value; PHPUnit::assertStringNotContainsString((string) $value, $this->rendered); return $this; } /** * Assert that the given string is not contained within the view text. * * @param string $value * @param bool $escape * @return $this */ public function assertDontSeeText($value, $escape = true) { $value = $escape ? e($value) : $value; PHPUnit::assertStringNotContainsString((string) $value, strip_tags($this->rendered)); return $this; } /** * Get the string contents of the rendered view. * * @return string */ public function __toString() { return $this->rendered; } } src/Illuminate/Testing/Constraints/CountInDatabase.php 0000644 00000003561 15107327040 0017103 0 ustar 00 <?php namespace Illuminate\Testing\Constraints; use Illuminate\Database\Connection; use PHPUnit\Framework\Constraint\Constraint; use ReflectionClass; class CountInDatabase extends Constraint { /** * The database connection. * * @var \Illuminate\Database\Connection */ protected $database; /** * The expected table entries count that will be checked against the actual count. * * @var int */ protected $expectedCount; /** * The actual table entries count that will be checked against the expected count. * * @var int */ protected $actualCount; /** * Create a new constraint instance. * * @param \Illuminate\Database\Connection $database * @param int $expectedCount * @return void */ public function __construct(Connection $database, int $expectedCount) { $this->expectedCount = $expectedCount; $this->database = $database; } /** * Check if the expected and actual count are equal. * * @param string $table * @return bool */ public function matches($table): bool { $this->actualCount = $this->database->table($table)->count(); return $this->actualCount === $this->expectedCount; } /** * Get the description of the failure. * * @param string $table * @return string */ public function failureDescription($table): string { return sprintf( "table [%s] matches expected entries count of %s. Entries found: %s.\n", $table, $this->expectedCount, $this->actualCount ); } /** * Get a string representation of the object. * * @param int $options * @return string */ public function toString($options = 0): string { return (new ReflectionClass($this))->name; } } src/Illuminate/Testing/Constraints/SoftDeletedInDatabase.php 0000644 00000005267 15107327040 0020222 0 ustar 00 <?php namespace Illuminate\Testing\Constraints; use Illuminate\Database\Connection; use PHPUnit\Framework\Constraint\Constraint; class SoftDeletedInDatabase extends Constraint { /** * Number of records that will be shown in the console in case of failure. * * @var int */ protected $show = 3; /** * The database connection. * * @var \Illuminate\Database\Connection */ protected $database; /** * The data that will be used to narrow the search in the database table. * * @var array */ protected $data; /** * The name of the column that indicates soft deletion has occurred. * * @var string */ protected $deletedAtColumn; /** * Create a new constraint instance. * * @param \Illuminate\Database\Connection $database * @param array $data * @param string $deletedAtColumn * @return void */ public function __construct(Connection $database, array $data, string $deletedAtColumn) { $this->data = $data; $this->database = $database; $this->deletedAtColumn = $deletedAtColumn; } /** * Check if the data is found in the given table. * * @param string $table * @return bool */ public function matches($table): bool { return $this->database->table($table) ->where($this->data) ->whereNotNull($this->deletedAtColumn) ->count() > 0; } /** * Get the description of the failure. * * @param string $table * @return string */ public function failureDescription($table): string { return sprintf( "any soft deleted row in the table [%s] matches the attributes %s.\n\n%s", $table, $this->toString(), $this->getAdditionalInfo($table) ); } /** * Get additional info about the records found in the database table. * * @param string $table * @return string */ protected function getAdditionalInfo($table) { $query = $this->database->table($table); $results = $query->limit($this->show)->get(); if ($results->isEmpty()) { return 'The table is empty'; } $description = 'Found: '.json_encode($results, JSON_PRETTY_PRINT); if ($query->count() > $this->show) { $description .= sprintf(' and %s others', $query->count() - $this->show); } return $description; } /** * Get a string representation of the object. * * @return string */ public function toString(): string { return json_encode($this->data); } } src/Illuminate/Testing/Constraints/HasInDatabase.php 0000644 00000006240 15107327040 0016523 0 ustar 00 <?php namespace Illuminate\Testing\Constraints; use Illuminate\Contracts\Database\Query\Expression; use Illuminate\Database\Connection; use PHPUnit\Framework\Constraint\Constraint; class HasInDatabase extends Constraint { /** * Number of records that will be shown in the console in case of failure. * * @var int */ protected $show = 3; /** * The database connection. * * @var \Illuminate\Database\Connection */ protected $database; /** * The data that will be used to narrow the search in the database table. * * @var array */ protected $data; /** * Create a new constraint instance. * * @param \Illuminate\Database\Connection $database * @param array $data * @return void */ public function __construct(Connection $database, array $data) { $this->data = $data; $this->database = $database; } /** * Check if the data is found in the given table. * * @param string $table * @return bool */ public function matches($table): bool { return $this->database->table($table)->where($this->data)->count() > 0; } /** * Get the description of the failure. * * @param string $table * @return string */ public function failureDescription($table): string { return sprintf( "a row in the table [%s] matches the attributes %s.\n\n%s", $table, $this->toString(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), $this->getAdditionalInfo($table) ); } /** * Get additional info about the records found in the database table. * * @param string $table * @return string */ protected function getAdditionalInfo($table) { $query = $this->database->table($table); $similarResults = $query->where( array_key_first($this->data), $this->data[array_key_first($this->data)] )->select(array_keys($this->data))->limit($this->show)->get(); if ($similarResults->isNotEmpty()) { $description = 'Found similar results: '.json_encode($similarResults, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); } else { $query = $this->database->table($table); $results = $query->select(array_keys($this->data))->limit($this->show)->get(); if ($results->isEmpty()) { return 'The table is empty'; } $description = 'Found: '.json_encode($results, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); } if ($query->count() > $this->show) { $description .= sprintf(' and %s others', $query->count() - $this->show); } return $description; } /** * Get a string representation of the object. * * @param int $options * @return string */ public function toString($options = 0): string { foreach ($this->data as $key => $data) { $output[$key] = $data instanceof Expression ? $data->getValue($this->database->getQueryGrammar()) : $data; } return json_encode($output ?? [], $options); } } src/Illuminate/Testing/Constraints/SeeInOrder.php 0000644 00000003401 15107327040 0016067 0 ustar 00 <?php namespace Illuminate\Testing\Constraints; use PHPUnit\Framework\Constraint\Constraint; use ReflectionClass; class SeeInOrder extends Constraint { /** * The string under validation. * * @var string */ protected $content; /** * The last value that failed to pass validation. * * @var string */ protected $failedValue; /** * Create a new constraint instance. * * @param string $content * @return void */ public function __construct($content) { $this->content = $content; } /** * Determine if the rule passes validation. * * @param array $values * @return bool */ public function matches($values): bool { $position = 0; foreach ($values as $value) { if (empty($value)) { continue; } $valuePosition = mb_strpos($this->content, $value, $position); if ($valuePosition === false || $valuePosition < $position) { $this->failedValue = $value; return false; } $position = $valuePosition + mb_strlen($value); } return true; } /** * Get the description of the failure. * * @param array $values * @return string */ public function failureDescription($values): string { return sprintf( 'Failed asserting that \'%s\' contains "%s" in specified order.', $this->content, $this->failedValue ); } /** * Get a string representation of the object. * * @return string */ public function toString(): string { return (new ReflectionClass($this))->name; } } src/Illuminate/Testing/Constraints/NotSoftDeletedInDatabase.php 0000644 00000005261 15107327040 0020675 0 ustar 00 <?php namespace Illuminate\Testing\Constraints; use Illuminate\Database\Connection; use PHPUnit\Framework\Constraint\Constraint; class NotSoftDeletedInDatabase extends Constraint { /** * Number of records that will be shown in the console in case of failure. * * @var int */ protected $show = 3; /** * The database connection. * * @var \Illuminate\Database\Connection */ protected $database; /** * The data that will be used to narrow the search in the database table. * * @var array */ protected $data; /** * The name of the column that indicates soft deletion has occurred. * * @var string */ protected $deletedAtColumn; /** * Create a new constraint instance. * * @param \Illuminate\Database\Connection $database * @param array $data * @param string $deletedAtColumn * @return void */ public function __construct(Connection $database, array $data, string $deletedAtColumn) { $this->database = $database; $this->data = $data; $this->deletedAtColumn = $deletedAtColumn; } /** * Check if the data is found in the given table. * * @param string $table * @return bool */ public function matches($table): bool { return $this->database->table($table) ->where($this->data) ->whereNull($this->deletedAtColumn) ->count() > 0; } /** * Get the description of the failure. * * @param string $table * @return string */ public function failureDescription($table): string { return sprintf( "any existing row in the table [%s] matches the attributes %s.\n\n%s", $table, $this->toString(), $this->getAdditionalInfo($table) ); } /** * Get additional info about the records found in the database table. * * @param string $table * @return string */ protected function getAdditionalInfo($table) { $query = $this->database->table($table); $results = $query->limit($this->show)->get(); if ($results->isEmpty()) { return 'The table is empty'; } $description = 'Found: '.json_encode($results, JSON_PRETTY_PRINT); if ($query->count() > $this->show) { $description .= sprintf(' and %s others', $query->count() - $this->show); } return $description; } /** * Get a string representation of the object. * * @return string */ public function toString(): string { return json_encode($this->data); } } src/Illuminate/Testing/Constraints/ArraySubset.php 0000644 00000007366 15107327040 0016352 0 ustar 00 <?php namespace Illuminate\Testing\Constraints; use ArrayObject; use PHPUnit\Framework\Constraint\Constraint; use SebastianBergmann\Comparator\ComparisonFailure; use Traversable; /** * @internal This class is not meant to be used or overwritten outside the framework itself. */ final class ArraySubset extends Constraint { /** * @var iterable */ private $subset; /** * @var bool */ private $strict; /** * Create a new array subset constraint instance. * * @param iterable $subset * @param bool $strict * @return void */ public function __construct(iterable $subset, bool $strict = false) { $this->strict = $strict; $this->subset = $subset; } /** * Evaluates the constraint for parameter $other. * * If $returnResult is set to false (the default), an exception is thrown * in case of a failure. null is returned otherwise. * * If $returnResult is true, the result of the evaluation is returned as * a boolean value instead: true in case of success, false in case of a * failure. * * @param mixed $other * @param string $description * @param bool $returnResult * @return bool|null * * @throws \PHPUnit\Framework\ExpectationFailedException * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException */ public function evaluate($other, string $description = '', bool $returnResult = false): ?bool { // type cast $other & $this->subset as an array to allow // support in standard array functions. $other = $this->toArray($other); $this->subset = $this->toArray($this->subset); $patched = array_replace_recursive($other, $this->subset); if ($this->strict) { $result = $other === $patched; } else { $result = $other == $patched; } if ($returnResult) { return $result; } if (! $result) { $f = new ComparisonFailure( $patched, $other, var_export($patched, true), var_export($other, true) ); $this->fail($other, $description, $f); } return null; } /** * Returns a string representation of the constraint. * * @return string * * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException */ public function toString(): string { return 'has the subset '.$this->exporter()->export($this->subset); } /** * Returns the description of the failure. * * The beginning of failure messages is "Failed asserting that" in most * cases. This method should return the second part of that sentence. * * @param mixed $other * @return string * * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException */ protected function failureDescription($other): string { return 'an array '.$this->toString(); } /** * Returns the description of the failure. * * The beginning of failure messages is "Failed asserting that" in most * cases. This method should return the second part of that sentence. * * @param iterable $other * @return array */ private function toArray(iterable $other): array { if (is_array($other)) { return $other; } if ($other instanceof ArrayObject) { return $other->getArrayCopy(); } if ($other instanceof Traversable) { return iterator_to_array($other); } // Keep BC even if we know that array would not be the expected one return (array) $other; } } src/Illuminate/Testing/ParallelTesting.php 0000644 00000015400 15107327040 0014655 0 ustar 00 <?php namespace Illuminate\Testing; use Illuminate\Contracts\Container\Container; use Illuminate\Support\Str; class ParallelTesting { /** * The container instance. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * The options resolver callback. * * @var \Closure|null */ protected $optionsResolver; /** * The token resolver callback. * * @var \Closure|null */ protected $tokenResolver; /** * All of the registered "setUp" process callbacks. * * @var array */ protected $setUpProcessCallbacks = []; /** * All of the registered "setUp" test case callbacks. * * @var array */ protected $setUpTestCaseCallbacks = []; /** * All of the registered "setUp" test database callbacks. * * @var array */ protected $setUpTestDatabaseCallbacks = []; /** * All of the registered "tearDown" process callbacks. * * @var array */ protected $tearDownProcessCallbacks = []; /** * All of the registered "tearDown" test case callbacks. * * @var array */ protected $tearDownTestCaseCallbacks = []; /** * Create a new parallel testing instance. * * @param \Illuminate\Contracts\Container\Container $container * @return void */ public function __construct(Container $container) { $this->container = $container; } /** * Set a callback that should be used when resolving options. * * @param \Closure|null $resolver * @return void */ public function resolveOptionsUsing($resolver) { $this->optionsResolver = $resolver; } /** * Set a callback that should be used when resolving the unique process token. * * @param \Closure|null $resolver * @return void */ public function resolveTokenUsing($resolver) { $this->tokenResolver = $resolver; } /** * Register a "setUp" process callback. * * @param callable $callback * @return void */ public function setUpProcess($callback) { $this->setUpProcessCallbacks[] = $callback; } /** * Register a "setUp" test case callback. * * @param callable $callback * @return void */ public function setUpTestCase($callback) { $this->setUpTestCaseCallbacks[] = $callback; } /** * Register a "setUp" test database callback. * * @param callable $callback * @return void */ public function setUpTestDatabase($callback) { $this->setUpTestDatabaseCallbacks[] = $callback; } /** * Register a "tearDown" process callback. * * @param callable $callback * @return void */ public function tearDownProcess($callback) { $this->tearDownProcessCallbacks[] = $callback; } /** * Register a "tearDown" test case callback. * * @param callable $callback * @return void */ public function tearDownTestCase($callback) { $this->tearDownTestCaseCallbacks[] = $callback; } /** * Call all of the "setUp" process callbacks. * * @return void */ public function callSetUpProcessCallbacks() { $this->whenRunningInParallel(function () { foreach ($this->setUpProcessCallbacks as $callback) { $this->container->call($callback, [ 'token' => $this->token(), ]); } }); } /** * Call all of the "setUp" test case callbacks. * * @param \Illuminate\Foundation\Testing\TestCase $testCase * @return void */ public function callSetUpTestCaseCallbacks($testCase) { $this->whenRunningInParallel(function () use ($testCase) { foreach ($this->setUpTestCaseCallbacks as $callback) { $this->container->call($callback, [ 'testCase' => $testCase, 'token' => $this->token(), ]); } }); } /** * Call all of the "setUp" test database callbacks. * * @param string $database * @return void */ public function callSetUpTestDatabaseCallbacks($database) { $this->whenRunningInParallel(function () use ($database) { foreach ($this->setUpTestDatabaseCallbacks as $callback) { $this->container->call($callback, [ 'database' => $database, 'token' => $this->token(), ]); } }); } /** * Call all of the "tearDown" process callbacks. * * @return void */ public function callTearDownProcessCallbacks() { $this->whenRunningInParallel(function () { foreach ($this->tearDownProcessCallbacks as $callback) { $this->container->call($callback, [ 'token' => $this->token(), ]); } }); } /** * Call all of the "tearDown" test case callbacks. * * @param \Illuminate\Foundation\Testing\TestCase $testCase * @return void */ public function callTearDownTestCaseCallbacks($testCase) { $this->whenRunningInParallel(function () use ($testCase) { foreach ($this->tearDownTestCaseCallbacks as $callback) { $this->container->call($callback, [ 'testCase' => $testCase, 'token' => $this->token(), ]); } }); } /** * Get a parallel testing option. * * @param string $option * @return mixed */ public function option($option) { $optionsResolver = $this->optionsResolver ?: function ($option) { $option = 'LARAVEL_PARALLEL_TESTING_'.Str::upper($option); return $_SERVER[$option] ?? false; }; return $optionsResolver($option); } /** * Gets a unique test token. * * @return string|false */ public function token() { return $this->tokenResolver ? call_user_func($this->tokenResolver) : ($_SERVER['TEST_TOKEN'] ?? false); } /** * Apply the callback if tests are running in parallel. * * @param callable $callback * @return void */ protected function whenRunningInParallel($callback) { if ($this->inParallel()) { $callback(); } } /** * Indicates if the current tests are been run in parallel. * * @return bool */ protected function inParallel() { return ! empty($_SERVER['LARAVEL_PARALLEL_TESTING']) && $this->token(); } } src/Illuminate/Testing/Fluent/Concerns/Interaction.php 0000644 00000002701 15107327040 0017051 0 ustar 00 <?php namespace Illuminate\Testing\Fluent\Concerns; use Illuminate\Support\Str; use PHPUnit\Framework\Assert as PHPUnit; trait Interaction { /** * The list of interacted properties. * * @var array */ protected $interacted = []; /** * Marks the property as interacted. * * @param string $key * @return void */ protected function interactsWith(string $key): void { $prop = Str::before($key, '.'); if (! in_array($prop, $this->interacted, true)) { $this->interacted[] = $prop; } } /** * Asserts that all properties have been interacted with. * * @return void */ public function interacted(): void { PHPUnit::assertSame( [], array_diff(array_keys($this->prop()), $this->interacted), $this->path ? sprintf('Unexpected properties were found in scope [%s].', $this->path) : 'Unexpected properties were found on the root level.' ); } /** * Disables the interaction check. * * @return $this */ public function etc(): self { $this->interacted = array_keys($this->prop()); return $this; } /** * Retrieve a prop within the current scope using "dot" notation. * * @param string|null $key * @return mixed */ abstract protected function prop(string $key = null); } src/Illuminate/Testing/Fluent/Concerns/Matching.php 0000644 00000013525 15107327041 0016333 0 ustar 00 <?php namespace Illuminate\Testing\Fluent\Concerns; use Closure; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Collection; use PHPUnit\Framework\Assert as PHPUnit; trait Matching { /** * Asserts that the property matches the expected value. * * @param string $key * @param mixed|\Closure $expected * @return $this */ public function where(string $key, $expected): self { $this->has($key); $actual = $this->prop($key); if ($expected instanceof Closure) { PHPUnit::assertTrue( $expected(is_array($actual) ? Collection::make($actual) : $actual), sprintf('Property [%s] was marked as invalid using a closure.', $this->dotPath($key)) ); return $this; } if ($expected instanceof Arrayable) { $expected = $expected->toArray(); } $this->ensureSorted($expected); $this->ensureSorted($actual); PHPUnit::assertSame( $expected, $actual, sprintf('Property [%s] does not match the expected value.', $this->dotPath($key)) ); return $this; } /** * Asserts that the property does not match the expected value. * * @param string $key * @param mixed|\Closure $expected * @return $this */ public function whereNot(string $key, $expected): self { $this->has($key); $actual = $this->prop($key); if ($expected instanceof Closure) { PHPUnit::assertFalse( $expected(is_array($actual) ? Collection::make($actual) : $actual), sprintf('Property [%s] was marked as invalid using a closure.', $this->dotPath($key)) ); return $this; } if ($expected instanceof Arrayable) { $expected = $expected->toArray(); } $this->ensureSorted($expected); $this->ensureSorted($actual); PHPUnit::assertNotSame( $expected, $actual, sprintf( 'Property [%s] contains a value that should be missing: [%s, %s]', $this->dotPath($key), $key, $expected ) ); return $this; } /** * Asserts that all properties match their expected values. * * @param array $bindings * @return $this */ public function whereAll(array $bindings): self { foreach ($bindings as $key => $value) { $this->where($key, $value); } return $this; } /** * Asserts that the property is of the expected type. * * @param string $key * @param string|array $expected * @return $this */ public function whereType(string $key, $expected): self { $this->has($key); $actual = $this->prop($key); if (! is_array($expected)) { $expected = explode('|', $expected); } PHPUnit::assertContains( strtolower(gettype($actual)), $expected, sprintf('Property [%s] is not of expected type [%s].', $this->dotPath($key), implode('|', $expected)) ); return $this; } /** * Asserts that all properties are of their expected types. * * @param array $bindings * @return $this */ public function whereAllType(array $bindings): self { foreach ($bindings as $key => $value) { $this->whereType($key, $value); } return $this; } /** * Asserts that the property contains the expected values. * * @param string $key * @param mixed $expected * @return $this */ public function whereContains(string $key, $expected) { $actual = Collection::make( $this->prop($key) ?? $this->prop() ); $missing = Collection::make($expected)->reject(function ($search) use ($key, $actual) { if ($actual->containsStrict($key, $search)) { return true; } return $actual->containsStrict($search); }); if ($missing->whereInstanceOf('Closure')->isNotEmpty()) { PHPUnit::assertEmpty( $missing->toArray(), sprintf( 'Property [%s] does not contain a value that passes the truth test within the given closure.', $key, ) ); } else { PHPUnit::assertEmpty( $missing->toArray(), sprintf( 'Property [%s] does not contain [%s].', $key, implode(', ', array_values($missing->toArray())) ) ); } return $this; } /** * Ensures that all properties are sorted the same way, recursively. * * @param mixed $value * @return void */ protected function ensureSorted(&$value): void { if (! is_array($value)) { return; } foreach ($value as &$arg) { $this->ensureSorted($arg); } ksort($value); } /** * Compose the absolute "dot" path to the given key. * * @param string $key * @return string */ abstract protected function dotPath(string $key = ''): string; /** * Ensure that the given prop exists. * * @param string $key * @param null $value * @param \Closure|null $scope * @return $this */ abstract public function has(string $key, $value = null, Closure $scope = null); /** * Retrieve a prop within the current scope using "dot" notation. * * @param string|null $key * @return mixed */ abstract protected function prop(string $key = null); } src/Illuminate/Testing/Fluent/Concerns/Debugging.php 0000644 00000001322 15107327041 0016464 0 ustar 00 <?php namespace Illuminate\Testing\Fluent\Concerns; trait Debugging { /** * Dumps the given props. * * @param string|null $prop * @return $this */ public function dump(string $prop = null): self { dump($this->prop($prop)); return $this; } /** * Dumps the given props and exits. * * @param string|null $prop * @return never */ public function dd(string $prop = null): void { dd($this->prop($prop)); } /** * Retrieve a prop within the current scope using "dot" notation. * * @param string|null $key * @return mixed */ abstract protected function prop(string $key = null); } src/Illuminate/Testing/Fluent/Concerns/Has.php 0000644 00000012007 15107327041 0015306 0 ustar 00 <?php namespace Illuminate\Testing\Fluent\Concerns; use Closure; use Illuminate\Support\Arr; use PHPUnit\Framework\Assert as PHPUnit; trait Has { /** * Assert that the prop is of the expected size. * * @param string|int $key * @param int|null $length * @return $this */ public function count($key, int $length = null): self { if (is_null($length)) { $path = $this->dotPath(); PHPUnit::assertCount( $key, $this->prop(), $path ? sprintf('Property [%s] does not have the expected size.', $path) : sprintf('Root level does not have the expected size.') ); return $this; } PHPUnit::assertCount( $length, $this->prop($key), sprintf('Property [%s] does not have the expected size.', $this->dotPath($key)) ); return $this; } /** * Ensure that the given prop exists. * * @param string|int $key * @param int|\Closure|null $length * @param \Closure|null $callback * @return $this */ public function has($key, $length = null, Closure $callback = null): self { $prop = $this->prop(); if (is_int($key) && is_null($length)) { return $this->count($key); } PHPUnit::assertTrue( Arr::has($prop, $key), sprintf('Property [%s] does not exist.', $this->dotPath($key)) ); $this->interactsWith($key); if (! is_null($callback)) { return $this->has($key, function (self $scope) use ($length, $callback) { return $scope ->tap(function (self $scope) use ($length) { if (! is_null($length)) { $scope->count($length); } }) ->first($callback) ->etc(); }); } if (is_callable($length)) { return $this->scope($key, $length); } if (! is_null($length)) { return $this->count($key, $length); } return $this; } /** * Assert that all of the given props exist. * * @param array|string $key * @return $this */ public function hasAll($key): self { $keys = is_array($key) ? $key : func_get_args(); foreach ($keys as $prop => $count) { if (is_int($prop)) { $this->has($count); } else { $this->has($prop, $count); } } return $this; } /** * Assert that at least one of the given props exists. * * @param array|string $key * @return $this */ public function hasAny($key): self { $keys = is_array($key) ? $key : func_get_args(); PHPUnit::assertTrue( Arr::hasAny($this->prop(), $keys), sprintf('None of properties [%s] exist.', implode(', ', $keys)) ); foreach ($keys as $key) { $this->interactsWith($key); } return $this; } /** * Assert that none of the given props exist. * * @param array|string $key * @return $this */ public function missingAll($key): self { $keys = is_array($key) ? $key : func_get_args(); foreach ($keys as $prop) { $this->missing($prop); } return $this; } /** * Assert that the given prop does not exist. * * @param string $key * @return $this */ public function missing(string $key): self { PHPUnit::assertNotTrue( Arr::has($this->prop(), $key), sprintf('Property [%s] was found while it was expected to be missing.', $this->dotPath($key)) ); return $this; } /** * Compose the absolute "dot" path to the given key. * * @param string $key * @return string */ abstract protected function dotPath(string $key = ''): string; /** * Marks the property as interacted. * * @param string $key * @return void */ abstract protected function interactsWith(string $key): void; /** * Retrieve a prop within the current scope using "dot" notation. * * @param string|null $key * @return mixed */ abstract protected function prop(string $key = null); /** * Instantiate a new "scope" at the path of the given key. * * @param string $key * @param \Closure $callback * @return $this */ abstract protected function scope(string $key, Closure $callback); /** * Disables the interaction check. * * @return $this */ abstract public function etc(); /** * Instantiate a new "scope" on the first element. * * @param \Closure $callback * @return $this */ abstract public function first(Closure $callback); } src/Illuminate/Testing/Fluent/AssertableJson.php 0000644 00000010153 15107327041 0015740 0 ustar 00 <?php namespace Illuminate\Testing\Fluent; use Closure; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Arr; use Illuminate\Support\Traits\Macroable; use Illuminate\Support\Traits\Tappable; use Illuminate\Testing\AssertableJsonString; use PHPUnit\Framework\Assert as PHPUnit; class AssertableJson implements Arrayable { use Concerns\Has, Concerns\Matching, Concerns\Debugging, Concerns\Interaction, Macroable, Tappable; /** * The properties in the current scope. * * @var array */ private $props; /** * The "dot" path to the current scope. * * @var string|null */ private $path; /** * Create a new fluent, assertable JSON data instance. * * @param array $props * @param string|null $path * @return void */ protected function __construct(array $props, string $path = null) { $this->path = $path; $this->props = $props; } /** * Compose the absolute "dot" path to the given key. * * @param string $key * @return string */ protected function dotPath(string $key = ''): string { if (is_null($this->path)) { return $key; } return rtrim(implode('.', [$this->path, $key]), '.'); } /** * Retrieve a prop within the current scope using "dot" notation. * * @param string|null $key * @return mixed */ protected function prop(string $key = null) { return Arr::get($this->props, $key); } /** * Instantiate a new "scope" at the path of the given key. * * @param string $key * @param \Closure $callback * @return $this */ protected function scope(string $key, Closure $callback): self { $props = $this->prop($key); $path = $this->dotPath($key); PHPUnit::assertIsArray($props, sprintf('Property [%s] is not scopeable.', $path)); $scope = new static($props, $path); $callback($scope); $scope->interacted(); return $this; } /** * Instantiate a new "scope" on the first child element. * * @param \Closure $callback * @return $this */ public function first(Closure $callback): self { $props = $this->prop(); $path = $this->dotPath(); PHPUnit::assertNotEmpty($props, $path === '' ? 'Cannot scope directly onto the first element of the root level because it is empty.' : sprintf('Cannot scope directly onto the first element of property [%s] because it is empty.', $path) ); $key = array_keys($props)[0]; $this->interactsWith($key); return $this->scope($key, $callback); } /** * Instantiate a new "scope" on each child element. * * @param \Closure $callback * @return $this */ public function each(Closure $callback): self { $props = $this->prop(); $path = $this->dotPath(); PHPUnit::assertNotEmpty($props, $path === '' ? 'Cannot scope directly onto each element of the root level because it is empty.' : sprintf('Cannot scope directly onto each element of property [%s] because it is empty.', $path) ); foreach (array_keys($props) as $key) { $this->interactsWith($key); $this->scope($key, $callback); } return $this; } /** * Create a new instance from an array. * * @param array $data * @return static */ public static function fromArray(array $data): self { return new static($data); } /** * Create a new instance from an AssertableJsonString. * * @param \Illuminate\Testing\AssertableJsonString $json * @return static */ public static function fromAssertableJsonString(AssertableJsonString $json): self { return static::fromArray($json->json()); } /** * Get the instance as an array. * * @return array */ public function toArray() { return $this->props; } } src/Illuminate/Config/composer.json 0000644 00000001466 15107327041 0013374 0 ustar 00 { "name": "illuminate/config", "description": "The Illuminate Config package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/collections": "^10.0", "illuminate/contracts": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Config\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Config/Repository.php 0000644 00000007035 15107327041 0013540 0 ustar 00 <?php namespace Illuminate\Config; use ArrayAccess; use Illuminate\Contracts\Config\Repository as ConfigContract; use Illuminate\Support\Arr; use Illuminate\Support\Traits\Macroable; class Repository implements ArrayAccess, ConfigContract { use Macroable; /** * All of the configuration items. * * @var array */ protected $items = []; /** * Create a new configuration repository. * * @param array $items * @return void */ public function __construct(array $items = []) { $this->items = $items; } /** * Determine if the given configuration value exists. * * @param string $key * @return bool */ public function has($key) { return Arr::has($this->items, $key); } /** * Get the specified configuration value. * * @param array|string $key * @param mixed $default * @return mixed */ public function get($key, $default = null) { if (is_array($key)) { return $this->getMany($key); } return Arr::get($this->items, $key, $default); } /** * Get many configuration values. * * @param array $keys * @return array */ public function getMany($keys) { $config = []; foreach ($keys as $key => $default) { if (is_numeric($key)) { [$key, $default] = [$default, null]; } $config[$key] = Arr::get($this->items, $key, $default); } return $config; } /** * Set a given configuration value. * * @param array|string $key * @param mixed $value * @return void */ public function set($key, $value = null) { $keys = is_array($key) ? $key : [$key => $value]; foreach ($keys as $key => $value) { Arr::set($this->items, $key, $value); } } /** * Prepend a value onto an array configuration value. * * @param string $key * @param mixed $value * @return void */ public function prepend($key, $value) { $array = $this->get($key, []); array_unshift($array, $value); $this->set($key, $array); } /** * Push a value onto an array configuration value. * * @param string $key * @param mixed $value * @return void */ public function push($key, $value) { $array = $this->get($key, []); $array[] = $value; $this->set($key, $array); } /** * Get all of the configuration items for the application. * * @return array */ public function all() { return $this->items; } /** * Determine if the given configuration option exists. * * @param string $key * @return bool */ public function offsetExists($key): bool { return $this->has($key); } /** * Get a configuration option. * * @param string $key * @return mixed */ public function offsetGet($key): mixed { return $this->get($key); } /** * Set a configuration option. * * @param string $key * @param mixed $value * @return void */ public function offsetSet($key, $value): void { $this->set($key, $value); } /** * Unset a configuration option. * * @param string $key * @return void */ public function offsetUnset($key): void { $this->set($key, null); } } src/Illuminate/Config/LICENSE.md 0000644 00000002063 15107327041 0012250 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Validation/ValidationException.php 0000644 00000007244 15107327041 0016221 0 ustar 00 <?php namespace Illuminate\Validation; use Exception; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Validator as ValidatorFacade; class ValidationException extends Exception { /** * The validator instance. * * @var \Illuminate\Contracts\Validation\Validator */ public $validator; /** * The recommended response to send to the client. * * @var \Symfony\Component\HttpFoundation\Response|null */ public $response; /** * The status code to use for the response. * * @var int */ public $status = 422; /** * The name of the error bag. * * @var string */ public $errorBag; /** * The path the client should be redirected to. * * @var string */ public $redirectTo; /** * Create a new exception instance. * * @param \Illuminate\Contracts\Validation\Validator $validator * @param \Symfony\Component\HttpFoundation\Response|null $response * @param string $errorBag * @return void */ public function __construct($validator, $response = null, $errorBag = 'default') { parent::__construct(static::summarize($validator)); $this->response = $response; $this->errorBag = $errorBag; $this->validator = $validator; } /** * Create a new validation exception from a plain array of messages. * * @param array $messages * @return static */ public static function withMessages(array $messages) { return new static(tap(ValidatorFacade::make([], []), function ($validator) use ($messages) { foreach ($messages as $key => $value) { foreach (Arr::wrap($value) as $message) { $validator->errors()->add($key, $message); } } })); } /** * Create an error message summary from the validation errors. * * @param \Illuminate\Contracts\Validation\Validator $validator * @return string */ protected static function summarize($validator) { $messages = $validator->errors()->all(); if (! count($messages) || ! is_string($messages[0])) { return $validator->getTranslator()->get('The given data was invalid.'); } $message = array_shift($messages); if ($count = count($messages)) { $pluralized = $count === 1 ? 'error' : 'errors'; $message .= ' '.$validator->getTranslator()->get("(and :count more $pluralized)", compact('count')); } return $message; } /** * Get all of the validation error messages. * * @return array */ public function errors() { return $this->validator->errors()->messages(); } /** * Set the HTTP status code to be used for the response. * * @param int $status * @return $this */ public function status($status) { $this->status = $status; return $this; } /** * Set the error bag on the exception. * * @param string $errorBag * @return $this */ public function errorBag($errorBag) { $this->errorBag = $errorBag; return $this; } /** * Set the URL to redirect to on a validation error. * * @param string $url * @return $this */ public function redirectTo($url) { $this->redirectTo = $url; return $this; } /** * Get the underlying response instance. * * @return \Symfony\Component\HttpFoundation\Response|null */ public function getResponse() { return $this->response; } } src/Illuminate/Validation/NestedRules.php 0000644 00000002350 15107327041 0014476 0 ustar 00 <?php namespace Illuminate\Validation; use Illuminate\Support\Arr; class NestedRules { /** * The callback to execute. * * @var callable */ protected $callback; /** * Create a new nested rule instance. * * @param callable $callback * @return void */ public function __construct(callable $callback) { $this->callback = $callback; } /** * Compile the callback into an array of rules. * * @param string $attribute * @param mixed $value * @param mixed $data * @return \stdClass */ public function compile($attribute, $value, $data = null) { $rules = call_user_func($this->callback, $value, $attribute, $data); $parser = new ValidationRuleParser( Arr::undot(Arr::wrap($data)) ); if (is_array($rules) && ! array_is_list($rules)) { $nested = []; foreach ($rules as $key => $rule) { $nested[$attribute.'.'.$key] = $rule; } $rules = $nested; } else { $rules = [$attribute => $rules]; } return $parser->explode(ValidationRuleParser::filterConditionalRules($rules, $data)); } } src/Illuminate/Validation/PresenceVerifierInterface.php 0000644 00000001516 15107327041 0017325 0 ustar 00 <?php namespace Illuminate\Validation; interface PresenceVerifierInterface { /** * Count the number of objects in a collection having the given value. * * @param string $collection * @param string $column * @param string $value * @param int|null $excludeId * @param string|null $idColumn * @param array $extra * @return int */ public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = []); /** * Count the number of objects in a collection with the given values. * * @param string $collection * @param string $column * @param array $values * @param array $extra * @return int */ public function getMultiCount($collection, $column, array $values, array $extra = []); } src/Illuminate/Validation/ValidationData.php 0000644 00000005554 15107327041 0015136 0 ustar 00 <?php namespace Illuminate\Validation; use Illuminate\Support\Arr; class ValidationData { /** * Initialize and gather data for the given attribute. * * @param string $attribute * @param array $masterData * @return array */ public static function initializeAndGatherData($attribute, $masterData) { $data = Arr::dot(static::initializeAttributeOnData($attribute, $masterData)); return array_merge($data, static::extractValuesForWildcards( $masterData, $data, $attribute )); } /** * Gather a copy of the attribute data filled with any missing attributes. * * @param string $attribute * @param array $masterData * @return array */ protected static function initializeAttributeOnData($attribute, $masterData) { $explicitPath = static::getLeadingExplicitAttributePath($attribute); $data = static::extractDataFromPath($explicitPath, $masterData); if (! str_contains($attribute, '*') || str_ends_with($attribute, '*')) { return $data; } return data_set($data, $attribute, null, true); } /** * Get all of the exact attribute values for a given wildcard attribute. * * @param array $masterData * @param array $data * @param string $attribute * @return array */ protected static function extractValuesForWildcards($masterData, $data, $attribute) { $keys = []; $pattern = str_replace('\*', '[^\.]+', preg_quote($attribute, '/')); foreach ($data as $key => $value) { if ((bool) preg_match('/^'.$pattern.'/', $key, $matches)) { $keys[] = $matches[0]; } } $keys = array_unique($keys); $data = []; foreach ($keys as $key) { $data[$key] = Arr::get($masterData, $key); } return $data; } /** * Extract data based on the given dot-notated path. * * Used to extract a sub-section of the data for faster iteration. * * @param string $attribute * @param array $masterData * @return array */ public static function extractDataFromPath($attribute, $masterData) { $results = []; $value = Arr::get($masterData, $attribute, '__missing__'); if ($value !== '__missing__') { Arr::set($results, $attribute, $value); } return $results; } /** * Get the explicit part of the attribute name. * * E.g. 'foo.bar.*.baz' -> 'foo.bar' * * Allows us to not spin through all of the flattened data for some operations. * * @param string $attribute * @return string */ public static function getLeadingExplicitAttributePath($attribute) { return rtrim(explode('*', $attribute)[0], '.') ?: null; } } src/Illuminate/Validation/Concerns/FilterEmailValidation.php 0000644 00000003206 15107327041 0020224 0 ustar 00 <?php namespace Illuminate\Validation\Concerns; use Egulias\EmailValidator\EmailLexer; use Egulias\EmailValidator\Result\InvalidEmail; use Egulias\EmailValidator\Validation\EmailValidation; class FilterEmailValidation implements EmailValidation { /** * The flags to pass to the filter_var function. * * @var int|null */ protected $flags; /** * Create a new validation instance. * * @param int $flags * @return void */ public function __construct($flags = null) { $this->flags = $flags; } /** * Create a new instance which allows any unicode characters in local-part. * * @return static */ public static function unicode() { return new static(FILTER_FLAG_EMAIL_UNICODE); } /** * Returns true if the given email is valid. * * @param string $email * @param \Egulias\EmailValidator\EmailLexer $emailLexer * @return bool */ public function isValid(string $email, EmailLexer $emailLexer): bool { return is_null($this->flags) ? filter_var($email, FILTER_VALIDATE_EMAIL) !== false : filter_var($email, FILTER_VALIDATE_EMAIL, $this->flags) !== false; } /** * Returns the validation error. * * @return \Egulias\EmailValidator\Result\InvalidEmail|null */ public function getError(): ?InvalidEmail { return null; } /** * Returns the validation warnings. * * @return \Egulias\EmailValidator\Warning\Warning[] */ public function getWarnings(): array { return []; } } src/Illuminate/Validation/Concerns/ValidatesAttributes.php 0000644 00000215316 15107327041 0020006 0 ustar 00 <?php namespace Illuminate\Validation\Concerns; use Brick\Math\BigDecimal; use Brick\Math\BigNumber; use Brick\Math\Exception\MathException as BrickMathException; use DateTime; use DateTimeInterface; use DateTimeZone; use Egulias\EmailValidator\EmailValidator; use Egulias\EmailValidator\Validation\DNSCheckValidation; use Egulias\EmailValidator\Validation\Extra\SpoofCheckValidation; use Egulias\EmailValidator\Validation\MultipleValidationWithAnd; use Egulias\EmailValidator\Validation\NoRFCWarningsValidation; use Egulias\EmailValidator\Validation\RFCValidation; use Exception; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Illuminate\Support\Exceptions\MathException; use Illuminate\Support\Facades\Date; use Illuminate\Support\Str; use Illuminate\Validation\Rules\Exists; use Illuminate\Validation\Rules\Unique; use Illuminate\Validation\ValidationData; use InvalidArgumentException; use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\UploadedFile; use ValueError; trait ValidatesAttributes { /** * Validate that an attribute was "accepted". * * This validation rule implies the attribute is "required". * * @param string $attribute * @param mixed $value * @return bool */ public function validateAccepted($attribute, $value) { $acceptable = ['yes', 'on', '1', 1, true, 'true']; return $this->validateRequired($attribute, $value) && in_array($value, $acceptable, true); } /** * Validate that an attribute was "accepted" when another attribute has a given value. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateAcceptedIf($attribute, $value, $parameters) { $acceptable = ['yes', 'on', '1', 1, true, 'true']; $this->requireParameterCount(2, $parameters, 'accepted_if'); [$values, $other] = $this->parseDependentRuleParameters($parameters); if (in_array($other, $values, is_bool($other) || is_null($other))) { return $this->validateRequired($attribute, $value) && in_array($value, $acceptable, true); } return true; } /** * Validate that an attribute was "declined". * * This validation rule implies the attribute is "required". * * @param string $attribute * @param mixed $value * @return bool */ public function validateDeclined($attribute, $value) { $acceptable = ['no', 'off', '0', 0, false, 'false']; return $this->validateRequired($attribute, $value) && in_array($value, $acceptable, true); } /** * Validate that an attribute was "declined" when another attribute has a given value. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateDeclinedIf($attribute, $value, $parameters) { $acceptable = ['no', 'off', '0', 0, false, 'false']; $this->requireParameterCount(2, $parameters, 'declined_if'); [$values, $other] = $this->parseDependentRuleParameters($parameters); if (in_array($other, $values, is_bool($other) || is_null($other))) { return $this->validateRequired($attribute, $value) && in_array($value, $acceptable, true); } return true; } /** * Validate that an attribute is an active URL. * * @param string $attribute * @param mixed $value * @return bool */ public function validateActiveUrl($attribute, $value) { if (! is_string($value)) { return false; } if ($url = parse_url($value, PHP_URL_HOST)) { try { $records = $this->getDnsRecords($url.'.', DNS_A | DNS_AAAA); if (is_array($records) && count($records) > 0) { return true; } } catch (Exception) { return false; } } return false; } /** * Get the DNS records for the given hostname. * * @param string $hostname * @param int $type * @return array|false */ protected function getDnsRecords($hostname, $type) { return dns_get_record($hostname, $type); } /** * Validate that an attribute is 7 bit ASCII. * * @param string $attribute * @param mixed $value * @return bool */ public function validateAscii($attribute, $value) { return Str::isAscii($value); } /** * "Break" on first validation fail. * * Always returns true, just lets us put "bail" in rules. * * @return bool */ public function validateBail() { return true; } /** * Validate the date is before a given date. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateBefore($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'before'); return $this->compareDates($attribute, $value, $parameters, '<'); } /** * Validate the date is before or equal a given date. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateBeforeOrEqual($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'before_or_equal'); return $this->compareDates($attribute, $value, $parameters, '<='); } /** * Validate the date is after a given date. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateAfter($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'after'); return $this->compareDates($attribute, $value, $parameters, '>'); } /** * Validate the date is equal or after a given date. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateAfterOrEqual($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'after_or_equal'); return $this->compareDates($attribute, $value, $parameters, '>='); } /** * Compare a given date against another using an operator. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @param string $operator * @return bool */ protected function compareDates($attribute, $value, $parameters, $operator) { if (! is_string($value) && ! is_numeric($value) && ! $value instanceof DateTimeInterface) { return false; } if ($format = $this->getDateFormat($attribute)) { return $this->checkDateTimeOrder($format, $value, $parameters[0], $operator); } if (is_null($date = $this->getDateTimestamp($parameters[0]))) { $date = $this->getDateTimestamp($this->getValue($parameters[0])); } return $this->compare($this->getDateTimestamp($value), $date, $operator); } /** * Get the date format for an attribute if it has one. * * @param string $attribute * @return string|null */ protected function getDateFormat($attribute) { if ($result = $this->getRule($attribute, 'DateFormat')) { return $result[1][0]; } } /** * Get the date timestamp. * * @param mixed $value * @return int */ protected function getDateTimestamp($value) { $date = is_null($value) ? null : $this->getDateTime($value); return $date ? $date->getTimestamp() : null; } /** * Given two date/time strings, check that one is after the other. * * @param string $format * @param string $first * @param string $second * @param string $operator * @return bool */ protected function checkDateTimeOrder($format, $first, $second, $operator) { $firstDate = $this->getDateTimeWithOptionalFormat($format, $first); if (! $secondDate = $this->getDateTimeWithOptionalFormat($format, $second)) { if (is_null($second = $this->getValue($second))) { return true; } $secondDate = $this->getDateTimeWithOptionalFormat($format, $second); } return ($firstDate && $secondDate) && $this->compare($firstDate, $secondDate, $operator); } /** * Get a DateTime instance from a string. * * @param string $format * @param string $value * @return \DateTime|null */ protected function getDateTimeWithOptionalFormat($format, $value) { if ($date = DateTime::createFromFormat('!'.$format, $value)) { return $date; } return $this->getDateTime($value); } /** * Get a DateTime instance from a string with no format. * * @param string $value * @return \DateTime|null */ protected function getDateTime($value) { try { return @Date::parse($value) ?: null; } catch (Exception) { // } } /** * Validate that an attribute contains only alphabetic characters. * If the 'ascii' option is passed, validate that an attribute contains only ascii alphabetic characters. * * @param string $attribute * @param mixed $value * @return bool */ public function validateAlpha($attribute, $value, $parameters) { if (isset($parameters[0]) && $parameters[0] === 'ascii') { return is_string($value) && preg_match('/\A[a-zA-Z]+\z/u', $value); } return is_string($value) && preg_match('/\A[\pL\pM]+\z/u', $value); } /** * Validate that an attribute contains only alpha-numeric characters, dashes, and underscores. * If the 'ascii' option is passed, validate that an attribute contains only ascii alpha-numeric characters, * dashes, and underscores. * * @param string $attribute * @param mixed $value * @return bool */ public function validateAlphaDash($attribute, $value, $parameters) { if (! is_string($value) && ! is_numeric($value)) { return false; } if (isset($parameters[0]) && $parameters[0] === 'ascii') { return preg_match('/\A[a-zA-Z0-9_-]+\z/u', $value) > 0; } return preg_match('/\A[\pL\pM\pN_-]+\z/u', $value) > 0; } /** * Validate that an attribute contains only alpha-numeric characters. * If the 'ascii' option is passed, validate that an attribute contains only ascii alpha-numeric characters. * * @param string $attribute * @param mixed $value * @return bool */ public function validateAlphaNum($attribute, $value, $parameters) { if (! is_string($value) && ! is_numeric($value)) { return false; } if (isset($parameters[0]) && $parameters[0] === 'ascii') { return preg_match('/\A[a-zA-Z0-9]+\z/u', $value) > 0; } return preg_match('/\A[\pL\pM\pN]+\z/u', $value) > 0; } /** * Validate that an attribute is an array. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateArray($attribute, $value, $parameters = []) { if (! is_array($value)) { return false; } if (empty($parameters)) { return true; } return empty(array_diff_key($value, array_fill_keys($parameters, ''))); } /** * Validate that an array has all of the given keys. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateRequiredArrayKeys($attribute, $value, $parameters) { if (! is_array($value)) { return false; } foreach ($parameters as $param) { if (! Arr::exists($value, $param)) { return false; } } return true; } /** * Validate the size of an attribute is between a set of values. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateBetween($attribute, $value, $parameters) { $this->requireParameterCount(2, $parameters, 'between'); return with( BigNumber::of($this->getSize($attribute, $value)), fn ($size) => $size->isGreaterThanOrEqualTo($this->trim($parameters[0])) && $size->isLessThanOrEqualTo($this->trim($parameters[1])) ); } /** * Validate that an attribute is a boolean. * * @param string $attribute * @param mixed $value * @return bool */ public function validateBoolean($attribute, $value) { $acceptable = [true, false, 0, 1, '0', '1']; return in_array($value, $acceptable, true); } /** * Validate that an attribute has a matching confirmation. * * @param string $attribute * @param mixed $value * @return bool */ public function validateConfirmed($attribute, $value) { return $this->validateSame($attribute, $value, [$attribute.'_confirmation']); } /** * Validate that the password of the currently authenticated user matches the given value. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ protected function validateCurrentPassword($attribute, $value, $parameters) { $auth = $this->container->make('auth'); $hasher = $this->container->make('hash'); $guard = $auth->guard(Arr::first($parameters)); if ($guard->guest()) { return false; } return $hasher->check($value, $guard->user()->getAuthPassword()); } /** * Validate that an attribute is a valid date. * * @param string $attribute * @param mixed $value * @return bool */ public function validateDate($attribute, $value) { if ($value instanceof DateTimeInterface) { return true; } try { if ((! is_string($value) && ! is_numeric($value)) || strtotime($value) === false) { return false; } } catch (Exception) { return false; } $date = date_parse($value); return checkdate($date['month'], $date['day'], $date['year']); } /** * Validate that an attribute matches a date format. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateDateFormat($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'date_format'); if (! is_string($value) && ! is_numeric($value)) { return false; } foreach ($parameters as $format) { try { $date = DateTime::createFromFormat('!'.$format, $value); if ($date && $date->format($format) == $value) { return true; } } catch (ValueError) { return false; } } return false; } /** * Validate that an attribute is equal to another date. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateDateEquals($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'date_equals'); return $this->compareDates($attribute, $value, $parameters, '='); } /** * Validate that an attribute has a given number of decimal places. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateDecimal($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'decimal'); if (! $this->validateNumeric($attribute, $value)) { return false; } $matches = []; if (preg_match('/^[+-]?\d*\.?(\d*)$/', $value, $matches) !== 1) { return false; } $decimals = strlen(end($matches)); if (! isset($parameters[1])) { return $decimals == $parameters[0]; } return $decimals >= $parameters[0] && $decimals <= $parameters[1]; } /** * Validate that an attribute is different from another attribute. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateDifferent($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'different'); foreach ($parameters as $parameter) { if (Arr::has($this->data, $parameter)) { $other = Arr::get($this->data, $parameter); if ($value === $other) { return false; } } } return true; } /** * Validate that an attribute has a given number of digits. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateDigits($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'digits'); return ! preg_match('/[^0-9]/', $value) && strlen((string) $value) == $parameters[0]; } /** * Validate that an attribute is between a given number of digits. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateDigitsBetween($attribute, $value, $parameters) { $this->requireParameterCount(2, $parameters, 'digits_between'); $length = strlen((string) $value); return ! preg_match('/[^0-9]/', $value) && $length >= $parameters[0] && $length <= $parameters[1]; } /** * Validate the dimensions of an image matches the given values. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateDimensions($attribute, $value, $parameters) { if ($this->isValidFileInstance($value) && in_array($value->getMimeType(), ['image/svg+xml', 'image/svg'])) { return true; } if (! $this->isValidFileInstance($value)) { return false; } $dimensions = method_exists($value, 'dimensions') ? $value->dimensions() : @getimagesize($value->getRealPath()); if (! $dimensions) { return false; } $this->requireParameterCount(1, $parameters, 'dimensions'); [$width, $height] = $dimensions; $parameters = $this->parseNamedParameters($parameters); if ($this->failsBasicDimensionChecks($parameters, $width, $height) || $this->failsRatioCheck($parameters, $width, $height)) { return false; } return true; } /** * Test if the given width and height fail any conditions. * * @param array<string,string> $parameters * @param int $width * @param int $height * @return bool */ protected function failsBasicDimensionChecks($parameters, $width, $height) { return (isset($parameters['width']) && $parameters['width'] != $width) || (isset($parameters['min_width']) && $parameters['min_width'] > $width) || (isset($parameters['max_width']) && $parameters['max_width'] < $width) || (isset($parameters['height']) && $parameters['height'] != $height) || (isset($parameters['min_height']) && $parameters['min_height'] > $height) || (isset($parameters['max_height']) && $parameters['max_height'] < $height); } /** * Determine if the given parameters fail a dimension ratio check. * * @param array<string,string> $parameters * @param int $width * @param int $height * @return bool */ protected function failsRatioCheck($parameters, $width, $height) { if (! isset($parameters['ratio'])) { return false; } [$numerator, $denominator] = array_replace( [1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d')) ); $precision = 1 / (max($width, $height) + 1); return abs($numerator / $denominator - $width / $height) > $precision; } /** * Validate an attribute is unique among other values. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateDistinct($attribute, $value, $parameters) { $data = Arr::except($this->getDistinctValues($attribute), $attribute); if (in_array('ignore_case', $parameters)) { return empty(preg_grep('/^'.preg_quote($value, '/').'$/iu', $data)); } return ! in_array($value, array_values($data), in_array('strict', $parameters)); } /** * Get the values to distinct between. * * @param string $attribute * @return array */ protected function getDistinctValues($attribute) { $attributeName = $this->getPrimaryAttribute($attribute); if (! property_exists($this, 'distinctValues')) { return $this->extractDistinctValues($attributeName); } if (! array_key_exists($attributeName, $this->distinctValues)) { $this->distinctValues[$attributeName] = $this->extractDistinctValues($attributeName); } return $this->distinctValues[$attributeName]; } /** * Extract the distinct values from the data. * * @param string $attribute * @return array */ protected function extractDistinctValues($attribute) { $attributeData = ValidationData::extractDataFromPath( ValidationData::getLeadingExplicitAttributePath($attribute), $this->data ); $pattern = str_replace('\*', '[^.]+', preg_quote($attribute, '#')); return Arr::where(Arr::dot($attributeData), function ($value, $key) use ($pattern) { return (bool) preg_match('#^'.$pattern.'\z#u', $key); }); } /** * Validate that an attribute is a valid e-mail address. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateEmail($attribute, $value, $parameters) { if (! is_string($value) && ! (is_object($value) && method_exists($value, '__toString'))) { return false; } $validations = collect($parameters) ->unique() ->map(fn ($validation) => match (true) { $validation === 'strict' => new NoRFCWarningsValidation(), $validation === 'dns' => new DNSCheckValidation(), $validation === 'spoof' => new SpoofCheckValidation(), $validation === 'filter' => new FilterEmailValidation(), $validation === 'filter_unicode' => FilterEmailValidation::unicode(), is_string($validation) && class_exists($validation) => $this->container->make($validation), default => new RFCValidation(), }) ->values() ->all() ?: [new RFCValidation]; return (new EmailValidator)->isValid($value, new MultipleValidationWithAnd($validations)); } /** * Validate the existence of an attribute value in a database table. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateExists($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'exists'); [$connection, $table] = $this->parseTable($parameters[0]); // The second parameter position holds the name of the column that should be // verified as existing. If this parameter is not specified we will guess // that the columns being "verified" shares the given attribute's name. $column = $this->getQueryColumn($parameters, $attribute); $expected = is_array($value) ? count(array_unique($value)) : 1; if ($expected === 0) { return true; } return $this->getExistCount( $connection, $table, $column, $value, $parameters ) >= $expected; } /** * Get the number of records that exist in storage. * * @param mixed $connection * @param string $table * @param string $column * @param mixed $value * @param array<int, int|string> $parameters * @return int */ protected function getExistCount($connection, $table, $column, $value, $parameters) { $verifier = $this->getPresenceVerifier($connection); $extra = $this->getExtraConditions( array_values(array_slice($parameters, 2)) ); if ($this->currentRule instanceof Exists) { $extra = array_merge($extra, $this->currentRule->queryCallbacks()); } return is_array($value) ? $verifier->getMultiCount($table, $column, $value, $extra) : $verifier->getCount($table, $column, $value, null, null, $extra); } /** * Validate the uniqueness of an attribute value on a given database table. * * If a database column is not specified, the attribute will be used. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateUnique($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'unique'); [$connection, $table, $idColumn] = $this->parseTable($parameters[0]); // The second parameter position holds the name of the column that needs to // be verified as unique. If this parameter isn't specified we will just // assume that this column to be verified shares the attribute's name. $column = $this->getQueryColumn($parameters, $attribute); $id = null; if (isset($parameters[2])) { [$idColumn, $id] = $this->getUniqueIds($idColumn, $parameters); if (! is_null($id)) { $id = stripslashes($id); } } // The presence verifier is responsible for counting rows within this store // mechanism which might be a relational database or any other permanent // data store like Redis, etc. We will use it to determine uniqueness. $verifier = $this->getPresenceVerifier($connection); $extra = $this->getUniqueExtra($parameters); if ($this->currentRule instanceof Unique) { $extra = array_merge($extra, $this->currentRule->queryCallbacks()); } return $verifier->getCount( $table, $column, $value, $id, $idColumn, $extra ) == 0; } /** * Get the excluded ID column and value for the unique rule. * * @param string|null $idColumn * @param array<int, int|string> $parameters * @return array */ protected function getUniqueIds($idColumn, $parameters) { $idColumn ??= $parameters[3] ?? 'id'; return [$idColumn, $this->prepareUniqueId($parameters[2])]; } /** * Prepare the given ID for querying. * * @param mixed $id * @return int */ protected function prepareUniqueId($id) { if (preg_match('/\[(.*)\]/', $id, $matches)) { $id = $this->getValue($matches[1]); } if (strtolower($id) === 'null') { $id = null; } if (filter_var($id, FILTER_VALIDATE_INT) !== false) { $id = (int) $id; } return $id; } /** * Get the extra conditions for a unique rule. * * @param array<int, int|string> $parameters * @return array */ protected function getUniqueExtra($parameters) { if (isset($parameters[4])) { return $this->getExtraConditions(array_slice($parameters, 4)); } return []; } /** * Parse the connection / table for the unique / exists rules. * * @param string $table * @return array */ public function parseTable($table) { [$connection, $table] = str_contains($table, '.') ? explode('.', $table, 2) : [null, $table]; if (str_contains($table, '\\') && class_exists($table) && is_a($table, Model::class, true)) { $model = new $table; $table = $model->getTable(); $connection ??= $model->getConnectionName(); if (str_contains($table, '.') && Str::startsWith($table, $connection)) { $connection = null; } $idColumn = $model->getKeyName(); } return [$connection, $table, $idColumn ?? null]; } /** * Get the column name for an exists / unique query. * * @param array<int, int|string> $parameters * @param string $attribute * @return bool */ public function getQueryColumn($parameters, $attribute) { return isset($parameters[1]) && $parameters[1] !== 'NULL' ? $parameters[1] : $this->guessColumnForQuery($attribute); } /** * Guess the database column from the given attribute name. * * @param string $attribute * @return string */ public function guessColumnForQuery($attribute) { if (in_array($attribute, Arr::collapse($this->implicitAttributes)) && ! is_numeric($last = last(explode('.', $attribute)))) { return $last; } return $attribute; } /** * Get the extra conditions for a unique / exists rule. * * @param array $segments * @return array */ protected function getExtraConditions(array $segments) { $extra = []; $count = count($segments); for ($i = 0; $i < $count; $i += 2) { $extra[$segments[$i]] = $segments[$i + 1]; } return $extra; } /** * Validate the given value is a valid file. * * @param string $attribute * @param mixed $value * @return bool */ public function validateFile($attribute, $value) { return $this->isValidFileInstance($value); } /** * Validate the given attribute is filled if it is present. * * @param string $attribute * @param mixed $value * @return bool */ public function validateFilled($attribute, $value) { if (Arr::has($this->data, $attribute)) { return $this->validateRequired($attribute, $value); } return true; } /** * Validate that an attribute is greater than another attribute. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateGt($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'gt'); $comparedToValue = $this->getValue($parameters[0]); $this->shouldBeNumeric($attribute, 'Gt'); if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) { return BigNumber::of($this->getSize($attribute, $value))->isGreaterThan($this->trim($parameters[0])); } if (is_numeric($parameters[0])) { return false; } if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) { return BigNumber::of($this->trim($value))->isGreaterThan($this->trim($comparedToValue)); } if (! $this->isSameType($value, $comparedToValue)) { return false; } return $this->getSize($attribute, $value) > $this->getSize($attribute, $comparedToValue); } /** * Validate that an attribute is less than another attribute. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateLt($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'lt'); $comparedToValue = $this->getValue($parameters[0]); $this->shouldBeNumeric($attribute, 'Lt'); if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) { return BigNumber::of($this->getSize($attribute, $value))->isLessThan($this->trim($parameters[0])); } if (is_numeric($parameters[0])) { return false; } if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) { return BigNumber::of($this->trim($value))->isLessThan($this->trim($comparedToValue)); } if (! $this->isSameType($value, $comparedToValue)) { return false; } return $this->getSize($attribute, $value) < $this->getSize($attribute, $comparedToValue); } /** * Validate that an attribute is greater than or equal another attribute. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateGte($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'gte'); $comparedToValue = $this->getValue($parameters[0]); $this->shouldBeNumeric($attribute, 'Gte'); if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) { return BigNumber::of($this->getSize($attribute, $value))->isGreaterThanOrEqualTo($this->trim($parameters[0])); } if (is_numeric($parameters[0])) { return false; } if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) { return BigNumber::of($this->trim($value))->isGreaterThanOrEqualTo($this->trim($comparedToValue)); } if (! $this->isSameType($value, $comparedToValue)) { return false; } return $this->getSize($attribute, $value) >= $this->getSize($attribute, $comparedToValue); } /** * Validate that an attribute is less than or equal another attribute. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateLte($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'lte'); $comparedToValue = $this->getValue($parameters[0]); $this->shouldBeNumeric($attribute, 'Lte'); if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) { return BigNumber::of($this->getSize($attribute, $value))->isLessThanOrEqualTo($this->trim($parameters[0])); } if (is_numeric($parameters[0])) { return false; } if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) { return BigNumber::of($this->trim($value))->isLessThanOrEqualTo($this->trim($comparedToValue)); } if (! $this->isSameType($value, $comparedToValue)) { return false; } return $this->getSize($attribute, $value) <= $this->getSize($attribute, $comparedToValue); } /** * Validate that an attribute is lowercase. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateLowercase($attribute, $value, $parameters) { return Str::lower($value) === $value; } /** * Validate that an attribute is uppercase. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateUppercase($attribute, $value, $parameters) { return Str::upper($value) === $value; } /** * Validate the MIME type of a file is an image MIME type. * * @param string $attribute * @param mixed $value * @return bool */ public function validateImage($attribute, $value) { return $this->validateMimes($attribute, $value, ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp']); } /** * Validate an attribute is contained within a list of values. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateIn($attribute, $value, $parameters) { if (is_array($value) && $this->hasRule($attribute, 'Array')) { foreach ($value as $element) { if (is_array($element)) { return false; } } return count(array_diff($value, $parameters)) === 0; } return ! is_array($value) && in_array((string) $value, $parameters); } /** * Validate that the values of an attribute are in another attribute. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateInArray($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'in_array'); $explicitPath = ValidationData::getLeadingExplicitAttributePath($parameters[0]); $attributeData = ValidationData::extractDataFromPath($explicitPath, $this->data); $otherValues = Arr::where(Arr::dot($attributeData), function ($value, $key) use ($parameters) { return Str::is($parameters[0], $key); }); return in_array($value, $otherValues); } /** * Validate that an attribute is an integer. * * @param string $attribute * @param mixed $value * @return bool */ public function validateInteger($attribute, $value) { return filter_var($value, FILTER_VALIDATE_INT) !== false; } /** * Validate that an attribute is a valid IP. * * @param string $attribute * @param mixed $value * @return bool */ public function validateIp($attribute, $value) { return filter_var($value, FILTER_VALIDATE_IP) !== false; } /** * Validate that an attribute is a valid IPv4. * * @param string $attribute * @param mixed $value * @return bool */ public function validateIpv4($attribute, $value) { return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false; } /** * Validate that an attribute is a valid IPv6. * * @param string $attribute * @param mixed $value * @return bool */ public function validateIpv6($attribute, $value) { return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false; } /** * Validate that an attribute is a valid MAC address. * * @param string $attribute * @param mixed $value * @return bool */ public function validateMacAddress($attribute, $value) { return filter_var($value, FILTER_VALIDATE_MAC) !== false; } /** * Validate the attribute is a valid JSON string. * * @param string $attribute * @param mixed $value * @return bool */ public function validateJson($attribute, $value) { if (is_array($value)) { return false; } if (! is_scalar($value) && ! is_null($value) && ! method_exists($value, '__toString')) { return false; } json_decode($value); return json_last_error() === JSON_ERROR_NONE; } /** * Validate the size of an attribute is less than a maximum value. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateMax($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'max'); if ($value instanceof UploadedFile && ! $value->isValid()) { return false; } return BigNumber::of($this->getSize($attribute, $value))->isLessThanOrEqualTo($this->trim($parameters[0])); } /** * Validate that an attribute has a maximum number of digits. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateMaxDigits($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'max_digits'); $length = strlen((string) $value); return ! preg_match('/[^0-9]/', $value) && $length <= $parameters[0]; } /** * Validate the guessed extension of a file upload is in a set of file extensions. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateMimes($attribute, $value, $parameters) { if (! $this->isValidFileInstance($value)) { return false; } if ($this->shouldBlockPhpUpload($value, $parameters)) { return false; } if (in_array('jpg', $parameters) || in_array('jpeg', $parameters)) { $parameters = array_unique(array_merge($parameters, ['jpg', 'jpeg'])); } return $value->getPath() !== '' && in_array($value->guessExtension(), $parameters); } /** * Validate the MIME type of a file upload attribute is in a set of MIME types. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateMimetypes($attribute, $value, $parameters) { if (! $this->isValidFileInstance($value)) { return false; } if ($this->shouldBlockPhpUpload($value, $parameters)) { return false; } return $value->getPath() !== '' && (in_array($value->getMimeType(), $parameters) || in_array(explode('/', $value->getMimeType())[0].'/*', $parameters)); } /** * Check if PHP uploads are explicitly allowed. * * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ protected function shouldBlockPhpUpload($value, $parameters) { if (in_array('php', $parameters)) { return false; } $phpExtensions = [ 'php', 'php3', 'php4', 'php5', 'php7', 'php8', 'phtml', 'phar', ]; return ($value instanceof UploadedFile) ? in_array(trim(strtolower($value->getClientOriginalExtension())), $phpExtensions) : in_array(trim(strtolower($value->getExtension())), $phpExtensions); } /** * Validate the size of an attribute is greater than a minimum value. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateMin($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'min'); return BigNumber::of($this->getSize($attribute, $value))->isGreaterThanOrEqualTo($this->trim($parameters[0])); } /** * Validate that an attribute has a minimum number of digits. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateMinDigits($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'min_digits'); $length = strlen((string) $value); return ! preg_match('/[^0-9]/', $value) && $length >= $parameters[0]; } /** * Validate that an attribute is missing. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateMissing($attribute, $value, $parameters) { return ! Arr::has($this->data, $attribute); } /** * Validate that an attribute is missing when another attribute has a given value. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateMissingIf($attribute, $value, $parameters) { $this->requireParameterCount(2, $parameters, 'missing_if'); [$values, $other] = $this->parseDependentRuleParameters($parameters); if (in_array($other, $values, is_bool($other) || is_null($other))) { return $this->validateMissing($attribute, $value, $parameters); } return true; } /** * Validate that an attribute is missing unless another attribute has a given value. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateMissingUnless($attribute, $value, $parameters) { $this->requireParameterCount(2, $parameters, 'missing_unless'); [$values, $other] = $this->parseDependentRuleParameters($parameters); if (! in_array($other, $values, is_bool($other) || is_null($other))) { return $this->validateMissing($attribute, $value, $parameters); } return true; } /** * Validate that an attribute is missing when any given attribute is present. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateMissingWith($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'missing_with'); if (Arr::hasAny($this->data, $parameters)) { return $this->validateMissing($attribute, $value, $parameters); } return true; } /** * Validate that an attribute is missing when all given attributes are present. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateMissingWithAll($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'missing_with_all'); if (Arr::has($this->data, $parameters)) { return $this->validateMissing($attribute, $value, $parameters); } return true; } /** * Validate the value of an attribute is a multiple of a given value. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateMultipleOf($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'multiple_of'); if (! $this->validateNumeric($attribute, $value) || ! $this->validateNumeric($attribute, $parameters[0])) { return false; } try { $numerator = BigDecimal::of($this->trim($value)); $denominator = BigDecimal::of($this->trim($parameters[0])); if ($numerator->isZero() && $denominator->isZero()) { return false; } if ($numerator->isZero()) { return true; } if ($denominator->isZero()) { return false; } return $numerator->remainder($denominator)->isZero(); } catch (BrickMathException $e) { throw new MathException('An error occurred while handling the multiple_of input values.', previous: $e); } } /** * "Indicate" validation should pass if value is null. * * Always returns true, just lets us put "nullable" in rules. * * @return bool */ public function validateNullable() { return true; } /** * Validate an attribute is not contained within a list of values. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateNotIn($attribute, $value, $parameters) { return ! $this->validateIn($attribute, $value, $parameters); } /** * Validate that an attribute is numeric. * * @param string $attribute * @param mixed $value * @return bool */ public function validateNumeric($attribute, $value) { return is_numeric($value); } /** * Validate that an attribute exists even if not filled. * * @param string $attribute * @param mixed $value * @return bool */ public function validatePresent($attribute, $value) { return Arr::has($this->data, $attribute); } /** * Validate that an attribute passes a regular expression check. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateRegex($attribute, $value, $parameters) { if (! is_string($value) && ! is_numeric($value)) { return false; } $this->requireParameterCount(1, $parameters, 'regex'); return preg_match($parameters[0], $value) > 0; } /** * Validate that an attribute does not pass a regular expression check. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateNotRegex($attribute, $value, $parameters) { if (! is_string($value) && ! is_numeric($value)) { return false; } $this->requireParameterCount(1, $parameters, 'not_regex'); return preg_match($parameters[0], $value) < 1; } /** * Validate that a required attribute exists. * * @param string $attribute * @param mixed $value * @return bool */ public function validateRequired($attribute, $value) { if (is_null($value)) { return false; } elseif (is_string($value) && trim($value) === '') { return false; } elseif (is_countable($value) && count($value) < 1) { return false; } elseif ($value instanceof File) { return (string) $value->getPath() !== ''; } return true; } /** * Validate that an attribute exists when another attribute has a given value. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateRequiredIf($attribute, $value, $parameters) { $this->requireParameterCount(2, $parameters, 'required_if'); if (! Arr::has($this->data, $parameters[0])) { return true; } [$values, $other] = $this->parseDependentRuleParameters($parameters); if (in_array($other, $values, is_bool($other) || is_null($other))) { return $this->validateRequired($attribute, $value); } return true; } /** * Validate that an attribute exists when another attribute was "accepted". * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateRequiredIfAccepted($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'required_if_accepted'); if ($this->validateAccepted($parameters[0], $this->getValue($parameters[0]))) { return $this->validateRequired($attribute, $value); } return true; } /** * Validate that an attribute does not exist or is an empty string. * * @param string $attribute * @param mixed $value * @return bool */ public function validateProhibited($attribute, $value) { return ! $this->validateRequired($attribute, $value); } /** * Validate that an attribute does not exist when another attribute has a given value. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateProhibitedIf($attribute, $value, $parameters) { $this->requireParameterCount(2, $parameters, 'prohibited_if'); [$values, $other] = $this->parseDependentRuleParameters($parameters); if (in_array($other, $values, is_bool($other) || is_null($other))) { return ! $this->validateRequired($attribute, $value); } return true; } /** * Validate that an attribute does not exist unless another attribute has a given value. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateProhibitedUnless($attribute, $value, $parameters) { $this->requireParameterCount(2, $parameters, 'prohibited_unless'); [$values, $other] = $this->parseDependentRuleParameters($parameters); if (! in_array($other, $values, is_bool($other) || is_null($other))) { return ! $this->validateRequired($attribute, $value); } return true; } /** * Validate that other attributes do not exist when this attribute exists. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateProhibits($attribute, $value, $parameters) { if ($this->validateRequired($attribute, $value)) { foreach ($parameters as $parameter) { if ($this->validateRequired($parameter, Arr::get($this->data, $parameter))) { return false; } } } return true; } /** * Indicate that an attribute is excluded. * * @return bool */ public function validateExclude() { return false; } /** * Indicate that an attribute should be excluded when another attribute has a given value. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateExcludeIf($attribute, $value, $parameters) { $this->requireParameterCount(2, $parameters, 'exclude_if'); if (! Arr::has($this->data, $parameters[0])) { return true; } [$values, $other] = $this->parseDependentRuleParameters($parameters); return ! in_array($other, $values, is_bool($other) || is_null($other)); } /** * Indicate that an attribute should be excluded when another attribute does not have a given value. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateExcludeUnless($attribute, $value, $parameters) { $this->requireParameterCount(2, $parameters, 'exclude_unless'); [$values, $other] = $this->parseDependentRuleParameters($parameters); return in_array($other, $values, is_bool($other) || is_null($other)); } /** * Validate that an attribute exists when another attribute does not have a given value. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateRequiredUnless($attribute, $value, $parameters) { $this->requireParameterCount(2, $parameters, 'required_unless'); [$values, $other] = $this->parseDependentRuleParameters($parameters); if (! in_array($other, $values, is_bool($other) || is_null($other))) { return $this->validateRequired($attribute, $value); } return true; } /** * Indicate that an attribute should be excluded when another attribute presents. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateExcludeWith($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'exclude_with'); if (! Arr::has($this->data, $parameters[0])) { return true; } return false; } /** * Indicate that an attribute should be excluded when another attribute is missing. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateExcludeWithout($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'exclude_without'); if ($this->anyFailingRequired($parameters)) { return false; } return true; } /** * Prepare the values and the other value for validation. * * @param array<int, int|string> $parameters * @return array */ public function parseDependentRuleParameters($parameters) { $other = Arr::get($this->data, $parameters[0]); $values = array_slice($parameters, 1); if ($this->shouldConvertToBoolean($parameters[0]) || is_bool($other)) { $values = $this->convertValuesToBoolean($values); } if (is_null($other)) { $values = $this->convertValuesToNull($values); } return [$values, $other]; } /** * Check if parameter should be converted to boolean. * * @param string $parameter * @return bool */ protected function shouldConvertToBoolean($parameter) { return in_array('boolean', Arr::get($this->rules, $parameter, [])); } /** * Convert the given values to boolean if they are string "true" / "false". * * @param array $values * @return array */ protected function convertValuesToBoolean($values) { return array_map(function ($value) { if ($value === 'true') { return true; } elseif ($value === 'false') { return false; } return $value; }, $values); } /** * Convert the given values to null if they are string "null". * * @param array $values * @return array */ protected function convertValuesToNull($values) { return array_map(function ($value) { return Str::lower($value) === 'null' ? null : $value; }, $values); } /** * Validate that an attribute exists when any other attribute exists. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateRequiredWith($attribute, $value, $parameters) { if (! $this->allFailingRequired($parameters)) { return $this->validateRequired($attribute, $value); } return true; } /** * Validate that an attribute exists when all other attributes exist. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateRequiredWithAll($attribute, $value, $parameters) { if (! $this->anyFailingRequired($parameters)) { return $this->validateRequired($attribute, $value); } return true; } /** * Validate that an attribute exists when another attribute does not. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateRequiredWithout($attribute, $value, $parameters) { if ($this->anyFailingRequired($parameters)) { return $this->validateRequired($attribute, $value); } return true; } /** * Validate that an attribute exists when all other attributes do not. * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateRequiredWithoutAll($attribute, $value, $parameters) { if ($this->allFailingRequired($parameters)) { return $this->validateRequired($attribute, $value); } return true; } /** * Determine if any of the given attributes fail the required test. * * @param array $attributes * @return bool */ protected function anyFailingRequired(array $attributes) { foreach ($attributes as $key) { if (! $this->validateRequired($key, $this->getValue($key))) { return true; } } return false; } /** * Determine if all of the given attributes fail the required test. * * @param array $attributes * @return bool */ protected function allFailingRequired(array $attributes) { foreach ($attributes as $key) { if ($this->validateRequired($key, $this->getValue($key))) { return false; } } return true; } /** * Validate that two attributes match. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateSame($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'same'); $other = Arr::get($this->data, $parameters[0]); return $value === $other; } /** * Validate the size of an attribute. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateSize($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'size'); return BigNumber::of($this->getSize($attribute, $value))->isEqualTo($this->trim($parameters[0])); } /** * "Validate" optional attributes. * * Always returns true, just lets us put sometimes in rules. * * @return bool */ public function validateSometimes() { return true; } /** * Validate the attribute starts with a given substring. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateStartsWith($attribute, $value, $parameters) { return Str::startsWith($value, $parameters); } /** * Validate the attribute does not start with a given substring. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateDoesntStartWith($attribute, $value, $parameters) { return ! Str::startsWith($value, $parameters); } /** * Validate the attribute ends with a given substring. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateEndsWith($attribute, $value, $parameters) { return Str::endsWith($value, $parameters); } /** * Validate the attribute does not end with a given substring. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateDoesntEndWith($attribute, $value, $parameters) { return ! Str::endsWith($value, $parameters); } /** * Validate that an attribute is a string. * * @param string $attribute * @param mixed $value * @return bool */ public function validateString($attribute, $value) { return is_string($value); } /** * Validate that an attribute is a valid timezone. * * @param string $attribute * @param mixed $value * @param array<string, null|string> $parameters * @return bool */ public function validateTimezone($attribute, $value, $parameters = []) { return in_array($value, timezone_identifiers_list( constant(DateTimeZone::class.'::'.Str::upper($parameters[0] ?? 'ALL')), isset($parameters[1]) ? Str::upper($parameters[1]) : null, ), true); } /** * Validate that an attribute is a valid URL. * * @param string $attribute * @param mixed $value * @param array<int, string> $parameters * @return bool */ public function validateUrl($attribute, $value, $parameters = []) { return Str::isUrl($value, $parameters); } /** * Validate that an attribute is a valid ULID. * * @param string $attribute * @param mixed $value * @return bool */ public function validateUlid($attribute, $value) { return Str::isUlid($value); } /** * Validate that an attribute is a valid UUID. * * @param string $attribute * @param mixed $value * @return bool */ public function validateUuid($attribute, $value) { return Str::isUuid($value); } /** * Get the size of an attribute. * * @param string $attribute * @param mixed $value * @return int|float|string */ protected function getSize($attribute, $value) { $hasNumeric = $this->hasRule($attribute, $this->numericRules); // This method will determine if the attribute is a number, string, or file and // return the proper size accordingly. If it is a number, then number itself // is the size. If it is a file, we take kilobytes, and for a string the // entire length of the string will be considered the attribute size. if (is_numeric($value) && $hasNumeric) { return $this->ensureExponentWithinAllowedRange($attribute, $this->trim($value)); } elseif (is_array($value)) { return count($value); } elseif ($value instanceof File) { return $value->getSize() / 1024; } return mb_strlen($value ?? ''); } /** * Check that the given value is a valid file instance. * * @param mixed $value * @return bool */ public function isValidFileInstance($value) { if ($value instanceof UploadedFile && ! $value->isValid()) { return false; } return $value instanceof File; } /** * Determine if a comparison passes between the given values. * * @param mixed $first * @param mixed $second * @param string $operator * @return bool * * @throws \InvalidArgumentException */ protected function compare($first, $second, $operator) { return match ($operator) { '<' => $first < $second, '>' => $first > $second, '<=' => $first <= $second, '>=' => $first >= $second, '=' => $first == $second, default => throw new InvalidArgumentException, }; } /** * Parse named parameters to $key => $value items. * * @param array<int, int|string> $parameters * @return array */ public function parseNamedParameters($parameters) { return array_reduce($parameters, function ($result, $item) { [$key, $value] = array_pad(explode('=', $item, 2), 2, null); $result[$key] = $value; return $result; }); } /** * Require a certain number of parameters to be present. * * @param int $count * @param array<int, int|string> $parameters * @param string $rule * @return void * * @throws \InvalidArgumentException */ public function requireParameterCount($count, $parameters, $rule) { if (count($parameters) < $count) { throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); } } /** * Check if the parameters are of the same type. * * @param mixed $first * @param mixed $second * @return bool */ protected function isSameType($first, $second) { return gettype($first) == gettype($second); } /** * Adds the existing rule to the numericRules array if the attribute's value is numeric. * * @param string $attribute * @param string $rule * @return void */ protected function shouldBeNumeric($attribute, $rule) { if (is_numeric($this->getValue($attribute))) { $this->numericRules[] = $rule; } } /** * Trim the value if it is a string. * * @param mixed $value * @return mixed */ protected function trim($value) { return is_string($value) ? trim($value) : $value; } /** * Ensure the exponent is within the allowed range. * * @param string $attribute * @param mixed $value * @return mixed */ protected function ensureExponentWithinAllowedRange($attribute, $value) { $stringValue = (string) $value; if (! is_numeric($value) || ! Str::contains($stringValue, 'e', ignoreCase: true)) { return $value; } $scale = (int) (Str::contains($stringValue, 'e') ? Str::after($stringValue, 'e') : Str::after($stringValue, 'E')); $withinRange = ( $this->ensureExponentWithinAllowedRangeUsing ?? fn ($scale) => $scale <= 1000 && $scale >= -1000 )($scale, $attribute, $value); if (! $withinRange) { throw new MathException('Scientific notation exponent outside of allowed range.'); } return $value; } } src/Illuminate/Validation/Concerns/FormatsMessages.php 0000644 00000040772 15107327041 0017130 0 ustar 00 <?php namespace Illuminate\Validation\Concerns; use Closure; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Symfony\Component\HttpFoundation\File\UploadedFile; trait FormatsMessages { use ReplacesAttributes; /** * Get the validation message for an attribute and rule. * * @param string $attribute * @param string $rule * @return string */ protected function getMessage($attribute, $rule) { $attributeWithPlaceholders = $attribute; $attribute = $this->replacePlaceholderInString($attribute); $inlineMessage = $this->getInlineMessage($attribute, $rule); // First we will retrieve the custom message for the validation rule if one // exists. If a custom validation message is being used we'll return the // custom message, otherwise we'll keep searching for a valid message. if (! is_null($inlineMessage)) { return $inlineMessage; } $lowerRule = Str::snake($rule); $customKey = "validation.custom.{$attribute}.{$lowerRule}"; $customMessage = $this->getCustomMessageFromTranslator( in_array($rule, $this->sizeRules) ? [$customKey.".{$this->getAttributeType($attribute)}", $customKey] : $customKey ); // First we check for a custom defined validation message for the attribute // and rule. This allows the developer to specify specific messages for // only some attributes and rules that need to get specially formed. if ($customMessage !== $customKey) { return $customMessage; } // If the rule being validated is a "size" rule, we will need to gather the // specific error message for the type of attribute being validated such // as a number, file or string which all have different message types. elseif (in_array($rule, $this->sizeRules)) { return $this->getSizeMessage($attributeWithPlaceholders, $rule); } // Finally, if no developer specified messages have been set, and no other // special messages apply for this rule, we will just pull the default // messages out of the translator service for this validation rule. $key = "validation.{$lowerRule}"; if ($key !== ($value = $this->translator->get($key))) { return $value; } return $this->getFromLocalArray( $attribute, $lowerRule, $this->fallbackMessages ) ?: $key; } /** * Get the proper inline error message for standard and size rules. * * @param string $attribute * @param string $rule * @return string|null */ protected function getInlineMessage($attribute, $rule) { $inlineEntry = $this->getFromLocalArray($attribute, Str::snake($rule)); return is_array($inlineEntry) && in_array($rule, $this->sizeRules) ? $inlineEntry[$this->getAttributeType($attribute)] : $inlineEntry; } /** * Get the inline message for a rule if it exists. * * @param string $attribute * @param string $lowerRule * @param array|null $source * @return string|null */ protected function getFromLocalArray($attribute, $lowerRule, $source = null) { $source = $source ?: $this->customMessages; $keys = ["{$attribute}.{$lowerRule}", $lowerRule, $attribute]; // First we will check for a custom message for an attribute specific rule // message for the fields, then we will check for a general custom line // that is not attribute specific. If we find either we'll return it. foreach ($keys as $key) { foreach (array_keys($source) as $sourceKey) { if (str_contains($sourceKey, '*')) { $pattern = str_replace('\*', '([^.]*)', preg_quote($sourceKey, '#')); if (preg_match('#^'.$pattern.'\z#u', $key) === 1) { $message = $source[$sourceKey]; if (is_array($message) && isset($message[$lowerRule])) { return $message[$lowerRule]; } return $message; } continue; } if (Str::is($sourceKey, $key)) { $message = $source[$sourceKey]; if ($sourceKey === $attribute && is_array($message) && isset($message[$lowerRule])) { return $message[$lowerRule]; } return $message; } } } } /** * Get the custom error message from the translator. * * @param array|string $keys * @return string */ protected function getCustomMessageFromTranslator($keys) { foreach (Arr::wrap($keys) as $key) { if (($message = $this->translator->get($key)) !== $key) { return $message; } // If an exact match was not found for the key, we will collapse all of these // messages and loop through them and try to find a wildcard match for the // given key. Otherwise, we will simply return the key's value back out. $shortKey = preg_replace( '/^validation\.custom\./', '', $key ); $message = $this->getWildcardCustomMessages(Arr::dot( (array) $this->translator->get('validation.custom') ), $shortKey, $key); if ($message !== $key) { return $message; } } return Arr::last(Arr::wrap($keys)); } /** * Check the given messages for a wildcard key. * * @param array $messages * @param string $search * @param string $default * @return string */ protected function getWildcardCustomMessages($messages, $search, $default) { foreach ($messages as $key => $message) { if ($search === $key || (Str::contains($key, ['*']) && Str::is($key, $search))) { return $message; } } return $default; } /** * Get the proper error message for an attribute and size rule. * * @param string $attribute * @param string $rule * @return string */ protected function getSizeMessage($attribute, $rule) { $lowerRule = Str::snake($rule); // There are three different types of size validations. The attribute may be // either a number, file, or string so we will check a few things to know // which type of value it is and return the correct line for that type. $type = $this->getAttributeType($attribute); $key = "validation.{$lowerRule}.{$type}"; return $this->translator->get($key); } /** * Get the data type of the given attribute. * * @param string $attribute * @return string */ protected function getAttributeType($attribute) { // We assume that the attributes present in the file array are files so that // means that if the attribute does not have a numeric rule and the files // list doesn't have it we'll just consider it a string by elimination. if ($this->hasRule($attribute, $this->numericRules)) { return 'numeric'; } elseif ($this->hasRule($attribute, ['Array'])) { return 'array'; } elseif ($this->getValue($attribute) instanceof UploadedFile) { return 'file'; } return 'string'; } /** * Replace all error message place-holders with actual values. * * @param string $message * @param string $attribute * @param string $rule * @param array $parameters * @return string */ public function makeReplacements($message, $attribute, $rule, $parameters) { $message = $this->replaceAttributePlaceholder( $message, $this->getDisplayableAttribute($attribute) ); $message = $this->replaceInputPlaceholder($message, $attribute); $message = $this->replaceIndexPlaceholder($message, $attribute); $message = $this->replacePositionPlaceholder($message, $attribute); if (isset($this->replacers[Str::snake($rule)])) { return $this->callReplacer($message, $attribute, Str::snake($rule), $parameters, $this); } elseif (method_exists($this, $replacer = "replace{$rule}")) { return $this->$replacer($message, $attribute, $rule, $parameters); } return $message; } /** * Get the displayable name of the attribute. * * @param string $attribute * @return string */ public function getDisplayableAttribute($attribute) { $primaryAttribute = $this->getPrimaryAttribute($attribute); $expectedAttributes = $attribute != $primaryAttribute ? [$attribute, $primaryAttribute] : [$attribute]; foreach ($expectedAttributes as $name) { // The developer may dynamically specify the array of custom attributes on this // validator instance. If the attribute exists in this array it is used over // the other ways of pulling the attribute name for this given attributes. if (isset($this->customAttributes[$name])) { return $this->customAttributes[$name]; } // We allow for a developer to specify language lines for any attribute in this // application, which allows flexibility for displaying a unique displayable // version of the attribute name instead of the name used in an HTTP POST. if ($line = $this->getAttributeFromTranslations($name)) { return $line; } } // When no language line has been specified for the attribute and it is also // an implicit attribute we will display the raw attribute's name and not // modify it with any of these replacements before we display the name. if (isset($this->implicitAttributes[$primaryAttribute])) { return ($formatter = $this->implicitAttributesFormatter) ? $formatter($attribute) : $attribute; } return str_replace('_', ' ', Str::snake($attribute)); } /** * Get the given attribute from the attribute translations. * * @param string $name * @return string */ protected function getAttributeFromTranslations($name) { return Arr::get($this->translator->get('validation.attributes'), $name); } /** * Replace the :attribute placeholder in the given message. * * @param string $message * @param string $value * @return string */ protected function replaceAttributePlaceholder($message, $value) { return str_replace( [':attribute', ':ATTRIBUTE', ':Attribute'], [$value, Str::upper($value), Str::ucfirst($value)], $message ); } /** * Replace the :index placeholder in the given message. * * @param string $message * @param string $attribute * @return string */ protected function replaceIndexPlaceholder($message, $attribute) { return $this->replaceIndexOrPositionPlaceholder( $message, $attribute, 'index' ); } /** * Replace the :position placeholder in the given message. * * @param string $message * @param string $attribute * @return string */ protected function replacePositionPlaceholder($message, $attribute) { return $this->replaceIndexOrPositionPlaceholder( $message, $attribute, 'position', fn ($segment) => $segment + 1 ); } /** * Replace the :index or :position placeholder in the given message. * * @param string $message * @param string $attribute * @param string $placeholder * @param \Closure|null $modifier * @return string */ protected function replaceIndexOrPositionPlaceholder($message, $attribute, $placeholder, Closure $modifier = null) { $segments = explode('.', $attribute); $modifier ??= fn ($value) => $value; $numericIndex = 1; foreach ($segments as $segment) { if (is_numeric($segment)) { if ($numericIndex === 1) { $message = str_ireplace(':'.$placeholder, $modifier((int) $segment), $message); } $message = str_ireplace( ':'.$this->numberToIndexOrPositionWord($numericIndex).'-'.$placeholder, $modifier((int) $segment), $message ); $numericIndex++; } } return $message; } /** * Get the word for a index or position segment. * * @param int $value * @return string */ protected function numberToIndexOrPositionWord(int $value) { return [ 1 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth', 5 => 'fifth', 6 => 'sixth', 7 => 'seventh', 8 => 'eighth', 9 => 'ninth', 10 => 'tenth', ][(int) $value] ?? 'other'; } /** * Replace the :input placeholder in the given message. * * @param string $message * @param string $attribute * @return string */ protected function replaceInputPlaceholder($message, $attribute) { $actualValue = $this->getValue($attribute); if (is_scalar($actualValue) || is_null($actualValue)) { $message = str_replace(':input', $this->getDisplayableValue($attribute, $actualValue), $message); } return $message; } /** * Get the displayable name of the value. * * @param string $attribute * @param mixed $value * @return string */ public function getDisplayableValue($attribute, $value) { if (isset($this->customValues[$attribute][$value])) { return $this->customValues[$attribute][$value]; } if (is_array($value)) { return 'array'; } $key = "validation.values.{$attribute}.{$value}"; if (($line = $this->translator->get($key)) !== $key) { return $line; } if (is_bool($value)) { return $value ? 'true' : 'false'; } if (is_null($value)) { return 'empty'; } return (string) $value; } /** * Transform an array of attributes to their displayable form. * * @param array $values * @return array */ protected function getAttributeList(array $values) { $attributes = []; // For each attribute in the list we will simply get its displayable form as // this is convenient when replacing lists of parameters like some of the // replacement functions do when formatting out the validation message. foreach ($values as $key => $value) { $attributes[$key] = $this->getDisplayableAttribute($value); } return $attributes; } /** * Call a custom validator message replacer. * * @param string $message * @param string $attribute * @param string $rule * @param array $parameters * @param \Illuminate\Validation\Validator $validator * @return string|null */ protected function callReplacer($message, $attribute, $rule, $parameters, $validator) { $callback = $this->replacers[$rule]; if ($callback instanceof Closure) { return $callback(...func_get_args()); } elseif (is_string($callback)) { return $this->callClassBasedReplacer($callback, $message, $attribute, $rule, $parameters, $validator); } } /** * Call a class based validator message replacer. * * @param string $callback * @param string $message * @param string $attribute * @param string $rule * @param array $parameters * @param \Illuminate\Validation\Validator $validator * @return string */ protected function callClassBasedReplacer($callback, $message, $attribute, $rule, $parameters, $validator) { [$class, $method] = Str::parseCallback($callback, 'replace'); return $this->container->make($class)->{$method}(...array_slice(func_get_args(), 1)); } } src/Illuminate/Validation/Concerns/ReplacesAttributes.php 0000644 00000057200 15107327041 0017624 0 ustar 00 <?php namespace Illuminate\Validation\Concerns; use Illuminate\Support\Arr; trait ReplacesAttributes { /** * Replace all place-holders for the accepted_if rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceAcceptedIf($message, $attribute, $rule, $parameters) { $parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0])); $parameters[0] = $this->getDisplayableAttribute($parameters[0]); return str_replace([':other', ':value'], $parameters, $message); } /** * Replace all place-holders for the declined_if rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceDeclinedIf($message, $attribute, $rule, $parameters) { $parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0])); $parameters[0] = $this->getDisplayableAttribute($parameters[0]); return str_replace([':other', ':value'], $parameters, $message); } /** * Replace all place-holders for the between rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceBetween($message, $attribute, $rule, $parameters) { return str_replace([':min', ':max'], $parameters, $message); } /** * Replace all place-holders for the date_format rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceDateFormat($message, $attribute, $rule, $parameters) { return str_replace(':format', $parameters[0], $message); } /** * Replace all place-holders for the decimal rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,int> $parameters * @return string */ protected function replaceDecimal($message, $attribute, $rule, $parameters) { return str_replace( ':decimal', isset($parameters[1]) ? $parameters[0].'-'.$parameters[1] : $parameters[0], $message ); } /** * Replace all place-holders for the different rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceDifferent($message, $attribute, $rule, $parameters) { return $this->replaceSame($message, $attribute, $rule, $parameters); } /** * Replace all place-holders for the digits rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceDigits($message, $attribute, $rule, $parameters) { return str_replace(':digits', $parameters[0], $message); } /** * Replace all place-holders for the digits (between) rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceDigitsBetween($message, $attribute, $rule, $parameters) { return $this->replaceBetween($message, $attribute, $rule, $parameters); } /** * Replace all place-holders for the min rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceMin($message, $attribute, $rule, $parameters) { return str_replace(':min', $parameters[0], $message); } /** * Replace all place-holders for the min digits rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceMinDigits($message, $attribute, $rule, $parameters) { return str_replace(':min', $parameters[0], $message); } /** * Replace all place-holders for the max rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceMax($message, $attribute, $rule, $parameters) { return str_replace(':max', $parameters[0], $message); } /** * Replace all place-holders for the max digits rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceMaxDigits($message, $attribute, $rule, $parameters) { return str_replace(':max', $parameters[0], $message); } /** * Replace all place-holders for the missing_if rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceMissingIf($message, $attribute, $rule, $parameters) { $parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0])); $parameters[0] = $this->getDisplayableAttribute($parameters[0]); return str_replace([':other', ':value'], $parameters, $message); } /** * Replace all place-holders for the missing_unless rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceMissingUnless($message, $attribute, $rule, $parameters) { return str_replace([':other', ':value'], [ $this->getDisplayableAttribute($parameters[0]), $this->getDisplayableValue($parameters[0], $parameters[1]), ], $message); } /** * Replace all place-holders for the missing_with rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceMissingWith($message, $attribute, $rule, $parameters) { return str_replace(':values', implode(' / ', $this->getAttributeList($parameters)), $message); } /** * Replace all place-holders for the missing_with_all rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceMissingWithAll($message, $attribute, $rule, $parameters) { return $this->replaceMissingWith($message, $attribute, $rule, $parameters); } /** * Replace all place-holders for the multiple_of rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceMultipleOf($message, $attribute, $rule, $parameters) { return str_replace(':value', $parameters[0] ?? '', $message); } /** * Replace all place-holders for the in rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceIn($message, $attribute, $rule, $parameters) { foreach ($parameters as &$parameter) { $parameter = $this->getDisplayableValue($attribute, $parameter); } return str_replace(':values', implode(', ', $parameters), $message); } /** * Replace all place-holders for the not_in rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceNotIn($message, $attribute, $rule, $parameters) { return $this->replaceIn($message, $attribute, $rule, $parameters); } /** * Replace all place-holders for the in_array rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceInArray($message, $attribute, $rule, $parameters) { return str_replace(':other', $this->getDisplayableAttribute($parameters[0]), $message); } /** * Replace all place-holders for the required_array_keys rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceRequiredArrayKeys($message, $attribute, $rule, $parameters) { foreach ($parameters as &$parameter) { $parameter = $this->getDisplayableValue($attribute, $parameter); } return str_replace(':values', implode(', ', $parameters), $message); } /** * Replace all place-holders for the mimetypes rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceMimetypes($message, $attribute, $rule, $parameters) { return str_replace(':values', implode(', ', $parameters), $message); } /** * Replace all place-holders for the mimes rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceMimes($message, $attribute, $rule, $parameters) { return str_replace(':values', implode(', ', $parameters), $message); } /** * Replace all place-holders for the required_with rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceRequiredWith($message, $attribute, $rule, $parameters) { return str_replace(':values', implode(' / ', $this->getAttributeList($parameters)), $message); } /** * Replace all place-holders for the required_with_all rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceRequiredWithAll($message, $attribute, $rule, $parameters) { return $this->replaceRequiredWith($message, $attribute, $rule, $parameters); } /** * Replace all place-holders for the required_without rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceRequiredWithout($message, $attribute, $rule, $parameters) { return $this->replaceRequiredWith($message, $attribute, $rule, $parameters); } /** * Replace all place-holders for the required_without_all rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceRequiredWithoutAll($message, $attribute, $rule, $parameters) { return $this->replaceRequiredWith($message, $attribute, $rule, $parameters); } /** * Replace all place-holders for the size rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceSize($message, $attribute, $rule, $parameters) { return str_replace(':size', $parameters[0], $message); } /** * Replace all place-holders for the gt rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceGt($message, $attribute, $rule, $parameters) { if (is_null($value = $this->getValue($parameters[0]))) { return str_replace(':value', $this->getDisplayableAttribute($parameters[0]), $message); } return str_replace(':value', $this->getSize($attribute, $value), $message); } /** * Replace all place-holders for the lt rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceLt($message, $attribute, $rule, $parameters) { if (is_null($value = $this->getValue($parameters[0]))) { return str_replace(':value', $this->getDisplayableAttribute($parameters[0]), $message); } return str_replace(':value', $this->getSize($attribute, $value), $message); } /** * Replace all place-holders for the gte rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceGte($message, $attribute, $rule, $parameters) { if (is_null($value = $this->getValue($parameters[0]))) { return str_replace(':value', $this->getDisplayableAttribute($parameters[0]), $message); } return str_replace(':value', $this->getSize($attribute, $value), $message); } /** * Replace all place-holders for the lte rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceLte($message, $attribute, $rule, $parameters) { if (is_null($value = $this->getValue($parameters[0]))) { return str_replace(':value', $this->getDisplayableAttribute($parameters[0]), $message); } return str_replace(':value', $this->getSize($attribute, $value), $message); } /** * Replace all place-holders for the required_if rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceRequiredIf($message, $attribute, $rule, $parameters) { $parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0])); $parameters[0] = $this->getDisplayableAttribute($parameters[0]); return str_replace([':other', ':value'], $parameters, $message); } /** * Replace all place-holders for the required_if_accepted rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceRequiredIfAccepted($message, $attribute, $rule, $parameters) { $parameters[0] = $this->getDisplayableAttribute($parameters[0]); return str_replace([':other'], $parameters, $message); } /** * Replace all place-holders for the required_unless rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceRequiredUnless($message, $attribute, $rule, $parameters) { $other = $this->getDisplayableAttribute($parameters[0]); $values = []; foreach (array_slice($parameters, 1) as $value) { $values[] = $this->getDisplayableValue($parameters[0], $value); } return str_replace([':other', ':values'], [$other, implode(', ', $values)], $message); } /** * Replace all place-holders for the prohibited_if rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceProhibitedIf($message, $attribute, $rule, $parameters) { $parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0])); $parameters[0] = $this->getDisplayableAttribute($parameters[0]); return str_replace([':other', ':value'], $parameters, $message); } /** * Replace all place-holders for the prohibited_unless rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceProhibitedUnless($message, $attribute, $rule, $parameters) { $other = $this->getDisplayableAttribute($parameters[0]); $values = []; foreach (array_slice($parameters, 1) as $value) { $values[] = $this->getDisplayableValue($parameters[0], $value); } return str_replace([':other', ':values'], [$other, implode(', ', $values)], $message); } /** * Replace all place-holders for the prohibited_with rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceProhibits($message, $attribute, $rule, $parameters) { return str_replace(':other', implode(' / ', $this->getAttributeList($parameters)), $message); } /** * Replace all place-holders for the same rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceSame($message, $attribute, $rule, $parameters) { return str_replace(':other', $this->getDisplayableAttribute($parameters[0]), $message); } /** * Replace all place-holders for the before rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceBefore($message, $attribute, $rule, $parameters) { if (! strtotime($parameters[0])) { return str_replace(':date', $this->getDisplayableAttribute($parameters[0]), $message); } return str_replace(':date', $this->getDisplayableValue($attribute, $parameters[0]), $message); } /** * Replace all place-holders for the before_or_equal rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceBeforeOrEqual($message, $attribute, $rule, $parameters) { return $this->replaceBefore($message, $attribute, $rule, $parameters); } /** * Replace all place-holders for the after rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceAfter($message, $attribute, $rule, $parameters) { return $this->replaceBefore($message, $attribute, $rule, $parameters); } /** * Replace all place-holders for the after_or_equal rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceAfterOrEqual($message, $attribute, $rule, $parameters) { return $this->replaceBefore($message, $attribute, $rule, $parameters); } /** * Replace all place-holders for the date_equals rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceDateEquals($message, $attribute, $rule, $parameters) { return $this->replaceBefore($message, $attribute, $rule, $parameters); } /** * Replace all place-holders for the dimensions rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceDimensions($message, $attribute, $rule, $parameters) { $parameters = $this->parseNamedParameters($parameters); if (is_array($parameters)) { foreach ($parameters as $key => $value) { $message = str_replace(':'.$key, $value, $message); } } return $message; } /** * Replace all place-holders for the ends_with rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceEndsWith($message, $attribute, $rule, $parameters) { foreach ($parameters as &$parameter) { $parameter = $this->getDisplayableValue($attribute, $parameter); } return str_replace(':values', implode(', ', $parameters), $message); } /** * Replace all place-holders for the doesnt_end_with rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceDoesntEndWith($message, $attribute, $rule, $parameters) { foreach ($parameters as &$parameter) { $parameter = $this->getDisplayableValue($attribute, $parameter); } return str_replace(':values', implode(', ', $parameters), $message); } /** * Replace all place-holders for the starts_with rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceStartsWith($message, $attribute, $rule, $parameters) { foreach ($parameters as &$parameter) { $parameter = $this->getDisplayableValue($attribute, $parameter); } return str_replace(':values', implode(', ', $parameters), $message); } /** * Replace all place-holders for the doesnt_start_with rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceDoesntStartWith($message, $attribute, $rule, $parameters) { foreach ($parameters as &$parameter) { $parameter = $this->getDisplayableValue($attribute, $parameter); } return str_replace(':values', implode(', ', $parameters), $message); } } src/Illuminate/Validation/ValidatesWhenResolvedTrait.php 0000644 00000004237 15107327041 0017515 0 ustar 00 <?php namespace Illuminate\Validation; use Illuminate\Foundation\Precognition; /** * Provides default implementation of ValidatesWhenResolved contract. */ trait ValidatesWhenResolvedTrait { /** * Validate the class instance. * * @return void */ public function validateResolved() { $this->prepareForValidation(); if (! $this->passesAuthorization()) { $this->failedAuthorization(); } $instance = $this->getValidatorInstance(); if ($this->isPrecognitive()) { $instance->after(Precognition::afterValidationHook($this)); } if ($instance->fails()) { $this->failedValidation($instance); } $this->passedValidation(); } /** * Prepare the data for validation. * * @return void */ protected function prepareForValidation() { // } /** * Get the validator instance for the request. * * @return \Illuminate\Validation\Validator */ protected function getValidatorInstance() { return $this->validator(); } /** * Handle a passed validation attempt. * * @return void */ protected function passedValidation() { // } /** * Handle a failed validation attempt. * * @param \Illuminate\Validation\Validator $validator * @return void * * @throws \Illuminate\Validation\ValidationException */ protected function failedValidation(Validator $validator) { $exception = $validator->getException(); throw new $exception($validator); } /** * Determine if the request passes the authorization check. * * @return bool */ protected function passesAuthorization() { if (method_exists($this, 'authorize')) { return $this->authorize(); } return true; } /** * Handle a failed authorization attempt. * * @return void * * @throws \Illuminate\Validation\UnauthorizedException */ protected function failedAuthorization() { throw new UnauthorizedException; } } src/Illuminate/Validation/ValidationRuleParser.php 0000644 00000022474 15107327041 0016351 0 ustar 00 <?php namespace Illuminate\Validation; use Closure; use Illuminate\Contracts\Validation\InvokableRule; use Illuminate\Contracts\Validation\Rule as RuleContract; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Validation\Rules\Exists; use Illuminate\Validation\Rules\Unique; class ValidationRuleParser { /** * The data being validated. * * @var array */ public $data; /** * The implicit attributes. * * @var array */ public $implicitAttributes = []; /** * Create a new validation rule parser. * * @param array $data * @return void */ public function __construct(array $data) { $this->data = $data; } /** * Parse the human-friendly rules into a full rules array for the validator. * * @param array $rules * @return \stdClass */ public function explode($rules) { $this->implicitAttributes = []; $rules = $this->explodeRules($rules); return (object) [ 'rules' => $rules, 'implicitAttributes' => $this->implicitAttributes, ]; } /** * Explode the rules into an array of explicit rules. * * @param array $rules * @return array */ protected function explodeRules($rules) { foreach ($rules as $key => $rule) { if (str_contains($key, '*')) { $rules = $this->explodeWildcardRules($rules, $key, [$rule]); unset($rules[$key]); } else { $rules[$key] = $this->explodeExplicitRule($rule, $key); } } return $rules; } /** * Explode the explicit rule into an array if necessary. * * @param mixed $rule * @param string $attribute * @return array */ protected function explodeExplicitRule($rule, $attribute) { if (is_string($rule)) { return explode('|', $rule); } if (is_object($rule)) { return Arr::wrap($this->prepareRule($rule, $attribute)); } return array_map( [$this, 'prepareRule'], $rule, array_fill((int) array_key_first($rule), count($rule), $attribute) ); } /** * Prepare the given rule for the Validator. * * @param mixed $rule * @param string $attribute * @return mixed */ protected function prepareRule($rule, $attribute) { if ($rule instanceof Closure) { $rule = new ClosureValidationRule($rule); } if ($rule instanceof InvokableRule || $rule instanceof ValidationRule) { $rule = InvokableValidationRule::make($rule); } if (! is_object($rule) || $rule instanceof RuleContract || ($rule instanceof Exists && $rule->queryCallbacks()) || ($rule instanceof Unique && $rule->queryCallbacks())) { return $rule; } if ($rule instanceof NestedRules) { return $rule->compile( $attribute, $this->data[$attribute] ?? null, Arr::dot($this->data) )->rules[$attribute]; } return (string) $rule; } /** * Define a set of rules that apply to each element in an array attribute. * * @param array $results * @param string $attribute * @param string|array $rules * @return array */ protected function explodeWildcardRules($results, $attribute, $rules) { $pattern = str_replace('\*', '[^\.]*', preg_quote($attribute)); $data = ValidationData::initializeAndGatherData($attribute, $this->data); foreach ($data as $key => $value) { if (Str::startsWith($key, $attribute) || (bool) preg_match('/^'.$pattern.'\z/', $key)) { foreach ((array) $rules as $rule) { if ($rule instanceof NestedRules) { $compiled = $rule->compile($key, $value, $data); $this->implicitAttributes = array_merge_recursive( $compiled->implicitAttributes, $this->implicitAttributes, [$attribute => [$key]] ); $results = $this->mergeRules($results, $compiled->rules); } else { $this->implicitAttributes[$attribute][] = $key; $results = $this->mergeRules($results, $key, $rule); } } } } return $results; } /** * Merge additional rules into a given attribute(s). * * @param array $results * @param string|array $attribute * @param string|array $rules * @return array */ public function mergeRules($results, $attribute, $rules = []) { if (is_array($attribute)) { foreach ((array) $attribute as $innerAttribute => $innerRules) { $results = $this->mergeRulesForAttribute($results, $innerAttribute, $innerRules); } return $results; } return $this->mergeRulesForAttribute( $results, $attribute, $rules ); } /** * Merge additional rules into a given attribute. * * @param array $results * @param string $attribute * @param string|array $rules * @return array */ protected function mergeRulesForAttribute($results, $attribute, $rules) { $merge = head($this->explodeRules([$rules])); $results[$attribute] = array_merge( isset($results[$attribute]) ? $this->explodeExplicitRule($results[$attribute], $attribute) : [], $merge ); return $results; } /** * Extract the rule name and parameters from a rule. * * @param array|string $rule * @return array */ public static function parse($rule) { if ($rule instanceof RuleContract || $rule instanceof NestedRules) { return [$rule, []]; } if (is_array($rule)) { $rule = static::parseArrayRule($rule); } else { $rule = static::parseStringRule($rule); } $rule[0] = static::normalizeRule($rule[0]); return $rule; } /** * Parse an array based rule. * * @param array $rule * @return array */ protected static function parseArrayRule(array $rule) { return [Str::studly(trim(Arr::get($rule, 0, ''))), array_slice($rule, 1)]; } /** * Parse a string based rule. * * @param string $rule * @return array */ protected static function parseStringRule($rule) { $parameters = []; // The format for specifying validation rules and parameters follows an // easy {rule}:{parameters} formatting convention. For instance the // rule "Max:3" states that the value may only be three letters. if (str_contains($rule, ':')) { [$rule, $parameter] = explode(':', $rule, 2); $parameters = static::parseParameters($rule, $parameter); } return [Str::studly(trim($rule)), $parameters]; } /** * Parse a parameter list. * * @param string $rule * @param string $parameter * @return array */ protected static function parseParameters($rule, $parameter) { return static::ruleIsRegex($rule) ? [$parameter] : str_getcsv($parameter); } /** * Determine if the rule is a regular expression. * * @param string $rule * @return bool */ protected static function ruleIsRegex($rule) { return in_array(strtolower($rule), ['regex', 'not_regex', 'notregex'], true); } /** * Normalizes a rule so that we can accept short types. * * @param string $rule * @return string */ protected static function normalizeRule($rule) { return match ($rule) { 'Int' => 'Integer', 'Bool' => 'Boolean', default => $rule, }; } /** * Expand the conditional rules in the given array of rules. * * @param array $rules * @param array $data * @return array */ public static function filterConditionalRules($rules, array $data = []) { return collect($rules)->mapWithKeys(function ($attributeRules, $attribute) use ($data) { if (! is_array($attributeRules) && ! $attributeRules instanceof ConditionalRules) { return [$attribute => $attributeRules]; } if ($attributeRules instanceof ConditionalRules) { return [$attribute => $attributeRules->passes($data) ? array_filter($attributeRules->rules($data)) : array_filter($attributeRules->defaultRules($data)), ]; } return [$attribute => collect($attributeRules)->map(function ($rule) use ($data) { if (! $rule instanceof ConditionalRules) { return [$rule]; } return $rule->passes($data) ? $rule->rules($data) : $rule->defaultRules($data); })->filter()->flatten(1)->values()->all()]; })->all(); } } src/Illuminate/Validation/ConditionalRules.php 0000644 00000003650 15107327041 0015523 0 ustar 00 <?php namespace Illuminate\Validation; use Illuminate\Support\Fluent; class ConditionalRules { /** * The boolean condition indicating if the rules should be added to the attribute. * * @var callable|bool */ protected $condition; /** * The rules to be added to the attribute. * * @var array|string|\Closure */ protected $rules; /** * The rules to be added to the attribute if the condition fails. * * @var array|string|\Closure */ protected $defaultRules; /** * Create a new conditional rules instance. * * @param callable|bool $condition * @param array|string|\Closure $rules * @param array|string|\Closure $defaultRules * @return void */ public function __construct($condition, $rules, $defaultRules = []) { $this->condition = $condition; $this->rules = $rules; $this->defaultRules = $defaultRules; } /** * Determine if the conditional rules should be added. * * @param array $data * @return bool */ public function passes(array $data = []) { return is_callable($this->condition) ? call_user_func($this->condition, new Fluent($data)) : $this->condition; } /** * Get the rules. * * @param array $data * @return array */ public function rules(array $data = []) { return is_string($this->rules) ? explode('|', $this->rules) : value($this->rules, new Fluent($data)); } /** * Get the default rules. * * @param array $data * @return array */ public function defaultRules(array $data = []) { return is_string($this->defaultRules) ? explode('|', $this->defaultRules) : value($this->defaultRules, new Fluent($data)); } } src/Illuminate/Validation/composer.json 0000644 00000002472 15107327041 0014257 0 ustar 00 { "name": "illuminate/validation", "description": "The Illuminate Validation package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "ext-filter": "*", "ext-mbstring": "*", "brick/math": "^0.9.3|^0.10.2|^0.11", "egulias/email-validator": "^3.2.5|^4.0", "illuminate/collections": "^10.0", "illuminate/container": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0", "illuminate/support": "^10.0", "illuminate/translation": "^10.0", "symfony/http-foundation": "^6.2", "symfony/mime": "^6.2" }, "autoload": { "psr-4": { "Illuminate\\Validation\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "illuminate/database": "Required to use the database presence verifier (^10.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Validation/Rule.php 0000644 00000011306 15107327041 0013151 0 ustar 00 <?php namespace Illuminate\Validation; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Traits\Macroable; use Illuminate\Validation\Rules\Can; use Illuminate\Validation\Rules\Dimensions; use Illuminate\Validation\Rules\Enum; use Illuminate\Validation\Rules\ExcludeIf; use Illuminate\Validation\Rules\Exists; use Illuminate\Validation\Rules\File; use Illuminate\Validation\Rules\ImageFile; use Illuminate\Validation\Rules\In; use Illuminate\Validation\Rules\NotIn; use Illuminate\Validation\Rules\ProhibitedIf; use Illuminate\Validation\Rules\RequiredIf; use Illuminate\Validation\Rules\Unique; class Rule { use Macroable; /** * Get a can constraint builder instance. * * @param string $ability * @param mixed ...$arguments * @return \Illuminate\Validation\Rules\Can */ public static function can($ability, ...$arguments) { return new Can($ability, $arguments); } /** * Create a new conditional rule set. * * @param callable|bool $condition * @param array|string|\Closure $rules * @param array|string|\Closure $defaultRules * @return \Illuminate\Validation\ConditionalRules */ public static function when($condition, $rules, $defaultRules = []) { return new ConditionalRules($condition, $rules, $defaultRules); } /** * Create a new nested rule set. * * @param callable $callback * @return \Illuminate\Validation\NestedRules */ public static function forEach($callback) { return new NestedRules($callback); } /** * Get a unique constraint builder instance. * * @param string $table * @param string $column * @return \Illuminate\Validation\Rules\Unique */ public static function unique($table, $column = 'NULL') { return new Unique($table, $column); } /** * Get an exists constraint builder instance. * * @param string $table * @param string $column * @return \Illuminate\Validation\Rules\Exists */ public static function exists($table, $column = 'NULL') { return new Exists($table, $column); } /** * Get an in constraint builder instance. * * @param \Illuminate\Contracts\Support\Arrayable|array|string $values * @return \Illuminate\Validation\Rules\In */ public static function in($values) { if ($values instanceof Arrayable) { $values = $values->toArray(); } return new In(is_array($values) ? $values : func_get_args()); } /** * Get a not_in constraint builder instance. * * @param \Illuminate\Contracts\Support\Arrayable|array|string $values * @return \Illuminate\Validation\Rules\NotIn */ public static function notIn($values) { if ($values instanceof Arrayable) { $values = $values->toArray(); } return new NotIn(is_array($values) ? $values : func_get_args()); } /** * Get a required_if constraint builder instance. * * @param callable|bool $callback * @return \Illuminate\Validation\Rules\RequiredIf */ public static function requiredIf($callback) { return new RequiredIf($callback); } /** * Get a exclude_if constraint builder instance. * * @param callable|bool $callback * @return \Illuminate\Validation\Rules\ExcludeIf */ public static function excludeIf($callback) { return new ExcludeIf($callback); } /** * Get a prohibited_if constraint builder instance. * * @param callable|bool $callback * @return \Illuminate\Validation\Rules\ProhibitedIf */ public static function prohibitedIf($callback) { return new ProhibitedIf($callback); } /** * Get an enum constraint builder instance. * * @param string $type * @return \Illuminate\Validation\Rules\Enum */ public static function enum($type) { return new Enum($type); } /** * Get a file constraint builder instance. * * @return \Illuminate\Validation\Rules\File */ public static function file() { return new File; } /** * Get an image file constraint builder instance. * * @return \Illuminate\Validation\Rules\ImageFile */ public static function imageFile() { return new ImageFile; } /** * Get a dimensions constraint builder instance. * * @param array $constraints * @return \Illuminate\Validation\Rules\Dimensions */ public static function dimensions(array $constraints = []) { return new Dimensions($constraints); } } src/Illuminate/Validation/NotPwnedVerifier.php 0000644 00000005030 15107327041 0015471 0 ustar 00 <?php namespace Illuminate\Validation; use Exception; use Illuminate\Contracts\Validation\UncompromisedVerifier; use Illuminate\Support\Str; class NotPwnedVerifier implements UncompromisedVerifier { /** * The HTTP factory instance. * * @var \Illuminate\Http\Client\Factory */ protected $factory; /** * The number of seconds the request can run before timing out. * * @var int */ protected $timeout; /** * Create a new uncompromised verifier. * * @param \Illuminate\Http\Client\Factory $factory * @param int|null $timeout * @return void */ public function __construct($factory, $timeout = null) { $this->factory = $factory; $this->timeout = $timeout ?? 30; } /** * Verify that the given data has not been compromised in public breaches. * * @param array $data * @return bool */ public function verify($data) { $value = $data['value']; $threshold = $data['threshold']; if (empty($value = (string) $value)) { return false; } [$hash, $hashPrefix] = $this->getHash($value); return ! $this->search($hashPrefix) ->contains(function ($line) use ($hash, $hashPrefix, $threshold) { [$hashSuffix, $count] = explode(':', $line); return $hashPrefix.$hashSuffix == $hash && $count > $threshold; }); } /** * Get the hash and its first 5 chars. * * @param string $value * @return array */ protected function getHash($value) { $hash = strtoupper(sha1((string) $value)); $hashPrefix = substr($hash, 0, 5); return [$hash, $hashPrefix]; } /** * Search by the given hash prefix and returns all occurrences of leaked passwords. * * @param string $hashPrefix * @return \Illuminate\Support\Collection */ protected function search($hashPrefix) { try { $response = $this->factory->withHeaders([ 'Add-Padding' => true, ])->timeout($this->timeout)->get( 'https://api.pwnedpasswords.com/range/'.$hashPrefix ); } catch (Exception $e) { report($e); } $body = (isset($response) && $response->successful()) ? $response->body() : ''; return Str::of($body)->trim()->explode("\n")->filter(function ($line) { return str_contains($line, ':'); }); } } src/Illuminate/Validation/InvokableValidationRule.php 0000644 00000007537 15107327041 0017032 0 ustar 00 <?php namespace Illuminate\Validation; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ImplicitRule; use Illuminate\Contracts\Validation\InvokableRule; use Illuminate\Contracts\Validation\Rule; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Contracts\Validation\ValidatorAwareRule; use Illuminate\Translation\CreatesPotentiallyTranslatedStrings; class InvokableValidationRule implements Rule, ValidatorAwareRule { use CreatesPotentiallyTranslatedStrings; /** * The invokable that validates the attribute. * * @var \Illuminate\Contracts\Validation\ValidationRule|\Illuminate\Contracts\Validation\InvokableRule */ protected $invokable; /** * Indicates if the validation invokable failed. * * @var bool */ protected $failed = false; /** * The validation error messages. * * @var array */ protected $messages = []; /** * The current validator. * * @var \Illuminate\Validation\Validator */ protected $validator; /** * The data under validation. * * @var array */ protected $data = []; /** * Create a new explicit Invokable validation rule. * * @param \Illuminate\Contracts\Validation\ValidationRule|\Illuminate\Contracts\Validation\InvokableRule $invokable * @return void */ protected function __construct(ValidationRule|InvokableRule $invokable) { $this->invokable = $invokable; } /** * Create a new implicit or explicit Invokable validation rule. * * @param \Illuminate\Contracts\Validation\ValidationRule|\Illuminate\Contracts\Validation\InvokableRule $invokable * @return \Illuminate\Contracts\Validation\Rule|\Illuminate\Validation\InvokableValidationRule */ public static function make($invokable) { if ($invokable->implicit ?? false) { return new class($invokable) extends InvokableValidationRule implements ImplicitRule { }; } return new InvokableValidationRule($invokable); } /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value) { $this->failed = false; if ($this->invokable instanceof DataAwareRule) { $this->invokable->setData($this->validator->getData()); } if ($this->invokable instanceof ValidatorAwareRule) { $this->invokable->setValidator($this->validator); } $method = $this->invokable instanceof ValidationRule ? 'validate' : '__invoke'; $this->invokable->{$method}($attribute, $value, function ($attribute, $message = null) { $this->failed = true; return $this->pendingPotentiallyTranslatedString($attribute, $message); }); return ! $this->failed; } /** * Get the underlying invokable rule. * * @return \Illuminate\Contracts\Validation\ValidationRule|\Illuminate\Contracts\Validation\InvokableRule */ public function invokable() { return $this->invokable; } /** * Get the validation error messages. * * @return array */ public function message() { return $this->messages; } /** * Set the data under validation. * * @param array $data * @return $this */ public function setData($data) { $this->data = $data; return $this; } /** * Set the current validator. * * @param \Illuminate\Validation\Validator $validator * @return $this */ public function setValidator($validator) { $this->validator = $validator; return $this; } } src/Illuminate/Validation/DatabasePresenceVerifier.php 0000644 00000007144 15107327041 0017134 0 ustar 00 <?php namespace Illuminate\Validation; use Closure; use Illuminate\Database\ConnectionResolverInterface; class DatabasePresenceVerifier implements DatabasePresenceVerifierInterface { /** * The database connection instance. * * @var \Illuminate\Database\ConnectionResolverInterface */ protected $db; /** * The database connection to use. * * @var string */ protected $connection; /** * Create a new database presence verifier. * * @param \Illuminate\Database\ConnectionResolverInterface $db * @return void */ public function __construct(ConnectionResolverInterface $db) { $this->db = $db; } /** * Count the number of objects in a collection having the given value. * * @param string $collection * @param string $column * @param string $value * @param int|null $excludeId * @param string|null $idColumn * @param array $extra * @return int */ public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = []) { $query = $this->table($collection)->where($column, '=', $value); if (! is_null($excludeId) && $excludeId !== 'NULL') { $query->where($idColumn ?: 'id', '<>', $excludeId); } return $this->addConditions($query, $extra)->count(); } /** * Count the number of objects in a collection with the given values. * * @param string $collection * @param string $column * @param array $values * @param array $extra * @return int */ public function getMultiCount($collection, $column, array $values, array $extra = []) { $query = $this->table($collection)->whereIn($column, $values); return $this->addConditions($query, $extra)->distinct()->count($column); } /** * Add the given conditions to the query. * * @param \Illuminate\Database\Query\Builder $query * @param array $conditions * @return \Illuminate\Database\Query\Builder */ protected function addConditions($query, $conditions) { foreach ($conditions as $key => $value) { if ($value instanceof Closure) { $query->where(function ($query) use ($value) { $value($query); }); } else { $this->addWhere($query, $key, $value); } } return $query; } /** * Add a "where" clause to the given query. * * @param \Illuminate\Database\Query\Builder $query * @param string $key * @param string $extraValue * @return void */ protected function addWhere($query, $key, $extraValue) { if ($extraValue === 'NULL') { $query->whereNull($key); } elseif ($extraValue === 'NOT_NULL') { $query->whereNotNull($key); } elseif (str_starts_with($extraValue, '!')) { $query->where($key, '!=', mb_substr($extraValue, 1)); } else { $query->where($key, $extraValue); } } /** * Get a query builder for the given table. * * @param string $table * @return \Illuminate\Database\Query\Builder */ protected function table($table) { return $this->db->connection($this->connection)->table($table)->useWritePdo(); } /** * Set the connection to be used. * * @param string $connection * @return void */ public function setConnection($connection) { $this->connection = $connection; } } src/Illuminate/Validation/Factory.php 0000644 00000021624 15107327041 0013655 0 ustar 00 <?php namespace Illuminate\Validation; use Closure; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Translation\Translator; use Illuminate\Contracts\Validation\Factory as FactoryContract; use Illuminate\Support\Str; class Factory implements FactoryContract { /** * The Translator implementation. * * @var \Illuminate\Contracts\Translation\Translator */ protected $translator; /** * The Presence Verifier implementation. * * @var \Illuminate\Validation\PresenceVerifierInterface */ protected $verifier; /** * The IoC container instance. * * @var \Illuminate\Contracts\Container\Container|null */ protected $container; /** * All of the custom validator extensions. * * @var array<string, \Closure|string> */ protected $extensions = []; /** * All of the custom implicit validator extensions. * * @var array<string, \Closure|string> */ protected $implicitExtensions = []; /** * All of the custom dependent validator extensions. * * @var array<string, \Closure|string> */ protected $dependentExtensions = []; /** * All of the custom validator message replacers. * * @var array<string, \Closure|string> */ protected $replacers = []; /** * All of the fallback messages for custom rules. * * @var array<string, string> */ protected $fallbackMessages = []; /** * Indicates that unvalidated array keys should be excluded, even if the parent array was validated. * * @var bool */ protected $excludeUnvalidatedArrayKeys = true; /** * The Validator resolver instance. * * @var \Closure */ protected $resolver; /** * Create a new Validator factory instance. * * @param \Illuminate\Contracts\Translation\Translator $translator * @param \Illuminate\Contracts\Container\Container|null $container * @return void */ public function __construct(Translator $translator, Container $container = null) { $this->container = $container; $this->translator = $translator; } /** * Create a new Validator instance. * * @param array $data * @param array $rules * @param array $messages * @param array $attributes * @return \Illuminate\Validation\Validator */ public function make(array $data, array $rules, array $messages = [], array $attributes = []) { $validator = $this->resolve( $data, $rules, $messages, $attributes ); // The presence verifier is responsible for checking the unique and exists data // for the validator. It is behind an interface so that multiple versions of // it may be written besides database. We'll inject it into the validator. if (! is_null($this->verifier)) { $validator->setPresenceVerifier($this->verifier); } // Next we'll set the IoC container instance of the validator, which is used to // resolve out class based validator extensions. If it is not set then these // types of extensions will not be possible on these validation instances. if (! is_null($this->container)) { $validator->setContainer($this->container); } $validator->excludeUnvalidatedArrayKeys = $this->excludeUnvalidatedArrayKeys; $this->addExtensions($validator); return $validator; } /** * Validate the given data against the provided rules. * * @param array $data * @param array $rules * @param array $messages * @param array $attributes * @return array * * @throws \Illuminate\Validation\ValidationException */ public function validate(array $data, array $rules, array $messages = [], array $attributes = []) { return $this->make($data, $rules, $messages, $attributes)->validate(); } /** * Resolve a new Validator instance. * * @param array $data * @param array $rules * @param array $messages * @param array $attributes * @return \Illuminate\Validation\Validator */ protected function resolve(array $data, array $rules, array $messages, array $attributes) { if (is_null($this->resolver)) { return new Validator($this->translator, $data, $rules, $messages, $attributes); } return call_user_func($this->resolver, $this->translator, $data, $rules, $messages, $attributes); } /** * Add the extensions to a validator instance. * * @param \Illuminate\Validation\Validator $validator * @return void */ protected function addExtensions(Validator $validator) { $validator->addExtensions($this->extensions); // Next, we will add the implicit extensions, which are similar to the required // and accepted rule in that they're run even if the attributes aren't in an // array of data which is given to a validator instance via instantiation. $validator->addImplicitExtensions($this->implicitExtensions); $validator->addDependentExtensions($this->dependentExtensions); $validator->addReplacers($this->replacers); $validator->setFallbackMessages($this->fallbackMessages); } /** * Register a custom validator extension. * * @param string $rule * @param \Closure|string $extension * @param string|null $message * @return void */ public function extend($rule, $extension, $message = null) { $this->extensions[$rule] = $extension; if ($message) { $this->fallbackMessages[Str::snake($rule)] = $message; } } /** * Register a custom implicit validator extension. * * @param string $rule * @param \Closure|string $extension * @param string|null $message * @return void */ public function extendImplicit($rule, $extension, $message = null) { $this->implicitExtensions[$rule] = $extension; if ($message) { $this->fallbackMessages[Str::snake($rule)] = $message; } } /** * Register a custom dependent validator extension. * * @param string $rule * @param \Closure|string $extension * @param string|null $message * @return void */ public function extendDependent($rule, $extension, $message = null) { $this->dependentExtensions[$rule] = $extension; if ($message) { $this->fallbackMessages[Str::snake($rule)] = $message; } } /** * Register a custom validator message replacer. * * @param string $rule * @param \Closure|string $replacer * @return void */ public function replacer($rule, $replacer) { $this->replacers[$rule] = $replacer; } /** * Indicate that unvalidated array keys should be included in validated data when the parent array is validated. * * @return void */ public function includeUnvalidatedArrayKeys() { $this->excludeUnvalidatedArrayKeys = false; } /** * Indicate that unvalidated array keys should be excluded from the validated data, even if the parent array was validated. * * @return void */ public function excludeUnvalidatedArrayKeys() { $this->excludeUnvalidatedArrayKeys = true; } /** * Set the Validator instance resolver. * * @param \Closure $resolver * @return void */ public function resolver(Closure $resolver) { $this->resolver = $resolver; } /** * Get the Translator implementation. * * @return \Illuminate\Contracts\Translation\Translator */ public function getTranslator() { return $this->translator; } /** * Get the Presence Verifier implementation. * * @return \Illuminate\Validation\PresenceVerifierInterface */ public function getPresenceVerifier() { return $this->verifier; } /** * Set the Presence Verifier implementation. * * @param \Illuminate\Validation\PresenceVerifierInterface $presenceVerifier * @return void */ public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier) { $this->verifier = $presenceVerifier; } /** * Get the container instance used by the validation factory. * * @return \Illuminate\Contracts\Container\Container|null */ public function getContainer() { return $this->container; } /** * Set the container instance used by the validation factory. * * @param \Illuminate\Contracts\Container\Container $container * @return $this */ public function setContainer(Container $container) { $this->container = $container; return $this; } } src/Illuminate/Validation/Rules/Can.php 0000644 00000002473 15107327041 0014042 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Illuminate\Contracts\Validation\Rule; use Illuminate\Support\Facades\Gate; class Can implements Rule { /** * The ability to check. * * @var string */ protected $ability; /** * The arguments to pass to the authorization check. * * @var array */ protected $arguments; /** * Constructor. * * @param string $ability * @param array $arguments */ public function __construct($ability, array $arguments = []) { $this->ability = $ability; $this->arguments = $arguments; } /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value) { $arguments = $this->arguments; $model = array_shift($arguments); return Gate::allows($this->ability, array_filter([$model, ...$arguments, $value])); } /** * Get the validation error message. * * @return array */ public function message() { $message = trans('validation.can'); return $message === 'validation.can' ? ['The :attribute field contains an unauthorized value.'] : $message; } } src/Illuminate/Validation/Rules/ImageFile.php 0000644 00000000774 15107327041 0015165 0 ustar 00 <?php namespace Illuminate\Validation\Rules; class ImageFile extends File { /** * Create a new image file rule instance. * * @return void */ public function __construct() { $this->rules('image'); } /** * The dimension constraints for the uploaded file. * * @param \Illuminate\Validation\Rules\Dimensions $dimensions */ public function dimensions($dimensions) { $this->rules($dimensions); return $this; } } src/Illuminate/Validation/Rules/DatabaseRule.php 0000644 00000012630 15107327041 0015671 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use BackedEnum; use Closure; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Eloquent\Model; trait DatabaseRule { /** * The table to run the query against. * * @var string */ protected $table; /** * The column to check on. * * @var string */ protected $column; /** * The extra where clauses for the query. * * @var array */ protected $wheres = []; /** * The array of custom query callbacks. * * @var array */ protected $using = []; /** * Create a new rule instance. * * @param string $table * @param string $column * @return void */ public function __construct($table, $column = 'NULL') { $this->column = $column; $this->table = $this->resolveTableName($table); } /** * Resolves the name of the table from the given string. * * @param string $table * @return string */ public function resolveTableName($table) { if (! str_contains($table, '\\') || ! class_exists($table)) { return $table; } if (is_subclass_of($table, Model::class)) { $model = new $table; if (str_contains($model->getTable(), '.')) { return $table; } return implode('.', array_map(function (string $part) { return trim($part, '.'); }, array_filter([$model->getConnectionName(), $model->getTable()]))); } return $table; } /** * Set a "where" constraint on the query. * * @param \Closure|string $column * @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|\Closure|array|string|int|bool|null $value * @return $this */ public function where($column, $value = null) { if ($value instanceof Arrayable || is_array($value)) { return $this->whereIn($column, $value); } if ($column instanceof Closure) { return $this->using($column); } if (is_null($value)) { return $this->whereNull($column); } if ($value instanceof BackedEnum) { $value = $value->value; } $this->wheres[] = compact('column', 'value'); return $this; } /** * Set a "where not" constraint on the query. * * @param string $column * @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|array|string $value * @return $this */ public function whereNot($column, $value) { if ($value instanceof Arrayable || is_array($value)) { return $this->whereNotIn($column, $value); } if ($value instanceof BackedEnum) { $value = $value->value; } return $this->where($column, '!'.$value); } /** * Set a "where null" constraint on the query. * * @param string $column * @return $this */ public function whereNull($column) { return $this->where($column, 'NULL'); } /** * Set a "where not null" constraint on the query. * * @param string $column * @return $this */ public function whereNotNull($column) { return $this->where($column, 'NOT_NULL'); } /** * Set a "where in" constraint on the query. * * @param string $column * @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|array $values * @return $this */ public function whereIn($column, $values) { return $this->where(function ($query) use ($column, $values) { $query->whereIn($column, $values); }); } /** * Set a "where not in" constraint on the query. * * @param string $column * @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|array $values * @return $this */ public function whereNotIn($column, $values) { return $this->where(function ($query) use ($column, $values) { $query->whereNotIn($column, $values); }); } /** * Ignore soft deleted models during the existence check. * * @param string $deletedAtColumn * @return $this */ public function withoutTrashed($deletedAtColumn = 'deleted_at') { $this->whereNull($deletedAtColumn); return $this; } /** * Only include soft deleted models during the existence check. * * @param string $deletedAtColumn * @return $this */ public function onlyTrashed($deletedAtColumn = 'deleted_at') { $this->whereNotNull($deletedAtColumn); return $this; } /** * Register a custom query callback. * * @param \Closure $callback * @return $this */ public function using(Closure $callback) { $this->using[] = $callback; return $this; } /** * Get the custom query callbacks for the rule. * * @return array */ public function queryCallbacks() { return $this->using; } /** * Format the where clauses. * * @return string */ protected function formatWheres() { return collect($this->wheres)->map(function ($where) { return $where['column'].','.'"'.str_replace('"', '""', $where['value']).'"'; })->implode(','); } } src/Illuminate/Validation/Rules/Exists.php 0000644 00000000666 15107327041 0014622 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Illuminate\Support\Traits\Conditionable; class Exists { use Conditionable, DatabaseRule; /** * Convert the rule to a validation string. * * @return string */ public function __toString() { return rtrim(sprintf('exists:%s,%s,%s', $this->table, $this->column, $this->formatWheres() ), ','); } } src/Illuminate/Validation/Rules/In.php 0000644 00000002121 15107327041 0013675 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use BackedEnum; use UnitEnum; class In { /** * The name of the rule. * * @var string */ protected $rule = 'in'; /** * The accepted values. * * @var array */ protected $values; /** * Create a new in rule instance. * * @param array $values * @return void */ public function __construct(array $values) { $this->values = $values; } /** * Convert the rule to a validation string. * * @return string * * @see \Illuminate\Validation\ValidationRuleParser::parseParameters */ public function __toString() { $values = array_map(function ($value) { $value = match (true) { $value instanceof BackedEnum => $value->value, $value instanceof UnitEnum => $value->name, default => $value, }; return '"'.str_replace('"', '""', $value).'"'; }, $this->values); return $this->rule.':'.implode(',', $values); } } src/Illuminate/Validation/Rules/Dimensions.php 0000644 00000004574 15107327041 0015455 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Illuminate\Support\Traits\Conditionable; class Dimensions { use Conditionable; /** * The constraints for the dimensions rule. * * @var array */ protected $constraints = []; /** * Create a new dimensions rule instance. * * @param array $constraints * @return void */ public function __construct(array $constraints = []) { $this->constraints = $constraints; } /** * Set the "width" constraint. * * @param int $value * @return $this */ public function width($value) { $this->constraints['width'] = $value; return $this; } /** * Set the "height" constraint. * * @param int $value * @return $this */ public function height($value) { $this->constraints['height'] = $value; return $this; } /** * Set the "min width" constraint. * * @param int $value * @return $this */ public function minWidth($value) { $this->constraints['min_width'] = $value; return $this; } /** * Set the "min height" constraint. * * @param int $value * @return $this */ public function minHeight($value) { $this->constraints['min_height'] = $value; return $this; } /** * Set the "max width" constraint. * * @param int $value * @return $this */ public function maxWidth($value) { $this->constraints['max_width'] = $value; return $this; } /** * Set the "max height" constraint. * * @param int $value * @return $this */ public function maxHeight($value) { $this->constraints['max_height'] = $value; return $this; } /** * Set the "ratio" constraint. * * @param float $value * @return $this */ public function ratio($value) { $this->constraints['ratio'] = $value; return $this; } /** * Convert the rule to a validation string. * * @return string */ public function __toString() { $result = ''; foreach ($this->constraints as $key => $value) { $result .= "$key=$value,"; } return 'dimensions:'.substr($result, 0, -1); } } src/Illuminate/Validation/Rules/RequiredIf.php 0000644 00000001701 15107327041 0015371 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use InvalidArgumentException; class RequiredIf { /** * The condition that validates the attribute. * * @var callable|bool */ public $condition; /** * Create a new required validation rule based on a condition. * * @param callable|bool $condition * @return void */ public function __construct($condition) { if (! is_string($condition)) { $this->condition = $condition; } else { throw new InvalidArgumentException('The provided condition must be a callable or boolean.'); } } /** * Convert the rule to a validation string. * * @return string */ public function __toString() { if (is_callable($this->condition)) { return call_user_func($this->condition) ? 'required' : ''; } return $this->condition ? 'required' : ''; } } src/Illuminate/Validation/Rules/File.php 0000644 00000021234 15107327041 0014214 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\Rule; use Illuminate\Contracts\Validation\ValidatorAwareRule; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Str; use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; class File implements Rule, DataAwareRule, ValidatorAwareRule { use Conditionable; use Macroable; /** * The MIME types that the given file should match. This array may also contain file extensions. * * @var array */ protected $allowedMimetypes = []; /** * The minimum size in kilobytes that the file can be. * * @var null|int */ protected $minimumFileSize = null; /** * The maximum size in kilobytes that the file can be. * * @var null|int */ protected $maximumFileSize = null; /** * An array of custom rules that will be merged into the validation rules. * * @var array */ protected $customRules = []; /** * The error message after validation, if any. * * @var array */ protected $messages = []; /** * The data under validation. * * @var array */ protected $data; /** * The validator performing the validation. * * @var \Illuminate\Validation\Validator */ protected $validator; /** * The callback that will generate the "default" version of the file rule. * * @var string|array|callable|null */ public static $defaultCallback; /** * Set the default callback to be used for determining the file default rules. * * If no arguments are passed, the default file rule configuration will be returned. * * @param static|callable|null $callback * @return static|null */ public static function defaults($callback = null) { if (is_null($callback)) { return static::default(); } if (! is_callable($callback) && ! $callback instanceof static) { throw new InvalidArgumentException('The given callback should be callable or an instance of '.static::class); } static::$defaultCallback = $callback; } /** * Get the default configuration of the file rule. * * @return static */ public static function default() { $file = is_callable(static::$defaultCallback) ? call_user_func(static::$defaultCallback) : static::$defaultCallback; return $file instanceof Rule ? $file : new self(); } /** * Limit the uploaded file to only image types. * * @return ImageFile */ public static function image() { return new ImageFile(); } /** * Limit the uploaded file to the given MIME types or file extensions. * * @param string|array<int, string> $mimetypes * @return static */ public static function types($mimetypes) { return tap(new static(), fn ($file) => $file->allowedMimetypes = (array) $mimetypes); } /** * Indicate that the uploaded file should be exactly a certain size in kilobytes. * * @param string|int $size * @return $this */ public function size($size) { $this->minimumFileSize = $this->toKilobytes($size); $this->maximumFileSize = $this->minimumFileSize; return $this; } /** * Indicate that the uploaded file should be between a minimum and maximum size in kilobytes. * * @param string|int $minSize * @param string|int $maxSize * @return $this */ public function between($minSize, $maxSize) { $this->minimumFileSize = $this->toKilobytes($minSize); $this->maximumFileSize = $this->toKilobytes($maxSize); return $this; } /** * Indicate that the uploaded file should be no less than the given number of kilobytes. * * @param string|int $size * @return $this */ public function min($size) { $this->minimumFileSize = $this->toKilobytes($size); return $this; } /** * Indicate that the uploaded file should be no more than the given number of kilobytes. * * @param string|int $size * @return $this */ public function max($size) { $this->maximumFileSize = $this->toKilobytes($size); return $this; } /** * Convert a potentially human-friendly file size to kilobytes. * * @param string|int $size * @return mixed */ protected function toKilobytes($size) { if (! is_string($size)) { return $size; } $value = floatval($size); return round(match (true) { Str::endsWith($size, 'kb') => $value * 1, Str::endsWith($size, 'mb') => $value * 1000, Str::endsWith($size, 'gb') => $value * 1000000, Str::endsWith($size, 'tb') => $value * 1000000000, default => throw new InvalidArgumentException('Invalid file size suffix.'), }); } /** * Specify additional validation rules that should be merged with the default rules during validation. * * @param string|array $rules * @return $this */ public function rules($rules) { $this->customRules = array_merge($this->customRules, Arr::wrap($rules)); return $this; } /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value) { $this->messages = []; $validator = Validator::make( $this->data, [$attribute => $this->buildValidationRules()], $this->validator->customMessages, $this->validator->customAttributes ); if ($validator->fails()) { return $this->fail($validator->messages()->all()); } return true; } /** * Build the array of underlying validation rules based on the current state. * * @return array */ protected function buildValidationRules() { $rules = ['file']; $rules = array_merge($rules, $this->buildMimetypes()); $rules[] = match (true) { is_null($this->minimumFileSize) && is_null($this->maximumFileSize) => null, is_null($this->maximumFileSize) => "min:{$this->minimumFileSize}", is_null($this->minimumFileSize) => "max:{$this->maximumFileSize}", $this->minimumFileSize !== $this->maximumFileSize => "between:{$this->minimumFileSize},{$this->maximumFileSize}", default => "size:{$this->minimumFileSize}", }; return array_merge(array_filter($rules), $this->customRules); } /** * Separate the given mimetypes from extensions and return an array of correct rules to validate against. * * @return array */ protected function buildMimetypes() { if (count($this->allowedMimetypes) === 0) { return []; } $rules = []; $mimetypes = array_filter( $this->allowedMimetypes, fn ($type) => str_contains($type, '/') ); $mimes = array_diff($this->allowedMimetypes, $mimetypes); if (count($mimetypes) > 0) { $rules[] = 'mimetypes:'.implode(',', $mimetypes); } if (count($mimes) > 0) { $rules[] = 'mimes:'.implode(',', $mimes); } return $rules; } /** * Adds the given failures, and return false. * * @param array|string $messages * @return bool */ protected function fail($messages) { $messages = collect(Arr::wrap($messages))->map(function ($message) { return $this->validator->getTranslator()->get($message); })->all(); $this->messages = array_merge($this->messages, $messages); return false; } /** * Get the validation error message. * * @return array */ public function message() { return $this->messages; } /** * Set the current validator. * * @param \Illuminate\Contracts\Validation\Validator $validator * @return $this */ public function setValidator($validator) { $this->validator = $validator; return $this; } /** * Set the current data under validation. * * @param array $data * @return $this */ public function setData($data) { $this->data = $data; return $this; } } src/Illuminate/Validation/Rules/Unique.php 0000644 00000003125 15107327041 0014602 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Traits\Conditionable; class Unique { use Conditionable, DatabaseRule; /** * The ID that should be ignored. * * @var mixed */ protected $ignore; /** * The name of the ID column. * * @var string */ protected $idColumn = 'id'; /** * Ignore the given ID during the unique check. * * @param mixed $id * @param string|null $idColumn * @return $this */ public function ignore($id, $idColumn = null) { if ($id instanceof Model) { return $this->ignoreModel($id, $idColumn); } $this->ignore = $id; $this->idColumn = $idColumn ?? 'id'; return $this; } /** * Ignore the given model during the unique check. * * @param \Illuminate\Database\Eloquent\Model $model * @param string|null $idColumn * @return $this */ public function ignoreModel($model, $idColumn = null) { $this->idColumn = $idColumn ?? $model->getKeyName(); $this->ignore = $model->{$this->idColumn}; return $this; } /** * Convert the rule to a validation string. * * @return string */ public function __toString() { return rtrim(sprintf('unique:%s,%s,%s,%s,%s', $this->table, $this->column, $this->ignore ? '"'.addslashes($this->ignore).'"' : 'NULL', $this->idColumn, $this->formatWheres() ), ','); } } src/Illuminate/Validation/Rules/Enum.php 0000644 00000002412 15107327041 0014236 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Illuminate\Contracts\Validation\Rule; use TypeError; class Enum implements Rule { /** * The type of the enum. * * @var string */ protected $type; /** * Create a new rule instance. * * @param string $type * @return void */ public function __construct($type) { $this->type = $type; } /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value) { if ($value instanceof $this->type) { return true; } if (is_null($value) || ! enum_exists($this->type) || ! method_exists($this->type, 'tryFrom')) { return false; } try { return ! is_null($this->type::tryFrom($value)); } catch (TypeError) { return false; } } /** * Get the validation error message. * * @return array */ public function message() { $message = trans('validation.enum'); return $message === 'validation.enum' ? ['The selected :attribute is invalid.'] : $message; } } src/Illuminate/Validation/Rules/Password.php 0000644 00000023063 15107327041 0015141 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Illuminate\Container\Container; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\Rule; use Illuminate\Contracts\Validation\UncompromisedVerifier; use Illuminate\Contracts\Validation\ValidatorAwareRule; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Traits\Conditionable; use InvalidArgumentException; class Password implements Rule, DataAwareRule, ValidatorAwareRule { use Conditionable; /** * The validator performing the validation. * * @var \Illuminate\Contracts\Validation\Validator */ protected $validator; /** * The data under validation. * * @var array */ protected $data; /** * The minimum size of the password. * * @var int */ protected $min = 8; /** * If the password requires at least one uppercase and one lowercase letter. * * @var bool */ protected $mixedCase = false; /** * If the password requires at least one letter. * * @var bool */ protected $letters = false; /** * If the password requires at least one number. * * @var bool */ protected $numbers = false; /** * If the password requires at least one symbol. * * @var bool */ protected $symbols = false; /** * If the password should not have been compromised in data leaks. * * @var bool */ protected $uncompromised = false; /** * The number of times a password can appear in data leaks before being considered compromised. * * @var int */ protected $compromisedThreshold = 0; /** * Additional validation rules that should be merged into the default rules during validation. * * @var array */ protected $customRules = []; /** * The failure messages, if any. * * @var array */ protected $messages = []; /** * The callback that will generate the "default" version of the password rule. * * @var string|array|callable|null */ public static $defaultCallback; /** * Create a new rule instance. * * @param int $min * @return void */ public function __construct($min) { $this->min = max((int) $min, 1); } /** * Set the default callback to be used for determining a password's default rules. * * If no arguments are passed, the default password rule configuration will be returned. * * @param static|callable|null $callback * @return static|null */ public static function defaults($callback = null) { if (is_null($callback)) { return static::default(); } if (! is_callable($callback) && ! $callback instanceof static) { throw new InvalidArgumentException('The given callback should be callable or an instance of '.static::class); } static::$defaultCallback = $callback; } /** * Get the default configuration of the password rule. * * @return static */ public static function default() { $password = is_callable(static::$defaultCallback) ? call_user_func(static::$defaultCallback) : static::$defaultCallback; return $password instanceof Rule ? $password : static::min(8); } /** * Get the default configuration of the password rule and mark the field as required. * * @return array */ public static function required() { return ['required', static::default()]; } /** * Get the default configuration of the password rule and mark the field as sometimes being required. * * @return array */ public static function sometimes() { return ['sometimes', static::default()]; } /** * Set the performing validator. * * @param \Illuminate\Contracts\Validation\Validator $validator * @return $this */ public function setValidator($validator) { $this->validator = $validator; return $this; } /** * Set the data under validation. * * @param array $data * @return $this */ public function setData($data) { $this->data = $data; return $this; } /** * Sets the minimum size of the password. * * @param int $size * @return $this */ public static function min($size) { return new static($size); } /** * Ensures the password has not been compromised in data leaks. * * @param int $threshold * @return $this */ public function uncompromised($threshold = 0) { $this->uncompromised = true; $this->compromisedThreshold = $threshold; return $this; } /** * Makes the password require at least one uppercase and one lowercase letter. * * @return $this */ public function mixedCase() { $this->mixedCase = true; return $this; } /** * Makes the password require at least one letter. * * @return $this */ public function letters() { $this->letters = true; return $this; } /** * Makes the password require at least one number. * * @return $this */ public function numbers() { $this->numbers = true; return $this; } /** * Makes the password require at least one symbol. * * @return $this */ public function symbols() { $this->symbols = true; return $this; } /** * Specify additional validation rules that should be merged with the default rules during validation. * * @param string|array $rules * @return $this */ public function rules($rules) { $this->customRules = Arr::wrap($rules); return $this; } /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value) { $this->messages = []; $validator = Validator::make( $this->data, [$attribute => array_merge(['string', 'min:'.$this->min], $this->customRules)], $this->validator->customMessages, $this->validator->customAttributes )->after(function ($validator) use ($attribute, $value) { if (! is_string($value)) { return; } if ($this->mixedCase && ! preg_match('/(\p{Ll}+.*\p{Lu})|(\p{Lu}+.*\p{Ll})/u', $value)) { $validator->errors()->add( $attribute, $this->getErrorMessage('validation.password.mixed') ); } if ($this->letters && ! preg_match('/\pL/u', $value)) { $validator->errors()->add( $attribute, $this->getErrorMessage('validation.password.letters') ); } if ($this->symbols && ! preg_match('/\p{Z}|\p{S}|\p{P}/u', $value)) { $validator->errors()->add( $attribute, $this->getErrorMessage('validation.password.symbols') ); } if ($this->numbers && ! preg_match('/\pN/u', $value)) { $validator->errors()->add( $attribute, $this->getErrorMessage('validation.password.numbers') ); } }); if ($validator->fails()) { return $this->fail($validator->messages()->all()); } if ($this->uncompromised && ! Container::getInstance()->make(UncompromisedVerifier::class)->verify([ 'value' => $value, 'threshold' => $this->compromisedThreshold, ])) { return $this->fail($this->getErrorMessage('validation.password.uncompromised')); } return true; } /** * Get the validation error message. * * @return array */ public function message() { return $this->messages; } /** * Get the translated password error message. * * @param string $key * @return string */ protected function getErrorMessage($key) { if (($message = $this->validator->getTranslator()->get($key)) !== $key) { return $message; } $messages = [ 'validation.password.mixed' => 'The :attribute must contain at least one uppercase and one lowercase letter.', 'validation.password.letters' => 'The :attribute must contain at least one letter.', 'validation.password.symbols' => 'The :attribute must contain at least one symbol.', 'validation.password.numbers' => 'The :attribute must contain at least one number.', 'validation.password.uncompromised' => 'The given :attribute has appeared in a data leak. Please choose a different :attribute.', ]; return $messages[$key]; } /** * Adds the given failures, and return false. * * @param array|string $messages * @return bool */ protected function fail($messages) { $messages = collect(Arr::wrap($messages))->map(function ($message) { return $this->validator->getTranslator()->get($message); })->all(); $this->messages = array_merge($this->messages, $messages); return false; } } src/Illuminate/Validation/Rules/ExcludeIf.php 0000644 00000002027 15107327041 0015204 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Closure; use InvalidArgumentException; class ExcludeIf { /** * The condition that validates the attribute. * * @var \Closure|bool */ public $condition; /** * Create a new exclude validation rule based on a condition. * * @param \Closure|bool $condition * @return void * * @throws \InvalidArgumentException */ public function __construct($condition) { if ($condition instanceof Closure || is_bool($condition)) { $this->condition = $condition; } else { throw new InvalidArgumentException('The provided condition must be a callable or boolean.'); } } /** * Convert the rule to a validation string. * * @return string */ public function __toString() { if (is_callable($this->condition)) { return call_user_func($this->condition) ? 'exclude' : ''; } return $this->condition ? 'exclude' : ''; } } src/Illuminate/Validation/Rules/NotIn.php 0000644 00000002016 15107327041 0014361 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use BackedEnum; use UnitEnum; class NotIn { /** * The name of the rule. * * @var string */ protected $rule = 'not_in'; /** * The accepted values. * * @var array */ protected $values; /** * Create a new "not in" rule instance. * * @param array $values * @return void */ public function __construct(array $values) { $this->values = $values; } /** * Convert the rule to a validation string. * * @return string */ public function __toString() { $values = array_map(function ($value) { $value = match (true) { $value instanceof BackedEnum => $value->value, $value instanceof UnitEnum => $value->name, default => $value, }; return '"'.str_replace('"', '""', $value).'"'; }, $this->values); return $this->rule.':'.implode(',', $values); } } src/Illuminate/Validation/Rules/ProhibitedIf.php 0000644 00000002043 15107327041 0015702 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Closure; use InvalidArgumentException; class ProhibitedIf { /** * The condition that validates the attribute. * * @var \Closure|bool */ public $condition; /** * Create a new prohibited validation rule based on a condition. * * @param \Closure|bool $condition * @return void * * @throws \InvalidArgumentException */ public function __construct($condition) { if ($condition instanceof Closure || is_bool($condition)) { $this->condition = $condition; } else { throw new InvalidArgumentException('The provided condition must be a callable or boolean.'); } } /** * Convert the rule to a validation string. * * @return string */ public function __toString() { if (is_callable($this->condition)) { return call_user_func($this->condition) ? 'prohibited' : ''; } return $this->condition ? 'prohibited' : ''; } } src/Illuminate/Validation/LICENSE.md 0000644 00000002063 15107327041 0013135 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Validation/Validator.php 0000644 00000121024 15107327041 0014166 0 ustar 00 <?php namespace Illuminate\Validation; use BadMethodCallException; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Translation\Translator; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ImplicitRule; use Illuminate\Contracts\Validation\Rule as RuleContract; use Illuminate\Contracts\Validation\Validator as ValidatorContract; use Illuminate\Contracts\Validation\ValidatorAwareRule; use Illuminate\Support\Arr; use Illuminate\Support\Fluent; use Illuminate\Support\MessageBag; use Illuminate\Support\Str; use Illuminate\Support\ValidatedInput; use InvalidArgumentException; use RuntimeException; use stdClass; use Symfony\Component\HttpFoundation\File\UploadedFile; class Validator implements ValidatorContract { use Concerns\FormatsMessages, Concerns\ValidatesAttributes; /** * The Translator implementation. * * @var \Illuminate\Contracts\Translation\Translator */ protected $translator; /** * The container instance. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * The Presence Verifier implementation. * * @var \Illuminate\Validation\PresenceVerifierInterface */ protected $presenceVerifier; /** * The failed validation rules. * * @var array */ protected $failedRules = []; /** * Attributes that should be excluded from the validated data. * * @var array */ protected $excludeAttributes = []; /** * The message bag instance. * * @var \Illuminate\Support\MessageBag */ protected $messages; /** * The data under validation. * * @var array */ protected $data; /** * The initial rules provided. * * @var array */ protected $initialRules; /** * The rules to be applied to the data. * * @var array */ protected $rules; /** * The current rule that is validating. * * @var string */ protected $currentRule; /** * The array of wildcard attributes with their asterisks expanded. * * @var array */ protected $implicitAttributes = []; /** * The callback that should be used to format the attribute. * * @var callable|null */ protected $implicitAttributesFormatter; /** * The cached data for the "distinct" rule. * * @var array */ protected $distinctValues = []; /** * All of the registered "after" callbacks. * * @var array */ protected $after = []; /** * The array of custom error messages. * * @var array */ public $customMessages = []; /** * The array of fallback error messages. * * @var array */ public $fallbackMessages = []; /** * The array of custom attribute names. * * @var array */ public $customAttributes = []; /** * The array of custom displayable values. * * @var array */ public $customValues = []; /** * Indicates if the validator should stop on the first rule failure. * * @var bool */ protected $stopOnFirstFailure = false; /** * Indicates that unvalidated array keys should be excluded, even if the parent array was validated. * * @var bool */ public $excludeUnvalidatedArrayKeys = false; /** * All of the custom validator extensions. * * @var array */ public $extensions = []; /** * All of the custom replacer extensions. * * @var array */ public $replacers = []; /** * The validation rules that may be applied to files. * * @var string[] */ protected $fileRules = [ 'Between', 'Dimensions', 'File', 'Image', 'Max', 'Mimes', 'Mimetypes', 'Min', 'Size', ]; /** * The validation rules that imply the field is required. * * @var string[] */ protected $implicitRules = [ 'Accepted', 'AcceptedIf', 'Declined', 'DeclinedIf', 'Filled', 'Missing', 'MissingIf', 'MissingUnless', 'MissingWith', 'MissingWithAll', 'Present', 'Required', 'RequiredIf', 'RequiredIfAccepted', 'RequiredUnless', 'RequiredWith', 'RequiredWithAll', 'RequiredWithout', 'RequiredWithoutAll', ]; /** * The validation rules which depend on other fields as parameters. * * @var string[] */ protected $dependentRules = [ 'After', 'AfterOrEqual', 'Before', 'BeforeOrEqual', 'Confirmed', 'Different', 'ExcludeIf', 'ExcludeUnless', 'ExcludeWith', 'ExcludeWithout', 'Gt', 'Gte', 'Lt', 'Lte', 'AcceptedIf', 'DeclinedIf', 'RequiredIf', 'RequiredIfAccepted', 'RequiredUnless', 'RequiredWith', 'RequiredWithAll', 'RequiredWithout', 'RequiredWithoutAll', 'Prohibited', 'ProhibitedIf', 'ProhibitedUnless', 'Prohibits', 'Same', 'Unique', ]; /** * The validation rules that can exclude an attribute. * * @var string[] */ protected $excludeRules = ['Exclude', 'ExcludeIf', 'ExcludeUnless', 'ExcludeWith', 'ExcludeWithout']; /** * The size related validation rules. * * @var string[] */ protected $sizeRules = ['Size', 'Between', 'Min', 'Max', 'Gt', 'Lt', 'Gte', 'Lte']; /** * The numeric related validation rules. * * @var string[] */ protected $numericRules = ['Numeric', 'Integer', 'Decimal']; /** * The current placeholder for dots in rule keys. * * @var string */ protected $dotPlaceholder; /** * The exception to throw upon failure. * * @var string */ protected $exception = ValidationException::class; /** * The custom callback to determine if an exponent is within allowed range. * * @var callable|null */ protected $ensureExponentWithinAllowedRangeUsing; /** * Create a new Validator instance. * * @param \Illuminate\Contracts\Translation\Translator $translator * @param array $data * @param array $rules * @param array $messages * @param array $attributes * @return void */ public function __construct(Translator $translator, array $data, array $rules, array $messages = [], array $attributes = []) { $this->dotPlaceholder = Str::random(); $this->initialRules = $rules; $this->translator = $translator; $this->customMessages = $messages; $this->data = $this->parseData($data); $this->customAttributes = $attributes; $this->setRules($rules); } /** * Parse the data array, converting dots and asterisks. * * @param array $data * @return array */ public function parseData(array $data) { $newData = []; foreach ($data as $key => $value) { if (is_array($value)) { $value = $this->parseData($value); } $key = str_replace( ['.', '*'], [$this->dotPlaceholder, '__asterisk__'], $key ); $newData[$key] = $value; } return $newData; } /** * Replace the placeholders used in data keys. * * @param array $data * @return array */ protected function replacePlaceholders($data) { $originalData = []; foreach ($data as $key => $value) { $originalData[$this->replacePlaceholderInString($key)] = is_array($value) ? $this->replacePlaceholders($value) : $value; } return $originalData; } /** * Replace the placeholders in the given string. * * @param string $value * @return string */ protected function replacePlaceholderInString(string $value) { return str_replace( [$this->dotPlaceholder, '__asterisk__'], ['.', '*'], $value ); } /** * Add an after validation callback. * * @param callable|array|string $callback * @return $this */ public function after($callback) { if (is_array($callback) && ! is_callable($callback)) { foreach ($callback as $rule) { $this->after(method_exists($rule, 'after') ? $rule->after(...) : $rule); } return $this; } $this->after[] = fn () => $callback($this); return $this; } /** * Determine if the data passes the validation rules. * * @return bool */ public function passes() { $this->messages = new MessageBag; [$this->distinctValues, $this->failedRules] = [[], []]; // We'll spin through each rule, validating the attributes attached to that // rule. Any error messages will be added to the containers with each of // the other error messages, returning true if we don't have messages. foreach ($this->rules as $attribute => $rules) { if ($this->shouldBeExcluded($attribute)) { $this->removeAttribute($attribute); continue; } if ($this->stopOnFirstFailure && $this->messages->isNotEmpty()) { break; } foreach ($rules as $rule) { $this->validateAttribute($attribute, $rule); if ($this->shouldBeExcluded($attribute)) { break; } if ($this->shouldStopValidating($attribute)) { break; } } } foreach ($this->rules as $attribute => $rules) { if ($this->shouldBeExcluded($attribute)) { $this->removeAttribute($attribute); } } // Here we will spin through all of the "after" hooks on this validator and // fire them off. This gives the callbacks a chance to perform all kinds // of other validation that needs to get wrapped up in this operation. foreach ($this->after as $after) { $after(); } return $this->messages->isEmpty(); } /** * Determine if the data fails the validation rules. * * @return bool */ public function fails() { return ! $this->passes(); } /** * Determine if the attribute should be excluded. * * @param string $attribute * @return bool */ protected function shouldBeExcluded($attribute) { foreach ($this->excludeAttributes as $excludeAttribute) { if ($attribute === $excludeAttribute || Str::startsWith($attribute, $excludeAttribute.'.')) { return true; } } return false; } /** * Remove the given attribute. * * @param string $attribute * @return void */ protected function removeAttribute($attribute) { Arr::forget($this->data, $attribute); Arr::forget($this->rules, $attribute); } /** * Run the validator's rules against its data. * * @return array * * @throws \Illuminate\Validation\ValidationException */ public function validate() { throw_if($this->fails(), $this->exception, $this); return $this->validated(); } /** * Run the validator's rules against its data. * * @param string $errorBag * @return array * * @throws \Illuminate\Validation\ValidationException */ public function validateWithBag(string $errorBag) { try { return $this->validate(); } catch (ValidationException $e) { $e->errorBag = $errorBag; throw $e; } } /** * Get a validated input container for the validated input. * * @param array|null $keys * @return \Illuminate\Support\ValidatedInput|array */ public function safe(array $keys = null) { return is_array($keys) ? (new ValidatedInput($this->validated()))->only($keys) : new ValidatedInput($this->validated()); } /** * Get the attributes and values that were validated. * * @return array * * @throws \Illuminate\Validation\ValidationException */ public function validated() { throw_if($this->invalid(), $this->exception, $this); $results = []; $missingValue = new stdClass; foreach ($this->getRules() as $key => $rules) { if ($this->excludeUnvalidatedArrayKeys && in_array('array', $rules) && ! empty(preg_grep('/^'.preg_quote($key, '/').'\.+/', array_keys($this->getRules())))) { continue; } $value = data_get($this->getData(), $key, $missingValue); if ($value !== $missingValue) { Arr::set($results, $key, $value); } } return $this->replacePlaceholders($results); } /** * Validate a given attribute against a rule. * * @param string $attribute * @param string $rule * @return void */ protected function validateAttribute($attribute, $rule) { $this->currentRule = $rule; [$rule, $parameters] = ValidationRuleParser::parse($rule); if ($rule === '') { return; } // First we will get the correct keys for the given attribute in case the field is nested in // an array. Then we determine if the given rule accepts other field names as parameters. // If so, we will replace any asterisks found in the parameters with the correct keys. if ($this->dependsOnOtherFields($rule)) { $parameters = $this->replaceDotInParameters($parameters); if ($keys = $this->getExplicitKeys($attribute)) { $parameters = $this->replaceAsterisksInParameters($parameters, $keys); } } $value = $this->getValue($attribute); // If the attribute is a file, we will verify that the file upload was actually successful // and if it wasn't we will add a failure for the attribute. Files may not successfully // upload if they are too large based on PHP's settings so we will bail in this case. if ($value instanceof UploadedFile && ! $value->isValid() && $this->hasRule($attribute, array_merge($this->fileRules, $this->implicitRules)) ) { return $this->addFailure($attribute, 'uploaded', []); } // If we have made it this far we will make sure the attribute is validatable and if it is // we will call the validation method with the attribute. If a method returns false the // attribute is invalid and we will add a failure message for this failing attribute. $validatable = $this->isValidatable($rule, $attribute, $value); if ($rule instanceof RuleContract) { return $validatable ? $this->validateUsingCustomRule($attribute, $value, $rule) : null; } $method = "validate{$rule}"; if ($validatable && ! $this->$method($attribute, $value, $parameters, $this)) { $this->addFailure($attribute, $rule, $parameters); } } /** * Determine if the given rule depends on other fields. * * @param string $rule * @return bool */ protected function dependsOnOtherFields($rule) { return in_array($rule, $this->dependentRules); } /** * Get the explicit keys from an attribute flattened with dot notation. * * E.g. 'foo.1.bar.spark.baz' -> [1, 'spark'] for 'foo.*.bar.*.baz' * * @param string $attribute * @return array */ protected function getExplicitKeys($attribute) { $pattern = str_replace('\*', '([^\.]+)', preg_quote($this->getPrimaryAttribute($attribute), '/')); if (preg_match('/^'.$pattern.'/', $attribute, $keys)) { array_shift($keys); return $keys; } return []; } /** * Get the primary attribute name. * * For example, if "name.0" is given, "name.*" will be returned. * * @param string $attribute * @return string */ protected function getPrimaryAttribute($attribute) { foreach ($this->implicitAttributes as $unparsed => $parsed) { if (in_array($attribute, $parsed, true)) { return $unparsed; } } return $attribute; } /** * Replace each field parameter which has an escaped dot with the dot placeholder. * * @param array $parameters * @return array */ protected function replaceDotInParameters(array $parameters) { return array_map(function ($field) { return str_replace('\.', $this->dotPlaceholder, $field); }, $parameters); } /** * Replace each field parameter which has asterisks with the given keys. * * @param array $parameters * @param array $keys * @return array */ protected function replaceAsterisksInParameters(array $parameters, array $keys) { return array_map(function ($field) use ($keys) { return vsprintf(str_replace('*', '%s', $field), $keys); }, $parameters); } /** * Determine if the attribute is validatable. * * @param object|string $rule * @param string $attribute * @param mixed $value * @return bool */ protected function isValidatable($rule, $attribute, $value) { if (in_array($rule, $this->excludeRules)) { return true; } return $this->presentOrRuleIsImplicit($rule, $attribute, $value) && $this->passesOptionalCheck($attribute) && $this->isNotNullIfMarkedAsNullable($rule, $attribute) && $this->hasNotFailedPreviousRuleIfPresenceRule($rule, $attribute); } /** * Determine if the field is present, or the rule implies required. * * @param object|string $rule * @param string $attribute * @param mixed $value * @return bool */ protected function presentOrRuleIsImplicit($rule, $attribute, $value) { if (is_string($value) && trim($value) === '') { return $this->isImplicit($rule); } return $this->validatePresent($attribute, $value) || $this->isImplicit($rule); } /** * Determine if a given rule implies the attribute is required. * * @param object|string $rule * @return bool */ protected function isImplicit($rule) { return $rule instanceof ImplicitRule || in_array($rule, $this->implicitRules); } /** * Determine if the attribute passes any optional check. * * @param string $attribute * @return bool */ protected function passesOptionalCheck($attribute) { if (! $this->hasRule($attribute, ['Sometimes'])) { return true; } $data = ValidationData::initializeAndGatherData($attribute, $this->data); return array_key_exists($attribute, $data) || array_key_exists($attribute, $this->data); } /** * Determine if the attribute fails the nullable check. * * @param string $rule * @param string $attribute * @return bool */ protected function isNotNullIfMarkedAsNullable($rule, $attribute) { if ($this->isImplicit($rule) || ! $this->hasRule($attribute, ['Nullable'])) { return true; } return ! is_null(Arr::get($this->data, $attribute, 0)); } /** * Determine if it's a necessary presence validation. * * This is to avoid possible database type comparison errors. * * @param string $rule * @param string $attribute * @return bool */ protected function hasNotFailedPreviousRuleIfPresenceRule($rule, $attribute) { return in_array($rule, ['Unique', 'Exists']) ? ! $this->messages->has($attribute) : true; } /** * Validate an attribute using a custom rule object. * * @param string $attribute * @param mixed $value * @param \Illuminate\Contracts\Validation\Rule $rule * @return void */ protected function validateUsingCustomRule($attribute, $value, $rule) { $attribute = $this->replacePlaceholderInString($attribute); $value = is_array($value) ? $this->replacePlaceholders($value) : $value; if ($rule instanceof ValidatorAwareRule) { $rule->setValidator($this); } if ($rule instanceof DataAwareRule) { $rule->setData($this->data); } if (! $rule->passes($attribute, $value)) { $ruleClass = $rule instanceof InvokableValidationRule ? get_class($rule->invokable()) : get_class($rule); $this->failedRules[$attribute][$ruleClass] = []; $messages = $this->getFromLocalArray($attribute, $ruleClass) ?? $rule->message(); $messages = $messages ? (array) $messages : [$ruleClass]; foreach ($messages as $key => $message) { $key = is_string($key) ? $key : $attribute; $this->messages->add($key, $this->makeReplacements( $message, $key, $ruleClass, [] )); } } } /** * Check if we should stop further validations on a given attribute. * * @param string $attribute * @return bool */ protected function shouldStopValidating($attribute) { $cleanedAttribute = $this->replacePlaceholderInString($attribute); if ($this->hasRule($attribute, ['Bail'])) { return $this->messages->has($cleanedAttribute); } if (isset($this->failedRules[$cleanedAttribute]) && array_key_exists('uploaded', $this->failedRules[$cleanedAttribute])) { return true; } // In case the attribute has any rule that indicates that the field is required // and that rule already failed then we should stop validation at this point // as now there is no point in calling other rules with this field empty. return $this->hasRule($attribute, $this->implicitRules) && isset($this->failedRules[$cleanedAttribute]) && array_intersect(array_keys($this->failedRules[$cleanedAttribute]), $this->implicitRules); } /** * Add a failed rule and error message to the collection. * * @param string $attribute * @param string $rule * @param array $parameters * @return void */ public function addFailure($attribute, $rule, $parameters = []) { if (! $this->messages) { $this->passes(); } $attributeWithPlaceholders = $attribute; $attribute = $this->replacePlaceholderInString($attribute); if (in_array($rule, $this->excludeRules)) { return $this->excludeAttribute($attribute); } $this->messages->add($attribute, $this->makeReplacements( $this->getMessage($attributeWithPlaceholders, $rule), $attribute, $rule, $parameters )); $this->failedRules[$attribute][$rule] = $parameters; } /** * Add the given attribute to the list of excluded attributes. * * @param string $attribute * @return void */ protected function excludeAttribute(string $attribute) { $this->excludeAttributes[] = $attribute; $this->excludeAttributes = array_unique($this->excludeAttributes); } /** * Returns the data which was valid. * * @return array */ public function valid() { if (! $this->messages) { $this->passes(); } return array_diff_key( $this->data, $this->attributesThatHaveMessages() ); } /** * Returns the data which was invalid. * * @return array */ public function invalid() { if (! $this->messages) { $this->passes(); } $invalid = array_intersect_key( $this->data, $this->attributesThatHaveMessages() ); $result = []; $failed = Arr::only(Arr::dot($invalid), array_keys($this->failed())); foreach ($failed as $key => $failure) { Arr::set($result, $key, $failure); } return $result; } /** * Generate an array of all attributes that have messages. * * @return array */ protected function attributesThatHaveMessages() { return collect($this->messages()->toArray())->map(function ($message, $key) { return explode('.', $key)[0]; })->unique()->flip()->all(); } /** * Get the failed validation rules. * * @return array */ public function failed() { return $this->failedRules; } /** * Get the message container for the validator. * * @return \Illuminate\Support\MessageBag */ public function messages() { if (! $this->messages) { $this->passes(); } return $this->messages; } /** * An alternative more semantic shortcut to the message container. * * @return \Illuminate\Support\MessageBag */ public function errors() { return $this->messages(); } /** * Get the messages for the instance. * * @return \Illuminate\Support\MessageBag */ public function getMessageBag() { return $this->messages(); } /** * Determine if the given attribute has a rule in the given set. * * @param string $attribute * @param string|array $rules * @return bool */ public function hasRule($attribute, $rules) { return ! is_null($this->getRule($attribute, $rules)); } /** * Get a rule and its parameters for a given attribute. * * @param string $attribute * @param string|array $rules * @return array|null */ protected function getRule($attribute, $rules) { if (! array_key_exists($attribute, $this->rules)) { return; } $rules = (array) $rules; foreach ($this->rules[$attribute] as $rule) { [$rule, $parameters] = ValidationRuleParser::parse($rule); if (in_array($rule, $rules)) { return [$rule, $parameters]; } } } /** * Get the data under validation. * * @return array */ public function attributes() { return $this->getData(); } /** * Get the data under validation. * * @return array */ public function getData() { return $this->data; } /** * Set the data under validation. * * @param array $data * @return $this */ public function setData(array $data) { $this->data = $this->parseData($data); $this->setRules($this->initialRules); return $this; } /** * Get the value of a given attribute. * * @param string $attribute * @return mixed */ protected function getValue($attribute) { return Arr::get($this->data, $attribute); } /** * Set the value of a given attribute. * * @param string $attribute * @param mixed $value * @return void */ public function setValue($attribute, $value) { Arr::set($this->data, $attribute, $value); } /** * Get the validation rules. * * @return array */ public function getRules() { return $this->rules; } /** * Get the validation rules with key placeholders removed. * * @return array */ public function getRulesWithoutPlaceholders() { return collect($this->rules) ->mapWithKeys(fn ($value, $key) => [ str_replace($this->dotPlaceholder, '\\.', $key) => $value, ]) ->all(); } /** * Set the validation rules. * * @param array $rules * @return $this */ public function setRules(array $rules) { $rules = collect($rules)->mapWithKeys(function ($value, $key) { return [str_replace('\.', $this->dotPlaceholder, $key) => $value]; })->toArray(); $this->initialRules = $rules; $this->rules = []; $this->addRules($rules); return $this; } /** * Parse the given rules and merge them into current rules. * * @param array $rules * @return void */ public function addRules($rules) { // The primary purpose of this parser is to expand any "*" rules to the all // of the explicit rules needed for the given data. For example the rule // names.* would get expanded to names.0, names.1, etc. for this data. $response = (new ValidationRuleParser($this->data)) ->explode(ValidationRuleParser::filterConditionalRules($rules, $this->data)); $this->rules = array_merge_recursive( $this->rules, $response->rules ); $this->implicitAttributes = array_merge( $this->implicitAttributes, $response->implicitAttributes ); } /** * Add conditions to a given field based on a Closure. * * @param string|array $attribute * @param string|array $rules * @param callable $callback * @return $this */ public function sometimes($attribute, $rules, callable $callback) { $payload = new Fluent($this->data); foreach ((array) $attribute as $key) { $response = (new ValidationRuleParser($this->data))->explode([$key => $rules]); $this->implicitAttributes = array_merge($response->implicitAttributes, $this->implicitAttributes); foreach ($response->rules as $ruleKey => $ruleValue) { if ($callback($payload, $this->dataForSometimesIteration($ruleKey, ! str_ends_with($key, '.*')))) { $this->addRules([$ruleKey => $ruleValue]); } } } return $this; } /** * Get the data that should be injected into the iteration of a wildcard "sometimes" callback. * * @param string $attribute * @return \Illuminate\Support\Fluent|array|mixed */ private function dataForSometimesIteration(string $attribute, $removeLastSegmentOfAttribute) { $lastSegmentOfAttribute = strrchr($attribute, '.'); $attribute = $lastSegmentOfAttribute && $removeLastSegmentOfAttribute ? Str::replaceLast($lastSegmentOfAttribute, '', $attribute) : $attribute; return is_array($data = data_get($this->data, $attribute)) ? new Fluent($data) : $data; } /** * Instruct the validator to stop validating after the first rule failure. * * @param bool $stopOnFirstFailure * @return $this */ public function stopOnFirstFailure($stopOnFirstFailure = true) { $this->stopOnFirstFailure = $stopOnFirstFailure; return $this; } /** * Register an array of custom validator extensions. * * @param array $extensions * @return void */ public function addExtensions(array $extensions) { if ($extensions) { $keys = array_map([Str::class, 'snake'], array_keys($extensions)); $extensions = array_combine($keys, array_values($extensions)); } $this->extensions = array_merge($this->extensions, $extensions); } /** * Register an array of custom implicit validator extensions. * * @param array $extensions * @return void */ public function addImplicitExtensions(array $extensions) { $this->addExtensions($extensions); foreach ($extensions as $rule => $extension) { $this->implicitRules[] = Str::studly($rule); } } /** * Register an array of custom dependent validator extensions. * * @param array $extensions * @return void */ public function addDependentExtensions(array $extensions) { $this->addExtensions($extensions); foreach ($extensions as $rule => $extension) { $this->dependentRules[] = Str::studly($rule); } } /** * Register a custom validator extension. * * @param string $rule * @param \Closure|string $extension * @return void */ public function addExtension($rule, $extension) { $this->extensions[Str::snake($rule)] = $extension; } /** * Register a custom implicit validator extension. * * @param string $rule * @param \Closure|string $extension * @return void */ public function addImplicitExtension($rule, $extension) { $this->addExtension($rule, $extension); $this->implicitRules[] = Str::studly($rule); } /** * Register a custom dependent validator extension. * * @param string $rule * @param \Closure|string $extension * @return void */ public function addDependentExtension($rule, $extension) { $this->addExtension($rule, $extension); $this->dependentRules[] = Str::studly($rule); } /** * Register an array of custom validator message replacers. * * @param array $replacers * @return void */ public function addReplacers(array $replacers) { if ($replacers) { $keys = array_map([Str::class, 'snake'], array_keys($replacers)); $replacers = array_combine($keys, array_values($replacers)); } $this->replacers = array_merge($this->replacers, $replacers); } /** * Register a custom validator message replacer. * * @param string $rule * @param \Closure|string $replacer * @return void */ public function addReplacer($rule, $replacer) { $this->replacers[Str::snake($rule)] = $replacer; } /** * Set the custom messages for the validator. * * @param array $messages * @return $this */ public function setCustomMessages(array $messages) { $this->customMessages = array_merge($this->customMessages, $messages); return $this; } /** * Set the custom attributes on the validator. * * @param array $attributes * @return $this */ public function setAttributeNames(array $attributes) { $this->customAttributes = $attributes; return $this; } /** * Add custom attributes to the validator. * * @param array $attributes * @return $this */ public function addCustomAttributes(array $attributes) { $this->customAttributes = array_merge($this->customAttributes, $attributes); return $this; } /** * Set the callback that used to format an implicit attribute. * * @param callable|null $formatter * @return $this */ public function setImplicitAttributesFormatter(callable $formatter = null) { $this->implicitAttributesFormatter = $formatter; return $this; } /** * Set the custom values on the validator. * * @param array $values * @return $this */ public function setValueNames(array $values) { $this->customValues = $values; return $this; } /** * Add the custom values for the validator. * * @param array $customValues * @return $this */ public function addCustomValues(array $customValues) { $this->customValues = array_merge($this->customValues, $customValues); return $this; } /** * Set the fallback messages for the validator. * * @param array $messages * @return void */ public function setFallbackMessages(array $messages) { $this->fallbackMessages = $messages; } /** * Get the Presence Verifier implementation. * * @param string|null $connection * @return \Illuminate\Validation\PresenceVerifierInterface * * @throws \RuntimeException */ public function getPresenceVerifier($connection = null) { if (! isset($this->presenceVerifier)) { throw new RuntimeException('Presence verifier has not been set.'); } if ($this->presenceVerifier instanceof DatabasePresenceVerifierInterface) { $this->presenceVerifier->setConnection($connection); } return $this->presenceVerifier; } /** * Set the Presence Verifier implementation. * * @param \Illuminate\Validation\PresenceVerifierInterface $presenceVerifier * @return void */ public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier) { $this->presenceVerifier = $presenceVerifier; } /** * Get the exception to throw upon failed validation. * * @return string */ public function getException() { return $this->exception; } /** * Set the exception to throw upon failed validation. * * @param string $exception * @return $this * * @throws \InvalidArgumentException */ public function setException($exception) { if (! is_a($exception, ValidationException::class, true)) { throw new InvalidArgumentException( sprintf('Exception [%s] is invalid. It must extend [%s].', $exception, ValidationException::class) ); } $this->exception = $exception; return $this; } /** * Ensure exponents are within range using the given callback. * * @param callable(int $scale, string $attribute, mixed $value) $callback * @return $this */ public function ensureExponentWithinAllowedRangeUsing($callback) { $this->ensureExponentWithinAllowedRangeUsing = $callback; return $this; } /** * Get the Translator implementation. * * @return \Illuminate\Contracts\Translation\Translator */ public function getTranslator() { return $this->translator; } /** * Set the Translator implementation. * * @param \Illuminate\Contracts\Translation\Translator $translator * @return void */ public function setTranslator(Translator $translator) { $this->translator = $translator; } /** * Set the IoC container instance. * * @param \Illuminate\Contracts\Container\Container $container * @return void */ public function setContainer(Container $container) { $this->container = $container; } /** * Call a custom validator extension. * * @param string $rule * @param array $parameters * @return bool|null */ protected function callExtension($rule, $parameters) { $callback = $this->extensions[$rule]; if (is_callable($callback)) { return $callback(...array_values($parameters)); } elseif (is_string($callback)) { return $this->callClassBasedExtension($callback, $parameters); } } /** * Call a class based validator extension. * * @param string $callback * @param array $parameters * @return bool */ protected function callClassBasedExtension($callback, $parameters) { [$class, $method] = Str::parseCallback($callback, 'validate'); return $this->container->make($class)->{$method}(...array_values($parameters)); } /** * Handle dynamic calls to class methods. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public function __call($method, $parameters) { $rule = Str::snake(substr($method, 8)); if (isset($this->extensions[$rule])) { return $this->callExtension($rule, $parameters); } throw new BadMethodCallException(sprintf( 'Method %s::%s does not exist.', static::class, $method )); } } src/Illuminate/Validation/UnauthorizedException.php 0000644 00000000200 15107327041 0016571 0 ustar 00 <?php namespace Illuminate\Validation; use RuntimeException; class UnauthorizedException extends RuntimeException { // } src/Illuminate/Validation/DatabasePresenceVerifierInterface.php 0000644 00000000437 15107327041 0020753 0 ustar 00 <?php namespace Illuminate\Validation; interface DatabasePresenceVerifierInterface extends PresenceVerifierInterface { /** * Set the connection to be used. * * @param string $connection * @return void */ public function setConnection($connection); } src/Illuminate/Validation/ValidationServiceProvider.php 0000644 00000004215 15107327041 0017371 0 ustar 00 <?php namespace Illuminate\Validation; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Contracts\Validation\UncompromisedVerifier; use Illuminate\Http\Client\Factory as HttpFactory; use Illuminate\Support\ServiceProvider; class ValidationServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Register the service provider. * * @return void */ public function register() { $this->registerPresenceVerifier(); $this->registerUncompromisedVerifier(); $this->registerValidationFactory(); } /** * Register the validation factory. * * @return void */ protected function registerValidationFactory() { $this->app->singleton('validator', function ($app) { $validator = new Factory($app['translator'], $app); // The validation presence verifier is responsible for determining the existence of // values in a given data collection which is typically a relational database or // other persistent data stores. It is used to check for "uniqueness" as well. if (isset($app['db'], $app['validation.presence'])) { $validator->setPresenceVerifier($app['validation.presence']); } return $validator; }); } /** * Register the database presence verifier. * * @return void */ protected function registerPresenceVerifier() { $this->app->singleton('validation.presence', function ($app) { return new DatabasePresenceVerifier($app['db']); }); } /** * Register the uncompromised password verifier. * * @return void */ protected function registerUncompromisedVerifier() { $this->app->singleton(UncompromisedVerifier::class, function ($app) { return new NotPwnedVerifier($app[HttpFactory::class]); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return ['validator', 'validation.presence', UncompromisedVerifier::class]; } } src/Illuminate/Validation/ClosureValidationRule.php 0000644 00000003646 15107327041 0016531 0 ustar 00 <?php namespace Illuminate\Validation; use Illuminate\Contracts\Validation\Rule as RuleContract; use Illuminate\Contracts\Validation\ValidatorAwareRule; use Illuminate\Translation\CreatesPotentiallyTranslatedStrings; class ClosureValidationRule implements RuleContract, ValidatorAwareRule { use CreatesPotentiallyTranslatedStrings; /** * The callback that validates the attribute. * * @var \Closure */ public $callback; /** * Indicates if the validation callback failed. * * @var bool */ public $failed = false; /** * The validation error messages. * * @var array */ public $messages = []; /** * The current validator. * * @var \Illuminate\Validation\Validator */ protected $validator; /** * Create a new Closure based validation rule. * * @param \Closure $callback * @return void */ public function __construct($callback) { $this->callback = $callback; } /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value) { $this->failed = false; $this->callback->__invoke($attribute, $value, function ($attribute, $message = null) { $this->failed = true; return $this->pendingPotentiallyTranslatedString($attribute, $message); }); return ! $this->failed; } /** * Get the validation error messages. * * @return string */ public function message() { return $this->messages; } /** * Set the current validator. * * @param \Illuminate\Validation\Validator $validator * @return $this */ public function setValidator($validator) { $this->validator = $validator; return $this; } } src/Illuminate/Cookie/CookieJar.php 0000644 00000014354 15107327041 0013235 0 ustar 00 <?php namespace Illuminate\Cookie; use Illuminate\Contracts\Cookie\QueueingFactory as JarContract; use Illuminate\Support\Arr; use Illuminate\Support\InteractsWithTime; use Illuminate\Support\Traits\Macroable; use Symfony\Component\HttpFoundation\Cookie; class CookieJar implements JarContract { use InteractsWithTime, Macroable; /** * The default path (if specified). * * @var string */ protected $path = '/'; /** * The default domain (if specified). * * @var string|null */ protected $domain; /** * The default secure setting (defaults to null). * * @var bool|null */ protected $secure; /** * The default SameSite option (defaults to lax). * * @var string */ protected $sameSite = 'lax'; /** * All of the cookies queued for sending. * * @var \Symfony\Component\HttpFoundation\Cookie[] */ protected $queued = []; /** * Create a new cookie instance. * * @param string $name * @param string $value * @param int $minutes * @param string|null $path * @param string|null $domain * @param bool|null $secure * @param bool $httpOnly * @param bool $raw * @param string|null $sameSite * @return \Symfony\Component\HttpFoundation\Cookie */ public function make($name, $value, $minutes = 0, $path = null, $domain = null, $secure = null, $httpOnly = true, $raw = false, $sameSite = null) { [$path, $domain, $secure, $sameSite] = $this->getPathAndDomain($path, $domain, $secure, $sameSite); $time = ($minutes == 0) ? 0 : $this->availableAt($minutes * 60); return new Cookie($name, $value, $time, $path, $domain, $secure, $httpOnly, $raw, $sameSite); } /** * Create a cookie that lasts "forever" (400 days). * * @param string $name * @param string $value * @param string|null $path * @param string|null $domain * @param bool|null $secure * @param bool $httpOnly * @param bool $raw * @param string|null $sameSite * @return \Symfony\Component\HttpFoundation\Cookie */ public function forever($name, $value, $path = null, $domain = null, $secure = null, $httpOnly = true, $raw = false, $sameSite = null) { return $this->make($name, $value, 576000, $path, $domain, $secure, $httpOnly, $raw, $sameSite); } /** * Expire the given cookie. * * @param string $name * @param string|null $path * @param string|null $domain * @return \Symfony\Component\HttpFoundation\Cookie */ public function forget($name, $path = null, $domain = null) { return $this->make($name, null, -2628000, $path, $domain); } /** * Determine if a cookie has been queued. * * @param string $key * @param string|null $path * @return bool */ public function hasQueued($key, $path = null) { return ! is_null($this->queued($key, null, $path)); } /** * Get a queued cookie instance. * * @param string $key * @param mixed $default * @param string|null $path * @return \Symfony\Component\HttpFoundation\Cookie|null */ public function queued($key, $default = null, $path = null) { $queued = Arr::get($this->queued, $key, $default); if ($path === null) { return Arr::last($queued, null, $default); } return Arr::get($queued, $path, $default); } /** * Queue a cookie to send with the next response. * * @param mixed ...$parameters * @return void */ public function queue(...$parameters) { if (isset($parameters[0]) && $parameters[0] instanceof Cookie) { $cookie = $parameters[0]; } else { $cookie = $this->make(...array_values($parameters)); } if (! isset($this->queued[$cookie->getName()])) { $this->queued[$cookie->getName()] = []; } $this->queued[$cookie->getName()][$cookie->getPath()] = $cookie; } /** * Queue a cookie to expire with the next response. * * @param string $name * @param string|null $path * @param string|null $domain * @return void */ public function expire($name, $path = null, $domain = null) { $this->queue($this->forget($name, $path, $domain)); } /** * Remove a cookie from the queue. * * @param string $name * @param string|null $path * @return void */ public function unqueue($name, $path = null) { if ($path === null) { unset($this->queued[$name]); return; } unset($this->queued[$name][$path]); if (empty($this->queued[$name])) { unset($this->queued[$name]); } } /** * Get the path and domain, or the default values. * * @param string $path * @param string|null $domain * @param bool|null $secure * @param string|null $sameSite * @return array */ protected function getPathAndDomain($path, $domain, $secure = null, $sameSite = null) { return [$path ?: $this->path, $domain ?: $this->domain, is_bool($secure) ? $secure : $this->secure, $sameSite ?: $this->sameSite]; } /** * Set the default path and domain for the jar. * * @param string $path * @param string|null $domain * @param bool|null $secure * @param string|null $sameSite * @return $this */ public function setDefaultPathAndDomain($path, $domain, $secure = false, $sameSite = null) { [$this->path, $this->domain, $this->secure, $this->sameSite] = [$path, $domain, $secure, $sameSite]; return $this; } /** * Get the cookies which have been queued for the next request. * * @return \Symfony\Component\HttpFoundation\Cookie[] */ public function getQueuedCookies() { return Arr::flatten($this->queued); } /** * Flush the cookies which have been queued for the next request. * * @return $this */ public function flushQueuedCookies() { $this->queued = []; return $this; } } src/Illuminate/Cookie/CookieValuePrefix.php 0000644 00000002144 15107327041 0014745 0 ustar 00 <?php namespace Illuminate\Cookie; class CookieValuePrefix { /** * Create a new cookie value prefix for the given cookie name. * * @param string $cookieName * @param string $key * @return string */ public static function create($cookieName, $key) { return hash_hmac('sha1', $cookieName.'v2', $key).'|'; } /** * Remove the cookie value prefix. * * @param string $cookieValue * @return string */ public static function remove($cookieValue) { return substr($cookieValue, 41); } /** * Validate a cookie value contains a valid prefix. If it does, return the cookie value with the prefix removed. Otherwise, return null. * * @param string $cookieName * @param string $cookieValue * @param string $key * @return string|null */ public static function validate($cookieName, $cookieValue, $key) { $hasValidPrefix = str_starts_with($cookieValue, static::create($cookieName, $key)); return $hasValidPrefix ? static::remove($cookieValue) : null; } } src/Illuminate/Cookie/composer.json 0000644 00000001761 15107327041 0013376 0 ustar 00 { "name": "illuminate/cookie", "description": "The Illuminate Cookie package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "ext-hash": "*", "illuminate/collections": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0", "illuminate/support": "^10.0", "symfony/http-foundation": "^6.2", "symfony/http-kernel": "^6.2" }, "autoload": { "psr-4": { "Illuminate\\Cookie\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Cookie/CookieServiceProvider.php 0000644 00000001065 15107327041 0015627 0 ustar 00 <?php namespace Illuminate\Cookie; use Illuminate\Support\ServiceProvider; class CookieServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton('cookie', function ($app) { $config = $app->make('config')->get('session'); return (new CookieJar)->setDefaultPathAndDomain( $config['path'], $config['domain'], $config['secure'], $config['same_site'] ?? null ); }); } } src/Illuminate/Cookie/LICENSE.md 0000644 00000002063 15107327041 0012254 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php 0000644 00000001712 15107327041 0020636 0 ustar 00 <?php namespace Illuminate\Cookie\Middleware; use Closure; use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar; class AddQueuedCookiesToResponse { /** * The cookie jar instance. * * @var \Illuminate\Contracts\Cookie\QueueingFactory */ protected $cookies; /** * Create a new CookieQueue instance. * * @param \Illuminate\Contracts\Cookie\QueueingFactory $cookies * @return void */ public function __construct(CookieJar $cookies) { $this->cookies = $cookies; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $response = $next($request); foreach ($this->cookies->getQueuedCookies() as $cookie) { $response->headers->setCookie($cookie); } return $response; } } src/Illuminate/Cookie/Middleware/EncryptCookies.php 0000644 00000013601 15107327041 0016377 0 ustar 00 <?php namespace Illuminate\Cookie\Middleware; use Closure; use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract; use Illuminate\Cookie\CookieValuePrefix; use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class EncryptCookies { /** * The encrypter instance. * * @var \Illuminate\Contracts\Encryption\Encrypter */ protected $encrypter; /** * The names of the cookies that should not be encrypted. * * @var array<int, string> */ protected $except = []; /** * Indicates if cookies should be serialized. * * @var bool */ protected static $serialize = false; /** * Create a new CookieGuard instance. * * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter * @return void */ public function __construct(EncrypterContract $encrypter) { $this->encrypter = $encrypter; } /** * Disable encryption for the given cookie name(s). * * @param string|array $name * @return void */ public function disableFor($name) { $this->except = array_merge($this->except, (array) $name); } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return \Symfony\Component\HttpFoundation\Response */ public function handle($request, Closure $next) { return $this->encrypt($next($this->decrypt($request))); } /** * Decrypt the cookies on the request. * * @param \Symfony\Component\HttpFoundation\Request $request * @return \Symfony\Component\HttpFoundation\Request */ protected function decrypt(Request $request) { foreach ($request->cookies as $key => $cookie) { if ($this->isDisabled($key)) { continue; } try { $value = $this->decryptCookie($key, $cookie); $request->cookies->set($key, $this->validateValue($key, $value)); } catch (DecryptException) { $request->cookies->set($key, null); } } return $request; } /** * Validate and remove the cookie value prefix from the value. * * @param string $key * @param string $value * @return string|array|null */ protected function validateValue(string $key, $value) { return is_array($value) ? $this->validateArray($key, $value) : CookieValuePrefix::validate($key, $value, $this->encrypter->getKey()); } /** * Validate and remove the cookie value prefix from all values of an array. * * @param string $key * @param array $value * @return array */ protected function validateArray(string $key, array $value) { $validated = []; foreach ($value as $index => $subValue) { $validated[$index] = $this->validateValue("{$key}[{$index}]", $subValue); } return $validated; } /** * Decrypt the given cookie and return the value. * * @param string $name * @param string|array $cookie * @return string|array */ protected function decryptCookie($name, $cookie) { return is_array($cookie) ? $this->decryptArray($cookie) : $this->encrypter->decrypt($cookie, static::serialized($name)); } /** * Decrypt an array based cookie. * * @param array $cookie * @return array */ protected function decryptArray(array $cookie) { $decrypted = []; foreach ($cookie as $key => $value) { if (is_string($value)) { $decrypted[$key] = $this->encrypter->decrypt($value, static::serialized($key)); } if (is_array($value)) { $decrypted[$key] = $this->decryptArray($value); } } return $decrypted; } /** * Encrypt the cookies on an outgoing response. * * @param \Symfony\Component\HttpFoundation\Response $response * @return \Symfony\Component\HttpFoundation\Response */ protected function encrypt(Response $response) { foreach ($response->headers->getCookies() as $cookie) { if ($this->isDisabled($cookie->getName())) { continue; } $response->headers->setCookie($this->duplicate( $cookie, $this->encrypter->encrypt( CookieValuePrefix::create($cookie->getName(), $this->encrypter->getKey()).$cookie->getValue(), static::serialized($cookie->getName()) ) )); } return $response; } /** * Duplicate a cookie with a new value. * * @param \Symfony\Component\HttpFoundation\Cookie $cookie * @param mixed $value * @return \Symfony\Component\HttpFoundation\Cookie */ protected function duplicate(Cookie $cookie, $value) { return new Cookie( $cookie->getName(), $value, $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly(), $cookie->isRaw(), $cookie->getSameSite() ); } /** * Determine whether encryption has been disabled for the given cookie. * * @param string $name * @return bool */ public function isDisabled($name) { return in_array($name, $this->except); } /** * Determine if the cookie contents should be serialized. * * @param string $name * @return bool */ public static function serialized($name) { return static::$serialize; } } src/Illuminate/Http/Exceptions/ThrottleRequestsException.php 0000644 00000001170 15107327041 0020426 0 ustar 00 <?php namespace Illuminate\Http\Exceptions; use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException; use Throwable; class ThrottleRequestsException extends TooManyRequestsHttpException { /** * Create a new throttle requests exception instance. * * @param string $message * @param \Throwable|null $previous * @param array $headers * @param int $code * @return void */ public function __construct($message = '', Throwable $previous = null, array $headers = [], $code = 0) { parent::__construct(null, $message, $previous, $code, $headers); } } src/Illuminate/Http/Exceptions/HttpResponseException.php 0000644 00000001426 15107327041 0017527 0 ustar 00 <?php namespace Illuminate\Http\Exceptions; use RuntimeException; use Symfony\Component\HttpFoundation\Response; class HttpResponseException extends RuntimeException { /** * The underlying response instance. * * @var \Symfony\Component\HttpFoundation\Response */ protected $response; /** * Create a new HTTP response exception instance. * * @param \Symfony\Component\HttpFoundation\Response $response * @return void */ public function __construct(Response $response) { $this->response = $response; } /** * Get the underlying response instance. * * @return \Symfony\Component\HttpFoundation\Response */ public function getResponse() { return $this->response; } } src/Illuminate/Http/Exceptions/PostTooLargeException.php 0000644 00000001124 15107327041 0017446 0 ustar 00 <?php namespace Illuminate\Http\Exceptions; use Symfony\Component\HttpKernel\Exception\HttpException; use Throwable; class PostTooLargeException extends HttpException { /** * Create a new "post too large" exception instance. * * @param string $message * @param \Throwable|null $previous * @param array $headers * @param int $code * @return void */ public function __construct($message = '', Throwable $previous = null, array $headers = [], $code = 0) { parent::__construct(413, $message, $previous, $headers, $code); } } src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php 0000644 00000026615 15107327041 0020714 0 ustar 00 <?php namespace Illuminate\Http\Resources; use Illuminate\Support\Arr; use Illuminate\Support\Str; trait ConditionallyLoadsAttributes { /** * Filter the given data, removing any optional values. * * @param array $data * @return array */ protected function filter($data) { $index = -1; foreach ($data as $key => $value) { $index++; if (is_array($value)) { $data[$key] = $this->filter($value); continue; } if (is_numeric($key) && $value instanceof MergeValue) { return $this->mergeData( $data, $index, $this->filter($value->data), array_values($value->data) === $value->data ); } if ($value instanceof self && is_null($value->resource)) { $data[$key] = null; } } return $this->removeMissingValues($data); } /** * Merge the given data in at the given index. * * @param array $data * @param int $index * @param array $merge * @param bool $numericKeys * @return array */ protected function mergeData($data, $index, $merge, $numericKeys) { if ($numericKeys) { return $this->removeMissingValues(array_merge( array_merge(array_slice($data, 0, $index, true), $merge), $this->filter(array_values(array_slice($data, $index + 1, null, true))) )); } return $this->removeMissingValues(array_slice($data, 0, $index, true) + $merge + $this->filter(array_slice($data, $index + 1, null, true))); } /** * Remove the missing values from the filtered data. * * @param array $data * @return array */ protected function removeMissingValues($data) { $numericKeys = true; foreach ($data as $key => $value) { if (($value instanceof PotentiallyMissing && $value->isMissing()) || ($value instanceof self && $value->resource instanceof PotentiallyMissing && $value->isMissing())) { unset($data[$key]); } else { $numericKeys = $numericKeys && is_numeric($key); } } if (property_exists($this, 'preserveKeys') && $this->preserveKeys === true) { return $data; } return $numericKeys ? array_values($data) : $data; } /** * Retrieve a value if the given "condition" is truthy. * * @param bool $condition * @param mixed $value * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ protected function when($condition, $value, $default = null) { if ($condition) { return value($value); } return func_num_args() === 3 ? value($default) : new MissingValue; } /** * Retrieve a value if the given "condition" is falsy. * * @param bool $condition * @param mixed $value * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ public function unless($condition, $value, $default = null) { $arguments = func_num_args() === 2 ? [$value] : [$value, $default]; return $this->when(! $condition, ...$arguments); } /** * Merge a value into the array. * * @param mixed $value * @return \Illuminate\Http\Resources\MergeValue|mixed */ protected function merge($value) { return $this->mergeWhen(true, $value); } /** * Merge a value if the given condition is truthy. * * @param bool $condition * @param mixed $value * @param mixed $default * @return \Illuminate\Http\Resources\MergeValue|mixed */ protected function mergeWhen($condition, $value, $default = null) { if ($condition) { return new MergeValue(value($value)); } return func_num_args() === 3 ? new MergeValue(value($default)) : new MissingValue(); } /** * Merge a value unless the given condition is truthy. * * @param bool $condition * @param mixed $value * @param mixed $default * @return \Illuminate\Http\Resources\MergeValue|mixed */ protected function mergeUnless($condition, $value, $default = null) { $arguments = func_num_args() === 2 ? [$value] : [$value, $default]; return $this->mergeWhen(! $condition, ...$arguments); } /** * Merge the given attributes. * * @param array $attributes * @return \Illuminate\Http\Resources\MergeValue */ protected function attributes($attributes) { return new MergeValue( Arr::only($this->resource->toArray(), $attributes) ); } /** * Retrieve an attribute if it exists on the resource. * * @param string $attribute * @param mixed $value * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ public function whenHas($attribute, $value = null, $default = null) { if (func_num_args() < 3) { $default = new MissingValue; } if (! array_key_exists($attribute, $this->resource->getAttributes())) { return value($default); } return func_num_args() === 1 ? $this->resource->{$attribute} : value($value, $this->resource->{$attribute}); } /** * Retrieve a model attribute if it is null. * * @param mixed $value * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ protected function whenNull($value, $default = null) { $arguments = func_num_args() == 1 ? [$value] : [$value, $default]; return $this->when(is_null($value), ...$arguments); } /** * Retrieve a model attribute if it is not null. * * @param mixed $value * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ protected function whenNotNull($value, $default = null) { $arguments = func_num_args() == 1 ? [$value] : [$value, $default]; return $this->when(! is_null($value), ...$arguments); } /** * Retrieve an accessor when it has been appended. * * @param string $attribute * @param mixed $value * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ protected function whenAppended($attribute, $value = null, $default = null) { if ($this->resource->hasAppended($attribute)) { return func_num_args() >= 2 ? value($value) : $this->resource->$attribute; } return func_num_args() === 3 ? value($default) : new MissingValue; } /** * Retrieve a relationship if it has been loaded. * * @param string $relationship * @param mixed $value * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ protected function whenLoaded($relationship, $value = null, $default = null) { if (func_num_args() < 3) { $default = new MissingValue; } if (! $this->resource->relationLoaded($relationship)) { return value($default); } if (func_num_args() === 1) { return $this->resource->{$relationship}; } if ($this->resource->{$relationship} === null) { return; } return value($value); } /** * Retrieve a relationship count if it exists. * * @param string $relationship * @param mixed $value * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ public function whenCounted($relationship, $value = null, $default = null) { if (func_num_args() < 3) { $default = new MissingValue; } $attribute = (string) Str::of($relationship)->snake()->finish('_count'); if (! isset($this->resource->getAttributes()[$attribute])) { return value($default); } if (func_num_args() === 1) { return $this->resource->{$attribute}; } if ($this->resource->{$attribute} === null) { return; } return value($value, $this->resource->{$attribute}); } /** * Retrieve a relationship aggregated value if it exists. * * @param string $relationship * @param string $column * @param string $aggregate * @param mixed $value * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ public function whenAggregated($relationship, $column, $aggregate, $value = null, $default = null) { $attribute = (string) Str::of($relationship)->snake()->append('_')->append($aggregate)->append('_')->finish($column); if (! isset($this->resource->getAttributes()[$attribute])) { return value($default); } if (func_num_args() === 3) { return $this->resource->{$attribute}; } if ($this->resource->{$attribute} === null) { return; } return value($value, $this->resource->{$attribute}); } /** * Execute a callback if the given pivot table has been loaded. * * @param string $table * @param mixed $value * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ protected function whenPivotLoaded($table, $value, $default = null) { return $this->whenPivotLoadedAs('pivot', ...func_get_args()); } /** * Execute a callback if the given pivot table with a custom accessor has been loaded. * * @param string $accessor * @param string $table * @param mixed $value * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ protected function whenPivotLoadedAs($accessor, $table, $value, $default = null) { if (func_num_args() === 3) { $default = new MissingValue; } return $this->when( $this->hasPivotLoadedAs($accessor, $table), ...[$value, $default] ); } /** * Determine if the resource has the specified pivot table loaded. * * @param string $table * @return bool */ protected function hasPivotLoaded($table) { return $this->hasPivotLoadedAs('pivot', $table); } /** * Determine if the resource has the specified pivot table loaded with a custom accessor. * * @param string $accessor * @param string $table * @return bool */ protected function hasPivotLoadedAs($accessor, $table) { return isset($this->resource->$accessor) && ($this->resource->$accessor instanceof $table || $this->resource->$accessor->getTable() === $table); } /** * Transform the given value if it is present. * * @param mixed $value * @param callable $callback * @param mixed $default * @return mixed */ protected function transform($value, callable $callback, $default = null) { return transform( $value, $callback, func_num_args() === 3 ? $default : new MissingValue ); } } src/Illuminate/Http/Resources/DelegatesToResource.php 0000644 00000006447 15107327041 0016763 0 ustar 00 <?php namespace Illuminate\Http\Resources; use Exception; use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\Macroable; trait DelegatesToResource { use ForwardsCalls, Macroable { __call as macroCall; } /** * Get the value of the resource's route key. * * @return mixed */ public function getRouteKey() { return $this->resource->getRouteKey(); } /** * Get the route key for the resource. * * @return string */ public function getRouteKeyName() { return $this->resource->getRouteKeyName(); } /** * Retrieve the model for a bound value. * * @param mixed $value * @param string|null $field * @return void * * @throws \Exception */ public function resolveRouteBinding($value, $field = null) { throw new Exception('Resources may not be implicitly resolved from route bindings.'); } /** * Retrieve the model for a bound value. * * @param string $childType * @param mixed $value * @param string|null $field * @return void * * @throws \Exception */ public function resolveChildRouteBinding($childType, $value, $field = null) { throw new Exception('Resources may not be implicitly resolved from route bindings.'); } /** * Determine if the given attribute exists. * * @param mixed $offset * @return bool */ public function offsetExists($offset): bool { return isset($this->resource[$offset]); } /** * Get the value for a given offset. * * @param mixed $offset * @return mixed */ public function offsetGet($offset): mixed { return $this->resource[$offset]; } /** * Set the value for a given offset. * * @param mixed $offset * @param mixed $value * @return void */ public function offsetSet($offset, $value): void { $this->resource[$offset] = $value; } /** * Unset the value for a given offset. * * @param mixed $offset * @return void */ public function offsetUnset($offset): void { unset($this->resource[$offset]); } /** * Determine if an attribute exists on the resource. * * @param string $key * @return bool */ public function __isset($key) { return isset($this->resource->{$key}); } /** * Unset an attribute on the resource. * * @param string $key * @return void */ public function __unset($key) { unset($this->resource->{$key}); } /** * Dynamically get properties from the underlying resource. * * @param string $key * @return mixed */ public function __get($key) { return $this->resource->{$key}; } /** * Dynamically pass method calls to the underlying resource. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } return $this->forwardCallTo($this->resource, $method, $parameters); } } src/Illuminate/Http/Resources/PotentiallyMissing.php 0000644 00000000331 15107327041 0016673 0 ustar 00 <?php namespace Illuminate\Http\Resources; interface PotentiallyMissing { /** * Determine if the object should be considered "missing". * * @return bool */ public function isMissing(); } src/Illuminate/Http/Resources/CollectsResources.php 0000644 00000005000 15107327041 0016476 0 ustar 00 <?php namespace Illuminate\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Pagination\AbstractCursorPaginator; use Illuminate\Pagination\AbstractPaginator; use Illuminate\Support\Collection; use Illuminate\Support\Str; use LogicException; use ReflectionClass; use Traversable; trait CollectsResources { /** * Map the given collection resource into its individual resources. * * @param mixed $resource * @return mixed */ protected function collectResource($resource) { if ($resource instanceof MissingValue) { return $resource; } if (is_array($resource)) { $resource = new Collection($resource); } $collects = $this->collects(); $this->collection = $collects && ! $resource->first() instanceof $collects ? $resource->mapInto($collects) : $resource->toBase(); return ($resource instanceof AbstractPaginator || $resource instanceof AbstractCursorPaginator) ? $resource->setCollection($this->collection) : $this->collection; } /** * Get the resource that this resource collects. * * @return string|null */ protected function collects() { $collects = null; if ($this->collects) { $collects = $this->collects; } elseif (str_ends_with(class_basename($this), 'Collection') && (class_exists($class = Str::replaceLast('Collection', '', get_class($this))) || class_exists($class = Str::replaceLast('Collection', 'Resource', get_class($this))))) { $collects = $class; } if (! $collects || is_a($collects, JsonResource::class, true)) { return $collects; } throw new LogicException('Resource collections must collect instances of '.JsonResource::class.'.'); } /** * Get the JSON serialization options that should be applied to the resource response. * * @return int */ public function jsonOptions() { $collects = $this->collects(); if (! $collects) { return 0; } return (new ReflectionClass($collects)) ->newInstanceWithoutConstructor() ->jsonOptions(); } /** * Get an iterator for the resource collection. * * @return \ArrayIterator */ public function getIterator(): Traversable { return $this->collection->getIterator(); } } src/Illuminate/Http/Resources/MergeValue.php 0000644 00000001243 15107327041 0015074 0 ustar 00 <?php namespace Illuminate\Http\Resources; use Illuminate\Support\Collection; use JsonSerializable; class MergeValue { /** * The data to be merged. * * @var array */ public $data; /** * Create a new merge value instance. * * @param \Illuminate\Support\Collection|\JsonSerializable|array $data * @return void */ public function __construct($data) { if ($data instanceof Collection) { $this->data = $data->all(); } elseif ($data instanceof JsonSerializable) { $this->data = $data->jsonSerialize(); } else { $this->data = $data; } } } src/Illuminate/Http/Resources/Json/ResourceCollection.php 0000644 00000006427 15107327041 0017565 0 ustar 00 <?php namespace Illuminate\Http\Resources\Json; use Countable; use Illuminate\Http\Request; use Illuminate\Http\Resources\CollectsResources; use Illuminate\Pagination\AbstractCursorPaginator; use Illuminate\Pagination\AbstractPaginator; use IteratorAggregate; class ResourceCollection extends JsonResource implements Countable, IteratorAggregate { use CollectsResources; /** * The resource that this resource collects. * * @var string */ public $collects; /** * The mapped collection instance. * * @var \Illuminate\Support\Collection */ public $collection; /** * Indicates if all existing request query parameters should be added to pagination links. * * @var bool */ protected $preserveAllQueryParameters = false; /** * The query parameters that should be added to the pagination links. * * @var array|null */ protected $queryParameters; /** * Create a new resource instance. * * @param mixed $resource * @return void */ public function __construct($resource) { parent::__construct($resource); $this->resource = $this->collectResource($resource); } /** * Indicate that all current query parameters should be appended to pagination links. * * @return $this */ public function preserveQuery() { $this->preserveAllQueryParameters = true; return $this; } /** * Specify the query string parameters that should be present on pagination links. * * @param array $query * @return $this */ public function withQuery(array $query) { $this->preserveAllQueryParameters = false; $this->queryParameters = $query; return $this; } /** * Return the count of items in the resource collection. * * @return int */ public function count(): int { return $this->collection->count(); } /** * Transform the resource into a JSON array. * * @param \Illuminate\Http\Request $request * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable */ public function toArray(Request $request) { return $this->collection->map->toArray($request)->all(); } /** * Create an HTTP response that represents the object. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function toResponse($request) { if ($this->resource instanceof AbstractPaginator || $this->resource instanceof AbstractCursorPaginator) { return $this->preparePaginatedResponse($request); } return parent::toResponse($request); } /** * Create a paginate-aware HTTP response. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ protected function preparePaginatedResponse($request) { if ($this->preserveAllQueryParameters) { $this->resource->appends($request->query()); } elseif (! is_null($this->queryParameters)) { $this->resource->appends($this->queryParameters); } return (new PaginatedResourceResponse($this))->toResponse($request); } } src/Illuminate/Http/Resources/Json/AnonymousResourceCollection.php 0000644 00000001234 15107327041 0021465 0 ustar 00 <?php namespace Illuminate\Http\Resources\Json; class AnonymousResourceCollection extends ResourceCollection { /** * The name of the resource being collected. * * @var string */ public $collects; /** * Indicates if the collection keys should be preserved. * * @var bool */ public $preserveKeys = false; /** * Create a new anonymous resource collection. * * @param mixed $resource * @param string $collects * @return void */ public function __construct($resource, $collects) { $this->collects = $collects; parent::__construct($resource); } } src/Illuminate/Http/Resources/Json/PaginatedResourceResponse.php 0000644 00000005107 15107327041 0021077 0 ustar 00 <?php namespace Illuminate\Http\Resources\Json; use Illuminate\Support\Arr; class PaginatedResourceResponse extends ResourceResponse { /** * Create an HTTP response that represents the object. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function toResponse($request) { return tap(response()->json( $this->wrap( $this->resource->resolve($request), array_merge_recursive( $this->paginationInformation($request), $this->resource->with($request), $this->resource->additional ) ), $this->calculateStatus(), [], $this->resource->jsonOptions() ), function ($response) use ($request) { $response->original = $this->resource->resource->map(function ($item) { return is_array($item) ? Arr::get($item, 'resource') : $item->resource; }); $this->resource->withResponse($request, $response); }); } /** * Add the pagination information to the response. * * @param \Illuminate\Http\Request $request * @return array */ protected function paginationInformation($request) { $paginated = $this->resource->resource->toArray(); $default = [ 'links' => $this->paginationLinks($paginated), 'meta' => $this->meta($paginated), ]; if (method_exists($this->resource, 'paginationInformation') || $this->resource->hasMacro('paginationInformation')) { return $this->resource->paginationInformation($request, $paginated, $default); } return $default; } /** * Get the pagination links for the response. * * @param array $paginated * @return array */ protected function paginationLinks($paginated) { return [ 'first' => $paginated['first_page_url'] ?? null, 'last' => $paginated['last_page_url'] ?? null, 'prev' => $paginated['prev_page_url'] ?? null, 'next' => $paginated['next_page_url'] ?? null, ]; } /** * Gather the meta data for the response. * * @param array $paginated * @return array */ protected function meta($paginated) { return Arr::except($paginated, [ 'data', 'first_page_url', 'last_page_url', 'prev_page_url', 'next_page_url', ]); } } src/Illuminate/Http/Resources/Json/JsonResource.php 0000644 00000014140 15107327041 0016372 0 ustar 00 <?php namespace Illuminate\Http\Resources\Json; use ArrayAccess; use Illuminate\Container\Container; use Illuminate\Contracts\Routing\UrlRoutable; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Responsable; use Illuminate\Database\Eloquent\JsonEncodingException; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Http\Resources\ConditionallyLoadsAttributes; use Illuminate\Http\Resources\DelegatesToResource; use JsonSerializable; class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRoutable { use ConditionallyLoadsAttributes, DelegatesToResource; /** * The resource instance. * * @var mixed */ public $resource; /** * The additional data that should be added to the top-level resource array. * * @var array */ public $with = []; /** * The additional meta data that should be added to the resource response. * * Added during response construction by the developer. * * @var array */ public $additional = []; /** * The "data" wrapper that should be applied. * * @var string|null */ public static $wrap = 'data'; /** * Create a new resource instance. * * @param mixed $resource * @return void */ public function __construct($resource) { $this->resource = $resource; } /** * Create a new resource instance. * * @param mixed ...$parameters * @return static */ public static function make(...$parameters) { return new static(...$parameters); } /** * Create a new anonymous resource collection. * * @param mixed $resource * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection */ public static function collection($resource) { return tap(static::newCollection($resource), function ($collection) { if (property_exists(static::class, 'preserveKeys')) { $collection->preserveKeys = (new static([]))->preserveKeys === true; } }); } /** * Create a new resource collection instance. * * @param mixed $resource * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection */ protected static function newCollection($resource) { return new AnonymousResourceCollection($resource, static::class); } /** * Resolve the resource to an array. * * @param \Illuminate\Http\Request|null $request * @return array */ public function resolve($request = null) { $data = $this->toArray( $request = $request ?: Container::getInstance()->make('request') ); if ($data instanceof Arrayable) { $data = $data->toArray(); } elseif ($data instanceof JsonSerializable) { $data = $data->jsonSerialize(); } return $this->filter((array) $data); } /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable */ public function toArray(Request $request) { if (is_null($this->resource)) { return []; } return is_array($this->resource) ? $this->resource : $this->resource->toArray(); } /** * Convert the model instance to JSON. * * @param int $options * @return string * * @throws \Illuminate\Database\Eloquent\JsonEncodingException */ public function toJson($options = 0) { $json = json_encode($this->jsonSerialize(), $options); if (json_last_error() !== JSON_ERROR_NONE) { throw JsonEncodingException::forResource($this, json_last_error_msg()); } return $json; } /** * Get any additional data that should be returned with the resource array. * * @param \Illuminate\Http\Request $request * @return array */ public function with(Request $request) { return $this->with; } /** * Add additional meta data to the resource response. * * @param array $data * @return $this */ public function additional(array $data) { $this->additional = $data; return $this; } /** * Get the JSON serialization options that should be applied to the resource response. * * @return int */ public function jsonOptions() { return 0; } /** * Customize the response for a request. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Http\JsonResponse $response * @return void */ public function withResponse(Request $request, JsonResponse $response) { // } /** * Set the string that should wrap the outer-most resource array. * * @param string $value * @return void */ public static function wrap($value) { static::$wrap = $value; } /** * Disable wrapping of the outer-most resource array. * * @return void */ public static function withoutWrapping() { static::$wrap = null; } /** * Transform the resource into an HTTP response. * * @param \Illuminate\Http\Request|null $request * @return \Illuminate\Http\JsonResponse */ public function response($request = null) { return $this->toResponse( $request ?: Container::getInstance()->make('request') ); } /** * Create an HTTP response that represents the object. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function toResponse($request) { return (new ResourceResponse($this))->toResponse($request); } /** * Prepare the resource for JSON serialization. * * @return array */ public function jsonSerialize(): array { return $this->resolve(Container::getInstance()->make('request')); } } src/Illuminate/Http/Resources/Json/ResourceResponse.php 0000644 00000006270 15107327041 0017264 0 ustar 00 <?php namespace Illuminate\Http\Resources\Json; use Illuminate\Contracts\Support\Responsable; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; class ResourceResponse implements Responsable { /** * The underlying resource. * * @var mixed */ public $resource; /** * Create a new resource response. * * @param mixed $resource * @return void */ public function __construct($resource) { $this->resource = $resource; } /** * Create an HTTP response that represents the object. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function toResponse($request) { return tap(response()->json( $this->wrap( $this->resource->resolve($request), $this->resource->with($request), $this->resource->additional ), $this->calculateStatus(), [], $this->resource->jsonOptions() ), function ($response) use ($request) { $response->original = $this->resource->resource; $this->resource->withResponse($request, $response); }); } /** * Wrap the given data if necessary. * * @param \Illuminate\Support\Collection|array $data * @param array $with * @param array $additional * @return array */ protected function wrap($data, $with = [], $additional = []) { if ($data instanceof Collection) { $data = $data->all(); } if ($this->haveDefaultWrapperAndDataIsUnwrapped($data)) { $data = [$this->wrapper() => $data]; } elseif ($this->haveAdditionalInformationAndDataIsUnwrapped($data, $with, $additional)) { $data = [($this->wrapper() ?? 'data') => $data]; } return array_merge_recursive($data, $with, $additional); } /** * Determine if we have a default wrapper and the given data is unwrapped. * * @param array $data * @return bool */ protected function haveDefaultWrapperAndDataIsUnwrapped($data) { return $this->wrapper() && ! array_key_exists($this->wrapper(), $data); } /** * Determine if "with" data has been added and our data is unwrapped. * * @param array $data * @param array $with * @param array $additional * @return bool */ protected function haveAdditionalInformationAndDataIsUnwrapped($data, $with, $additional) { return (! empty($with) || ! empty($additional)) && (! $this->wrapper() || ! array_key_exists($this->wrapper(), $data)); } /** * Get the default data wrapper for the resource. * * @return string */ protected function wrapper() { return get_class($this->resource)::$wrap; } /** * Calculate the appropriate status code for the response. * * @return int */ protected function calculateStatus() { return $this->resource->resource instanceof Model && $this->resource->resource->wasRecentlyCreated ? 201 : 200; } } src/Illuminate/Http/Resources/MissingValue.php 0000644 00000000415 15107327041 0015446 0 ustar 00 <?php namespace Illuminate\Http\Resources; class MissingValue implements PotentiallyMissing { /** * Determine if the object should be considered "missing". * * @return bool */ public function isMissing() { return true; } } src/Illuminate/Http/FileHelpers.php 0000644 00000002264 15107327041 0013274 0 ustar 00 <?php namespace Illuminate\Http; use Illuminate\Support\Str; trait FileHelpers { /** * The cache copy of the file's hash name. * * @var string */ protected $hashName = null; /** * Get the fully qualified path to the file. * * @return string */ public function path() { return $this->getRealPath(); } /** * Get the file's extension. * * @return string */ public function extension() { return $this->guessExtension(); } /** * Get a filename for the file. * * @param string|null $path * @return string */ public function hashName($path = null) { if ($path) { $path = rtrim($path, '/').'/'; } $hash = $this->hashName ?: $this->hashName = Str::random(40); if ($extension = $this->guessExtension()) { $extension = '.'.$extension; } return $path.$hash.$extension; } /** * Get the dimensions of the image (if applicable). * * @return array|null */ public function dimensions() { return @getimagesize($this->getRealPath()); } } src/Illuminate/Http/Concerns/InteractsWithInput.php 0000644 00000035356 15107327041 0016464 0 ustar 00 <?php namespace Illuminate\Http\Concerns; use Illuminate\Http\UploadedFile; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Date; use SplFileInfo; use stdClass; use Symfony\Component\HttpFoundation\InputBag; use Symfony\Component\VarDumper\VarDumper; trait InteractsWithInput { /** * Retrieve a server variable from the request. * * @param string|null $key * @param string|array|null $default * @return string|array|null */ public function server($key = null, $default = null) { return $this->retrieveItem('server', $key, $default); } /** * Determine if a header is set on the request. * * @param string $key * @return bool */ public function hasHeader($key) { return ! is_null($this->header($key)); } /** * Retrieve a header from the request. * * @param string|null $key * @param string|array|null $default * @return string|array|null */ public function header($key = null, $default = null) { return $this->retrieveItem('headers', $key, $default); } /** * Get the bearer token from the request headers. * * @return string|null */ public function bearerToken() { $header = $this->header('Authorization', ''); $position = strrpos($header, 'Bearer '); if ($position !== false) { $header = substr($header, $position + 7); return str_contains($header, ',') ? strstr($header, ',', true) : $header; } } /** * Determine if the request contains a given input item key. * * @param string|array $key * @return bool */ public function exists($key) { return $this->has($key); } /** * Determine if the request contains a given input item key. * * @param string|array $key * @return bool */ public function has($key) { $keys = is_array($key) ? $key : func_get_args(); $input = $this->all(); foreach ($keys as $value) { if (! Arr::has($input, $value)) { return false; } } return true; } /** * Determine if the request contains any of the given inputs. * * @param string|array $keys * @return bool */ public function hasAny($keys) { $keys = is_array($keys) ? $keys : func_get_args(); $input = $this->all(); return Arr::hasAny($input, $keys); } /** * Apply the callback if the request contains the given input item key. * * @param string $key * @param callable $callback * @param callable|null $default * @return $this|mixed */ public function whenHas($key, callable $callback, callable $default = null) { if ($this->has($key)) { return $callback(data_get($this->all(), $key)) ?: $this; } if ($default) { return $default(); } return $this; } /** * Determine if the request contains a non-empty value for an input item. * * @param string|array $key * @return bool */ public function filled($key) { $keys = is_array($key) ? $key : func_get_args(); foreach ($keys as $value) { if ($this->isEmptyString($value)) { return false; } } return true; } /** * Determine if the request contains an empty value for an input item. * * @param string|array $key * @return bool */ public function isNotFilled($key) { $keys = is_array($key) ? $key : func_get_args(); foreach ($keys as $value) { if (! $this->isEmptyString($value)) { return false; } } return true; } /** * Determine if the request contains a non-empty value for any of the given inputs. * * @param string|array $keys * @return bool */ public function anyFilled($keys) { $keys = is_array($keys) ? $keys : func_get_args(); foreach ($keys as $key) { if ($this->filled($key)) { return true; } } return false; } /** * Apply the callback if the request contains a non-empty value for the given input item key. * * @param string $key * @param callable $callback * @param callable|null $default * @return $this|mixed */ public function whenFilled($key, callable $callback, callable $default = null) { if ($this->filled($key)) { return $callback(data_get($this->all(), $key)) ?: $this; } if ($default) { return $default(); } return $this; } /** * Determine if the request is missing a given input item key. * * @param string|array $key * @return bool */ public function missing($key) { $keys = is_array($key) ? $key : func_get_args(); return ! $this->has($keys); } /** * Apply the callback if the request is missing the given input item key. * * @param string $key * @param callable $callback * @param callable|null $default * @return $this|mixed */ public function whenMissing($key, callable $callback, callable $default = null) { if ($this->missing($key)) { return $callback(data_get($this->all(), $key)) ?: $this; } if ($default) { return $default(); } return $this; } /** * Determine if the given input key is an empty string for "filled". * * @param string $key * @return bool */ protected function isEmptyString($key) { $value = $this->input($key); return ! is_bool($value) && ! is_array($value) && trim((string) $value) === ''; } /** * Get the keys for all of the input and files. * * @return array */ public function keys() { return array_merge(array_keys($this->input()), $this->files->keys()); } /** * Get all of the input and files for the request. * * @param array|mixed|null $keys * @return array */ public function all($keys = null) { $input = array_replace_recursive($this->input(), $this->allFiles()); if (! $keys) { return $input; } $results = []; foreach (is_array($keys) ? $keys : func_get_args() as $key) { Arr::set($results, $key, Arr::get($input, $key)); } return $results; } /** * Retrieve an input item from the request. * * @param string|null $key * @param mixed $default * @return mixed */ public function input($key = null, $default = null) { return data_get( $this->getInputSource()->all() + $this->query->all(), $key, $default ); } /** * Retrieve input from the request as a Stringable instance. * * @param string $key * @param mixed $default * @return \Illuminate\Support\Stringable */ public function str($key, $default = null) { return $this->string($key, $default); } /** * Retrieve input from the request as a Stringable instance. * * @param string $key * @param mixed $default * @return \Illuminate\Support\Stringable */ public function string($key, $default = null) { return str($this->input($key, $default)); } /** * Retrieve input as a boolean value. * * Returns true when value is "1", "true", "on", and "yes". Otherwise, returns false. * * @param string|null $key * @param bool $default * @return bool */ public function boolean($key = null, $default = false) { return filter_var($this->input($key, $default), FILTER_VALIDATE_BOOLEAN); } /** * Retrieve input as an integer value. * * @param string $key * @param int $default * @return int */ public function integer($key, $default = 0) { return intval($this->input($key, $default)); } /** * Retrieve input as a float value. * * @param string $key * @param float $default * @return float */ public function float($key, $default = 0.0) { return floatval($this->input($key, $default)); } /** * Retrieve input from the request as a Carbon instance. * * @param string $key * @param string|null $format * @param string|null $tz * @return \Illuminate\Support\Carbon|null * * @throws \Carbon\Exceptions\InvalidFormatException */ public function date($key, $format = null, $tz = null) { if ($this->isNotFilled($key)) { return null; } if (is_null($format)) { return Date::parse($this->input($key), $tz); } return Date::createFromFormat($format, $this->input($key), $tz); } /** * Retrieve input from the request as an enum. * * @template TEnum * * @param string $key * @param class-string<TEnum> $enumClass * @return TEnum|null */ public function enum($key, $enumClass) { if ($this->isNotFilled($key) || ! enum_exists($enumClass) || ! method_exists($enumClass, 'tryFrom')) { return null; } return $enumClass::tryFrom($this->input($key)); } /** * Retrieve input from the request as a collection. * * @param array|string|null $key * @return \Illuminate\Support\Collection */ public function collect($key = null) { return collect(is_array($key) ? $this->only($key) : $this->input($key)); } /** * Get a subset containing the provided keys with values from the input data. * * @param array|mixed $keys * @return array */ public function only($keys) { $results = []; $input = $this->all(); $placeholder = new stdClass; foreach (is_array($keys) ? $keys : func_get_args() as $key) { $value = data_get($input, $key, $placeholder); if ($value !== $placeholder) { Arr::set($results, $key, $value); } } return $results; } /** * Get all of the input except for a specified array of items. * * @param array|mixed $keys * @return array */ public function except($keys) { $keys = is_array($keys) ? $keys : func_get_args(); $results = $this->all(); Arr::forget($results, $keys); return $results; } /** * Retrieve a query string item from the request. * * @param string|null $key * @param string|array|null $default * @return string|array|null */ public function query($key = null, $default = null) { return $this->retrieveItem('query', $key, $default); } /** * Retrieve a request payload item from the request. * * @param string|null $key * @param string|array|null $default * @return string|array|null */ public function post($key = null, $default = null) { return $this->retrieveItem('request', $key, $default); } /** * Determine if a cookie is set on the request. * * @param string $key * @return bool */ public function hasCookie($key) { return ! is_null($this->cookie($key)); } /** * Retrieve a cookie from the request. * * @param string|null $key * @param string|array|null $default * @return string|array|null */ public function cookie($key = null, $default = null) { return $this->retrieveItem('cookies', $key, $default); } /** * Get an array of all of the files on the request. * * @return array */ public function allFiles() { $files = $this->files->all(); return $this->convertedFiles = $this->convertedFiles ?? $this->convertUploadedFiles($files); } /** * Convert the given array of Symfony UploadedFiles to custom Laravel UploadedFiles. * * @param array $files * @return array */ protected function convertUploadedFiles(array $files) { return array_map(function ($file) { if (is_null($file) || (is_array($file) && empty(array_filter($file)))) { return $file; } return is_array($file) ? $this->convertUploadedFiles($file) : UploadedFile::createFromBase($file); }, $files); } /** * Determine if the uploaded data contains a file. * * @param string $key * @return bool */ public function hasFile($key) { if (! is_array($files = $this->file($key))) { $files = [$files]; } foreach ($files as $file) { if ($this->isValidFile($file)) { return true; } } return false; } /** * Check that the given file is a valid file instance. * * @param mixed $file * @return bool */ protected function isValidFile($file) { return $file instanceof SplFileInfo && $file->getPath() !== ''; } /** * Retrieve a file from the request. * * @param string|null $key * @param mixed $default * @return \Illuminate\Http\UploadedFile|\Illuminate\Http\UploadedFile[]|array|null */ public function file($key = null, $default = null) { return data_get($this->allFiles(), $key, $default); } /** * Retrieve a parameter item from a given source. * * @param string $source * @param string|null $key * @param string|array|null $default * @return string|array|null */ protected function retrieveItem($source, $key, $default) { if (is_null($key)) { return $this->$source->all(); } if ($this->$source instanceof InputBag) { return $this->$source->all()[$key] ?? $default; } return $this->$source->get($key, $default); } /** * Dump the request items and end the script. * * @param mixed ...$keys * @return never */ public function dd(...$keys) { $this->dump(...$keys); exit(1); } /** * Dump the items. * * @param mixed $keys * @return $this */ public function dump($keys = []) { $keys = is_array($keys) ? $keys : func_get_args(); VarDumper::dump(count($keys) > 0 ? $this->only($keys) : $this->all()); return $this; } } src/Illuminate/Http/Concerns/CanBePrecognitive.php 0000644 00000002000 15107327041 0016157 0 ustar 00 <?php namespace Illuminate\Http\Concerns; use Illuminate\Support\Collection; trait CanBePrecognitive { /** * Filter the given array of rules into an array of rules that are included in precognitive headers. * * @param array $rules * @return array */ public function filterPrecognitiveRules($rules) { if (! $this->headers->has('Precognition-Validate-Only')) { return $rules; } return Collection::make($rules) ->only(explode(',', $this->header('Precognition-Validate-Only'))) ->all(); } /** * Determine if the request is attempting to be precognitive. * * @return bool */ public function isAttemptingPrecognition() { return $this->header('Precognition') === 'true'; } /** * Determine if the request is precognitive. * * @return bool */ public function isPrecognitive() { return $this->attributes->get('precognitive', false); } } src/Illuminate/Http/Concerns/InteractsWithContentTypes.php 0000644 00000010403 15107327041 0020006 0 ustar 00 <?php namespace Illuminate\Http\Concerns; use Illuminate\Support\Str; trait InteractsWithContentTypes { /** * Determine if the request is sending JSON. * * @return bool */ public function isJson() { return Str::contains($this->header('CONTENT_TYPE') ?? '', ['/json', '+json']); } /** * Determine if the current request probably expects a JSON response. * * @return bool */ public function expectsJson() { return ($this->ajax() && ! $this->pjax() && $this->acceptsAnyContentType()) || $this->wantsJson(); } /** * Determine if the current request is asking for JSON. * * @return bool */ public function wantsJson() { $acceptable = $this->getAcceptableContentTypes(); return isset($acceptable[0]) && Str::contains(strtolower($acceptable[0]), ['/json', '+json']); } /** * Determines whether the current requests accepts a given content type. * * @param string|array $contentTypes * @return bool */ public function accepts($contentTypes) { $accepts = $this->getAcceptableContentTypes(); if (count($accepts) === 0) { return true; } $types = (array) $contentTypes; foreach ($accepts as $accept) { if ($accept === '*/*' || $accept === '*') { return true; } foreach ($types as $type) { $accept = strtolower($accept); $type = strtolower($type); if ($this->matchesType($accept, $type) || $accept === strtok($type, '/').'/*') { return true; } } } return false; } /** * Return the most suitable content type from the given array based on content negotiation. * * @param string|array $contentTypes * @return string|null */ public function prefers($contentTypes) { $accepts = $this->getAcceptableContentTypes(); $contentTypes = (array) $contentTypes; foreach ($accepts as $accept) { if (in_array($accept, ['*/*', '*'])) { return $contentTypes[0]; } foreach ($contentTypes as $contentType) { $type = $contentType; if (! is_null($mimeType = $this->getMimeType($contentType))) { $type = $mimeType; } $accept = strtolower($accept); $type = strtolower($type); if ($this->matchesType($type, $accept) || $accept === strtok($type, '/').'/*') { return $contentType; } } } } /** * Determine if the current request accepts any content type. * * @return bool */ public function acceptsAnyContentType() { $acceptable = $this->getAcceptableContentTypes(); return count($acceptable) === 0 || ( isset($acceptable[0]) && ($acceptable[0] === '*/*' || $acceptable[0] === '*') ); } /** * Determines whether a request accepts JSON. * * @return bool */ public function acceptsJson() { return $this->accepts('application/json'); } /** * Determines whether a request accepts HTML. * * @return bool */ public function acceptsHtml() { return $this->accepts('text/html'); } /** * Determine if the given content types match. * * @param string $actual * @param string $type * @return bool */ public static function matchesType($actual, $type) { if ($actual === $type) { return true; } $split = explode('/', $actual); return isset($split[1]) && preg_match('#'.preg_quote($split[0], '#').'/.+\+'.preg_quote($split[1], '#').'#', $type); } /** * Get the data format expected in the response. * * @param string $default * @return string */ public function format($default = 'html') { foreach ($this->getAcceptableContentTypes() as $type) { if ($format = $this->getFormat($type)) { return $format; } } return $default; } } src/Illuminate/Http/Concerns/InteractsWithFlashData.php 0000644 00000003010 15107327041 0017172 0 ustar 00 <?php namespace Illuminate\Http\Concerns; use Illuminate\Database\Eloquent\Model; trait InteractsWithFlashData { /** * Retrieve an old input item. * * @param string|null $key * @param \Illuminate\Database\Eloquent\Model|string|array|null $default * @return string|array|null */ public function old($key = null, $default = null) { $default = $default instanceof Model ? $default->getAttribute($key) : $default; return $this->hasSession() ? $this->session()->getOldInput($key, $default) : $default; } /** * Flash the input for the current request to the session. * * @return void */ public function flash() { $this->session()->flashInput($this->input()); } /** * Flash only some of the input to the session. * * @param array|mixed $keys * @return void */ public function flashOnly($keys) { $this->session()->flashInput( $this->only(is_array($keys) ? $keys : func_get_args()) ); } /** * Flash only some of the input to the session. * * @param array|mixed $keys * @return void */ public function flashExcept($keys) { $this->session()->flashInput( $this->except(is_array($keys) ? $keys : func_get_args()) ); } /** * Flush all of the old input from the session. * * @return void */ public function flush() { $this->session()->flashInput([]); } } src/Illuminate/Http/File.php 0000644 00000000233 15107327041 0011743 0 ustar 00 <?php namespace Illuminate\Http; use Symfony\Component\HttpFoundation\File\File as SymfonyFile; class File extends SymfonyFile { use FileHelpers; } src/Illuminate/Http/RedirectResponse.php 0000644 00000013502 15107327041 0014347 0 ustar 00 <?php namespace Illuminate\Http; use Illuminate\Contracts\Support\MessageProvider; use Illuminate\Session\Store as SessionStore; use Illuminate\Support\MessageBag; use Illuminate\Support\Str; use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\Macroable; use Illuminate\Support\ViewErrorBag; use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile; use Symfony\Component\HttpFoundation\RedirectResponse as BaseRedirectResponse; class RedirectResponse extends BaseRedirectResponse { use ForwardsCalls, ResponseTrait, Macroable { Macroable::__call as macroCall; } /** * The request instance. * * @var \Illuminate\Http\Request */ protected $request; /** * The session store instance. * * @var \Illuminate\Session\Store */ protected $session; /** * Flash a piece of data to the session. * * @param string|array $key * @param mixed $value * @return $this */ public function with($key, $value = null) { $key = is_array($key) ? $key : [$key => $value]; foreach ($key as $k => $v) { $this->session->flash($k, $v); } return $this; } /** * Add multiple cookies to the response. * * @param array $cookies * @return $this */ public function withCookies(array $cookies) { foreach ($cookies as $cookie) { $this->headers->setCookie($cookie); } return $this; } /** * Flash an array of input to the session. * * @param array|null $input * @return $this */ public function withInput(array $input = null) { $this->session->flashInput($this->removeFilesFromInput( ! is_null($input) ? $input : $this->request->input() )); return $this; } /** * Remove all uploaded files form the given input array. * * @param array $input * @return array */ protected function removeFilesFromInput(array $input) { foreach ($input as $key => $value) { if (is_array($value)) { $input[$key] = $this->removeFilesFromInput($value); } if ($value instanceof SymfonyUploadedFile) { unset($input[$key]); } } return $input; } /** * Flash an array of input to the session. * * @return $this */ public function onlyInput() { return $this->withInput($this->request->only(func_get_args())); } /** * Flash an array of input to the session. * * @return $this */ public function exceptInput() { return $this->withInput($this->request->except(func_get_args())); } /** * Flash a container of errors to the session. * * @param \Illuminate\Contracts\Support\MessageProvider|array|string $provider * @param string $key * @return $this */ public function withErrors($provider, $key = 'default') { $value = $this->parseErrors($provider); $errors = $this->session->get('errors', new ViewErrorBag); if (! $errors instanceof ViewErrorBag) { $errors = new ViewErrorBag; } $this->session->flash( 'errors', $errors->put($key, $value) ); return $this; } /** * Parse the given errors into an appropriate value. * * @param \Illuminate\Contracts\Support\MessageProvider|array|string $provider * @return \Illuminate\Support\MessageBag */ protected function parseErrors($provider) { if ($provider instanceof MessageProvider) { return $provider->getMessageBag(); } return new MessageBag((array) $provider); } /** * Add a fragment identifier to the URL. * * @param string $fragment * @return $this */ public function withFragment($fragment) { return $this->withoutFragment() ->setTargetUrl($this->getTargetUrl().'#'.Str::after($fragment, '#')); } /** * Remove any fragment identifier from the response URL. * * @return $this */ public function withoutFragment() { return $this->setTargetUrl(Str::before($this->getTargetUrl(), '#')); } /** * Get the original response content. * * @return null */ public function getOriginalContent() { // } /** * Get the request instance. * * @return \Illuminate\Http\Request|null */ public function getRequest() { return $this->request; } /** * Set the request instance. * * @param \Illuminate\Http\Request $request * @return void */ public function setRequest(Request $request) { $this->request = $request; } /** * Get the session store instance. * * @return \Illuminate\Session\Store|null */ public function getSession() { return $this->session; } /** * Set the session store instance. * * @param \Illuminate\Session\Store $session * @return void */ public function setSession(SessionStore $session) { $this->session = $session; } /** * Dynamically bind flash data in the session. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } if (str_starts_with($method, 'with')) { return $this->with(Str::snake(substr($method, 4)), $parameters[0]); } static::throwBadMethodCallException($method); } } src/Illuminate/Http/composer.json 0000644 00000002421 15107327041 0013076 0 ustar 00 { "name": "illuminate/http", "description": "The Illuminate Http package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "ext-filter": "*", "fruitcake/php-cors": "^1.2", "guzzlehttp/uri-template": "^1.0", "illuminate/collections": "^10.0", "illuminate/macroable": "^10.0", "illuminate/session": "^10.0", "illuminate/support": "^10.0", "symfony/http-foundation": "^6.2", "symfony/http-kernel": "^6.2", "symfony/mime": "^6.2" }, "autoload": { "psr-4": { "Illuminate\\Http\\": "" } }, "suggest": { "ext-gd": "Required to use Illuminate\\Http\\Testing\\FileFactory::image().", "guzzlehttp/guzzle": "Required to use the HTTP Client (^7.5)." }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Http/Request.php 0000644 00000043333 15107327041 0012524 0 ustar 00 <?php namespace Illuminate\Http; use ArrayAccess; use Closure; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Session\SymfonySessionDecorator; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use RuntimeException; use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException; use Symfony\Component\HttpFoundation\InputBag; use Symfony\Component\HttpFoundation\Request as SymfonyRequest; use Symfony\Component\HttpFoundation\Session\SessionInterface; /** * @method array validate(array $rules, ...$params) * @method array validateWithBag(string $errorBag, array $rules, ...$params) * @method bool hasValidSignature(bool $absolute = true) */ class Request extends SymfonyRequest implements Arrayable, ArrayAccess { use Concerns\CanBePrecognitive, Concerns\InteractsWithContentTypes, Concerns\InteractsWithFlashData, Concerns\InteractsWithInput, Macroable; /** * The decoded JSON content for the request. * * @var \Symfony\Component\HttpFoundation\InputBag|null */ protected $json; /** * All of the converted files for the request. * * @var array */ protected $convertedFiles; /** * The user resolver callback. * * @var \Closure */ protected $userResolver; /** * The route resolver callback. * * @var \Closure */ protected $routeResolver; /** * Create a new Illuminate HTTP request from server variables. * * @return static */ public static function capture() { static::enableHttpMethodParameterOverride(); return static::createFromBase(SymfonyRequest::createFromGlobals()); } /** * Return the Request instance. * * @return $this */ public function instance() { return $this; } /** * Get the request method. * * @return string */ public function method() { return $this->getMethod(); } /** * Get the root URL for the application. * * @return string */ public function root() { return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(), '/'); } /** * Get the URL (no query string) for the request. * * @return string */ public function url() { return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/'); } /** * Get the full URL for the request. * * @return string */ public function fullUrl() { $query = $this->getQueryString(); $question = $this->getBaseUrl().$this->getPathInfo() === '/' ? '/?' : '?'; return $query ? $this->url().$question.$query : $this->url(); } /** * Get the full URL for the request with the added query string parameters. * * @param array $query * @return string */ public function fullUrlWithQuery(array $query) { $question = $this->getBaseUrl().$this->getPathInfo() === '/' ? '/?' : '?'; return count($this->query()) > 0 ? $this->url().$question.Arr::query(array_merge($this->query(), $query)) : $this->fullUrl().$question.Arr::query($query); } /** * Get the full URL for the request without the given query string parameters. * * @param array|string $keys * @return string */ public function fullUrlWithoutQuery($keys) { $query = Arr::except($this->query(), $keys); $question = $this->getBaseUrl().$this->getPathInfo() === '/' ? '/?' : '?'; return count($query) > 0 ? $this->url().$question.Arr::query($query) : $this->url(); } /** * Get the current path info for the request. * * @return string */ public function path() { $pattern = trim($this->getPathInfo(), '/'); return $pattern === '' ? '/' : $pattern; } /** * Get the current decoded path info for the request. * * @return string */ public function decodedPath() { return rawurldecode($this->path()); } /** * Get a segment from the URI (1 based index). * * @param int $index * @param string|null $default * @return string|null */ public function segment($index, $default = null) { return Arr::get($this->segments(), $index - 1, $default); } /** * Get all of the segments for the request path. * * @return array */ public function segments() { $segments = explode('/', $this->decodedPath()); return array_values(array_filter($segments, function ($value) { return $value !== ''; })); } /** * Determine if the current request URI matches a pattern. * * @param mixed ...$patterns * @return bool */ public function is(...$patterns) { $path = $this->decodedPath(); return collect($patterns)->contains(fn ($pattern) => Str::is($pattern, $path)); } /** * Determine if the route name matches a given pattern. * * @param mixed ...$patterns * @return bool */ public function routeIs(...$patterns) { return $this->route() && $this->route()->named(...$patterns); } /** * Determine if the current request URL and query string match a pattern. * * @param mixed ...$patterns * @return bool */ public function fullUrlIs(...$patterns) { $url = $this->fullUrl(); return collect($patterns)->contains(fn ($pattern) => Str::is($pattern, $url)); } /** * Get the host name. * * @return string */ public function host() { return $this->getHost(); } /** * Get the HTTP host being requested. * * @return string */ public function httpHost() { return $this->getHttpHost(); } /** * Get the scheme and HTTP host. * * @return string */ public function schemeAndHttpHost() { return $this->getSchemeAndHttpHost(); } /** * Determine if the request is the result of an AJAX call. * * @return bool */ public function ajax() { return $this->isXmlHttpRequest(); } /** * Determine if the request is the result of a PJAX call. * * @return bool */ public function pjax() { return $this->headers->get('X-PJAX') == true; } /** * Determine if the request is the result of a prefetch call. * * @return bool */ public function prefetch() { return strcasecmp($this->server->get('HTTP_X_MOZ') ?? '', 'prefetch') === 0 || strcasecmp($this->headers->get('Purpose') ?? '', 'prefetch') === 0; } /** * Determine if the request is over HTTPS. * * @return bool */ public function secure() { return $this->isSecure(); } /** * Get the client IP address. * * @return string|null */ public function ip() { return $this->getClientIp(); } /** * Get the client IP addresses. * * @return array */ public function ips() { return $this->getClientIps(); } /** * Get the client user agent. * * @return string|null */ public function userAgent() { return $this->headers->get('User-Agent'); } /** * Merge new input into the current request's input array. * * @param array $input * @return $this */ public function merge(array $input) { $this->getInputSource()->add($input); return $this; } /** * Merge new input into the request's input, but only when that key is missing from the request. * * @param array $input * @return $this */ public function mergeIfMissing(array $input) { return $this->merge(collect($input)->filter(function ($value, $key) { return $this->missing($key); })->toArray()); } /** * Replace the input for the current request. * * @param array $input * @return $this */ public function replace(array $input) { $this->getInputSource()->replace($input); return $this; } /** * This method belongs to Symfony HttpFoundation and is not usually needed when using Laravel. * * Instead, you may use the "input" method. * * @param string $key * @param mixed $default * @return mixed */ public function get(string $key, mixed $default = null): mixed { return parent::get($key, $default); } /** * Get the JSON payload for the request. * * @param string|null $key * @param mixed $default * @return \Symfony\Component\HttpFoundation\InputBag|mixed */ public function json($key = null, $default = null) { if (! isset($this->json)) { $this->json = new InputBag((array) json_decode($this->getContent(), true)); } if (is_null($key)) { return $this->json; } return data_get($this->json->all(), $key, $default); } /** * Get the input source for the request. * * @return \Symfony\Component\HttpFoundation\InputBag */ protected function getInputSource() { if ($this->isJson()) { return $this->json(); } return in_array($this->getRealMethod(), ['GET', 'HEAD']) ? $this->query : $this->request; } /** * Create a new request instance from the given Laravel request. * * @param \Illuminate\Http\Request $from * @param \Illuminate\Http\Request|null $to * @return static */ public static function createFrom(self $from, $to = null) { $request = $to ?: new static; $files = array_filter($from->files->all()); $request->initialize( $from->query->all(), $from->request->all(), $from->attributes->all(), $from->cookies->all(), $files, $from->server->all(), $from->getContent() ); $request->headers->replace($from->headers->all()); $request->setRequestLocale($from->getLocale()); $request->setDefaultRequestLocale($from->getDefaultLocale()); $request->setJson($from->json()); if ($from->hasSession() && $session = $from->session()) { $request->setLaravelSession($session); } $request->setUserResolver($from->getUserResolver()); $request->setRouteResolver($from->getRouteResolver()); return $request; } /** * Create an Illuminate request from a Symfony instance. * * @param \Symfony\Component\HttpFoundation\Request $request * @return static */ public static function createFromBase(SymfonyRequest $request) { $newRequest = new static( $request->query->all(), $request->request->all(), $request->attributes->all(), $request->cookies->all(), (new static)->filterFiles($request->files->all()) ?? [], $request->server->all() ); $newRequest->headers->replace($request->headers->all()); $newRequest->content = $request->content; if ($newRequest->isJson()) { $newRequest->request = $newRequest->json(); } return $newRequest; } /** * {@inheritdoc} * * @return static */ public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null): static { return parent::duplicate($query, $request, $attributes, $cookies, $this->filterFiles($files), $server); } /** * Filter the given array of files, removing any empty values. * * @param mixed $files * @return mixed */ protected function filterFiles($files) { if (! $files) { return; } foreach ($files as $key => $file) { if (is_array($file)) { $files[$key] = $this->filterFiles($files[$key]); } if (empty($files[$key])) { unset($files[$key]); } } return $files; } /** * {@inheritdoc} */ public function hasSession(bool $skipIfUninitialized = false): bool { return ! is_null($this->session); } /** * {@inheritdoc} */ public function getSession(): SessionInterface { return $this->hasSession() ? new SymfonySessionDecorator($this->session()) : throw new SessionNotFoundException; } /** * Get the session associated with the request. * * @return \Illuminate\Contracts\Session\Session * * @throws \RuntimeException */ public function session() { if (! $this->hasSession()) { throw new RuntimeException('Session store not set on request.'); } return $this->session; } /** * Set the session instance on the request. * * @param \Illuminate\Contracts\Session\Session $session * @return void */ public function setLaravelSession($session) { $this->session = $session; } /** * Set the locale for the request instance. * * @param string $locale * @return void */ public function setRequestLocale(string $locale) { $this->locale = $locale; } /** * Set the default locale for the request instance. * * @param string $locale * @return void */ public function setDefaultRequestLocale(string $locale) { $this->defaultLocale = $locale; } /** * Get the user making the request. * * @param string|null $guard * @return mixed */ public function user($guard = null) { return call_user_func($this->getUserResolver(), $guard); } /** * Get the route handling the request. * * @param string|null $param * @param mixed $default * @return \Illuminate\Routing\Route|object|string|null */ public function route($param = null, $default = null) { $route = call_user_func($this->getRouteResolver()); if (is_null($route) || is_null($param)) { return $route; } return $route->parameter($param, $default); } /** * Get a unique fingerprint for the request / route / IP address. * * @return string * * @throws \RuntimeException */ public function fingerprint() { if (! $route = $this->route()) { throw new RuntimeException('Unable to generate fingerprint. Route unavailable.'); } return sha1(implode('|', array_merge( $route->methods(), [$route->getDomain(), $route->uri(), $this->ip()] ))); } /** * Set the JSON payload for the request. * * @param \Symfony\Component\HttpFoundation\InputBag $json * @return $this */ public function setJson($json) { $this->json = $json; return $this; } /** * Get the user resolver callback. * * @return \Closure */ public function getUserResolver() { return $this->userResolver ?: function () { // }; } /** * Set the user resolver callback. * * @param \Closure $callback * @return $this */ public function setUserResolver(Closure $callback) { $this->userResolver = $callback; return $this; } /** * Get the route resolver callback. * * @return \Closure */ public function getRouteResolver() { return $this->routeResolver ?: function () { // }; } /** * Set the route resolver callback. * * @param \Closure $callback * @return $this */ public function setRouteResolver(Closure $callback) { $this->routeResolver = $callback; return $this; } /** * Get all of the input and files for the request. * * @return array */ public function toArray(): array { return $this->all(); } /** * Determine if the given offset exists. * * @param string $offset * @return bool */ public function offsetExists($offset): bool { $route = $this->route(); return Arr::has( $this->all() + ($route ? $route->parameters() : []), $offset ); } /** * Get the value at the given offset. * * @param string $offset * @return mixed */ public function offsetGet($offset): mixed { return $this->__get($offset); } /** * Set the value at the given offset. * * @param string $offset * @param mixed $value * @return void */ public function offsetSet($offset, $value): void { $this->getInputSource()->set($offset, $value); } /** * Remove the value at the given offset. * * @param string $offset * @return void */ public function offsetUnset($offset): void { $this->getInputSource()->remove($offset); } /** * Check if an input element is set on the request. * * @param string $key * @return bool */ public function __isset($key) { return ! is_null($this->__get($key)); } /** * Get an input element from the request. * * @param string $key * @return mixed */ public function __get($key) { return Arr::get($this->all(), $key, fn () => $this->route($key)); } } src/Illuminate/Http/Client/Pool.php 0000644 00000003413 15107327041 0013216 0 ustar 00 <?php namespace Illuminate\Http\Client; use GuzzleHttp\Utils; /** * @mixin \Illuminate\Http\Client\Factory */ class Pool { /** * The factory instance. * * @var \Illuminate\Http\Client\Factory */ protected $factory; /** * The handler function for the Guzzle client. * * @var callable */ protected $handler; /** * The pool of requests. * * @var array */ protected $pool = []; /** * Create a new requests pool. * * @param \Illuminate\Http\Client\Factory|null $factory * @return void */ public function __construct(Factory $factory = null) { $this->factory = $factory ?: new Factory(); $this->handler = Utils::chooseHandler(); } /** * Add a request to the pool with a key. * * @param string $key * @return \Illuminate\Http\Client\PendingRequest */ public function as(string $key) { return $this->pool[$key] = $this->asyncRequest(); } /** * Retrieve a new async pending request. * * @return \Illuminate\Http\Client\PendingRequest */ protected function asyncRequest() { return $this->factory->setHandler($this->handler)->async(); } /** * Retrieve the requests in the pool. * * @return array */ public function getRequests() { return $this->pool; } /** * Add a request to the pool with a numeric index. * * @param string $method * @param array $parameters * @return \Illuminate\Http\Client\PendingRequest|\GuzzleHttp\Promise\Promise */ public function __call($method, $parameters) { return $this->pool[] = $this->asyncRequest()->$method(...$parameters); } } src/Illuminate/Http/Client/RequestException.php 0000644 00000001767 15107327041 0015626 0 ustar 00 <?php namespace Illuminate\Http\Client; use GuzzleHttp\Psr7\Message; class RequestException extends HttpClientException { /** * The response instance. * * @var \Illuminate\Http\Client\Response */ public $response; /** * Create a new exception instance. * * @param \Illuminate\Http\Client\Response $response * @return void */ public function __construct(Response $response) { parent::__construct($this->prepareMessage($response), $response->status()); $this->response = $response; } /** * Prepare the exception message. * * @param \Illuminate\Http\Client\Response $response * @return string */ protected function prepareMessage(Response $response) { $message = "HTTP request returned status code {$response->status()}"; $summary = Message::bodySummary($response->toPsrResponse()); return is_null($summary) ? $message : $message .= ":\n{$summary}\n"; } } src/Illuminate/Http/Client/HttpClientException.php 0000644 00000000161 15107327041 0016237 0 ustar 00 <?php namespace Illuminate\Http\Client; use Exception; class HttpClientException extends Exception { // } src/Illuminate/Http/Client/Concerns/DeterminesStatusCode.php 0000644 00000006412 15107327041 0020157 0 ustar 00 <?php namespace Illuminate\Http\Client\Concerns; trait DeterminesStatusCode { /** * Determine if the response code was 200 "OK" response. * * @return bool */ public function ok() { return $this->status() === 200; } /** * Determine if the response code was 201 "Created" response. * * @return bool */ public function created() { return $this->status() === 201; } /** * Determine if the response code was 202 "Accepted" response. * * @return bool */ public function accepted() { return $this->status() === 202; } /** * Determine if the response code was the given status code and the body has no content. * * @param int $status * @return bool */ public function noContent($status = 204) { return $this->status() === $status && $this->body() === ''; } /** * Determine if the response code was a 301 "Moved Permanently". * * @return bool */ public function movedPermanently() { return $this->status() === 301; } /** * Determine if the response code was a 302 "Found" response. * * @return bool */ public function found() { return $this->status() === 302; } /** * Determine if the response code was a 304 "Not Modified" response. * * @return bool */ public function notModified() { return $this->status() === 304; } /** * Determine if the response was a 400 "Bad Request" response. * * @return bool */ public function badRequest() { return $this->status() === 400; } /** * Determine if the response was a 401 "Unauthorized" response. * * @return bool */ public function unauthorized() { return $this->status() === 401; } /** * Determine if the response was a 402 "Payment Required" response. * * @return bool */ public function paymentRequired() { return $this->status() === 402; } /** * Determine if the response was a 403 "Forbidden" response. * * @return bool */ public function forbidden() { return $this->status() === 403; } /** * Determine if the response was a 404 "Not Found" response. * * @return bool */ public function notFound() { return $this->status() === 404; } /** * Determine if the response was a 408 "Request Timeout" response. * * @return bool */ public function requestTimeout() { return $this->status() === 408; } /** * Determine if the response was a 409 "Conflict" response. * * @return bool */ public function conflict() { return $this->status() === 409; } /** * Determine if the response was a 422 "Unprocessable Entity" response. * * @return bool */ public function unprocessableEntity() { return $this->status() === 422; } /** * Determine if the response was a 429 "Too Many Requests" response. * * @return bool */ public function tooManyRequests() { return $this->status() === 429; } } src/Illuminate/Http/Client/Events/ConnectionFailed.php 0000644 00000000720 15107327041 0016753 0 ustar 00 <?php namespace Illuminate\Http\Client\Events; use Illuminate\Http\Client\Request; class ConnectionFailed { /** * The request instance. * * @var \Illuminate\Http\Client\Request */ public $request; /** * Create a new event instance. * * @param \Illuminate\Http\Client\Request $request * @return void */ public function __construct(Request $request) { $this->request = $request; } } src/Illuminate/Http/Client/Events/RequestSending.php 0000644 00000000716 15107327041 0016514 0 ustar 00 <?php namespace Illuminate\Http\Client\Events; use Illuminate\Http\Client\Request; class RequestSending { /** * The request instance. * * @var \Illuminate\Http\Client\Request */ public $request; /** * Create a new event instance. * * @param \Illuminate\Http\Client\Request $request * @return void */ public function __construct(Request $request) { $this->request = $request; } } src/Illuminate/Http/Client/Events/ResponseReceived.php 0000644 00000001342 15107327041 0017015 0 ustar 00 <?php namespace Illuminate\Http\Client\Events; use Illuminate\Http\Client\Request; use Illuminate\Http\Client\Response; class ResponseReceived { /** * The request instance. * * @var \Illuminate\Http\Client\Request */ public $request; /** * The response instance. * * @var \Illuminate\Http\Client\Response */ public $response; /** * Create a new event instance. * * @param \Illuminate\Http\Client\Request $request * @param \Illuminate\Http\Client\Response $response * @return void */ public function __construct(Request $request, Response $response) { $this->request = $request; $this->response = $response; } } src/Illuminate/Http/Client/Request.php 0000644 00000014337 15107327041 0013744 0 ustar 00 <?php namespace Illuminate\Http\Client; use ArrayAccess; use Illuminate\Support\Arr; use Illuminate\Support\Traits\Macroable; use LogicException; class Request implements ArrayAccess { use Macroable; /** * The underlying PSR request. * * @var \Psr\Http\Message\RequestInterface */ protected $request; /** * The decoded payload for the request. * * @var array */ protected $data; /** * Create a new request instance. * * @param \Psr\Http\Message\RequestInterface $request * @return void */ public function __construct($request) { $this->request = $request; } /** * Get the request method. * * @return string */ public function method() { return $this->request->getMethod(); } /** * Get the URL of the request. * * @return string */ public function url() { return (string) $this->request->getUri(); } /** * Determine if the request has a given header. * * @param string $key * @param mixed $value * @return bool */ public function hasHeader($key, $value = null) { if (is_null($value)) { return ! empty($this->request->getHeaders()[$key]); } $headers = $this->headers(); if (! Arr::has($headers, $key)) { return false; } $value = is_array($value) ? $value : [$value]; return empty(array_diff($value, $headers[$key])); } /** * Determine if the request has the given headers. * * @param array|string $headers * @return bool */ public function hasHeaders($headers) { if (is_string($headers)) { $headers = [$headers => null]; } foreach ($headers as $key => $value) { if (! $this->hasHeader($key, $value)) { return false; } } return true; } /** * Get the values for the header with the given name. * * @param string $key * @return array */ public function header($key) { return Arr::get($this->headers(), $key, []); } /** * Get the request headers. * * @return array */ public function headers() { return $this->request->getHeaders(); } /** * Get the body of the request. * * @return string */ public function body() { return (string) $this->request->getBody(); } /** * Determine if the request contains the given file. * * @param string $name * @param string|null $value * @param string|null $filename * @return bool */ public function hasFile($name, $value = null, $filename = null) { if (! $this->isMultipart()) { return false; } return collect($this->data)->reject(function ($file) use ($name, $value, $filename) { return $file['name'] != $name || ($value && $file['contents'] != $value) || ($filename && $file['filename'] != $filename); })->count() > 0; } /** * Get the request's data (form parameters or JSON). * * @return array */ public function data() { if ($this->isForm()) { return $this->parameters(); } elseif ($this->isJson()) { return $this->json(); } return $this->data ?? []; } /** * Get the request's form parameters. * * @return array */ protected function parameters() { if (! $this->data) { parse_str($this->body(), $parameters); $this->data = $parameters; } return $this->data; } /** * Get the JSON decoded body of the request. * * @return array */ protected function json() { if (! $this->data) { $this->data = json_decode($this->body(), true) ?? []; } return $this->data; } /** * Determine if the request is simple form data. * * @return bool */ public function isForm() { return $this->hasHeader('Content-Type', 'application/x-www-form-urlencoded'); } /** * Determine if the request is JSON. * * @return bool */ public function isJson() { return $this->hasHeader('Content-Type') && str_contains($this->header('Content-Type')[0], 'json'); } /** * Determine if the request is multipart. * * @return bool */ public function isMultipart() { return $this->hasHeader('Content-Type') && str_contains($this->header('Content-Type')[0], 'multipart'); } /** * Set the decoded data on the request. * * @param array $data * @return $this */ public function withData(array $data) { $this->data = $data; return $this; } /** * Get the underlying PSR compliant request instance. * * @return \Psr\Http\Message\RequestInterface */ public function toPsrRequest() { return $this->request; } /** * Determine if the given offset exists. * * @param string $offset * @return bool */ public function offsetExists($offset): bool { return isset($this->data()[$offset]); } /** * Get the value for a given offset. * * @param string $offset * @return mixed */ public function offsetGet($offset): mixed { return $this->data()[$offset]; } /** * Set the value at the given offset. * * @param string $offset * @param mixed $value * @return void * * @throws \LogicException */ public function offsetSet($offset, $value): void { throw new LogicException('Request data may not be mutated using array access.'); } /** * Unset the value at the given offset. * * @param string $offset * @return void * * @throws \LogicException */ public function offsetUnset($offset): void { throw new LogicException('Request data may not be mutated using array access.'); } } src/Illuminate/Http/Client/Factory.php 0000644 00000025227 15107327041 0013723 0 ustar 00 <?php namespace Illuminate\Http\Client; use Closure; use GuzzleHttp\Middleware; use GuzzleHttp\Promise\Create; use GuzzleHttp\Promise\PromiseInterface; use GuzzleHttp\Psr7\Response as Psr7Response; use GuzzleHttp\TransferStats; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use PHPUnit\Framework\Assert as PHPUnit; /** * @mixin \Illuminate\Http\Client\PendingRequest */ class Factory { use Macroable { __call as macroCall; } /** * The event dispatcher implementation. * * @var \Illuminate\Contracts\Events\Dispatcher|null */ protected $dispatcher; /** * The middleware to apply to every request. * * @var array */ protected $globalMiddleware = []; /** * The stub callables that will handle requests. * * @var \Illuminate\Support\Collection */ protected $stubCallbacks; /** * Indicates if the factory is recording requests and responses. * * @var bool */ protected $recording = false; /** * The recorded response array. * * @var array */ protected $recorded = []; /** * All created response sequences. * * @var array */ protected $responseSequences = []; /** * Indicates that an exception should be thrown if any request is not faked. * * @var bool */ protected $preventStrayRequests = false; /** * Create a new factory instance. * * @param \Illuminate\Contracts\Events\Dispatcher|null $dispatcher * @return void */ public function __construct(Dispatcher $dispatcher = null) { $this->dispatcher = $dispatcher; $this->stubCallbacks = collect(); } /** * Add middleware to apply to every request. * * @param callable $middleware * @return $this */ public function globalMiddleware($middleware) { $this->globalMiddleware[] = $middleware; return $this; } /** * Add request middleware to apply to every request. * * @param callable $middleware * @return $this */ public function globalRequestMiddleware($middleware) { $this->globalMiddleware[] = Middleware::mapRequest($middleware); return $this; } /** * Add response middleware to apply to every request. * * @param callable $middleware * @return $this */ public function globalResponseMiddleware($middleware) { $this->globalMiddleware[] = Middleware::mapResponse($middleware); return $this; } /** * Create a new response instance for use during stubbing. * * @param array|string|null $body * @param int $status * @param array $headers * @return \GuzzleHttp\Promise\PromiseInterface */ public static function response($body = null, $status = 200, $headers = []) { if (is_array($body)) { $body = json_encode($body); $headers['Content-Type'] = 'application/json'; } $response = new Psr7Response($status, $headers, $body); return Create::promiseFor($response); } /** * Get an invokable object that returns a sequence of responses in order for use during stubbing. * * @param array $responses * @return \Illuminate\Http\Client\ResponseSequence */ public function sequence(array $responses = []) { return $this->responseSequences[] = new ResponseSequence($responses); } /** * Register a stub callable that will intercept requests and be able to return stub responses. * * @param callable|array|null $callback * @return $this */ public function fake($callback = null) { $this->record(); $this->recorded = []; if (is_null($callback)) { $callback = function () { return static::response(); }; } if (is_array($callback)) { foreach ($callback as $url => $callable) { $this->stubUrl($url, $callable); } return $this; } $this->stubCallbacks = $this->stubCallbacks->merge(collect([ function ($request, $options) use ($callback) { $response = $callback instanceof Closure ? $callback($request, $options) : $callback; if ($response instanceof PromiseInterface) { $options['on_stats'](new TransferStats( $request->toPsrRequest(), $response->wait(), )); } return $response; }, ])); return $this; } /** * Register a response sequence for the given URL pattern. * * @param string $url * @return \Illuminate\Http\Client\ResponseSequence */ public function fakeSequence($url = '*') { return tap($this->sequence(), function ($sequence) use ($url) { $this->fake([$url => $sequence]); }); } /** * Stub the given URL using the given callback. * * @param string $url * @param \Illuminate\Http\Client\Response|\GuzzleHttp\Promise\PromiseInterface|callable $callback * @return $this */ public function stubUrl($url, $callback) { return $this->fake(function ($request, $options) use ($url, $callback) { if (! Str::is(Str::start($url, '*'), $request->url())) { return; } return $callback instanceof Closure || $callback instanceof ResponseSequence ? $callback($request, $options) : $callback; }); } /** * Indicate that an exception should be thrown if any request is not faked. * * @param bool $prevent * @return $this */ public function preventStrayRequests($prevent = true) { $this->preventStrayRequests = $prevent; return $this; } /** * Indicate that an exception should not be thrown if any request is not faked. * * @return $this */ public function allowStrayRequests() { return $this->preventStrayRequests(false); } /** * Begin recording request / response pairs. * * @return $this */ protected function record() { $this->recording = true; return $this; } /** * Record a request response pair. * * @param \Illuminate\Http\Client\Request $request * @param \Illuminate\Http\Client\Response $response * @return void */ public function recordRequestResponsePair($request, $response) { if ($this->recording) { $this->recorded[] = [$request, $response]; } } /** * Assert that a request / response pair was recorded matching a given truth test. * * @param callable $callback * @return void */ public function assertSent($callback) { PHPUnit::assertTrue( $this->recorded($callback)->count() > 0, 'An expected request was not recorded.' ); } /** * Assert that the given request was sent in the given order. * * @param array $callbacks * @return void */ public function assertSentInOrder($callbacks) { $this->assertSentCount(count($callbacks)); foreach ($callbacks as $index => $url) { $callback = is_callable($url) ? $url : function ($request) use ($url) { return $request->url() == $url; }; PHPUnit::assertTrue($callback( $this->recorded[$index][0], $this->recorded[$index][1] ), 'An expected request (#'.($index + 1).') was not recorded.'); } } /** * Assert that a request / response pair was not recorded matching a given truth test. * * @param callable $callback * @return void */ public function assertNotSent($callback) { PHPUnit::assertFalse( $this->recorded($callback)->count() > 0, 'Unexpected request was recorded.' ); } /** * Assert that no request / response pair was recorded. * * @return void */ public function assertNothingSent() { PHPUnit::assertEmpty( $this->recorded, 'Requests were recorded.' ); } /** * Assert how many requests have been recorded. * * @param int $count * @return void */ public function assertSentCount($count) { PHPUnit::assertCount($count, $this->recorded); } /** * Assert that every created response sequence is empty. * * @return void */ public function assertSequencesAreEmpty() { foreach ($this->responseSequences as $responseSequence) { PHPUnit::assertTrue( $responseSequence->isEmpty(), 'Not all response sequences are empty.' ); } } /** * Get a collection of the request / response pairs matching the given truth test. * * @param callable $callback * @return \Illuminate\Support\Collection */ public function recorded($callback = null) { if (empty($this->recorded)) { return collect(); } $callback = $callback ?: function () { return true; }; return collect($this->recorded)->filter(function ($pair) use ($callback) { return $callback($pair[0], $pair[1]); }); } /** * Create a new pending request instance for this factory. * * @return \Illuminate\Http\Client\PendingRequest */ protected function newPendingRequest() { return new PendingRequest($this, $this->globalMiddleware); } /** * Get the current event dispatcher implementation. * * @return \Illuminate\Contracts\Events\Dispatcher|null */ public function getDispatcher() { return $this->dispatcher; } /** * Execute a method against a new pending request instance. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); } return tap($this->newPendingRequest(), function ($request) { $request->stub($this->stubCallbacks)->preventStrayRequests($this->preventStrayRequests); })->{$method}(...$parameters); } } src/Illuminate/Http/Client/Response.php 0000644 00000023171 15107327041 0014106 0 ustar 00 <?php namespace Illuminate\Http\Client; use ArrayAccess; use Illuminate\Support\Collection; use Illuminate\Support\Traits\Macroable; use LogicException; class Response implements ArrayAccess { use Concerns\DeterminesStatusCode, Macroable { __call as macroCall; } /** * The underlying PSR response. * * @var \Psr\Http\Message\ResponseInterface */ protected $response; /** * The decoded JSON response. * * @var array */ protected $decoded; /** * The request cookies. * * @var \GuzzleHttp\Cookie\CookieJar */ public $cookies; /** * The transfer stats for the request. * * @var \GuzzleHttp\TransferStats|null */ public $transferStats; /** * Create a new response instance. * * @param \Psr\Http\Message\MessageInterface $response * @return void */ public function __construct($response) { $this->response = $response; } /** * Get the body of the response. * * @return string */ public function body() { return (string) $this->response->getBody(); } /** * Get the JSON decoded body of the response as an array or scalar value. * * @param string|null $key * @param mixed $default * @return mixed */ public function json($key = null, $default = null) { if (! $this->decoded) { $this->decoded = json_decode($this->body(), true); } if (is_null($key)) { return $this->decoded; } return data_get($this->decoded, $key, $default); } /** * Get the JSON decoded body of the response as an object. * * @return object|null */ public function object() { return json_decode($this->body(), false); } /** * Get the JSON decoded body of the response as a collection. * * @param string|null $key * @return \Illuminate\Support\Collection */ public function collect($key = null) { return Collection::make($this->json($key)); } /** * Get a header from the response. * * @param string $header * @return string */ public function header(string $header) { return $this->response->getHeaderLine($header); } /** * Get the headers from the response. * * @return array */ public function headers() { return $this->response->getHeaders(); } /** * Get the status code of the response. * * @return int */ public function status() { return (int) $this->response->getStatusCode(); } /** * Get the reason phrase of the response. * * @return string */ public function reason() { return $this->response->getReasonPhrase(); } /** * Get the effective URI of the response. * * @return \Psr\Http\Message\UriInterface|null */ public function effectiveUri() { return $this->transferStats?->getEffectiveUri(); } /** * Determine if the request was successful. * * @return bool */ public function successful() { return $this->status() >= 200 && $this->status() < 300; } /** * Determine if the response was a redirect. * * @return bool */ public function redirect() { return $this->status() >= 300 && $this->status() < 400; } /** * Determine if the response indicates a client or server error occurred. * * @return bool */ public function failed() { return $this->serverError() || $this->clientError(); } /** * Determine if the response indicates a client error occurred. * * @return bool */ public function clientError() { return $this->status() >= 400 && $this->status() < 500; } /** * Determine if the response indicates a server error occurred. * * @return bool */ public function serverError() { return $this->status() >= 500; } /** * Execute the given callback if there was a server or client error. * * @param callable $callback * @return $this */ public function onError(callable $callback) { if ($this->failed()) { $callback($this); } return $this; } /** * Get the response cookies. * * @return \GuzzleHttp\Cookie\CookieJar */ public function cookies() { return $this->cookies; } /** * Get the handler stats of the response. * * @return array */ public function handlerStats() { return $this->transferStats?->getHandlerStats() ?? []; } /** * Close the stream and any underlying resources. * * @return $this */ public function close() { $this->response->getBody()->close(); return $this; } /** * Get the underlying PSR response for the response. * * @return \Psr\Http\Message\ResponseInterface */ public function toPsrResponse() { return $this->response; } /** * Create an exception if a server or client error occurred. * * @return \Illuminate\Http\Client\RequestException|null */ public function toException() { if ($this->failed()) { return new RequestException($this); } } /** * Throw an exception if a server or client error occurred. * * @param \Closure|null $callback * @return $this * * @throws \Illuminate\Http\Client\RequestException */ public function throw() { $callback = func_get_args()[0] ?? null; if ($this->failed()) { throw tap($this->toException(), function ($exception) use ($callback) { if ($callback && is_callable($callback)) { $callback($this, $exception); } }); } return $this; } /** * Throw an exception if a server or client error occurred and the given condition evaluates to true. * * @param \Closure|bool $condition * @param \Closure|null $throwCallback * @return $this * * @throws \Illuminate\Http\Client\RequestException */ public function throwIf($condition) { return value($condition, $this) ? $this->throw(func_get_args()[1] ?? null) : $this; } /** * Throw an exception if the response status code matches the given code. * * @param callable|int $statusCode * @return $this * * @throws \Illuminate\Http\Client\RequestException */ public function throwIfStatus($statusCode) { if (is_callable($statusCode) && $statusCode($this->status(), $this)) { return $this->throw(); } return $this->status() === $statusCode ? $this->throw() : $this; } /** * Throw an exception unless the response status code matches the given code. * * @param callable|int $statusCode * @return $this * * @throws \Illuminate\Http\Client\RequestException */ public function throwUnlessStatus($statusCode) { if (is_callable($statusCode)) { return $statusCode($this->status(), $this) ? $this : $this->throw(); } return $this->status() === $statusCode ? $this : $this->throw(); } /** * Throw an exception if the response status code is a 4xx level code. * * @return $this * * @throws \Illuminate\Http\Client\RequestException */ public function throwIfClientError() { return $this->clientError() ? $this->throw() : $this; } /** * Throw an exception if the response status code is a 5xx level code. * * @return $this * * @throws \Illuminate\Http\Client\RequestException */ public function throwIfServerError() { return $this->serverError() ? $this->throw() : $this; } /** * Determine if the given offset exists. * * @param string $offset * @return bool */ public function offsetExists($offset): bool { return isset($this->json()[$offset]); } /** * Get the value for a given offset. * * @param string $offset * @return mixed */ public function offsetGet($offset): mixed { return $this->json()[$offset]; } /** * Set the value at the given offset. * * @param string $offset * @param mixed $value * @return void * * @throws \LogicException */ public function offsetSet($offset, $value): void { throw new LogicException('Response data may not be mutated using array access.'); } /** * Unset the value at the given offset. * * @param string $offset * @return void * * @throws \LogicException */ public function offsetUnset($offset): void { throw new LogicException('Response data may not be mutated using array access.'); } /** * Get the body of the response. * * @return string */ public function __toString() { return $this->body(); } /** * Dynamically proxy other methods to the underlying response. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return static::hasMacro($method) ? $this->macroCall($method, $parameters) : $this->response->{$method}(...$parameters); } } src/Illuminate/Http/Client/ConnectionException.php 0000644 00000000153 15107327041 0016261 0 ustar 00 <?php namespace Illuminate\Http\Client; class ConnectionException extends HttpClientException { // } src/Illuminate/Http/Client/ResponseSequence.php 0000644 00000006735 15107327041 0015606 0 ustar 00 <?php namespace Illuminate\Http\Client; use Illuminate\Support\Traits\Macroable; use OutOfBoundsException; class ResponseSequence { use Macroable; /** * The responses in the sequence. * * @var array */ protected $responses; /** * Indicates that invoking this sequence when it is empty should throw an exception. * * @var bool */ protected $failWhenEmpty = true; /** * The response that should be returned when the sequence is empty. * * @var \GuzzleHttp\Promise\PromiseInterface */ protected $emptyResponse; /** * Create a new response sequence. * * @param array $responses * @return void */ public function __construct(array $responses) { $this->responses = $responses; } /** * Push a response to the sequence. * * @param string|array|null $body * @param int $status * @param array $headers * @return $this */ public function push($body = null, int $status = 200, array $headers = []) { return $this->pushResponse( Factory::response($body, $status, $headers) ); } /** * Push a response with the given status code to the sequence. * * @param int $status * @param array $headers * @return $this */ public function pushStatus(int $status, array $headers = []) { return $this->pushResponse( Factory::response('', $status, $headers) ); } /** * Push response with the contents of a file as the body to the sequence. * * @param string $filePath * @param int $status * @param array $headers * @return $this */ public function pushFile(string $filePath, int $status = 200, array $headers = []) { $string = file_get_contents($filePath); return $this->pushResponse( Factory::response($string, $status, $headers) ); } /** * Push a response to the sequence. * * @param mixed $response * @return $this */ public function pushResponse($response) { $this->responses[] = $response; return $this; } /** * Make the sequence return a default response when it is empty. * * @param \GuzzleHttp\Promise\PromiseInterface|\Closure $response * @return $this */ public function whenEmpty($response) { $this->failWhenEmpty = false; $this->emptyResponse = $response; return $this; } /** * Make the sequence return a default response when it is empty. * * @return $this */ public function dontFailWhenEmpty() { return $this->whenEmpty(Factory::response()); } /** * Indicate that this sequence has depleted all of its responses. * * @return bool */ public function isEmpty() { return count($this->responses) === 0; } /** * Get the next response in the sequence. * * @return mixed * * @throws \OutOfBoundsException */ public function __invoke() { if ($this->failWhenEmpty && $this->isEmpty()) { throw new OutOfBoundsException('A request was made, but the response sequence is empty.'); } if (! $this->failWhenEmpty && $this->isEmpty()) { return value($this->emptyResponse ?? Factory::response()); } return array_shift($this->responses); } } src/Illuminate/Http/Client/PendingRequest.php 0000644 00000111720 15107327041 0015243 0 ustar 00 <?php namespace Illuminate\Http\Client; use Closure; use Exception; use GuzzleHttp\Client; use GuzzleHttp\Cookie\CookieJar; use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\TransferException; use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; use GuzzleHttp\UriTemplate\UriTemplate; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Http\Client\Events\ConnectionFailed; use Illuminate\Http\Client\Events\RequestSending; use Illuminate\Http\Client\Events\ResponseReceived; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Illuminate\Support\Stringable; use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\Macroable; use JsonSerializable; use Psr\Http\Message\MessageInterface; use Psr\Http\Message\RequestInterface; use RuntimeException; use Symfony\Component\VarDumper\VarDumper; class PendingRequest { use Conditionable, Macroable; /** * The factory instance. * * @var \Illuminate\Http\Client\Factory|null */ protected $factory; /** * The Guzzle client instance. * * @var \GuzzleHttp\Client */ protected $client; /** * The Guzzle HTTP handler. * * @var callable */ protected $handler; /** * The base URL for the request. * * @var string */ protected $baseUrl = ''; /** * The parameters that can be substituted into the URL. * * @var array */ protected $urlParameters = []; /** * The request body format. * * @var string */ protected $bodyFormat; /** * The raw body for the request. * * @var string */ protected $pendingBody; /** * The pending files for the request. * * @var array */ protected $pendingFiles = []; /** * The request cookies. * * @var array */ protected $cookies; /** * The transfer stats for the request. * * @var \GuzzleHttp\TransferStats */ protected $transferStats; /** * The request options. * * @var array */ protected $options = []; /** * A callback to run when throwing if a server or client error occurs. * * @var \Closure */ protected $throwCallback; /** * A callback to check if an exception should be thrown when a server or client error occurs. * * @var \Closure */ protected $throwIfCallback; /** * The number of times to try the request. * * @var int */ protected $tries = 1; /** * The number of milliseconds to wait between retries. * * @var Closure|int */ protected $retryDelay = 100; /** * Whether to throw an exception when all retries fail. * * @var bool */ protected $retryThrow = true; /** * The callback that will determine if the request should be retried. * * @var callable|null */ protected $retryWhenCallback = null; /** * The callbacks that should execute before the request is sent. * * @var \Illuminate\Support\Collection */ protected $beforeSendingCallbacks; /** * The stub callables that will handle requests. * * @var \Illuminate\Support\Collection|null */ protected $stubCallbacks; /** * Indicates that an exception should be thrown if any request is not faked. * * @var bool */ protected $preventStrayRequests = false; /** * The middleware callables added by users that will handle requests. * * @var \Illuminate\Support\Collection */ protected $middleware; /** * Whether the requests should be asynchronous. * * @var bool */ protected $async = false; /** * The pending request promise. * * @var \GuzzleHttp\Promise\PromiseInterface */ protected $promise; /** * The sent request object, if a request has been made. * * @var \Illuminate\Http\Client\Request|null */ protected $request; /** * The Guzzle request options that are mergable via array_merge_recursive. * * @var array */ protected $mergableOptions = [ 'cookies', 'form_params', 'headers', 'json', 'multipart', 'query', ]; /** * Create a new HTTP Client instance. * * @param \Illuminate\Http\Client\Factory|null $factory * @param array $middleware * @return void */ public function __construct(Factory $factory = null, $middleware = []) { $this->factory = $factory; $this->middleware = new Collection($middleware); $this->asJson(); $this->options = [ 'connect_timeout' => 10, 'http_errors' => false, 'timeout' => 30, ]; $this->beforeSendingCallbacks = collect([function (Request $request, array $options, PendingRequest $pendingRequest) { $pendingRequest->request = $request; $pendingRequest->cookies = $options['cookies']; $pendingRequest->dispatchRequestSendingEvent(); }]); } /** * Set the base URL for the pending request. * * @param string $url * @return $this */ public function baseUrl(string $url) { $this->baseUrl = $url; return $this; } /** * Attach a raw body to the request. * * @param string $content * @param string $contentType * @return $this */ public function withBody($content, $contentType = 'application/json') { $this->bodyFormat('body'); $this->pendingBody = $content; $this->contentType($contentType); return $this; } /** * Indicate the request contains JSON. * * @return $this */ public function asJson() { return $this->bodyFormat('json')->contentType('application/json'); } /** * Indicate the request contains form parameters. * * @return $this */ public function asForm() { return $this->bodyFormat('form_params')->contentType('application/x-www-form-urlencoded'); } /** * Attach a file to the request. * * @param string|array $name * @param string|resource $contents * @param string|null $filename * @param array $headers * @return $this */ public function attach($name, $contents = '', $filename = null, array $headers = []) { if (is_array($name)) { foreach ($name as $file) { $this->attach(...$file); } return $this; } $this->asMultipart(); $this->pendingFiles[] = array_filter([ 'name' => $name, 'contents' => $contents, 'headers' => $headers, 'filename' => $filename, ]); return $this; } /** * Indicate the request is a multi-part form request. * * @return $this */ public function asMultipart() { return $this->bodyFormat('multipart'); } /** * Specify the body format of the request. * * @param string $format * @return $this */ public function bodyFormat(string $format) { return tap($this, function () use ($format) { $this->bodyFormat = $format; }); } /** * Set the given query parameters in the request URI. * * @param array $parameters * @return $this */ public function withQueryParameters(array $parameters) { return tap($this, function () use ($parameters) { $this->options = array_merge_recursive($this->options, [ 'query' => $parameters, ]); }); } /** * Specify the request's content type. * * @param string $contentType * @return $this */ public function contentType(string $contentType) { $this->options['headers']['Content-Type'] = $contentType; return $this; } /** * Indicate that JSON should be returned by the server. * * @return $this */ public function acceptJson() { return $this->accept('application/json'); } /** * Indicate the type of content that should be returned by the server. * * @param string $contentType * @return $this */ public function accept($contentType) { return $this->withHeaders(['Accept' => $contentType]); } /** * Add the given headers to the request. * * @param array $headers * @return $this */ public function withHeaders(array $headers) { return tap($this, function () use ($headers) { $this->options = array_merge_recursive($this->options, [ 'headers' => $headers, ]); }); } /** * Add the given header to the request. * * @param string $name * @param mixed $value * @return $this */ public function withHeader($name, $value) { return $this->withHeaders([$name => $value]); } /** * Replace the given headers on the request. * * @param array $headers * @return $this */ public function replaceHeaders(array $headers) { $this->options['headers'] = array_merge($this->options['headers'] ?? [], $headers); return $this; } /** * Specify the basic authentication username and password for the request. * * @param string $username * @param string $password * @return $this */ public function withBasicAuth(string $username, string $password) { return tap($this, function () use ($username, $password) { $this->options['auth'] = [$username, $password]; }); } /** * Specify the digest authentication username and password for the request. * * @param string $username * @param string $password * @return $this */ public function withDigestAuth($username, $password) { return tap($this, function () use ($username, $password) { $this->options['auth'] = [$username, $password, 'digest']; }); } /** * Specify an authorization token for the request. * * @param string $token * @param string $type * @return $this */ public function withToken($token, $type = 'Bearer') { return tap($this, function () use ($token, $type) { $this->options['headers']['Authorization'] = trim($type.' '.$token); }); } /** * Specify the user agent for the request. * * @param string|bool $userAgent * @return $this */ public function withUserAgent($userAgent) { return tap($this, function () use ($userAgent) { $this->options['headers']['User-Agent'] = trim($userAgent); }); } /** * Specify the URL parameters that can be substituted into the request URL. * * @param array $parameters * @return $this */ public function withUrlParameters(array $parameters = []) { return tap($this, function () use ($parameters) { $this->urlParameters = $parameters; }); } /** * Specify the cookies that should be included with the request. * * @param array $cookies * @param string $domain * @return $this */ public function withCookies(array $cookies, string $domain) { return tap($this, function () use ($cookies, $domain) { $this->options = array_merge_recursive($this->options, [ 'cookies' => CookieJar::fromArray($cookies, $domain), ]); }); } /** * Specify the maximum number of redirects to allow. * * @param int $max * @return $this */ public function maxRedirects(int $max) { return tap($this, function () use ($max) { $this->options['allow_redirects']['max'] = $max; }); } /** * Indicate that redirects should not be followed. * * @return $this */ public function withoutRedirecting() { return tap($this, function () { $this->options['allow_redirects'] = false; }); } /** * Indicate that TLS certificates should not be verified. * * @return $this */ public function withoutVerifying() { return tap($this, function () { $this->options['verify'] = false; }); } /** * Specify the path where the body of the response should be stored. * * @param string|resource $to * @return $this */ public function sink($to) { return tap($this, function () use ($to) { $this->options['sink'] = $to; }); } /** * Specify the timeout (in seconds) for the request. * * @param int $seconds * @return $this */ public function timeout(int $seconds) { return tap($this, function () use ($seconds) { $this->options['timeout'] = $seconds; }); } /** * Specify the connect timeout (in seconds) for the request. * * @param int $seconds * @return $this */ public function connectTimeout(int $seconds) { return tap($this, function () use ($seconds) { $this->options['connect_timeout'] = $seconds; }); } /** * Specify the number of times the request should be attempted. * * @param int $times * @param Closure|int $sleepMilliseconds * @param callable|null $when * @param bool $throw * @return $this */ public function retry(int $times, Closure|int $sleepMilliseconds = 0, ?callable $when = null, bool $throw = true) { $this->tries = $times; $this->retryDelay = $sleepMilliseconds; $this->retryThrow = $throw; $this->retryWhenCallback = $when; return $this; } /** * Replace the specified options on the request. * * @param array $options * @return $this */ public function withOptions(array $options) { return tap($this, function () use ($options) { $this->options = array_replace_recursive( array_merge_recursive($this->options, Arr::only($options, $this->mergableOptions)), $options ); }); } /** * Add new middleware the client handler stack. * * @param callable $middleware * @return $this */ public function withMiddleware(callable $middleware) { $this->middleware->push($middleware); return $this; } /** * Add new request middleware the client handler stack. * * @param callable $middleware * @return $this */ public function withRequestMiddleware(callable $middleware) { $this->middleware->push(Middleware::mapRequest($middleware)); return $this; } /** * Add new response middleware the client handler stack. * * @param callable $middleware * @return $this */ public function withResponseMiddleware(callable $middleware) { $this->middleware->push(Middleware::mapResponse($middleware)); return $this; } /** * Add a new "before sending" callback to the request. * * @param callable $callback * @return $this */ public function beforeSending($callback) { return tap($this, function () use ($callback) { $this->beforeSendingCallbacks[] = $callback; }); } /** * Throw an exception if a server or client error occurs. * * @param callable|null $callback * @return $this */ public function throw(callable $callback = null) { $this->throwCallback = $callback ?: fn () => null; return $this; } /** * Throw an exception if a server or client error occurred and the given condition evaluates to true. * * @param callable|bool $condition * @param callable|null $throwCallback * @return $this */ public function throwIf($condition) { if (is_callable($condition)) { $this->throwIfCallback = $condition; } return $condition ? $this->throw(func_get_args()[1] ?? null) : $this; } /** * Throw an exception if a server or client error occurred and the given condition evaluates to false. * * @param bool $condition * @return $this */ public function throwUnless($condition) { return $this->throwIf(! $condition); } /** * Dump the request before sending. * * @return $this */ public function dump() { $values = func_get_args(); return $this->beforeSending(function (Request $request, array $options) use ($values) { foreach (array_merge($values, [$request, $options]) as $value) { VarDumper::dump($value); } }); } /** * Dump the request before sending and end the script. * * @return $this */ public function dd() { $values = func_get_args(); return $this->beforeSending(function (Request $request, array $options) use ($values) { foreach (array_merge($values, [$request, $options]) as $value) { VarDumper::dump($value); } exit(1); }); } /** * Issue a GET request to the given URL. * * @param string $url * @param array|string|null $query * @return \Illuminate\Http\Client\Response */ public function get(string $url, $query = null) { return $this->send('GET', $url, func_num_args() === 1 ? [] : [ 'query' => $query, ]); } /** * Issue a HEAD request to the given URL. * * @param string $url * @param array|string|null $query * @return \Illuminate\Http\Client\Response */ public function head(string $url, $query = null) { return $this->send('HEAD', $url, func_num_args() === 1 ? [] : [ 'query' => $query, ]); } /** * Issue a POST request to the given URL. * * @param string $url * @param array $data * @return \Illuminate\Http\Client\Response */ public function post(string $url, $data = []) { return $this->send('POST', $url, [ $this->bodyFormat => $data, ]); } /** * Issue a PATCH request to the given URL. * * @param string $url * @param array $data * @return \Illuminate\Http\Client\Response */ public function patch(string $url, $data = []) { return $this->send('PATCH', $url, [ $this->bodyFormat => $data, ]); } /** * Issue a PUT request to the given URL. * * @param string $url * @param array $data * @return \Illuminate\Http\Client\Response */ public function put(string $url, $data = []) { return $this->send('PUT', $url, [ $this->bodyFormat => $data, ]); } /** * Issue a DELETE request to the given URL. * * @param string $url * @param array $data * @return \Illuminate\Http\Client\Response */ public function delete(string $url, $data = []) { return $this->send('DELETE', $url, empty($data) ? [] : [ $this->bodyFormat => $data, ]); } /** * Send a pool of asynchronous requests concurrently. * * @param callable $callback * @return array<array-key, \Illuminate\Http\Client\Response> */ public function pool(callable $callback) { $results = []; $requests = tap(new Pool($this->factory), $callback)->getRequests(); foreach ($requests as $key => $item) { $results[$key] = $item instanceof static ? $item->getPromise()->wait() : $item->wait(); } return $results; } /** * Send the request to the given URL. * * @param string $method * @param string $url * @param array $options * @return \Illuminate\Http\Client\Response * * @throws \Exception */ public function send(string $method, string $url, array $options = []) { if (! Str::startsWith($url, ['http://', 'https://'])) { $url = ltrim(rtrim($this->baseUrl, '/').'/'.ltrim($url, '/'), '/'); } $url = $this->expandUrlParameters($url); $options = $this->parseHttpOptions($options); [$this->pendingBody, $this->pendingFiles] = [null, []]; if ($this->async) { return $this->makePromise($method, $url, $options); } $shouldRetry = null; return retry($this->tries ?? 1, function ($attempt) use ($method, $url, $options, &$shouldRetry) { try { return tap($this->newResponse($this->sendRequest($method, $url, $options)), function ($response) use ($attempt, &$shouldRetry) { $this->populateResponse($response); $this->dispatchResponseReceivedEvent($response); if (! $response->successful()) { try { $shouldRetry = $this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $response->toException(), $this) : true; } catch (Exception $exception) { $shouldRetry = false; throw $exception; } if ($this->throwCallback && ($this->throwIfCallback === null || call_user_func($this->throwIfCallback, $response))) { $response->throw($this->throwCallback); } if ($attempt < $this->tries && $shouldRetry) { $response->throw(); } if ($this->tries > 1 && $this->retryThrow) { $response->throw(); } } }); } catch (ConnectException $e) { $this->dispatchConnectionFailedEvent(); throw new ConnectionException($e->getMessage(), 0, $e); } }, $this->retryDelay ?? 100, function ($exception) use (&$shouldRetry) { $result = $shouldRetry ?? ($this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $exception, $this) : true); $shouldRetry = null; return $result; }); } /** * Substitute the URL parameters in the given URL. * * @param string $url * @return string */ protected function expandUrlParameters(string $url) { return UriTemplate::expand($url, $this->urlParameters); } /** * Parse the given HTTP options and set the appropriate additional options. * * @param array $options * @return array */ protected function parseHttpOptions(array $options) { if (isset($options[$this->bodyFormat])) { if ($this->bodyFormat === 'multipart') { $options[$this->bodyFormat] = $this->parseMultipartBodyFormat($options[$this->bodyFormat]); } elseif ($this->bodyFormat === 'body') { $options[$this->bodyFormat] = $this->pendingBody; } if (is_array($options[$this->bodyFormat])) { $options[$this->bodyFormat] = array_merge( $options[$this->bodyFormat], $this->pendingFiles ); } } else { $options[$this->bodyFormat] = $this->pendingBody; } return collect($options)->map(function ($value, $key) { if ($key === 'json' && $value instanceof JsonSerializable) { return $value; } return $value instanceof Arrayable ? $value->toArray() : $value; })->all(); } /** * Parse multi-part form data. * * @param array $data * @return array|array[] */ protected function parseMultipartBodyFormat(array $data) { return collect($data)->map(function ($value, $key) { return is_array($value) ? $value : ['name' => $key, 'contents' => $value]; })->values()->all(); } /** * Send an asynchronous request to the given URL. * * @param string $method * @param string $url * @param array $options * @return \GuzzleHttp\Promise\PromiseInterface */ protected function makePromise(string $method, string $url, array $options = []) { return $this->promise = $this->sendRequest($method, $url, $options) ->then(function (MessageInterface $message) { return tap($this->newResponse($message), function ($response) { $this->populateResponse($response); $this->dispatchResponseReceivedEvent($response); }); }) ->otherwise(function (TransferException $e) { return $e instanceof RequestException && $e->hasResponse() ? $this->populateResponse($this->newResponse($e->getResponse())) : $e; }); } /** * Send a request either synchronously or asynchronously. * * @param string $method * @param string $url * @param array $options * @return \Psr\Http\Message\MessageInterface|\GuzzleHttp\Promise\PromiseInterface * * @throws \Exception */ protected function sendRequest(string $method, string $url, array $options = []) { $clientMethod = $this->async ? 'requestAsync' : 'request'; $laravelData = $this->parseRequestData($method, $url, $options); $onStats = function ($transferStats) { if (($callback = ($this->options['on_stats'] ?? false)) instanceof Closure) { $transferStats = $callback($transferStats) ?: $transferStats; } $this->transferStats = $transferStats; }; $mergedOptions = $this->normalizeRequestOptions($this->mergeOptions([ 'laravel_data' => $laravelData, 'on_stats' => $onStats, ], $options)); return $this->buildClient()->$clientMethod($method, $url, $mergedOptions); } /** * Get the request data as an array so that we can attach it to the request for convenient assertions. * * @param string $method * @param string $url * @param array $options * @return array */ protected function parseRequestData($method, $url, array $options) { if ($this->bodyFormat === 'body') { return []; } $laravelData = $options[$this->bodyFormat] ?? $options['query'] ?? []; $urlString = Str::of($url); if (empty($laravelData) && $method === 'GET' && $urlString->contains('?')) { $laravelData = (string) $urlString->after('?'); } if (is_string($laravelData)) { parse_str($laravelData, $parsedData); $laravelData = is_array($parsedData) ? $parsedData : []; } if ($laravelData instanceof JsonSerializable) { $laravelData = $laravelData->jsonSerialize(); } return is_array($laravelData) ? $laravelData : []; } /** * Normalize the given request options. * * @param array $options * @return array */ protected function normalizeRequestOptions(array $options) { foreach ($options as $key => $value) { $options[$key] = match (true) { is_array($value) => $this->normalizeRequestOptions($value), $value instanceof Stringable => $value->toString(), default => $value, }; } return $options; } /** * Populate the given response with additional data. * * @param \Illuminate\Http\Client\Response $response * @return \Illuminate\Http\Client\Response */ protected function populateResponse(Response $response) { $response->cookies = $this->cookies; $response->transferStats = $this->transferStats; return $response; } /** * Build the Guzzle client. * * @return \GuzzleHttp\Client */ public function buildClient() { return $this->client ?? $this->createClient($this->buildHandlerStack()); } /** * Determine if a reusable client is required. * * @return bool */ protected function requestsReusableClient() { return ! is_null($this->client) || $this->async; } /** * Retrieve a reusable Guzzle client. * * @return \GuzzleHttp\Client */ protected function getReusableClient() { return $this->client = $this->client ?: $this->createClient($this->buildHandlerStack()); } /** * Create new Guzzle client. * * @param \GuzzleHttp\HandlerStack $handlerStack * @return \GuzzleHttp\Client */ public function createClient($handlerStack) { return new Client([ 'handler' => $handlerStack, 'cookies' => true, ]); } /** * Build the Guzzle client handler stack. * * @return \GuzzleHttp\HandlerStack */ public function buildHandlerStack() { return $this->pushHandlers(HandlerStack::create($this->handler)); } /** * Add the necessary handlers to the given handler stack. * * @param \GuzzleHttp\HandlerStack $handlerStack * @return \GuzzleHttp\HandlerStack */ public function pushHandlers($handlerStack) { return tap($handlerStack, function ($stack) { $stack->push($this->buildBeforeSendingHandler()); $this->middleware->each(function ($middleware) use ($stack) { $stack->push($middleware); }); $stack->push($this->buildRecorderHandler()); $stack->push($this->buildStubHandler()); }); } /** * Build the before sending handler. * * @return \Closure */ public function buildBeforeSendingHandler() { return function ($handler) { return function ($request, $options) use ($handler) { return $handler($this->runBeforeSendingCallbacks($request, $options), $options); }; }; } /** * Build the recorder handler. * * @return \Closure */ public function buildRecorderHandler() { return function ($handler) { return function ($request, $options) use ($handler) { $promise = $handler($request, $options); return $promise->then(function ($response) use ($request, $options) { $this->factory?->recordRequestResponsePair( (new Request($request))->withData($options['laravel_data']), $this->newResponse($response) ); return $response; }); }; }; } /** * Build the stub handler. * * @return \Closure */ public function buildStubHandler() { return function ($handler) { return function ($request, $options) use ($handler) { $response = ($this->stubCallbacks ?? collect()) ->map ->__invoke((new Request($request))->withData($options['laravel_data']), $options) ->filter() ->first(); if (is_null($response)) { if ($this->preventStrayRequests) { throw new RuntimeException('Attempted request to ['.(string) $request->getUri().'] without a matching fake.'); } return $handler($request, $options); } $response = is_array($response) ? Factory::response($response) : $response; $sink = $options['sink'] ?? null; if ($sink) { $response->then($this->sinkStubHandler($sink)); } return $response; }; }; } /** * Get the sink stub handler callback. * * @param string $sink * @return \Closure */ protected function sinkStubHandler($sink) { return function ($response) use ($sink) { $body = $response->getBody()->getContents(); if (is_string($sink)) { file_put_contents($sink, $body); return; } fwrite($sink, $body); rewind($sink); }; } /** * Execute the "before sending" callbacks. * * @param \GuzzleHttp\Psr7\RequestInterface $request * @param array $options * @return \GuzzleHttp\Psr7\RequestInterface */ public function runBeforeSendingCallbacks($request, array $options) { return tap($request, function (&$request) use ($options) { $this->beforeSendingCallbacks->each(function ($callback) use (&$request, $options) { $callbackResult = call_user_func( $callback, (new Request($request))->withData($options['laravel_data']), $options, $this ); if ($callbackResult instanceof RequestInterface) { $request = $callbackResult; } elseif ($callbackResult instanceof Request) { $request = $callbackResult->toPsrRequest(); } }); }); } /** * Replace the given options with the current request options. * * @param array ...$options * @return array */ public function mergeOptions(...$options) { return array_replace_recursive( array_merge_recursive($this->options, Arr::only($options, $this->mergableOptions)), ...$options ); } /** * Create a new response instance using the given PSR response. * * @param \Psr\Http\Message\MessageInterface $response * @return Response */ protected function newResponse($response) { return new Response($response); } /** * Register a stub callable that will intercept requests and be able to return stub responses. * * @param callable $callback * @return $this */ public function stub($callback) { $this->stubCallbacks = collect($callback); return $this; } /** * Indicate that an exception should be thrown if any request is not faked. * * @param bool $prevent * @return $this */ public function preventStrayRequests($prevent = true) { $this->preventStrayRequests = $prevent; return $this; } /** * Toggle asynchronicity in requests. * * @param bool $async * @return $this */ public function async(bool $async = true) { $this->async = $async; return $this; } /** * Retrieve the pending request promise. * * @return \GuzzleHttp\Promise\PromiseInterface|null */ public function getPromise() { return $this->promise; } /** * Dispatch the RequestSending event if a dispatcher is available. * * @return void */ protected function dispatchRequestSendingEvent() { if ($dispatcher = $this->factory?->getDispatcher()) { $dispatcher->dispatch(new RequestSending($this->request)); } } /** * Dispatch the ResponseReceived event if a dispatcher is available. * * @param \Illuminate\Http\Client\Response $response * @return void */ protected function dispatchResponseReceivedEvent(Response $response) { if (! ($dispatcher = $this->factory?->getDispatcher()) || ! $this->request) { return; } $dispatcher->dispatch(new ResponseReceived($this->request, $response)); } /** * Dispatch the ConnectionFailed event if a dispatcher is available. * * @return void */ protected function dispatchConnectionFailedEvent() { if ($dispatcher = $this->factory?->getDispatcher()) { $dispatcher->dispatch(new ConnectionFailed($this->request)); } } /** * Set the client instance. * * @param \GuzzleHttp\Client $client * @return $this */ public function setClient(Client $client) { $this->client = $client; return $this; } /** * Create a new client instance using the given handler. * * @param callable $handler * @return $this */ public function setHandler($handler) { $this->handler = $handler; return $this; } /** * Get the pending request options. * * @return array */ public function getOptions() { return $this->options; } } src/Illuminate/Http/LICENSE.md 0000644 00000002063 15107327041 0011762 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Http/Middleware/TrustProxies.php 0000644 00000007361 15107327041 0015645 0 ustar 00 <?php namespace Illuminate\Http\Middleware; use Closure; use Illuminate\Http\Request; class TrustProxies { /** * The trusted proxies for the application. * * @var array<int, string>|string|null */ protected $proxies; /** * The proxy header mappings. * * @var int */ protected $headers = Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO | Request::HEADER_X_FORWARDED_PREFIX | Request::HEADER_X_FORWARDED_AWS_ELB; /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed * * @throws \Symfony\Component\HttpKernel\Exception\HttpException */ public function handle(Request $request, Closure $next) { $request::setTrustedProxies([], $this->getTrustedHeaderNames()); $this->setTrustedProxyIpAddresses($request); return $next($request); } /** * Sets the trusted proxies on the request. * * @param \Illuminate\Http\Request $request * @return void */ protected function setTrustedProxyIpAddresses(Request $request) { $trustedIps = $this->proxies() ?: config('trustedproxy.proxies'); if ($trustedIps === '*' || $trustedIps === '**') { return $this->setTrustedProxyIpAddressesToTheCallingIp($request); } $trustedIps = is_string($trustedIps) ? array_map('trim', explode(',', $trustedIps)) : $trustedIps; if (is_array($trustedIps)) { return $this->setTrustedProxyIpAddressesToSpecificIps($request, $trustedIps); } } /** * Specify the IP addresses to trust explicitly. * * @param \Illuminate\Http\Request $request * @param array $trustedIps * @return void */ protected function setTrustedProxyIpAddressesToSpecificIps(Request $request, array $trustedIps) { $request->setTrustedProxies($trustedIps, $this->getTrustedHeaderNames()); } /** * Set the trusted proxy to be the IP address calling this servers. * * @param \Illuminate\Http\Request $request * @return void */ protected function setTrustedProxyIpAddressesToTheCallingIp(Request $request) { $request->setTrustedProxies([$request->server->get('REMOTE_ADDR')], $this->getTrustedHeaderNames()); } /** * Retrieve trusted header name(s), falling back to defaults if config not set. * * @return int A bit field of Request::HEADER_*, to set which headers to trust from your proxies. */ protected function getTrustedHeaderNames() { if (is_int($this->headers)) { return $this->headers; } return match ($this->headers) { 'HEADER_X_FORWARDED_AWS_ELB' => Request::HEADER_X_FORWARDED_AWS_ELB, 'HEADER_FORWARDED' => Request::HEADER_FORWARDED, 'HEADER_X_FORWARDED_FOR' => Request::HEADER_X_FORWARDED_FOR, 'HEADER_X_FORWARDED_HOST' => Request::HEADER_X_FORWARDED_HOST, 'HEADER_X_FORWARDED_PORT' => Request::HEADER_X_FORWARDED_PORT, 'HEADER_X_FORWARDED_PROTO' => Request::HEADER_X_FORWARDED_PROTO, 'HEADER_X_FORWARDED_PREFIX' => Request::HEADER_X_FORWARDED_PREFIX, default => Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO | Request::HEADER_X_FORWARDED_PREFIX | Request::HEADER_X_FORWARDED_AWS_ELB, }; } /** * Get the trusted proxies. * * @return array|string|null */ protected function proxies() { return $this->proxies; } } src/Illuminate/Http/Middleware/AddLinkHeadersForPreloadedAssets.php 0000644 00000001352 15107327041 0021420 0 ustar 00 <?php namespace Illuminate\Http\Middleware; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Vite; class AddLinkHeadersForPreloadedAssets { /** * Handle the incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return \Illuminate\Http\Response */ public function handle($request, $next) { return tap($next($request), function ($response) { if (Vite::preloadedAssets() !== []) { $response->header('Link', Collection::make(Vite::preloadedAssets()) ->map(fn ($attributes, $url) => "<{$url}>; ".implode('; ', $attributes)) ->join(', ')); } }); } } src/Illuminate/Http/Middleware/TrustHosts.php 0000644 00000003244 15107327041 0015310 0 ustar 00 <?php namespace Illuminate\Http\Middleware; use Illuminate\Contracts\Foundation\Application; use Illuminate\Http\Request; abstract class TrustHosts { /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * Create a new middleware instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function __construct(Application $app) { $this->app = $app; } /** * Get the host patterns that should be trusted. * * @return array */ abstract public function hosts(); /** * Handle the incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return \Illuminate\Http\Response */ public function handle(Request $request, $next) { if ($this->shouldSpecifyTrustedHosts()) { Request::setTrustedHosts(array_filter($this->hosts())); } return $next($request); } /** * Determine if the application should specify trusted hosts. * * @return bool */ protected function shouldSpecifyTrustedHosts() { return ! $this->app->environment('local') && ! $this->app->runningUnitTests(); } /** * Get a regular expression matching the application URL and all of its subdomains. * * @return string|null */ protected function allSubdomainsOfApplicationUrl() { if ($host = parse_url($this->app['config']->get('app.url'), PHP_URL_HOST)) { return '^(.+\.)?'.preg_quote($host).'$'; } } } src/Illuminate/Http/Middleware/SetCacheHeaders.php 0000644 00000004550 15107327041 0016122 0 ustar 00 <?php namespace Illuminate\Http\Middleware; use Closure; use Illuminate\Support\Carbon; use Illuminate\Support\Str; use Symfony\Component\HttpFoundation\BinaryFileResponse; class SetCacheHeaders { /** * Specify the options for the middleware. * * @param array|string $options * @return string */ public static function using($options) { if (is_string($options)) { return static::class.':'.$options; } return collect($options) ->map(fn ($value, $key) => is_int($key) ? $value : "{$key}={$value}") ->map(fn ($value) => Str::finish($value, ';')) ->pipe(fn ($options) => rtrim(static::class.':'.$options->implode(''), ';')); } /** * Add cache related HTTP headers. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string|array $options * @return \Symfony\Component\HttpFoundation\Response * * @throws \InvalidArgumentException */ public function handle($request, Closure $next, $options = []) { $response = $next($request); if (! $request->isMethodCacheable() || (! $response->getContent() && ! $response instanceof BinaryFileResponse)) { return $response; } if (is_string($options)) { $options = $this->parseOptions($options); } if (isset($options['etag']) && $options['etag'] === true) { $options['etag'] = $response->getEtag() ?? md5($response->getContent()); } if (isset($options['last_modified'])) { if (is_numeric($options['last_modified'])) { $options['last_modified'] = Carbon::createFromTimestamp($options['last_modified']); } else { $options['last_modified'] = Carbon::parse($options['last_modified']); } } $response->setCache($options); $response->isNotModified($request); return $response; } /** * Parse the given header options. * * @param string $options * @return array */ protected function parseOptions($options) { return collect(explode(';', rtrim($options, ';')))->mapWithKeys(function ($option) { $data = explode('=', $option, 2); return [$data[0] => $data[1] ?? true]; })->all(); } } src/Illuminate/Http/Middleware/HandleCors.php 0000644 00000005303 15107327041 0015166 0 ustar 00 <?php namespace Illuminate\Http\Middleware; use Closure; use Fruitcake\Cors\CorsService; use Illuminate\Contracts\Container\Container; use Illuminate\Http\Request; class HandleCors { /** * The container instance. * * @var \Illuminate\Contracts\Container\Container */ protected $container; /** * The CORS service instance. * * @var \Fruitcake\Cors\CorsService */ protected $cors; /** * Create a new middleware instance. * * @param \Illuminate\Contracts\Container\Container $container * @param \Fruitcake\Cors\CorsService $cors * @return void */ public function __construct(Container $container, CorsService $cors) { $this->container = $container; $this->cors = $cors; } /** * Handle the incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return \Illuminate\Http\Response */ public function handle($request, Closure $next) { if (! $this->hasMatchingPath($request)) { return $next($request); } $this->cors->setOptions($this->container['config']->get('cors', [])); if ($this->cors->isPreflightRequest($request)) { $response = $this->cors->handlePreflightRequest($request); $this->cors->varyHeader($response, 'Access-Control-Request-Method'); return $response; } $response = $next($request); if ($request->getMethod() === 'OPTIONS') { $this->cors->varyHeader($response, 'Access-Control-Request-Method'); } return $this->cors->addActualRequestHeaders($response, $request); } /** * Get the path from the configuration to determine if the CORS service should run. * * @param \Illuminate\Http\Request $request * @return bool */ protected function hasMatchingPath(Request $request): bool { $paths = $this->getPathsByHost($request->getHost()); foreach ($paths as $path) { if ($path !== '/') { $path = trim($path, '/'); } if ($request->fullUrlIs($path) || $request->is($path)) { return true; } } return false; } /** * Get the CORS paths for the given host. * * @param string $host * @return array */ protected function getPathsByHost(string $host) { $paths = $this->container['config']->get('cors.paths', []); if (isset($paths[$host])) { return $paths[$host]; } return array_filter($paths, function ($path) { return is_string($path); }); } } src/Illuminate/Http/Middleware/CheckResponseForModifications.php 0000644 00000001033 15107327041 0021054 0 ustar 00 <?php namespace Illuminate\Http\Middleware; use Closure; use Symfony\Component\HttpFoundation\Response; class CheckResponseForModifications { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $response = $next($request); if ($response instanceof Response) { $response->isNotModified($request); } return $response; } } src/Illuminate/Http/Middleware/FrameGuard.php 0000644 00000000763 15107327041 0015166 0 ustar 00 <?php namespace Illuminate\Http\Middleware; use Closure; class FrameGuard { /** * Handle the given request and get the response. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return \Symfony\Component\HttpFoundation\Response */ public function handle($request, Closure $next) { $response = $next($request); $response->headers->set('X-Frame-Options', 'SAMEORIGIN', false); return $response; } } src/Illuminate/Http/Response.php 0000644 00000006033 15107327041 0012666 0 ustar 00 <?php namespace Illuminate\Http; use ArrayObject; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Contracts\Support\Renderable; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; use JsonSerializable; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; use Symfony\Component\HttpFoundation\ResponseHeaderBag; class Response extends SymfonyResponse { use ResponseTrait, Macroable { Macroable::__call as macroCall; } /** * Create a new HTTP response. * * @param mixed $content * @param int $status * @param array $headers * @return void * * @throws \InvalidArgumentException */ public function __construct($content = '', $status = 200, array $headers = []) { $this->headers = new ResponseHeaderBag($headers); $this->setContent($content); $this->setStatusCode($status); $this->setProtocolVersion('1.0'); } /** * Set the content on the response. * * @param mixed $content * @return $this * * @throws \InvalidArgumentException */ public function setContent(mixed $content): static { $this->original = $content; // If the content is "JSONable" we will set the appropriate header and convert // the content to JSON. This is useful when returning something like models // from routes that will be automatically transformed to their JSON form. if ($this->shouldBeJson($content)) { $this->header('Content-Type', 'application/json'); $content = $this->morphToJson($content); if ($content === false) { throw new InvalidArgumentException(json_last_error_msg()); } } // If this content implements the "Renderable" interface then we will call the // render method on the object so we will avoid any "__toString" exceptions // that might be thrown and have their errors obscured by PHP's handling. elseif ($content instanceof Renderable) { $content = $content->render(); } parent::setContent($content); return $this; } /** * Determine if the given content should be turned into JSON. * * @param mixed $content * @return bool */ protected function shouldBeJson($content) { return $content instanceof Arrayable || $content instanceof Jsonable || $content instanceof ArrayObject || $content instanceof JsonSerializable || is_array($content); } /** * Morph the given content into JSON. * * @param mixed $content * @return string */ protected function morphToJson($content) { if ($content instanceof Jsonable) { return $content->toJson(); } elseif ($content instanceof Arrayable) { return json_encode($content->toArray()); } return json_encode($content); } } src/Illuminate/Http/UploadedFile.php 0000644 00000010011 15107327041 0013414 0 ustar 00 <?php namespace Illuminate\Http; use Illuminate\Container\Container; use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory; use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Http\Testing\FileFactory; use Illuminate\Support\Arr; use Illuminate\Support\Traits\Macroable; use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile; class UploadedFile extends SymfonyUploadedFile { use FileHelpers, Macroable; /** * Begin creating a new file fake. * * @return \Illuminate\Http\Testing\FileFactory */ public static function fake() { return new FileFactory; } /** * Store the uploaded file on a filesystem disk. * * @param string $path * @param array|string $options * @return string|false */ public function store($path = '', $options = []) { return $this->storeAs($path, $this->hashName(), $this->parseOptions($options)); } /** * Store the uploaded file on a filesystem disk with public visibility. * * @param string $path * @param array|string $options * @return string|false */ public function storePublicly($path = '', $options = []) { $options = $this->parseOptions($options); $options['visibility'] = 'public'; return $this->storeAs($path, $this->hashName(), $options); } /** * Store the uploaded file on a filesystem disk with public visibility. * * @param string $path * @param string $name * @param array|string $options * @return string|false */ public function storePubliclyAs($path, $name = null, $options = []) { if (is_null($name) || is_array($name)) { [$path, $name, $options] = ['', $path, $name ?? []]; } $options = $this->parseOptions($options); $options['visibility'] = 'public'; return $this->storeAs($path, $name, $options); } /** * Store the uploaded file on a filesystem disk. * * @param string $path * @param string|array $name * @param array|string $options * @return string|false */ public function storeAs($path, $name = null, $options = []) { if (is_null($name) || is_array($name)) { [$path, $name, $options] = ['', $path, $name ?? []]; } $options = $this->parseOptions($options); $disk = Arr::pull($options, 'disk'); return Container::getInstance()->make(FilesystemFactory::class)->disk($disk)->putFileAs( $path, $this, $name, $options ); } /** * Get the contents of the uploaded file. * * @return false|string * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function get() { if (! $this->isValid()) { throw new FileNotFoundException("File does not exist at path {$this->getPathname()}."); } return file_get_contents($this->getPathname()); } /** * Get the file's extension supplied by the client. * * @return string */ public function clientExtension() { return $this->guessClientExtension(); } /** * Create a new file instance from a base instance. * * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file * @param bool $test * @return static */ public static function createFromBase(SymfonyUploadedFile $file, $test = false) { return $file instanceof static ? $file : new static( $file->getPathname(), $file->getClientOriginalName(), $file->getClientMimeType(), $file->getError(), $test ); } /** * Parse and format the given options. * * @param array|string $options * @return array */ protected function parseOptions($options) { if (is_string($options)) { $options = ['disk' => $options]; } return $options; } } src/Illuminate/Http/JsonResponse.php 0000644 00000006624 15107327041 0013526 0 ustar 00 <?php namespace Illuminate\Http; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; use JsonSerializable; use Symfony\Component\HttpFoundation\JsonResponse as BaseJsonResponse; class JsonResponse extends BaseJsonResponse { use ResponseTrait, Macroable { Macroable::__call as macroCall; } /** * Create a new JSON response instance. * * @param mixed $data * @param int $status * @param array $headers * @param int $options * @param bool $json * @return void */ public function __construct($data = null, $status = 200, $headers = [], $options = 0, $json = false) { $this->encodingOptions = $options; parent::__construct($data, $status, $headers, $json); } /** * {@inheritdoc} * * @return static */ public static function fromJsonString(?string $data = null, int $status = 200, array $headers = []): static { return new static($data, $status, $headers, 0, true); } /** * Sets the JSONP callback. * * @param string|null $callback * @return $this */ public function withCallback($callback = null) { return $this->setCallback($callback); } /** * Get the json_decoded data from the response. * * @param bool $assoc * @param int $depth * @return mixed */ public function getData($assoc = false, $depth = 512) { return json_decode($this->data, $assoc, $depth); } /** * {@inheritdoc} * * @return static */ public function setData($data = []): static { $this->original = $data; // Ensure json_last_error() is cleared... json_decode('[]'); $this->data = match (true) { $data instanceof Jsonable => $data->toJson($this->encodingOptions), $data instanceof JsonSerializable => json_encode($data->jsonSerialize(), $this->encodingOptions), $data instanceof Arrayable => json_encode($data->toArray(), $this->encodingOptions), default => json_encode($data, $this->encodingOptions), }; if (! $this->hasValidJson(json_last_error())) { throw new InvalidArgumentException(json_last_error_msg()); } return $this->update(); } /** * Determine if an error occurred during JSON encoding. * * @param int $jsonError * @return bool */ protected function hasValidJson($jsonError) { if ($jsonError === JSON_ERROR_NONE) { return true; } return $this->hasEncodingOption(JSON_PARTIAL_OUTPUT_ON_ERROR) && in_array($jsonError, [ JSON_ERROR_RECURSION, JSON_ERROR_INF_OR_NAN, JSON_ERROR_UNSUPPORTED_TYPE, ]); } /** * {@inheritdoc} * * @return static */ public function setEncodingOptions($options): static { $this->encodingOptions = (int) $options; return $this->setData($this->getData()); } /** * Determine if a JSON encoding option is set. * * @param int $option * @return bool */ public function hasEncodingOption($option) { return (bool) ($this->encodingOptions & $option); } } src/Illuminate/Http/ResponseTrait.php 0000644 00000007405 15107327041 0013676 0 ustar 00 <?php namespace Illuminate\Http; use Illuminate\Http\Exceptions\HttpResponseException; use Symfony\Component\HttpFoundation\HeaderBag; use Throwable; trait ResponseTrait { /** * The original content of the response. * * @var mixed */ public $original; /** * The exception that triggered the error response (if applicable). * * @var \Throwable|null */ public $exception; /** * Get the status code for the response. * * @return int */ public function status() { return $this->getStatusCode(); } /** * Get the status text for the response. * * @return string */ public function statusText() { return $this->statusText; } /** * Get the content of the response. * * @return string */ public function content() { return $this->getContent(); } /** * Get the original response content. * * @return mixed */ public function getOriginalContent() { $original = $this->original; return $original instanceof self ? $original->{__FUNCTION__}() : $original; } /** * Set a header on the Response. * * @param string $key * @param array|string $values * @param bool $replace * @return $this */ public function header($key, $values, $replace = true) { $this->headers->set($key, $values, $replace); return $this; } /** * Add an array of headers to the response. * * @param \Symfony\Component\HttpFoundation\HeaderBag|array $headers * @return $this */ public function withHeaders($headers) { if ($headers instanceof HeaderBag) { $headers = $headers->all(); } foreach ($headers as $key => $value) { $this->headers->set($key, $value); } return $this; } /** * Add a cookie to the response. * * @param \Symfony\Component\HttpFoundation\Cookie|mixed $cookie * @return $this */ public function cookie($cookie) { return $this->withCookie(...func_get_args()); } /** * Add a cookie to the response. * * @param \Symfony\Component\HttpFoundation\Cookie|mixed $cookie * @return $this */ public function withCookie($cookie) { if (is_string($cookie) && function_exists('cookie')) { $cookie = cookie(...func_get_args()); } $this->headers->setCookie($cookie); return $this; } /** * Expire a cookie when sending the response. * * @param \Symfony\Component\HttpFoundation\Cookie|mixed $cookie * @param string|null $path * @param string|null $domain * @return $this */ public function withoutCookie($cookie, $path = null, $domain = null) { if (is_string($cookie) && function_exists('cookie')) { $cookie = cookie($cookie, null, -2628000, $path, $domain); } $this->headers->setCookie($cookie); return $this; } /** * Get the callback of the response. * * @return string|null */ public function getCallback() { return $this->callback ?? null; } /** * Set the exception to attach to the response. * * @param \Throwable $e * @return $this */ public function withException(Throwable $e) { $this->exception = $e; return $this; } /** * Throws the response in a HttpResponseException instance. * * @return void * * @throws \Illuminate\Http\Exceptions\HttpResponseException */ public function throwResponse() { throw new HttpResponseException($this); } } src/Illuminate/Http/Testing/FileFactory.php 0000644 00000005220 15107327041 0014711 0 ustar 00 <?php namespace Illuminate\Http\Testing; use LogicException; class FileFactory { /** * Create a new fake file. * * @param string $name * @param string|int $kilobytes * @param string|null $mimeType * @return \Illuminate\Http\Testing\File */ public function create($name, $kilobytes = 0, $mimeType = null) { if (is_string($kilobytes)) { return $this->createWithContent($name, $kilobytes); } return tap(new File($name, tmpfile()), function ($file) use ($kilobytes, $mimeType) { $file->sizeToReport = $kilobytes * 1024; $file->mimeTypeToReport = $mimeType; }); } /** * Create a new fake file with content. * * @param string $name * @param string $content * @return \Illuminate\Http\Testing\File */ public function createWithContent($name, $content) { $tmpfile = tmpfile(); fwrite($tmpfile, $content); return tap(new File($name, $tmpfile), function ($file) use ($tmpfile) { $file->sizeToReport = fstat($tmpfile)['size']; }); } /** * Create a new fake image. * * @param string $name * @param int $width * @param int $height * @return \Illuminate\Http\Testing\File * * @throws \LogicException */ public function image($name, $width = 10, $height = 10) { return new File($name, $this->generateImage( $width, $height, pathinfo($name, PATHINFO_EXTENSION) )); } /** * Generate a dummy image of the given width and height. * * @param int $width * @param int $height * @param string $extension * @return resource * * @throws \LogicException */ protected function generateImage($width, $height, $extension) { if (! function_exists('imagecreatetruecolor')) { throw new LogicException('GD extension is not installed.'); } return tap(tmpfile(), function ($temp) use ($width, $height, $extension) { ob_start(); $extension = in_array($extension, ['jpeg', 'png', 'gif', 'webp', 'wbmp', 'bmp']) ? strtolower($extension) : 'jpeg'; $image = imagecreatetruecolor($width, $height); if (! function_exists($functionName = "image{$extension}")) { ob_get_clean(); throw new LogicException("{$functionName} function is not defined and image cannot be generated."); } call_user_func($functionName, $image); fwrite($temp, ob_get_clean()); }); } } src/Illuminate/Http/Testing/File.php 0000644 00000005557 15107327041 0013376 0 ustar 00 <?php namespace Illuminate\Http\Testing; use Illuminate\Http\UploadedFile; class File extends UploadedFile { /** * The name of the file. * * @var string */ public $name; /** * The temporary file resource. * * @var resource */ public $tempFile; /** * The "size" to report. * * @var int */ public $sizeToReport; /** * The MIME type to report. * * @var string|null */ public $mimeTypeToReport; /** * Create a new file instance. * * @param string $name * @param resource $tempFile * @return void */ public function __construct($name, $tempFile) { $this->name = $name; $this->tempFile = $tempFile; parent::__construct( $this->tempFilePath(), $name, $this->getMimeType(), null, true ); } /** * Create a new fake file. * * @param string $name * @param string|int $kilobytes * @return \Illuminate\Http\Testing\File */ public static function create($name, $kilobytes = 0) { return (new FileFactory)->create($name, $kilobytes); } /** * Create a new fake file with content. * * @param string $name * @param string $content * @return \Illuminate\Http\Testing\File */ public static function createWithContent($name, $content) { return (new FileFactory)->createWithContent($name, $content); } /** * Create a new fake image. * * @param string $name * @param int $width * @param int $height * @return \Illuminate\Http\Testing\File */ public static function image($name, $width = 10, $height = 10) { return (new FileFactory)->image($name, $width, $height); } /** * Set the "size" of the file in kilobytes. * * @param int $kilobytes * @return $this */ public function size($kilobytes) { $this->sizeToReport = $kilobytes * 1024; return $this; } /** * Get the size of the file. * * @return int */ public function getSize(): int { return $this->sizeToReport ?: parent::getSize(); } /** * Set the "MIME type" for the file. * * @param string $mimeType * @return $this */ public function mimeType($mimeType) { $this->mimeTypeToReport = $mimeType; return $this; } /** * Get the MIME type of the file. * * @return string */ public function getMimeType(): string { return $this->mimeTypeToReport ?: MimeType::from($this->name); } /** * Get the path to the temporary file. * * @return string */ protected function tempFilePath() { return stream_get_meta_data($this->tempFile)['uri']; } } src/Illuminate/Http/Testing/MimeType.php 0000644 00000002625 15107327041 0014241 0 ustar 00 <?php namespace Illuminate\Http\Testing; use Illuminate\Support\Arr; use Symfony\Component\Mime\MimeTypes; class MimeType { /** * The mime types instance. * * @var \Symfony\Component\Mime\MimeTypes|null */ private static $mime; /** * Get the mime types instance. * * @return \Symfony\Component\Mime\MimeTypesInterface */ public static function getMimeTypes() { if (self::$mime === null) { self::$mime = new MimeTypes; } return self::$mime; } /** * Get the MIME type for a file based on the file's extension. * * @param string $filename * @return string */ public static function from($filename) { $extension = pathinfo($filename, PATHINFO_EXTENSION); return self::get($extension); } /** * Get the MIME type for a given extension or return all mimes. * * @param string $extension * @return string */ public static function get($extension) { return Arr::first(self::getMimeTypes()->getMimeTypes($extension)) ?? 'application/octet-stream'; } /** * Search for the extension of a given MIME type. * * @param string $mimeType * @return string|null */ public static function search($mimeType) { return Arr::first(self::getMimeTypes()->getExtensions($mimeType)); } } src/Illuminate/Pipeline/PipelineServiceProvider.php 0000644 00000001456 15107327041 0016523 0 ustar 00 <?php namespace Illuminate\Pipeline; use Illuminate\Contracts\Pipeline\Hub as PipelineHubContract; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; class PipelineServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton( PipelineHubContract::class, Hub::class ); $this->app->bind('pipeline', fn ($app) => new Pipeline($app)); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return [ PipelineHubContract::class, 'pipeline', ]; } } src/Illuminate/Pipeline/composer.json 0000644 00000001470 15107327041 0013727 0 ustar 00 { "name": "illuminate/pipeline", "description": "The Illuminate Pipeline package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/contracts": "^10.0", "illuminate/support": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Pipeline\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Pipeline/LICENSE.md 0000644 00000002063 15107327041 0012610 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Pipeline/Pipeline.php 0000644 00000015340 15107327041 0013464 0 ustar 00 <?php namespace Illuminate\Pipeline; use Closure; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract; use RuntimeException; use Throwable; class Pipeline implements PipelineContract { /** * The container implementation. * * @var \Illuminate\Contracts\Container\Container|null */ protected $container; /** * The object being passed through the pipeline. * * @var mixed */ protected $passable; /** * The array of class pipes. * * @var array */ protected $pipes = []; /** * The method to call on each pipe. * * @var string */ protected $method = 'handle'; /** * Create a new class instance. * * @param \Illuminate\Contracts\Container\Container|null $container * @return void */ public function __construct(Container $container = null) { $this->container = $container; } /** * Set the object being sent through the pipeline. * * @param mixed $passable * @return $this */ public function send($passable) { $this->passable = $passable; return $this; } /** * Set the array of pipes. * * @param array|mixed $pipes * @return $this */ public function through($pipes) { $this->pipes = is_array($pipes) ? $pipes : func_get_args(); return $this; } /** * Push additional pipes onto the pipeline. * * @param array|mixed $pipes * @return $this */ public function pipe($pipes) { array_push($this->pipes, ...(is_array($pipes) ? $pipes : func_get_args())); return $this; } /** * Set the method to call on the pipes. * * @param string $method * @return $this */ public function via($method) { $this->method = $method; return $this; } /** * Run the pipeline with a final destination callback. * * @param \Closure $destination * @return mixed */ public function then(Closure $destination) { $pipeline = array_reduce( array_reverse($this->pipes()), $this->carry(), $this->prepareDestination($destination) ); return $pipeline($this->passable); } /** * Run the pipeline and return the result. * * @return mixed */ public function thenReturn() { return $this->then(function ($passable) { return $passable; }); } /** * Get the final piece of the Closure onion. * * @param \Closure $destination * @return \Closure */ protected function prepareDestination(Closure $destination) { return function ($passable) use ($destination) { try { return $destination($passable); } catch (Throwable $e) { return $this->handleException($passable, $e); } }; } /** * Get a Closure that represents a slice of the application onion. * * @return \Closure */ protected function carry() { return function ($stack, $pipe) { return function ($passable) use ($stack, $pipe) { try { if (is_callable($pipe)) { // If the pipe is a callable, then we will call it directly, but otherwise we // will resolve the pipes out of the dependency container and call it with // the appropriate method and arguments, returning the results back out. return $pipe($passable, $stack); } elseif (! is_object($pipe)) { [$name, $parameters] = $this->parsePipeString($pipe); // If the pipe is a string we will parse the string and resolve the class out // of the dependency injection container. We can then build a callable and // execute the pipe function giving in the parameters that are required. $pipe = $this->getContainer()->make($name); $parameters = array_merge([$passable, $stack], $parameters); } else { // If the pipe is already an object we'll just make a callable and pass it to // the pipe as-is. There is no need to do any extra parsing and formatting // since the object we're given was already a fully instantiated object. $parameters = [$passable, $stack]; } $carry = method_exists($pipe, $this->method) ? $pipe->{$this->method}(...$parameters) : $pipe(...$parameters); return $this->handleCarry($carry); } catch (Throwable $e) { return $this->handleException($passable, $e); } }; }; } /** * Parse full pipe string to get name and parameters. * * @param string $pipe * @return array */ protected function parsePipeString($pipe) { [$name, $parameters] = array_pad(explode(':', $pipe, 2), 2, []); if (is_string($parameters)) { $parameters = explode(',', $parameters); } return [$name, $parameters]; } /** * Get the array of configured pipes. * * @return array */ protected function pipes() { return $this->pipes; } /** * Get the container instance. * * @return \Illuminate\Contracts\Container\Container * * @throws \RuntimeException */ protected function getContainer() { if (! $this->container) { throw new RuntimeException('A container instance has not been passed to the Pipeline.'); } return $this->container; } /** * Set the container instance. * * @param \Illuminate\Contracts\Container\Container $container * @return $this */ public function setContainer(Container $container) { $this->container = $container; return $this; } /** * Handle the value returned from each pipe before passing it to the next. * * @param mixed $carry * @return mixed */ protected function handleCarry($carry) { return $carry; } /** * Handle the given exception. * * @param mixed $passable * @param \Throwable $e * @return mixed * * @throws \Throwable */ protected function handleException($passable, Throwable $e) { throw $e; } } src/Illuminate/Pipeline/Hub.php 0000644 00000004103 15107327041 0012430 0 ustar 00 <?php namespace Illuminate\Pipeline; use Closure; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Pipeline\Hub as HubContract; class Hub implements HubContract { /** * The container implementation. * * @var \Illuminate\Contracts\Container\Container|null */ protected $container; /** * All of the available pipelines. * * @var array */ protected $pipelines = []; /** * Create a new Hub instance. * * @param \Illuminate\Contracts\Container\Container|null $container * @return void */ public function __construct(Container $container = null) { $this->container = $container; } /** * Define the default named pipeline. * * @param \Closure $callback * @return void */ public function defaults(Closure $callback) { return $this->pipeline('default', $callback); } /** * Define a new named pipeline. * * @param string $name * @param \Closure $callback * @return void */ public function pipeline($name, Closure $callback) { $this->pipelines[$name] = $callback; } /** * Send an object through one of the available pipelines. * * @param mixed $object * @param string|null $pipeline * @return mixed */ public function pipe($object, $pipeline = null) { $pipeline = $pipeline ?: 'default'; return call_user_func( $this->pipelines[$pipeline], new Pipeline($this->container), $object ); } /** * Get the container instance used by the hub. * * @return \Illuminate\Contracts\Container\Container */ public function getContainer() { return $this->container; } /** * Set the container instance used by the hub. * * @param \Illuminate\Contracts\Container\Container $container * @return $this */ public function setContainer(Container $container) { $this->container = $container; return $this; } } src/Illuminate/Collections/helpers.php 0000644 00000014022 15107327041 0014066 0 ustar 00 <?php use Illuminate\Support\Arr; use Illuminate\Support\Collection; if (! function_exists('collect')) { /** * Create a collection from the given value. * * @template TKey of array-key * @template TValue * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue>|null $value * @return \Illuminate\Support\Collection<TKey, TValue> */ function collect($value = []) { return new Collection($value); } } if (! function_exists('data_fill')) { /** * Fill in data where it's missing. * * @param mixed $target * @param string|array $key * @param mixed $value * @return mixed */ function data_fill(&$target, $key, $value) { return data_set($target, $key, $value, false); } } if (! function_exists('data_get')) { /** * Get an item from an array or object using "dot" notation. * * @param mixed $target * @param string|array|int|null $key * @param mixed $default * @return mixed */ function data_get($target, $key, $default = null) { if (is_null($key)) { return $target; } $key = is_array($key) ? $key : explode('.', $key); foreach ($key as $i => $segment) { unset($key[$i]); if (is_null($segment)) { return $target; } if ($segment === '*') { if ($target instanceof Collection) { $target = $target->all(); } elseif (! is_iterable($target)) { return value($default); } $result = []; foreach ($target as $item) { $result[] = data_get($item, $key); } return in_array('*', $key) ? Arr::collapse($result) : $result; } if (Arr::accessible($target) && Arr::exists($target, $segment)) { $target = $target[$segment]; } elseif (is_object($target) && isset($target->{$segment})) { $target = $target->{$segment}; } else { return value($default); } } return $target; } } if (! function_exists('data_set')) { /** * Set an item on an array or object using dot notation. * * @param mixed $target * @param string|array $key * @param mixed $value * @param bool $overwrite * @return mixed */ function data_set(&$target, $key, $value, $overwrite = true) { $segments = is_array($key) ? $key : explode('.', $key); if (($segment = array_shift($segments)) === '*') { if (! Arr::accessible($target)) { $target = []; } if ($segments) { foreach ($target as &$inner) { data_set($inner, $segments, $value, $overwrite); } } elseif ($overwrite) { foreach ($target as &$inner) { $inner = $value; } } } elseif (Arr::accessible($target)) { if ($segments) { if (! Arr::exists($target, $segment)) { $target[$segment] = []; } data_set($target[$segment], $segments, $value, $overwrite); } elseif ($overwrite || ! Arr::exists($target, $segment)) { $target[$segment] = $value; } } elseif (is_object($target)) { if ($segments) { if (! isset($target->{$segment})) { $target->{$segment} = []; } data_set($target->{$segment}, $segments, $value, $overwrite); } elseif ($overwrite || ! isset($target->{$segment})) { $target->{$segment} = $value; } } else { $target = []; if ($segments) { data_set($target[$segment], $segments, $value, $overwrite); } elseif ($overwrite) { $target[$segment] = $value; } } return $target; } } if (! function_exists('data_forget')) { /** * Remove / unset an item from an array or object using "dot" notation. * * @param mixed $target * @param string|array|int|null $key * @return mixed */ function data_forget(&$target, $key) { $segments = is_array($key) ? $key : explode('.', $key); if (($segment = array_shift($segments)) === '*' && Arr::accessible($target)) { if ($segments) { foreach ($target as &$inner) { data_forget($inner, $segments); } } } elseif (Arr::accessible($target)) { if ($segments && Arr::exists($target, $segment)) { data_forget($target[$segment], $segments); } else { Arr::forget($target, $segment); } } elseif (is_object($target)) { if ($segments && isset($target->{$segment})) { data_forget($target->{$segment}, $segments); } elseif (isset($target->{$segment})) { unset($target->{$segment}); } } return $target; } } if (! function_exists('head')) { /** * Get the first element of an array. Useful for method chaining. * * @param array $array * @return mixed */ function head($array) { return reset($array); } } if (! function_exists('last')) { /** * Get the last element from an array. * * @param array $array * @return mixed */ function last($array) { return end($array); } } if (! function_exists('value')) { /** * Return the default value of the given value. * * @param mixed $value * @param mixed ...$args * @return mixed */ function value($value, ...$args) { return $value instanceof Closure ? $value(...$args) : $value; } } src/Illuminate/Collections/Enumerable.php 0000644 00000104530 15107327041 0014507 0 ustar 00 <?php namespace Illuminate\Support; use CachingIterator; use Countable; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use IteratorAggregate; use JsonSerializable; use Traversable; /** * @template TKey of array-key * * @template-covariant TValue * * @extends \Illuminate\Contracts\Support\Arrayable<TKey, TValue> * @extends \IteratorAggregate<TKey, TValue> */ interface Enumerable extends Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable { /** * Create a new collection instance if the value isn't one already. * * @template TMakeKey of array-key * @template TMakeValue * * @param \Illuminate\Contracts\Support\Arrayable<TMakeKey, TMakeValue>|iterable<TMakeKey, TMakeValue>|null $items * @return static<TMakeKey, TMakeValue> */ public static function make($items = []); /** * Create a new instance by invoking the callback a given amount of times. * * @param int $number * @param callable|null $callback * @return static */ public static function times($number, callable $callback = null); /** * Create a collection with the given range. * * @param int $from * @param int $to * @return static */ public static function range($from, $to); /** * Wrap the given value in a collection if applicable. * * @template TWrapValue * * @param iterable<array-key, TWrapValue>|TWrapValue $value * @return static<array-key, TWrapValue> */ public static function wrap($value); /** * Get the underlying items from the given collection if applicable. * * @template TUnwrapKey of array-key * @template TUnwrapValue * * @param array<TUnwrapKey, TUnwrapValue>|static<TUnwrapKey, TUnwrapValue> $value * @return array<TUnwrapKey, TUnwrapValue> */ public static function unwrap($value); /** * Create a new instance with no items. * * @return static */ public static function empty(); /** * Get all items in the enumerable. * * @return array */ public function all(); /** * Alias for the "avg" method. * * @param (callable(TValue): float|int)|string|null $callback * @return float|int|null */ public function average($callback = null); /** * Get the median of a given key. * * @param string|array<array-key, string>|null $key * @return float|int|null */ public function median($key = null); /** * Get the mode of a given key. * * @param string|array<array-key, string>|null $key * @return array<int, float|int>|null */ public function mode($key = null); /** * Collapse the items into a single enumerable. * * @return static<int, mixed> */ public function collapse(); /** * Alias for the "contains" method. * * @param (callable(TValue, TKey): bool)|TValue|string $key * @param mixed $operator * @param mixed $value * @return bool */ public function some($key, $operator = null, $value = null); /** * Determine if an item exists, using strict comparison. * * @param (callable(TValue): bool)|TValue|array-key $key * @param TValue|null $value * @return bool */ public function containsStrict($key, $value = null); /** * Get the average value of a given key. * * @param (callable(TValue): float|int)|string|null $callback * @return float|int|null */ public function avg($callback = null); /** * Determine if an item exists in the enumerable. * * @param (callable(TValue, TKey): bool)|TValue|string $key * @param mixed $operator * @param mixed $value * @return bool */ public function contains($key, $operator = null, $value = null); /** * Determine if an item is not contained in the collection. * * @param mixed $key * @param mixed $operator * @param mixed $value * @return bool */ public function doesntContain($key, $operator = null, $value = null); /** * Cross join with the given lists, returning all possible permutations. * * @template TCrossJoinKey * @template TCrossJoinValue * * @param \Illuminate\Contracts\Support\Arrayable<TCrossJoinKey, TCrossJoinValue>|iterable<TCrossJoinKey, TCrossJoinValue> ...$lists * @return static<int, array<int, TValue|TCrossJoinValue>> */ public function crossJoin(...$lists); /** * Dump the collection and end the script. * * @param mixed ...$args * @return never */ public function dd(...$args); /** * Dump the collection. * * @return $this */ public function dump(); /** * Get the items that are not present in the given items. * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items * @return static */ public function diff($items); /** * Get the items that are not present in the given items, using the callback. * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items * @param callable(TValue, TValue): int $callback * @return static */ public function diffUsing($items, callable $callback); /** * Get the items whose keys and values are not present in the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function diffAssoc($items); /** * Get the items whose keys and values are not present in the given items, using the callback. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @param callable(TKey, TKey): int $callback * @return static */ public function diffAssocUsing($items, callable $callback); /** * Get the items whose keys are not present in the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function diffKeys($items); /** * Get the items whose keys are not present in the given items, using the callback. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @param callable(TKey, TKey): int $callback * @return static */ public function diffKeysUsing($items, callable $callback); /** * Retrieve duplicate items. * * @param (callable(TValue): bool)|string|null $callback * @param bool $strict * @return static */ public function duplicates($callback = null, $strict = false); /** * Retrieve duplicate items using strict comparison. * * @param (callable(TValue): bool)|string|null $callback * @return static */ public function duplicatesStrict($callback = null); /** * Execute a callback over each item. * * @param callable(TValue, TKey): mixed $callback * @return $this */ public function each(callable $callback); /** * Execute a callback over each nested chunk of items. * * @param callable $callback * @return static */ public function eachSpread(callable $callback); /** * Determine if all items pass the given truth test. * * @param (callable(TValue, TKey): bool)|TValue|string $key * @param mixed $operator * @param mixed $value * @return bool */ public function every($key, $operator = null, $value = null); /** * Get all items except for those with the specified keys. * * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey> $keys * @return static */ public function except($keys); /** * Run a filter over each of the items. * * @param (callable(TValue): bool)|null $callback * @return static */ public function filter(callable $callback = null); /** * Apply the callback if the given "value" is (or resolves to) truthy. * * @template TWhenReturnType as null * * @param bool $value * @param (callable($this): TWhenReturnType)|null $callback * @param (callable($this): TWhenReturnType)|null $default * @return $this|TWhenReturnType */ public function when($value, callable $callback = null, callable $default = null); /** * Apply the callback if the collection is empty. * * @template TWhenEmptyReturnType * * @param (callable($this): TWhenEmptyReturnType) $callback * @param (callable($this): TWhenEmptyReturnType)|null $default * @return $this|TWhenEmptyReturnType */ public function whenEmpty(callable $callback, callable $default = null); /** * Apply the callback if the collection is not empty. * * @template TWhenNotEmptyReturnType * * @param callable($this): TWhenNotEmptyReturnType $callback * @param (callable($this): TWhenNotEmptyReturnType)|null $default * @return $this|TWhenNotEmptyReturnType */ public function whenNotEmpty(callable $callback, callable $default = null); /** * Apply the callback if the given "value" is (or resolves to) truthy. * * @template TUnlessReturnType * * @param bool $value * @param (callable($this): TUnlessReturnType) $callback * @param (callable($this): TUnlessReturnType)|null $default * @return $this|TUnlessReturnType */ public function unless($value, callable $callback, callable $default = null); /** * Apply the callback unless the collection is empty. * * @template TUnlessEmptyReturnType * * @param callable($this): TUnlessEmptyReturnType $callback * @param (callable($this): TUnlessEmptyReturnType)|null $default * @return $this|TUnlessEmptyReturnType */ public function unlessEmpty(callable $callback, callable $default = null); /** * Apply the callback unless the collection is not empty. * * @template TUnlessNotEmptyReturnType * * @param callable($this): TUnlessNotEmptyReturnType $callback * @param (callable($this): TUnlessNotEmptyReturnType)|null $default * @return $this|TUnlessNotEmptyReturnType */ public function unlessNotEmpty(callable $callback, callable $default = null); /** * Filter items by the given key value pair. * * @param string $key * @param mixed $operator * @param mixed $value * @return static */ public function where($key, $operator = null, $value = null); /** * Filter items where the value for the given key is null. * * @param string|null $key * @return static */ public function whereNull($key = null); /** * Filter items where the value for the given key is not null. * * @param string|null $key * @return static */ public function whereNotNull($key = null); /** * Filter items by the given key value pair using strict comparison. * * @param string $key * @param mixed $value * @return static */ public function whereStrict($key, $value); /** * Filter items by the given key value pair. * * @param string $key * @param \Illuminate\Contracts\Support\Arrayable|iterable $values * @param bool $strict * @return static */ public function whereIn($key, $values, $strict = false); /** * Filter items by the given key value pair using strict comparison. * * @param string $key * @param \Illuminate\Contracts\Support\Arrayable|iterable $values * @return static */ public function whereInStrict($key, $values); /** * Filter items such that the value of the given key is between the given values. * * @param string $key * @param \Illuminate\Contracts\Support\Arrayable|iterable $values * @return static */ public function whereBetween($key, $values); /** * Filter items such that the value of the given key is not between the given values. * * @param string $key * @param \Illuminate\Contracts\Support\Arrayable|iterable $values * @return static */ public function whereNotBetween($key, $values); /** * Filter items by the given key value pair. * * @param string $key * @param \Illuminate\Contracts\Support\Arrayable|iterable $values * @param bool $strict * @return static */ public function whereNotIn($key, $values, $strict = false); /** * Filter items by the given key value pair using strict comparison. * * @param string $key * @param \Illuminate\Contracts\Support\Arrayable|iterable $values * @return static */ public function whereNotInStrict($key, $values); /** * Filter the items, removing any items that don't match the given type(s). * * @template TWhereInstanceOf * * @param class-string<TWhereInstanceOf>|array<array-key, class-string<TWhereInstanceOf>> $type * @return static<TKey, TWhereInstanceOf> */ public function whereInstanceOf($type); /** * Get the first item from the enumerable passing the given truth test. * * @template TFirstDefault * * @param (callable(TValue,TKey): bool)|null $callback * @param TFirstDefault|(\Closure(): TFirstDefault) $default * @return TValue|TFirstDefault */ public function first(callable $callback = null, $default = null); /** * Get the first item by the given key value pair. * * @param string $key * @param mixed $operator * @param mixed $value * @return TValue|null */ public function firstWhere($key, $operator = null, $value = null); /** * Get a flattened array of the items in the collection. * * @param int $depth * @return static */ public function flatten($depth = INF); /** * Flip the values with their keys. * * @return static<TValue, TKey> */ public function flip(); /** * Get an item from the collection by key. * * @template TGetDefault * * @param TKey $key * @param TGetDefault|(\Closure(): TGetDefault) $default * @return TValue|TGetDefault */ public function get($key, $default = null); /** * Group an associative array by a field or using a callback. * * @param (callable(TValue, TKey): array-key)|array|string $groupBy * @param bool $preserveKeys * @return static<array-key, static<array-key, TValue>> */ public function groupBy($groupBy, $preserveKeys = false); /** * Key an associative array by a field or using a callback. * * @param (callable(TValue, TKey): array-key)|array|string $keyBy * @return static<array-key, TValue> */ public function keyBy($keyBy); /** * Determine if an item exists in the collection by key. * * @param TKey|array<array-key, TKey> $key * @return bool */ public function has($key); /** * Determine if any of the keys exist in the collection. * * @param mixed $key * @return bool */ public function hasAny($key); /** * Concatenate values of a given key as a string. * * @param callable|string $value * @param string|null $glue * @return string */ public function implode($value, $glue = null); /** * Intersect the collection with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function intersect($items); /** * Intersect the collection with the given items by key. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function intersectByKeys($items); /** * Determine if the collection is empty or not. * * @return bool */ public function isEmpty(); /** * Determine if the collection is not empty. * * @return bool */ public function isNotEmpty(); /** * Determine if the collection contains a single item. * * @return bool */ public function containsOneItem(); /** * Join all items from the collection using a string. The final items can use a separate glue string. * * @param string $glue * @param string $finalGlue * @return string */ public function join($glue, $finalGlue = ''); /** * Get the keys of the collection items. * * @return static<int, TKey> */ public function keys(); /** * Get the last item from the collection. * * @template TLastDefault * * @param (callable(TValue, TKey): bool)|null $callback * @param TLastDefault|(\Closure(): TLastDefault) $default * @return TValue|TLastDefault */ public function last(callable $callback = null, $default = null); /** * Run a map over each of the items. * * @template TMapValue * * @param callable(TValue, TKey): TMapValue $callback * @return static<TKey, TMapValue> */ public function map(callable $callback); /** * Run a map over each nested chunk of items. * * @param callable $callback * @return static */ public function mapSpread(callable $callback); /** * Run a dictionary map over the items. * * The callback should return an associative array with a single key/value pair. * * @template TMapToDictionaryKey of array-key * @template TMapToDictionaryValue * * @param callable(TValue, TKey): array<TMapToDictionaryKey, TMapToDictionaryValue> $callback * @return static<TMapToDictionaryKey, array<int, TMapToDictionaryValue>> */ public function mapToDictionary(callable $callback); /** * Run a grouping map over the items. * * The callback should return an associative array with a single key/value pair. * * @template TMapToGroupsKey of array-key * @template TMapToGroupsValue * * @param callable(TValue, TKey): array<TMapToGroupsKey, TMapToGroupsValue> $callback * @return static<TMapToGroupsKey, static<int, TMapToGroupsValue>> */ public function mapToGroups(callable $callback); /** * Run an associative map over each of the items. * * The callback should return an associative array with a single key/value pair. * * @template TMapWithKeysKey of array-key * @template TMapWithKeysValue * * @param callable(TValue, TKey): array<TMapWithKeysKey, TMapWithKeysValue> $callback * @return static<TMapWithKeysKey, TMapWithKeysValue> */ public function mapWithKeys(callable $callback); /** * Map a collection and flatten the result by a single level. * * @template TFlatMapKey of array-key * @template TFlatMapValue * * @param callable(TValue, TKey): (\Illuminate\Support\Collection<TFlatMapKey, TFlatMapValue>|array<TFlatMapKey, TFlatMapValue>) $callback * @return static<TFlatMapKey, TFlatMapValue> */ public function flatMap(callable $callback); /** * Map the values into a new class. * * @template TMapIntoValue * * @param class-string<TMapIntoValue> $class * @return static<TKey, TMapIntoValue> */ public function mapInto($class); /** * Merge the collection with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function merge($items); /** * Recursively merge the collection with the given items. * * @template TMergeRecursiveValue * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TMergeRecursiveValue>|iterable<TKey, TMergeRecursiveValue> $items * @return static<TKey, TValue|TMergeRecursiveValue> */ public function mergeRecursive($items); /** * Create a collection by using this collection for keys and another for its values. * * @template TCombineValue * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TCombineValue>|iterable<array-key, TCombineValue> $values * @return static<TValue, TCombineValue> */ public function combine($values); /** * Union the collection with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function union($items); /** * Get the min value of a given key. * * @param (callable(TValue):mixed)|string|null $callback * @return mixed */ public function min($callback = null); /** * Get the max value of a given key. * * @param (callable(TValue):mixed)|string|null $callback * @return mixed */ public function max($callback = null); /** * Create a new collection consisting of every n-th element. * * @param int $step * @param int $offset * @return static */ public function nth($step, $offset = 0); /** * Get the items with the specified keys. * * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string $keys * @return static */ public function only($keys); /** * "Paginate" the collection by slicing it into a smaller collection. * * @param int $page * @param int $perPage * @return static */ public function forPage($page, $perPage); /** * Partition the collection into two arrays using the given callback or key. * * @param (callable(TValue, TKey): bool)|TValue|string $key * @param mixed $operator * @param mixed $value * @return static<int<0, 1>, static<TKey, TValue>> */ public function partition($key, $operator = null, $value = null); /** * Push all of the given items onto the collection. * * @param iterable<array-key, TValue> $source * @return static */ public function concat($source); /** * Get one or a specified number of items randomly from the collection. * * @param int|null $number * @return static<int, TValue>|TValue * * @throws \InvalidArgumentException */ public function random($number = null); /** * Reduce the collection to a single value. * * @template TReduceInitial * @template TReduceReturnType * * @param callable(TReduceInitial|TReduceReturnType, TValue, TKey): TReduceReturnType $callback * @param TReduceInitial $initial * @return TReduceReturnType */ public function reduce(callable $callback, $initial = null); /** * Reduce the collection to multiple aggregate values. * * @param callable $callback * @param mixed ...$initial * @return array * * @throws \UnexpectedValueException */ public function reduceSpread(callable $callback, ...$initial); /** * Replace the collection items with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function replace($items); /** * Recursively replace the collection items with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function replaceRecursive($items); /** * Reverse items order. * * @return static */ public function reverse(); /** * Search the collection for a given value and return the corresponding key if successful. * * @param TValue|callable(TValue,TKey): bool $value * @param bool $strict * @return TKey|bool */ public function search($value, $strict = false); /** * Shuffle the items in the collection. * * @param int|null $seed * @return static */ public function shuffle($seed = null); /** * Create chunks representing a "sliding window" view of the items in the collection. * * @param int $size * @param int $step * @return static<int, static> */ public function sliding($size = 2, $step = 1); /** * Skip the first {$count} items. * * @param int $count * @return static */ public function skip($count); /** * Skip items in the collection until the given condition is met. * * @param TValue|callable(TValue,TKey): bool $value * @return static */ public function skipUntil($value); /** * Skip items in the collection while the given condition is met. * * @param TValue|callable(TValue,TKey): bool $value * @return static */ public function skipWhile($value); /** * Get a slice of items from the enumerable. * * @param int $offset * @param int|null $length * @return static */ public function slice($offset, $length = null); /** * Split a collection into a certain number of groups. * * @param int $numberOfGroups * @return static<int, static> */ public function split($numberOfGroups); /** * Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception. * * @param (callable(TValue, TKey): bool)|string $key * @param mixed $operator * @param mixed $value * @return TValue * * @throws \Illuminate\Support\ItemNotFoundException * @throws \Illuminate\Support\MultipleItemsFoundException */ public function sole($key = null, $operator = null, $value = null); /** * Get the first item in the collection but throw an exception if no matching items exist. * * @param (callable(TValue, TKey): bool)|string $key * @param mixed $operator * @param mixed $value * @return TValue * * @throws \Illuminate\Support\ItemNotFoundException */ public function firstOrFail($key = null, $operator = null, $value = null); /** * Chunk the collection into chunks of the given size. * * @param int $size * @return static<int, static> */ public function chunk($size); /** * Chunk the collection into chunks with a callback. * * @param callable(TValue, TKey, static<int, TValue>): bool $callback * @return static<int, static<int, TValue>> */ public function chunkWhile(callable $callback); /** * Split a collection into a certain number of groups, and fill the first groups completely. * * @param int $numberOfGroups * @return static<int, static> */ public function splitIn($numberOfGroups); /** * Sort through each item with a callback. * * @param (callable(TValue, TValue): int)|null|int $callback * @return static */ public function sort($callback = null); /** * Sort items in descending order. * * @param int $options * @return static */ public function sortDesc($options = SORT_REGULAR); /** * Sort the collection using the given callback. * * @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback * @param int $options * @param bool $descending * @return static */ public function sortBy($callback, $options = SORT_REGULAR, $descending = false); /** * Sort the collection in descending order using the given callback. * * @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback * @param int $options * @return static */ public function sortByDesc($callback, $options = SORT_REGULAR); /** * Sort the collection keys. * * @param int $options * @param bool $descending * @return static */ public function sortKeys($options = SORT_REGULAR, $descending = false); /** * Sort the collection keys in descending order. * * @param int $options * @return static */ public function sortKeysDesc($options = SORT_REGULAR); /** * Sort the collection keys using a callback. * * @param callable(TKey, TKey): int $callback * @return static */ public function sortKeysUsing(callable $callback); /** * Get the sum of the given values. * * @param (callable(TValue): mixed)|string|null $callback * @return mixed */ public function sum($callback = null); /** * Take the first or last {$limit} items. * * @param int $limit * @return static */ public function take($limit); /** * Take items in the collection until the given condition is met. * * @param TValue|callable(TValue,TKey): bool $value * @return static */ public function takeUntil($value); /** * Take items in the collection while the given condition is met. * * @param TValue|callable(TValue,TKey): bool $value * @return static */ public function takeWhile($value); /** * Pass the collection to the given callback and then return it. * * @param callable(TValue): mixed $callback * @return $this */ public function tap(callable $callback); /** * Pass the enumerable to the given callback and return the result. * * @template TPipeReturnType * * @param callable($this): TPipeReturnType $callback * @return TPipeReturnType */ public function pipe(callable $callback); /** * Pass the collection into a new class. * * @template TPipeIntoValue * * @param class-string<TPipeIntoValue> $class * @return TPipeIntoValue */ public function pipeInto($class); /** * Pass the collection through a series of callable pipes and return the result. * * @param array<callable> $pipes * @return mixed */ public function pipeThrough($pipes); /** * Get the values of a given key. * * @param string|array<array-key, string> $value * @param string|null $key * @return static<int, mixed> */ public function pluck($value, $key = null); /** * Create a collection of all elements that do not pass a given truth test. * * @param (callable(TValue, TKey): bool)|bool|TValue $callback * @return static */ public function reject($callback = true); /** * Convert a flatten "dot" notation array into an expanded array. * * @return static */ public function undot(); /** * Return only unique items from the collection array. * * @param (callable(TValue, TKey): mixed)|string|null $key * @param bool $strict * @return static */ public function unique($key = null, $strict = false); /** * Return only unique items from the collection array using strict comparison. * * @param (callable(TValue, TKey): mixed)|string|null $key * @return static */ public function uniqueStrict($key = null); /** * Reset the keys on the underlying array. * * @return static<int, TValue> */ public function values(); /** * Pad collection to the specified length with a value. * * @template TPadValue * * @param int $size * @param TPadValue $value * @return static<int, TValue|TPadValue> */ public function pad($size, $value); /** * Get the values iterator. * * @return \Traversable<TKey, TValue> */ public function getIterator(): Traversable; /** * Count the number of items in the collection. * * @return int */ public function count(): int; /** * Count the number of items in the collection by a field or using a callback. * * @param (callable(TValue, TKey): array-key)|string|null $countBy * @return static<array-key, int> */ public function countBy($countBy = null); /** * Zip the collection together with one or more arrays. * * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); * => [[1, 4], [2, 5], [3, 6]] * * @template TZipValue * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TZipValue>|iterable<array-key, TZipValue> ...$items * @return static<int, static<int, TValue|TZipValue>> */ public function zip($items); /** * Collect the values into a collection. * * @return \Illuminate\Support\Collection<TKey, TValue> */ public function collect(); /** * Get the collection of items as a plain array. * * @return array<TKey, mixed> */ public function toArray(); /** * Convert the object into something JSON serializable. * * @return mixed */ public function jsonSerialize(): mixed; /** * Get the collection of items as JSON. * * @param int $options * @return string */ public function toJson($options = 0); /** * Get a CachingIterator instance. * * @param int $flags * @return \CachingIterator */ public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING); /** * Convert the collection to its string representation. * * @return string */ public function __toString(); /** * Indicate that the model's string representation should be escaped when __toString is invoked. * * @param bool $escape * @return $this */ public function escapeWhenCastingToString($escape = true); /** * Add a method to the list of proxied methods. * * @param string $method * @return void */ public static function proxy($method); /** * Dynamically access collection proxies. * * @param string $key * @return mixed * * @throws \Exception */ public function __get($key); } src/Illuminate/Collections/Collection.php 0000644 00000132704 15107327041 0014527 0 ustar 00 <?php namespace Illuminate\Support; use ArrayAccess; use ArrayIterator; use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString; use Illuminate\Support\Traits\EnumeratesValues; use Illuminate\Support\Traits\Macroable; use stdClass; use Traversable; /** * @template TKey of array-key * * @template-covariant TValue * * @implements \ArrayAccess<TKey, TValue> * @implements \Illuminate\Support\Enumerable<TKey, TValue> */ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerable { /** * @use \Illuminate\Support\Traits\EnumeratesValues<TKey, TValue> */ use EnumeratesValues, Macroable; /** * The items contained in the collection. * * @var array<TKey, TValue> */ protected $items = []; /** * Create a new collection. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue>|null $items * @return void */ public function __construct($items = []) { $this->items = $this->getArrayableItems($items); } /** * Create a collection with the given range. * * @param int $from * @param int $to * @return static<int, int> */ public static function range($from, $to) { return new static(range($from, $to)); } /** * Get all of the items in the collection. * * @return array<TKey, TValue> */ public function all() { return $this->items; } /** * Get a lazy collection for the items in this collection. * * @return \Illuminate\Support\LazyCollection<TKey, TValue> */ public function lazy() { return new LazyCollection($this->items); } /** * Get the average value of a given key. * * @param (callable(TValue): float|int)|string|null $callback * @return float|int|null */ public function avg($callback = null) { $callback = $this->valueRetriever($callback); $items = $this ->map(fn ($value) => $callback($value)) ->filter(fn ($value) => ! is_null($value)); if ($count = $items->count()) { return $items->sum() / $count; } } /** * Get the median of a given key. * * @param string|array<array-key, string>|null $key * @return float|int|null */ public function median($key = null) { $values = (isset($key) ? $this->pluck($key) : $this) ->filter(fn ($item) => ! is_null($item)) ->sort()->values(); $count = $values->count(); if ($count === 0) { return; } $middle = (int) ($count / 2); if ($count % 2) { return $values->get($middle); } return (new static([ $values->get($middle - 1), $values->get($middle), ]))->average(); } /** * Get the mode of a given key. * * @param string|array<array-key, string>|null $key * @return array<int, float|int>|null */ public function mode($key = null) { if ($this->count() === 0) { return; } $collection = isset($key) ? $this->pluck($key) : $this; $counts = new static; $collection->each(fn ($value) => $counts[$value] = isset($counts[$value]) ? $counts[$value] + 1 : 1); $sorted = $counts->sort(); $highestValue = $sorted->last(); return $sorted->filter(fn ($value) => $value == $highestValue) ->sort()->keys()->all(); } /** * Collapse the collection of items into a single array. * * @return static<int, mixed> */ public function collapse() { return new static(Arr::collapse($this->items)); } /** * Determine if an item exists in the collection. * * @param (callable(TValue, TKey): bool)|TValue|string $key * @param mixed $operator * @param mixed $value * @return bool */ public function contains($key, $operator = null, $value = null) { if (func_num_args() === 1) { if ($this->useAsCallable($key)) { $placeholder = new stdClass; return $this->first($key, $placeholder) !== $placeholder; } return in_array($key, $this->items); } return $this->contains($this->operatorForWhere(...func_get_args())); } /** * Determine if an item exists, using strict comparison. * * @param (callable(TValue): bool)|TValue|array-key $key * @param TValue|null $value * @return bool */ public function containsStrict($key, $value = null) { if (func_num_args() === 2) { return $this->contains(fn ($item) => data_get($item, $key) === $value); } if ($this->useAsCallable($key)) { return ! is_null($this->first($key)); } return in_array($key, $this->items, true); } /** * Determine if an item is not contained in the collection. * * @param mixed $key * @param mixed $operator * @param mixed $value * @return bool */ public function doesntContain($key, $operator = null, $value = null) { return ! $this->contains(...func_get_args()); } /** * Cross join with the given lists, returning all possible permutations. * * @template TCrossJoinKey * @template TCrossJoinValue * * @param \Illuminate\Contracts\Support\Arrayable<TCrossJoinKey, TCrossJoinValue>|iterable<TCrossJoinKey, TCrossJoinValue> ...$lists * @return static<int, array<int, TValue|TCrossJoinValue>> */ public function crossJoin(...$lists) { return new static(Arr::crossJoin( $this->items, ...array_map([$this, 'getArrayableItems'], $lists) )); } /** * Get the items in the collection that are not present in the given items. * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items * @return static */ public function diff($items) { return new static(array_diff($this->items, $this->getArrayableItems($items))); } /** * Get the items in the collection that are not present in the given items, using the callback. * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items * @param callable(TValue, TValue): int $callback * @return static */ public function diffUsing($items, callable $callback) { return new static(array_udiff($this->items, $this->getArrayableItems($items), $callback)); } /** * Get the items in the collection whose keys and values are not present in the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function diffAssoc($items) { return new static(array_diff_assoc($this->items, $this->getArrayableItems($items))); } /** * Get the items in the collection whose keys and values are not present in the given items, using the callback. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @param callable(TKey, TKey): int $callback * @return static */ public function diffAssocUsing($items, callable $callback) { return new static(array_diff_uassoc($this->items, $this->getArrayableItems($items), $callback)); } /** * Get the items in the collection whose keys are not present in the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function diffKeys($items) { return new static(array_diff_key($this->items, $this->getArrayableItems($items))); } /** * Get the items in the collection whose keys are not present in the given items, using the callback. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @param callable(TKey, TKey): int $callback * @return static */ public function diffKeysUsing($items, callable $callback) { return new static(array_diff_ukey($this->items, $this->getArrayableItems($items), $callback)); } /** * Retrieve duplicate items from the collection. * * @param (callable(TValue): bool)|string|null $callback * @param bool $strict * @return static */ public function duplicates($callback = null, $strict = false) { $items = $this->map($this->valueRetriever($callback)); $uniqueItems = $items->unique(null, $strict); $compare = $this->duplicateComparator($strict); $duplicates = new static; foreach ($items as $key => $value) { if ($uniqueItems->isNotEmpty() && $compare($value, $uniqueItems->first())) { $uniqueItems->shift(); } else { $duplicates[$key] = $value; } } return $duplicates; } /** * Retrieve duplicate items from the collection using strict comparison. * * @param (callable(TValue): bool)|string|null $callback * @return static */ public function duplicatesStrict($callback = null) { return $this->duplicates($callback, true); } /** * Get the comparison function to detect duplicates. * * @param bool $strict * @return callable(TValue, TValue): bool */ protected function duplicateComparator($strict) { if ($strict) { return fn ($a, $b) => $a === $b; } return fn ($a, $b) => $a == $b; } /** * Get all items except for those with the specified keys. * * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string $keys * @return static */ public function except($keys) { if (is_null($keys)) { return new static($this->items); } if ($keys instanceof Enumerable) { $keys = $keys->all(); } elseif (! is_array($keys)) { $keys = func_get_args(); } return new static(Arr::except($this->items, $keys)); } /** * Run a filter over each of the items. * * @param (callable(TValue, TKey): bool)|null $callback * @return static */ public function filter(callable $callback = null) { if ($callback) { return new static(Arr::where($this->items, $callback)); } return new static(array_filter($this->items)); } /** * Get the first item from the collection passing the given truth test. * * @template TFirstDefault * * @param (callable(TValue, TKey): bool)|null $callback * @param TFirstDefault|(\Closure(): TFirstDefault) $default * @return TValue|TFirstDefault */ public function first(callable $callback = null, $default = null) { return Arr::first($this->items, $callback, $default); } /** * Get a flattened array of the items in the collection. * * @param int $depth * @return static<int, mixed> */ public function flatten($depth = INF) { return new static(Arr::flatten($this->items, $depth)); } /** * Flip the items in the collection. * * @return static<TValue, TKey> */ public function flip() { return new static(array_flip($this->items)); } /** * Remove an item from the collection by key. * * \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TKey>|TKey $keys * * @return $this */ public function forget($keys) { foreach ($this->getArrayableItems($keys) as $key) { $this->offsetUnset($key); } return $this; } /** * Get an item from the collection by key. * * @template TGetDefault * * @param TKey $key * @param TGetDefault|(\Closure(): TGetDefault) $default * @return TValue|TGetDefault */ public function get($key, $default = null) { if (array_key_exists($key, $this->items)) { return $this->items[$key]; } return value($default); } /** * Get an item from the collection by key or add it to collection if it does not exist. * * @template TGetOrPutValue * * @param mixed $key * @param TGetOrPutValue|(\Closure(): TGetOrPutValue) $value * @return TValue|TGetOrPutValue */ public function getOrPut($key, $value) { if (array_key_exists($key, $this->items)) { return $this->items[$key]; } $this->offsetSet($key, $value = value($value)); return $value; } /** * Group an associative array by a field or using a callback. * * @param (callable(TValue, TKey): array-key)|array|string $groupBy * @param bool $preserveKeys * @return static<array-key, static<array-key, TValue>> */ public function groupBy($groupBy, $preserveKeys = false) { if (! $this->useAsCallable($groupBy) && is_array($groupBy)) { $nextGroups = $groupBy; $groupBy = array_shift($nextGroups); } $groupBy = $this->valueRetriever($groupBy); $results = []; foreach ($this->items as $key => $value) { $groupKeys = $groupBy($value, $key); if (! is_array($groupKeys)) { $groupKeys = [$groupKeys]; } foreach ($groupKeys as $groupKey) { $groupKey = match (true) { is_bool($groupKey) => (int) $groupKey, $groupKey instanceof \BackedEnum => $groupKey->value, $groupKey instanceof \Stringable => (string) $groupKey, default => $groupKey, }; if (! array_key_exists($groupKey, $results)) { $results[$groupKey] = new static; } $results[$groupKey]->offsetSet($preserveKeys ? $key : null, $value); } } $result = new static($results); if (! empty($nextGroups)) { return $result->map->groupBy($nextGroups, $preserveKeys); } return $result; } /** * Key an associative array by a field or using a callback. * * @param (callable(TValue, TKey): array-key)|array|string $keyBy * @return static<array-key, TValue> */ public function keyBy($keyBy) { $keyBy = $this->valueRetriever($keyBy); $results = []; foreach ($this->items as $key => $item) { $resolvedKey = $keyBy($item, $key); if (is_object($resolvedKey)) { $resolvedKey = (string) $resolvedKey; } $results[$resolvedKey] = $item; } return new static($results); } /** * Determine if an item exists in the collection by key. * * @param TKey|array<array-key, TKey> $key * @return bool */ public function has($key) { $keys = is_array($key) ? $key : func_get_args(); foreach ($keys as $value) { if (! array_key_exists($value, $this->items)) { return false; } } return true; } /** * Determine if any of the keys exist in the collection. * * @param mixed $key * @return bool */ public function hasAny($key) { if ($this->isEmpty()) { return false; } $keys = is_array($key) ? $key : func_get_args(); foreach ($keys as $value) { if ($this->has($value)) { return true; } } return false; } /** * Concatenate values of a given key as a string. * * @param callable|string $value * @param string|null $glue * @return string */ public function implode($value, $glue = null) { if ($this->useAsCallable($value)) { return implode($glue ?? '', $this->map($value)->all()); } $first = $this->first(); if (is_array($first) || (is_object($first) && ! $first instanceof Stringable)) { return implode($glue ?? '', $this->pluck($value)->all()); } return implode($value ?? '', $this->items); } /** * Intersect the collection with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function intersect($items) { return new static(array_intersect($this->items, $this->getArrayableItems($items))); } /** * Intersect the collection with the given items, using the callback. * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items * @param callable(TValue, TValue): int $callback * @return static */ public function intersectUsing($items, callable $callback) { return new static(array_uintersect($this->items, $this->getArrayableItems($items), $callback)); } /** * Intersect the collection with the given items with additional index check. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function intersectAssoc($items) { return new static(array_intersect_assoc($this->items, $this->getArrayableItems($items))); } /** * Intersect the collection with the given items with additional index check, using the callback. * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items * @param callable(TValue, TValue): int $callback * @return static */ public function intersectAssocUsing($items, callable $callback) { return new static(array_intersect_uassoc($this->items, $this->getArrayableItems($items), $callback)); } /** * Intersect the collection with the given items by key. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function intersectByKeys($items) { return new static(array_intersect_key( $this->items, $this->getArrayableItems($items) )); } /** * Determine if the collection is empty or not. * * @return bool */ public function isEmpty() { return empty($this->items); } /** * Determine if the collection contains a single item. * * @return bool */ public function containsOneItem() { return $this->count() === 1; } /** * Join all items from the collection using a string. The final items can use a separate glue string. * * @param string $glue * @param string $finalGlue * @return string */ public function join($glue, $finalGlue = '') { if ($finalGlue === '') { return $this->implode($glue); } $count = $this->count(); if ($count === 0) { return ''; } if ($count === 1) { return $this->last(); } $collection = new static($this->items); $finalItem = $collection->pop(); return $collection->implode($glue).$finalGlue.$finalItem; } /** * Get the keys of the collection items. * * @return static<int, TKey> */ public function keys() { return new static(array_keys($this->items)); } /** * Get the last item from the collection. * * @template TLastDefault * * @param (callable(TValue, TKey): bool)|null $callback * @param TLastDefault|(\Closure(): TLastDefault) $default * @return TValue|TLastDefault */ public function last(callable $callback = null, $default = null) { return Arr::last($this->items, $callback, $default); } /** * Get the values of a given key. * * @param string|int|array<array-key, string> $value * @param string|null $key * @return static<array-key, mixed> */ public function pluck($value, $key = null) { return new static(Arr::pluck($this->items, $value, $key)); } /** * Run a map over each of the items. * * @template TMapValue * * @param callable(TValue, TKey): TMapValue $callback * @return static<TKey, TMapValue> */ public function map(callable $callback) { return new static(Arr::map($this->items, $callback)); } /** * Run a dictionary map over the items. * * The callback should return an associative array with a single key/value pair. * * @template TMapToDictionaryKey of array-key * @template TMapToDictionaryValue * * @param callable(TValue, TKey): array<TMapToDictionaryKey, TMapToDictionaryValue> $callback * @return static<TMapToDictionaryKey, array<int, TMapToDictionaryValue>> */ public function mapToDictionary(callable $callback) { $dictionary = []; foreach ($this->items as $key => $item) { $pair = $callback($item, $key); $key = key($pair); $value = reset($pair); if (! isset($dictionary[$key])) { $dictionary[$key] = []; } $dictionary[$key][] = $value; } return new static($dictionary); } /** * Run an associative map over each of the items. * * The callback should return an associative array with a single key/value pair. * * @template TMapWithKeysKey of array-key * @template TMapWithKeysValue * * @param callable(TValue, TKey): array<TMapWithKeysKey, TMapWithKeysValue> $callback * @return static<TMapWithKeysKey, TMapWithKeysValue> */ public function mapWithKeys(callable $callback) { return new static(Arr::mapWithKeys($this->items, $callback)); } /** * Merge the collection with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function merge($items) { return new static(array_merge($this->items, $this->getArrayableItems($items))); } /** * Recursively merge the collection with the given items. * * @template TMergeRecursiveValue * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TMergeRecursiveValue>|iterable<TKey, TMergeRecursiveValue> $items * @return static<TKey, TValue|TMergeRecursiveValue> */ public function mergeRecursive($items) { return new static(array_merge_recursive($this->items, $this->getArrayableItems($items))); } /** * Create a collection by using this collection for keys and another for its values. * * @template TCombineValue * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TCombineValue>|iterable<array-key, TCombineValue> $values * @return static<TValue, TCombineValue> */ public function combine($values) { return new static(array_combine($this->all(), $this->getArrayableItems($values))); } /** * Union the collection with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function union($items) { return new static($this->items + $this->getArrayableItems($items)); } /** * Create a new collection consisting of every n-th element. * * @param int $step * @param int $offset * @return static */ public function nth($step, $offset = 0) { $new = []; $position = 0; foreach ($this->slice($offset)->items as $item) { if ($position % $step === 0) { $new[] = $item; } $position++; } return new static($new); } /** * Get the items with the specified keys. * * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string|null $keys * @return static */ public function only($keys) { if (is_null($keys)) { return new static($this->items); } if ($keys instanceof Enumerable) { $keys = $keys->all(); } $keys = is_array($keys) ? $keys : func_get_args(); return new static(Arr::only($this->items, $keys)); } /** * Get and remove the last N items from the collection. * * @param int $count * @return static<int, TValue>|TValue|null */ public function pop($count = 1) { if ($count === 1) { return array_pop($this->items); } if ($this->isEmpty()) { return new static; } $results = []; $collectionCount = $this->count(); foreach (range(1, min($count, $collectionCount)) as $item) { array_push($results, array_pop($this->items)); } return new static($results); } /** * Push an item onto the beginning of the collection. * * @param TValue $value * @param TKey $key * @return $this */ public function prepend($value, $key = null) { $this->items = Arr::prepend($this->items, ...func_get_args()); return $this; } /** * Push one or more items onto the end of the collection. * * @param TValue ...$values * @return $this */ public function push(...$values) { foreach ($values as $value) { $this->items[] = $value; } return $this; } /** * Push all of the given items onto the collection. * * @param iterable<array-key, TValue> $source * @return static */ public function concat($source) { $result = new static($this); foreach ($source as $item) { $result->push($item); } return $result; } /** * Get and remove an item from the collection. * * @template TPullDefault * * @param TKey $key * @param TPullDefault|(\Closure(): TPullDefault) $default * @return TValue|TPullDefault */ public function pull($key, $default = null) { return Arr::pull($this->items, $key, $default); } /** * Put an item in the collection by key. * * @param TKey $key * @param TValue $value * @return $this */ public function put($key, $value) { $this->offsetSet($key, $value); return $this; } /** * Get one or a specified number of items randomly from the collection. * * @param (callable(self<TKey, TValue>): int)|int|null $number * @param bool $preserveKeys * @return static<int, TValue>|TValue * * @throws \InvalidArgumentException */ public function random($number = null, $preserveKeys = false) { if (is_null($number)) { return Arr::random($this->items); } if (is_callable($number)) { return new static(Arr::random($this->items, $number($this), $preserveKeys)); } return new static(Arr::random($this->items, $number, $preserveKeys)); } /** * Replace the collection items with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function replace($items) { return new static(array_replace($this->items, $this->getArrayableItems($items))); } /** * Recursively replace the collection items with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function replaceRecursive($items) { return new static(array_replace_recursive($this->items, $this->getArrayableItems($items))); } /** * Reverse items order. * * @return static */ public function reverse() { return new static(array_reverse($this->items, true)); } /** * Search the collection for a given value and return the corresponding key if successful. * * @param TValue|(callable(TValue,TKey): bool) $value * @param bool $strict * @return TKey|false */ public function search($value, $strict = false) { if (! $this->useAsCallable($value)) { return array_search($value, $this->items, $strict); } foreach ($this->items as $key => $item) { if ($value($item, $key)) { return $key; } } return false; } /** * Get and remove the first N items from the collection. * * @param int $count * @return static<int, TValue>|TValue|null */ public function shift($count = 1) { if ($count === 1) { return array_shift($this->items); } if ($this->isEmpty()) { return new static; } $results = []; $collectionCount = $this->count(); foreach (range(1, min($count, $collectionCount)) as $item) { array_push($results, array_shift($this->items)); } return new static($results); } /** * Shuffle the items in the collection. * * @param int|null $seed * @return static */ public function shuffle($seed = null) { return new static(Arr::shuffle($this->items, $seed)); } /** * Create chunks representing a "sliding window" view of the items in the collection. * * @param int $size * @param int $step * @return static<int, static> */ public function sliding($size = 2, $step = 1) { $chunks = floor(($this->count() - $size) / $step) + 1; return static::times($chunks, fn ($number) => $this->slice(($number - 1) * $step, $size)); } /** * Skip the first {$count} items. * * @param int $count * @return static */ public function skip($count) { return $this->slice($count); } /** * Skip items in the collection until the given condition is met. * * @param TValue|callable(TValue,TKey): bool $value * @return static */ public function skipUntil($value) { return new static($this->lazy()->skipUntil($value)->all()); } /** * Skip items in the collection while the given condition is met. * * @param TValue|callable(TValue,TKey): bool $value * @return static */ public function skipWhile($value) { return new static($this->lazy()->skipWhile($value)->all()); } /** * Slice the underlying collection array. * * @param int $offset * @param int|null $length * @return static */ public function slice($offset, $length = null) { return new static(array_slice($this->items, $offset, $length, true)); } /** * Split a collection into a certain number of groups. * * @param int $numberOfGroups * @return static<int, static> */ public function split($numberOfGroups) { if ($this->isEmpty()) { return new static; } $groups = new static; $groupSize = floor($this->count() / $numberOfGroups); $remain = $this->count() % $numberOfGroups; $start = 0; for ($i = 0; $i < $numberOfGroups; $i++) { $size = $groupSize; if ($i < $remain) { $size++; } if ($size) { $groups->push(new static(array_slice($this->items, $start, $size))); $start += $size; } } return $groups; } /** * Split a collection into a certain number of groups, and fill the first groups completely. * * @param int $numberOfGroups * @return static<int, static> */ public function splitIn($numberOfGroups) { return $this->chunk(ceil($this->count() / $numberOfGroups)); } /** * Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception. * * @param (callable(TValue, TKey): bool)|string $key * @param mixed $operator * @param mixed $value * @return TValue * * @throws \Illuminate\Support\ItemNotFoundException * @throws \Illuminate\Support\MultipleItemsFoundException */ public function sole($key = null, $operator = null, $value = null) { $filter = func_num_args() > 1 ? $this->operatorForWhere(...func_get_args()) : $key; $items = $this->unless($filter == null)->filter($filter); $count = $items->count(); if ($count === 0) { throw new ItemNotFoundException; } if ($count > 1) { throw new MultipleItemsFoundException($count); } return $items->first(); } /** * Get the first item in the collection but throw an exception if no matching items exist. * * @param (callable(TValue, TKey): bool)|string $key * @param mixed $operator * @param mixed $value * @return TValue * * @throws \Illuminate\Support\ItemNotFoundException */ public function firstOrFail($key = null, $operator = null, $value = null) { $filter = func_num_args() > 1 ? $this->operatorForWhere(...func_get_args()) : $key; $placeholder = new stdClass(); $item = $this->first($filter, $placeholder); if ($item === $placeholder) { throw new ItemNotFoundException; } return $item; } /** * Chunk the collection into chunks of the given size. * * @param int $size * @return static<int, static> */ public function chunk($size) { if ($size <= 0) { return new static; } $chunks = []; foreach (array_chunk($this->items, $size, true) as $chunk) { $chunks[] = new static($chunk); } return new static($chunks); } /** * Chunk the collection into chunks with a callback. * * @param callable(TValue, TKey, static<int, TValue>): bool $callback * @return static<int, static<int, TValue>> */ public function chunkWhile(callable $callback) { return new static( $this->lazy()->chunkWhile($callback)->mapInto(static::class) ); } /** * Sort through each item with a callback. * * @param (callable(TValue, TValue): int)|null|int $callback * @return static */ public function sort($callback = null) { $items = $this->items; $callback && is_callable($callback) ? uasort($items, $callback) : asort($items, $callback ?? SORT_REGULAR); return new static($items); } /** * Sort items in descending order. * * @param int $options * @return static */ public function sortDesc($options = SORT_REGULAR) { $items = $this->items; arsort($items, $options); return new static($items); } /** * Sort the collection using the given callback. * * @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback * @param int $options * @param bool $descending * @return static */ public function sortBy($callback, $options = SORT_REGULAR, $descending = false) { if (is_array($callback) && ! is_callable($callback)) { return $this->sortByMany($callback); } $results = []; $callback = $this->valueRetriever($callback); // First we will loop through the items and get the comparator from a callback // function which we were given. Then, we will sort the returned values and // grab all the corresponding values for the sorted keys from this array. foreach ($this->items as $key => $value) { $results[$key] = $callback($value, $key); } $descending ? arsort($results, $options) : asort($results, $options); // Once we have sorted all of the keys in the array, we will loop through them // and grab the corresponding model so we can set the underlying items list // to the sorted version. Then we'll just return the collection instance. foreach (array_keys($results) as $key) { $results[$key] = $this->items[$key]; } return new static($results); } /** * Sort the collection using multiple comparisons. * * @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}> $comparisons * @return static */ protected function sortByMany(array $comparisons = []) { $items = $this->items; uasort($items, function ($a, $b) use ($comparisons) { foreach ($comparisons as $comparison) { $comparison = Arr::wrap($comparison); $prop = $comparison[0]; $ascending = Arr::get($comparison, 1, true) === true || Arr::get($comparison, 1, true) === 'asc'; if (! is_string($prop) && is_callable($prop)) { $result = $prop($a, $b); } else { $values = [data_get($a, $prop), data_get($b, $prop)]; if (! $ascending) { $values = array_reverse($values); } $result = $values[0] <=> $values[1]; } if ($result === 0) { continue; } return $result; } }); return new static($items); } /** * Sort the collection in descending order using the given callback. * * @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback * @param int $options * @return static */ public function sortByDesc($callback, $options = SORT_REGULAR) { return $this->sortBy($callback, $options, true); } /** * Sort the collection keys. * * @param int $options * @param bool $descending * @return static */ public function sortKeys($options = SORT_REGULAR, $descending = false) { $items = $this->items; $descending ? krsort($items, $options) : ksort($items, $options); return new static($items); } /** * Sort the collection keys in descending order. * * @param int $options * @return static */ public function sortKeysDesc($options = SORT_REGULAR) { return $this->sortKeys($options, true); } /** * Sort the collection keys using a callback. * * @param callable(TKey, TKey): int $callback * @return static */ public function sortKeysUsing(callable $callback) { $items = $this->items; uksort($items, $callback); return new static($items); } /** * Splice a portion of the underlying collection array. * * @param int $offset * @param int|null $length * @param array<array-key, TValue> $replacement * @return static */ public function splice($offset, $length = null, $replacement = []) { if (func_num_args() === 1) { return new static(array_splice($this->items, $offset)); } return new static(array_splice($this->items, $offset, $length, $this->getArrayableItems($replacement))); } /** * Take the first or last {$limit} items. * * @param int $limit * @return static */ public function take($limit) { if ($limit < 0) { return $this->slice($limit, abs($limit)); } return $this->slice(0, $limit); } /** * Take items in the collection until the given condition is met. * * @param TValue|callable(TValue,TKey): bool $value * @return static */ public function takeUntil($value) { return new static($this->lazy()->takeUntil($value)->all()); } /** * Take items in the collection while the given condition is met. * * @param TValue|callable(TValue,TKey): bool $value * @return static */ public function takeWhile($value) { return new static($this->lazy()->takeWhile($value)->all()); } /** * Transform each item in the collection using a callback. * * @param callable(TValue, TKey): TValue $callback * @return $this */ public function transform(callable $callback) { $this->items = $this->map($callback)->all(); return $this; } /** * Flatten a multi-dimensional associative array with dots. * * @return static */ public function dot() { return new static(Arr::dot($this->all())); } /** * Convert a flatten "dot" notation array into an expanded array. * * @return static */ public function undot() { return new static(Arr::undot($this->all())); } /** * Return only unique items from the collection array. * * @param (callable(TValue, TKey): mixed)|string|null $key * @param bool $strict * @return static */ public function unique($key = null, $strict = false) { if (is_null($key) && $strict === false) { return new static(array_unique($this->items, SORT_REGULAR)); } $callback = $this->valueRetriever($key); $exists = []; return $this->reject(function ($item, $key) use ($callback, $strict, &$exists) { if (in_array($id = $callback($item, $key), $exists, $strict)) { return true; } $exists[] = $id; }); } /** * Reset the keys on the underlying array. * * @return static<int, TValue> */ public function values() { return new static(array_values($this->items)); } /** * Zip the collection together with one or more arrays. * * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); * => [[1, 4], [2, 5], [3, 6]] * * @template TZipValue * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TZipValue>|iterable<array-key, TZipValue> ...$items * @return static<int, static<int, TValue|TZipValue>> */ public function zip($items) { $arrayableItems = array_map(fn ($items) => $this->getArrayableItems($items), func_get_args()); $params = array_merge([fn () => new static(func_get_args()), $this->items], $arrayableItems); return new static(array_map(...$params)); } /** * Pad collection to the specified length with a value. * * @template TPadValue * * @param int $size * @param TPadValue $value * @return static<int, TValue|TPadValue> */ public function pad($size, $value) { return new static(array_pad($this->items, $size, $value)); } /** * Get an iterator for the items. * * @return \ArrayIterator<TKey, TValue> */ public function getIterator(): Traversable { return new ArrayIterator($this->items); } /** * Count the number of items in the collection. * * @return int */ public function count(): int { return count($this->items); } /** * Count the number of items in the collection by a field or using a callback. * * @param (callable(TValue, TKey): array-key)|string|null $countBy * @return static<array-key, int> */ public function countBy($countBy = null) { return new static($this->lazy()->countBy($countBy)->all()); } /** * Add an item to the collection. * * @param TValue $item * @return $this */ public function add($item) { $this->items[] = $item; return $this; } /** * Get a base Support collection instance from this collection. * * @return \Illuminate\Support\Collection<TKey, TValue> */ public function toBase() { return new self($this); } /** * Determine if an item exists at an offset. * * @param TKey $key * @return bool */ public function offsetExists($key): bool { return isset($this->items[$key]); } /** * Get an item at a given offset. * * @param TKey $key * @return TValue */ public function offsetGet($key): mixed { return $this->items[$key]; } /** * Set the item at a given offset. * * @param TKey|null $key * @param TValue $value * @return void */ public function offsetSet($key, $value): void { if (is_null($key)) { $this->items[] = $value; } else { $this->items[$key] = $value; } } /** * Unset the item at a given offset. * * @param TKey $key * @return void */ public function offsetUnset($key): void { unset($this->items[$key]); } } src/Illuminate/Collections/composer.json 0000644 00000002004 15107327041 0014432 0 ustar 00 { "name": "illuminate/collections", "description": "The Illuminate Collections package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "illuminate/conditionable": "^10.0", "illuminate/contracts": "^10.0", "illuminate/macroable": "^10.0" }, "autoload": { "psr-4": { "Illuminate\\Support\\": "" }, "files": [ "helpers.php" ] }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "symfony/var-dumper": "Required to use the dump method (^6.2)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } src/Illuminate/Collections/Traits/EnumeratesValues.php 0000644 00000075047 15107327041 0017200 0 ustar 00 <?php namespace Illuminate\Support\Traits; use CachingIterator; use Closure; use Exception; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Enumerable; use Illuminate\Support\HigherOrderCollectionProxy; use JsonSerializable; use Symfony\Component\VarDumper\VarDumper; use Traversable; use UnexpectedValueException; use UnitEnum; /** * @template TKey of array-key * * @template-covariant TValue * * @property-read HigherOrderCollectionProxy $average * @property-read HigherOrderCollectionProxy $avg * @property-read HigherOrderCollectionProxy $contains * @property-read HigherOrderCollectionProxy $doesntContain * @property-read HigherOrderCollectionProxy $each * @property-read HigherOrderCollectionProxy $every * @property-read HigherOrderCollectionProxy $filter * @property-read HigherOrderCollectionProxy $first * @property-read HigherOrderCollectionProxy $flatMap * @property-read HigherOrderCollectionProxy $groupBy * @property-read HigherOrderCollectionProxy $keyBy * @property-read HigherOrderCollectionProxy $map * @property-read HigherOrderCollectionProxy $max * @property-read HigherOrderCollectionProxy $min * @property-read HigherOrderCollectionProxy $partition * @property-read HigherOrderCollectionProxy $percentage * @property-read HigherOrderCollectionProxy $reject * @property-read HigherOrderCollectionProxy $skipUntil * @property-read HigherOrderCollectionProxy $skipWhile * @property-read HigherOrderCollectionProxy $some * @property-read HigherOrderCollectionProxy $sortBy * @property-read HigherOrderCollectionProxy $sortByDesc * @property-read HigherOrderCollectionProxy $sum * @property-read HigherOrderCollectionProxy $takeUntil * @property-read HigherOrderCollectionProxy $takeWhile * @property-read HigherOrderCollectionProxy $unique * @property-read HigherOrderCollectionProxy $unless * @property-read HigherOrderCollectionProxy $until * @property-read HigherOrderCollectionProxy $when */ trait EnumeratesValues { use Conditionable; /** * Indicates that the object's string representation should be escaped when __toString is invoked. * * @var bool */ protected $escapeWhenCastingToString = false; /** * The methods that can be proxied. * * @var array<int, string> */ protected static $proxies = [ 'average', 'avg', 'contains', 'doesntContain', 'each', 'every', 'filter', 'first', 'flatMap', 'groupBy', 'keyBy', 'map', 'max', 'min', 'partition', 'percentage', 'reject', 'skipUntil', 'skipWhile', 'some', 'sortBy', 'sortByDesc', 'sum', 'takeUntil', 'takeWhile', 'unique', 'unless', 'until', 'when', ]; /** * Create a new collection instance if the value isn't one already. * * @template TMakeKey of array-key * @template TMakeValue * * @param \Illuminate\Contracts\Support\Arrayable<TMakeKey, TMakeValue>|iterable<TMakeKey, TMakeValue>|null $items * @return static<TMakeKey, TMakeValue> */ public static function make($items = []) { return new static($items); } /** * Wrap the given value in a collection if applicable. * * @template TWrapValue * * @param iterable<array-key, TWrapValue>|TWrapValue $value * @return static<array-key, TWrapValue> */ public static function wrap($value) { return $value instanceof Enumerable ? new static($value) : new static(Arr::wrap($value)); } /** * Get the underlying items from the given collection if applicable. * * @template TUnwrapKey of array-key * @template TUnwrapValue * * @param array<TUnwrapKey, TUnwrapValue>|static<TUnwrapKey, TUnwrapValue> $value * @return array<TUnwrapKey, TUnwrapValue> */ public static function unwrap($value) { return $value instanceof Enumerable ? $value->all() : $value; } /** * Create a new instance with no items. * * @return static */ public static function empty() { return new static([]); } /** * Create a new collection by invoking the callback a given amount of times. * * @template TTimesValue * * @param int $number * @param (callable(int): TTimesValue)|null $callback * @return static<int, TTimesValue> */ public static function times($number, callable $callback = null) { if ($number < 1) { return new static; } return static::range(1, $number) ->unless($callback == null) ->map($callback); } /** * Alias for the "avg" method. * * @param (callable(TValue): float|int)|string|null $callback * @return float|int|null */ public function average($callback = null) { return $this->avg($callback); } /** * Alias for the "contains" method. * * @param (callable(TValue, TKey): bool)|TValue|string $key * @param mixed $operator * @param mixed $value * @return bool */ public function some($key, $operator = null, $value = null) { return $this->contains(...func_get_args()); } /** * Dump the items and end the script. * * @param mixed ...$args * @return never */ public function dd(...$args) { $this->dump(...$args); exit(1); } /** * Dump the items. * * @return $this */ public function dump() { (new Collection(func_get_args())) ->push($this->all()) ->each(function ($item) { VarDumper::dump($item); }); return $this; } /** * Execute a callback over each item. * * @param callable(TValue, TKey): mixed $callback * @return $this */ public function each(callable $callback) { foreach ($this as $key => $item) { if ($callback($item, $key) === false) { break; } } return $this; } /** * Execute a callback over each nested chunk of items. * * @param callable(...mixed): mixed $callback * @return static */ public function eachSpread(callable $callback) { return $this->each(function ($chunk, $key) use ($callback) { $chunk[] = $key; return $callback(...$chunk); }); } /** * Determine if all items pass the given truth test. * * @param (callable(TValue, TKey): bool)|TValue|string $key * @param mixed $operator * @param mixed $value * @return bool */ public function every($key, $operator = null, $value = null) { if (func_num_args() === 1) { $callback = $this->valueRetriever($key); foreach ($this as $k => $v) { if (! $callback($v, $k)) { return false; } } return true; } return $this->every($this->operatorForWhere(...func_get_args())); } /** * Get the first item by the given key value pair. * * @param callable|string $key * @param mixed $operator * @param mixed $value * @return TValue|null */ public function firstWhere($key, $operator = null, $value = null) { return $this->first($this->operatorForWhere(...func_get_args())); } /** * Get a single key's value from the first matching item in the collection. * * @template TValueDefault * * @param string $key * @param TValueDefault|(\Closure(): TValueDefault) $default * @return TValue|TValueDefault */ public function value($key, $default = null) { if ($value = $this->firstWhere($key)) { return data_get($value, $key, $default); } return value($default); } /** * Ensure that every item in the collection is of the expected type. * * @template TEnsureOfType * * @param class-string<TEnsureOfType> $type * @return static<TKey, TEnsureOfType> * * @throws \UnexpectedValueException */ public function ensure($type) { return $this->each(function ($item) use ($type) { $itemType = get_debug_type($item); if ($itemType !== $type && ! $item instanceof $type) { throw new UnexpectedValueException( sprintf("Collection should only include '%s' items, but '%s' found.", $type, $itemType) ); } }); } /** * Determine if the collection is not empty. * * @return bool */ public function isNotEmpty() { return ! $this->isEmpty(); } /** * Run a map over each nested chunk of items. * * @template TMapSpreadValue * * @param callable(mixed): TMapSpreadValue $callback * @return static<TKey, TMapSpreadValue> */ public function mapSpread(callable $callback) { return $this->map(function ($chunk, $key) use ($callback) { $chunk[] = $key; return $callback(...$chunk); }); } /** * Run a grouping map over the items. * * The callback should return an associative array with a single key/value pair. * * @template TMapToGroupsKey of array-key * @template TMapToGroupsValue * * @param callable(TValue, TKey): array<TMapToGroupsKey, TMapToGroupsValue> $callback * @return static<TMapToGroupsKey, static<int, TMapToGroupsValue>> */ public function mapToGroups(callable $callback) { $groups = $this->mapToDictionary($callback); return $groups->map([$this, 'make']); } /** * Map a collection and flatten the result by a single level. * * @template TFlatMapKey of array-key * @template TFlatMapValue * * @param callable(TValue, TKey): (\Illuminate\Support\Collection<TFlatMapKey, TFlatMapValue>|array<TFlatMapKey, TFlatMapValue>) $callback * @return static<TFlatMapKey, TFlatMapValue> */ public function flatMap(callable $callback) { return $this->map($callback)->collapse(); } /** * Map the values into a new class. * * @template TMapIntoValue * * @param class-string<TMapIntoValue> $class * @return static<TKey, TMapIntoValue> */ public function mapInto($class) { return $this->map(fn ($value, $key) => new $class($value, $key)); } /** * Get the min value of a given key. * * @param (callable(TValue):mixed)|string|null $callback * @return mixed */ public function min($callback = null) { $callback = $this->valueRetriever($callback); return $this->map(fn ($value) => $callback($value)) ->filter(fn ($value) => ! is_null($value)) ->reduce(fn ($result, $value) => is_null($result) || $value < $result ? $value : $result); } /** * Get the max value of a given key. * * @param (callable(TValue):mixed)|string|null $callback * @return mixed */ public function max($callback = null) { $callback = $this->valueRetriever($callback); return $this->filter(fn ($value) => ! is_null($value))->reduce(function ($result, $item) use ($callback) { $value = $callback($item); return is_null($result) || $value > $result ? $value : $result; }); } /** * "Paginate" the collection by slicing it into a smaller collection. * * @param int $page * @param int $perPage * @return static */ public function forPage($page, $perPage) { $offset = max(0, ($page - 1) * $perPage); return $this->slice($offset, $perPage); } /** * Partition the collection into two arrays using the given callback or key. * * @param (callable(TValue, TKey): bool)|TValue|string $key * @param TValue|string|null $operator * @param TValue|null $value * @return static<int<0, 1>, static<TKey, TValue>> */ public function partition($key, $operator = null, $value = null) { $passed = []; $failed = []; $callback = func_num_args() === 1 ? $this->valueRetriever($key) : $this->operatorForWhere(...func_get_args()); foreach ($this as $key => $item) { if ($callback($item, $key)) { $passed[$key] = $item; } else { $failed[$key] = $item; } } return new static([new static($passed), new static($failed)]); } /** * Calculate the percentage of items that pass a given truth test. * * @param (callable(TValue, TKey): bool) $callback * @param int $precision * @return float|null */ public function percentage(callable $callback, int $precision = 2) { if ($this->isEmpty()) { return null; } return round( $this->filter($callback)->count() / $this->count() * 100, $precision ); } /** * Get the sum of the given values. * * @param (callable(TValue): mixed)|string|null $callback * @return mixed */ public function sum($callback = null) { $callback = is_null($callback) ? $this->identity() : $this->valueRetriever($callback); return $this->reduce(fn ($result, $item) => $result + $callback($item), 0); } /** * Apply the callback if the collection is empty. * * @template TWhenEmptyReturnType * * @param (callable($this): TWhenEmptyReturnType) $callback * @param (callable($this): TWhenEmptyReturnType)|null $default * @return $this|TWhenEmptyReturnType */ public function whenEmpty(callable $callback, callable $default = null) { return $this->when($this->isEmpty(), $callback, $default); } /** * Apply the callback if the collection is not empty. * * @template TWhenNotEmptyReturnType * * @param callable($this): TWhenNotEmptyReturnType $callback * @param (callable($this): TWhenNotEmptyReturnType)|null $default * @return $this|TWhenNotEmptyReturnType */ public function whenNotEmpty(callable $callback, callable $default = null) { return $this->when($this->isNotEmpty(), $callback, $default); } /** * Apply the callback unless the collection is empty. * * @template TUnlessEmptyReturnType * * @param callable($this): TUnlessEmptyReturnType $callback * @param (callable($this): TUnlessEmptyReturnType)|null $default * @return $this|TUnlessEmptyReturnType */ public function unlessEmpty(callable $callback, callable $default = null) { return $this->whenNotEmpty($callback, $default); } /** * Apply the callback unless the collection is not empty. * * @template TUnlessNotEmptyReturnType * * @param callable($this): TUnlessNotEmptyReturnType $callback * @param (callable($this): TUnlessNotEmptyReturnType)|null $default * @return $this|TUnlessNotEmptyReturnType */ public function unlessNotEmpty(callable $callback, callable $default = null) { return $this->whenEmpty($callback, $default); } /** * Filter items by the given key value pair. * * @param callable|string $key * @param mixed $operator * @param mixed $value * @return static */ public function where($key, $operator = null, $value = null) { return $this->filter($this->operatorForWhere(...func_get_args())); } /** * Filter items where the value for the given key is null. * * @param string|null $key * @return static */ public function whereNull($key = null) { return $this->whereStrict($key, null); } /** * Filter items where the value for the given key is not null. * * @param string|null $key * @return static */ public function whereNotNull($key = null) { return $this->where($key, '!==', null); } /** * Filter items by the given key value pair using strict comparison. * * @param string $key * @param mixed $value * @return static */ public function whereStrict($key, $value) { return $this->where($key, '===', $value); } /** * Filter items by the given key value pair. * * @param string $key * @param \Illuminate\Contracts\Support\Arrayable|iterable $values * @param bool $strict * @return static */ public function whereIn($key, $values, $strict = false) { $values = $this->getArrayableItems($values); return $this->filter(fn ($item) => in_array(data_get($item, $key), $values, $strict)); } /** * Filter items by the given key value pair using strict comparison. * * @param string $key * @param \Illuminate\Contracts\Support\Arrayable|iterable $values * @return static */ public function whereInStrict($key, $values) { return $this->whereIn($key, $values, true); } /** * Filter items such that the value of the given key is between the given values. * * @param string $key * @param \Illuminate\Contracts\Support\Arrayable|iterable $values * @return static */ public function whereBetween($key, $values) { return $this->where($key, '>=', reset($values))->where($key, '<=', end($values)); } /** * Filter items such that the value of the given key is not between the given values. * * @param string $key * @param \Illuminate\Contracts\Support\Arrayable|iterable $values * @return static */ public function whereNotBetween($key, $values) { return $this->filter( fn ($item) => data_get($item, $key) < reset($values) || data_get($item, $key) > end($values) ); } /** * Filter items by the given key value pair. * * @param string $key * @param \Illuminate\Contracts\Support\Arrayable|iterable $values * @param bool $strict * @return static */ public function whereNotIn($key, $values, $strict = false) { $values = $this->getArrayableItems($values); return $this->reject(fn ($item) => in_array(data_get($item, $key), $values, $strict)); } /** * Filter items by the given key value pair using strict comparison. * * @param string $key * @param \Illuminate\Contracts\Support\Arrayable|iterable $values * @return static */ public function whereNotInStrict($key, $values) { return $this->whereNotIn($key, $values, true); } /** * Filter the items, removing any items that don't match the given type(s). * * @template TWhereInstanceOf * * @param class-string<TWhereInstanceOf>|array<array-key, class-string<TWhereInstanceOf>> $type * @return static<TKey, TWhereInstanceOf> */ public function whereInstanceOf($type) { return $this->filter(function ($value) use ($type) { if (is_array($type)) { foreach ($type as $classType) { if ($value instanceof $classType) { return true; } } return false; } return $value instanceof $type; }); } /** * Pass the collection to the given callback and return the result. * * @template TPipeReturnType * * @param callable($this): TPipeReturnType $callback * @return TPipeReturnType */ public function pipe(callable $callback) { return $callback($this); } /** * Pass the collection into a new class. * * @template TPipeIntoValue * * @param class-string<TPipeIntoValue> $class * @return TPipeIntoValue */ public function pipeInto($class) { return new $class($this); } /** * Pass the collection through a series of callable pipes and return the result. * * @param array<callable> $callbacks * @return mixed */ public function pipeThrough($callbacks) { return Collection::make($callbacks)->reduce( fn ($carry, $callback) => $callback($carry), $this, ); } /** * Reduce the collection to a single value. * * @template TReduceInitial * @template TReduceReturnType * * @param callable(TReduceInitial|TReduceReturnType, TValue, TKey): TReduceReturnType $callback * @param TReduceInitial $initial * @return TReduceReturnType */ public function reduce(callable $callback, $initial = null) { $result = $initial; foreach ($this as $key => $value) { $result = $callback($result, $value, $key); } return $result; } /** * Reduce the collection to multiple aggregate values. * * @param callable $callback * @param mixed ...$initial * @return array * * @throws \UnexpectedValueException */ public function reduceSpread(callable $callback, ...$initial) { $result = $initial; foreach ($this as $key => $value) { $result = call_user_func_array($callback, array_merge($result, [$value, $key])); if (! is_array($result)) { throw new UnexpectedValueException(sprintf( "%s::reduceSpread expects reducer to return an array, but got a '%s' instead.", class_basename(static::class), gettype($result) )); } } return $result; } /** * Reduce an associative collection to a single value. * * @template TReduceWithKeysInitial * @template TReduceWithKeysReturnType * * @param callable(TReduceWithKeysInitial|TReduceWithKeysReturnType, TValue, TKey): TReduceWithKeysReturnType $callback * @param TReduceWithKeysInitial $initial * @return TReduceWithKeysReturnType */ public function reduceWithKeys(callable $callback, $initial = null) { return $this->reduce($callback, $initial); } /** * Create a collection of all elements that do not pass a given truth test. * * @param (callable(TValue, TKey): bool)|bool|TValue $callback * @return static */ public function reject($callback = true) { $useAsCallable = $this->useAsCallable($callback); return $this->filter(function ($value, $key) use ($callback, $useAsCallable) { return $useAsCallable ? ! $callback($value, $key) : $value != $callback; }); } /** * Pass the collection to the given callback and then return it. * * @param callable($this): mixed $callback * @return $this */ public function tap(callable $callback) { $callback($this); return $this; } /** * Return only unique items from the collection array. * * @param (callable(TValue, TKey): mixed)|string|null $key * @param bool $strict * @return static */ public function unique($key = null, $strict = false) { $callback = $this->valueRetriever($key); $exists = []; return $this->reject(function ($item, $key) use ($callback, $strict, &$exists) { if (in_array($id = $callback($item, $key), $exists, $strict)) { return true; } $exists[] = $id; }); } /** * Return only unique items from the collection array using strict comparison. * * @param (callable(TValue, TKey): mixed)|string|null $key * @return static */ public function uniqueStrict($key = null) { return $this->unique($key, true); } /** * Collect the values into a collection. * * @return \Illuminate\Support\Collection<TKey, TValue> */ public function collect() { return new Collection($this->all()); } /** * Get the collection of items as a plain array. * * @return array<TKey, mixed> */ public function toArray() { return $this->map(fn ($value) => $value instanceof Arrayable ? $value->toArray() : $value)->all(); } /** * Convert the object into something JSON serializable. * * @return array<TKey, mixed> */ public function jsonSerialize(): array { return array_map(function ($value) { if ($value instanceof JsonSerializable) { return $value->jsonSerialize(); } elseif ($value instanceof Jsonable) { return json_decode($value->toJson(), true); } elseif ($value instanceof Arrayable) { return $value->toArray(); } return $value; }, $this->all()); } /** * Get the collection of items as JSON. * * @param int $options * @return string */ public function toJson($options = 0) { return json_encode($this->jsonSerialize(), $options); } /** * Get a CachingIterator instance. * * @param int $flags * @return \CachingIterator */ public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING) { return new CachingIterator($this->getIterator(), $flags); } /** * Convert the collection to its string representation. * * @return string */ public function __toString() { return $this->escapeWhenCastingToString ? e($this->toJson()) : $this->toJson(); } /** * Indicate that the model's string representation should be escaped when __toString is invoked. * * @param bool $escape * @return $this */ public function escapeWhenCastingToString($escape = true) { $this->escapeWhenCastingToString = $escape; return $this; } /** * Add a method to the list of proxied methods. * * @param string $method * @return void */ public static function proxy($method) { static::$proxies[] = $method; } /** * Dynamically access collection proxies. * * @param string $key * @return mixed * * @throws \Exception */ public function __get($key) { if (! in_array($key, static::$proxies)) { throw new Exception("Property [{$key}] does not exist on this collection instance."); } return new HigherOrderCollectionProxy($this, $key); } /** * Results array of items from Collection or Arrayable. * * @param mixed $items * @return array<TKey, TValue> */ protected function getArrayableItems($items) { if (is_array($items)) { return $items; } return match (true) { $items instanceof Enumerable => $items->all(), $items instanceof Arrayable => $items->toArray(), $items instanceof Traversable => iterator_to_array($items), $items instanceof Jsonable => json_decode($items->toJson(), true), $items instanceof JsonSerializable => (array) $items->jsonSerialize(), $items instanceof UnitEnum => [$items], default => (array) $items, }; } /** * Get an operator checker callback. * * @param callable|string $key * @param string|null $operator * @param mixed $value * @return \Closure */ protected function operatorForWhere($key, $operator = null, $value = null) { if ($this->useAsCallable($key)) { return $key; } if (func_num_args() === 1) { $value = true; $operator = '='; } if (func_num_args() === 2) { $value = $operator; $operator = '='; } return function ($item) use ($key, $operator, $value) { $retrieved = data_get($item, $key); $strings = array_filter([$retrieved, $value], function ($value) { return is_string($value) || (is_object($value) && method_exists($value, '__toString')); }); if (count($strings) < 2 && count(array_filter([$retrieved, $value], 'is_object')) == 1) { return in_array($operator, ['!=', '<>', '!==']); } switch ($operator) { default: case '=': case '==': return $retrieved == $value; case '!=': case '<>': return $retrieved != $value; case '<': return $retrieved < $value; case '>': return $retrieved > $value; case '<=': return $retrieved <= $value; case '>=': return $retrieved >= $value; case '===': return $retrieved === $value; case '!==': return $retrieved !== $value; case '<=>': return $retrieved <=> $value; } }; } /** * Determine if the given value is callable, but not a string. * * @param mixed $value * @return bool */ protected function useAsCallable($value) { return ! is_string($value) && is_callable($value); } /** * Get a value retrieving callback. * * @param callable|string|null $value * @return callable */ protected function valueRetriever($value) { if ($this->useAsCallable($value)) { return $value; } return fn ($item) => data_get($item, $value); } /** * Make a function to check an item's equality. * * @param mixed $value * @return \Closure(mixed): bool */ protected function equality($value) { return fn ($item) => $item === $value; } /** * Make a function using another function, by negating its result. * * @param \Closure $callback * @return \Closure */ protected function negate(Closure $callback) { return fn (...$params) => ! $callback(...$params); } /** * Make a function that returns what's passed to it. * * @return \Closure(TValue): TValue */ protected function identity() { return fn ($value) => $value; } } src/Illuminate/Collections/ItemNotFoundException.php 0000644 00000000166 15107327041 0016662 0 ustar 00 <?php namespace Illuminate\Support; use RuntimeException; class ItemNotFoundException extends RuntimeException { } src/Illuminate/Collections/LazyCollection.php 0000644 00000135251 15107327041 0015367 0 ustar 00 <?php namespace Illuminate\Support; use ArrayIterator; use Closure; use DateTimeInterface; use Generator; use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString; use Illuminate\Support\Traits\EnumeratesValues; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; use IteratorAggregate; use stdClass; use Traversable; /** * @template TKey of array-key * * @template-covariant TValue * * @implements \Illuminate\Support\Enumerable<TKey, TValue> */ class LazyCollection implements CanBeEscapedWhenCastToString, Enumerable { /** * @use \Illuminate\Support\Traits\EnumeratesValues<TKey, TValue> */ use EnumeratesValues, Macroable; /** * The source from which to generate items. * * @var (Closure(): \Generator<TKey, TValue, mixed, void>)|static|array<TKey, TValue> */ public $source; /** * Create a new lazy collection instance. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue>|(Closure(): \Generator<TKey, TValue, mixed, void>)|self<TKey, TValue>|array<TKey, TValue>|null $source * @return void */ public function __construct($source = null) { if ($source instanceof Closure || $source instanceof self) { $this->source = $source; } elseif (is_null($source)) { $this->source = static::empty(); } elseif ($source instanceof Generator) { throw new InvalidArgumentException( 'Generators should not be passed directly to LazyCollection. Instead, pass a generator function.' ); } else { $this->source = $this->getArrayableItems($source); } } /** * Create a new collection instance if the value isn't one already. * * @template TMakeKey of array-key * @template TMakeValue * * @param \Illuminate\Contracts\Support\Arrayable<TMakeKey, TMakeValue>|iterable<TMakeKey, TMakeValue>|(Closure(): \Generator<TMakeKey, TMakeValue, mixed, void>)|self<TMakeKey, TMakeValue>|array<TMakeKey, TMakeValue>|null $items * @return static<TMakeKey, TMakeValue> */ public static function make($items = []) { return new static($items); } /** * Create a collection with the given range. * * @param int $from * @param int $to * @return static<int, int> */ public static function range($from, $to) { return new static(function () use ($from, $to) { if ($from <= $to) { for (; $from <= $to; $from++) { yield $from; } } else { for (; $from >= $to; $from--) { yield $from; } } }); } /** * Get all items in the enumerable. * * @return array<TKey, TValue> */ public function all() { if (is_array($this->source)) { return $this->source; } return iterator_to_array($this->getIterator()); } /** * Eager load all items into a new lazy collection backed by an array. * * @return static */ public function eager() { return new static($this->all()); } /** * Cache values as they're enumerated. * * @return static */ public function remember() { $iterator = $this->getIterator(); $iteratorIndex = 0; $cache = []; return new static(function () use ($iterator, &$iteratorIndex, &$cache) { for ($index = 0; true; $index++) { if (array_key_exists($index, $cache)) { yield $cache[$index][0] => $cache[$index][1]; continue; } if ($iteratorIndex < $index) { $iterator->next(); $iteratorIndex++; } if (! $iterator->valid()) { break; } $cache[$index] = [$iterator->key(), $iterator->current()]; yield $cache[$index][0] => $cache[$index][1]; } }); } /** * Get the average value of a given key. * * @param (callable(TValue): float|int)|string|null $callback * @return float|int|null */ public function avg($callback = null) { return $this->collect()->avg($callback); } /** * Get the median of a given key. * * @param string|array<array-key, string>|null $key * @return float|int|null */ public function median($key = null) { return $this->collect()->median($key); } /** * Get the mode of a given key. * * @param string|array<string>|null $key * @return array<int, float|int>|null */ public function mode($key = null) { return $this->collect()->mode($key); } /** * Collapse the collection of items into a single array. * * @return static<int, mixed> */ public function collapse() { return new static(function () { foreach ($this as $values) { if (is_array($values) || $values instanceof Enumerable) { foreach ($values as $value) { yield $value; } } } }); } /** * Determine if an item exists in the enumerable. * * @param (callable(TValue, TKey): bool)|TValue|string $key * @param mixed $operator * @param mixed $value * @return bool */ public function contains($key, $operator = null, $value = null) { if (func_num_args() === 1 && $this->useAsCallable($key)) { $placeholder = new stdClass; /** @var callable $key */ return $this->first($key, $placeholder) !== $placeholder; } if (func_num_args() === 1) { $needle = $key; foreach ($this as $value) { if ($value == $needle) { return true; } } return false; } return $this->contains($this->operatorForWhere(...func_get_args())); } /** * Determine if an item exists, using strict comparison. * * @param (callable(TValue): bool)|TValue|array-key $key * @param TValue|null $value * @return bool */ public function containsStrict($key, $value = null) { if (func_num_args() === 2) { return $this->contains(fn ($item) => data_get($item, $key) === $value); } if ($this->useAsCallable($key)) { return ! is_null($this->first($key)); } foreach ($this as $item) { if ($item === $key) { return true; } } return false; } /** * Determine if an item is not contained in the enumerable. * * @param mixed $key * @param mixed $operator * @param mixed $value * @return bool */ public function doesntContain($key, $operator = null, $value = null) { return ! $this->contains(...func_get_args()); } /** * Cross join the given iterables, returning all possible permutations. * * @template TCrossJoinKey * @template TCrossJoinValue * * @param \Illuminate\Contracts\Support\Arrayable<TCrossJoinKey, TCrossJoinValue>|iterable<TCrossJoinKey, TCrossJoinValue> ...$arrays * @return static<int, array<int, TValue|TCrossJoinValue>> */ public function crossJoin(...$arrays) { return $this->passthru('crossJoin', func_get_args()); } /** * Count the number of items in the collection by a field or using a callback. * * @param (callable(TValue, TKey): array-key)|string|null $countBy * @return static<array-key, int> */ public function countBy($countBy = null) { $countBy = is_null($countBy) ? $this->identity() : $this->valueRetriever($countBy); return new static(function () use ($countBy) { $counts = []; foreach ($this as $key => $value) { $group = $countBy($value, $key); if (empty($counts[$group])) { $counts[$group] = 0; } $counts[$group]++; } yield from $counts; }); } /** * Get the items that are not present in the given items. * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items * @return static */ public function diff($items) { return $this->passthru('diff', func_get_args()); } /** * Get the items that are not present in the given items, using the callback. * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items * @param callable(TValue, TValue): int $callback * @return static */ public function diffUsing($items, callable $callback) { return $this->passthru('diffUsing', func_get_args()); } /** * Get the items whose keys and values are not present in the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function diffAssoc($items) { return $this->passthru('diffAssoc', func_get_args()); } /** * Get the items whose keys and values are not present in the given items, using the callback. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @param callable(TKey, TKey): int $callback * @return static */ public function diffAssocUsing($items, callable $callback) { return $this->passthru('diffAssocUsing', func_get_args()); } /** * Get the items whose keys are not present in the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function diffKeys($items) { return $this->passthru('diffKeys', func_get_args()); } /** * Get the items whose keys are not present in the given items, using the callback. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @param callable(TKey, TKey): int $callback * @return static */ public function diffKeysUsing($items, callable $callback) { return $this->passthru('diffKeysUsing', func_get_args()); } /** * Retrieve duplicate items. * * @param (callable(TValue): bool)|string|null $callback * @param bool $strict * @return static */ public function duplicates($callback = null, $strict = false) { return $this->passthru('duplicates', func_get_args()); } /** * Retrieve duplicate items using strict comparison. * * @param (callable(TValue): bool)|string|null $callback * @return static */ public function duplicatesStrict($callback = null) { return $this->passthru('duplicatesStrict', func_get_args()); } /** * Get all items except for those with the specified keys. * * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey> $keys * @return static */ public function except($keys) { return $this->passthru('except', func_get_args()); } /** * Run a filter over each of the items. * * @param (callable(TValue, TKey): bool)|null $callback * @return static */ public function filter(callable $callback = null) { if (is_null($callback)) { $callback = fn ($value) => (bool) $value; } return new static(function () use ($callback) { foreach ($this as $key => $value) { if ($callback($value, $key)) { yield $key => $value; } } }); } /** * Get the first item from the enumerable passing the given truth test. * * @template TFirstDefault * * @param (callable(TValue): bool)|null $callback * @param TFirstDefault|(\Closure(): TFirstDefault) $default * @return TValue|TFirstDefault */ public function first(callable $callback = null, $default = null) { $iterator = $this->getIterator(); if (is_null($callback)) { if (! $iterator->valid()) { return value($default); } return $iterator->current(); } foreach ($iterator as $key => $value) { if ($callback($value, $key)) { return $value; } } return value($default); } /** * Get a flattened list of the items in the collection. * * @param int $depth * @return static<int, mixed> */ public function flatten($depth = INF) { $instance = new static(function () use ($depth) { foreach ($this as $item) { if (! is_array($item) && ! $item instanceof Enumerable) { yield $item; } elseif ($depth === 1) { yield from $item; } else { yield from (new static($item))->flatten($depth - 1); } } }); return $instance->values(); } /** * Flip the items in the collection. * * @return static<TValue, TKey> */ public function flip() { return new static(function () { foreach ($this as $key => $value) { yield $value => $key; } }); } /** * Get an item by key. * * @template TGetDefault * * @param TKey|null $key * @param TGetDefault|(\Closure(): TGetDefault) $default * @return TValue|TGetDefault */ public function get($key, $default = null) { if (is_null($key)) { return; } foreach ($this as $outerKey => $outerValue) { if ($outerKey == $key) { return $outerValue; } } return value($default); } /** * Group an associative array by a field or using a callback. * * @param (callable(TValue, TKey): array-key)|array|string $groupBy * @param bool $preserveKeys * @return static<array-key, static<array-key, TValue>> */ public function groupBy($groupBy, $preserveKeys = false) { return $this->passthru('groupBy', func_get_args()); } /** * Key an associative array by a field or using a callback. * * @param (callable(TValue, TKey): array-key)|array|string $keyBy * @return static<array-key, TValue> */ public function keyBy($keyBy) { return new static(function () use ($keyBy) { $keyBy = $this->valueRetriever($keyBy); foreach ($this as $key => $item) { $resolvedKey = $keyBy($item, $key); if (is_object($resolvedKey)) { $resolvedKey = (string) $resolvedKey; } yield $resolvedKey => $item; } }); } /** * Determine if an item exists in the collection by key. * * @param mixed $key * @return bool */ public function has($key) { $keys = array_flip(is_array($key) ? $key : func_get_args()); $count = count($keys); foreach ($this as $key => $value) { if (array_key_exists($key, $keys) && --$count == 0) { return true; } } return false; } /** * Determine if any of the keys exist in the collection. * * @param mixed $key * @return bool */ public function hasAny($key) { $keys = array_flip(is_array($key) ? $key : func_get_args()); foreach ($this as $key => $value) { if (array_key_exists($key, $keys)) { return true; } } return false; } /** * Concatenate values of a given key as a string. * * @param callable|string $value * @param string|null $glue * @return string */ public function implode($value, $glue = null) { return $this->collect()->implode(...func_get_args()); } /** * Intersect the collection with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function intersect($items) { return $this->passthru('intersect', func_get_args()); } /** * Intersect the collection with the given items, using the callback. * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items * @param callable(TValue, TValue): int $callback * @return static */ public function intersectUsing() { return $this->passthru('intersectUsing', func_get_args()); } /** * Intersect the collection with the given items with additional index check. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function intersectAssoc($items) { return $this->passthru('intersectAssoc', func_get_args()); } /** * Intersect the collection with the given items with additional index check, using the callback. * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items * @param callable(TValue, TValue): int $callback * @return static */ public function intersectAssocUsing($items, callable $callback) { return $this->passthru('intersectAssocUsing', func_get_args()); } /** * Intersect the collection with the given items by key. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function intersectByKeys($items) { return $this->passthru('intersectByKeys', func_get_args()); } /** * Determine if the items are empty or not. * * @return bool */ public function isEmpty() { return ! $this->getIterator()->valid(); } /** * Determine if the collection contains a single item. * * @return bool */ public function containsOneItem() { return $this->take(2)->count() === 1; } /** * Join all items from the collection using a string. The final items can use a separate glue string. * * @param string $glue * @param string $finalGlue * @return string */ public function join($glue, $finalGlue = '') { return $this->collect()->join(...func_get_args()); } /** * Get the keys of the collection items. * * @return static<int, TKey> */ public function keys() { return new static(function () { foreach ($this as $key => $value) { yield $key; } }); } /** * Get the last item from the collection. * * @template TLastDefault * * @param (callable(TValue, TKey): bool)|null $callback * @param TLastDefault|(\Closure(): TLastDefault) $default * @return TValue|TLastDefault */ public function last(callable $callback = null, $default = null) { $needle = $placeholder = new stdClass; foreach ($this as $key => $value) { if (is_null($callback) || $callback($value, $key)) { $needle = $value; } } return $needle === $placeholder ? value($default) : $needle; } /** * Get the values of a given key. * * @param string|array<array-key, string> $value * @param string|null $key * @return static<int, mixed> */ public function pluck($value, $key = null) { return new static(function () use ($value, $key) { [$value, $key] = $this->explodePluckParameters($value, $key); foreach ($this as $item) { $itemValue = data_get($item, $value); if (is_null($key)) { yield $itemValue; } else { $itemKey = data_get($item, $key); if (is_object($itemKey) && method_exists($itemKey, '__toString')) { $itemKey = (string) $itemKey; } yield $itemKey => $itemValue; } } }); } /** * Run a map over each of the items. * * @template TMapValue * * @param callable(TValue, TKey): TMapValue $callback * @return static<TKey, TMapValue> */ public function map(callable $callback) { return new static(function () use ($callback) { foreach ($this as $key => $value) { yield $key => $callback($value, $key); } }); } /** * Run a dictionary map over the items. * * The callback should return an associative array with a single key/value pair. * * @template TMapToDictionaryKey of array-key * @template TMapToDictionaryValue * * @param callable(TValue, TKey): array<TMapToDictionaryKey, TMapToDictionaryValue> $callback * @return static<TMapToDictionaryKey, array<int, TMapToDictionaryValue>> */ public function mapToDictionary(callable $callback) { return $this->passthru('mapToDictionary', func_get_args()); } /** * Run an associative map over each of the items. * * The callback should return an associative array with a single key/value pair. * * @template TMapWithKeysKey of array-key * @template TMapWithKeysValue * * @param callable(TValue, TKey): array<TMapWithKeysKey, TMapWithKeysValue> $callback * @return static<TMapWithKeysKey, TMapWithKeysValue> */ public function mapWithKeys(callable $callback) { return new static(function () use ($callback) { foreach ($this as $key => $value) { yield from $callback($value, $key); } }); } /** * Merge the collection with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function merge($items) { return $this->passthru('merge', func_get_args()); } /** * Recursively merge the collection with the given items. * * @template TMergeRecursiveValue * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TMergeRecursiveValue>|iterable<TKey, TMergeRecursiveValue> $items * @return static<TKey, TValue|TMergeRecursiveValue> */ public function mergeRecursive($items) { return $this->passthru('mergeRecursive', func_get_args()); } /** * Create a collection by using this collection for keys and another for its values. * * @template TCombineValue * * @param \IteratorAggregate<array-key, TCombineValue>|array<array-key, TCombineValue>|(callable(): \Generator<array-key, TCombineValue>) $values * @return static<TValue, TCombineValue> */ public function combine($values) { return new static(function () use ($values) { $values = $this->makeIterator($values); $errorMessage = 'Both parameters should have an equal number of elements'; foreach ($this as $key) { if (! $values->valid()) { trigger_error($errorMessage, E_USER_WARNING); break; } yield $key => $values->current(); $values->next(); } if ($values->valid()) { trigger_error($errorMessage, E_USER_WARNING); } }); } /** * Union the collection with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function union($items) { return $this->passthru('union', func_get_args()); } /** * Create a new collection consisting of every n-th element. * * @param int $step * @param int $offset * @return static */ public function nth($step, $offset = 0) { return new static(function () use ($step, $offset) { $position = 0; foreach ($this->slice($offset) as $item) { if ($position % $step === 0) { yield $item; } $position++; } }); } /** * Get the items with the specified keys. * * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string $keys * @return static */ public function only($keys) { if ($keys instanceof Enumerable) { $keys = $keys->all(); } elseif (! is_null($keys)) { $keys = is_array($keys) ? $keys : func_get_args(); } return new static(function () use ($keys) { if (is_null($keys)) { yield from $this; } else { $keys = array_flip($keys); foreach ($this as $key => $value) { if (array_key_exists($key, $keys)) { yield $key => $value; unset($keys[$key]); if (empty($keys)) { break; } } } } }); } /** * Push all of the given items onto the collection. * * @param iterable<array-key, TValue> $source * @return static */ public function concat($source) { return (new static(function () use ($source) { yield from $this; yield from $source; }))->values(); } /** * Get one or a specified number of items randomly from the collection. * * @param int|null $number * @return static<int, TValue>|TValue * * @throws \InvalidArgumentException */ public function random($number = null) { $result = $this->collect()->random(...func_get_args()); return is_null($number) ? $result : new static($result); } /** * Replace the collection items with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function replace($items) { return new static(function () use ($items) { $items = $this->getArrayableItems($items); foreach ($this as $key => $value) { if (array_key_exists($key, $items)) { yield $key => $items[$key]; unset($items[$key]); } else { yield $key => $value; } } foreach ($items as $key => $value) { yield $key => $value; } }); } /** * Recursively replace the collection items with the given items. * * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items * @return static */ public function replaceRecursive($items) { return $this->passthru('replaceRecursive', func_get_args()); } /** * Reverse items order. * * @return static */ public function reverse() { return $this->passthru('reverse', func_get_args()); } /** * Search the collection for a given value and return the corresponding key if successful. * * @param TValue|(callable(TValue,TKey): bool) $value * @param bool $strict * @return TKey|false */ public function search($value, $strict = false) { /** @var (callable(TValue,TKey): bool) $predicate */ $predicate = $this->useAsCallable($value) ? $value : function ($item) use ($value, $strict) { return $strict ? $item === $value : $item == $value; }; foreach ($this as $key => $item) { if ($predicate($item, $key)) { return $key; } } return false; } /** * Shuffle the items in the collection. * * @param int|null $seed * @return static */ public function shuffle($seed = null) { return $this->passthru('shuffle', func_get_args()); } /** * Create chunks representing a "sliding window" view of the items in the collection. * * @param int $size * @param int $step * @return static<int, static> */ public function sliding($size = 2, $step = 1) { return new static(function () use ($size, $step) { $iterator = $this->getIterator(); $chunk = []; while ($iterator->valid()) { $chunk[$iterator->key()] = $iterator->current(); if (count($chunk) == $size) { yield (new static($chunk))->tap(function () use (&$chunk, $step) { $chunk = array_slice($chunk, $step, null, true); }); // If the $step between chunks is bigger than each chunk's $size // we will skip the extra items (which should never be in any // chunk) before we continue to the next chunk in the loop. if ($step > $size) { $skip = $step - $size; for ($i = 0; $i < $skip && $iterator->valid(); $i++) { $iterator->next(); } } } $iterator->next(); } }); } /** * Skip the first {$count} items. * * @param int $count * @return static */ public function skip($count) { return new static(function () use ($count) { $iterator = $this->getIterator(); while ($iterator->valid() && $count--) { $iterator->next(); } while ($iterator->valid()) { yield $iterator->key() => $iterator->current(); $iterator->next(); } }); } /** * Skip items in the collection until the given condition is met. * * @param TValue|callable(TValue,TKey): bool $value * @return static */ public function skipUntil($value) { $callback = $this->useAsCallable($value) ? $value : $this->equality($value); return $this->skipWhile($this->negate($callback)); } /** * Skip items in the collection while the given condition is met. * * @param TValue|callable(TValue,TKey): bool $value * @return static */ public function skipWhile($value) { $callback = $this->useAsCallable($value) ? $value : $this->equality($value); return new static(function () use ($callback) { $iterator = $this->getIterator(); while ($iterator->valid() && $callback($iterator->current(), $iterator->key())) { $iterator->next(); } while ($iterator->valid()) { yield $iterator->key() => $iterator->current(); $iterator->next(); } }); } /** * Get a slice of items from the enumerable. * * @param int $offset * @param int|null $length * @return static */ public function slice($offset, $length = null) { if ($offset < 0 || $length < 0) { return $this->passthru('slice', func_get_args()); } $instance = $this->skip($offset); return is_null($length) ? $instance : $instance->take($length); } /** * Split a collection into a certain number of groups. * * @param int $numberOfGroups * @return static<int, static> */ public function split($numberOfGroups) { return $this->passthru('split', func_get_args()); } /** * Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception. * * @param (callable(TValue, TKey): bool)|string $key * @param mixed $operator * @param mixed $value * @return TValue * * @throws \Illuminate\Support\ItemNotFoundException * @throws \Illuminate\Support\MultipleItemsFoundException */ public function sole($key = null, $operator = null, $value = null) { $filter = func_num_args() > 1 ? $this->operatorForWhere(...func_get_args()) : $key; return $this ->unless($filter == null) ->filter($filter) ->take(2) ->collect() ->sole(); } /** * Get the first item in the collection but throw an exception if no matching items exist. * * @param (callable(TValue, TKey): bool)|string $key * @param mixed $operator * @param mixed $value * @return TValue * * @throws \Illuminate\Support\ItemNotFoundException */ public function firstOrFail($key = null, $operator = null, $value = null) { $filter = func_num_args() > 1 ? $this->operatorForWhere(...func_get_args()) : $key; return $this ->unless($filter == null) ->filter($filter) ->take(1) ->collect() ->firstOrFail(); } /** * Chunk the collection into chunks of the given size. * * @param int $size * @return static<int, static> */ public function chunk($size) { if ($size <= 0) { return static::empty(); } return new static(function () use ($size) { $iterator = $this->getIterator(); while ($iterator->valid()) { $chunk = []; while (true) { $chunk[$iterator->key()] = $iterator->current(); if (count($chunk) < $size) { $iterator->next(); if (! $iterator->valid()) { break; } } else { break; } } yield new static($chunk); $iterator->next(); } }); } /** * Split a collection into a certain number of groups, and fill the first groups completely. * * @param int $numberOfGroups * @return static<int, static> */ public function splitIn($numberOfGroups) { return $this->chunk(ceil($this->count() / $numberOfGroups)); } /** * Chunk the collection into chunks with a callback. * * @param callable(TValue, TKey, Collection<TKey, TValue>): bool $callback * @return static<int, static<int, TValue>> */ public function chunkWhile(callable $callback) { return new static(function () use ($callback) { $iterator = $this->getIterator(); $chunk = new Collection; if ($iterator->valid()) { $chunk[$iterator->key()] = $iterator->current(); $iterator->next(); } while ($iterator->valid()) { if (! $callback($iterator->current(), $iterator->key(), $chunk)) { yield new static($chunk); $chunk = new Collection; } $chunk[$iterator->key()] = $iterator->current(); $iterator->next(); } if ($chunk->isNotEmpty()) { yield new static($chunk); } }); } /** * Sort through each item with a callback. * * @param (callable(TValue, TValue): int)|null|int $callback * @return static */ public function sort($callback = null) { return $this->passthru('sort', func_get_args()); } /** * Sort items in descending order. * * @param int $options * @return static */ public function sortDesc($options = SORT_REGULAR) { return $this->passthru('sortDesc', func_get_args()); } /** * Sort the collection using the given callback. * * @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback * @param int $options * @param bool $descending * @return static */ public function sortBy($callback, $options = SORT_REGULAR, $descending = false) { return $this->passthru('sortBy', func_get_args()); } /** * Sort the collection in descending order using the given callback. * * @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback * @param int $options * @return static */ public function sortByDesc($callback, $options = SORT_REGULAR) { return $this->passthru('sortByDesc', func_get_args()); } /** * Sort the collection keys. * * @param int $options * @param bool $descending * @return static */ public function sortKeys($options = SORT_REGULAR, $descending = false) { return $this->passthru('sortKeys', func_get_args()); } /** * Sort the collection keys in descending order. * * @param int $options * @return static */ public function sortKeysDesc($options = SORT_REGULAR) { return $this->passthru('sortKeysDesc', func_get_args()); } /** * Sort the collection keys using a callback. * * @param callable(TKey, TKey): int $callback * @return static */ public function sortKeysUsing(callable $callback) { return $this->passthru('sortKeysUsing', func_get_args()); } /** * Take the first or last {$limit} items. * * @param int $limit * @return static */ public function take($limit) { if ($limit < 0) { return new static(function () use ($limit) { $limit = abs($limit); $ringBuffer = []; $position = 0; foreach ($this as $key => $value) { $ringBuffer[$position] = [$key, $value]; $position = ($position + 1) % $limit; } for ($i = 0, $end = min($limit, count($ringBuffer)); $i < $end; $i++) { $pointer = ($position + $i) % $limit; yield $ringBuffer[$pointer][0] => $ringBuffer[$pointer][1]; } }); } return new static(function () use ($limit) { $iterator = $this->getIterator(); while ($limit--) { if (! $iterator->valid()) { break; } yield $iterator->key() => $iterator->current(); if ($limit) { $iterator->next(); } } }); } /** * Take items in the collection until the given condition is met. * * @param TValue|callable(TValue,TKey): bool $value * @return static */ public function takeUntil($value) { /** @var callable(TValue, TKey): bool $callback */ $callback = $this->useAsCallable($value) ? $value : $this->equality($value); return new static(function () use ($callback) { foreach ($this as $key => $item) { if ($callback($item, $key)) { break; } yield $key => $item; } }); } /** * Take items in the collection until a given point in time. * * @param \DateTimeInterface $timeout * @return static */ public function takeUntilTimeout(DateTimeInterface $timeout) { $timeout = $timeout->getTimestamp(); return new static(function () use ($timeout) { if ($this->now() >= $timeout) { return; } foreach ($this as $key => $value) { yield $key => $value; if ($this->now() >= $timeout) { break; } } }); } /** * Take items in the collection while the given condition is met. * * @param TValue|callable(TValue,TKey): bool $value * @return static */ public function takeWhile($value) { /** @var callable(TValue, TKey): bool $callback */ $callback = $this->useAsCallable($value) ? $value : $this->equality($value); return $this->takeUntil(fn ($item, $key) => ! $callback($item, $key)); } /** * Pass each item in the collection to the given callback, lazily. * * @param callable(TValue, TKey): mixed $callback * @return static */ public function tapEach(callable $callback) { return new static(function () use ($callback) { foreach ($this as $key => $value) { $callback($value, $key); yield $key => $value; } }); } /** * Flatten a multi-dimensional associative array with dots. * * @return static */ public function dot() { return $this->passthru('dot', []); } /** * Convert a flatten "dot" notation array into an expanded array. * * @return static */ public function undot() { return $this->passthru('undot', []); } /** * Return only unique items from the collection array. * * @param (callable(TValue, TKey): mixed)|string|null $key * @param bool $strict * @return static */ public function unique($key = null, $strict = false) { $callback = $this->valueRetriever($key); return new static(function () use ($callback, $strict) { $exists = []; foreach ($this as $key => $item) { if (! in_array($id = $callback($item, $key), $exists, $strict)) { yield $key => $item; $exists[] = $id; } } }); } /** * Reset the keys on the underlying array. * * @return static<int, TValue> */ public function values() { return new static(function () { foreach ($this as $item) { yield $item; } }); } /** * Zip the collection together with one or more arrays. * * e.g. new LazyCollection([1, 2, 3])->zip([4, 5, 6]); * => [[1, 4], [2, 5], [3, 6]] * * @template TZipValue * * @param \Illuminate\Contracts\Support\Arrayable<array-key, TZipValue>|iterable<array-key, TZipValue> ...$items * @return static<int, static<int, TValue|TZipValue>> */ public function zip($items) { $iterables = func_get_args(); return new static(function () use ($iterables) { $iterators = Collection::make($iterables)->map(function ($iterable) { return $this->makeIterator($iterable); })->prepend($this->getIterator()); while ($iterators->contains->valid()) { yield new static($iterators->map->current()); $iterators->each->next(); } }); } /** * Pad collection to the specified length with a value. * * @template TPadValue * * @param int $size * @param TPadValue $value * @return static<int, TValue|TPadValue> */ public function pad($size, $value) { if ($size < 0) { return $this->passthru('pad', func_get_args()); } return new static(function () use ($size, $value) { $yielded = 0; foreach ($this as $index => $item) { yield $index => $item; $yielded++; } while ($yielded++ < $size) { yield $value; } }); } /** * Get the values iterator. * * @return \Traversable<TKey, TValue> */ public function getIterator(): Traversable { return $this->makeIterator($this->source); } /** * Count the number of items in the collection. * * @return int */ public function count(): int { if (is_array($this->source)) { return count($this->source); } return iterator_count($this->getIterator()); } /** * Make an iterator from the given source. * * @template TIteratorKey of array-key * @template TIteratorValue * * @param \IteratorAggregate<TIteratorKey, TIteratorValue>|array<TIteratorKey, TIteratorValue>|(callable(): \Generator<TIteratorKey, TIteratorValue>) $source * @return \Traversable<TIteratorKey, TIteratorValue> */ protected function makeIterator($source) { if ($source instanceof IteratorAggregate) { return $source->getIterator(); } if (is_array($source)) { return new ArrayIterator($source); } if (is_callable($source)) { $maybeTraversable = $source(); return $maybeTraversable instanceof Traversable ? $maybeTraversable : new ArrayIterator(Arr::wrap($maybeTraversable)); } return new ArrayIterator((array) $source); } /** * Explode the "value" and "key" arguments passed to "pluck". * * @param string|string[] $value * @param string|string[]|null $key * @return array{string[],string[]|null} */ protected function explodePluckParameters($value, $key) { $value = is_string($value) ? explode('.', $value) : $value; $key = is_null($key) || is_array($key) ? $key : explode('.', $key); return [$value, $key]; } /** * Pass this lazy collection through a method on the collection class. * * @param string $method * @param array<mixed> $params * @return static */ protected function passthru($method, array $params) { return new static(function () use ($method, $params) { yield from $this->collect()->$method(...$params); }); } /** * Get the current time. * * @return int */ protected function now() { return Carbon::now()->timestamp; } } src/Illuminate/Collections/LICENSE.md 0000644 00000002063 15107327041 0013321 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. src/Illuminate/Collections/Arr.php 0000644 00000053340 15107327041 0013156 0 ustar 00 <?php namespace Illuminate\Support; use ArgumentCountError; use ArrayAccess; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; class Arr { use Macroable; /** * Determine whether the given value is array accessible. * * @param mixed $value * @return bool */ public static function accessible($value) { return is_array($value) || $value instanceof ArrayAccess; } /** * Add an element to an array using "dot" notation if it doesn't exist. * * @param array $array * @param string|int|float $key * @param mixed $value * @return array */ public static function add($array, $key, $value) { if (is_null(static::get($array, $key))) { static::set($array, $key, $value); } return $array; } /** * Collapse an array of arrays into a single array. * * @param iterable $array * @return array */ public static function collapse($array) { $results = []; foreach ($array as $values) { if ($values instanceof Collection) { $values = $values->all(); } elseif (! is_array($values)) { continue; } $results[] = $values; } return array_merge([], ...$results); } /** * Cross join the given arrays, returning all possible permutations. * * @param iterable ...$arrays * @return array */ public static function crossJoin(...$arrays) { $results = [[]]; foreach ($arrays as $index => $array) { $append = []; foreach ($results as $product) { foreach ($array as $item) { $product[$index] = $item; $append[] = $product; } } $results = $append; } return $results; } /** * Divide an array into two arrays. One with keys and the other with values. * * @param array $array * @return array */ public static function divide($array) { return [array_keys($array), array_values($array)]; } /** * Flatten a multi-dimensional associative array with dots. * * @param iterable $array * @param string $prepend * @return array */ public static function dot($array, $prepend = '') { $results = []; foreach ($array as $key => $value) { if (is_array($value) && ! empty($value)) { $results = array_merge($results, static::dot($value, $prepend.$key.'.')); } else { $results[$prepend.$key] = $value; } } return $results; } /** * Convert a flatten "dot" notation array into an expanded array. * * @param iterable $array * @return array */ public static function undot($array) { $results = []; foreach ($array as $key => $value) { static::set($results, $key, $value); } return $results; } /** * Get all of the given array except for a specified array of keys. * * @param array $array * @param array|string|int|float $keys * @return array */ public static function except($array, $keys) { static::forget($array, $keys); return $array; } /** * Determine if the given key exists in the provided array. * * @param \ArrayAccess|array $array * @param string|int $key * @return bool */ public static function exists($array, $key) { if ($array instanceof Enumerable) { return $array->has($key); } if ($array instanceof ArrayAccess) { return $array->offsetExists($key); } if (is_float($key)) { $key = (string) $key; } return array_key_exists($key, $array); } /** * Return the first element in an array passing a given truth test. * * @param iterable $array * @param callable|null $callback * @param mixed $default * @return mixed */ public static function first($array, callable $callback = null, $default = null) { if (is_null($callback)) { if (empty($array)) { return value($default); } foreach ($array as $item) { return $item; } } foreach ($array as $key => $value) { if ($callback($value, $key)) { return $value; } } return value($default); } /** * Return the last element in an array passing a given truth test. * * @param array $array * @param callable|null $callback * @param mixed $default * @return mixed */ public static function last($array, callable $callback = null, $default = null) { if (is_null($callback)) { return empty($array) ? value($default) : end($array); } return static::first(array_reverse($array, true), $callback, $default); } /** * Flatten a multi-dimensional array into a single level. * * @param iterable $array * @param int $depth * @return array */ public static function flatten($array, $depth = INF) { $result = []; foreach ($array as $item) { $item = $item instanceof Collection ? $item->all() : $item; if (! is_array($item)) { $result[] = $item; } else { $values = $depth === 1 ? array_values($item) : static::flatten($item, $depth - 1); foreach ($values as $value) { $result[] = $value; } } } return $result; } /** * Remove one or many array items from a given array using "dot" notation. * * @param array $array * @param array|string|int|float $keys * @return void */ public static function forget(&$array, $keys) { $original = &$array; $keys = (array) $keys; if (count($keys) === 0) { return; } foreach ($keys as $key) { // if the exact key exists in the top-level, remove it if (static::exists($array, $key)) { unset($array[$key]); continue; } $parts = explode('.', $key); // clean up before each pass $array = &$original; while (count($parts) > 1) { $part = array_shift($parts); if (isset($array[$part]) && static::accessible($array[$part])) { $array = &$array[$part]; } else { continue 2; } } unset($array[array_shift($parts)]); } } /** * Get an item from an array using "dot" notation. * * @param \ArrayAccess|array $array * @param string|int|null $key * @param mixed $default * @return mixed */ public static function get($array, $key, $default = null) { if (! static::accessible($array)) { return value($default); } if (is_null($key)) { return $array; } if (static::exists($array, $key)) { return $array[$key]; } if (! str_contains($key, '.')) { return $array[$key] ?? value($default); } foreach (explode('.', $key) as $segment) { if (static::accessible($array) && static::exists($array, $segment)) { $array = $array[$segment]; } else { return value($default); } } return $array; } /** * Check if an item or items exist in an array using "dot" notation. * * @param \ArrayAccess|array $array * @param string|array $keys * @return bool */ public static function has($array, $keys) { $keys = (array) $keys; if (! $array || $keys === []) { return false; } foreach ($keys as $key) { $subKeyArray = $array; if (static::exists($array, $key)) { continue; } foreach (explode('.', $key) as $segment) { if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) { $subKeyArray = $subKeyArray[$segment]; } else { return false; } } } return true; } /** * Determine if any of the keys exist in an array using "dot" notation. * * @param \ArrayAccess|array $array * @param string|array $keys * @return bool */ public static function hasAny($array, $keys) { if (is_null($keys)) { return false; } $keys = (array) $keys; if (! $array) { return false; } if ($keys === []) { return false; } foreach ($keys as $key) { if (static::has($array, $key)) { return true; } } return false; } /** * Determines if an array is associative. * * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. * * @param array $array * @return bool */ public static function isAssoc(array $array) { return ! array_is_list($array); } /** * Determines if an array is a list. * * An array is a "list" if all array keys are sequential integers starting from 0 with no gaps in between. * * @param array $array * @return bool */ public static function isList($array) { return array_is_list($array); } /** * Join all items using a string. The final items can use a separate glue string. * * @param array $array * @param string $glue * @param string $finalGlue * @return string */ public static function join($array, $glue, $finalGlue = '') { if ($finalGlue === '') { return implode($glue, $array); } if (count($array) === 0) { return ''; } if (count($array) === 1) { return end($array); } $finalItem = array_pop($array); return implode($glue, $array).$finalGlue.$finalItem; } /** * Key an associative array by a field or using a callback. * * @param array $array * @param callable|array|string $keyBy * @return array */ public static function keyBy($array, $keyBy) { return Collection::make($array)->keyBy($keyBy)->all(); } /** * Prepend the key names of an associative array. * * @param array $array * @param string $prependWith * @return array */ public static function prependKeysWith($array, $prependWith) { return Collection::make($array)->mapWithKeys(function ($item, $key) use ($prependWith) { return [$prependWith.$key => $item]; })->all(); } /** * Get a subset of the items from the given array. * * @param array $array * @param array|string $keys * @return array */ public static function only($array, $keys) { return array_intersect_key($array, array_flip((array) $keys)); } /** * Pluck an array of values from an array. * * @param iterable $array * @param string|array|int|null $value * @param string|array|null $key * @return array */ public static function pluck($array, $value, $key = null) { $results = []; [$value, $key] = static::explodePluckParameters($value, $key); foreach ($array as $item) { $itemValue = data_get($item, $value); // If the key is "null", we will just append the value to the array and keep // looping. Otherwise we will key the array using the value of the key we // received from the developer. Then we'll return the final array form. if (is_null($key)) { $results[] = $itemValue; } else { $itemKey = data_get($item, $key); if (is_object($itemKey) && method_exists($itemKey, '__toString')) { $itemKey = (string) $itemKey; } $results[$itemKey] = $itemValue; } } return $results; } /** * Explode the "value" and "key" arguments passed to "pluck". * * @param string|array $value * @param string|array|null $key * @return array */ protected static function explodePluckParameters($value, $key) { $value = is_string($value) ? explode('.', $value) : $value; $key = is_null($key) || is_array($key) ? $key : explode('.', $key); return [$value, $key]; } /** * Run a map over each of the items in the array. * * @param array $array * @param callable $callback * @return array */ public static function map(array $array, callable $callback) { $keys = array_keys($array); try { $items = array_map($callback, $array, $keys); } catch (ArgumentCountError) { $items = array_map($callback, $array); } return array_combine($keys, $items); } /** * Run an associative map over each of the items. * * The callback should return an associative array with a single key/value pair. * * @template TKey * @template TValue * @template TMapWithKeysKey of array-key * @template TMapWithKeysValue * * @param array<TKey, TValue> $array * @param callable(TValue, TKey): array<TMapWithKeysKey, TMapWithKeysValue> $callback * @return array */ public static function mapWithKeys(array $array, callable $callback) { $result = []; foreach ($array as $key => $value) { $assoc = $callback($value, $key); foreach ($assoc as $mapKey => $mapValue) { $result[$mapKey] = $mapValue; } } return $result; } /** * Push an item onto the beginning of an array. * * @param array $array * @param mixed $value * @param mixed $key * @return array */ public static function prepend($array, $value, $key = null) { if (func_num_args() == 2) { array_unshift($array, $value); } else { $array = [$key => $value] + $array; } return $array; } /** * Get a value from the array, and remove it. * * @param array $array * @param string|int $key * @param mixed $default * @return mixed */ public static function pull(&$array, $key, $default = null) { $value = static::get($array, $key, $default); static::forget($array, $key); return $value; } /** * Convert the array into a query string. * * @param array $array * @return string */ public static function query($array) { return http_build_query($array, '', '&', PHP_QUERY_RFC3986); } /** * Get one or a specified number of random values from an array. * * @param array $array * @param int|null $number * @param bool $preserveKeys * @return mixed * * @throws \InvalidArgumentException */ public static function random($array, $number = null, $preserveKeys = false) { $requested = is_null($number) ? 1 : $number; $count = count($array); if ($requested > $count) { throw new InvalidArgumentException( "You requested {$requested} items, but there are only {$count} items available." ); } if (is_null($number)) { return $array[array_rand($array)]; } if ((int) $number === 0) { return []; } $keys = array_rand($array, $number); $results = []; if ($preserveKeys) { foreach ((array) $keys as $key) { $results[$key] = $array[$key]; } } else { foreach ((array) $keys as $key) { $results[] = $array[$key]; } } return $results; } /** * Set an array item to a given value using "dot" notation. * * If no key is given to the method, the entire array will be replaced. * * @param array $array * @param string|int|null $key * @param mixed $value * @return array */ public static function set(&$array, $key, $value) { if (is_null($key)) { return $array = $value; } $keys = explode('.', $key); foreach ($keys as $i => $key) { if (count($keys) === 1) { break; } unset($keys[$i]); // If the key doesn't exist at this depth, we will just create an empty array // to hold the next value, allowing us to create the arrays to hold final // values at the correct depth. Then we'll keep digging into the array. if (! isset($array[$key]) || ! is_array($array[$key])) { $array[$key] = []; } $array = &$array[$key]; } $array[array_shift($keys)] = $value; return $array; } /** * Shuffle the given array and return the result. * * @param array $array * @param int|null $seed * @return array */ public static function shuffle($array, $seed = null) { if (is_null($seed)) { shuffle($array); } else { mt_srand($seed); shuffle($array); mt_srand(); } return $array; } /** * Sort the array using the given callback or "dot" notation. * * @param array $array * @param callable|array|string|null $callback * @return array */ public static function sort($array, $callback = null) { return Collection::make($array)->sortBy($callback)->all(); } /** * Sort the array in descending order using the given callback or "dot" notation. * * @param array $array * @param callable|array|string|null $callback * @return array */ public static function sortDesc($array, $callback = null) { return Collection::make($array)->sortByDesc($callback)->all(); } /** * Recursively sort an array by keys and values. * * @param array $array * @param int $options * @param bool $descending * @return array */ public static function sortRecursive($array, $options = SORT_REGULAR, $descending = false) { foreach ($array as &$value) { if (is_array($value)) { $value = static::sortRecursive($value, $options, $descending); } } if (! array_is_list($array)) { $descending ? krsort($array, $options) : ksort($array, $options); } else { $descending ? rsort($array, $options) : sort($array, $options); } return $array; } /** * Recursively sort an array by keys and values in descending order. * * @param array $array * @param int $options * @return array */ public static function sortRecursiveDesc($array, $options = SORT_REGULAR) { return static::sortRecursive($array, $options, true); } /** * Conditionally compile classes from an array into a CSS class list. * * @param array $array * @return string */ public static function toCssClasses($array) { $classList = static::wrap($array); $classes = []; foreach ($classList as $class => $constraint) { if (is_numeric($class)) { $classes[] = $constraint; } elseif ($constraint) { $classes[] = $class; } } return implode(' ', $classes); } /** * Conditionally compile styles from an array into a style list. * * @param array $array * @return string */ public static function toCssStyles($array) { $styleList = static::wrap($array); $styles = []; foreach ($styleList as $class => $constraint) { if (is_numeric($class)) { $styles[] = Str::finish($constraint, ';'); } elseif ($constraint) { $styles[] = Str::finish($class, ';'); } } return implode(' ', $styles); } /** * Filter the array using the given callback. * * @param array $array * @param callable $callback * @return array */ public static function where($array, callable $callback) { return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH); } /** * Filter items where the value is not null. * * @param array $array * @return array */ public static function whereNotNull($array) { return static::where($array, fn ($value) => ! is_null($value)); } /** * If the given value is not an array and not null, wrap it in one. * * @param mixed $value * @return array */ public static function wrap($value) { if (is_null($value)) { return []; } return is_array($value) ? $value : [$value]; } } src/Illuminate/Collections/MultipleItemsFoundException.php 0000644 00000001340 15107327041 0020073 0 ustar 00 <?php namespace Illuminate\Support; use RuntimeException; class MultipleItemsFoundException extends RuntimeException { /** * The number of items found. * * @var int */ public $count; /** * Create a new exception instance. * * @param int $count * @param int $code * @param \Throwable|null $previous * @return void */ public function __construct($count, $code = 0, $previous = null) { $this->count = $count; parent::__construct("$count items were found.", $code, $previous); } /** * Get the number of items found. * * @return int */ public function getCount() { return $this->count; } } src/Illuminate/Collections/HigherOrderCollectionProxy.php 0000644 00000002600 15107327041 0017703 0 ustar 00 <?php namespace Illuminate\Support; /** * @mixin \Illuminate\Support\Enumerable */ class HigherOrderCollectionProxy { /** * The collection being operated on. * * @var \Illuminate\Support\Enumerable */ protected $collection; /** * The method being proxied. * * @var string */ protected $method; /** * Create a new proxy instance. * * @param \Illuminate\Support\Enumerable $collection * @param string $method * @return void */ public function __construct(Enumerable $collection, $method) { $this->method = $method; $this->collection = $collection; } /** * Proxy accessing an attribute onto the collection items. * * @param string $key * @return mixed */ public function __get($key) { return $this->collection->{$this->method}(function ($value) use ($key) { return is_array($value) ? $value[$key] : $value->{$key}; }); } /** * Proxy a method call onto the collection items. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { return $this->collection->{$this->method}(function ($value) use ($method, $parameters) { return $value->{$method}(...$parameters); }); } } README.md 0000644 00000006651 15107327041 0006033 0 ustar 00 <p align="center"><a href="https://laravel.com" target="_blank"><img src="https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg" width="400"></a></p> <p align="center"> <a href="https://github.com/laravel/framework/actions"><img src="https://github.com/laravel/framework/workflows/tests/badge.svg" alt="Build Status"></a> <a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/dt/laravel/framework" alt="Total Downloads"></a> <a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/v/laravel/framework" alt="Latest Stable Version"></a> <a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/l/laravel/framework" alt="License"></a> </p> ## About Laravel > **Note:** This repository contains the core code of the Laravel framework. If you want to build an application using Laravel, visit the main [Laravel repository](https://github.com/laravel/laravel). Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable, creative experience to be truly fulfilling. Laravel attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as: - [Simple, fast routing engine](https://laravel.com/docs/routing). - [Powerful dependency injection container](https://laravel.com/docs/container). - Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage. - Database agnostic [schema migrations](https://laravel.com/docs/migrations). - [Robust background job processing](https://laravel.com/docs/queues). - [Real-time event broadcasting](https://laravel.com/docs/broadcasting). Laravel is accessible, yet powerful, providing tools needed for large, robust applications. A superb combination of simplicity, elegance, and innovation gives you a complete toolset required to build any application with which you are tasked. ## Learning Laravel Laravel has the most extensive and thorough documentation and video tutorial library of any modern web application framework. The [Laravel documentation](https://laravel.com/docs) is in-depth and complete, making it a breeze to get started learning the framework. You may also try the [Laravel Bootcamp](https://bootcamp.laravel.com), where you will be guided through building a modern Laravel application from scratch. If you're not in the mood to read, [Laracasts](https://laracasts.com) contains over 1100 video tutorials covering a range of topics including Laravel, modern PHP, unit testing, JavaScript, and more. Boost the skill level of yourself and your entire team by digging into our comprehensive video library. ## Contributing Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions). ## Code of Conduct In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct). ## Security Vulnerabilities Please review [our security policy](https://github.com/laravel/framework/security/policy) on how to report security vulnerabilities. ## License The Laravel framework is open-sourced software licensed under the [MIT license](LICENSE.md). composer.json 0000644 00000020605 15107327041 0007271 0 ustar 00 { "name": "laravel/framework", "description": "The Laravel Framework.", "keywords": ["framework", "laravel"], "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.1", "ext-ctype": "*", "ext-filter": "*", "ext-hash": "*", "ext-mbstring": "*", "ext-openssl": "*", "ext-session": "*", "ext-tokenizer": "*", "composer-runtime-api": "^2.2", "brick/math": "^0.9.3|^0.10.2|^0.11", "doctrine/inflector": "^2.0.5", "dragonmantank/cron-expression": "^3.3.2", "egulias/email-validator": "^3.2.1|^4.0", "fruitcake/php-cors": "^1.2", "guzzlehttp/uri-template": "^1.0", "laravel/prompts": "^0.1.9", "laravel/serializable-closure": "^1.3", "league/commonmark": "^2.2.1", "league/flysystem": "^3.8.0", "monolog/monolog": "^3.0", "nesbot/carbon": "^2.67", "nunomaduro/termwind": "^1.13", "psr/container": "^1.1.1|^2.0.1", "psr/log": "^1.0|^2.0|^3.0", "psr/simple-cache": "^1.0|^2.0|^3.0", "ramsey/uuid": "^4.7", "symfony/console": "^6.2", "symfony/error-handler": "^6.2", "symfony/finder": "^6.2", "symfony/http-foundation": "^6.3", "symfony/http-kernel": "^6.2", "symfony/mailer": "^6.2", "symfony/mime": "^6.2", "symfony/process": "^6.2", "symfony/routing": "^6.2", "symfony/uid": "^6.2", "symfony/var-dumper": "^6.2", "tijsverkoyen/css-to-inline-styles": "^2.2.5", "vlucas/phpdotenv": "^5.4.1", "voku/portable-ascii": "^2.0" }, "replace": { "illuminate/auth": "self.version", "illuminate/broadcasting": "self.version", "illuminate/bus": "self.version", "illuminate/cache": "self.version", "illuminate/collections": "self.version", "illuminate/conditionable": "self.version", "illuminate/config": "self.version", "illuminate/console": "self.version", "illuminate/container": "self.version", "illuminate/contracts": "self.version", "illuminate/cookie": "self.version", "illuminate/database": "self.version", "illuminate/encryption": "self.version", "illuminate/events": "self.version", "illuminate/filesystem": "self.version", "illuminate/hashing": "self.version", "illuminate/http": "self.version", "illuminate/log": "self.version", "illuminate/macroable": "self.version", "illuminate/mail": "self.version", "illuminate/notifications": "self.version", "illuminate/pagination": "self.version", "illuminate/pipeline": "self.version", "illuminate/process": "self.version", "illuminate/queue": "self.version", "illuminate/redis": "self.version", "illuminate/routing": "self.version", "illuminate/session": "self.version", "illuminate/support": "self.version", "illuminate/testing": "self.version", "illuminate/translation": "self.version", "illuminate/validation": "self.version", "illuminate/view": "self.version" }, "require-dev": { "ext-gmp": "*", "ably/ably-php": "^1.0", "aws/aws-sdk-php": "^3.235.5", "doctrine/dbal": "^3.5.1", "fakerphp/faker": "^1.21", "guzzlehttp/guzzle": "^7.5", "league/flysystem-aws-s3-v3": "^3.0", "league/flysystem-ftp": "^3.0", "league/flysystem-path-prefixing": "^3.3", "league/flysystem-read-only": "^3.3", "league/flysystem-sftp-v3": "^3.0", "mockery/mockery": "^1.5.1", "nyholm/psr7": "^1.2", "orchestra/testbench-core": "^8.12", "pda/pheanstalk": "^4.0", "phpstan/phpstan": "^1.4.7", "phpunit/phpunit": "^10.0.7", "predis/predis": "^2.0.2", "symfony/cache": "^6.2", "symfony/http-client": "^6.2.4", "symfony/psr-http-message-bridge": "^2.0" }, "provide": { "psr/container-implementation": "1.1|2.0", "psr/simple-cache-implementation": "1.0|2.0|3.0" }, "conflict": { "tightenco/collect": "<5.5.33" }, "autoload": { "files": [ "src/Illuminate/Collections/helpers.php", "src/Illuminate/Events/functions.php", "src/Illuminate/Foundation/helpers.php", "src/Illuminate/Support/helpers.php" ], "psr-4": { "Illuminate\\": "src/Illuminate/", "Illuminate\\Support\\": [ "src/Illuminate/Macroable/", "src/Illuminate/Collections/", "src/Illuminate/Conditionable/" ] } }, "autoload-dev": { "files": [ "tests/Database/stubs/MigrationCreatorFakeMigration.php" ], "psr-4": { "Illuminate\\Tests\\": "tests/" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "suggest": { "ext-apcu": "Required to use the APC cache driver.", "ext-fileinfo": "Required to use the Filesystem class.", "ext-ftp": "Required to use the Flysystem FTP driver.", "ext-gd": "Required to use Illuminate\\Http\\Testing\\FileFactory::image().", "ext-memcached": "Required to use the memcache cache driver.", "ext-pcntl": "Required to use all features of the queue worker and console signal trapping.", "ext-pdo": "Required to use all database features.", "ext-posix": "Required to use all features of the queue worker.", "ext-redis": "Required to use the Redis cache and queue drivers (^4.0|^5.0).", "ably/ably-php": "Required to use the Ably broadcast driver (^1.0).", "aws/aws-sdk-php": "Required to use the SQS queue driver, DynamoDb failed job storage, and SES mail driver (^3.235.5).", "brianium/paratest": "Required to run tests in parallel (^6.0).", "doctrine/dbal": "Required to rename columns and drop SQLite columns (^3.5.1).", "fakerphp/faker": "Required to use the eloquent factory builder (^1.9.1).", "filp/whoops": "Required for friendly error pages in development (^2.14.3).", "guzzlehttp/guzzle": "Required to use the HTTP Client and the ping methods on schedules (^7.5).", "laravel/tinker": "Required to use the tinker console command (^2.0).", "league/flysystem-aws-s3-v3": "Required to use the Flysystem S3 driver (^3.0).", "league/flysystem-ftp": "Required to use the Flysystem FTP driver (^3.0).", "league/flysystem-path-prefixing": "Required to use the scoped driver (^3.3).", "league/flysystem-read-only": "Required to use read-only disks (^3.3)", "league/flysystem-sftp-v3": "Required to use the Flysystem SFTP driver (^3.0).", "mockery/mockery": "Required to use mocking (^1.5.1).", "nyholm/psr7": "Required to use PSR-7 bridging features (^1.2).", "pda/pheanstalk": "Required to use the beanstalk queue driver (^4.0).", "phpunit/phpunit": "Required to use assertions and run tests (^9.5.8|^10.0.7).", "predis/predis": "Required to use the predis connector (^2.0.2).", "psr/http-message": "Required to allow Storage::put to accept a StreamInterface (^1.0).", "pusher/pusher-php-server": "Required to use the Pusher broadcast driver (^6.0|^7.0).", "symfony/cache": "Required to PSR-6 cache bridge (^6.2).", "symfony/filesystem": "Required to enable support for relative symbolic links (^6.2).", "symfony/http-client": "Required to enable support for the Symfony API mail transports (^6.2).", "symfony/mailgun-mailer": "Required to enable support for the Mailgun mail transport (^6.2).", "symfony/postmark-mailer": "Required to enable support for the Postmark mail transport (^6.2).", "symfony/psr-http-message-bridge": "Required to use PSR-7 bridging features (^2.0)." }, "config": { "sort-packages": true, "allow-plugins": { "composer/package-versions-deprecated": true } }, "minimum-stability": "stable", "prefer-stable": true } LICENSE.md 0000644 00000002063 15107327041 0006151 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CHANGELOG.md 0000644 00000247424 15107327041 0006372 0 ustar 00 # Release Notes for 10.x ## [Unreleased](https://github.com/laravel/framework/compare/v10.30.0...10.x) ## [v10.30.0](https://github.com/laravel/framework/compare/v10.29.0...v10.30.0) - 2023-10-31 - [10.x] Test Improvements by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/48815 - [10.x] Verify hash config by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48814 - [10.x] Fix the issue of using the now function within the ArrayCache in Lumen by [@cxlblm](https://github.com/cxlblm) in https://github.com/laravel/framework/pull/48826 - [10.x] Match service provider after resolved by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48824 - [10.x] Fix type error registering PSR Request by [@kpicaza](https://github.com/kpicaza) in https://github.com/laravel/framework/pull/48823 - [10.x] Ability to configure default session block timeouts by [@bytestream](https://github.com/bytestream) in https://github.com/laravel/framework/pull/48795 - [10.x] Improvements for `artisan migrate --pretend` command 🚀 by [@NickSdot](https://github.com/NickSdot) in https://github.com/laravel/framework/pull/48768 - [10.x] Add support for getting native columns' attributes by [@hafezdivandari](https://github.com/hafezdivandari) in https://github.com/laravel/framework/pull/48357 - fix(Eloquent/Builder): calling the methods on passthru base object should be case-insensitive by [@luka-papez](https://github.com/luka-papez) in https://github.com/laravel/framework/pull/48852 - [10.x] Fix `QueriesRelationships[@getRelationHashedColumn](https://github.com/getRelationHashedColumn)()` typehint by [@cosmastech](https://github.com/cosmastech) in https://github.com/laravel/framework/pull/48847 - [10.x] Remember the job on the exception by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48830 - fix bug for always throwing exception when we pass a callable to throwUnlessStatus method [test included] by [@mhfereydouni](https://github.com/mhfereydouni) in https://github.com/laravel/framework/pull/48844 - [10.x] Dispatch events based on a DB transaction result by [@mateusjatenee](https://github.com/mateusjatenee) in https://github.com/laravel/framework/pull/48705 - [10.x] Reset ShouldDispatchAfterCommitEventTest objects properties by [@mateusjatenee](https://github.com/mateusjatenee) in https://github.com/laravel/framework/pull/48858 - [10.x] Throw exception when trying to escape array for database connection by [@sidneyprins](https://github.com/sidneyprins) in https://github.com/laravel/framework/pull/48836 - [10.x] Fix Stringable objects not converted to string in HTTP facade Query parameters and Body by [@LasseRafn](https://github.com/LasseRafn) in https://github.com/laravel/framework/pull/48849 ## [v10.29.0](https://github.com/laravel/framework/compare/v10.28.0...v10.29.0) - 2023-10-24 - [10.x] Fixes `Str::password()` does not always generate password with numbers by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/48681 - [10.x] Fixes cache:prune-stale-tags preg_match delimiter no escaped by [@ame1973](https://github.com/ame1973) in https://github.com/laravel/framework/pull/48702 - [10.x] Allow route:list to expand middleware groups in 'VeryVerbose' mode by [@NickSdot](https://github.com/NickSdot) in https://github.com/laravel/framework/pull/48703 - [10.x] Fix model:prune command error with non-class php files by [@zlodes](https://github.com/zlodes) in https://github.com/laravel/framework/pull/48708 - [10.x] Show CliDumper source content on last line by [@CalebDW](https://github.com/CalebDW) in https://github.com/laravel/framework/pull/48707 - [10.x] Revival of the reverted changes in 10.25.0: `firstOrCreate` `updateOrCreate` improvement through `createOrFirst` + additional query tests by [@mpyw](https://github.com/mpyw) in https://github.com/laravel/framework/pull/48637 - [10.x] allow resolving view from closure by [@PH7-Jack](https://github.com/PH7-Jack) in https://github.com/laravel/framework/pull/48719 - [10.x] Allow creation of PSR request with merged data by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48696 - [10.x] Update DocBlock for `convertCase` Method to Reflect Optional $encoding Parameter by [@salehhashemi1992](https://github.com/salehhashemi1992) in https://github.com/laravel/framework/pull/48729 - [10.x] Use ValidationException class from Validator Property by [@a-h-abid](https://github.com/a-h-abid) in https://github.com/laravel/framework/pull/48736 - [10.x] Implement Test Coverage for `Str::convertCase` Method by [@salehhashemi1992](https://github.com/salehhashemi1992) in https://github.com/laravel/framework/pull/48730 - [10.x] Extend Test Coverage for `Str::take` Function by [@salehhashemi1992](https://github.com/salehhashemi1992) in https://github.com/laravel/framework/pull/48728 - [10.x] Add `replaceMatches` to Str class by [@hosmelq](https://github.com/hosmelq) in https://github.com/laravel/framework/pull/48727 - [10.x] Fix duplicate conditions on retrying `SELECT` calls under `createOrFirst()` by [@KentarouTakeda](https://github.com/KentarouTakeda) in https://github.com/laravel/framework/pull/48725 - [10.x] Uses `stefanzweifel/git-auto-commit-action[@v5](https://github.com/v5)` by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/48763 - [10.x] fix typo in comment by [@vintagesucks](https://github.com/vintagesucks) in https://github.com/laravel/framework/pull/48770 - [10.x] Require DBAL 3 when installing by [@Jubeki](https://github.com/Jubeki) in https://github.com/laravel/framework/pull/48769 - [10.x] Escape the delimiter when extracting an excerpt from text by [@standaniels](https://github.com/standaniels) in https://github.com/laravel/framework/pull/48765 - [10.x] Fix `replaceMatches` in Str class by [@hosmelq](https://github.com/hosmelq) in https://github.com/laravel/framework/pull/48760 - [10.x] Moves logger instance creation to a protected method by [@rodrigopedra](https://github.com/rodrigopedra) in https://github.com/laravel/framework/pull/48759 - [10.x] Add runningConsoleCommand(...$commands) method by [@trevorgehman](https://github.com/trevorgehman) in https://github.com/laravel/framework/pull/48751 - [10.x] Update annotations in wrap method to accommodate Collection instances by [@salehhashemi1992](https://github.com/salehhashemi1992) in https://github.com/laravel/framework/pull/48746 - [10.x] Add Tests for Str::replaceMatches Method by [@salehhashemi1992](https://github.com/salehhashemi1992) in https://github.com/laravel/framework/pull/48771 - [10.x] Do not bubble exceptions thrown rendering error view when debug is false (prevent infinite loops) by [@simensen](https://github.com/simensen) in https://github.com/laravel/framework/pull/48732 - [10.x] Correct phpdoc for Grammar::setConnection by [@Neol3108](https://github.com/Neol3108) in https://github.com/laravel/framework/pull/48779 - [10.x] Add `displayName` for queued Artisan commands by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/framework/pull/48778 - [10.x] Test Improvements by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/48797 - [10.x] Make inherited relations and virtual attributes appear in model:show command by [@sebj54](https://github.com/sebj54) in https://github.com/laravel/framework/pull/48800 ## [v10.28.0](https://github.com/laravel/framework/compare/v10.27.0...v10.28.0) - 2023-10-10 - [10.x] Fixed issue: Added a call to the `getValue` method by [@lozobojan](https://github.com/lozobojan) in https://github.com/laravel/framework/pull/48652 - [10.x] Add an example for queue retry range option by [@pionl](https://github.com/pionl) in https://github.com/laravel/framework/pull/48691 - [10.x] Add percentage to be used as High Order Messages by [@WendellAdriel](https://github.com/WendellAdriel) in https://github.com/laravel/framework/pull/48689 - [10.x] Optimize `exists` validation for empty array input by [@mtawil](https://github.com/mtawil) in https://github.com/laravel/framework/pull/48684 ## [v10.27.0](https://github.com/laravel/framework/compare/v10.26.2...v10.27.0) - 2023-10-09 - [10.x] Store blocks after prepare strings by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/48641 - [10.x] throw TransportException instead of Exception in SES mail drivers by [@bchalier](https://github.com/bchalier) in https://github.com/laravel/framework/pull/48645 - [10.x] Fix `Model::replicate()` when using unique keys by [@axlon](https://github.com/axlon) in https://github.com/laravel/framework/pull/48636 - [10.x] Don't crash if replacement cannot be represented as a string by [@GrahamCampbell](https://github.com/GrahamCampbell) in https://github.com/laravel/framework/pull/48530 - [10.x] Extended `pluck()` testcases by [@bert-w](https://github.com/bert-w) in https://github.com/laravel/framework/pull/48657 - [10.x] Fixes `GeneratorCommand` not able to prevent uppercase reserved name such as `__CLASS__` by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/48667 - [10.x] Fix timing sensitive flaky test by [@KentarouTakeda](https://github.com/KentarouTakeda) in https://github.com/laravel/framework/pull/48664 - [10.x] Fixed implementation related to `afterCommit` on Postgres and MSSQL database drivers by [@SakiTakamachi](https://github.com/SakiTakamachi) in https://github.com/laravel/framework/pull/48662 - [10.x] Implement chunkById in descending order by [@cristiancalara](https://github.com/cristiancalara) in https://github.com/laravel/framework/pull/48666 ## [v10.26.2](https://github.com/laravel/framework/compare/v10.26.1...v10.26.2) - 2023-10-03 - Revert "Hint query builder closures (#48562)" by @taylorotwell in https://github.com/laravel/framework/pull/48620 ## [v10.26.1](https://github.com/laravel/framework/compare/v10.26.0...v10.26.1) - 2023-10-03 - [10.x] Fix selection of vendor files after searching by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/framework/pull/48619 ## [v10.26.0](https://github.com/laravel/framework/compare/v10.25.2...v10.26.0) - 2023-10-03 - [10.x] Convert Expression to string for from in having subqueries by @ikari7789 in https://github.com/laravel/framework/pull/48525 - [10.x] Allow searching on `vendor:publish` prompt by @jessarcher in https://github.com/laravel/framework/pull/48586 - [10.x] Enhance Test Coverage for Macroable Trait by @salehhashemi1992 in https://github.com/laravel/framework/pull/48583 - [10.x] Add new SQL error messages by @magnusvin in https://github.com/laravel/framework/pull/48601 - [10.x] Ensure array cache considers milliseconds by @timacdonald in https://github.com/laravel/framework/pull/48573 - [10.x] Prevent `session:table` command from creating duplicates by @jessarcher in https://github.com/laravel/framework/pull/48602 - [10.x] Handle expiration in seconds by @timacdonald in https://github.com/laravel/framework/pull/48600 - [10.x] Avoid duplicate code for create table commands by extending new `Illuminate\Console\MigrationGeneratorCommand` by @crynobone in https://github.com/laravel/framework/pull/48603 - [10.x] Add Closure Type Hinting for Query Builders by @AJenbo in https://github.com/laravel/framework/pull/48562 ## [v10.25.2](https://github.com/laravel/framework/compare/v10.25.1...v10.25.2) - 2023-09-28 - [10.x] Account for new MariaDB platform by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48563 - [10.x] Add Windows fallback for `multisearch` prompt by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/framework/pull/48565 - Revert "[10.x] Fix blade failing to compile when mixing inline/block [@php](https://github.com/php) directives" by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/48575 - [10.x] Added Validation Macro Functionality Tests by [@salehhashemi1992](https://github.com/salehhashemi1992) in https://github.com/laravel/framework/pull/48570 - Revert expiry time changes by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/48576 ## [v10.25.1](https://github.com/laravel/framework/compare/v10.25.0...v10.25.1) - 2023-09-27 - [10.x] Correct parameter type on MakesHttpRequests:followRedirects() by [@AJenbo](https://github.com/AJenbo) in https://github.com/laravel/framework/pull/48557 - [10.x] Fix `firstOrNew` on `HasManyThrough` relations by [@tonysm](https://github.com/tonysm) in https://github.com/laravel/framework/pull/48542 - [10.x] Fix "after commit" callbacks not running on nested transactions using `RefreshDatabase` or `DatabaseMigrations` by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/48523 - [10.x] Use the dedicated key getters in BelongsTo by [@iamgergo](https://github.com/iamgergo) in https://github.com/laravel/framework/pull/48509 - [10.x] Fix undefined constant `STDIN` error with `Artisan::call` during a request by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/framework/pull/48559 ## [v10.25.0](https://github.com/laravel/framework/compare/v10.24.0...v10.25.0) - 2023-09-26 - [10.x] Fix key type in [@return](https://github.com/return) tag of EnumeratesValues::ensure() docblock by [@wimski](https://github.com/wimski) in https://github.com/laravel/framework/pull/48456 - [10.x] Add str()->take($limit) and Str::take($string, $limit) by [@moshe-autoleadstar](https://github.com/moshe-autoleadstar) in https://github.com/laravel/framework/pull/48467 - [10.x] Throttle exceptions by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48391 - [10.x] Fix blade failing to compile when mixing inline/block [@php](https://github.com/php) directives by [@CalebDW](https://github.com/CalebDW) in https://github.com/laravel/framework/pull/48420 - [10.x] Fix test name for stringable position by [@shawnlindstrom](https://github.com/shawnlindstrom) in https://github.com/laravel/framework/pull/48480 - [10.x] Create fluent method convertCase by [@rmunate](https://github.com/rmunate) in https://github.com/laravel/framework/pull/48492 - [10.x] Fix `CanBeOneOfMany` giving erroneous results by [@Guilhem-DELAITRE](https://github.com/Guilhem-DELAITRE) in https://github.com/laravel/framework/pull/47427 - [10.x] Disable autoincrement for unsupported column type by [@ikari7789](https://github.com/ikari7789) in https://github.com/laravel/framework/pull/48501 - [10.x] Increase bcrypt rounds to 12 by [@valorin](https://github.com/valorin) in https://github.com/laravel/framework/pull/48494 - [10.x] Ensure array driver expires values at the expiry time by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48497 - [10.x] Fix typos by [@szepeviktor](https://github.com/szepeviktor) in https://github.com/laravel/framework/pull/48513 - [10.x] Improve tests for `Arr::first` and `Arr::last` by [@tamiroh](https://github.com/tamiroh) in https://github.com/laravel/framework/pull/48511 - [10.x] Set morph type for MorphToMany pivot model by [@gazben](https://github.com/gazben) in https://github.com/laravel/framework/pull/48432 - [10.x] Revert from using `createOrFirst` in other `*OrCreate` methods by [@tonysm](https://github.com/tonysm) in https://github.com/laravel/framework/pull/48531 - [10.x] Fix typos in tests by [@szepeviktor](https://github.com/szepeviktor) in https://github.com/laravel/framework/pull/48534 - [10.x] Adds `updateOrCreate` on HasManyThrough relations regression test by [@tonysm](https://github.com/tonysm) in https://github.com/laravel/framework/pull/48533 - [10.x] Convert exception rate limit to seconds by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48543 - [10.x] Adds the `firstOrCreate` and `createOrFirst` methods to the `HasManyThrough` relation by [@tonysm](https://github.com/tonysm) in https://github.com/laravel/framework/pull/48541 - [10.x] Handle custom extensions when caching views by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48524 - [10.x] Set prompt interactivity mode by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/framework/pull/48468 ## [v10.24.0](https://github.com/laravel/framework/compare/v10.23.1...v10.24.0) - 2023-09-19 - Make types of parameter of join method consistent in the Query Builder by [@melicerte](https://github.com/melicerte) in https://github.com/laravel/framework/pull/48386 - [10.x] Fix file race condition after view:cache and artisan up by [@roxik](https://github.com/roxik) in https://github.com/laravel/framework/pull/48368 - [10.x] Re-enable SQL Server CI by [@GrahamCampbell](https://github.com/GrahamCampbell) in https://github.com/laravel/framework/pull/48393 - Update request.stub by [@olivsinz](https://github.com/olivsinz) in https://github.com/laravel/framework/pull/48402 - [10.x] phpdoc: Auth\Access\Response constructor allows null message by [@snmatsui](https://github.com/snmatsui) in https://github.com/laravel/framework/pull/48394 - [10.x] Test Improvements by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/48390 - Turn off autocomplete for csrf_field by [@maxheckel](https://github.com/maxheckel) in https://github.com/laravel/framework/pull/48371 - [10.x] Remove PHP 8.1 Check for including Enums in Tests by [@Jubeki](https://github.com/Jubeki) in https://github.com/laravel/framework/pull/48415 - [10.x] Improve naming by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48413 - [10.x] Fix "Text file busy" error when call deleteDirectory by [@ycs77](https://github.com/ycs77) in https://github.com/laravel/framework/pull/48422 - Fix Cache::many() with small numeric keys by [@AlexKarpan](https://github.com/AlexKarpan) in https://github.com/laravel/framework/pull/48423 - [10.x] Update actions/checkout from v3 to v4 by [@tamiroh](https://github.com/tamiroh) in https://github.com/laravel/framework/pull/48439 - `lazyById` doesn't check availability of id (alias) column in database response and silently ends up with endless loop. `chunkById` does. by [@decadence](https://github.com/decadence) in https://github.com/laravel/framework/pull/48436 - [10.x] Allow older jobs to be faked by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48434 - [10.x] introduce `Str::substrPos` by [@amacado](https://github.com/amacado) in https://github.com/laravel/framework/pull/48421 - [10.x] Guess table name correctly in migrations if column's name have ('to', 'from' and/or 'in') terms by [@i350](https://github.com/i350) in https://github.com/laravel/framework/pull/48437 - [10.x] Refactored LazyCollection::take() to save memory by [@fuwasegu](https://github.com/fuwasegu) in https://github.com/laravel/framework/pull/48382 - [10.x] Get value attribute when default value is an enum by [@squiaios](https://github.com/squiaios) in https://github.com/laravel/framework/pull/48452 - [10.x] Composer helper improvements by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/48448 - [10.x] Test Symfony v6.4 by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/48400 ## [v10.23.1](https://github.com/laravel/framework/compare/v10.23.0...v10.23.1) - 2023-09-13 - Use PHP native json_validate in isJson function if available by [@jnoordsij](https://github.com/jnoordsij) in https://github.com/laravel/framework/pull/48367 - [10.x] Remove and update a few tearDown methods. by [@lucasmichot](https://github.com/lucasmichot) in https://github.com/laravel/framework/pull/48381 - [10.x] Test Improvements by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/48378 - add "resolve" to `Component::ignoredMethods()` method by [@PH7-Jack](https://github.com/PH7-Jack) in https://github.com/laravel/framework/pull/48373 - [10.x] Add `notModified` method to HTTP client by [@lucasmichot](https://github.com/lucasmichot) in https://github.com/laravel/framework/pull/48379 - [10.x] Update the visibility of setUp and tearDown by [@lucasmichot](https://github.com/lucasmichot) in https://github.com/laravel/framework/pull/48383 - Revert "[10.x] Validate version and variant in `Str::isUuid()`" by [@taylorotwell](https://github.com/taylorotwell) in https://github.com/laravel/framework/pull/48385 ## [v10.23.0](https://github.com/laravel/framework/compare/v10.22.0...v10.23.0) - 2023-09-12 - [10.x] Do not add token to AWS credentials without validating it first by [@mmehmet](https://github.com/mmehmet) in https://github.com/laravel/framework/pull/48297 - [10.x] Add array to docs of `ResponseFactory::redirectToAction` by [@NiclasvanEyk](https://github.com/NiclasvanEyk) in https://github.com/laravel/framework/pull/48309 - [10.x] Deduplicate exceptions by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48288 - [10.x] Change Arr::sortRecursiveDesc() method to static. by [@gkisiel](https://github.com/gkisiel) in https://github.com/laravel/framework/pull/48327 - [10.x] Validate version and variant in `Str::isUuid()` by [@inxilpro](https://github.com/inxilpro) in https://github.com/laravel/framework/pull/48321 - [10.x] Adds `make:view` Artisan command by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/48330 - [10.x] Make ComponentAttributeBag JsonSerializable by [@iamgergo](https://github.com/iamgergo) in https://github.com/laravel/framework/pull/48338 - [10.x] add missing method to message bag class by [@PH7-Jack](https://github.com/PH7-Jack) in https://github.com/laravel/framework/pull/48348 - [10.x] Add newResponse method to PendingRequest by [@denniseilander](https://github.com/denniseilander) in https://github.com/laravel/framework/pull/48344 - [10.x] Add before/after database truncation methods to DatabaseTruncation trait by [@cwilby](https://github.com/cwilby) in https://github.com/laravel/framework/pull/48345 - [10.x] Passthru test options by [@jasonmccreary](https://github.com/jasonmccreary) in https://github.com/laravel/framework/pull/48335 - [10.x] Support for phpredis 6.0.0 by [@stemis](https://github.com/stemis) in https://github.com/laravel/framework/pull/48362 - [10.x] Improve test cases and achieve 100% code coverage by [@sohelrana820](https://github.com/sohelrana820) in https://github.com/laravel/framework/pull/48360 - [10.x] Support for phpredis 6.0.0 by [@stemis](https://github.com/stemis) in https://github.com/laravel/framework/pull/48364 - [10.x] Render mailable inline images by [@pniaps](https://github.com/pniaps) in https://github.com/laravel/framework/pull/48292 ## [v10.22.0](https://github.com/laravel/framework/compare/v10.21.1...v10.22.0) - 2023-09-05 - [10.x] Add ulid testing helpers by [@Jasonej](https://github.com/Jasonej) in https://github.com/laravel/framework/pull/48276 - [10.x] Fix issue with table prefix duplication in DatabaseTruncation trait by [@mobidev86](https://github.com/mobidev86) in https://github.com/laravel/framework/pull/48291 - [10.x] Fixed a typo in phpdoc block by [@back2Lobby](https://github.com/back2Lobby) in https://github.com/laravel/framework/pull/48296 ## [v10.21.1](https://github.com/laravel/framework/compare/v10.21.0...v10.21.1) - 2023-09-04 - [10.x] HotFix: throw captured `UniqueConstraintViolationException` if there are no matching records on `SELECT` retry by [@mpyw](https://github.com/mpyw) in https://github.com/laravel/framework/pull/48234 - [10.x] Adds testing helpers for Precognition by [@peterfox](https://github.com/peterfox) in https://github.com/laravel/framework/pull/48151 - [10.x] GeneratorCommand - Sorting possible models and events by [@TWithers](https://github.com/TWithers) in https://github.com/laravel/framework/pull/48249 - [10.x] Add Enum Support to the In and NotIn Validation Rules by [@geisi](https://github.com/geisi) in https://github.com/laravel/framework/pull/48247 - PHP 8.3 Support by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/48265 - [10.x] Call `renderForAssertions` in all Mailable assertions by [@jamsch](https://github.com/jamsch) in https://github.com/laravel/framework/pull/48254 - [10.x] Introduce `requireEnv` helper by [@lucasmichot](https://github.com/lucasmichot) in https://github.com/laravel/framework/pull/48261 - [10.x] Combine prefix with table for `compileDropPrimary` PostgreSQL by [@dyriavin](https://github.com/dyriavin) in https://github.com/laravel/framework/pull/48268 - [10.x] BelongsToMany Docblock Improvements by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/48282 ## [v10.21.0](https://github.com/laravel/framework/compare/v10.20.0...v10.21.0) - 2023-08-29 - [10.x] Add broadcastAs function at BroadcastNotificationCreated by [@raphaelcangucu](https://github.com/raphaelcangucu) in https://github.com/laravel/framework/pull/48136 - [10.x] Fix `createOrFirst` on transactions by [@tonysm](https://github.com/tonysm) in https://github.com/laravel/framework/pull/48144 - [10.x] Improve `PendingRequest::pool()` return type by [@axlon](https://github.com/axlon) in https://github.com/laravel/framework/pull/48150 - [10.x] Adds start and end string replacement helpers by [@joedixon](https://github.com/joedixon) in https://github.com/laravel/framework/pull/48025 - [10.x] Fix flaky test using microtime by [@tonysm](https://github.com/tonysm) in https://github.com/laravel/framework/pull/48156 - [10.x] Allow failed job providers to be countable by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48177 - [10.x] Change the return type of getPublicToken function by [@fahamjv](https://github.com/fahamjv) in https://github.com/laravel/framework/pull/48173 - [10.x] Fix flakey `HttpClientTest` test by [@joshbonnick](https://github.com/joshbonnick) in https://github.com/laravel/framework/pull/48166 - [10.x] Give access to job UUID in the job queued event by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48179 - [10.x] Add `serializeAndRestore()` to `QueueFake` and`BusFake` by [@cosmastech](https://github.com/cosmastech) in https://github.com/laravel/framework/pull/48131 - Add visibility Support for Scoped Disk Configurations by [@okaufmann](https://github.com/okaufmann) in https://github.com/laravel/framework/pull/48186 - [10.x] Ensuring Primary Reference on Retry in `createOrFirst()` by [@mpyw](https://github.com/mpyw) in https://github.com/laravel/framework/pull/48161 - [10.x] Make the `firstOrCreate` methods in relations use `createOrFirst` behind the scenes by [@tonysm](https://github.com/tonysm) in https://github.com/laravel/framework/pull/48192 - [10.x] Enhancing `updateOrCreate()` to Use `firstOrCreate()` by [@mpyw](https://github.com/mpyw) in https://github.com/laravel/framework/pull/48160 - [10.x] Introduce short-hand "false" syntax for Blade component props by [@ryangjchandler](https://github.com/ryangjchandler) in https://github.com/laravel/framework/pull/48084 - [10.x] Fix validation of attributes that depend on previous excluded attribute by [@hans-thomas](https://github.com/hans-thomas) in https://github.com/laravel/framework/pull/48122 - [10.x] Remove unused `catch` exception variables by [@osbre](https://github.com/osbre) in https://github.com/laravel/framework/pull/48209 - Revert "feature: introduce short hand false syntax for component prop… by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/48220 - [10.x] Return from maintenance middleware early if URL is excluded by [@axlon](https://github.com/axlon) in https://github.com/laravel/framework/pull/48218 - [10.x] Array to string conversion error exception by [@hans-thomas](https://github.com/hans-thomas) in https://github.com/laravel/framework/pull/48219 - [10.x] Migrate to `laravel/facade-documenter` repository by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48223 - Remove unneeded Return type in Docblock of Illuminate\Database\Eloquent\Builder.php by [@FrazerFlanagan](https://github.com/FrazerFlanagan) in https://github.com/laravel/framework/pull/48228 - [10.x] Fix issues with updated_at by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/48230 - [10.x] Use Symfony Response in exception handler by [@thomasschiet](https://github.com/thomasschiet) in https://github.com/laravel/framework/pull/48226 - [10.x] Allow failed jobs to be counted by "connection" and "queue" by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48216 - [10.x] Add method `Str::convertCase` by [@rmunate](https://github.com/rmunate) in https://github.com/laravel/framework/pull/48224 - [10.x] Make the `updateOrCreate` methods in relations use `firstOrCreate` behind the scenes by [@mpyw](https://github.com/mpyw) in https://github.com/laravel/framework/pull/48213 ## [v10.20.0](https://github.com/laravel/framework/compare/v10.19.0...v10.20.0) - 2023-08-22 - [10.x] Allow default values when merging values into a resource by [@axlon](https://github.com/axlon) in https://github.com/laravel/framework/pull/48073 - [10.x] Adds a `createOrFirst` method to Eloquent by [@tonysm](https://github.com/tonysm) in https://github.com/laravel/framework/pull/47973 - [10.x] Allow utilising `withTrashed()`, `withoutTrashed()` and `onlyTrashed()` on `MorphTo` relationship even without `SoftDeletes` Model by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/47880 - [10.x] Mark Request JSON data to be InputBag in docblocks by [@jnoordsij](https://github.com/jnoordsij) in https://github.com/laravel/framework/pull/48085 - [10.x] Markdown Mailables: Allow omitting Footer and Header when customising components by [@jorisnoo](https://github.com/jorisnoo) in https://github.com/laravel/framework/pull/48080 - [10.x] Update EmailVerificationRequest return docblock by [@ahmedash95](https://github.com/ahmedash95) in https://github.com/laravel/framework/pull/48087 - [10.x] Add commonly reusable Composer related commands from 1st party packages by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/48096 - [10.x] Add ability to measure a single callable and get result by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48077 - [10.x] Fixes incorrect method visibility and add unit tests for `Illuminate\Support\Composer` by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/48104 - [10.x] Skip convert empty string to null test by [@hungthai1401](https://github.com/hungthai1401) in https://github.com/laravel/framework/pull/48105 - [10.x] Using complete insert for mysqldump when appending migration dump to schema file by [@emulgeator](https://github.com/emulgeator) in https://github.com/laravel/framework/pull/48126 - [10.x] Add `hasPackage` method to Composer class by [@emargareten](https://github.com/emargareten) in https://github.com/laravel/framework/pull/48124 - [10.x] Add `assertJsonPathCanonicalizing` method by [@gdebrauwer](https://github.com/gdebrauwer) in https://github.com/laravel/framework/pull/48117 - [10.x] Configurable storage path via environment variable by [@sl0wik](https://github.com/sl0wik) in https://github.com/laravel/framework/pull/48115 - [10.x] Support providing subquery as value to `where` builder method by [@gdebrauwer](https://github.com/gdebrauwer) in https://github.com/laravel/framework/pull/48116 - [10.x] Minor Tweaks by [@utsavsomaiya](https://github.com/utsavsomaiya) in https://github.com/laravel/framework/pull/48138 ## [v10.19.0](https://github.com/laravel/framework/compare/v10.18.0...v10.19.0) - 2023-08-15 - [10.x] Fix typo in update `HasUniqueIds` by [@iamcarlos94](https://github.com/iamcarlos94) in https://github.com/laravel/framework/pull/47994 - [10.x] Gracefully handle scientific notation by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/48002 - [10.x] Fix docblocks for throw_if and throw_unless by [@AbdelElrafa](https://github.com/AbdelElrafa) in https://github.com/laravel/framework/pull/48003 - [10.x] Add `wordWrap` to `Str` by [@joshbonnick](https://github.com/joshbonnick) in https://github.com/laravel/framework/pull/48012 - [10.x] Fix RetryBatchCommand overlapping of failed jobs when run concurrently with the same Batch ID using isolatableId by [@rybakihor](https://github.com/rybakihor) in https://github.com/laravel/framework/pull/48000 - [10.x] Fix `assertRedirectToRoute` when route uri is empty by [@khernik93](https://github.com/khernik93) in https://github.com/laravel/framework/pull/48023 - [10.x] Fix empty table displayed when using the --pending option but there are no pending migrations by [@TheBlckbird](https://github.com/TheBlckbird) in https://github.com/laravel/framework/pull/48019 - [10.x] Fix forced use of write DB connection by [@oleksiikhr](https://github.com/oleksiikhr) in https://github.com/laravel/framework/pull/48015 - [10.x] Use model cast when builder created updated at value by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/47942 - [10.x] Fix Collection::search and LazyCollection::search return type by [@bastien-phi](https://github.com/bastien-phi) in https://github.com/laravel/framework/pull/48030 - [10.x] Add ability to customize class resolution in event discovery by [@bastien-phi](https://github.com/bastien-phi) in https://github.com/laravel/framework/pull/48031 - [10.x] Add `percentage` method to Collections by [@WendellAdriel](https://github.com/WendellAdriel) in https://github.com/laravel/framework/pull/48034 - [10.x] Fix parsing error in console when parameter description contains `--` by [@rxrw](https://github.com/rxrw) in https://github.com/laravel/framework/pull/48021 - [10.x] Allow Listeners to dynamically specify delay using `withDelay` by [@CalebDW](https://github.com/CalebDW) in https://github.com/laravel/framework/pull/48026 - [10.x] Add dynamic return types to rescue helper by [@axlon](https://github.com/axlon) in https://github.com/laravel/framework/pull/48062 - [10.x] createMany & createManyQuietly add count argument by [@JHWelch](https://github.com/JHWelch) in https://github.com/laravel/framework/pull/48048 - [10.x] Attributes support on default component slot by [@royduin](https://github.com/royduin) in https://github.com/laravel/framework/pull/48039 - [10.x] Add WithoutRelations attribute for model serialization by [@Neol3108](https://github.com/Neol3108) in https://github.com/laravel/framework/pull/47989 - [10.x] Can apply WithoutRelations to entire class by [@cosmastech](https://github.com/cosmastech) in https://github.com/laravel/framework/pull/48068 - [10.x] createMany & createManyQuietly make argument optional by [@JHWelch](https://github.com/JHWelch) in https://github.com/laravel/framework/pull/48070 ## [v10.18.0](https://github.com/laravel/framework/compare/v17.1...v10.18.0) - 2023-08-08 - [10.x] Allow DatabaseRefreshed event to include given `database` and `seed` options by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/47923 - [10.x] Use generics in `throw_if` and `throw_unless` to indicate dynamic exception type by [@osbre](https://github.com/osbre) in https://github.com/laravel/framework/pull/47938 - [10.x] Fixes artisan about --only should be case insensitive by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/47955 - [10.x] Improve decimal shape validation by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/47954 - docs: update phpdoc in Str helper for remove function by [@squiaios](https://github.com/squiaios) in https://github.com/laravel/framework/pull/47967 - [10.x] Remove return on void callback by [@gonzunigad](https://github.com/gonzunigad) in https://github.com/laravel/framework/pull/47969 - [9.x] Improve decimal shape validation by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/47957 - [10.x] Add `content` method to Vite by [@michael-rubel](https://github.com/michael-rubel) in https://github.com/laravel/framework/pull/47968 - [10.x] Allow empty port in psql schema dump by [@Arzaroth](https://github.com/Arzaroth) in https://github.com/laravel/framework/pull/47988 - [10.x] Show config when the value is false or zero by [@saeedhosseiinii](https://github.com/saeedhosseiinii) in https://github.com/laravel/framework/pull/47987 - [10.x] Add getter for components on IO interaction by [@chris-ware](https://github.com/chris-ware) in https://github.com/laravel/framework/pull/47982 ## [v10.17.1](https://github.com/laravel/framework/compare/v10.17.0...v10.17.1) - 2023-08-02 - [9.x] Back porting #47838 by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/47840 - [9.x] Normalise predis command argument where it maybe an object. by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/47902 - [9.x] Migrate JSON data to shared InputBag by [@ImJustToNy](https://github.com/ImJustToNy) in https://github.com/laravel/framework/pull/47919 - [10.x] Fix docblocks of the dispatchable trait by [@imanghafoori1](https://github.com/imanghafoori1) in https://github.com/laravel/framework/pull/47921 - [9.x] Circumvent PHP 8.2.9 date format bug that makes artisan serve crash by [@levu42](https://github.com/levu42) in https://github.com/laravel/framework/pull/47931 - [10.x] Fix prompt and console component spacing when calling another command by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/framework/pull/47928 - [10.x] Fix prompt rendering after `callSilent` by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/framework/pull/47929 - [10.x] Update ensure() collection method to correctly work with Interfaces and object inheritance by [@karpilin](https://github.com/karpilin) in https://github.com/laravel/framework/pull/47934 ## [v10.17.0](https://github.com/laravel/framework/compare/v10.16.1...v10.17.0) - 2023-08-01 - [10.x] Update `TrustProxies` to rely on `$headers` if properly set by [@inxilpro](https://github.com/inxilpro) in https://github.com/laravel/framework/pull/47844 - [10.x] Accept protocols as argument for URL validation by [@MrMicky-FR](https://github.com/MrMicky-FR) in https://github.com/laravel/framework/pull/47843 - [10.x] Support human-friendly text for file size by [@jxxe](https://github.com/jxxe) in https://github.com/laravel/framework/pull/47846 - [10.x] Added UploadedFile as return type by [@khrigo](https://github.com/khrigo) in https://github.com/laravel/framework/pull/47847 - [10.x] Add option to adjust database default lock timeout by [@joelharkes](https://github.com/joelharkes) in https://github.com/laravel/framework/pull/47854 - [10.x] PHP 8.3 builds by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/47788 - [10.x] Add Collection::enforce() method by [@inxilpro](https://github.com/inxilpro) in https://github.com/laravel/framework/pull/47785 - [10.x] Allow custom mutex names for isolated commands by [@rybakihor](https://github.com/rybakihor) in https://github.com/laravel/framework/pull/47814 - Fix for issues with closure-based scheduled commands in schedule:test by [@mobidev86](https://github.com/mobidev86) in https://github.com/laravel/framework/pull/47862 - [10.x] Extract customised deleted_at column name from Model FQN by [@edvordo](https://github.com/edvordo) in https://github.com/laravel/framework/pull/47873 - [10.x] Adding Minutes Option in Some Frequencies by [@joaopalopes24](https://github.com/joaopalopes24) in https://github.com/laravel/framework/pull/47789 - [10.x] Add `config:show` command by [@xiCO2k](https://github.com/xiCO2k) in https://github.com/laravel/framework/pull/47858 - [10.x] Test Improvements for `hashed` password by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/47904 - [10.x] Use shared facade script by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/47901 - [10.x] Add --test and --pest options to make:component by [@nshiro](https://github.com/nshiro) in https://github.com/laravel/framework/pull/47894 - [10.x] Prompts by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/framework/pull/46772 - [10.x] Migrate JSON data to shared InputBag by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/47914 - [10.x] Fix `Factory::configure()` return type by [@axlon](https://github.com/axlon) in https://github.com/laravel/framework/pull/47920 - [10.x] Fix Http global middleware for queue, octane, and dependency injection by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/47915 ## [v10.16.1](https://github.com/laravel/framework/compare/v10.17.1...v10.16.1) - 2023-07-26 - [10.x] Fix BusFake::assertChained() for a single job by [@gehrisandro](https://github.com/gehrisandro) in https://github.com/laravel/framework/pull/47832 - [10.x] Retain `$request->request` `InputBag` type by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/47838 ## [v10.16.0](https://github.com/laravel/framework/compare/v10.15.0...v10.16.0) - 2023-07-25 - [10.x] Improve display of sub-minute tasks in `schedule:list` command. by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/framework/pull/47720 - [10.x] Add new SQL error message "No connection could be made because the target machine actively refused it" by [@magnusvin](https://github.com/magnusvin) in https://github.com/laravel/framework/pull/47718 - [10.x] Ignore second in HttpRequestTest date comparison by [@kylekatarnls](https://github.com/kylekatarnls) in https://github.com/laravel/framework/pull/47719 - [10.x] Call `renderForAssertions` in `assertHasSubject` by [@ttrig](https://github.com/ttrig) in https://github.com/laravel/framework/pull/47728 - [10.x] We dont want Symfony to catch pcntl signal by [@ChristopheBorcard](https://github.com/ChristopheBorcard) in https://github.com/laravel/framework/pull/47725 - [10.x] Use atomic locks for command mutex by [@Gaitholabi](https://github.com/Gaitholabi) in https://github.com/laravel/framework/pull/47624 - [10.x] Improve typehint for Model::getConnectionResolver() by [@LukeTowers](https://github.com/LukeTowers) in https://github.com/laravel/framework/pull/47749 - [10.x] add getRedisConnection to ThrottleRequestsWithRedis by [@snmatsui](https://github.com/snmatsui) in https://github.com/laravel/framework/pull/47742 - [10.x] Adjusts for Volt by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/47757 - [10.x] Fix sql server paging problems by [@joelharkes](https://github.com/joelharkes) in https://github.com/laravel/framework/pull/47763 - [10.x] Typo type of data by [@hungthai1401](https://github.com/hungthai1401) in https://github.com/laravel/framework/pull/47775 - [10.x] Add missing tests for the `schedule:list` command. by [@xiCO2k](https://github.com/xiCO2k) in https://github.com/laravel/framework/pull/47787 - [10.x] Fix `Str::replace` return type by [@datlechin](https://github.com/datlechin) in https://github.com/laravel/framework/pull/47779 - [10.x] Collection::except() with null returns all by [@pniaps](https://github.com/pniaps) in https://github.com/laravel/framework/pull/47821 - [10.x] fix issue #47727 with wrong return type by [@renky](https://github.com/renky) in https://github.com/laravel/framework/pull/47820 - [10.x] Remove unused variable in `VendorPublishCommand` by [@hungthai1401](https://github.com/hungthai1401) in https://github.com/laravel/framework/pull/47817 - [10.x] Remove unused variable in `MigrateCommand` by [@sangnguyenplus](https://github.com/sangnguyenplus) in https://github.com/laravel/framework/pull/47816 - [10.x] Revert 47763 fix sql server by [@dunhamjared](https://github.com/dunhamjared) in https://github.com/laravel/framework/pull/47792 - [10.x] Add test for Message ID, References and Custom Headers for Mailables by [@alexbowers](https://github.com/alexbowers) in https://github.com/laravel/framework/pull/47791 - [10.x] Add support for `BackedEnum` in Collection `groupBy` method by [@osbre](https://github.com/osbre) in https://github.com/laravel/framework/pull/47823 - [10.x] Support inline disk for scoped driver by [@alexbowers](https://github.com/alexbowers) in https://github.com/laravel/framework/pull/47776 - [10.x] Allowing bind of IPv6 addresses in development server by [@MuriloChianfa](https://github.com/MuriloChianfa) in https://github.com/laravel/framework/pull/47804 - [10.x] Add more info to issue template by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/47828 ## [v10.15.0](https://github.com/laravel/framework/compare/v10.14.1...v10.15.0) - 2023-07-11 - [10.x] Change return type of `getPrivateToken` in AblyBroadcaster by [@milwad](https://github.com/milwad)-dev in https://github.com/laravel/framework/pull/47602 - [10.x] Add toRawSql, dumpRawSql() and ddRawSql() to Query Builders by [@tpetry](https://github.com/tpetry) in https://github.com/laravel/framework/pull/47507 - [10.x] Fix recorderHandler not recording changes made by middleware by [@j3j5](https://github.com/j3j5) in https://github.com/laravel/framework/pull/47614 - Pass queue from Mailable to SendQueuedMailable job by [@Tarpsvo](https://github.com/Tarpsvo) in https://github.com/laravel/framework/pull/47612 - [10.x] Sub-minute Scheduling by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/framework/pull/47279 - [10.x] Fixes failing tests running on DynamoDB Local 2.0.0 by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/47653 - [10.x] Allow password reset callback to modify the result by [@GrahamCampbell](https://github.com/GrahamCampbell) in https://github.com/laravel/framework/pull/47641 - Forget with collections by [@joelbutcher](https://github.com/joelbutcher) in https://github.com/laravel/framework/pull/47637 - [10.x] Do not apply global scopes when incrementing/decrementing an existing model by [@cosmastech](https://github.com/cosmastech) in https://github.com/laravel/framework/pull/47629 - [10.x] Adds inline attachments support for "notifications" markdown mailables by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/47643 - Assertions for counting outgoing mailables by [@jasonmccreary](https://github.com/jasonmccreary) in https://github.com/laravel/framework/pull/47655 - [10.x] Add getRawQueryLog() method by [@fuwasegu](https://github.com/fuwasegu) in https://github.com/laravel/framework/pull/47623 - [10.x] Fix Storage::cloud() return type by [@tattali](https://github.com/tattali) in https://github.com/laravel/framework/pull/47664 - [10.x] Add `isUrl` to the `Str` class and use it from the validator by [@GrahamCampbell](https://github.com/GrahamCampbell) in https://github.com/laravel/framework/pull/47688 - [10.x] Remove unwanted call to include stack traces by [@HazzazBinFaiz](https://github.com/HazzazBinFaiz) in https://github.com/laravel/framework/pull/47687 - [10.x] Make Vite throw a new `ManifestNotFoundException` by [@innocenzi](https://github.com/innocenzi) in https://github.com/laravel/framework/pull/47681 - [10.x] Move class from file logic in Console Kernel to dedicated method by [@CalebDW](https://github.com/CalebDW) in https://github.com/laravel/framework/pull/47665 - [10.x] Dispatch model pruning started and ended events by [@ziadoz](https://github.com/ziadoz) in https://github.com/laravel/framework/pull/47669 - [10.x] Update DatabaseRule to handle Enums for simple where clause by [@CalebDW](https://github.com/CalebDW) in https://github.com/laravel/framework/pull/47679 - [10.x] Add data_remove helper by [@PhiloNL](https://github.com/PhiloNL) in https://github.com/laravel/framework/pull/47618 - [10.x] Added tests for `isUrl` to Str. by [@michaelnabil230](https://github.com/michaelnabil230) in https://github.com/laravel/framework/pull/47690 - [10.x] Added `isUrl` to Stringable. by [@michaelnabil230](https://github.com/michaelnabil230) in https://github.com/laravel/framework/pull/47689 - [10.x] Tweak return type for missing config by [@sfreytag](https://github.com/sfreytag) in https://github.com/laravel/framework/pull/47702 - [10.x] Fix parallel testing without any database connection by [@deleugpn](https://github.com/deleugpn) in https://github.com/laravel/framework/pull/47705 - [10.x] Test Improvements by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/47709 - [10.x] Allows HTTP exceptions to be thrown for views by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/47714 ## [v10.14.1](https://github.com/laravel/framework/compare/v10.14.0...v10.14.1) - 2023-06-28 - [10.x] Fix `Dispatcher::until` return type by @Neol3108 in https://github.com/laravel/framework/pull/47585 - [10.x] Add Collection::wrap to add method on BatchFake by @schonhoff in https://github.com/laravel/framework/pull/47589 - [10.x] Fixes grammar in FoundationServiceProvider by @adampatterson in https://github.com/laravel/framework/pull/47593 - [10.x] Ensure duration is present by @timacdonald in https://github.com/laravel/framework/pull/47596 ## [v10.14.0](https://github.com/laravel/framework/compare/v10.13.5...v10.14.0) - 2023-06-27 - [10.x] Add test for `withCookies` method in RedirectResponse by @milwad-dev in https://github.com/laravel/framework/pull/47383 - [10.x] Add new error message "SSL: Handshake timed out" handling to PDO Dete… by @yehorherasymchuk in https://github.com/laravel/framework/pull/47392 - [10.x] Add new error messages for detecting lost connections by @mfn in https://github.com/laravel/framework/pull/47398 - [10.x] Update phpdoc `except` method in Middleware by @milwad-dev in https://github.com/laravel/framework/pull/47408 - [10.x] Fix inconsistent type hint for `$passwordTimeoutSeconds` by @devfrey in https://github.com/laravel/framework/pull/47414 - Change visibility of `path` method in FileStore.php by @foremtehan in https://github.com/laravel/framework/pull/47413 - [10.x] Fix return type of `buildException` method by @milwad-dev in https://github.com/laravel/framework/pull/47422 - [10.x] Allow serialization of NotificationSent by @cosmastech in https://github.com/laravel/framework/pull/47375 - [10.x] Incorrect comment in `PredisConnector` and `PhpRedisConnector` by @hungthai1401 in https://github.com/laravel/framework/pull/47438 - [10.x] Can set custom Response for denial within `Gate@inspect()` by @cosmastech in https://github.com/laravel/framework/pull/47436 - [10.x] Remove unnecessary param in `addSingletonUpdate` by @milwad-dev in https://github.com/laravel/framework/pull/47446 - [10.x] Fix return type of `prefixedResource` & `prefixedResource` by @milwad-dev in https://github.com/laravel/framework/pull/47445 - [10.x] Add Factory::getNamespace() by @tylernathanreed in https://github.com/laravel/framework/pull/47463 - [10.x] Add `whenAggregated` method to `ConditionallyLoadsAttributes` trait by @akr4m in https://github.com/laravel/framework/pull/47417 - [10.x] Add PendingRequest `withHeader()` method by @ralphjsmit in https://github.com/laravel/framework/pull/47474 - [10.x] Fix $exceptTables to allow an array of table names by @cwilby in https://github.com/laravel/framework/pull/47477 - [10.x] Fix `eachById` on `HasManyThrough` relation by @cristiancalara in https://github.com/laravel/framework/pull/47479 - [10.x] Allow object caching to be disabled for custom class casters by @CalebDW in https://github.com/laravel/framework/pull/47423 - [10.x] "Can" validation rule by @stevebauman in https://github.com/laravel/framework/pull/47371 - [10.x] refactor(Parser.php): Removing the extra "else" statement by @saMahmoudzadeh in https://github.com/laravel/framework/pull/47483 - [10.x] Add `UncompromisedVerifier::class` to `provides()` in `ValidationServiceProvider` by @xurshudyan in https://github.com/laravel/framework/pull/47500 - [9.x] Fix SES V2 Transport "reply to" addresses by @jacobmllr95 in https://github.com/laravel/framework/pull/47522 - [10.x] Reindex appends attributes by @hungthai1401 in https://github.com/laravel/framework/pull/47519 - [10.x] Fix `ListenerMakeCommand` deprecations by @dammy001 in https://github.com/laravel/framework/pull/47517 - [10.x] Add `HandlesPotentiallyTranslatedString` trait by @xurshudyan in https://github.com/laravel/framework/pull/47488 - [10.x] update [JsonResponse]: using match expression instead of if-elseif-else by @saMahmoudzadeh in https://github.com/laravel/framework/pull/47524 - [10.x] Add `withQueryParameters` to the HTTP client by @mnapoli in https://github.com/laravel/framework/pull/47297 - [10.x] Allow `%` symbol in component attribute names by @JayBizzle in https://github.com/laravel/framework/pull/47533 - [10.x] Fix Http client pool return type by @srdante in https://github.com/laravel/framework/pull/47530 - [10.x] Use `match` expression in `resolveSynchronousFake` by @osbre in https://github.com/laravel/framework/pull/47540 - [10.x] Use `match` expression in `compileHaving` by @osbre in https://github.com/laravel/framework/pull/47548 - [10.x] Use `match` expression in `getArrayableItems` by @osbre in https://github.com/laravel/framework/pull/47549 - [10.x] Fix return type in `SessionGuard` by @PerryvanderMeer in https://github.com/laravel/framework/pull/47553 - [10.x] Fix return type in `DatabaseQueue` by @PerryvanderMeer in https://github.com/laravel/framework/pull/47552 - [10.x] Fix return type in `DumpCommand` by @PerryvanderMeer in https://github.com/laravel/framework/pull/47556 - [10.x] Fix return type in `MigrateMakeCommand` by @PerryvanderMeer in https://github.com/laravel/framework/pull/47557 - [10.x] Add missing return to `Factory` by @PerryvanderMeer in https://github.com/laravel/framework/pull/47559 - [10.x] Update doc in Eloquent model by @alirezasalehizadeh in https://github.com/laravel/framework/pull/47562 - [10.x] Fix return types by @PerryvanderMeer in https://github.com/laravel/framework/pull/47561 - [10.x] Fix PHPDoc throw type by @fernandokbs in https://github.com/laravel/framework/pull/47566 - [10.x] Add hasAny function to ComponentAttributeBag, Allow multiple keys in has function by @indykoning in https://github.com/laravel/framework/pull/47569 - [10.x] Ensure captured time is in configured timezone by @timacdonald in https://github.com/laravel/framework/pull/47567 - [10.x] Add Method to Report only logged exceptions by @joelharkes in https://github.com/laravel/framework/pull/47554 - [10.x] Add global middleware to `Http` client by @timacdonald in https://github.com/laravel/framework/pull/47525 - [9.x] Fixes unable to use `trans()->has()` on JSON language files. by @crynobone in https://github.com/laravel/framework/pull/47582 ## [v10.13.5](https://github.com/laravel/framework/compare/v10.13.3...v10.13.5) - 2023-06-08 - Revert "[10.x] Update Kernel::load() to use same `classFromFile` logic as events" by @taylorotwell in https://github.com/laravel/framework/pull/47382 ## [v10.13.3](https://github.com/laravel/framework/compare/v10.13.2...v10.13.3) - 2023-06-08 ### What's Changed - Narrow down array type for `$attributes` in `CastsAttributes` by @devfrey in https://github.com/laravel/framework/pull/47365 - Add test for `assertViewHasAll` method by @milwad-dev in https://github.com/laravel/framework/pull/47366 - Fix `schedule:list` to display named Jobs by @liamkeily in https://github.com/laravel/framework/pull/47367 - Support `ConditionalRules` within `NestedRules` by @cosmastech in https://github.com/laravel/framework/pull/47344 - Small test fixes by @stevebauman in https://github.com/laravel/framework/pull/47369 - Pluralisation typo in queue:clear command output by @sebsobseb in https://github.com/laravel/framework/pull/47376 - Add getForeignKeyFrom method by @iamgergo in https://github.com/laravel/framework/pull/47378 - Add shouldHashKeys to ThrottleRequests middleware by @fosron in https://github.com/laravel/framework/pull/47368 ## [v10.13.2 (2023-06-05)](https://github.com/laravel/framework/compare/v10.13.1...v10.13.2) ### Added - Added `Illuminate/Http/Client/PendingRequest::replaceHeaders()` ([#47335](https://github.com/laravel/framework/pull/47335)) - Added `Illuminate/Notifications/Messages/MailMessage::attachMany()` ([#47345](https://github.com/laravel/framework/pull/47345)) ### Reverted - Revert "[10.x] Remove session on authenticatable deletion v2" ([#47354](https://github.com/laravel/framework/pull/47354)) ### Fixed - Fixes usage of Redis::many() with empty array ([#47307](https://github.com/laravel/framework/pull/47307)) - Fix mapped renderable exception handling ([#47347](https://github.com/laravel/framework/pull/47347)) - Avoid duplicates in fillable/guarded on merge in Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php ([#47351](https://github.com/laravel/framework/pull/47351)) ### Changed - Update Kernel::load() to use same classFromFile logic as events ([#47327](https://github.com/laravel/framework/pull/47327)) - Remove redundant 'setAccessible' methods ([#47348](https://github.com/laravel/framework/pull/47348)) ## [v10.13.1 (2023-06-02)](https://github.com/laravel/framework/compare/v10.13.0...v10.13.1) ### Added - Added `Illuminate\Contracts\Database\Query\ConditionExpression` interface and functional for this ([#47210](https://github.com/laravel/framework/pull/47210)) - Added return type for `Illuminate/Notifications/Channels/MailChannel::send()` ([#47310](https://github.com/laravel/framework/pull/47310)) ### Reverted - Revert "[10.x] Fix inconsistency between report and render methods" ([#47326](https://github.com/laravel/framework/pull/47326)) ### Changed - Display queue runtime in human readable format ([#47227](https://github.com/laravel/framework/pull/47227)) ## [v10.13.0 (2023-05-30)](https://github.com/laravel/framework/compare/v10.12.0...v10.13.0) ### Added - Added `Illuminate/Hashing/HashManager::isHashed()` ([#47197](https://github.com/laravel/framework/pull/47197)) - Escaping functionality within the Grammar ([#46558](https://github.com/laravel/framework/pull/46558)) - Provide testing hooks in `Illuminate/Support/Sleep.php` ([#47228](https://github.com/laravel/framework/pull/47228)) - Added missing methods to AssertsStatusCodes ([#47277](https://github.com/laravel/framework/pull/47277)) - Wrap response preparation in events ([#47229](https://github.com/laravel/framework/pull/47229)) ### Fixed - Fixed bug when function wrapped around definition of related factory ([#47168](https://github.com/laravel/framework/pull/47168)) - Fixed inconsistency between report and render methods ([#47201](https://github.com/laravel/framework/pull/47201)) - Fixes Model::isDirty() when AsCollection or AsEncryptedCollection have arguments ([#47235](https://github.com/laravel/framework/pull/47235)) - Fixed escaped String for JSON_CONTAINS ([#47244](https://github.com/laravel/framework/pull/47244)) - Fixes missing output on ProcessFailedException exception ([#47285](https://github.com/laravel/framework/pull/47285)) ### Changed - Remove useless else statements ([#47186](https://github.com/laravel/framework/pull/47186)) - RedisStore improvement - don't open transaction unless all values are serialaizable ([#47193](https://github.com/laravel/framework/pull/47193)) - Use carbon::now() to get current timestamp in takeUntilTimeout lazycollection-method ([#47200](https://github.com/laravel/framework/pull/47200)) - Avoid duplicates in visible/hidden on merge ([#47264](https://github.com/laravel/framework/pull/47264)) - Add a missing semicolon to CompilesClasses ([#47280](https://github.com/laravel/framework/pull/47280)) - Send along value to InvalidPayloadException ([#47223](https://github.com/laravel/framework/pull/47223)) ## [v10.12.0 (2023-05-23)](https://github.com/laravel/framework/compare/v10.11.0...v10.12.0) ### Added - Added `Illuminate/Queue/Events/JobTimedOut.php` ([#47068](https://github.com/laravel/framework/pull/47068)) - Added `when()` and `unless()` methods to `Illuminate/Support/Sleep` ([#47114](https://github.com/laravel/framework/pull/47114)) - Adds inline attachments support for markdown mailables ([#47140](https://github.com/laravel/framework/pull/47140)) - Added `Illuminate/Testing/Concerns/AssertsStatusCodes::assertMethodNotAllowed()` ([#47169](https://github.com/laravel/framework/pull/47169)) - Added `forceCreateQuietly` method ([#47162](https://github.com/laravel/framework/pull/47162)) - Added parameters to timezone validation rule ([#47171](https://github.com/laravel/framework/pull/47171)) ### Fixed - Fixes singleton and api singletons creatable|destryoable|only|except combinations ([#47098](https://github.com/laravel/framework/pull/47098)) - Don't use empty key or secret for DynamoDBClient ([#47144](https://github.com/laravel/framework/pull/47144)) ### Changed - Remove session on authenticatable deletion ([#47141](https://github.com/laravel/framework/pull/47141)) - Added error handling and ensure re-enabling of foreign key constraints in `Illuminate/Database/Schema/Builder::withoutForeignKeyConstraints()` ([#47182](https://github.com/laravel/framework/pull/47182)) ### Refactoring - Remove useless else statements ([#47161](https://github.com/laravel/framework/pull/47161)) ## [v10.11.0 (2023-05-16)](https://github.com/laravel/framework/compare/v10.10.1...v10.11.0) ### Added - Added the ability to extend the generic types for DatabaseNotificationCollection ([#47048](https://github.com/laravel/framework/pull/47048)) - Added `/Illuminate/Support/Carbon::createFromId()` ([#47046](https://github.com/laravel/framework/pull/47046)) - Added Name attributes on slots ([#47065](https://github.com/laravel/framework/pull/47065)) - Added Precognition-Success header ([#47081](https://github.com/laravel/framework/pull/47081)) - Added Macroable trait to Sleep class ([#47099](https://github.com/laravel/framework/pull/47099)) ### Fixed - Fixed `Illuminate/Database/Console/ShowModelCommand::getPolicy()` ([#47043](https://github.com/laravel/framework/pull/47043)) ### Changed - Remove return from channelRoutes method ([#47059](https://github.com/laravel/framework/pull/47059)) - Bug in `Illuminate/Database/Migrations/Migrator::reset()` with string path ([#47047](https://github.com/laravel/framework/pull/47047)) - Unify logic around cursor paginate ([#47094](https://github.com/laravel/framework/pull/47094)) - Clears resolved instance of Vite when using withoutVite ([#47091](https://github.com/laravel/framework/pull/47091)) - Remove workarounds for old Guzzle versions ([#47084](https://github.com/laravel/framework/pull/47084)) ## [v10.10.1 (2023-05-11)](https://github.com/laravel/framework/compare/v10.10.0...v10.10.1) ### Added - Added `/Illuminate/Collections/Arr::mapWithKeys()` ([#47000](https://github.com/laravel/framework/pull/47000)) - Added `dd` and `dump` methods to `Illuminate/Support/Carbon.php` ([#47002](https://github.com/laravel/framework/pull/47002)) - Added `Illuminate/Queue/Failed/FileFailedJobProvider` ([#47007](https://github.com/laravel/framework/pull/47007)) - Added arguments to the signed middleware to ignore properties ([#46987](https://github.com/laravel/framework/pull/46987)) ### Fixed - Added keys length check to prevent mget error in `Illuminate/Cache/RedisStore::many()` ([#46998](https://github.com/laravel/framework/pull/46998)) - 'hashed' cast - do not rehash already hashed value ([#47029](https://github.com/laravel/framework/pull/47029)) ### Changed - Used `Carbon::now()` instead of `now()` ([#47017](https://github.com/laravel/framework/pull/47017)) - Use file locks when writing failed jobs to disk ([b822d28](https://github.com/laravel/framework/commit/b822d2810d29ab1aedf667abc76ed969d28bbaf5)) - Raise visibility of Mailable prepareMailableForDelivery() ([#47031](https://github.com/laravel/framework/pull/47031)) ## [v10.10.0 (2023-05-09)](https://github.com/laravel/framework/compare/v10.9.0...v10.10.0) ### Added - Added `$isolated` and `isolatedExitCode` properties to `Illuminate/Console/Command` ([#46925](https://github.com/laravel/framework/pull/46925)) - Added ability to restore/set Global Scopes ([#46922](https://github.com/laravel/framework/pull/46922)) - Added `Illuminate/Collections/Arr::sortRecursiveDesc()` ([#46945](https://github.com/laravel/framework/pull/46945)) - Added `Illuminate/Support/Sleep` ([#46904](https://github.com/laravel/framework/pull/46904), [#46963](https://github.com/laravel/framework/pull/46963)) - Added `Illuminate/Database/Eloquent/Concerns/HasAttributes::castAttributeAsHashedString()` ([#46947]https://github.com/laravel/framework/pull/46947) - Added url support for mail config ([#46964](https://github.com/laravel/framework/pull/46964)) ### Fixed - Fixed replace missing_unless ([89ac58a](https://github.com/laravel/framework/commit/89ac58aa9b4fb7ef9f3b2290921488da1454ed30)) - Gracefully handle invalid code points in e() ([#46914](https://github.com/laravel/framework/pull/46914)) - HasCasts returning false instead of true ([#46992](https://github.com/laravel/framework/pull/46992)) ### Changed - Use method on UploadedFile to validate image dimensions ([#46912](https://github.com/laravel/framework/pull/46912)) - Expose Js::json() helper ([#46935](https://github.com/laravel/framework/pull/46935)) - Respect parents on middleware priority ([#46972](https://github.com/laravel/framework/pull/46972)) - Do reconnect when redis throws connection lost error ([#46989](https://github.com/laravel/framework/pull/46989)) - Throw timeoutException instead of maxAttemptsExceededException when a job times out ([#46968](https://github.com/laravel/framework/pull/46968)) ## [v10.9.0 (2023-04-25)](https://github.com/laravel/framework/compare/v10.8.0...v10.9.0) ### Added - Add new HTTP status assertions ([#46841](https://github.com/laravel/framework/pull/46841)) - Allow pruning all cancelled and unfinished queue batches ([#46833](https://github.com/laravel/framework/pull/46833)) - Added `IGNITION_LOCAL_SITES_PATH` to `$passthroughVariables` in `ServeCommand.php` ([#46857](https://github.com/laravel/framework/pull/46857)) - Added named static methods for middleware ([#46362](https://github.com/laravel/framework/pull/46362)) ### Fixed - Fix date_format rule throw ValueError ([#46824](https://github.com/laravel/framework/pull/46824)) ### Changed - Allow separate directory for locks on filestore ([#46811](https://github.com/laravel/framework/pull/46811)) - Allow to whereMorphedTo work with null model ([#46821](https://github.com/laravel/framework/pull/46821)) - Use pivot model fromDateTime instead of assuming Carbon in `Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable::addTimestampsToAttachment()` ([#46822](https://github.com/laravel/framework/pull/46822)) - Make rules method in FormRequest optional ([#46846](https://github.com/laravel/framework/pull/46846)) - Throw LogicException when calling FileFactory@image() if mimetype is not supported ([#46859](https://github.com/laravel/framework/pull/46859)) - Improve job release method to accept date instance ([#46854](https://github.com/laravel/framework/pull/46854)) - Use foreignUlid if model uses HasUlids trait when call foreignIdFor ([#46876](https://github.com/laravel/framework/pull/46876)) ## [v10.8.0 (2023-04-18)](https://github.com/laravel/framework/compare/v10.7.1...v10.8.0) ### Added - Added syntax sugar to the Process::pipe method ([#46745](https://github.com/laravel/framework/pull/46745)) - Allow specifying index name when calling ForeignIdColumnDefinition@constrained() ([#46746](https://github.com/laravel/framework/pull/46746)) - Allow to customise redirect URL in AuthenticateSession Middleware ([#46752](https://github.com/laravel/framework/pull/46752)) - Added Class based after validation rules ([#46757](https://github.com/laravel/framework/pull/46757)) - Added max exceptions to broadcast event ([#46800](https://github.com/laravel/framework/pull/46800)) ### Fixed - Fixed compiled view file ends with .php ([#46755](https://github.com/laravel/framework/pull/46755)) - Fix validation rule names ([#46768](https://github.com/laravel/framework/pull/46768)) - Fixed validateDecimal() ([#46809](https://github.com/laravel/framework/pull/46809)) ### Changed - Add headers to exception in `Illuminate/Foundation/Application::abourd()` ([#46780](https://github.com/laravel/framework/pull/46780)) - Minor skeleton slimming (framework edition) ([#46786](https://github.com/laravel/framework/pull/46786)) - Release lock for job implementing ShouldBeUnique that is dispatched afterResponse() ([#46806](https://github.com/laravel/framework/pull/46806)) ## [v10.7.1 (2023-04-11)](https://github.com/laravel/framework/compare/v10.7.0...v10.7.1) ### Changed - Changed `Illuminate/Process/Factory::pipe()` method. It will be run pipes immediately ([e34ab39](https://github.com/laravel/framework/commit/e34ab392800bfc175334c90e9321caa7261c2d65)) ## [v10.7.0 (2023-04-11)](https://github.com/laravel/framework/compare/v10.6.2...v10.7.0) ### Added - Allow `Illuminate/Foundation/Testing/WithFaker` to be used when app is not bound ([#46529](https://github.com/laravel/framework/pull/46529)) - Allow Event::assertListening to check for invokable event listeners ([#46683](https://github.com/laravel/framework/pull/46683)) - Added `Illuminate/Process/Factory::pipe()` ([#46527](https://github.com/laravel/framework/pull/46527)) - Added `Illuminate/Validation/Validator::setValue` ([#46716](https://github.com/laravel/framework/pull/46716)) ### Fixed - PHP 8.0 fix for Closure jobs ([#46505](https://github.com/laravel/framework/pull/46505)) - Fix preg_split error when there is a slash in the attribute in `Illuminate/Validation/ValidationData` ([#46549](https://github.com/laravel/framework/pull/46549)) - Fixed Cache::spy incompatibility with Cache::get ([#46689](https://github.com/laravel/framework/pull/46689)) - server command: Fixed server Closing output on invalid $requestPort ([#46726](https://github.com/laravel/framework/pull/46726)) - Fix nested join when not JoinClause instance ([#46712](https://github.com/laravel/framework/pull/46712)) - Fix query builder whereBetween method with carbon date period ([#46720](https://github.com/laravel/framework/pull/46720)) ### Changed - Removes unnecessary parameters in `creatable()` / `destroyable()` methods in `Illuminate/Routing/PendingSingletonResourceRegistration` ([#46677](https://github.com/laravel/framework/pull/46677)) - Return non-zero exit code for uncaught exceptions ([#46541](https://github.com/laravel/framework/pull/46541)) ## [v10.6.2 (2023-04-05)](https://github.com/laravel/framework/compare/v10.6.1...v10.6.2) ### Added - Added trait `Illuminate/Foundation/Testing/WithConsoleEvents` ([#46694](https://github.com/laravel/framework/pull/46694)) ### Changed - Added missing ignored methods to `Illuminate/View/Component` ([#46692](https://github.com/laravel/framework/pull/46692)) - console.stub: remove void return type from handle ([#46697](https://github.com/laravel/framework/pull/46697)) ## [v10.6.1 (2023-04-04)](https://github.com/laravel/framework/compare/v10.6.0...v10.6.1) ### Reverted - Reverted ["Set container instance on session manager"Set container instance on session manager](https://github.com/laravel/framework/pull/46621) ([#46691](https://github.com/laravel/framework/pull/46691)) ## [v10.6.0 (2023-04-04)](https://github.com/laravel/framework/compare/v10.5.1...v10.6.0) ### Added - Added ability to set a custom class for the AsCollection and AsEncryptedCollection casts ([#46619](https://github.com/laravel/framework/pull/46619)) ### Changed - Set container instance on session manager ([#46621](https://github.com/laravel/framework/pull/46621)) - Added empty string definition to Str::squish function ([#46660](https://github.com/laravel/framework/pull/46660)) - Allow $sleepMilliseconds parameter receive a Closure in retry method from PendingRequest ([#46653](https://github.com/laravel/framework/pull/46653)) - Support contextual binding on first class callables ([de8d515](https://github.com/laravel/framework/commit/de8d515fc6d1fabc8f14450342554e0eb67df725), [e511a3b](https://github.com/laravel/framework/commit/e511a3bdb15c294866428b4fe665a4ad14540038)) ## [v10.5.1 (2023-03-29)](https://github.com/laravel/framework/compare/v10.5.0...v10.5.1) ### Added - Added methods to determine if API resource has pivot loaded ([#46555](https://github.com/laravel/framework/pull/46555)) - Added caseSensitive flag to Stringable replace function ([#46578](https://github.com/laravel/framework/pull/46578)) - Allow insert..select (insertUsing()) to have empty $columns ([#46605](https://github.com/laravel/framework/pull/46605), [399bff9](https://github.com/laravel/framework/commit/399bff9331252e64a3439ea43e05f87f901dad55)) - Added `Illuminate/Database/Connection::selectResultSets()` ([#46592](https://github.com/laravel/framework/pull/46592)) ### Changed - Make sure pivot model has previously defined values ([#46559](https://github.com/laravel/framework/pull/46559)) - Move SetUniqueIds to run before the creating event ([#46622](https://github.com/laravel/framework/pull/46622)) ## [v10.5.0 (2023-03-28)](https://github.com/laravel/framework/compare/v10.4.1...v10.5.0) ### Added - Added `Illuminate/Cache/CacheManager::setApplication()` ([#46594](https://github.com/laravel/framework/pull/46594)) ### Fixed - Fix infinite loading on batches list on Horizon ([#46536](https://github.com/laravel/framework/pull/46536)) - Fix whereNull queries with raw expressions for the MySql grammar ([#46538](https://github.com/laravel/framework/pull/46538)) - Fix getDirty method when using AsEnumArrayObject / AsEnumCollection ([#46561](https://github.com/laravel/framework/pull/46561)) ### Changed - Skip `Illuminate/Support/Reflector::isParameterBackedEnumWithStringBackingType` for non ReflectionNamedType ([#46511](https://github.com/laravel/framework/pull/46511)) - Replace Deprecated DBAL Comparator creation with schema aware Comparator ([#46517](https://github.com/laravel/framework/pull/46517)) - Added Storage::json() method to read and decode a json file ([#46548](https://github.com/laravel/framework/pull/46548)) - Force cast json decoded failed_job_ids to array in DatabaseBatchRepository ([#46581](https://github.com/laravel/framework/pull/46581)) - Handle empty arrays for DynamoDbStore multi-key operations ([#46579](https://github.com/laravel/framework/pull/46579)) - Stop adding constraints twice on *Many to *One relationships via one() ([#46575](https://github.com/laravel/framework/pull/46575)) - allow override of the Builder paginate() total ([#46415](https://github.com/laravel/framework/pull/46415)) - Add a possibility to set a custom on_stats function for the Http Facade ([#46569](https://github.com/laravel/framework/pull/46569)) ## [v10.4.1 (2023-03-18)](https://github.com/laravel/framework/compare/v10.4.0...v10.4.1) ### Changed - Move Symfony events dispatcher registration to Console\Kernel ([#46508](https://github.com/laravel/framework/pull/46508)) ## [v10.4.0 (2023-03-17)](https://github.com/laravel/framework/compare/v10.3.3...v10.4.0) ### Added - Added `Illuminate/Testing/Concerns/AssertsStatusCodes::assertUnsupportedMediaType()` ([#46426](https://github.com/laravel/framework/pull/46426)) - Added curl_error_code: 77 to DetectsLostConnections ([#46429](https://github.com/laravel/framework/pull/46429)) - Allow for converting a HasMany to HasOne && MorphMany to MorphOne ([#46443](https://github.com/laravel/framework/pull/46443)) - Add option to create macroable method for paginationInformation ([#46461](https://github.com/laravel/framework/pull/46461)) - Added `Illuminate/Filesystem/Filesystem::json()` ([#46481](https://github.com/laravel/framework/pull/46481)) ### Fixed - Fix parsed input arguments for command events using dispatcher rerouting ([#46442](https://github.com/laravel/framework/pull/46442)) - Fix enums uses with optional implicit parameters ([#46483](https://github.com/laravel/framework/pull/46483)) - Fix deprecations for embedded images in symfony mailer ([#46488](https://github.com/laravel/framework/pull/46488)) ### Changed - Added alternative database port in Postgres DSN ([#46403](https://github.com/laravel/framework/pull/46403)) - Allow calling getControllerClass on closure-based routes ([#46411](https://github.com/laravel/framework/pull/46411)) - Remove obsolete method_exists(ReflectionClass::class, 'isEnum') call ([#46445](https://github.com/laravel/framework/pull/46445)) - Convert eloquent builder to base builder in whereExists ([#46460](https://github.com/laravel/framework/pull/46460)) - Refactor shared static methodExcludedByOptions method to trait ([#46498](https://github.com/laravel/framework/pull/46498)) ## [v10.3.3 (2023-03-09)](https://github.com/laravel/framework/compare/v10.3.2...v10.3.3) ### Reverted - Reverted ["Allow override of the Builder paginate() total"](https://github.com/laravel/framework/pull/46336) ([#46406](https://github.com/laravel/framework/pull/46406)) ## [v10.3.2 (2023-03-08)](https://github.com/laravel/framework/compare/v10.3.1...v10.3.2) ### Reverted - Reverted ["FIX on CanBeOneOfMany trait giving erroneous results"](https://github.com/laravel/framework/pull/46309) ([#46402](https://github.com/laravel/framework/pull/46402)) ### Fixed - Fixes Expression no longer implements Stringable ([#46395](https://github.com/laravel/framework/pull/46395)) ## [v10.3.1 (2023-03-08)](https://github.com/laravel/framework/compare/v10.3.0...v10.3.1) ### Reverted - Reverted ["Use fallback when previous URL is the same as the current in `Illuminate/Routing/UrlGenerator::previous()`"](https://github.com/laravel/framework/pull/46234) ([#46392](https://github.com/laravel/framework/pull/46392)) ## [v10.3.0 (2023-03-07)](https://github.com/laravel/framework/compare/v10.2.0...v10.3.0) ### Added - Adding Pipeline Facade ([#46271](https://github.com/laravel/framework/pull/46271)) - Add Support for SaveQuietly and Upsert with UUID/ULID Primary Keys ([#46161](https://github.com/laravel/framework/pull/46161)) - Add charAt method to both Str and Stringable ([#46349](https://github.com/laravel/framework/pull/46349), [dfb59bc2](https://github.com/laravel/framework/commit/dfb59bc263a4e28ac8992deeabd2ccd9392d1681)) - Adds Countable to the InvokedProcessPool class ([#46346](https://github.com/laravel/framework/pull/46346)) - Add processors to logging (placeholders) ([#46344](https://github.com/laravel/framework/pull/46344)) ### Fixed - Fixed `Illuminate/Mail/Mailable::buildMarkdownView()` ([791f8ea7](https://github.com/laravel/framework/commit/791f8ea70b5872ae4483a32f6aeb28dd2ed4b8d7)) - FIX on CanBeOneOfMany trait giving erroneous results ([#46309](https://github.com/laravel/framework/pull/46309)) ### Changed - Use fallback when previous URL is the same as the current in `Illuminate/Routing/UrlGenerator::previous()` ([#46234](https://github.com/laravel/framework/pull/46234)) - Allow override of the Builder paginate() total ([#46336](https://github.com/laravel/framework/pull/46336)) ## [v10.2.0 (2023-03-02)](https://github.com/laravel/framework/compare/v10.1.5...v10.2.0) ### Added - Adding `Conditionable` train to Logger ([#46259](https://github.com/laravel/framework/pull/46259)) - Added "dot" method to Illuminate\Support\Collection class ([#46265](https://github.com/laravel/framework/pull/46265)) - Added a "channel:list" command ([#46248](https://github.com/laravel/framework/pull/46248)) - Added JobPopping and JobPopped events ([#46220](https://github.com/laravel/framework/pull/46220)) - Add isMatch method to Str and Stringable helpers ([#46303](https://github.com/laravel/framework/pull/46303)) - Add ArrayAccess to Stringable ([#46279](https://github.com/laravel/framework/pull/46279)) ### Reverted - Revert "[10.x] Fix custom themes not resetting on Markdown renderer" ([#46328](https://github.com/laravel/framework/pull/46328)) ### Fixed - Fix typo in function `createMissingSqliteDatbase` name in `src/Illuminate/Database/Console/Migrations/MigrateCommand.php` ([#46326](https://github.com/laravel/framework/pull/46326)) ### Changed - Generate default command name based on class name in `ConsoleMakeCommand` ([#46256](https://github.com/laravel/framework/pull/46256)) - Do not mutate underlying values on redirect ([#46281](https://github.com/laravel/framework/pull/46281)) - Do not use null to initialise $lastExecutionStartedAt in `ScheduleWorkCommand` ([#46285](https://github.com/laravel/framework/pull/46285)) - Remove obsolete function_exists('enum_exists') calls ([#46319](https://github.com/laravel/framework/pull/46319)) - Cast json decoded failed_job_ids to array in DatabaseBatchRepository::toBatch ([#46329](https://github.com/laravel/framework/pull/46329)) ## [v10.1.5 (2023-02-24)](https://github.com/laravel/framework/compare/v10.1.4...v10.1.5) ### Fixed - Fixed `Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase::expectsDatabaseQueryCount()` $connection parameter ([#46228](https://github.com/laravel/framework/pull/46228)) - Fixed Facade Fake ([#46257](https://github.com/laravel/framework/pull/46257)) ### Changed - Remove autoload dumping from make:migration ([#46215](https://github.com/laravel/framework/pull/46215)) ## [v10.1.4 (2023-02-23)](https://github.com/laravel/framework/compare/v10.1.3...v10.1.4) ### Changed - Improve Facade Fake Awareness ([#46188](https://github.com/laravel/framework/pull/46188), [#46232](https://github.com/laravel/framework/pull/46232)) ## [v10.1.3 (2023-02-22)](https://github.com/laravel/framework/compare/v10.1.2...v10.1.3) ### Added - Added protected method `Illuminate/Http/Resources/Json/JsonResource::newCollection()` for simplifies collection customisation ([#46217](https://github.com/laravel/framework/pull/46217)) ### Fixed - Fixes constructable migrations ([#46223](https://github.com/laravel/framework/pull/46223)) ### Changes - Accept time when generating ULID in `Str::ulid()` ([#46201](https://github.com/laravel/framework/pull/46201)) ## [v10.1.2 (2023-02-22)](https://github.com/laravel/framework/compare/v10.1.1...v10.1.2) ### Reverted - Revert changes from `Arr::random()` ([cf3eb90](https://github.com/laravel/framework/commit/cf3eb90a6473444bb7a78d1a3af1e9312a62020d)) ## [v10.1.1 (2023-02-21)](https://github.com/laravel/framework/compare/v10.1.0...v10.1.1) ### Added - Add the ability to re-resolve cache drivers ([#46203](https://github.com/laravel/framework/pull/46203)) ### Fixed - Fixed `Illuminate/Collections/Arr::shuffle()` for empty array ([0c6cae0](https://github.com/laravel/framework/commit/0c6cae0ef647158b9554cad05ff39db7e7ad0d33)) ## [v10.1.0 (2023-02-21)](https://github.com/laravel/framework/compare/v10.0.3...v10.1.0) ### Fixed - Fixing issue where 0 is discarded as a valid timestamp ([#46158](https://github.com/laravel/framework/pull/46158)) - Fix custom themes not resetting on Markdown renderer ([#46200](https://github.com/laravel/framework/pull/46200)) ### Changed - Use secure randomness in Arr:random and Arr:shuffle ([#46105](https://github.com/laravel/framework/pull/46105)) - Use mixed return type on controller stubs ([#46166](https://github.com/laravel/framework/pull/46166)) - Use InteractsWithDictionary in Eloquent collection ([#46196](https://github.com/laravel/framework/pull/46196)) ## [v10.0.3 (2023-02-17)](https://github.com/laravel/framework/compare/v10.0.2...v10.0.3) ### Added - Added missing expression support for pluck in Builder ([#46146](https://github.com/laravel/framework/pull/46146)) ## [v10.0.2 (2023-02-16)](https://github.com/laravel/framework/compare/v10.0.1...v10.0.2) ### Added - Register policies automatically to the gate ([#46132](https://github.com/laravel/framework/pull/46132)) ## [v10.0.1 (2023-02-16)](https://github.com/laravel/framework/compare/v10.0.0...v10.0.1) ### Added - Standard Input can be applied to PendingProcess ([#46119](https://github.com/laravel/framework/pull/46119)) ### Fixed - Fix Expression string casting ([#46137](https://github.com/laravel/framework/pull/46137)) ### Changed - Add AddQueuedCookiesToResponse to middlewarePriority so it is handled in the right place ([#46130](https://github.com/laravel/framework/pull/46130)) - Show queue connection in MonitorCommand ([#46122](https://github.com/laravel/framework/pull/46122)) ## [v10.0.0 (2023-02-14)](https://github.com/laravel/framework/compare/v10.0.0...10.x) Please consult the [upgrade guide](https://laravel.com/docs/10.x/upgrade) and [release notes](https://laravel.com/docs/10.x/releases) in the official Laravel documentation.